slmisc.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright (c) 1992, 1999, 2001, 2002, 2003 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. #define _GNU_SOURCE
  10. #include "slinclud.h"
  11. #include "slang.h"
  12. #include "_slang.h"
  13. const int SLang_Version = SLANG_VERSION;
  14. /* p and ch may point to the same buffer */
  15. char *_SLexpand_escaped_char(char *p, char *ch)
  16. {
  17. int i = 0;
  18. int max = 0, num, base = 0;
  19. char ch1;
  20. ch1 = *p++;
  21. switch (ch1)
  22. {
  23. default: num = ch1; break;
  24. case 'n': num = '\n'; break;
  25. case 't': num = '\t'; break;
  26. case 'v': num = '\v'; break;
  27. case 'b': num = '\b'; break;
  28. case 'r': num = '\r'; break;
  29. case 'f': num = '\f'; break;
  30. case 'E': case 'e': num = 27; break;
  31. case 'a': num = 7;
  32. break;
  33. /* octal */
  34. case '0': case '1': case '2': case '3':
  35. case '4': case '5': case '6': case '7':
  36. max = '7';
  37. base = 8; i = 2; num = ch1 - '0';
  38. break;
  39. case 'd': /* decimal -- S-Lang extension */
  40. base = 10;
  41. i = 3;
  42. max = '9';
  43. num = 0;
  44. break;
  45. case 'x': /* hex */
  46. base = 16;
  47. max = '9';
  48. i = 2;
  49. num = 0;
  50. break;
  51. }
  52. while (i--)
  53. {
  54. ch1 = *p;
  55. if ((ch1 <= max) && (ch1 >= '0'))
  56. {
  57. num = base * num + (ch1 - '0');
  58. }
  59. else if (base == 16)
  60. {
  61. ch1 |= 0x20;
  62. if ((ch1 < 'a') || ((ch1 > 'f'))) break;
  63. num = base * num + 10 + (ch1 - 'a');
  64. }
  65. else break;
  66. p++;
  67. }
  68. *ch = (char) num;
  69. return p;
  70. }