Home | History | Annotate | Line # | Download | only in libopts
load.c revision 1.3.4.1.2.1
      1  1.3.4.1.2.1       riz /*	$NetBSD: load.c,v 1.3.4.1.2.1 2015/11/08 01:55:34 riz Exp $	*/
      2          1.1    kardel 
      3          1.1    kardel 
      4          1.2  christos /**
      5          1.2  christos  *  \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.3  christos  * @addtogroup autoopts
     12          1.3  christos  * @{
     13          1.3  christos  */
     14          1.3  christos /*
     15          1.1    kardel  *  This file is part of AutoOpts, a companion to AutoGen.
     16          1.1    kardel  *  AutoOpts is free software.
     17  1.3.4.1.2.1       riz  *  AutoOpts is Copyright (C) 1992-2015 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.3  christos  *  These files have the following sha256 sums:
     30          1.1    kardel  *
     31          1.3  christos  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
     32          1.3  christos  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
     33          1.3  christos  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
     34          1.1    kardel  */
     35          1.1    kardel 
     36          1.1    kardel /* = = = START-STATIC-FORWARD = = = */
     37          1.3  christos static bool
     38          1.3  christos get_realpath(char * buf, size_t b_sz);
     39          1.3  christos 
     40          1.3  christos static bool
     41          1.3  christos add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path);
     42          1.3  christos 
     43          1.3  christos static bool
     44          1.3  christos add_env_val(char * buf, int buf_sz, char const * name);
     45          1.1    kardel 
     46          1.3  christos static char *
     47          1.3  christos assemble_arg_val(char * txt, tOptionLoadMode mode);
     48          1.3  christos 
     49          1.3  christos static char *
     50          1.3  christos trim_quotes(char * arg);
     51          1.3  christos 
     52          1.3  christos static bool
     53          1.3  christos direction_ok(opt_state_mask_t f, int dir);
     54          1.1    kardel /* = = = END-STATIC-FORWARD = = = */
     55          1.1    kardel 
     56          1.3  christos static bool
     57          1.3  christos get_realpath(char * buf, size_t b_sz)
     58          1.3  christos {
     59          1.3  christos #if defined(HAVE_CANONICALIZE_FILE_NAME)
     60          1.3  christos     {
     61          1.3  christos         size_t name_len;
     62          1.3  christos 
     63          1.3  christos         char * pz = canonicalize_file_name(buf);
     64          1.3  christos         if (pz == NULL)
     65          1.3  christos             return false;
     66          1.3  christos 
     67          1.3  christos         name_len = strlen(pz);
     68          1.3  christos         if (name_len >= (size_t)b_sz) {
     69          1.3  christos             free(pz);
     70          1.3  christos             return false;
     71          1.3  christos         }
     72          1.3  christos 
     73          1.3  christos         memcpy(buf, pz, name_len + 1);
     74          1.3  christos         free(pz);
     75          1.3  christos     }
     76          1.3  christos 
     77          1.3  christos #elif defined(HAVE_REALPATH)
     78          1.3  christos     {
     79          1.3  christos         size_t name_len;
     80          1.3  christos         char z[PATH_MAX+1];
     81          1.3  christos 
     82          1.3  christos         if (realpath(buf, z) == NULL)
     83          1.3  christos             return false;
     84          1.3  christos 
     85          1.3  christos         name_len = strlen(z);
     86          1.3  christos         if (name_len >= b_sz)
     87          1.3  christos             return false;
     88          1.3  christos 
     89          1.3  christos         memcpy(buf, z, name_len + 1);
     90          1.3  christos     }
     91          1.3  christos #endif
     92          1.3  christos     return true;
     93          1.3  christos }
     94          1.3  christos 
     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.3.4.1.2.1       riz  * arg:   + char *       + p_buf     + The result buffer +
    100  1.3.4.1.2.1       riz  * arg:   + int          + b_sz      + The size of this buffer +
    101  1.3.4.1.2.1       riz  * arg:   + char const * + fname     + The input name +
    102  1.3.4.1.2.1       riz  * arg:   + char const * + prg_path  + The full path of the current program +
    103          1.1    kardel  *
    104          1.3  christos  * ret-type: bool
    105          1.3  christos  * 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.2  christos  *  This routine will copy the @code{pzName} input name into the
    114          1.2  christos  *  @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.3  christos  * 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.3  christos bool
    147          1.3  christos optionMakePath(char * p_buf, int b_sz, char const * fname, char const * prg_path)
    148          1.1    kardel {
    149          1.3  christos     {
    150          1.3  christos         size_t len = strlen(fname);
    151          1.1    kardel 
    152          1.3  christos         if (((size_t)b_sz <= len) || (len == 0))
    153          1.3  christos             return false;
    154          1.3  christos     }
    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.3  christos     if (*fname != '$') {
    160          1.3  christos         char   const * src = fname;
    161          1.3  christos         char * dst = p_buf;
    162          1.3  christos         int    ct  = b_sz;
    163          1.1    kardel 
    164          1.1    kardel         for (;;) {
    165          1.3  christos             if ( (*(dst++) = *(src++)) == NUL)
    166          1.1    kardel                 break;
    167          1.1    kardel             if (--ct <= 0)
    168          1.3  christos                 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.3  christos     else switch (fname[1]) {
    178          1.1    kardel     case NUL:
    179          1.3  christos         return false;
    180          1.1    kardel 
    181          1.1    kardel     case '$':
    182          1.3  christos         if (! add_prog_path(p_buf, b_sz, fname, prg_path))
    183          1.3  christos             return false;
    184          1.1    kardel         break;
    185          1.1    kardel 
    186          1.1    kardel     case '@':
    187          1.2  christos         if (program_pkgdatadir[0] == NUL)
    188          1.3  christos             return false;
    189          1.1    kardel 
    190          1.3  christos         if (snprintf(p_buf, (size_t)b_sz, "%s%s",
    191          1.3  christos                      program_pkgdatadir, fname + 2) >= b_sz)
    192          1.3  christos             return false;
    193          1.1    kardel         break;
    194          1.1    kardel 
    195          1.1    kardel     default:
    196          1.3  christos         if (! add_env_val(p_buf, b_sz, fname))
    197          1.3  christos             return false;
    198          1.1    kardel     }
    199          1.1    kardel 
    200          1.3  christos     return get_realpath(p_buf, b_sz);
    201          1.1    kardel }
    202          1.1    kardel 
    203          1.3  christos /**
    204          1.3  christos  * convert a leading "$$" into a path to the executable.
    205          1.3  christos  */
    206          1.3  christos static bool
    207          1.3  christos add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path)
    208          1.1    kardel {
    209          1.3  christos     char const *   path;
    210          1.3  christos     char const *   pz;
    211          1.1    kardel     int     skip = 2;
    212          1.1    kardel 
    213          1.3  christos     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.3  christos         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.3  christos     if (strchr(prg_path, DIRCH) != NULL)
    228          1.3  christos         path = prg_path;
    229          1.1    kardel     else {
    230  1.3.4.1.2.1       riz         path = pathfind(getenv("PATH"), prg_path, "rx");
    231          1.1    kardel 
    232          1.3  christos         if (path == NULL)
    233          1.3  christos             return false;
    234          1.1    kardel     }
    235          1.1    kardel 
    236          1.3  christos     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.3  christos         return false;
    244          1.1    kardel 
    245          1.3  christos     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.3  christos     if ((unsigned)(pz - path) + 1 + strlen(fname) >= (unsigned)b_sz)
    252          1.3  christos         return false;
    253          1.1    kardel 
    254          1.3  christos     memcpy(buf, path, (size_t)((pz - path)+1));
    255          1.3  christos     strcpy(buf + (pz - path) + 1, fname);
    256          1.1    kardel 
    257          1.1    kardel     /*
    258          1.3  christos      *  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.3  christos     if (path != prg_path)
    262          1.3  christos         AGFREE(path);
    263          1.3  christos     return true;
    264          1.1    kardel }
    265          1.1    kardel 
    266          1.3  christos /**
    267          1.3  christos  * Add an environment variable value.
    268          1.3  christos  */
    269          1.3  christos static bool
    270          1.3  christos add_env_val(char * buf, int buf_sz, char const * name)
    271          1.1    kardel {
    272          1.3  christos     char * dir_part = buf;
    273          1.1    kardel 
    274          1.1    kardel     for (;;) {
    275          1.3  christos         int ch = (int)*++name;
    276          1.1    kardel         if (! IS_VALUE_NAME_CHAR(ch))
    277          1.1    kardel             break;
    278          1.3  christos         *(dir_part++) = (char)ch;
    279          1.1    kardel     }
    280          1.1    kardel 
    281          1.3  christos     if (dir_part == buf)
    282          1.3  christos         return false;
    283          1.1    kardel 
    284          1.3  christos     *dir_part = NUL;
    285          1.1    kardel 
    286          1.3  christos     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.3  christos     if (dir_part == NULL)
    292          1.3  christos         return false;
    293          1.1    kardel 
    294          1.3  christos     if (strlen(dir_part) + 1 + strlen(name) >= (unsigned)buf_sz)
    295          1.3  christos         return false;
    296          1.1    kardel 
    297          1.3  christos     sprintf(buf, "%s%s", dir_part, name);
    298          1.3  christos     return true;
    299          1.1    kardel }
    300          1.1    kardel 
    301          1.3  christos /**
    302          1.3  christos  * Trim leading and trailing white space.
    303          1.3  christos  * If we are cooking the text and the text is quoted, then "cook"
    304          1.3  christos  * the string.  To cook, the string must be quoted.
    305          1.3  christos  *
    306          1.3  christos  * @param[in,out] txt  the input and output string
    307          1.3  christos  * @param[in]     mode the handling mode (cooking method)
    308          1.3  christos  */
    309          1.1    kardel LOCAL void
    310          1.3  christos munge_str(char * txt, tOptionLoadMode mode)
    311          1.1    kardel {
    312          1.3  christos     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.3  christos     if (IS_WHITESPACE_CHAR(*txt)) {
    318          1.3  christos         char * src = SPN_WHITESPACE_CHARS(txt+1);
    319          1.3  christos         size_t l   = strlen(src) + 1;
    320          1.3  christos         memmove(txt, src, l);
    321          1.3  christos         pzE = txt + l - 1;
    322          1.3  christos 
    323          1.1    kardel     } else
    324          1.3  christos         pzE = txt + strlen(txt);
    325          1.1    kardel 
    326          1.3  christos     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.3  christos     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.3  christos     (void)ao_string_cook(txt, NULL);
    345          1.1    kardel }
    346          1.1    kardel 
    347          1.3  christos static char *
    348          1.3  christos assemble_arg_val(char * txt, tOptionLoadMode mode)
    349          1.1    kardel {
    350          1.3  christos     char * end = strpbrk(txt, ARG_BREAK_STR);
    351          1.3  christos     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.3  christos     if (end == NULL)
    357          1.3  christos         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.3  christos         *(end++) = NUL;
    366          1.3  christos         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.3  christos     space_break = IS_WHITESPACE_CHAR(*end);
    375          1.3  christos     *(end++) = NUL;
    376          1.1    kardel 
    377          1.3  christos     end = SPN_WHITESPACE_CHARS(end);
    378          1.3  christos     if (space_break && ((*end == ':') || (*end == '=')))
    379          1.3  christos         end = SPN_WHITESPACE_CHARS(end+1);
    380          1.3  christos 
    381          1.3  christos     return end;
    382          1.1    kardel }
    383          1.1    kardel 
    384          1.3  christos static char *
    385          1.3  christos trim_quotes(char * arg)
    386          1.3  christos {
    387          1.3  christos     switch (*arg) {
    388          1.3  christos     case '"':
    389          1.3  christos     case '\'':
    390          1.3  christos         ao_string_cook(arg, NULL);
    391          1.3  christos     }
    392          1.3  christos     return arg;
    393          1.3  christos }
    394          1.1    kardel 
    395          1.3  christos /**
    396          1.3  christos  * See if the option is to be processed in the current scan direction
    397          1.3  christos  * (-1 or +1).
    398          1.1    kardel  */
    399          1.3  christos static bool
    400          1.3  christos direction_ok(opt_state_mask_t f, int dir)
    401          1.1    kardel {
    402          1.3  christos     if (dir == 0)
    403          1.3  christos         return true;
    404          1.1    kardel 
    405          1.3  christos     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.3  christos         if (PRESETTING(dir))
    413          1.3  christos             return false;
    414          1.1    kardel         break;
    415          1.1    kardel 
    416          1.1    kardel     case OPTST_IMM:
    417          1.3  christos         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.3  christos             if ((f & OPTST_DISABLED) == 0)
    424          1.3  christos                 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.3  christos             if ((f & OPTST_DISABLED) != 0)
    432          1.3  christos                 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.3  christos         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.3  christos             if ((f & OPTST_DISABLED) != 0)
    444          1.3  christos                 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.3  christos             if ((f & OPTST_DISABLED) == 0)
    452          1.3  christos                 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.3  christos         if (PROCESSING(dir))
    463          1.3  christos             return false;
    464          1.3  christos         break;
    465          1.3  christos     }
    466          1.3  christos     return true;
    467          1.3  christos }
    468          1.3  christos 
    469          1.3  christos /**
    470          1.3  christos  *  Load an option from a block of text.  The text must start with the
    471          1.3  christos  *  configurable/option name and be followed by its associated value.
    472          1.3  christos  *  That value may be processed in any of several ways.  See "tOptionLoadMode"
    473          1.3  christos  *  in autoopts.h.
    474          1.3  christos  *
    475          1.3  christos  * @param[in,out] opts       program options descriptor
    476          1.3  christos  * @param[in,out] opt_state  option processing state
    477          1.3  christos  * @param[in,out] line       source line with long option name in it
    478          1.3  christos  * @param[in]     direction  current processing direction (preset or not)
    479          1.3  christos  * @param[in]     load_mode  option loading mode (OPTION_LOAD_*)
    480          1.3  christos  */
    481          1.3  christos LOCAL void
    482          1.3  christos load_opt_line(tOptions * opts, tOptState * opt_state, char * line,
    483          1.3  christos               tDirection direction, tOptionLoadMode load_mode )
    484          1.3  christos {
    485          1.3  christos     /*
    486          1.3  christos      * When parsing a stored line, we only look at the characters after
    487          1.3  christos      * a hyphen.  Long names must always be at least two characters and
    488          1.3  christos      * short options are always exactly one character long.
    489          1.3  christos      */
    490          1.3  christos     line = SPN_LOAD_LINE_SKIP_CHARS(line);
    491          1.3  christos 
    492          1.3  christos     {
    493          1.3  christos         char * arg = assemble_arg_val(line, load_mode);
    494          1.3  christos 
    495          1.3  christos         if (IS_OPTION_NAME_CHAR(line[1])) {
    496          1.3  christos 
    497          1.3  christos             if (! SUCCESSFUL(opt_find_long(opts, line, opt_state)))
    498          1.3  christos                 return;
    499          1.3  christos 
    500          1.3  christos         } else if (! SUCCESSFUL(opt_find_short(opts, *line, opt_state)))
    501          1.3  christos             return;
    502          1.3  christos 
    503          1.3  christos         if ((! CALLED(direction)) && (opt_state->flags & OPTST_NO_INIT))
    504          1.1    kardel             return;
    505          1.3  christos 
    506          1.3  christos         opt_state->pzOptArg = trim_quotes(arg);
    507          1.1    kardel     }
    508          1.1    kardel 
    509          1.3  christos     if (! direction_ok(opt_state->flags, direction))
    510          1.3  christos         return;
    511          1.3  christos 
    512          1.1    kardel     /*
    513          1.1    kardel      *  Fix up the args.
    514          1.1    kardel      */
    515          1.3  christos     if (OPTST_GET_ARGTYPE(opt_state->pOD->fOptState) == OPARG_TYPE_NONE) {
    516          1.3  christos         if (*opt_state->pzOptArg != NUL)
    517          1.1    kardel             return;
    518          1.3  christos         opt_state->pzOptArg = NULL;
    519          1.1    kardel 
    520          1.3  christos     } else if (opt_state->pOD->fOptState & OPTST_ARG_OPTIONAL) {
    521          1.3  christos         if (*opt_state->pzOptArg == NUL)
    522          1.3  christos              opt_state->pzOptArg = NULL;
    523          1.1    kardel         else {
    524          1.3  christos             AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
    525          1.3  christos             opt_state->flags |= OPTST_ALLOC_ARG;
    526          1.1    kardel         }
    527          1.1    kardel 
    528          1.1    kardel     } else {
    529          1.3  christos         if (*opt_state->pzOptArg == NUL)
    530          1.3  christos              opt_state->pzOptArg = zNil;
    531          1.1    kardel         else {
    532          1.3  christos             AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
    533          1.3  christos             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.3  christos         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.3.4.1.2.1       riz  * arg:   tOptions *,   opts,  program options descriptor
    550  1.3.4.1.2.1       riz  * 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.3  christos  *  the string.  The caller need not skip over any introductory hyphens.
    562          1.3  christos  *  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.3  christos optionLoadLine(tOptions * opts, char const * line)
    573          1.1    kardel {
    574          1.1    kardel     tOptState st = OPTSTATE_INITIALIZER(SET);
    575          1.3  christos     char *    pz;
    576          1.3  christos     proc_state_mask_t sv_flags = opts->fOptSet;
    577          1.3  christos     opts->fOptSet &= ~OPTPROC_ERRSTOP;
    578          1.3  christos     AGDUPSTR(pz, line, "opt line");
    579          1.3  christos     load_opt_line(opts, &st, pz, DIRECTION_CALLED, OPTION_LOAD_COOKED);
    580          1.2  christos     AGFREE(pz);
    581          1.3  christos     opts->fOptSet = sv_flags;
    582          1.1    kardel }
    583          1.3  christos /** @}
    584          1.3  christos  *
    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