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