4 * This file contains the routines that deal with processing quoted strings
5 * into an internal format.
11 * This file is part of AutoOpts, a companion to AutoGen.
12 * AutoOpts is free software.
13 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
15 * AutoOpts is available under any one of two licenses. The license
16 * in use must be one of these two and the choice is under the control
17 * of the user of the license.
19 * The GNU Lesser General Public License, version 3 or later
20 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
22 * The Modified Berkeley Software Distribution License
23 * See the file "COPYING.mbsd"
25 * These files have the following sha256 sums:
27 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
28 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
29 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
32 /* = = = START-STATIC-FORWARD = = = */
34 contiguous_quote(char ** pps, char * pq, int * lnct_p);
35 /* = = = END-STATIC-FORWARD = = = */
37 /*=export_func ao_string_cook_escape_char
40 * what: escape-process a string fragment
41 * arg: + char const * + pzScan + points to character after the escape +
42 * arg: + char * + pRes + Where to put the result byte +
43 * arg: + unsigned int + nl_ch + replacement char if scanned char is \n +
45 * ret-type: unsigned int
46 * ret-desc: The number of bytes consumed processing the escaped character.
50 * This function converts "t" into "\t" and all your other favorite
51 * escapes, including numeric ones: hex and ocatal, too.
52 * The returned result tells the caller how far to advance the
53 * scan pointer (passed in). The default is to just pass through the
54 * escaped character and advance the scan by one.
56 * Some applications need to keep an escaped newline, others need to
57 * suppress it. This is accomplished by supplying a '\n' replacement
58 * character that is different from \n, if need be. For example, use
59 * 0x7F and never emit a 0x7F.
61 * err: @code{NULL} is returned if the string is mal-formed.
64 ao_string_cook_escape_char(char const * pzIn, char * pRes, uint_t nl)
68 switch (*pRes = *pzIn++) {
69 case NUL: /* NUL - end of input string */
76 case NL: /* NL - emit newline */
80 case 'a': *pRes = '\a'; break;
81 case 'b': *pRes = '\b'; break;
82 case 'f': *pRes = '\f'; break;
83 case 'n': *pRes = NL; break;
84 case 'r': *pRes = '\r'; break;
85 case 't': *pRes = '\t'; break;
86 case 'v': *pRes = '\v'; break;
89 case 'X': /* HEX Escape */
90 if (IS_HEX_DIGIT_CHAR(*pzIn)) {
98 } while (IS_HEX_DIGIT_CHAR(pzIn[ct]));
100 *pRes = (char)strtoul(z, NULL, 16);
105 case '0': case '1': case '2': case '3':
106 case '4': case '5': case '6': case '7':
109 * IF the character copied was an octal digit,
110 * THEN set the output character to an octal value.
111 * The 3 octal digit result might exceed 0xFF, so check it.
118 while (IS_OCT_DIGIT_CHAR(pzIn[ct])) {
125 val = strtoul(z, NULL, 8);
132 default: /* quoted character is result character */;
139 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
141 * A quoted string has been found.
142 * Find the end of it and compress any escape sequences.
145 contiguous_quote(char ** pps, char * pq, int * lnct_p)
147 char * ps = *pps + 1;
150 while (IS_WHITESPACE_CHAR(*ps))
155 * IF the next character is a quote character,
156 * THEN we will concatenate the strings.
161 *pq = *(ps++); /* assign new quote character and return */
167 * Allow for a comment embedded in the concatenated string.
176 * Skip to end of line
187 char * p = strstr( ps+2, "*/" );
189 * Skip to terminating star slash
208 * The next non-whitespace character is not a quote.
209 * The series of quoted strings has come to an end.
217 /*=export_func ao_string_cook
220 * what: concatenate and escape-process strings
221 * arg: + char * + pzScan + The *MODIFIABLE* input buffer +
222 * arg: + int * + lnct_p + The (possibly NULL) pointer to a line count +
225 * ret-desc: The address of the text following the processed strings.
226 * The return value is NULL if the strings are ill-formed.
230 * A series of one or more quoted strings are concatenated together.
231 * If they are quoted with double quotes (@code{"}), then backslash
232 * escapes are processed per the C programming language. If they are
233 * single quote strings, then the backslashes are honored only when they
234 * precede another backslash or a single quote character.
236 * err: @code{NULL} is returned if the string(s) is/are mal-formed.
239 ao_string_cook(char * pzScan, int * lnct_p)
245 * It is a quoted string. Process the escape sequence characters
246 * (in the set "abfnrtv") and make sure we find a closing quote.
248 char * pzD = pzScan++;
256 * IF the next character is the quote character, THEN we may end the
257 * string. We end it unless the next non-blank character *after* the
258 * string happens to also be a quote. If it is, then we will change
259 * our quote character to the new quote character and continue
263 *pzD = NUL; /* This is probably the end of the line */
264 if (! contiguous_quote(&pzS, &q, lnct_p))
269 * We are inside a quoted string. Copy text.
271 switch (*(pzD++) = *(pzS++)) {
281 * IF we are escaping a new line,
282 * THEN drop both the escape and the newline from
292 * ELSE IF the quote character is '"' or '`',
293 * THEN we do the full escape character processing
295 else if (q != '\'') {
297 ct = ao_string_cook_escape_char(pzS, pzD-1, (uint_t)NL);
302 } /* if (q != '\'') */
305 * OTHERWISE, we only process "\\", "\'" and "\#" sequences.
306 * The latter only to easily hide preprocessing directives.
314 } /* switch (*(pzD++) = *(pzS++)) */
322 * c-file-style: "stroustrup"
323 * indent-tabs-mode: nil
325 * end of autoopts/cook.c */