paul@181 | 1 | /* |
paul@181 | 2 | * cstring.c -- parse and print strings using the C escape sequences |
paul@181 | 3 | */ |
paul@181 | 4 | |
paul@181 | 5 | #include "config.h" |
paul@181 | 6 | #include <stdio.h> |
paul@181 | 7 | #include <stdlib.h> |
paul@181 | 8 | #include <ctype.h> |
paul@181 | 9 | #ifdef HAVE_GETOPT_H |
paul@181 | 10 | #include <getopt.h> |
paul@181 | 11 | #endif |
paul@181 | 12 | #include <string.h> |
paul@181 | 13 | |
paul@181 | 14 | #include "cstring.h" |
paul@181 | 15 | |
paul@181 | 16 | int parse_c_string(char *str) |
paul@181 | 17 | { |
paul@181 | 18 | char *to, *from, ch; |
paul@181 | 19 | int v; |
paul@181 | 20 | |
paul@181 | 21 | to = from = str; |
paul@181 | 22 | |
paul@181 | 23 | for (to = from = (char *) str; |
paul@181 | 24 | *from && *from != '"'; to++, from++) { |
paul@181 | 25 | if (*from == '\\') { |
paul@181 | 26 | ch = *(++from); |
paul@181 | 27 | switch (ch) { |
paul@181 | 28 | case 'a': |
paul@181 | 29 | *to = '\a'; |
paul@181 | 30 | break; |
paul@181 | 31 | case 'b': |
paul@181 | 32 | *to = '\b'; |
paul@181 | 33 | break; |
paul@181 | 34 | case 'f': |
paul@181 | 35 | *to = '\f'; |
paul@181 | 36 | break; |
paul@181 | 37 | case 'n': |
paul@181 | 38 | *to = '\n'; |
paul@181 | 39 | break; |
paul@181 | 40 | case 't': |
paul@181 | 41 | *to = '\t'; |
paul@181 | 42 | break; |
paul@181 | 43 | case 'v': |
paul@181 | 44 | *to = '\v'; |
paul@181 | 45 | break; |
paul@181 | 46 | case 'x': |
paul@181 | 47 | ch = *(from + 1); |
paul@181 | 48 | if (ch >= 'a' && ch <= 'f') |
paul@181 | 49 | ch = ch - 'a' + 'A'; |
paul@181 | 50 | if (ch >= '0' && ch <= '9') |
paul@181 | 51 | v = ch - '0'; |
paul@181 | 52 | else if (ch >= 'A' && ch <= 'F') |
paul@181 | 53 | v = ch + 10 - 'A'; |
paul@181 | 54 | else { |
paul@181 | 55 | *to = 'x'; |
paul@181 | 56 | break; |
paul@181 | 57 | } |
paul@181 | 58 | from++; |
paul@181 | 59 | ch = *(from + 1); |
paul@181 | 60 | if (ch >= 'a' && ch <= 'f') |
paul@181 | 61 | ch = ch - 'a' + 'A'; |
paul@181 | 62 | if (ch >= '0' && ch <= '9') |
paul@181 | 63 | v = (v * 16) + (ch - '0'); |
paul@181 | 64 | else if (ch >= 'A' && ch <= 'F') |
paul@181 | 65 | v = (v * 16) + (ch + 10 - 'A'); |
paul@181 | 66 | else { |
paul@181 | 67 | *to = 'x'; |
paul@181 | 68 | from--; |
paul@181 | 69 | break; |
paul@181 | 70 | } |
paul@181 | 71 | from++; |
paul@181 | 72 | *to = v; |
paul@181 | 73 | break; |
paul@181 | 74 | default: |
paul@181 | 75 | if (ch >= '0' && ch <= '9') { |
paul@181 | 76 | v = ch - '0'; |
paul@181 | 77 | ch = *(from + 1); |
paul@181 | 78 | if (ch >= '0' && ch <= '9') { |
paul@181 | 79 | from++; |
paul@181 | 80 | v = (8 * v) + (ch - '0'); |
paul@181 | 81 | ch = *(from + 1); |
paul@181 | 82 | if (ch >= '0' && ch <= '9') { |
paul@181 | 83 | from++; |
paul@181 | 84 | v = (8 * v) + (ch - '0'); |
paul@181 | 85 | } |
paul@181 | 86 | } |
paul@181 | 87 | ch = v; |
paul@181 | 88 | } |
paul@181 | 89 | *to = ch; |
paul@181 | 90 | } |
paul@181 | 91 | continue; |
paul@181 | 92 | } |
paul@181 | 93 | *to = *from; |
paul@181 | 94 | } |
paul@181 | 95 | *to = '\0'; |
paul@181 | 96 | return to - (char *) str; |
paul@181 | 97 | } |
paul@181 | 98 | |
paul@181 | 99 | void print_c_string(FILE *f, const char *cp, int len) |
paul@181 | 100 | { |
paul@181 | 101 | unsigned char ch; |
paul@181 | 102 | |
paul@181 | 103 | if (len < 0) |
paul@181 | 104 | len = strlen(cp); |
paul@181 | 105 | |
paul@181 | 106 | while (len--) { |
paul@181 | 107 | ch = *cp++; |
paul@181 | 108 | if (ch == '\a') |
paul@181 | 109 | fputs("\\a", f); |
paul@181 | 110 | else if (ch == '\b') |
paul@181 | 111 | fputs("\\b", f); |
paul@181 | 112 | else if (ch == '\f') |
paul@181 | 113 | fputs("\\f", f); |
paul@181 | 114 | else if (ch == '\n') |
paul@181 | 115 | fputs("\\n", f); |
paul@181 | 116 | else if (ch == '\t') |
paul@181 | 117 | fputs("\\t", f); |
paul@181 | 118 | else if (ch == '\v') |
paul@181 | 119 | fputs("\\v", f); |
paul@181 | 120 | else if (ch == '\\') |
paul@181 | 121 | fputs("\\\\", f); |
paul@181 | 122 | else if (ch == '\'') |
paul@181 | 123 | fputs("\\\'", f); |
paul@181 | 124 | else if (ch == '\"') |
paul@181 | 125 | fputs("\\\"", f); |
paul@181 | 126 | else if ((ch < 32) || (ch > 126)) |
paul@181 | 127 | fprintf(f, "\\%03o", ch); |
paul@181 | 128 | else |
paul@181 | 129 | fputc(ch, f); |
paul@181 | 130 | } |
paul@181 | 131 | } |
paul@181 | 132 | |
paul@181 | 133 | #ifdef DEBUG_PROGRAM |
paul@181 | 134 | int main(int argc, char **argv) |
paul@181 | 135 | { |
paul@181 | 136 | char buf[4096]; |
paul@181 | 137 | int c, raw = 0; |
paul@181 | 138 | |
paul@181 | 139 | while ((c = getopt(argc, argv, "r")) != EOF) { |
paul@181 | 140 | switch (c) { |
paul@181 | 141 | case 'r': |
paul@181 | 142 | raw++; |
paul@181 | 143 | break; |
paul@181 | 144 | default: |
paul@181 | 145 | fprintf(stderr, "Usage: %s [-r]\n", argv[0]); |
paul@181 | 146 | exit(1); |
paul@181 | 147 | } |
paul@181 | 148 | } |
paul@181 | 149 | |
paul@181 | 150 | while (!feof(stdin)) { |
paul@181 | 151 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
paul@181 | 152 | break; |
paul@181 | 153 | c = parse_c_string(buf); |
paul@181 | 154 | if (raw) |
paul@181 | 155 | fputs(buf, stdout); |
paul@181 | 156 | else { |
paul@181 | 157 | print_c_string(stdout, buf, c); |
paul@181 | 158 | printf(" <%d>\n", c); |
paul@181 | 159 | } |
paul@181 | 160 | } |
paul@181 | 161 | } |
paul@181 | 162 | #endif |