5 * Id: f0ececd5fec43bacb417d7b50294accc2121923f
6 * Time-stamp: "2008-12-06 10:16:05 bkorb"
8 * This file contains the routines that deal with processing text strings
9 * for options, either from a NUL-terminated string passed in or from an
12 * This file is part of AutoOpts, a companion to AutoGen.
13 * AutoOpts is free software.
14 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
16 * AutoOpts is available under any one of two licenses. The license
17 * in use must be one of these two and the choice is under the control
18 * of the user of the license.
20 * The GNU Lesser General Public License, version 3 or later
21 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
23 * The Modified Berkeley Software Distribution License
24 * See the file "COPYING.mbsd"
26 * These files have the following md5sums:
28 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
29 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
30 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
33 tOptionLoadMode option_load_mode
= OPTION_LOAD_UNCOOKED
;
35 /* = = = START-STATIC-FORWARD = = = */
36 /* static forward declarations maintained by mk-fwd */
52 assembleArgValue( char* pzTxt
, tOptionLoadMode mode
);
53 /* = = = END-STATIC-FORWARD = = = */
55 /*=export_func optionMakePath
58 * what: translate and construct a path
59 * arg: + char* + pzBuf + The result buffer +
60 * arg: + int + bufSize + The size of this buffer +
61 * arg: + char const* + pzName + The input name +
62 * arg: + char const* + pzProgPath + The full path of the current program +
65 * ret-desc: AG_TRUE if the name was handled, otherwise AG_FALSE.
66 * If the name does not start with ``$'', then it is handled
67 * simply by copying the input name to the output buffer and
68 * resolving the name with either
69 * @code{canonicalize_file_name(3GLIBC)} or @code{realpath(3C)}.
73 * This routine will copy the @code{pzName} input name into the @code{pzBuf}
74 * output buffer, carefully not exceeding @code{bufSize} bytes. If the
75 * first character of the input name is a @code{'$'} character, then there
76 * is special handling:
78 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
79 * searching @code{$PATH} if necessary.
81 * @code{$@} is replaced with the AutoGen package data installation directory
82 * (aka @code{pkgdatadir}).
84 * @code{$NAME} is replaced by the contents of the @code{NAME} environment
85 * variable. If not found, the search fails.
87 * Please note: both @code{$$} and @code{$NAME} must be at the start of the
88 * @code{pzName} string and must either be the entire string or be followed
89 * by the @code{'/'} (backslash on windows) character.
91 * err: @code{AG_FALSE} is returned if:
93 * @bullet{} The input name exceeds @code{bufSize} bytes.
95 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
96 * and the next character is not '/'.
98 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
101 * @bullet{} @code{NAME} is not a known environment variable
103 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
104 * errors (cannot resolve the resulting path).
113 size_t name_len
= strlen( pzName
);
116 # define PKGDATADIR ""
119 tSCC pkgdatadir
[] = PKGDATADIR
;
121 ag_bool res
= AG_TRUE
;
123 if (bufSize
<= name_len
)
127 * IF not an environment variable, just copy the data
129 if (*pzName
!= '$') {
135 if ( (*(pzD
++) = *(pzS
++)) == NUL
)
143 * IF the name starts with "$$", then it must be "$$" or
144 * it must start with "$$/". In either event, replace the "$$"
145 * with the path to the executable and append a "/" character.
147 else switch (pzName
[1]) {
152 res
= insertProgramPath( pzBuf
, bufSize
, pzName
, pzProgPath
);
156 if (pkgdatadir
[0] == NUL
)
159 if (name_len
+ sizeof (pkgdatadir
) > bufSize
)
162 strcpy(pzBuf
, pkgdatadir
);
163 strcpy(pzBuf
+ sizeof(pkgdatadir
) - 1, pzName
+ 2);
167 res
= insertEnvVal( pzBuf
, bufSize
, pzName
, pzProgPath
);
173 #if defined(HAVE_CANONICALIZE_FILE_NAME)
175 char* pz
= canonicalize_file_name(pzBuf
);
178 if (strlen(pz
) < bufSize
)
183 #elif defined(HAVE_REALPATH)
185 char z
[ PATH_MAX
+1 ];
187 if (realpath( pzBuf
, z
) == NULL
)
190 if (strlen(z
) < bufSize
)
220 * See if the path is included in the program name.
221 * If it is, we're done. Otherwise, we have to hunt
222 * for the program using "pathfind".
224 if (strchr( pzProgPath
, DIRCH
) != NULL
)
227 pzPath
= pathfind( getenv( "PATH" ), (char*)pzProgPath
, "rx" );
233 pz
= strrchr( pzPath
, DIRCH
);
236 * IF we cannot find a directory name separator,
237 * THEN we do not have a path name to our executable file.
245 * Concatenate the file name to the end of the executable path.
246 * The result may be either a file or a directory.
248 if ((pz
- pzPath
)+1 + strlen(pzName
) >= bufSize
)
251 memcpy( pzBuf
, pzPath
, (size_t)((pz
- pzPath
)+1) );
252 strcpy( pzBuf
+ (pz
- pzPath
) + 1, pzName
);
255 * If the "pzPath" path was gotten from "pathfind()", then it was
256 * allocated and we need to deallocate it.
258 if (pzPath
!= pzProgPath
)
274 int ch
= (int)*++pzName
;
275 if (! IS_VALUE_NAME_CHAR(ch
))
277 *(pzDir
++) = (char)ch
;
285 pzDir
= getenv( pzBuf
);
288 * Environment value not found -- skip the home list entry
293 if (strlen( pzDir
) + 1 + strlen( pzName
) >= bufSize
)
296 sprintf( pzBuf
, "%s%s", pzDir
, pzName
);
302 mungeString( char* pzTxt
, tOptionLoadMode mode
)
306 if (mode
== OPTION_LOAD_KEEP
)
309 if (IS_WHITESPACE_CHAR(*pzTxt
)) {
312 while (IS_WHITESPACE_CHAR(*++pzS
)) ;
313 while ((*(pzD
++) = *(pzS
++)) != NUL
) ;
316 pzE
= pzTxt
+ strlen( pzTxt
);
318 while ((pzE
> pzTxt
) && IS_WHITESPACE_CHAR(pzE
[-1])) pzE
--;
321 if (mode
== OPTION_LOAD_UNCOOKED
)
336 (void)ao_string_cook( pzTxt
, NULL
);
341 assembleArgValue( char* pzTxt
, tOptionLoadMode mode
)
343 tSCC zBrk
[] = " \t\n:=";
344 char* pzEnd
= strpbrk( pzTxt
, zBrk
);
348 * Not having an argument to a configurable name is okay.
351 return pzTxt
+ strlen(pzTxt
);
354 * If we are keeping all whitespace, then the modevalue starts with the
355 * character that follows the end of the configurable name, regardless
356 * of which character caused it.
358 if (mode
== OPTION_LOAD_KEEP
) {
364 * If the name ended on a white space character, remember that
365 * because we'll have to skip over an immediately following ':' or '='
366 * (and the white space following *that*).
368 space_break
= IS_WHITESPACE_CHAR(*pzEnd
);
370 while (IS_WHITESPACE_CHAR(*pzEnd
)) pzEnd
++;
371 if (space_break
&& ((*pzEnd
== ':') || (*pzEnd
== '=')))
372 while (IS_WHITESPACE_CHAR(*++pzEnd
)) ;
379 * Load an option from a block of text. The text must start with the
380 * configurable/option name and be followed by its associated value.
381 * That value may be processed in any of several ways. See "tOptionLoadMode"
389 tDirection direction
,
390 tOptionLoadMode load_mode
)
392 while (IS_WHITESPACE_CHAR(*pzLine
)) pzLine
++;
395 char* pzArg
= assembleArgValue( pzLine
, load_mode
);
397 if (! SUCCESSFUL( longOptionFind( pOpts
, pzLine
, pOS
)))
399 if (pOS
->flags
& OPTST_NO_INIT
)
401 pOS
->pzOptArg
= pzArg
;
404 switch (pOS
->flags
& (OPTST_IMM
|OPTST_DISABLE_IMM
)) {
407 * The selected option has no immediate action.
408 * THEREFORE, if the direction is PRESETTING
409 * THEN we skip this option.
411 if (PRESETTING(direction
))
416 if (PRESETTING(direction
)) {
418 * We are in the presetting direction with an option we handle
419 * immediately for enablement, but normally for disablement.
420 * Therefore, skip if disabled.
422 if ((pOS
->flags
& OPTST_DISABLED
) == 0)
426 * We are in the processing direction with an option we handle
427 * immediately for enablement, but normally for disablement.
428 * Therefore, skip if NOT disabled.
430 if ((pOS
->flags
& OPTST_DISABLED
) != 0)
435 case OPTST_DISABLE_IMM
:
436 if (PRESETTING(direction
)) {
438 * We are in the presetting direction with an option we handle
439 * immediately for disablement, but normally for disablement.
440 * Therefore, skip if NOT disabled.
442 if ((pOS
->flags
& OPTST_DISABLED
) != 0)
446 * We are in the processing direction with an option we handle
447 * immediately for disablement, but normally for disablement.
448 * Therefore, skip if disabled.
450 if ((pOS
->flags
& OPTST_DISABLED
) == 0)
455 case OPTST_IMM
|OPTST_DISABLE_IMM
:
457 * The selected option is always for immediate action.
458 * THEREFORE, if the direction is PROCESSING
459 * THEN we skip this option.
461 if (PROCESSING(direction
))
469 if (OPTST_GET_ARGTYPE(pOS
->pOD
->fOptState
) == OPARG_TYPE_NONE
) {
470 if (*pOS
->pzOptArg
!= NUL
)
472 pOS
->pzOptArg
= NULL
;
474 } else if (pOS
->pOD
->fOptState
& OPTST_ARG_OPTIONAL
) {
475 if (*pOS
->pzOptArg
== NUL
)
476 pOS
->pzOptArg
= NULL
;
478 AGDUPSTR( pOS
->pzOptArg
, pOS
->pzOptArg
, "option argument" );
479 pOS
->flags
|= OPTST_ALLOC_ARG
;
483 if (*pOS
->pzOptArg
== NUL
)
484 pOS
->pzOptArg
= zNil
;
486 AGDUPSTR( pOS
->pzOptArg
, pOS
->pzOptArg
, "option argument" );
487 pOS
->flags
|= OPTST_ALLOC_ARG
;
492 tOptionLoadMode sv
= option_load_mode
;
493 option_load_mode
= load_mode
;
494 handleOption( pOpts
, pOS
);
495 option_load_mode
= sv
;
500 /*=export_func optionLoadLine
502 * what: process a string for an option name and value
504 * arg: tOptions*, pOpts, program options descriptor
505 * arg: char const*, pzLine, NUL-terminated text
509 * This is a client program callable routine for setting options from, for
510 * example, the contents of a file that they read in. Only one option may
511 * appear in the text. It will be treated as a normal (non-preset) option.
513 * When passed a pointer to the option struct and a string, it will find
514 * the option named by the first token on the string and set the option
515 * argument to the remainder of the string. The caller must NUL terminate
516 * the string. Any embedded new lines will be included in the option
517 * argument. If the input looks like one or more quoted strings, then the
518 * input will be "cooked". The "cooking" is identical to the string
519 * formation used in AutoGen definition files (@pxref{basic expression}),
520 * except that you may not use backquotes.
522 * err: Invalid options are silently ignored. Invalid option arguments
523 * will cause a warning to print, but the function should return.
530 tOptState st
= OPTSTATE_INITIALIZER(SET
);
532 AGDUPSTR( pz
, pzLine
, "user option line" );
533 loadOptionLine( pOpts
, &st
, pz
, DIRECTION_PROCESS
, OPTION_LOAD_COOKED
);
539 * c-file-style: "stroustrup"
540 * indent-tabs-mode: nil
542 * end of autoopts/load.c */