Home | History | Annotate | Line # | Download | only in libopts
load.c revision 1.1.1.1.6.2
      1  1.1.1.1.6.2    yamt /*	$NetBSD: load.c,v 1.1.1.1.6.2 2014/05/22 15:50:14 yamt Exp $	*/
      2          1.1  kardel 
      3          1.1  kardel 
      4  1.1.1.1.6.1    yamt /**
      5  1.1.1.1.6.1    yamt  *  \file load.c
      6          1.1  kardel  *
      7          1.1  kardel  *  This file contains the routines that deal with processing text strings
      8          1.1  kardel  *  for options, either from a NUL-terminated string passed in or from an
      9          1.1  kardel  *  rc/ini file.
     10          1.1  kardel  *
     11  1.1.1.1.6.2    yamt  * @addtogroup autoopts
     12  1.1.1.1.6.2    yamt  * @{
     13  1.1.1.1.6.2    yamt  */
     14  1.1.1.1.6.2    yamt /*
     15          1.1  kardel  *  This file is part of AutoOpts, a companion to AutoGen.
     16          1.1  kardel  *  AutoOpts is free software.
     17  1.1.1.1.6.2    yamt  *  AutoOpts is Copyright (C) 1992-2013 by Bruce Korb - all rights reserved
     18          1.1  kardel  *
     19          1.1  kardel  *  AutoOpts is available under any one of two licenses.  The license
     20          1.1  kardel  *  in use must be one of these two and the choice is under the control
     21          1.1  kardel  *  of the user of the license.
     22          1.1  kardel  *
     23          1.1  kardel  *   The GNU Lesser General Public License, version 3 or later
     24          1.1  kardel  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
     25          1.1  kardel  *
     26          1.1  kardel  *   The Modified Berkeley Software Distribution License
     27          1.1  kardel  *      See the file "COPYING.mbsd"
     28          1.1  kardel  *
     29  1.1.1.1.6.2    yamt  *  These files have the following sha256 sums:
     30          1.1  kardel  *
     31  1.1.1.1.6.2    yamt  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
     32  1.1.1.1.6.2    yamt  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
     33  1.1.1.1.6.2    yamt  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
     34          1.1  kardel  */
     35          1.1  kardel 
     36          1.1  kardel /* = = = START-STATIC-FORWARD = = = */
     37  1.1.1.1.6.2    yamt static bool
     38  1.1.1.1.6.2    yamt get_realpath(char * buf, size_t b_sz);
     39  1.1.1.1.6.2    yamt 
     40  1.1.1.1.6.2    yamt static bool
     41  1.1.1.1.6.2    yamt add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path);
     42  1.1.1.1.6.2    yamt 
     43  1.1.1.1.6.2    yamt static bool
     44  1.1.1.1.6.2    yamt add_env_val(char * buf, int buf_sz, char const * name);
     45          1.1  kardel 
     46  1.1.1.1.6.2    yamt static char *
     47  1.1.1.1.6.2    yamt assemble_arg_val(char * txt, tOptionLoadMode mode);
     48  1.1.1.1.6.2    yamt 
     49  1.1.1.1.6.2    yamt static char *
     50  1.1.1.1.6.2    yamt trim_quotes(char * arg);
     51  1.1.1.1.6.2    yamt 
     52  1.1.1.1.6.2    yamt static bool
     53  1.1.1.1.6.2    yamt direction_ok(opt_state_mask_t f, int dir);
     54          1.1  kardel /* = = = END-STATIC-FORWARD = = = */
     55          1.1  kardel 
     56  1.1.1.1.6.2    yamt static bool
     57  1.1.1.1.6.2    yamt get_realpath(char * buf, size_t b_sz)
     58  1.1.1.1.6.2    yamt {
     59  1.1.1.1.6.2    yamt #if defined(HAVE_CANONICALIZE_FILE_NAME)
     60  1.1.1.1.6.2    yamt     {
     61  1.1.1.1.6.2    yamt         size_t name_len;
     62  1.1.1.1.6.2    yamt 
     63  1.1.1.1.6.2    yamt         char * pz = canonicalize_file_name(buf);
     64  1.1.1.1.6.2    yamt         if (pz == NULL)
     65  1.1.1.1.6.2    yamt             return false;
     66  1.1.1.1.6.2    yamt 
     67  1.1.1.1.6.2    yamt         name_len = strlen(pz);
     68  1.1.1.1.6.2    yamt         if (name_len >= (size_t)b_sz) {
     69  1.1.1.1.6.2    yamt             free(pz);
     70  1.1.1.1.6.2    yamt             return false;
     71  1.1.1.1.6.2    yamt         }
     72  1.1.1.1.6.2    yamt 
     73  1.1.1.1.6.2    yamt         memcpy(buf, pz, name_len + 1);
     74  1.1.1.1.6.2    yamt         free(pz);
     75  1.1.1.1.6.2    yamt     }
     76  1.1.1.1.6.2    yamt 
     77  1.1.1.1.6.2    yamt #elif defined(HAVE_REALPATH)
     78  1.1.1.1.6.2    yamt     {
     79  1.1.1.1.6.2    yamt         size_t name_len;
     80  1.1.1.1.6.2    yamt         char z[PATH_MAX+1];
     81  1.1.1.1.6.2    yamt 
     82  1.1.1.1.6.2    yamt         if (realpath(buf, z) == NULL)
     83  1.1.1.1.6.2    yamt             return false;
     84  1.1.1.1.6.2    yamt 
     85  1.1.1.1.6.2    yamt         name_len = strlen(z);
     86  1.1.1.1.6.2    yamt         if (name_len >= b_sz)
     87  1.1.1.1.6.2    yamt             return false;
     88  1.1.1.1.6.2    yamt 
     89  1.1.1.1.6.2    yamt         memcpy(buf, z, name_len + 1);
     90  1.1.1.1.6.2    yamt     }
     91  1.1.1.1.6.2    yamt #endif
     92  1.1.1.1.6.2    yamt     return true;
     93  1.1.1.1.6.2    yamt }
     94  1.1.1.1.6.2    yamt 
     95          1.1  kardel /*=export_func  optionMakePath
     96          1.1  kardel  * private:
     97          1.1  kardel  *
     98          1.1  kardel  * what:  translate and construct a path
     99  1.1.1.1.6.2    yamt  * arg:   + char*       + p_buf     + The result buffer +
    100  1.1.1.1.6.2    yamt  * arg:   + int         + b_sz      + The size of this buffer +
    101  1.1.1.1.6.2    yamt  * arg:   + char const* + fname     + The input name +
    102  1.1.1.1.6.2    yamt  * arg:   + char const* + prg_path  + The full path of the current program +
    103          1.1  kardel  *
    104  1.1.1.1.6.2    yamt  * ret-type: bool
    105  1.1.1.1.6.2    yamt  * ret-desc: true if the name was handled, otherwise false.
    106          1.1  kardel  *           If the name does not start with ``$'', then it is handled
    107          1.1  kardel  *           simply by copying the input name to the output buffer and
    108          1.1  kardel  *           resolving the name with either
    109          1.1  kardel  *           @code{canonicalize_file_name(3GLIBC)} or @code{realpath(3C)}.
    110          1.1  kardel  *
    111          1.1  kardel  * doc:
    112          1.1  kardel  *
    113  1.1.1.1.6.1    yamt  *  This routine will copy the @code{pzName} input name into the
    114  1.1.1.1.6.1    yamt  *  @code{pzBuf} output buffer, not exceeding @code{bufSize} bytes.  If the
    115          1.1  kardel  *  first character of the input name is a @code{'$'} character, then there
    116          1.1  kardel  *  is special handling:
    117          1.1  kardel  *  @*
    118          1.1  kardel  *  @code{$$} is replaced with the directory name of the @code{pzProgPath},
    119          1.1  kardel  *  searching @code{$PATH} if necessary.
    120          1.1  kardel  *  @*
    121          1.1  kardel  *  @code{$@} is replaced with the AutoGen package data installation directory
    122          1.1  kardel  *  (aka @code{pkgdatadir}).
    123          1.1  kardel  *  @*
    124          1.1  kardel  *  @code{$NAME} is replaced by the contents of the @code{NAME} environment
    125          1.1  kardel  *  variable.  If not found, the search fails.
    126          1.1  kardel  *
    127          1.1  kardel  *  Please note: both @code{$$} and @code{$NAME} must be at the start of the
    128          1.1  kardel  *     @code{pzName} string and must either be the entire string or be followed
    129          1.1  kardel  *     by the @code{'/'} (backslash on windows) character.
    130          1.1  kardel  *
    131  1.1.1.1.6.2    yamt  * err:  @code{false} is returned if:
    132          1.1  kardel  *       @*
    133          1.1  kardel  *       @bullet{} The input name exceeds @code{bufSize} bytes.
    134          1.1  kardel  *       @*
    135          1.1  kardel  *       @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
    136          1.1  kardel  *                 and the next character is not '/'.
    137          1.1  kardel  *       @*
    138          1.1  kardel  *       @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
    139          1.1  kardel  *                 was specified.
    140          1.1  kardel  *       @*
    141          1.1  kardel  *       @bullet{} @code{NAME} is not a known environment variable
    142          1.1  kardel  *       @*
    143          1.1  kardel  *       @bullet{} @code{canonicalize_file_name} or @code{realpath} return
    144          1.1  kardel  *                 errors (cannot resolve the resulting path).
    145          1.1  kardel =*/
    146  1.1.1.1.6.2    yamt bool
    147  1.1.1.1.6.2    yamt optionMakePath(char * p_buf, int b_sz, char const * fname, char const * prg_path)
    148          1.1  kardel {
    149  1.1.1.1.6.2    yamt     {
    150  1.1.1.1.6.2    yamt         size_t len = strlen(fname);
    151          1.1  kardel 
    152  1.1.1.1.6.2    yamt         if (((size_t)b_sz <= len) || (len == 0))
    153  1.1.1.1.6.2    yamt             return false;
    154  1.1.1.1.6.2    yamt     }
    155          1.1  kardel 
    156          1.1  kardel     /*
    157          1.1  kardel      *  IF not an environment variable, just copy the data
    158          1.1  kardel      */
    159  1.1.1.1.6.2    yamt     if (*fname != '$') {
    160  1.1.1.1.6.2    yamt         char   const * src = fname;
    161  1.1.1.1.6.2    yamt         char * dst = p_buf;
    162  1.1.1.1.6.2    yamt         int    ct  = b_sz;
    163          1.1  kardel 
    164          1.1  kardel         for (;;) {
    165  1.1.1.1.6.2    yamt             if ( (*(dst++) = *(src++)) == NUL)
    166          1.1  kardel                 break;
    167          1.1  kardel             if (--ct <= 0)
    168  1.1.1.1.6.2    yamt                 return false;
    169          1.1  kardel         }
    170          1.1  kardel     }
    171          1.1  kardel 
    172          1.1  kardel     /*
    173          1.1  kardel      *  IF the name starts with "$$", then it must be "$$" or
    174          1.1  kardel      *  it must start with "$$/".  In either event, replace the "$$"
    175          1.1  kardel      *  with the path to the executable and append a "/" character.
    176          1.1  kardel      */
    177  1.1.1.1.6.2    yamt     else switch (fname[1]) {
    178          1.1  kardel     case NUL:
    179  1.1.1.1.6.2    yamt         return false;
    180          1.1  kardel 
    181          1.1  kardel     case '$':
    182  1.1.1.1.6.2    yamt         if (! add_prog_path(p_buf, b_sz, fname, prg_path))
    183  1.1.1.1.6.2    yamt             return false;
    184          1.1  kardel         break;
    185          1.1  kardel 
    186          1.1  kardel     case '@':
    187  1.1.1.1.6.1    yamt         if (program_pkgdatadir[0] == NUL)
    188  1.1.1.1.6.2    yamt             return false;
    189          1.1  kardel 
    190  1.1.1.1.6.2    yamt         if (snprintf(p_buf, (size_t)b_sz, "%s%s",
    191  1.1.1.1.6.2    yamt                      program_pkgdatadir, fname + 2) >= b_sz)
    192  1.1.1.1.6.2    yamt             return false;
    193          1.1  kardel         break;
    194          1.1  kardel 
    195          1.1  kardel     default:
    196  1.1.1.1.6.2    yamt         if (! add_env_val(p_buf, b_sz, fname))
    197  1.1.1.1.6.2    yamt             return false;
    198          1.1  kardel     }
    199          1.1  kardel 
    200  1.1.1.1.6.2    yamt     return get_realpath(p_buf, b_sz);
    201          1.1  kardel }
    202          1.1  kardel 
    203  1.1.1.1.6.2    yamt /**
    204  1.1.1.1.6.2    yamt  * convert a leading "$$" into a path to the executable.
    205  1.1.1.1.6.2    yamt  */
    206  1.1.1.1.6.2    yamt static bool
    207  1.1.1.1.6.2    yamt add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path)
    208          1.1  kardel {
    209  1.1.1.1.6.2    yamt     char const *   path;
    210  1.1.1.1.6.2    yamt     char const *   pz;
    211          1.1  kardel     int     skip = 2;
    212          1.1  kardel 
    213  1.1.1.1.6.2    yamt     switch (fname[2]) {
    214          1.1  kardel     case DIRCH:
    215          1.1  kardel         skip = 3;
    216          1.1  kardel     case NUL:
    217          1.1  kardel         break;
    218          1.1  kardel     default:
    219  1.1.1.1.6.2    yamt         return false;
    220          1.1  kardel     }
    221          1.1  kardel 
    222          1.1  kardel     /*
    223          1.1  kardel      *  See if the path is included in the program name.
    224          1.1  kardel      *  If it is, we're done.  Otherwise, we have to hunt
    225          1.1  kardel      *  for the program using "pathfind".
    226          1.1  kardel      */
    227  1.1.1.1.6.2    yamt     if (strchr(prg_path, DIRCH) != NULL)
    228  1.1.1.1.6.2    yamt         path = prg_path;
    229          1.1  kardel     else {
    230  1.1.1.1.6.2    yamt         path = pathfind(getenv("PATH"), (char*)(intptr_t)prg_path, "rx");
    231          1.1  kardel 
    232  1.1.1.1.6.2    yamt         if (path == NULL)
    233  1.1.1.1.6.2    yamt             return false;
    234          1.1  kardel     }
    235          1.1  kardel 
    236  1.1.1.1.6.2    yamt     pz = strrchr(path, DIRCH);
    237          1.1  kardel 
    238          1.1  kardel     /*
    239          1.1  kardel      *  IF we cannot find a directory name separator,
    240          1.1  kardel      *  THEN we do not have a path name to our executable file.
    241          1.1  kardel      */
    242          1.1  kardel     if (pz == NULL)
    243  1.1.1.1.6.2    yamt         return false;
    244          1.1  kardel 
    245  1.1.1.1.6.2    yamt     fname += skip;
    246          1.1  kardel 
    247          1.1  kardel     /*
    248          1.1  kardel      *  Concatenate the file name to the end of the executable path.
    249          1.1  kardel      *  The result may be either a file or a directory.
    250          1.1  kardel      */
    251  1.1.1.1.6.2    yamt     if ((unsigned)(pz - path) + 1 + strlen(fname) >= (unsigned)b_sz)
    252  1.1.1.1.6.2    yamt         return false;
    253          1.1  kardel 
    254  1.1.1.1.6.2    yamt     memcpy(buf, path, (size_t)((pz - path)+1));
    255  1.1.1.1.6.2    yamt     strcpy(buf + (pz - path) + 1, fname);
    256          1.1  kardel 
    257          1.1  kardel     /*
    258  1.1.1.1.6.2    yamt      *  If the "path" path was gotten from "pathfind()", then it was
    259          1.1  kardel      *  allocated and we need to deallocate it.
    260          1.1  kardel      */
    261  1.1.1.1.6.2    yamt     if (path != prg_path)
    262  1.1.1.1.6.2    yamt         AGFREE(path);
    263  1.1.1.1.6.2    yamt     return true;
    264          1.1  kardel }
    265          1.1  kardel 
    266  1.1.1.1.6.2    yamt /**
    267  1.1.1.1.6.2    yamt  * Add an environment variable value.
    268  1.1.1.1.6.2    yamt  */
    269  1.1.1.1.6.2    yamt static bool
    270  1.1.1.1.6.2    yamt add_env_val(char * buf, int buf_sz, char const * name)
    271          1.1  kardel {
    272  1.1.1.1.6.2    yamt     char * dir_part = buf;
    273          1.1  kardel 
    274          1.1  kardel     for (;;) {
    275  1.1.1.1.6.2    yamt         int ch = (int)*++name;
    276          1.1  kardel         if (! IS_VALUE_NAME_CHAR(ch))
    277          1.1  kardel             break;
    278  1.1.1.1.6.2    yamt         *(dir_part++) = (char)ch;
    279          1.1  kardel     }
    280          1.1  kardel 
    281  1.1.1.1.6.2    yamt     if (dir_part == buf)
    282  1.1.1.1.6.2    yamt         return false;
    283          1.1  kardel 
    284  1.1.1.1.6.2    yamt     *dir_part = NUL;
    285          1.1  kardel 
    286  1.1.1.1.6.2    yamt     dir_part = getenv(buf);
    287          1.1  kardel 
    288          1.1  kardel     /*
    289          1.1  kardel      *  Environment value not found -- skip the home list entry
    290          1.1  kardel      */
    291  1.1.1.1.6.2    yamt     if (dir_part == NULL)
    292  1.1.1.1.6.2    yamt         return false;
    293          1.1  kardel 
    294  1.1.1.1.6.2    yamt     if (strlen(dir_part) + 1 + strlen(name) >= (unsigned)buf_sz)
    295  1.1.1.1.6.2    yamt         return false;
    296          1.1  kardel 
    297  1.1.1.1.6.2    yamt     sprintf(buf, "%s%s", dir_part, name);
    298  1.1.1.1.6.2    yamt     return true;
    299          1.1  kardel }
    300          1.1  kardel 
    301  1.1.1.1.6.2    yamt /**
    302  1.1.1.1.6.2    yamt  * Trim leading and trailing white space.
    303  1.1.1.1.6.2    yamt  * If we are cooking the text and the text is quoted, then "cook"
    304  1.1.1.1.6.2    yamt  * the string.  To cook, the string must be quoted.
    305  1.1.1.1.6.2    yamt  *
    306  1.1.1.1.6.2    yamt  * @param[in,out] txt  the input and output string
    307  1.1.1.1.6.2    yamt  * @param[in]     mode the handling mode (cooking method)
    308  1.1.1.1.6.2    yamt  */
    309          1.1  kardel LOCAL void
    310  1.1.1.1.6.2    yamt munge_str(char * txt, tOptionLoadMode mode)
    311          1.1  kardel {
    312  1.1.1.1.6.2    yamt     char * pzE;
    313          1.1  kardel 
    314          1.1  kardel     if (mode == OPTION_LOAD_KEEP)
    315          1.1  kardel         return;
    316          1.1  kardel 
    317  1.1.1.1.6.2    yamt     if (IS_WHITESPACE_CHAR(*txt)) {
    318  1.1.1.1.6.2    yamt         char * src = SPN_WHITESPACE_CHARS(txt+1);
    319  1.1.1.1.6.2    yamt         size_t l   = strlen(src) + 1;
    320  1.1.1.1.6.2    yamt         memmove(txt, src, l);
    321  1.1.1.1.6.2    yamt         pzE = txt + l - 1;
    322  1.1.1.1.6.2    yamt 
    323          1.1  kardel     } else
    324  1.1.1.1.6.2    yamt         pzE = txt + strlen(txt);
    325          1.1  kardel 
    326  1.1.1.1.6.2    yamt     pzE  = SPN_WHITESPACE_BACK(txt, pzE);
    327          1.1  kardel     *pzE = NUL;
    328          1.1  kardel 
    329          1.1  kardel     if (mode == OPTION_LOAD_UNCOOKED)
    330          1.1  kardel         return;
    331          1.1  kardel 
    332  1.1.1.1.6.2    yamt     switch (*txt) {
    333          1.1  kardel     default: return;
    334          1.1  kardel     case '"':
    335          1.1  kardel     case '\'': break;
    336          1.1  kardel     }
    337          1.1  kardel 
    338          1.1  kardel     switch (pzE[-1]) {
    339          1.1  kardel     default: return;
    340          1.1  kardel     case '"':
    341          1.1  kardel     case '\'': break;
    342          1.1  kardel     }
    343          1.1  kardel 
    344  1.1.1.1.6.2    yamt     (void)ao_string_cook(txt, NULL);
    345          1.1  kardel }
    346          1.1  kardel 
    347  1.1.1.1.6.2    yamt static char *
    348  1.1.1.1.6.2    yamt assemble_arg_val(char * txt, tOptionLoadMode mode)
    349          1.1  kardel {
    350  1.1.1.1.6.2    yamt     char * end = strpbrk(txt, ARG_BREAK_STR);
    351  1.1.1.1.6.2    yamt     int    space_break;
    352          1.1  kardel 
    353          1.1  kardel     /*
    354          1.1  kardel      *  Not having an argument to a configurable name is okay.
    355          1.1  kardel      */
    356  1.1.1.1.6.2    yamt     if (end == NULL)
    357  1.1.1.1.6.2    yamt         return txt + strlen(txt);
    358          1.1  kardel 
    359          1.1  kardel     /*
    360          1.1  kardel      *  If we are keeping all whitespace, then the  modevalue starts with the
    361          1.1  kardel      *  character that follows the end of the configurable name, regardless
    362          1.1  kardel      *  of which character caused it.
    363          1.1  kardel      */
    364          1.1  kardel     if (mode == OPTION_LOAD_KEEP) {
    365  1.1.1.1.6.2    yamt         *(end++) = NUL;
    366  1.1.1.1.6.2    yamt         return end;
    367          1.1  kardel     }
    368          1.1  kardel 
    369          1.1  kardel     /*
    370          1.1  kardel      *  If the name ended on a white space character, remember that
    371          1.1  kardel      *  because we'll have to skip over an immediately following ':' or '='
    372          1.1  kardel      *  (and the white space following *that*).
    373          1.1  kardel      */
    374  1.1.1.1.6.2    yamt     space_break = IS_WHITESPACE_CHAR(*end);
    375  1.1.1.1.6.2    yamt     *(end++) = NUL;
    376          1.1  kardel 
    377  1.1.1.1.6.2    yamt     end = SPN_WHITESPACE_CHARS(end);
    378  1.1.1.1.6.2    yamt     if (space_break && ((*end == ':') || (*end == '=')))
    379  1.1.1.1.6.2    yamt         end = SPN_WHITESPACE_CHARS(end+1);
    380  1.1.1.1.6.2    yamt 
    381  1.1.1.1.6.2    yamt     return end;
    382          1.1  kardel }
    383          1.1  kardel 
    384  1.1.1.1.6.2    yamt static char *
    385  1.1.1.1.6.2    yamt trim_quotes(char * arg)
    386  1.1.1.1.6.2    yamt {
    387  1.1.1.1.6.2    yamt     switch (*arg) {
    388  1.1.1.1.6.2    yamt     case '"':
    389  1.1.1.1.6.2    yamt     case '\'':
    390  1.1.1.1.6.2    yamt         ao_string_cook(arg, NULL);
    391  1.1.1.1.6.2    yamt     }
    392  1.1.1.1.6.2    yamt     return arg;
    393  1.1.1.1.6.2    yamt }
    394          1.1  kardel 
    395  1.1.1.1.6.2    yamt /**
    396  1.1.1.1.6.2    yamt  * See if the option is to be processed in the current scan direction
    397  1.1.1.1.6.2    yamt  * (-1 or +1).
    398          1.1  kardel  */
    399  1.1.1.1.6.2    yamt static bool
    400  1.1.1.1.6.2    yamt direction_ok(opt_state_mask_t f, int dir)
    401          1.1  kardel {
    402  1.1.1.1.6.2    yamt     if (dir == 0)
    403  1.1.1.1.6.2    yamt         return true;
    404          1.1  kardel 
    405  1.1.1.1.6.2    yamt     switch (f & (OPTST_IMM|OPTST_DISABLE_IMM)) {
    406          1.1  kardel     case 0:
    407          1.1  kardel         /*
    408          1.1  kardel          *  The selected option has no immediate action.
    409          1.1  kardel          *  THEREFORE, if the direction is PRESETTING
    410          1.1  kardel          *  THEN we skip this option.
    411          1.1  kardel          */
    412  1.1.1.1.6.2    yamt         if (PRESETTING(dir))
    413  1.1.1.1.6.2    yamt             return false;
    414          1.1  kardel         break;
    415          1.1  kardel 
    416          1.1  kardel     case OPTST_IMM:
    417  1.1.1.1.6.2    yamt         if (PRESETTING(dir)) {
    418          1.1  kardel             /*
    419          1.1  kardel              *  We are in the presetting direction with an option we handle
    420          1.1  kardel              *  immediately for enablement, but normally for disablement.
    421          1.1  kardel              *  Therefore, skip if disabled.
    422          1.1  kardel              */
    423  1.1.1.1.6.2    yamt             if ((f & OPTST_DISABLED) == 0)
    424  1.1.1.1.6.2    yamt                 return false;
    425          1.1  kardel         } else {
    426          1.1  kardel             /*
    427          1.1  kardel              *  We are in the processing direction with an option we handle
    428          1.1  kardel              *  immediately for enablement, but normally for disablement.
    429          1.1  kardel              *  Therefore, skip if NOT disabled.
    430          1.1  kardel              */
    431  1.1.1.1.6.2    yamt             if ((f & OPTST_DISABLED) != 0)
    432  1.1.1.1.6.2    yamt                 return false;
    433          1.1  kardel         }
    434          1.1  kardel         break;
    435          1.1  kardel 
    436          1.1  kardel     case OPTST_DISABLE_IMM:
    437  1.1.1.1.6.2    yamt         if (PRESETTING(dir)) {
    438          1.1  kardel             /*
    439          1.1  kardel              *  We are in the presetting direction with an option we handle
    440          1.1  kardel              *  immediately for disablement, but normally for disablement.
    441          1.1  kardel              *  Therefore, skip if NOT disabled.
    442          1.1  kardel              */
    443  1.1.1.1.6.2    yamt             if ((f & OPTST_DISABLED) != 0)
    444  1.1.1.1.6.2    yamt                 return false;
    445          1.1  kardel         } else {
    446          1.1  kardel             /*
    447          1.1  kardel              *  We are in the processing direction with an option we handle
    448          1.1  kardel              *  immediately for disablement, but normally for disablement.
    449          1.1  kardel              *  Therefore, skip if disabled.
    450          1.1  kardel              */
    451  1.1.1.1.6.2    yamt             if ((f & OPTST_DISABLED) == 0)
    452  1.1.1.1.6.2    yamt                 return false;
    453          1.1  kardel         }
    454          1.1  kardel         break;
    455          1.1  kardel 
    456          1.1  kardel     case OPTST_IMM|OPTST_DISABLE_IMM:
    457          1.1  kardel         /*
    458          1.1  kardel          *  The selected option is always for immediate action.
    459          1.1  kardel          *  THEREFORE, if the direction is PROCESSING
    460          1.1  kardel          *  THEN we skip this option.
    461          1.1  kardel          */
    462  1.1.1.1.6.2    yamt         if (PROCESSING(dir))
    463  1.1.1.1.6.2    yamt             return false;
    464          1.1  kardel         break;
    465          1.1  kardel     }
    466  1.1.1.1.6.2    yamt     return true;
    467  1.1.1.1.6.2    yamt }
    468  1.1.1.1.6.2    yamt 
    469  1.1.1.1.6.2    yamt /**
    470  1.1.1.1.6.2    yamt  *  Load an option from a block of text.  The text must start with the
    471  1.1.1.1.6.2    yamt  *  configurable/option name and be followed by its associated value.
    472  1.1.1.1.6.2    yamt  *  That value may be processed in any of several ways.  See "tOptionLoadMode"
    473  1.1.1.1.6.2    yamt  *  in autoopts.h.
    474  1.1.1.1.6.2    yamt  *
    475  1.1.1.1.6.2    yamt  * @param[in,out] opts       program options descriptor
    476  1.1.1.1.6.2    yamt  * @param[in,out] opt_state  option processing state
    477  1.1.1.1.6.2    yamt  * @param[in,out] line       source line with long option name in it
    478  1.1.1.1.6.2    yamt  * @param[in]     direction  current processing direction (preset or not)
    479  1.1.1.1.6.2    yamt  * @param[in]     load_mode  option loading mode (OPTION_LOAD_*)
    480  1.1.1.1.6.2    yamt  */
    481  1.1.1.1.6.2    yamt LOCAL void
    482  1.1.1.1.6.2    yamt load_opt_line(tOptions * opts, tOptState * opt_state, char * line,
    483  1.1.1.1.6.2    yamt               tDirection direction, tOptionLoadMode load_mode )
    484  1.1.1.1.6.2    yamt {
    485  1.1.1.1.6.2    yamt     /*
    486  1.1.1.1.6.2    yamt      * When parsing a stored line, we only look at the characters after
    487  1.1.1.1.6.2    yamt      * a hyphen.  Long names must always be at least two characters and
    488  1.1.1.1.6.2    yamt      * short options are always exactly one character long.
    489  1.1.1.1.6.2    yamt      */
    490  1.1.1.1.6.2    yamt     line = SPN_LOAD_LINE_SKIP_CHARS(line);
    491  1.1.1.1.6.2    yamt 
    492  1.1.1.1.6.2    yamt     {
    493  1.1.1.1.6.2    yamt         char * arg = assemble_arg_val(line, load_mode);
    494  1.1.1.1.6.2    yamt 
    495  1.1.1.1.6.2    yamt         if (IS_OPTION_NAME_CHAR(line[1])) {
    496  1.1.1.1.6.2    yamt 
    497  1.1.1.1.6.2    yamt             if (! SUCCESSFUL(opt_find_long(opts, line, opt_state)))
    498  1.1.1.1.6.2    yamt                 return;
    499  1.1.1.1.6.2    yamt 
    500  1.1.1.1.6.2    yamt         } else if (! SUCCESSFUL(opt_find_short(opts, *line, opt_state)))
    501  1.1.1.1.6.2    yamt             return;
    502  1.1.1.1.6.2    yamt 
    503  1.1.1.1.6.2    yamt         if ((! CALLED(direction)) && (opt_state->flags & OPTST_NO_INIT))
    504  1.1.1.1.6.2    yamt             return;
    505  1.1.1.1.6.2    yamt 
    506  1.1.1.1.6.2    yamt         opt_state->pzOptArg = trim_quotes(arg);
    507  1.1.1.1.6.2    yamt     }
    508  1.1.1.1.6.2    yamt 
    509  1.1.1.1.6.2    yamt     if (! direction_ok(opt_state->flags, direction))
    510  1.1.1.1.6.2    yamt         return;
    511          1.1  kardel 
    512          1.1  kardel     /*
    513          1.1  kardel      *  Fix up the args.
    514          1.1  kardel      */
    515  1.1.1.1.6.2    yamt     if (OPTST_GET_ARGTYPE(opt_state->pOD->fOptState) == OPARG_TYPE_NONE) {
    516  1.1.1.1.6.2    yamt         if (*opt_state->pzOptArg != NUL)
    517          1.1  kardel             return;
    518  1.1.1.1.6.2    yamt         opt_state->pzOptArg = NULL;
    519          1.1  kardel 
    520  1.1.1.1.6.2    yamt     } else if (opt_state->pOD->fOptState & OPTST_ARG_OPTIONAL) {
    521  1.1.1.1.6.2    yamt         if (*opt_state->pzOptArg == NUL)
    522  1.1.1.1.6.2    yamt              opt_state->pzOptArg = NULL;
    523          1.1  kardel         else {
    524  1.1.1.1.6.2    yamt             AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
    525  1.1.1.1.6.2    yamt             opt_state->flags |= OPTST_ALLOC_ARG;
    526          1.1  kardel         }
    527          1.1  kardel 
    528          1.1  kardel     } else {
    529  1.1.1.1.6.2    yamt         if (*opt_state->pzOptArg == NUL)
    530  1.1.1.1.6.2    yamt              opt_state->pzOptArg = zNil;
    531          1.1  kardel         else {
    532  1.1.1.1.6.2    yamt             AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
    533  1.1.1.1.6.2    yamt             opt_state->flags |= OPTST_ALLOC_ARG;
    534          1.1  kardel         }
    535          1.1  kardel     }
    536          1.1  kardel 
    537          1.1  kardel     {
    538          1.1  kardel         tOptionLoadMode sv = option_load_mode;
    539          1.1  kardel         option_load_mode = load_mode;
    540  1.1.1.1.6.2    yamt         handle_opt(opts, opt_state);
    541          1.1  kardel         option_load_mode = sv;
    542          1.1  kardel     }
    543          1.1  kardel }
    544          1.1  kardel 
    545          1.1  kardel /*=export_func  optionLoadLine
    546          1.1  kardel  *
    547          1.1  kardel  * what:  process a string for an option name and value
    548          1.1  kardel  *
    549  1.1.1.1.6.2    yamt  * arg:   tOptions*,   opts,  program options descriptor
    550  1.1.1.1.6.2    yamt  * arg:   char const*, line,  NUL-terminated text
    551          1.1  kardel  *
    552          1.1  kardel  * doc:
    553          1.1  kardel  *
    554          1.1  kardel  *  This is a client program callable routine for setting options from, for
    555          1.1  kardel  *  example, the contents of a file that they read in.  Only one option may
    556          1.1  kardel  *  appear in the text.  It will be treated as a normal (non-preset) option.
    557          1.1  kardel  *
    558          1.1  kardel  *  When passed a pointer to the option struct and a string, it will find
    559          1.1  kardel  *  the option named by the first token on the string and set the option
    560          1.1  kardel  *  argument to the remainder of the string.  The caller must NUL terminate
    561  1.1.1.1.6.2    yamt  *  the string.  The caller need not skip over any introductory hyphens.
    562  1.1.1.1.6.2    yamt  *  Any embedded new lines will be included in the option
    563          1.1  kardel  *  argument.  If the input looks like one or more quoted strings, then the
    564          1.1  kardel  *  input will be "cooked".  The "cooking" is identical to the string
    565          1.1  kardel  *  formation used in AutoGen definition files (@pxref{basic expression}),
    566          1.1  kardel  *  except that you may not use backquotes.
    567          1.1  kardel  *
    568          1.1  kardel  * err:   Invalid options are silently ignored.  Invalid option arguments
    569          1.1  kardel  *        will cause a warning to print, but the function should return.
    570          1.1  kardel =*/
    571          1.1  kardel void
    572  1.1.1.1.6.2    yamt optionLoadLine(tOptions * opts, char const * line)
    573          1.1  kardel {
    574          1.1  kardel     tOptState st = OPTSTATE_INITIALIZER(SET);
    575  1.1.1.1.6.2    yamt     char *    pz;
    576  1.1.1.1.6.2    yamt     proc_state_mask_t sv_flags = opts->fOptSet;
    577  1.1.1.1.6.2    yamt     opts->fOptSet &= ~OPTPROC_ERRSTOP;
    578  1.1.1.1.6.2    yamt     AGDUPSTR(pz, line, "opt line");
    579  1.1.1.1.6.2    yamt     load_opt_line(opts, &st, pz, DIRECTION_CALLED, OPTION_LOAD_COOKED);
    580  1.1.1.1.6.1    yamt     AGFREE(pz);
    581  1.1.1.1.6.2    yamt     opts->fOptSet = sv_flags;
    582          1.1  kardel }
    583  1.1.1.1.6.2    yamt /** @}
    584  1.1.1.1.6.2    yamt  *
    585          1.1  kardel  * Local Variables:
    586          1.1  kardel  * mode: C
    587          1.1  kardel  * c-file-style: "stroustrup"
    588          1.1  kardel  * indent-tabs-mode: nil
    589          1.1  kardel  * End:
    590          1.1  kardel  * end of autoopts/load.c */
    591