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.4KB

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