Home | History | Annotate | Line # | Download | only in libopts
cook.c revision 1.1.1.2
      1      1.1  kardel /*	$NetBSD: cook.c,v 1.1.1.2 2012/01/31 21:27:47 kardel Exp $	*/
      2      1.1  kardel 
      3  1.1.1.2  kardel /**
      4  1.1.1.2  kardel  * \file cook.c
      5  1.1.1.2  kardel  *
      6  1.1.1.2  kardel  *  Time-stamp:      "2011-03-12 15:05:26 bkorb"
      7      1.1  kardel  *
      8      1.1  kardel  *  This file contains the routines that deal with processing quoted strings
      9      1.1  kardel  *  into an internal format.
     10      1.1  kardel  *
     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.2  kardel  *  AutoOpts is Copyright (c) 1992-2011 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  kardel  *  These files have the following md5sums:
     26      1.1  kardel  *
     27      1.1  kardel  *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
     28      1.1  kardel  *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
     29      1.1  kardel  *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
     30      1.1  kardel  */
     31      1.1  kardel 
     32      1.1  kardel /* = = = START-STATIC-FORWARD = = = */
     33  1.1.1.2  kardel static ag_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  kardel  * arg:   + char const*  + pzScan  + points to character after the escape +
     42      1.1  kardel  * 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  kardel ao_string_cook_escape_char( char const* pzIn, char* pRes, u_int nl )
     65      1.1  kardel {
     66      1.1  kardel     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  kardel         if (*pzIn != '\n')
     73      1.1  kardel             return 1;
     74      1.1  kardel         res++;
     75      1.1  kardel         /* FALLTHROUGH */
     76      1.1  kardel     case '\n':        /* 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  kardel     case 'n': *pRes = '\n'; 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  kardel             char z[4], *pz = z;
     92      1.1  kardel 
     93      1.1  kardel             do *(pz++) = *(pzIn++);
     94      1.1  kardel             while (IS_HEX_DIGIT_CHAR(*pzIn) && (pz < z + 2));
     95      1.1  kardel             *pz = NUL;
     96      1.1  kardel             *pRes = (unsigned char)strtoul(z, NULL, 16);
     97      1.1  kardel             res += pz - z;
     98      1.1  kardel         }
     99      1.1  kardel         break;
    100      1.1  kardel 
    101      1.1  kardel     case '0': case '1': case '2': case '3':
    102      1.1  kardel     case '4': case '5': case '6': case '7':
    103      1.1  kardel     {
    104      1.1  kardel         /*
    105      1.1  kardel          *  IF the character copied was an octal digit,
    106      1.1  kardel          *  THEN set the output character to an octal value
    107      1.1  kardel          */
    108      1.1  kardel         char z[4], *pz = z + 1;
    109      1.1  kardel         unsigned long val;
    110      1.1  kardel         z[0] = *pRes;
    111      1.1  kardel 
    112      1.1  kardel         while (IS_OCT_DIGIT_CHAR(*pzIn) && (pz < z + 3))
    113      1.1  kardel             *(pz++) = *(pzIn++);
    114      1.1  kardel         *pz = NUL;
    115      1.1  kardel         val = strtoul(z, NULL, 8);
    116      1.1  kardel         if (val > 0xFF)
    117      1.1  kardel             val = 0xFF;
    118      1.1  kardel         *pRes = (unsigned char)val;
    119      1.1  kardel         res = pz - z;
    120      1.1  kardel         break;
    121      1.1  kardel     }
    122      1.1  kardel 
    123      1.1  kardel     default: ;
    124      1.1  kardel     }
    125      1.1  kardel 
    126      1.1  kardel     return res;
    127      1.1  kardel }
    128      1.1  kardel 
    129      1.1  kardel 
    130      1.1  kardel /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    131      1.1  kardel  *
    132      1.1  kardel  *  A quoted string has been found.
    133      1.1  kardel  *  Find the end of it and compress any escape sequences.
    134      1.1  kardel  */
    135  1.1.1.2  kardel static ag_bool
    136  1.1.1.2  kardel contiguous_quote(char ** pps, char * pq, int * lnct_p)
    137  1.1.1.2  kardel {
    138  1.1.1.2  kardel     char * ps = *pps + 1;
    139  1.1.1.2  kardel 
    140  1.1.1.2  kardel     for (;;) {
    141  1.1.1.2  kardel         while (IS_WHITESPACE_CHAR(*ps))
    142  1.1.1.2  kardel             if (*(ps++) == '\n')
    143  1.1.1.2  kardel                 (*lnct_p)++;
    144  1.1.1.2  kardel 
    145  1.1.1.2  kardel         /*
    146  1.1.1.2  kardel          *  IF the next character is a quote character,
    147  1.1.1.2  kardel          *  THEN we will concatenate the strings.
    148  1.1.1.2  kardel          */
    149  1.1.1.2  kardel         switch (*ps) {
    150  1.1.1.2  kardel         case '"':
    151  1.1.1.2  kardel         case '\'':
    152  1.1.1.2  kardel             *pq  = *(ps++);  /* assign new quote character and return */
    153  1.1.1.2  kardel             *pps = ps;
    154  1.1.1.2  kardel             return AG_TRUE;
    155  1.1.1.2  kardel 
    156  1.1.1.2  kardel         case '/':
    157  1.1.1.2  kardel             /*
    158  1.1.1.2  kardel              *  Allow for a comment embedded in the concatenated string.
    159  1.1.1.2  kardel              */
    160  1.1.1.2  kardel             switch (ps[1]) {
    161  1.1.1.2  kardel             default:
    162  1.1.1.2  kardel                 *pps = NULL;
    163  1.1.1.2  kardel                 return AG_FALSE;
    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                  *  Skip to end of line
    168  1.1.1.2  kardel                  */
    169  1.1.1.2  kardel                 ps = strchr(ps, '\n');
    170  1.1.1.2  kardel                 if (ps == NULL) {
    171  1.1.1.2  kardel                     *pps = NULL;
    172  1.1.1.2  kardel                     return AG_FALSE;
    173  1.1.1.2  kardel                 }
    174  1.1.1.2  kardel                 break;
    175  1.1.1.2  kardel 
    176  1.1.1.2  kardel             case '*':
    177  1.1.1.2  kardel             {
    178  1.1.1.2  kardel                 char* p = strstr( ps+2, "*/" );
    179  1.1.1.2  kardel                 /*
    180  1.1.1.2  kardel                  *  Skip to terminating star slash
    181  1.1.1.2  kardel                  */
    182  1.1.1.2  kardel                 if (p == NULL) {
    183  1.1.1.2  kardel                     *pps = NULL;
    184  1.1.1.2  kardel                     return AG_FALSE;
    185  1.1.1.2  kardel                 }
    186  1.1.1.2  kardel 
    187  1.1.1.2  kardel                 while (ps < p) {
    188  1.1.1.2  kardel                     if (*(ps++) == '\n')
    189  1.1.1.2  kardel                         (*lnct_p)++;
    190  1.1.1.2  kardel                 }
    191  1.1.1.2  kardel 
    192  1.1.1.2  kardel                 ps = p + 2;
    193  1.1.1.2  kardel             }
    194  1.1.1.2  kardel             }
    195  1.1.1.2  kardel             continue;
    196  1.1.1.2  kardel 
    197  1.1.1.2  kardel         default:
    198  1.1.1.2  kardel             /*
    199  1.1.1.2  kardel              *  The next non-whitespace character is not a quote.
    200  1.1.1.2  kardel              *  The series of quoted strings has come to an end.
    201  1.1.1.2  kardel              */
    202  1.1.1.2  kardel             *pps = ps;
    203  1.1.1.2  kardel             return AG_FALSE;
    204  1.1.1.2  kardel         }
    205  1.1.1.2  kardel     }
    206  1.1.1.2  kardel }
    207  1.1.1.2  kardel 
    208      1.1  kardel /*=export_func  ao_string_cook
    209      1.1  kardel  * private:
    210      1.1  kardel  *
    211      1.1  kardel  * what:  concatenate and escape-process strings
    212  1.1.1.2  kardel  * arg:   + char* + pzScan  + The *MODIFIABLE* input buffer +
    213  1.1.1.2  kardel  * arg:   + int*  + lnct_p  + The (possibly NULL) pointer to a line count +
    214      1.1  kardel  *
    215      1.1  kardel  * ret-type: char*
    216      1.1  kardel  * ret-desc: The address of the text following the processed strings.
    217      1.1  kardel  *           The return value is NULL if the strings are ill-formed.
    218      1.1  kardel  *
    219      1.1  kardel  * doc:
    220      1.1  kardel  *
    221      1.1  kardel  *  A series of one or more quoted strings are concatenated together.
    222      1.1  kardel  *  If they are quoted with double quotes (@code{"}), then backslash
    223      1.1  kardel  *  escapes are processed per the C programming language.  If they are
    224      1.1  kardel  *  single quote strings, then the backslashes are honored only when they
    225      1.1  kardel  *  precede another backslash or a single quote character.
    226      1.1  kardel  *
    227      1.1  kardel  * err:  @code{NULL} is returned if the string(s) is/are mal-formed.
    228      1.1  kardel =*/
    229  1.1.1.2  kardel char *
    230  1.1.1.2  kardel ao_string_cook(char * pzScan, int * lnct_p)
    231      1.1  kardel {
    232      1.1  kardel     int   l = 0;
    233      1.1  kardel     char  q = *pzScan;
    234      1.1  kardel 
    235      1.1  kardel     /*
    236      1.1  kardel      *  It is a quoted string.  Process the escape sequence characters
    237      1.1  kardel      *  (in the set "abfnrtv") and make sure we find a closing quote.
    238      1.1  kardel      */
    239      1.1  kardel     char* pzD = pzScan++;
    240      1.1  kardel     char* pzS = pzScan;
    241      1.1  kardel 
    242  1.1.1.2  kardel     if (lnct_p == NULL)
    243  1.1.1.2  kardel         lnct_p = &l;
    244      1.1  kardel 
    245      1.1  kardel     for (;;) {
    246      1.1  kardel         /*
    247      1.1  kardel          *  IF the next character is the quote character, THEN we may end the
    248      1.1  kardel          *  string.  We end it unless the next non-blank character *after* the
    249      1.1  kardel          *  string happens to also be a quote.  If it is, then we will change
    250      1.1  kardel          *  our quote character to the new quote character and continue
    251      1.1  kardel          *  condensing text.
    252      1.1  kardel          */
    253      1.1  kardel         while (*pzS == q) {
    254      1.1  kardel             *pzD = NUL; /* This is probably the end of the line */
    255  1.1.1.2  kardel             if (! contiguous_quote(&pzS, &q, lnct_p))
    256      1.1  kardel                 return pzS;
    257      1.1  kardel         }
    258      1.1  kardel 
    259      1.1  kardel         /*
    260      1.1  kardel          *  We are inside a quoted string.  Copy text.
    261      1.1  kardel          */
    262      1.1  kardel         switch (*(pzD++) = *(pzS++)) {
    263      1.1  kardel         case NUL:
    264      1.1  kardel             return NULL;
    265      1.1  kardel 
    266      1.1  kardel         case '\n':
    267  1.1.1.2  kardel             (*lnct_p)++;
    268      1.1  kardel             break;
    269      1.1  kardel 
    270      1.1  kardel         case '\\':
    271      1.1  kardel             /*
    272      1.1  kardel              *  IF we are escaping a new line,
    273      1.1  kardel              *  THEN drop both the escape and the newline from
    274      1.1  kardel              *       the result string.
    275      1.1  kardel              */
    276      1.1  kardel             if (*pzS == '\n') {
    277      1.1  kardel                 pzS++;
    278      1.1  kardel                 pzD--;
    279  1.1.1.2  kardel                 (*lnct_p)++;
    280      1.1  kardel             }
    281      1.1  kardel 
    282      1.1  kardel             /*
    283      1.1  kardel              *  ELSE IF the quote character is '"' or '`',
    284      1.1  kardel              *  THEN we do the full escape character processing
    285      1.1  kardel              */
    286      1.1  kardel             else if (q != '\'') {
    287      1.1  kardel                 int ct = ao_string_cook_escape_char( pzS, pzD-1, (u_int)'\n' );
    288      1.1  kardel                 if (ct == 0)
    289      1.1  kardel                     return NULL;
    290      1.1  kardel 
    291      1.1  kardel                 pzS += ct;
    292      1.1  kardel             }     /* if (q != '\'')                  */
    293      1.1  kardel 
    294      1.1  kardel             /*
    295      1.1  kardel              *  OTHERWISE, we only process "\\", "\'" and "\#" sequences.
    296      1.1  kardel              *  The latter only to easily hide preprocessing directives.
    297      1.1  kardel              */
    298      1.1  kardel             else switch (*pzS) {
    299      1.1  kardel             case '\\':
    300      1.1  kardel             case '\'':
    301      1.1  kardel             case '#':
    302      1.1  kardel                 pzD[-1] = *pzS++;
    303      1.1  kardel             }
    304      1.1  kardel         }     /* switch (*(pzD++) = *(pzS++))    */
    305      1.1  kardel     }         /* for (;;)                        */
    306      1.1  kardel }
    307      1.1  kardel /*
    308      1.1  kardel  * Local Variables:
    309      1.1  kardel  * mode: C
    310      1.1  kardel  * c-file-style: "stroustrup"
    311      1.1  kardel  * indent-tabs-mode: nil
    312      1.1  kardel  * End:
    313      1.1  kardel  * end of autoopts/cook.c */
    314