Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ansiesc.c 3.4KB

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