ctrlparse.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Parse a ^C style character specification.
  3. * Returns NULL in `next' if we didn't recognise it as a control character,
  4. * in which case `c' should be ignored.
  5. * The precise current parsing is an oddity inherited from the terminal
  6. * answerback-string parsing code. All sequences start with ^; all except
  7. * ^<123> are two characters. The ones that are worth keeping are probably:
  8. * ^? 127
  9. * ^@A-Z[\]^_ 0-31
  10. * a-z 1-26
  11. * <num> specified by number (decimal, 0octal, 0xHEX)
  12. * ~ ^ escape
  13. */
  14. #include <stdlib.h>
  15. #include "defs.h"
  16. #include "misc.h"
  17. char ctrlparse(char *s, char **next)
  18. {
  19. char c = 0;
  20. if (*s != '^') {
  21. *next = NULL;
  22. } else {
  23. s++;
  24. if (*s == '\0') {
  25. *next = NULL;
  26. } else if (*s == '<') {
  27. s++;
  28. c = (char)strtol(s, next, 0);
  29. if ((*next == s) || (**next != '>')) {
  30. c = 0;
  31. *next = NULL;
  32. } else
  33. (*next)++;
  34. } else if (*s >= 'a' && *s <= 'z') {
  35. c = (*s - ('a' - 1));
  36. *next = s+1;
  37. } else if ((*s >= '@' && *s <= '_') || *s == '?' || (*s & 0x80)) {
  38. c = ('@' ^ *s);
  39. *next = s+1;
  40. } else if (*s == '~') {
  41. c = '^';
  42. *next = s+1;
  43. }
  44. }
  45. return c;
  46. }