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