4 * Time-stamp: "2012-03-31 13:13:34 bkorb"
6 * This file contains the routines that deal with processing text strings
7 * for options, either from a NUL-terminated string passed in or from an
10 * This file is part of AutoOpts, a companion to AutoGen.
11 * AutoOpts is free software.
12 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
14 * AutoOpts is available under any one of two licenses. The license
15 * in use must be one of these two and the choice is under the control
16 * of the user of the license.
18 * The GNU Lesser General Public License, version 3 or later
19 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
21 * The Modified Berkeley Software Distribution License
22 * See the file "COPYING.mbsd"
24 * These files have the following md5sums:
26 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
27 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
28 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
31 /* = = = START-STATIC-FORWARD = = = */
33 add_prog_path(char * pzBuf
, int bufSize
, char const * pzName
,
34 char const * pzProgPath
);
37 add_env_val(char * pzBuf
, int bufSize
, char const * pzName
);
40 assemble_arg_val(char * pzTxt
, tOptionLoadMode mode
);
41 /* = = = END-STATIC-FORWARD = = = */
43 /*=export_func optionMakePath
46 * what: translate and construct a path
47 * arg: + char* + pzBuf + The result buffer +
48 * arg: + int + bufSize + The size of this buffer +
49 * arg: + char const* + pzName + The input name +
50 * arg: + char const* + pzProgPath + The full path of the current program +
53 * ret-desc: true if the name was handled, otherwise false.
54 * If the name does not start with ``$'', then it is handled
55 * simply by copying the input name to the output buffer and
56 * resolving the name with either
57 * @code{canonicalize_file_name(3GLIBC)} or @code{realpath(3C)}.
61 * This routine will copy the @code{pzName} input name into the
62 * @code{pzBuf} output buffer, not exceeding @code{bufSize} bytes. If the
63 * first character of the input name is a @code{'$'} character, then there
64 * is special handling:
66 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
67 * searching @code{$PATH} if necessary.
69 * @code{$@} is replaced with the AutoGen package data installation directory
70 * (aka @code{pkgdatadir}).
72 * @code{$NAME} is replaced by the contents of the @code{NAME} environment
73 * variable. If not found, the search fails.
75 * Please note: both @code{$$} and @code{$NAME} must be at the start of the
76 * @code{pzName} string and must either be the entire string or be followed
77 * by the @code{'/'} (backslash on windows) character.
79 * err: @code{false} is returned if:
81 * @bullet{} The input name exceeds @code{bufSize} bytes.
83 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
84 * and the next character is not '/'.
86 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
89 * @bullet{} @code{NAME} is not a known environment variable
91 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
92 * errors (cannot resolve the resulting path).
95 optionMakePath(char * pzBuf
, int bufSize
, char const * pzName
,
96 char const * pzProgPath
)
98 size_t name_len
= strlen(pzName
);
100 if (((size_t)bufSize
<= name_len
) || (name_len
== 0))
104 * IF not an environment variable, just copy the data
106 if (*pzName
!= '$') {
107 char const* pzS
= pzName
;
112 if ( (*(pzD
++) = *(pzS
++)) == NUL
)
120 * IF the name starts with "$$", then it must be "$$" or
121 * it must start with "$$/". In either event, replace the "$$"
122 * with the path to the executable and append a "/" character.
124 else switch (pzName
[1]) {
129 if (! add_prog_path(pzBuf
, bufSize
, pzName
, pzProgPath
))
134 if (program_pkgdatadir
[0] == NUL
)
137 if (snprintf(pzBuf
, bufSize
, "%s%s", program_pkgdatadir
, pzName
+ 2)
143 if (! add_env_val(pzBuf
, bufSize
, pzName
))
147 #if defined(HAVE_CANONICALIZE_FILE_NAME)
149 char * pz
= canonicalize_file_name(pzBuf
);
153 name_len
= strlen(pz
);
154 if (name_len
>= (size_t)bufSize
) {
159 memcpy(pzBuf
, pz
, name_len
+ 1);
163 #elif defined(HAVE_REALPATH)
167 if (realpath(pzBuf
, z
) == NULL
)
170 name_len
= strlen(z
);
171 if (name_len
>= bufSize
)
174 memcpy(pzBuf
, z
, name_len
+ 1);
182 add_prog_path(char * pzBuf
, int bufSize
, char const * pzName
,
183 char const * pzProgPath
)
199 * See if the path is included in the program name.
200 * If it is, we're done. Otherwise, we have to hunt
201 * for the program using "pathfind".
203 if (strchr(pzProgPath
, DIRCH
) != NULL
)
206 pzPath
= pathfind(getenv("PATH"), (char*)pzProgPath
, "rx");
212 pz
= strrchr(pzPath
, DIRCH
);
215 * IF we cannot find a directory name separator,
216 * THEN we do not have a path name to our executable file.
224 * Concatenate the file name to the end of the executable path.
225 * The result may be either a file or a directory.
227 if ((pz
- pzPath
)+1 + strlen(pzName
) >= (unsigned)bufSize
)
230 memcpy(pzBuf
, pzPath
, (size_t)((pz
- pzPath
)+1));
231 strcpy(pzBuf
+ (pz
- pzPath
) + 1, pzName
);
234 * If the "pzPath" path was gotten from "pathfind()", then it was
235 * allocated and we need to deallocate it.
237 if (pzPath
!= pzProgPath
)
244 add_env_val(char * pzBuf
, int bufSize
, char const * pzName
)
246 char * pzDir
= pzBuf
;
249 int ch
= (int)*++pzName
;
250 if (! IS_VALUE_NAME_CHAR(ch
))
252 *(pzDir
++) = (char)ch
;
260 pzDir
= getenv(pzBuf
);
263 * Environment value not found -- skip the home list entry
268 if (strlen(pzDir
) + 1 + strlen(pzName
) >= (unsigned)bufSize
)
271 sprintf(pzBuf
, "%s%s", pzDir
, pzName
);
277 mungeString(char* pzTxt
, tOptionLoadMode mode
)
281 if (mode
== OPTION_LOAD_KEEP
)
284 if (IS_WHITESPACE_CHAR(*pzTxt
)) {
285 char * pzS
= SPN_WHITESPACE_CHARS(pzTxt
+1);
286 size_t l
= strlen(pzS
) + 1;
287 memmove(pzTxt
, pzS
, l
);
291 pzE
= pzTxt
+ strlen(pzTxt
);
293 pzE
= SPN_WHITESPACE_BACK(pzTxt
, pzE
);
296 if (mode
== OPTION_LOAD_UNCOOKED
)
311 (void)ao_string_cook(pzTxt
, NULL
);
316 assemble_arg_val(char * pzTxt
, tOptionLoadMode mode
)
318 char* pzEnd
= strpbrk(pzTxt
, ARG_BREAK_STR
);
322 * Not having an argument to a configurable name is okay.
325 return pzTxt
+ strlen(pzTxt
);
328 * If we are keeping all whitespace, then the modevalue starts with the
329 * character that follows the end of the configurable name, regardless
330 * of which character caused it.
332 if (mode
== OPTION_LOAD_KEEP
) {
338 * If the name ended on a white space character, remember that
339 * because we'll have to skip over an immediately following ':' or '='
340 * (and the white space following *that*).
342 space_break
= IS_WHITESPACE_CHAR(*pzEnd
);
345 pzEnd
= SPN_WHITESPACE_CHARS(pzEnd
);
346 if (space_break
&& ((*pzEnd
== ':') || (*pzEnd
== '=')))
347 pzEnd
= SPN_WHITESPACE_CHARS(pzEnd
+1);
354 * Load an option from a block of text. The text must start with the
355 * configurable/option name and be followed by its associated value.
356 * That value may be processed in any of several ways. See "tOptionLoadMode"
364 tDirection direction
,
365 tOptionLoadMode load_mode
)
367 pzLine
= SPN_WHITESPACE_CHARS(pzLine
);
370 char* pzArg
= assemble_arg_val(pzLine
, load_mode
);
372 if (! SUCCESSFUL(opt_find_long(pOpts
, pzLine
, pOS
)))
374 if (pOS
->flags
& OPTST_NO_INIT
)
376 pOS
->pzOptArg
= pzArg
;
379 switch (pOS
->flags
& (OPTST_IMM
|OPTST_DISABLE_IMM
)) {
382 * The selected option has no immediate action.
383 * THEREFORE, if the direction is PRESETTING
384 * THEN we skip this option.
386 if (PRESETTING(direction
))
391 if (PRESETTING(direction
)) {
393 * We are in the presetting direction with an option we handle
394 * immediately for enablement, but normally for disablement.
395 * Therefore, skip if disabled.
397 if ((pOS
->flags
& OPTST_DISABLED
) == 0)
401 * We are in the processing direction with an option we handle
402 * immediately for enablement, but normally for disablement.
403 * Therefore, skip if NOT disabled.
405 if ((pOS
->flags
& OPTST_DISABLED
) != 0)
410 case OPTST_DISABLE_IMM
:
411 if (PRESETTING(direction
)) {
413 * We are in the presetting direction with an option we handle
414 * immediately for disablement, but normally for disablement.
415 * Therefore, skip if NOT disabled.
417 if ((pOS
->flags
& OPTST_DISABLED
) != 0)
421 * We are in the processing direction with an option we handle
422 * immediately for disablement, but normally for disablement.
423 * Therefore, skip if disabled.
425 if ((pOS
->flags
& OPTST_DISABLED
) == 0)
430 case OPTST_IMM
|OPTST_DISABLE_IMM
:
432 * The selected option is always for immediate action.
433 * THEREFORE, if the direction is PROCESSING
434 * THEN we skip this option.
436 if (PROCESSING(direction
))
444 if (OPTST_GET_ARGTYPE(pOS
->pOD
->fOptState
) == OPARG_TYPE_NONE
) {
445 if (*pOS
->pzOptArg
!= NUL
)
447 pOS
->pzOptArg
= NULL
;
449 } else if (pOS
->pOD
->fOptState
& OPTST_ARG_OPTIONAL
) {
450 if (*pOS
->pzOptArg
== NUL
)
451 pOS
->pzOptArg
= NULL
;
453 AGDUPSTR(pOS
->pzOptArg
, pOS
->pzOptArg
, "option argument");
454 pOS
->flags
|= OPTST_ALLOC_ARG
;
458 if (*pOS
->pzOptArg
== NUL
)
459 pOS
->pzOptArg
= zNil
;
461 AGDUPSTR(pOS
->pzOptArg
, pOS
->pzOptArg
, "option argument");
462 pOS
->flags
|= OPTST_ALLOC_ARG
;
467 tOptionLoadMode sv
= option_load_mode
;
468 option_load_mode
= load_mode
;
469 handle_opt(pOpts
, pOS
);
470 option_load_mode
= sv
;
475 /*=export_func optionLoadLine
477 * what: process a string for an option name and value
479 * arg: tOptions*, pOpts, program options descriptor
480 * arg: char const*, pzLine, NUL-terminated text
484 * This is a client program callable routine for setting options from, for
485 * example, the contents of a file that they read in. Only one option may
486 * appear in the text. It will be treated as a normal (non-preset) option.
488 * When passed a pointer to the option struct and a string, it will find
489 * the option named by the first token on the string and set the option
490 * argument to the remainder of the string. The caller must NUL terminate
491 * the string. Any embedded new lines will be included in the option
492 * argument. If the input looks like one or more quoted strings, then the
493 * input will be "cooked". The "cooking" is identical to the string
494 * formation used in AutoGen definition files (@pxref{basic expression}),
495 * except that you may not use backquotes.
497 * err: Invalid options are silently ignored. Invalid option arguments
498 * will cause a warning to print, but the function should return.
501 optionLoadLine(tOptions
* pOpts
, char const * pzLine
)
503 tOptState st
= OPTSTATE_INITIALIZER(SET
);
505 AGDUPSTR(pz
, pzLine
, "user option line");
506 loadOptionLine(pOpts
, &st
, pz
, DIRECTION_PROCESS
, OPTION_LOAD_COOKED
);
512 * c-file-style: "stroustrup"
513 * indent-tabs-mode: nil
515 * end of autoopts/load.c */