You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ansiesc.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <string.h>
  19. #include <assert.h>
  20. #include <gpxe/ansiesc.h>
  21. /** @file
  22. *
  23. * ANSI escape sequences
  24. *
  25. */
  26. /**
  27. * Call ANSI escape sequence handler
  28. *
  29. * @v handlers List of escape sequence handlers
  30. * @v function Control function identifier
  31. * @v count Parameter count
  32. * @v params Parameter list
  33. */
  34. static void ansiesc_call_handler ( struct ansiesc_handler *handlers,
  35. unsigned int function, int count,
  36. int params[] ) {
  37. struct ansiesc_handler *handler;
  38. for ( handler = handlers ; handler->function ; handler++ ) {
  39. if ( handler->function == function ) {
  40. handler->handle ( count, params );
  41. break;
  42. }
  43. }
  44. }
  45. /**
  46. * Process character that may be part of ANSI escape sequence
  47. *
  48. * @v ctx ANSI escape sequence context
  49. * @v c Character
  50. * @ret c Original character if not part of escape sequence
  51. * @ret <0 Character was part of escape sequence
  52. *
  53. * ANSI escape sequences will be plucked out of the character stream
  54. * and interpreted; once complete they will be passed to the
  55. * appropriate handler if one exists in this ANSI escape sequence
  56. * context.
  57. *
  58. * In the interests of code size, we are rather liberal about the
  59. * sequences we are prepared to accept as valid.
  60. */
  61. int ansiesc_process ( struct ansiesc_context *ctx, int c ) {
  62. if ( ctx->count == 0 ) {
  63. if ( c == ESC ) {
  64. /* First byte of CSI : begin escape sequence */
  65. ctx->count = 1;
  66. memset ( ctx->params, 0xff, sizeof ( ctx->params ) );
  67. ctx->function = 0;
  68. return -1;
  69. } else {
  70. /* Normal character */
  71. return c;
  72. }
  73. } else {
  74. if ( c == '[' ) {
  75. /* Second byte of CSI : do nothing */
  76. } else if ( ( c >= '0' ) && ( c <= '9' ) ) {
  77. /* Parameter Byte : part of a parameter value */
  78. int *param = &ctx->params[ctx->count - 1];
  79. if ( *param < 0 )
  80. *param = 0;
  81. *param = ( ( *param * 10 ) + ( c - '0' ) );
  82. } else if ( c == ';' ) {
  83. /* Parameter Byte : parameter delimiter */
  84. ctx->count++;
  85. if ( ctx->count > ( sizeof ( ctx->params ) /
  86. sizeof ( ctx->params[0] ) ) ) {
  87. /* Excessive parameters : abort sequence */
  88. ctx->count = 0;
  89. DBG ( "Too many parameters in ANSI escape "
  90. "sequence\n" );
  91. }
  92. } else if ( ( c >= 0x20 ) && ( c <= 0x2f ) ) {
  93. /* Intermediate Byte */
  94. ctx->function <<= 8;
  95. ctx->function |= c;
  96. } else {
  97. /* Treat as Final Byte. Zero ctx->count before
  98. * calling handler to avoid potential infinite loops.
  99. */
  100. int count = ctx->count;
  101. ctx->count = 0;
  102. ctx->function <<= 8;
  103. ctx->function |= c;
  104. ansiesc_call_handler ( ctx->handlers, ctx->function,
  105. count, ctx->params );
  106. }
  107. return -1;
  108. }
  109. }