slmisc.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (c) 1992, 1999, 2001, 2002 John E. Davis
  2. * This file is part of the S-Lang library.
  3. *
  4. * Trimmed down for use in GNU Midnight Commander.
  5. *
  6. * You may distribute under the terms of either the GNU General Public
  7. * License or the Perl Artistic License.
  8. */
  9. #include "slinclud.h"
  10. #include "slang.h"
  11. #include "_slang.h"
  12. /* p and ch may point to the same buffer */
  13. char *_SLexpand_escaped_char(char *p, char *ch)
  14. {
  15. int i = 0;
  16. int max = 0, num, base = 0;
  17. char ch1;
  18. ch1 = *p++;
  19. switch (ch1)
  20. {
  21. default: num = ch1; break;
  22. case 'n': num = '\n'; break;
  23. case 't': num = '\t'; break;
  24. case 'v': num = '\v'; break;
  25. case 'b': num = '\b'; break;
  26. case 'r': num = '\r'; break;
  27. case 'f': num = '\f'; break;
  28. case 'E': case 'e': num = 27; break;
  29. case 'a': num = 7;
  30. break;
  31. /* octal */
  32. case '0': case '1': case '2': case '3':
  33. case '4': case '5': case '6': case '7':
  34. max = '7';
  35. base = 8; i = 2; num = ch1 - '0';
  36. break;
  37. case 'd': /* decimal -- S-Lang extension */
  38. base = 10;
  39. i = 3;
  40. max = '9';
  41. num = 0;
  42. break;
  43. case 'x': /* hex */
  44. base = 16;
  45. max = '9';
  46. i = 2;
  47. num = 0;
  48. break;
  49. }
  50. while (i--)
  51. {
  52. ch1 = *p;
  53. if ((ch1 <= max) && (ch1 >= '0'))
  54. {
  55. num = base * num + (ch1 - '0');
  56. }
  57. else if (base == 16)
  58. {
  59. ch1 |= 0x20;
  60. if ((ch1 < 'a') || ((ch1 > 'f'))) break;
  61. num = base * num + 10 + (ch1 - 'a');
  62. }
  63. else break;
  64. p++;
  65. }
  66. *ch = (char) num;
  67. return p;
  68. }