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