1290001Sglebius/**
2290001Sglebius * \file cook.c
3181834Sroberto *
4181834Sroberto *  This file contains the routines that deal with processing quoted strings
5181834Sroberto *  into an internal format.
6290001Sglebius *
7290001Sglebius * @addtogroup autoopts
8290001Sglebius * @{
9181834Sroberto */
10181834Sroberto/*
11290001Sglebius *  This file is part of AutoOpts, a companion to AutoGen.
12290001Sglebius *  AutoOpts is free software.
13290001Sglebius *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14181834Sroberto *
15290001Sglebius *  AutoOpts is available under any one of two licenses.  The license
16290001Sglebius *  in use must be one of these two and the choice is under the control
17290001Sglebius *  of the user of the license.
18181834Sroberto *
19290001Sglebius *   The GNU Lesser General Public License, version 3 or later
20290001Sglebius *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21181834Sroberto *
22290001Sglebius *   The Modified Berkeley Software Distribution License
23290001Sglebius *      See the file "COPYING.mbsd"
24181834Sroberto *
25290001Sglebius *  These files have the following sha256 sums:
26181834Sroberto *
27290001Sglebius *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28290001Sglebius *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29290001Sglebius *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30181834Sroberto */
31181834Sroberto
32181834Sroberto/* = = = START-STATIC-FORWARD = = = */
33290001Sglebiusstatic bool
34290001Sglebiuscontiguous_quote(char ** pps, char * pq, int * lnct_p);
35181834Sroberto/* = = = END-STATIC-FORWARD = = = */
36181834Sroberto
37181834Sroberto/*=export_func  ao_string_cook_escape_char
38181834Sroberto * private:
39181834Sroberto *
40181834Sroberto * what:  escape-process a string fragment
41290001Sglebius * arg:   + char const * + pzScan  + points to character after the escape +
42290001Sglebius * arg:   + char *       + pRes    + Where to put the result byte +
43181834Sroberto * arg:   + unsigned int + nl_ch   + replacement char if scanned char is \n +
44181834Sroberto *
45181834Sroberto * ret-type: unsigned int
46181834Sroberto * ret-desc: The number of bytes consumed processing the escaped character.
47181834Sroberto *
48181834Sroberto * doc:
49181834Sroberto *
50181834Sroberto *  This function converts "t" into "\t" and all your other favorite
51181834Sroberto *  escapes, including numeric ones:  hex and ocatal, too.
52181834Sroberto *  The returned result tells the caller how far to advance the
53181834Sroberto *  scan pointer (passed in).  The default is to just pass through the
54181834Sroberto *  escaped character and advance the scan by one.
55181834Sroberto *
56181834Sroberto *  Some applications need to keep an escaped newline, others need to
57181834Sroberto *  suppress it.  This is accomplished by supplying a '\n' replacement
58181834Sroberto *  character that is different from \n, if need be.  For example, use
59181834Sroberto *  0x7F and never emit a 0x7F.
60181834Sroberto *
61181834Sroberto * err:  @code{NULL} is returned if the string is mal-formed.
62181834Sroberto=*/
63181834Srobertounsigned int
64290001Sglebiusao_string_cook_escape_char(char const * pzIn, char * pRes, uint_t nl)
65181834Sroberto{
66290001Sglebius    unsigned int res = 1;
67181834Sroberto
68181834Sroberto    switch (*pRes = *pzIn++) {
69181834Sroberto    case NUL:         /* NUL - end of input string */
70181834Sroberto        return 0;
71181834Sroberto    case '\r':
72290001Sglebius        if (*pzIn != NL)
73181834Sroberto            return 1;
74181834Sroberto        res++;
75181834Sroberto        /* FALLTHROUGH */
76290001Sglebius    case NL:        /* NL  - emit newline        */
77181834Sroberto        *pRes = (char)nl;
78181834Sroberto        return res;
79181834Sroberto
80181834Sroberto    case 'a': *pRes = '\a'; break;
81181834Sroberto    case 'b': *pRes = '\b'; break;
82181834Sroberto    case 'f': *pRes = '\f'; break;
83290001Sglebius    case 'n': *pRes = NL;   break;
84181834Sroberto    case 'r': *pRes = '\r'; break;
85181834Sroberto    case 't': *pRes = '\t'; break;
86181834Sroberto    case 'v': *pRes = '\v'; break;
87181834Sroberto
88290001Sglebius    case 'x':
89290001Sglebius    case 'X':         /* HEX Escape       */
90290001Sglebius        if (IS_HEX_DIGIT_CHAR(*pzIn))  {
91290001Sglebius            char z[4];
92290001Sglebius            unsigned int ct = 0;
93181834Sroberto
94290001Sglebius            do  {
95290001Sglebius                z[ct] = pzIn[ct];
96290001Sglebius                if (++ct >= 2)
97290001Sglebius                    break;
98290001Sglebius            } while (IS_HEX_DIGIT_CHAR(pzIn[ct]));
99290001Sglebius            z[ct] = NUL;
100290001Sglebius            *pRes = (char)strtoul(z, NULL, 16);
101290001Sglebius            return ct + 1;
102290001Sglebius        }
103290001Sglebius        break;
104181834Sroberto
105290001Sglebius    case '0': case '1': case '2': case '3':
106290001Sglebius    case '4': case '5': case '6': case '7':
107290001Sglebius    {
108290001Sglebius        /*
109290001Sglebius         *  IF the character copied was an octal digit,
110290001Sglebius         *  THEN set the output character to an octal value.
111290001Sglebius         *  The 3 octal digit result might exceed 0xFF, so check it.
112290001Sglebius         */
113290001Sglebius        char z[4];
114290001Sglebius        unsigned long val;
115290001Sglebius        unsigned int  ct = 0;
116181834Sroberto
117290001Sglebius        z[ct++] = *--pzIn;
118290001Sglebius        while (IS_OCT_DIGIT_CHAR(pzIn[ct])) {
119290001Sglebius            z[ct] = pzIn[ct];
120290001Sglebius            if (++ct >= 3)
121181834Sroberto                break;
122181834Sroberto        }
123181834Sroberto
124290001Sglebius        z[ct] = NUL;
125290001Sglebius        val = strtoul(z, NULL, 8);
126290001Sglebius        if (val > 0xFF)
127290001Sglebius            val = 0xFF;
128290001Sglebius        *pRes = (char)val;
129290001Sglebius        return ct;
130290001Sglebius    }
131290001Sglebius
132290001Sglebius    default: /* quoted character is result character */;
133290001Sglebius    }
134290001Sglebius
135290001Sglebius    return res;
136290001Sglebius}
137290001Sglebius
138290001Sglebius
139290001Sglebius/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
140290001Sglebius *
141290001Sglebius *  A quoted string has been found.
142290001Sglebius *  Find the end of it and compress any escape sequences.
143290001Sglebius */
144290001Sglebiusstatic bool
145290001Sglebiuscontiguous_quote(char ** pps, char * pq, int * lnct_p)
146290001Sglebius{
147290001Sglebius    char * ps = *pps + 1;
148290001Sglebius
149290001Sglebius    for (;;) {
150290001Sglebius        while (IS_WHITESPACE_CHAR(*ps))
151290001Sglebius            if (*(ps++) == NL)
152290001Sglebius                (*lnct_p)++;
153290001Sglebius
154181834Sroberto        /*
155290001Sglebius         *  IF the next character is a quote character,
156290001Sglebius         *  THEN we will concatenate the strings.
157181834Sroberto         */
158290001Sglebius        switch (*ps) {
159290001Sglebius        case '"':
160290001Sglebius        case '\'':
161290001Sglebius            *pq  = *(ps++);  /* assign new quote character and return */
162290001Sglebius            *pps = ps;
163290001Sglebius            return true;
164181834Sroberto
165290001Sglebius        case '/':
166181834Sroberto            /*
167290001Sglebius             *  Allow for a comment embedded in the concatenated string.
168181834Sroberto             */
169290001Sglebius            switch (ps[1]) {
170290001Sglebius            default:
171290001Sglebius                *pps = NULL;
172290001Sglebius                return false;
173290001Sglebius
174290001Sglebius            case '/':
175290001Sglebius                /*
176290001Sglebius                 *  Skip to end of line
177290001Sglebius                 */
178290001Sglebius                ps = strchr(ps, NL);
179290001Sglebius                if (ps == NULL) {
180290001Sglebius                    *pps = NULL;
181290001Sglebius                    return false;
182290001Sglebius                }
183181834Sroberto                break;
184181834Sroberto
185290001Sglebius            case '*':
186290001Sglebius            {
187290001Sglebius                char * p = strstr( ps+2, "*/" );
188290001Sglebius                /*
189290001Sglebius                 *  Skip to terminating star slash
190290001Sglebius                 */
191290001Sglebius                if (p == NULL) {
192290001Sglebius                    *pps = NULL;
193290001Sglebius                    return false;
194290001Sglebius                }
195181834Sroberto
196290001Sglebius                while (ps < p) {
197290001Sglebius                    if (*(ps++) == NL)
198290001Sglebius                        (*lnct_p)++;
199290001Sglebius                }
200290001Sglebius
201290001Sglebius                ps = p + 2;
202181834Sroberto            }
203290001Sglebius            }
204290001Sglebius            continue;
205181834Sroberto
206290001Sglebius        default:
207181834Sroberto            /*
208290001Sglebius             *  The next non-whitespace character is not a quote.
209290001Sglebius             *  The series of quoted strings has come to an end.
210181834Sroberto             */
211290001Sglebius            *pps = ps;
212290001Sglebius            return false;
213181834Sroberto        }
214181834Sroberto    }
215181834Sroberto}
216181834Sroberto
217181834Sroberto/*=export_func  ao_string_cook
218181834Sroberto * private:
219181834Sroberto *
220181834Sroberto * what:  concatenate and escape-process strings
221290001Sglebius * arg:   + char * + pzScan  + The *MODIFIABLE* input buffer +
222290001Sglebius * arg:   + int *  + lnct_p  + The (possibly NULL) pointer to a line count +
223181834Sroberto *
224290001Sglebius * ret-type: char *
225181834Sroberto * ret-desc: The address of the text following the processed strings.
226181834Sroberto *           The return value is NULL if the strings are ill-formed.
227181834Sroberto *
228181834Sroberto * doc:
229181834Sroberto *
230181834Sroberto *  A series of one or more quoted strings are concatenated together.
231181834Sroberto *  If they are quoted with double quotes (@code{"}), then backslash
232181834Sroberto *  escapes are processed per the C programming language.  If they are
233181834Sroberto *  single quote strings, then the backslashes are honored only when they
234181834Sroberto *  precede another backslash or a single quote character.
235181834Sroberto *
236181834Sroberto * err:  @code{NULL} is returned if the string(s) is/are mal-formed.
237181834Sroberto=*/
238290001Sglebiuschar *
239290001Sglebiusao_string_cook(char * pzScan, int * lnct_p)
240181834Sroberto{
241181834Sroberto    int   l = 0;
242181834Sroberto    char  q = *pzScan;
243181834Sroberto
244181834Sroberto    /*
245181834Sroberto     *  It is a quoted string.  Process the escape sequence characters
246181834Sroberto     *  (in the set "abfnrtv") and make sure we find a closing quote.
247181834Sroberto     */
248290001Sglebius    char * pzD = pzScan++;
249290001Sglebius    char * pzS = pzScan;
250181834Sroberto
251290001Sglebius    if (lnct_p == NULL)
252290001Sglebius        lnct_p = &l;
253181834Sroberto
254181834Sroberto    for (;;) {
255181834Sroberto        /*
256181834Sroberto         *  IF the next character is the quote character, THEN we may end the
257181834Sroberto         *  string.  We end it unless the next non-blank character *after* the
258181834Sroberto         *  string happens to also be a quote.  If it is, then we will change
259181834Sroberto         *  our quote character to the new quote character and continue
260181834Sroberto         *  condensing text.
261181834Sroberto         */
262181834Sroberto        while (*pzS == q) {
263181834Sroberto            *pzD = NUL; /* This is probably the end of the line */
264290001Sglebius            if (! contiguous_quote(&pzS, &q, lnct_p))
265181834Sroberto                return pzS;
266181834Sroberto        }
267181834Sroberto
268181834Sroberto        /*
269181834Sroberto         *  We are inside a quoted string.  Copy text.
270181834Sroberto         */
271181834Sroberto        switch (*(pzD++) = *(pzS++)) {
272181834Sroberto        case NUL:
273181834Sroberto            return NULL;
274181834Sroberto
275290001Sglebius        case NL:
276290001Sglebius            (*lnct_p)++;
277181834Sroberto            break;
278181834Sroberto
279181834Sroberto        case '\\':
280181834Sroberto            /*
281181834Sroberto             *  IF we are escaping a new line,
282181834Sroberto             *  THEN drop both the escape and the newline from
283181834Sroberto             *       the result string.
284181834Sroberto             */
285290001Sglebius            if (*pzS == NL) {
286181834Sroberto                pzS++;
287181834Sroberto                pzD--;
288290001Sglebius                (*lnct_p)++;
289181834Sroberto            }
290181834Sroberto
291181834Sroberto            /*
292181834Sroberto             *  ELSE IF the quote character is '"' or '`',
293181834Sroberto             *  THEN we do the full escape character processing
294181834Sroberto             */
295181834Sroberto            else if (q != '\'') {
296290001Sglebius                unsigned int ct;
297290001Sglebius                ct = ao_string_cook_escape_char(pzS, pzD-1, (uint_t)NL);
298181834Sroberto                if (ct == 0)
299181834Sroberto                    return NULL;
300181834Sroberto
301181834Sroberto                pzS += ct;
302181834Sroberto            }     /* if (q != '\'')                  */
303181834Sroberto
304181834Sroberto            /*
305181834Sroberto             *  OTHERWISE, we only process "\\", "\'" and "\#" sequences.
306181834Sroberto             *  The latter only to easily hide preprocessing directives.
307181834Sroberto             */
308181834Sroberto            else switch (*pzS) {
309181834Sroberto            case '\\':
310181834Sroberto            case '\'':
311181834Sroberto            case '#':
312181834Sroberto                pzD[-1] = *pzS++;
313181834Sroberto            }
314181834Sroberto        }     /* switch (*(pzD++) = *(pzS++))    */
315181834Sroberto    }         /* for (;;)                        */
316181834Sroberto}
317290001Sglebius
318290001Sglebius/** @}
319290001Sglebius *
320181834Sroberto * Local Variables:
321181834Sroberto * mode: C
322181834Sroberto * c-file-style: "stroustrup"
323181834Sroberto * indent-tabs-mode: nil
324181834Sroberto * End:
325181834Sroberto * end of autoopts/cook.c */
326