5 * Id: load.c,v 4.20 2007/02/04 22:17:39 bkorb Exp
6 * Time-stamp: "2007-02-04 11:54:57 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
14 * Automated Options copyright 1992-2007 Bruce Korb
16 * Automated Options is free software.
17 * You may redistribute it and/or modify it under the terms of the
18 * GNU General Public License, as published by the Free Software
19 * Foundation; either version 2, or (at your option) any later version.
21 * Automated Options is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with Automated Options. See the file "COPYING". If not,
28 * write to: The Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor,
30 * Boston, MA 02110-1301, USA.
32 * As a special exception, Bruce Korb gives permission for additional
33 * uses of the text contained in his release of AutoOpts.
35 * The exception is that, if you link the AutoOpts library with other
36 * files to produce an executable, this does not by itself cause the
37 * resulting executable to be covered by the GNU General Public License.
38 * Your use of that executable is in no way restricted on account of
39 * linking the AutoOpts library code into it.
41 * This exception does not however invalidate any other reasons why
42 * the executable file might be covered by the GNU General Public License.
44 * This exception applies only to the code released by Bruce Korb under
45 * the name AutoOpts. If you copy code from other sources under the
46 * General Public License into a copy of AutoOpts, as the General Public
47 * License permits, the exception does not apply to the code that you add
48 * in this way. To avoid misleading anyone as to the status of such
49 * modified files, you must delete this exception notice from them.
51 * If you write modifications of your own for AutoOpts, it is your choice
52 * whether to permit this exception to apply to your modifications.
53 * If you do not wish that, delete this exception notice.
56 tOptionLoadMode option_load_mode
= OPTION_LOAD_UNCOOKED
;
58 /* = = = START-STATIC-FORWARD = = = */
59 /* static forward declarations maintained by :mkfwd */
75 assembleArgValue( char* pzTxt
, tOptionLoadMode mode
);
76 /* = = = END-STATIC-FORWARD = = = */
78 /*=export_func optionMakePath
81 * what: translate and construct a path
82 * arg: + char* + pzBuf + The result buffer +
83 * arg: + int + bufSize + The size of this buffer +
84 * arg: + char const* + pzName + The input name +
85 * arg: + char const* + pzProgPath + The full path of the current program +
88 * ret-desc: AG_TRUE if the name was handled, otherwise AG_FALSE.
89 * If the name does not start with ``$'', then it is handled
90 * simply by copying the input name to the output buffer and
91 * resolving the name with either @code{canonicalize_file_name(3GLIBC)}
92 * or @code{realpath(3C)}.
96 * This routine will copy the @code{pzName} input name into the @code{pzBuf}
97 * output buffer, carefully not exceeding @code{bufSize} bytes. If the
98 * first character of the input name is a @code{'$'} character, then there
99 * is special handling:
101 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
102 * searching @code{$PATH} if necessary.
104 * @code{$@} is replaced with the AutoGen package data installation directory
105 * (aka @code{pkgdatadir}).
107 * @code{$NAME} is replaced by the contents of the @code{NAME} environment
108 * variable. If not found, the search fails.
110 * Please note: both @code{$$} and @code{$NAME} must be at the start of the
111 * @code{pzName} string and must either be the entire string or be followed
112 * by the @code{'/'} (backslash on windows) character.
114 * err: @code{AG_FALSE} is returned if:
116 * @bullet{} The input name exceeds @code{bufSize} bytes.
118 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
119 * and the next character is not '/'.
121 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
124 * @bullet{} @code{NAME} is not a known environment variable
126 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
127 * errors (cannot resolve the resulting path).
136 size_t name_len
= strlen( pzName
);
139 # define PKGDATADIR ""
142 tSCC pkgdatadir
[] = PKGDATADIR
;
144 ag_bool res
= AG_TRUE
;
146 if (bufSize
<= name_len
)
150 * IF not an environment variable, just copy the data
152 if (*pzName
!= '$') {
158 if ( (*(pzD
++) = *(pzS
++)) == NUL
)
166 * IF the name starts with "$$", then it must be "$$" or
167 * it must start with "$$/". In either event, replace the "$$"
168 * with the path to the executable and append a "/" character.
170 else switch (pzName
[1]) {
175 res
= insertProgramPath( pzBuf
, bufSize
, pzName
, pzProgPath
);
179 if (pkgdatadir
[0] == NUL
)
182 if (name_len
+ sizeof (pkgdatadir
) > bufSize
)
185 strcpy(pzBuf
, pkgdatadir
);
186 strcpy(pzBuf
+ sizeof(pkgdatadir
) - 1, pzName
+ 2);
190 res
= insertEnvVal( pzBuf
, bufSize
, pzName
, pzProgPath
);
196 #if defined(HAVE_CANONICALIZE_FILE_NAME)
198 char* pz
= canonicalize_file_name(pzBuf
);
201 if (strlen(pz
) < bufSize
)
206 #elif defined(HAVE_REALPATH)
208 char z
[ PATH_MAX
+1 ];
210 if (realpath( pzBuf
, z
) == NULL
)
213 if (strlen(z
) < bufSize
)
243 * See if the path is included in the program name.
244 * If it is, we're done. Otherwise, we have to hunt
245 * for the program using "pathfind".
247 if (strchr( pzProgPath
, DIRCH
) != NULL
)
250 pzPath
= pathfind( getenv( "PATH" ), (char*)pzProgPath
, "rx" );
256 pz
= strrchr( pzPath
, DIRCH
);
259 * IF we cannot find a directory name separator,
260 * THEN we do not have a path name to our executable file.
268 * Concatenate the file name to the end of the executable path.
269 * The result may be either a file or a directory.
271 if ((pz
- pzPath
)+1 + strlen(pzName
) >= bufSize
)
274 memcpy( pzBuf
, pzPath
, (size_t)((pz
- pzPath
)+1) );
275 strcpy( pzBuf
+ (pz
- pzPath
) + 1, pzName
);
278 * If the "pzPath" path was gotten from "pathfind()", then it was
279 * allocated and we need to deallocate it.
281 if (pzPath
!= pzProgPath
)
282 free( (void*)pzPath
);
297 int ch
= (int)*++pzName
;
298 if (! ISNAMECHAR( ch
))
300 *(pzDir
++) = (char)ch
;
308 pzDir
= getenv( pzBuf
);
311 * Environment value not found -- skip the home list entry
316 if (strlen( pzDir
) + 1 + strlen( pzName
) >= bufSize
)
319 sprintf( pzBuf
, "%s%s", pzDir
, pzName
);
325 mungeString( char* pzTxt
, tOptionLoadMode mode
)
329 if (mode
== OPTION_LOAD_KEEP
)
332 if (isspace( (int)*pzTxt
)) {
335 while (isspace( (int)*++pzS
)) ;
336 while ((*(pzD
++) = *(pzS
++)) != NUL
) ;
339 pzE
= pzTxt
+ strlen( pzTxt
);
341 while ((pzE
> pzTxt
) && isspace( (int)pzE
[-1] )) pzE
--;
344 if (mode
== OPTION_LOAD_UNCOOKED
)
359 (void)ao_string_cook( pzTxt
, NULL
);
364 assembleArgValue( char* pzTxt
, tOptionLoadMode mode
)
366 tSCC zBrk
[] = " \t:=";
367 char* pzEnd
= strpbrk( pzTxt
, zBrk
);
371 * Not having an argument to a configurable name is okay.
374 return pzTxt
+ strlen(pzTxt
);
377 * If we are keeping all whitespace, then the modevalue starts with the
378 * character that follows the end of the configurable name, regardless
379 * of which character caused it.
381 if (mode
== OPTION_LOAD_KEEP
) {
387 * If the name ended on a white space character, remember that
388 * because we'll have to skip over an immediately following ':' or '='
389 * (and the white space following *that*).
391 space_break
= isspace((int)*pzEnd
);
393 while (isspace((int)*pzEnd
)) pzEnd
++;
394 if (space_break
&& ((*pzEnd
== ':') || (*pzEnd
== '=')))
395 while (isspace((int)*++pzEnd
)) ;
402 * Load an option from a block of text. The text must start with the
403 * configurable/option name and be followed by its associated value.
404 * That value may be processed in any of several ways. See "tOptionLoadMode"
412 tDirection direction
,
413 tOptionLoadMode load_mode
)
415 while (isspace( (int)*pzLine
)) pzLine
++;
418 char* pzArg
= assembleArgValue( pzLine
, load_mode
);
420 if (! SUCCESSFUL( longOptionFind( pOpts
, pzLine
, pOS
)))
422 if (pOS
->flags
& OPTST_NO_INIT
)
424 pOS
->pzOptArg
= pzArg
;
427 switch (pOS
->flags
& (OPTST_IMM
|OPTST_DISABLE_IMM
)) {
430 * The selected option has no immediate action.
431 * THEREFORE, if the direction is PRESETTING
432 * THEN we skip this option.
434 if (PRESETTING(direction
))
439 if (PRESETTING(direction
)) {
441 * We are in the presetting direction with an option we handle
442 * immediately for enablement, but normally for disablement.
443 * Therefore, skip if disabled.
445 if ((pOS
->flags
& OPTST_DISABLED
) == 0)
449 * We are in the processing direction with an option we handle
450 * immediately for enablement, but normally for disablement.
451 * Therefore, skip if NOT disabled.
453 if ((pOS
->flags
& OPTST_DISABLED
) != 0)
458 case OPTST_DISABLE_IMM
:
459 if (PRESETTING(direction
)) {
461 * We are in the presetting direction with an option we handle
462 * immediately for disablement, but normally for disablement.
463 * Therefore, skip if NOT disabled.
465 if ((pOS
->flags
& OPTST_DISABLED
) != 0)
469 * We are in the processing direction with an option we handle
470 * immediately for disablement, but normally for disablement.
471 * Therefore, skip if disabled.
473 if ((pOS
->flags
& OPTST_DISABLED
) == 0)
478 case OPTST_IMM
|OPTST_DISABLE_IMM
:
480 * The selected option is always for immediate action.
481 * THEREFORE, if the direction is PROCESSING
482 * THEN we skip this option.
484 if (PROCESSING(direction
))
492 if (OPTST_GET_ARGTYPE(pOS
->pOD
->fOptState
) == OPARG_TYPE_NONE
) {
493 if (*pOS
->pzOptArg
!= NUL
)
495 pOS
->pzOptArg
= NULL
;
497 } else if (pOS
->pOD
->fOptState
& OPTST_ARG_OPTIONAL
) {
498 if (*pOS
->pzOptArg
== NUL
)
499 pOS
->pzOptArg
= NULL
;
501 AGDUPSTR( pOS
->pzOptArg
, pOS
->pzOptArg
, "option argument" );
502 pOS
->flags
|= OPTST_ALLOC_ARG
;
506 if (*pOS
->pzOptArg
== NUL
)
507 pOS
->pzOptArg
= zNil
;
509 AGDUPSTR( pOS
->pzOptArg
, pOS
->pzOptArg
, "option argument" );
510 pOS
->flags
|= OPTST_ALLOC_ARG
;
515 tOptionLoadMode sv
= option_load_mode
;
516 option_load_mode
= load_mode
;
517 handleOption( pOpts
, pOS
);
518 option_load_mode
= sv
;
523 /*=export_func optionLoadLine
525 * what: process a string for an option name and value
527 * arg: tOptions*, pOpts, program options descriptor
528 * arg: char const*, pzLine, NUL-terminated text
532 * This is a client program callable routine for setting options from, for
533 * example, the contents of a file that they read in. Only one option may
534 * appear in the text. It will be treated as a normal (non-preset) option.
536 * When passed a pointer to the option struct and a string, it will find
537 * the option named by the first token on the string and set the option
538 * argument to the remainder of the string. The caller must NUL terminate
539 * the string. Any embedded new lines will be included in the option
540 * argument. If the input looks like one or more quoted strings, then the
541 * input will be "cooked". The "cooking" is identical to the string
542 * formation used in AutoGen definition files (@pxref{basic expression}),
543 * except that you may not use backquotes.
545 * err: Invalid options are silently ignored. Invalid option arguments
546 * will cause a warning to print, but the function should return.
553 tOptState st
= OPTSTATE_INITIALIZER(SET
);
555 AGDUPSTR( pz
, pzLine
, "user option line" );
556 loadOptionLine( pOpts
, &st
, pz
, DIRECTION_PROCESS
, OPTION_LOAD_COOKED
);
562 * c-file-style: "stroustrup"
563 * indent-tabs-mode: nil
565 * end of autoopts/load.c */