Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ansiesc.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <gpxe/ansiesc.h>
  22. /** @file
  23. *
  24. * ANSI escape sequences
  25. *
  26. */
  27. /**
  28. * Call ANSI escape sequence handler
  29. *
  30. * @v handlers List of escape sequence handlers
  31. * @v function Control function identifier
  32. * @v count Parameter count
  33. * @v params Parameter list
  34. */
  35. static void ansiesc_call_handler ( struct ansiesc_handler *handlers,
  36. unsigned int function, int count,
  37. int params[] ) {
  38. struct ansiesc_handler *handler;
  39. for ( handler = handlers ; handler->function ; handler++ ) {
  40. if ( handler->function == function ) {
  41. handler->handle ( count, params );
  42. break;
  43. }
  44. }
  45. }
  46. /**
  47. * Process character that may be part of ANSI escape sequence
  48. *
  49. * @v ctx ANSI escape sequence context
  50. * @v c Character
  51. * @ret c Original character if not part of escape sequence
  52. * @ret <0 Character was part of escape sequence
  53. *
  54. * ANSI escape sequences will be plucked out of the character stream
  55. * and interpreted; once complete they will be passed to the
  56. * appropriate handler if one exists in this ANSI escape sequence
  57. * context.
  58. *
  59. * In the interests of code size, we are rather liberal about the
  60. * sequences we are prepared to accept as valid.
  61. */
  62. int ansiesc_process ( struct ansiesc_context *ctx, int c ) {
  63. if ( ctx->count == 0 ) {
  64. if ( c == ESC ) {
  65. /* First byte of CSI : begin escape sequence */
  66. ctx->count = 1;
  67. memset ( ctx->params, 0xff, sizeof ( ctx->params ) );
  68. ctx->function = 0;
  69. return -1;
  70. } else {
  71. /* Normal character */
  72. return c;
  73. }
  74. } else {
  75. if ( c == '[' ) {
  76. /* Second byte of CSI : do nothing */
  77. } else if ( ( c >= '0' ) && ( c <= '9' ) ) {
  78. /* Parameter Byte : part of a parameter value */
  79. int *param = &ctx->params[ctx->count - 1];
  80. if ( *param < 0 )
  81. *param = 0;
  82. *param = ( ( *param * 10 ) + ( c - '0' ) );
  83. } else if ( c == ';' ) {
  84. /* Parameter Byte : parameter delimiter */
  85. ctx->count++;
  86. if ( ctx->count > ( sizeof ( ctx->params ) /
  87. sizeof ( ctx->params[0] ) ) ) {
  88. /* Excessive parameters : abort sequence */
  89. ctx->count = 0;
  90. DBG ( "Too many parameters in ANSI escape "
  91. "sequence\n" );
  92. }
  93. } else if ( ( c >= 0x20 ) && ( c <= 0x2f ) ) {
  94. /* Intermediate Byte */
  95. ctx->function <<= 8;
  96. ctx->function |= c;
  97. } else {
  98. /* Treat as Final Byte. Zero ctx->count before
  99. * calling handler to avoid potential infinite loops.
  100. */
  101. int count = ctx->count;
  102. ctx->count = 0;
  103. ctx->function <<= 8;
  104. ctx->function |= c;
  105. ansiesc_call_handler ( ctx->handlers, ctx->function,
  106. count, ctx->params );
  107. }
  108. return -1;
  109. }
  110. }