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