2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
5 * 1. Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * 2. Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
14 * Routine for doing backslash elimination.
17 #include <sys/cdefs.h>
20 #include "bootstrap.h"
22 #define DIGIT(x) (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
25 * backslash: Return malloc'd copy of str with all standard "backslash
26 * processing" done on it. Original can be free'd if desired.
29 backslash(const char *str)
32 * Remove backslashes from the strings. Turn \040 etc. into a single
33 * character (we allow eight bit values). Currently NUL is not
36 * Turn "\n" and "\t" into '\n' and '\t' characters. Etc.
43 if ((new_str = strdup(str)) == NULL)
55 /* preserve backslashed quotes, dollar signs */
60 new_str[i++] = *str++;
102 case '0': case '1': case '2': case '3': case '4':
103 case '5': case '6': case '7': case '8': case '9': {
106 /* Three digit octal constant? */
107 if (*str >= '0' && *str <= '3' &&
108 *(str + 1) >= '0' && *(str + 1) <= '7' &&
109 *(str + 2) >= '0' && *(str + 2) <= '7') {
111 val = (DIGIT(*str) << 6) + (DIGIT(*(str + 1)) << 3) +
114 /* Allow null value if user really wants to shoot
115 at feet, but beware! */
121 /* One or two digit hex constant?
122 * If two are there they will both be taken.
123 * Use \z to split them up if this is not wanted.
126 (*(str + 1) == 'x' || *(str + 1) == 'X') &&
127 isxdigit(*(str + 2))) {
128 val = DIGIT(*(str + 2));
129 if (isxdigit(*(str + 3))) {
130 val = (val << 4) + DIGIT(*(str + 3));
135 /* Yep, allow null value here too */
143 new_str[i++] = *str++;
153 new_str[i++] = *str++;
159 * The final character was a '\'. Put it in as a single backslash.