1 /* $NetBSD: cook.c,v 1.1.1.1 2009/12/13 16:57:18 kardel Exp $ */ 2 3 /* 4 * Id: 3da9a5fc88c904673b3b95d0c9667b2bcbccfc80 5 * Time-stamp: "2007-11-16 22:49:11 bkorb" 6 * 7 * This file contains the routines that deal with processing quoted strings 8 * into an internal format. 9 * 10 * This file is part of AutoOpts, a companion to AutoGen. 11 * AutoOpts is free software. 12 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved 13 * 14 * AutoOpts is available under any one of two licenses. The license 15 * in use must be one of these two and the choice is under the control 16 * of the user of the license. 17 * 18 * The GNU Lesser General Public License, version 3 or later 19 * See the files "COPYING.lgplv3" and "COPYING.gplv3" 20 * 21 * The Modified Berkeley Software Distribution License 22 * See the file "COPYING.mbsd" 23 * 24 * These files have the following md5sums: 25 * 26 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3 27 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3 28 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd 29 */ 30 31 /* = = = START-STATIC-FORWARD = = = */ 32 /* static forward declarations maintained by mk-fwd */ 33 /* = = = END-STATIC-FORWARD = = = */ 34 35 /*=export_func ao_string_cook_escape_char 36 * private: 37 * 38 * what: escape-process a string fragment 39 * arg: + char const* + pzScan + points to character after the escape + 40 * arg: + char* + pRes + Where to put the result byte + 41 * arg: + unsigned int + nl_ch + replacement char if scanned char is \n + 42 * 43 * ret-type: unsigned int 44 * ret-desc: The number of bytes consumed processing the escaped character. 45 * 46 * doc: 47 * 48 * This function converts "t" into "\t" and all your other favorite 49 * escapes, including numeric ones: hex and ocatal, too. 50 * The returned result tells the caller how far to advance the 51 * scan pointer (passed in). The default is to just pass through the 52 * escaped character and advance the scan by one. 53 * 54 * Some applications need to keep an escaped newline, others need to 55 * suppress it. This is accomplished by supplying a '\n' replacement 56 * character that is different from \n, if need be. For example, use 57 * 0x7F and never emit a 0x7F. 58 * 59 * err: @code{NULL} is returned if the string is mal-formed. 60 =*/ 61 unsigned int 62 ao_string_cook_escape_char( char const* pzIn, char* pRes, u_int nl ) 63 { 64 unsigned int res = 1; 65 66 switch (*pRes = *pzIn++) { 67 case NUL: /* NUL - end of input string */ 68 return 0; 69 case '\r': 70 if (*pzIn != '\n') 71 return 1; 72 res++; 73 /* FALLTHROUGH */ 74 case '\n': /* NL - emit newline */ 75 *pRes = (char)nl; 76 return res; 77 78 case 'a': *pRes = '\a'; break; 79 case 'b': *pRes = '\b'; break; 80 case 'f': *pRes = '\f'; break; 81 case 'n': *pRes = '\n'; break; 82 case 'r': *pRes = '\r'; break; 83 case 't': *pRes = '\t'; break; 84 case 'v': *pRes = '\v'; break; 85 86 case 'x': 87 case 'X': /* HEX Escape */ 88 if (IS_HEX_DIGIT_CHAR(*pzIn)) { 89 char z[4], *pz = z; 90 91 do *(pz++) = *(pzIn++); 92 while (IS_HEX_DIGIT_CHAR(*pzIn) && (pz < z + 2)); 93 *pz = NUL; 94 *pRes = (unsigned char)strtoul(z, NULL, 16); 95 res += pz - z; 96 } 97 break; 98 99 case '0': case '1': case '2': case '3': 100 case '4': case '5': case '6': case '7': 101 { 102 /* 103 * IF the character copied was an octal digit, 104 * THEN set the output character to an octal value 105 */ 106 char z[4], *pz = z + 1; 107 unsigned long val; 108 z[0] = *pRes; 109 110 while (IS_OCT_DIGIT_CHAR(*pzIn) && (pz < z + 3)) 111 *(pz++) = *(pzIn++); 112 *pz = NUL; 113 val = strtoul(z, NULL, 8); 114 if (val > 0xFF) 115 val = 0xFF; 116 *pRes = (unsigned char)val; 117 res = pz - z; 118 break; 119 } 120 121 default: ; 122 } 123 124 return res; 125 } 126 127 128 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 129 * 130 * A quoted string has been found. 131 * Find the end of it and compress any escape sequences. 132 */ 133 /*=export_func ao_string_cook 134 * private: 135 * 136 * what: concatenate and escape-process strings 137 * arg: + char* + pzScan + The *MODIFIABLE* input buffer + 138 * arg: + int* + pLineCt + The (possibly NULL) pointer to a line count + 139 * 140 * ret-type: char* 141 * ret-desc: The address of the text following the processed strings. 142 * The return value is NULL if the strings are ill-formed. 143 * 144 * doc: 145 * 146 * A series of one or more quoted strings are concatenated together. 147 * If they are quoted with double quotes (@code{"}), then backslash 148 * escapes are processed per the C programming language. If they are 149 * single quote strings, then the backslashes are honored only when they 150 * precede another backslash or a single quote character. 151 * 152 * err: @code{NULL} is returned if the string(s) is/are mal-formed. 153 =*/ 154 char* 155 ao_string_cook( char* pzScan, int* pLineCt ) 156 { 157 int l = 0; 158 char q = *pzScan; 159 160 /* 161 * It is a quoted string. Process the escape sequence characters 162 * (in the set "abfnrtv") and make sure we find a closing quote. 163 */ 164 char* pzD = pzScan++; 165 char* pzS = pzScan; 166 167 if (pLineCt == NULL) 168 pLineCt = &l; 169 170 for (;;) { 171 /* 172 * IF the next character is the quote character, THEN we may end the 173 * string. We end it unless the next non-blank character *after* the 174 * string happens to also be a quote. If it is, then we will change 175 * our quote character to the new quote character and continue 176 * condensing text. 177 */ 178 while (*pzS == q) { 179 *pzD = NUL; /* This is probably the end of the line */ 180 pzS++; 181 182 scan_for_quote: 183 while (IS_WHITESPACE_CHAR(*pzS)) 184 if (*(pzS++) == '\n') 185 (*pLineCt)++; 186 187 /* 188 * IF the next character is a quote character, 189 * THEN we will concatenate the strings. 190 */ 191 switch (*pzS) { 192 case '"': 193 case '\'': 194 break; 195 196 case '/': 197 /* 198 * Allow for a comment embedded in the concatenated string. 199 */ 200 switch (pzS[1]) { 201 default: return NULL; 202 case '/': 203 /* 204 * Skip to end of line 205 */ 206 pzS = strchr( pzS, '\n' ); 207 if (pzS == NULL) 208 return NULL; 209 (*pLineCt)++; 210 break; 211 212 case '*': 213 { 214 char* p = strstr( pzS+2, "*/" ); 215 /* 216 * Skip to terminating star slash 217 */ 218 if (p == NULL) 219 return NULL; 220 while (pzS < p) { 221 if (*(pzS++) == '\n') 222 (*pLineCt)++; 223 } 224 225 pzS = p + 2; 226 } 227 } 228 goto scan_for_quote; 229 230 default: 231 /* 232 * The next non-whitespace character is not a quote. 233 * The series of quoted strings has come to an end. 234 */ 235 return pzS; 236 } 237 238 q = *(pzS++); /* assign new quote character and advance scan */ 239 } 240 241 /* 242 * We are inside a quoted string. Copy text. 243 */ 244 switch (*(pzD++) = *(pzS++)) { 245 case NUL: 246 return NULL; 247 248 case '\n': 249 (*pLineCt)++; 250 break; 251 252 case '\\': 253 /* 254 * IF we are escaping a new line, 255 * THEN drop both the escape and the newline from 256 * the result string. 257 */ 258 if (*pzS == '\n') { 259 pzS++; 260 pzD--; 261 (*pLineCt)++; 262 } 263 264 /* 265 * ELSE IF the quote character is '"' or '`', 266 * THEN we do the full escape character processing 267 */ 268 else if (q != '\'') { 269 int ct = ao_string_cook_escape_char( pzS, pzD-1, (u_int)'\n' ); 270 if (ct == 0) 271 return NULL; 272 273 pzS += ct; 274 } /* if (q != '\'') */ 275 276 /* 277 * OTHERWISE, we only process "\\", "\'" and "\#" sequences. 278 * The latter only to easily hide preprocessing directives. 279 */ 280 else switch (*pzS) { 281 case '\\': 282 case '\'': 283 case '#': 284 pzD[-1] = *pzS++; 285 } 286 } /* switch (*(pzD++) = *(pzS++)) */ 287 } /* for (;;) */ 288 } 289 /* 290 * Local Variables: 291 * mode: C 292 * c-file-style: "stroustrup" 293 * indent-tabs-mode: nil 294 * End: 295 * end of autoopts/cook.c */ 296