1 1.1 cherry /* $NetBSD: interp_backslash.c,v 1.1 2006/04/07 14:21:29 cherry Exp $ */ 2 1.1 cherry 3 1.1 cherry /*- 4 1.1 cherry * Redistribution and use in source and binary forms, with or without 5 1.1 cherry * modification, are permitted provided that the following conditions 6 1.1 cherry * are met: 7 1.1 cherry * 1. Redistributions of source code must retain the above copyright 8 1.1 cherry * notice, this list of conditions and the following disclaimer. 9 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright 10 1.1 cherry * notice, this list of conditions and the following disclaimer in the 11 1.1 cherry * documentation and/or other materials provided with the distribution. 12 1.1 cherry * 13 1.1 cherry * Jordan K. Hubbard 14 1.1 cherry * 29 August 1998 15 1.1 cherry * 16 1.1 cherry * Routine for doing backslash elimination. 17 1.1 cherry */ 18 1.1 cherry 19 1.1 cherry #include <sys/cdefs.h> 20 1.1 cherry 21 1.1 cherry #include <lib/libsa/stand.h> 22 1.1 cherry #include <lib/libkern/libkern.h> 23 1.1 cherry 24 1.1 cherry #include "bootstrap.h" 25 1.1 cherry 26 1.1 cherry #define DIGIT(x) (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 27 1.1 cherry 28 1.1 cherry /* 29 1.1 cherry * backslash: Return malloc'd copy of str with all standard "backslash 30 1.1 cherry * processing" done on it. Original can be free'd if desired. 31 1.1 cherry */ 32 1.1 cherry char * 33 1.1 cherry backslash(char *str) 34 1.1 cherry { 35 1.1 cherry /* 36 1.1 cherry * Remove backslashes from the strings. Turn \040 etc. into a single 37 1.1 cherry * character (we allow eight bit values). Currently NUL is not 38 1.1 cherry * allowed. 39 1.1 cherry * 40 1.1 cherry * Turn "\n" and "\t" into '\n' and '\t' characters. Etc. 41 1.1 cherry * 42 1.1 cherry */ 43 1.1 cherry char *new_str; 44 1.1 cherry int seenbs = 0; 45 1.1 cherry int i = 0; 46 1.1 cherry 47 1.1 cherry if ((new_str = strdup(str)) == NULL) 48 1.1 cherry return NULL; 49 1.1 cherry 50 1.1 cherry while (*str) { 51 1.1 cherry if (seenbs) { 52 1.1 cherry seenbs = 0; 53 1.1 cherry switch (*str) { 54 1.1 cherry case '\\': 55 1.1 cherry new_str[i++] = '\\'; 56 1.1 cherry str++; 57 1.1 cherry break; 58 1.1 cherry 59 1.1 cherry /* preserve backslashed quotes, dollar signs */ 60 1.1 cherry case '\'': 61 1.1 cherry case '"': 62 1.1 cherry case '$': 63 1.1 cherry new_str[i++] = '\\'; 64 1.1 cherry new_str[i++] = *str++; 65 1.1 cherry break; 66 1.1 cherry 67 1.1 cherry case 'b': 68 1.1 cherry new_str[i++] = '\b'; 69 1.1 cherry str++; 70 1.1 cherry break; 71 1.1 cherry 72 1.1 cherry case 'f': 73 1.1 cherry new_str[i++] = '\f'; 74 1.1 cherry str++; 75 1.1 cherry break; 76 1.1 cherry 77 1.1 cherry case 'r': 78 1.1 cherry new_str[i++] = '\r'; 79 1.1 cherry str++; 80 1.1 cherry break; 81 1.1 cherry 82 1.1 cherry case 'n': 83 1.1 cherry new_str[i++] = '\n'; 84 1.1 cherry str++; 85 1.1 cherry break; 86 1.1 cherry 87 1.1 cherry case 's': 88 1.1 cherry new_str[i++] = ' '; 89 1.1 cherry str++; 90 1.1 cherry break; 91 1.1 cherry 92 1.1 cherry case 't': 93 1.1 cherry new_str[i++] = '\t'; 94 1.1 cherry str++; 95 1.1 cherry break; 96 1.1 cherry 97 1.1 cherry case 'v': 98 1.1 cherry new_str[i++] = '\13'; 99 1.1 cherry str++; 100 1.1 cherry break; 101 1.1 cherry 102 1.1 cherry case 'z': 103 1.1 cherry str++; 104 1.1 cherry break; 105 1.1 cherry 106 1.1 cherry case '0': case '1': case '2': case '3': case '4': 107 1.1 cherry case '5': case '6': case '7': case '8': case '9': { 108 1.1 cherry char val; 109 1.1 cherry 110 1.1 cherry /* Three digit octal constant? */ 111 1.1 cherry if (*str >= '0' && *str <= '3' && 112 1.1 cherry *(str + 1) >= '0' && *(str + 1) <= '7' && 113 1.1 cherry *(str + 2) >= '0' && *(str + 2) <= '7') { 114 1.1 cherry 115 1.1 cherry val = (DIGIT(*str) << 6) + (DIGIT(*(str + 1)) << 3) + 116 1.1 cherry DIGIT(*(str + 2)); 117 1.1 cherry 118 1.1 cherry /* Allow null value if user really wants to shoot 119 1.1 cherry at feet, but beware! */ 120 1.1 cherry new_str[i++] = val; 121 1.1 cherry str += 3; 122 1.1 cherry break; 123 1.1 cherry } 124 1.1 cherry 125 1.1 cherry /* One or two digit hex constant? 126 1.1 cherry * If two are there they will both be taken. 127 1.1 cherry * Use \z to split them up if this is not wanted. 128 1.1 cherry */ 129 1.1 cherry if (*str == '0' && 130 1.1 cherry (*(str + 1) == 'x' || *(str + 1) == 'X') && 131 1.1 cherry isxdigit(*(str + 2))) { 132 1.1 cherry val = DIGIT(*(str + 2)); 133 1.1 cherry if (isxdigit(*(str + 3))) { 134 1.1 cherry val = (val << 4) + DIGIT(*(str + 3)); 135 1.1 cherry str += 4; 136 1.1 cherry } 137 1.1 cherry else 138 1.1 cherry str += 3; 139 1.1 cherry /* Yep, allow null value here too */ 140 1.1 cherry new_str[i++] = val; 141 1.1 cherry break; 142 1.1 cherry } 143 1.1 cherry } 144 1.1 cherry break; 145 1.1 cherry 146 1.1 cherry default: 147 1.1 cherry new_str[i++] = *str++; 148 1.1 cherry break; 149 1.1 cherry } 150 1.1 cherry } 151 1.1 cherry else { 152 1.1 cherry if (*str == '\\') { 153 1.1 cherry seenbs = 1; 154 1.1 cherry str++; 155 1.1 cherry } 156 1.1 cherry else 157 1.1 cherry new_str[i++] = *str++; 158 1.1 cherry } 159 1.1 cherry } 160 1.1 cherry 161 1.1 cherry if (seenbs) { 162 1.1 cherry /* 163 1.1 cherry * The final character was a '\'. Put it in as a single backslash. 164 1.1 cherry */ 165 1.1 cherry new_str[i++] = '\\'; 166 1.1 cherry } 167 1.1 cherry new_str[i] = '\0'; 168 1.1 cherry return new_str; 169 1.1 cherry } 170