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