diff options
Diffstat (limited to 'src/libopts/load.c')
-rw-r--r-- | src/libopts/load.c | 178 |
1 files changed, 97 insertions, 81 deletions
diff --git a/src/libopts/load.c b/src/libopts/load.c index 1ea7808061..7f69cdad00 100644 --- a/src/libopts/load.c +++ b/src/libopts/load.c @@ -1,15 +1,18 @@ /** * \file load.c - * Time-stamp: "2012-03-31 13:13:34 bkorb" * * This file contains the routines that deal with processing text strings * for options, either from a NUL-terminated string passed in or from an * rc/ini file. * + * @addtogroup autoopts + * @{ + */ +/* * This file is part of AutoOpts, a companion to AutoGen. * AutoOpts is free software. - * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved + * AutoOpts is Copyright (C) 1992-2013 by Bruce Korb - all rights reserved * * AutoOpts is available under any one of two licenses. The license * in use must be one of these two and the choice is under the control @@ -21,23 +24,23 @@ * The Modified Berkeley Software Distribution License * See the file "COPYING.mbsd" * - * These files have the following md5sums: + * These files have the following sha256 sums: * - * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3 - * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3 - * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd + * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3 + * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3 + * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd */ /* = = = START-STATIC-FORWARD = = = */ static bool -add_prog_path(char * pzBuf, int bufSize, char const * pzName, +add_prog_path(char * pzBuf, int b_sz, char const * pzName, char const * pzProgPath); static bool -add_env_val(char * pzBuf, int bufSize, char const * pzName); +add_env_val(char * buf, int buf_sz, char const * name); static char * -assemble_arg_val(char * pzTxt, tOptionLoadMode mode); +assemble_arg_val(char * txt, tOptionLoadMode mode); /* = = = END-STATIC-FORWARD = = = */ /*=export_func optionMakePath @@ -92,12 +95,12 @@ assemble_arg_val(char * pzTxt, tOptionLoadMode mode); * errors (cannot resolve the resulting path). =*/ bool -optionMakePath(char * pzBuf, int bufSize, char const * pzName, +optionMakePath(char * pzBuf, int b_sz, char const * pzName, char const * pzProgPath) { size_t name_len = strlen(pzName); - if (((size_t)bufSize <= name_len) || (name_len == 0)) + if (((size_t)b_sz <= name_len) || (name_len == 0)) return false; /* @@ -106,7 +109,7 @@ optionMakePath(char * pzBuf, int bufSize, char const * pzName, if (*pzName != '$') { char const* pzS = pzName; char* pzD = pzBuf; - int ct = bufSize; + int ct = b_sz; for (;;) { if ( (*(pzD++) = *(pzS++)) == NUL) @@ -126,7 +129,7 @@ optionMakePath(char * pzBuf, int bufSize, char const * pzName, return false; case '$': - if (! add_prog_path(pzBuf, bufSize, pzName, pzProgPath)) + if (! add_prog_path(pzBuf, b_sz, pzName, pzProgPath)) return false; break; @@ -134,13 +137,13 @@ optionMakePath(char * pzBuf, int bufSize, char const * pzName, if (program_pkgdatadir[0] == NUL) return false; - if (snprintf(pzBuf, bufSize, "%s%s", program_pkgdatadir, pzName + 2) - >= bufSize) + if (snprintf(pzBuf, (size_t)b_sz, "%s%s", + program_pkgdatadir, pzName + 2) >= b_sz) return false; break; default: - if (! add_env_val(pzBuf, bufSize, pzName)) + if (! add_env_val(pzBuf, b_sz, pzName)) return false; } @@ -151,7 +154,7 @@ optionMakePath(char * pzBuf, int bufSize, char const * pzName, return false; name_len = strlen(pz); - if (name_len >= (size_t)bufSize) { + if (name_len >= (size_t)b_sz) { free(pz); return false; } @@ -168,7 +171,7 @@ optionMakePath(char * pzBuf, int bufSize, char const * pzName, return false; name_len = strlen(z); - if (name_len >= bufSize) + if (name_len >= b_sz) return false; memcpy(pzBuf, z, name_len + 1); @@ -179,7 +182,7 @@ optionMakePath(char * pzBuf, int bufSize, char const * pzName, } static bool -add_prog_path(char * pzBuf, int bufSize, char const * pzName, +add_prog_path(char * pzBuf, int b_sz, char const * pzName, char const * pzProgPath) { char const* pzPath; @@ -224,7 +227,7 @@ add_prog_path(char * pzBuf, int bufSize, char const * pzName, * Concatenate the file name to the end of the executable path. * The result may be either a file or a directory. */ - if ((pz - pzPath)+1 + strlen(pzName) >= (unsigned)bufSize) + if ((unsigned)(pz - pzPath) + 1 + strlen(pzName) >= (unsigned)b_sz) return false; memcpy(pzBuf, pzPath, (size_t)((pz - pzPath)+1)); @@ -239,64 +242,70 @@ add_prog_path(char * pzBuf, int bufSize, char const * pzName, return true; } - static bool -add_env_val(char * pzBuf, int bufSize, char const * pzName) +add_env_val(char * buf, int buf_sz, char const * name) { - char * pzDir = pzBuf; + char * dir_part = buf; for (;;) { - int ch = (int)*++pzName; + int ch = (int)*++name; if (! IS_VALUE_NAME_CHAR(ch)) break; - *(pzDir++) = (char)ch; + *(dir_part++) = (char)ch; } - if (pzDir == pzBuf) + if (dir_part == buf) return false; - *pzDir = NUL; + *dir_part = NUL; - pzDir = getenv(pzBuf); + dir_part = getenv(buf); /* * Environment value not found -- skip the home list entry */ - if (pzDir == NULL) + if (dir_part == NULL) return false; - if (strlen(pzDir) + 1 + strlen(pzName) >= (unsigned)bufSize) + if (strlen(dir_part) + 1 + strlen(name) >= (unsigned)buf_sz) return false; - sprintf(pzBuf, "%s%s", pzDir, pzName); + sprintf(buf, "%s%s", dir_part, name); return true; } - +/** + * Trim leading and trailing white space. + * If we are cooking the text and the text is quoted, then "cook" + * the string. To cook, the string must be quoted. + * + * @param[in,out] txt the input and output string + * @param[in] mode the handling mode (cooking method) + */ LOCAL void -mungeString(char* pzTxt, tOptionLoadMode mode) +munge_str(char * txt, tOptionLoadMode mode) { char * pzE; if (mode == OPTION_LOAD_KEEP) return; - if (IS_WHITESPACE_CHAR(*pzTxt)) { - char * pzS = SPN_WHITESPACE_CHARS(pzTxt+1); + if (IS_WHITESPACE_CHAR(*txt)) { + char * pzS = SPN_WHITESPACE_CHARS(txt+1); size_t l = strlen(pzS) + 1; - memmove(pzTxt, pzS, l); - pzE = pzTxt + l - 1; + memmove(txt, pzS, l); + pzE = txt + l - 1; } else - pzE = pzTxt + strlen(pzTxt); + pzE = txt + strlen(txt); - pzE = SPN_WHITESPACE_BACK(pzTxt, pzE); + pzE = SPN_WHITESPACE_BACK(txt, pzE); *pzE = NUL; if (mode == OPTION_LOAD_UNCOOKED) return; - switch (*pzTxt) { + switch (*txt) { default: return; case '"': case '\'': break; @@ -308,21 +317,20 @@ mungeString(char* pzTxt, tOptionLoadMode mode) case '\'': break; } - (void)ao_string_cook(pzTxt, NULL); + (void)ao_string_cook(txt, NULL); } - static char * -assemble_arg_val(char * pzTxt, tOptionLoadMode mode) +assemble_arg_val(char * txt, tOptionLoadMode mode) { - char* pzEnd = strpbrk(pzTxt, ARG_BREAK_STR); + char* pzEnd = strpbrk(txt, ARG_BREAK_STR); int space_break; /* * Not having an argument to a configurable name is okay. */ if (pzEnd == NULL) - return pzTxt + strlen(pzTxt); + return txt + strlen(txt); /* * If we are keeping all whitespace, then the modevalue starts with the @@ -349,34 +357,41 @@ assemble_arg_val(char * pzTxt, tOptionLoadMode mode) return pzEnd; } - -/* +/** * Load an option from a block of text. The text must start with the * configurable/option name and be followed by its associated value. * That value may be processed in any of several ways. See "tOptionLoadMode" * in autoopts.h. + * + * @param[in,out] opts program options descriptor + * @param[in,out] opt_state option processing state + * @param[in,out] line source line with long option name in it + * @param[in] direction current processing direction (preset or not) + * @param[in] load_mode option loading mode (OPTION_LOAD_*) */ LOCAL void loadOptionLine( - tOptions* pOpts, - tOptState* pOS, - char* pzLine, + tOptions * opts, + tOptState * opt_state, + char * line, tDirection direction, tOptionLoadMode load_mode ) { - pzLine = SPN_WHITESPACE_CHARS(pzLine); + line = SPN_LOAD_LINE_SKIP_CHARS(line); { - char* pzArg = assemble_arg_val(pzLine, load_mode); + char * arg = assemble_arg_val(line, load_mode); - if (! SUCCESSFUL(opt_find_long(pOpts, pzLine, pOS))) + if (! SUCCESSFUL(opt_find_long(opts, line, opt_state))) return; - if (pOS->flags & OPTST_NO_INIT) + + if (opt_state->flags & OPTST_NO_INIT) return; - pOS->pzOptArg = pzArg; + + opt_state->pzOptArg = arg; } - switch (pOS->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) { + switch (opt_state->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) { case 0: /* * The selected option has no immediate action. @@ -394,7 +409,7 @@ loadOptionLine( * immediately for enablement, but normally for disablement. * Therefore, skip if disabled. */ - if ((pOS->flags & OPTST_DISABLED) == 0) + if ((opt_state->flags & OPTST_DISABLED) == 0) return; } else { /* @@ -402,7 +417,7 @@ loadOptionLine( * immediately for enablement, but normally for disablement. * Therefore, skip if NOT disabled. */ - if ((pOS->flags & OPTST_DISABLED) != 0) + if ((opt_state->flags & OPTST_DISABLED) != 0) return; } break; @@ -414,7 +429,7 @@ loadOptionLine( * immediately for disablement, but normally for disablement. * Therefore, skip if NOT disabled. */ - if ((pOS->flags & OPTST_DISABLED) != 0) + if ((opt_state->flags & OPTST_DISABLED) != 0) return; } else { /* @@ -422,7 +437,7 @@ loadOptionLine( * immediately for disablement, but normally for disablement. * Therefore, skip if disabled. */ - if ((pOS->flags & OPTST_DISABLED) == 0) + if ((opt_state->flags & OPTST_DISABLED) == 0) return; } break; @@ -441,43 +456,42 @@ loadOptionLine( /* * Fix up the args. */ - if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) { - if (*pOS->pzOptArg != NUL) + if (OPTST_GET_ARGTYPE(opt_state->pOD->fOptState) == OPARG_TYPE_NONE) { + if (*opt_state->pzOptArg != NUL) return; - pOS->pzOptArg = NULL; + opt_state->pzOptArg = NULL; - } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) { - if (*pOS->pzOptArg == NUL) - pOS->pzOptArg = NULL; + } else if (opt_state->pOD->fOptState & OPTST_ARG_OPTIONAL) { + if (*opt_state->pzOptArg == NUL) + opt_state->pzOptArg = NULL; else { - AGDUPSTR(pOS->pzOptArg, pOS->pzOptArg, "option argument"); - pOS->flags |= OPTST_ALLOC_ARG; + AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg"); + opt_state->flags |= OPTST_ALLOC_ARG; } } else { - if (*pOS->pzOptArg == NUL) - pOS->pzOptArg = zNil; + if (*opt_state->pzOptArg == NUL) + opt_state->pzOptArg = zNil; else { - AGDUPSTR(pOS->pzOptArg, pOS->pzOptArg, "option argument"); - pOS->flags |= OPTST_ALLOC_ARG; + AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg"); + opt_state->flags |= OPTST_ALLOC_ARG; } } { tOptionLoadMode sv = option_load_mode; option_load_mode = load_mode; - handle_opt(pOpts, pOS); + handle_opt(opts, opt_state); option_load_mode = sv; } } - /*=export_func optionLoadLine * * what: process a string for an option name and value * - * arg: tOptions*, pOpts, program options descriptor - * arg: char const*, pzLine, NUL-terminated text + * arg: tOptions*, opts, program options descriptor + * arg: char const*, line, NUL-terminated text * * doc: * @@ -488,7 +502,8 @@ loadOptionLine( * When passed a pointer to the option struct and a string, it will find * the option named by the first token on the string and set the option * argument to the remainder of the string. The caller must NUL terminate - * the string. Any embedded new lines will be included in the option + * the string. The caller need not skip over any introductory hyphens. + * Any embedded new lines will be included in the option * argument. If the input looks like one or more quoted strings, then the * input will be "cooked". The "cooking" is identical to the string * formation used in AutoGen definition files (@pxref{basic expression}), @@ -498,15 +513,16 @@ loadOptionLine( * will cause a warning to print, but the function should return. =*/ void -optionLoadLine(tOptions * pOpts, char const * pzLine) +optionLoadLine(tOptions * opts, char const * line) { tOptState st = OPTSTATE_INITIALIZER(SET); char* pz; - AGDUPSTR(pz, pzLine, "user option line"); - loadOptionLine(pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED); + AGDUPSTR(pz, line, "user option line"); + loadOptionLine(opts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED); AGFREE(pz); } -/* +/** @} + * * Local Variables: * mode: C * c-file-style: "stroustrup" |