5 * This file contains the routines that deal with processing text strings
6 * for options, either from a NUL-terminated string passed in or from an
13 * This file is part of AutoOpts, a companion to AutoGen.
14 * AutoOpts is free software.
15 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
17 * AutoOpts is available under any one of two licenses. The license
18 * in use must be one of these two and the choice is under the control
19 * of the user of the license.
21 * The GNU Lesser General Public License, version 3 or later
22 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
24 * The Modified Berkeley Software Distribution License
25 * See the file "COPYING.mbsd"
27 * These files have the following sha256 sums:
29 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
30 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
31 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
34 /* = = = START-STATIC-FORWARD = = = */
36 get_realpath(char * buf, size_t b_sz);
39 add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path);
42 add_env_val(char * buf, int buf_sz, char const * name);
45 assemble_arg_val(char * txt, tOptionLoadMode mode);
48 trim_quotes(char * arg);
51 direction_ok(opt_state_mask_t f, int dir);
52 /* = = = END-STATIC-FORWARD = = = */
55 get_realpath(char * buf, size_t b_sz)
57 #if defined(HAVE_CANONICALIZE_FILE_NAME)
61 char * pz = canonicalize_file_name(buf);
65 name_len = strlen(pz);
66 if (name_len >= (size_t)b_sz) {
71 memcpy(buf, pz, name_len + 1);
75 #elif defined(HAVE_REALPATH)
80 if (realpath(buf, z) == NULL)
87 memcpy(buf, z, name_len + 1);
93 /*=export_func optionMakePath
96 * what: translate and construct a path
97 * arg: + char * + p_buf + The result buffer +
98 * arg: + int + b_sz + The size of this buffer +
99 * arg: + char const * + fname + The input name +
100 * arg: + char const * + prg_path + The full path of the current program +
103 * ret-desc: true if the name was handled, otherwise false.
104 * If the name does not start with ``$'', then it is handled
105 * simply by copying the input name to the output buffer and
106 * resolving the name with either
107 * @code{canonicalize_file_name(3GLIBC)} or @code{realpath(3C)}.
111 * This routine will copy the @code{pzName} input name into the
112 * @code{pzBuf} output buffer, not exceeding @code{bufSize} bytes. If the
113 * first character of the input name is a @code{'$'} character, then there
114 * is special handling:
116 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
117 * searching @code{$PATH} if necessary.
119 * @code{$@} is replaced with the AutoGen package data installation directory
120 * (aka @code{pkgdatadir}).
122 * @code{$NAME} is replaced by the contents of the @code{NAME} environment
123 * variable. If not found, the search fails.
125 * Please note: both @code{$$} and @code{$NAME} must be at the start of the
126 * @code{pzName} string and must either be the entire string or be followed
127 * by the @code{'/'} (backslash on windows) character.
129 * err: @code{false} is returned if:
131 * @bullet{} The input name exceeds @code{bufSize} bytes.
133 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
134 * and the next character is not '/'.
136 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
139 * @bullet{} @code{NAME} is not a known environment variable
141 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
142 * errors (cannot resolve the resulting path).
145 optionMakePath(char * p_buf, int b_sz, char const * fname, char const * prg_path)
148 size_t len = strlen(fname);
150 if (((size_t)b_sz <= len) || (len == 0))
155 * IF not an environment variable, just copy the data
158 char const * src = fname;
163 if ( (*(dst++) = *(src++)) == NUL)
171 * IF the name starts with "$$", then it must be "$$" or
172 * it must start with "$$/". In either event, replace the "$$"
173 * with the path to the executable and append a "/" character.
175 else switch (fname[1]) {
180 if (! add_prog_path(p_buf, b_sz, fname, prg_path))
185 if (program_pkgdatadir[0] == NUL)
188 if (snprintf(p_buf, (size_t)b_sz, "%s%s",
189 program_pkgdatadir, fname + 2) >= b_sz)
194 if (! add_env_val(p_buf, b_sz, fname))
198 return get_realpath(p_buf, b_sz);
202 * convert a leading "$$" into a path to the executable.
205 add_prog_path(char * buf, int b_sz, char const * fname, char const * prg_path)
221 * See if the path is included in the program name.
222 * If it is, we're done. Otherwise, we have to hunt
223 * for the program using "pathfind".
225 if (strchr(prg_path, DIRCH) != NULL)
228 path = pathfind(getenv("PATH"), prg_path, "rx");
234 pz = strrchr(path, DIRCH);
237 * IF we cannot find a directory name separator,
238 * THEN we do not have a path name to our executable file.
246 * Concatenate the file name to the end of the executable path.
247 * The result may be either a file or a directory.
249 if ((unsigned)(pz - path) + 1 + strlen(fname) >= (unsigned)b_sz)
252 memcpy(buf, path, (size_t)((pz - path)+1));
253 strcpy(buf + (pz - path) + 1, fname);
256 * If the "path" path was gotten from "pathfind()", then it was
257 * allocated and we need to deallocate it.
259 if (path != prg_path)
265 * Add an environment variable value.
268 add_env_val(char * buf, int buf_sz, char const * name)
270 char * dir_part = buf;
273 int ch = (int)*++name;
274 if (! IS_VALUE_NAME_CHAR(ch))
276 *(dir_part++) = (char)ch;
284 dir_part = getenv(buf);
287 * Environment value not found -- skip the home list entry
289 if (dir_part == NULL)
292 if (strlen(dir_part) + 1 + strlen(name) >= (unsigned)buf_sz)
295 sprintf(buf, "%s%s", dir_part, name);
300 * Trim leading and trailing white space.
301 * If we are cooking the text and the text is quoted, then "cook"
302 * the string. To cook, the string must be quoted.
304 * @param[in,out] txt the input and output string
305 * @param[in] mode the handling mode (cooking method)
308 munge_str(char * txt, tOptionLoadMode mode)
312 if (mode == OPTION_LOAD_KEEP)
315 if (IS_WHITESPACE_CHAR(*txt)) {
316 char * src = SPN_WHITESPACE_CHARS(txt+1);
317 size_t l = strlen(src) + 1;
318 memmove(txt, src, l);
322 pzE = txt + strlen(txt);
324 pzE = SPN_WHITESPACE_BACK(txt, pzE);
327 if (mode == OPTION_LOAD_UNCOOKED)
342 (void)ao_string_cook(txt, NULL);
346 assemble_arg_val(char * txt, tOptionLoadMode mode)
348 char * end = strpbrk(txt, ARG_BREAK_STR);
352 * Not having an argument to a configurable name is okay.
355 return txt + strlen(txt);
358 * If we are keeping all whitespace, then the modevalue starts with the
359 * character that follows the end of the configurable name, regardless
360 * of which character caused it.
362 if (mode == OPTION_LOAD_KEEP) {
368 * If the name ended on a white space character, remember that
369 * because we'll have to skip over an immediately following ':' or '='
370 * (and the white space following *that*).
372 space_break = IS_WHITESPACE_CHAR(*end);
375 end = SPN_WHITESPACE_CHARS(end);
376 if (space_break && ((*end == ':') || (*end == '=')))
377 end = SPN_WHITESPACE_CHARS(end+1);
383 trim_quotes(char * arg)
388 ao_string_cook(arg, NULL);
394 * See if the option is to be processed in the current scan direction
398 direction_ok(opt_state_mask_t f, int dir)
403 switch (f & (OPTST_IMM|OPTST_DISABLE_IMM)) {
406 * The selected option has no immediate action.
407 * THEREFORE, if the direction is PRESETTING
408 * THEN we skip this option.
415 if (PRESETTING(dir)) {
417 * We are in the presetting direction with an option we handle
418 * immediately for enablement, but normally for disablement.
419 * Therefore, skip if disabled.
421 if ((f & OPTST_DISABLED) == 0)
425 * We are in the processing direction with an option we handle
426 * immediately for enablement, but normally for disablement.
427 * Therefore, skip if NOT disabled.
429 if ((f & OPTST_DISABLED) != 0)
434 case OPTST_DISABLE_IMM:
435 if (PRESETTING(dir)) {
437 * We are in the presetting direction with an option we handle
438 * immediately for disablement, but normally for disablement.
439 * Therefore, skip if NOT disabled.
441 if ((f & OPTST_DISABLED) != 0)
445 * We are in the processing direction with an option we handle
446 * immediately for disablement, but normally for disablement.
447 * Therefore, skip if disabled.
449 if ((f & OPTST_DISABLED) == 0)
454 case OPTST_IMM|OPTST_DISABLE_IMM:
456 * The selected option is always for immediate action.
457 * THEREFORE, if the direction is PROCESSING
458 * THEN we skip this option.
468 * Load an option from a block of text. The text must start with the
469 * configurable/option name and be followed by its associated value.
470 * That value may be processed in any of several ways. See "tOptionLoadMode"
473 * @param[in,out] opts program options descriptor
474 * @param[in,out] opt_state option processing state
475 * @param[in,out] line source line with long option name in it
476 * @param[in] direction current processing direction (preset or not)
477 * @param[in] load_mode option loading mode (OPTION_LOAD_*)
480 load_opt_line(tOptions * opts, tOptState * opt_state, char * line,
481 tDirection direction, tOptionLoadMode load_mode )
484 * When parsing a stored line, we only look at the characters after
485 * a hyphen. Long names must always be at least two characters and
486 * short options are always exactly one character long.
488 line = SPN_LOAD_LINE_SKIP_CHARS(line);
491 char * arg = assemble_arg_val(line, load_mode);
493 if (IS_OPTION_NAME_CHAR(line[1])) {
495 if (! SUCCESSFUL(opt_find_long(opts, line, opt_state)))
498 } else if (! SUCCESSFUL(opt_find_short(opts, *line, opt_state)))
501 if ((! CALLED(direction)) && (opt_state->flags & OPTST_NO_INIT))
504 opt_state->pzOptArg = trim_quotes(arg);
507 if (! direction_ok(opt_state->flags, direction))
513 if (OPTST_GET_ARGTYPE(opt_state->pOD->fOptState) == OPARG_TYPE_NONE) {
514 if (*opt_state->pzOptArg != NUL)
516 opt_state->pzOptArg = NULL;
518 } else if (opt_state->pOD->fOptState & OPTST_ARG_OPTIONAL) {
519 if (*opt_state->pzOptArg == NUL)
520 opt_state->pzOptArg = NULL;
522 AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
523 opt_state->flags |= OPTST_ALLOC_ARG;
527 if (*opt_state->pzOptArg == NUL)
528 opt_state->pzOptArg = zNil;
530 AGDUPSTR(opt_state->pzOptArg, opt_state->pzOptArg, "opt arg");
531 opt_state->flags |= OPTST_ALLOC_ARG;
536 tOptionLoadMode sv = option_load_mode;
537 option_load_mode = load_mode;
538 handle_opt(opts, opt_state);
539 option_load_mode = sv;
543 /*=export_func optionLoadLine
545 * what: process a string for an option name and value
547 * arg: tOptions *, opts, program options descriptor
548 * arg: char const *, line, NUL-terminated text
552 * This is a client program callable routine for setting options from, for
553 * example, the contents of a file that they read in. Only one option may
554 * appear in the text. It will be treated as a normal (non-preset) option.
556 * When passed a pointer to the option struct and a string, it will find
557 * the option named by the first token on the string and set the option
558 * argument to the remainder of the string. The caller must NUL terminate
559 * the string. The caller need not skip over any introductory hyphens.
560 * Any embedded new lines will be included in the option
561 * argument. If the input looks like one or more quoted strings, then the
562 * input will be "cooked". The "cooking" is identical to the string
563 * formation used in AutoGen definition files (@pxref{basic expression}),
564 * except that you may not use backquotes.
566 * err: Invalid options are silently ignored. Invalid option arguments
567 * will cause a warning to print, but the function should return.
570 optionLoadLine(tOptions * opts, char const * line)
572 tOptState st = OPTSTATE_INITIALIZER(SET);
574 proc_state_mask_t sv_flags = opts->fOptSet;
575 opts->fOptSet &= ~OPTPROC_ERRSTOP;
576 AGDUPSTR(pz, line, "opt line");
577 load_opt_line(opts, &st, pz, DIRECTION_CALLED, OPTION_LOAD_COOKED);
579 opts->fOptSet = sv_flags;
585 * c-file-style: "stroustrup"
586 * indent-tabs-mode: nil
588 * end of autoopts/load.c */