3
0

process_escape_sequence.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
  6. * and Vladimir Oleynik <dzo@simtreas.ru>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <limits.h>
  26. #include <ctype.h>
  27. #include "libbb.h"
  28. #define WANT_HEX_ESCAPES 1
  29. /* Usual "this only works for ascii compatible encodings" disclaimer. */
  30. #undef _tolower
  31. #define _tolower(X) ((X)|((char) 0x20))
  32. char bb_process_escape_sequence(const char **ptr)
  33. {
  34. static const char charmap[] = {
  35. 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
  36. '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
  37. const char *p;
  38. const char *q;
  39. unsigned int num_digits;
  40. unsigned int r;
  41. unsigned int n;
  42. unsigned int d;
  43. unsigned int base;
  44. num_digits = n = 0;
  45. base = 8;
  46. q = *ptr;
  47. #ifdef WANT_HEX_ESCAPES
  48. if (*q == 'x') {
  49. ++q;
  50. base = 16;
  51. ++num_digits;
  52. }
  53. #endif
  54. do {
  55. d = (unsigned int)(*q - '0');
  56. #ifdef WANT_HEX_ESCAPES
  57. if (d >= 10) {
  58. d = ((unsigned int)(_tolower(*q) - 'a')) + 10;
  59. }
  60. #endif
  61. if (d >= base) {
  62. #ifdef WANT_HEX_ESCAPES
  63. if ((base == 16) && (!--num_digits)) {
  64. /* return '\\'; */
  65. --q;
  66. }
  67. #endif
  68. break;
  69. }
  70. r = n * base + d;
  71. if (r > UCHAR_MAX) {
  72. break;
  73. }
  74. n = r;
  75. ++q;
  76. } while (++num_digits < 3);
  77. if (num_digits == 0) { /* mnemonic escape sequence? */
  78. p = charmap;
  79. do {
  80. if (*p == *q) {
  81. q++;
  82. break;
  83. }
  84. } while (*++p);
  85. n = *(p+(sizeof(charmap)/2));
  86. }
  87. *ptr = q;
  88. return (char) n;
  89. }
  90. /* END CODE */
  91. /*
  92. Local Variables:
  93. c-file-style: "linux"
  94. c-basic-offset: 4
  95. tab-width: 4
  96. End:
  97. */