3 * $Id: load.c,v 4.20 2007/02/04 22:17:39 bkorb Exp $
4 * Time-stamp: "2007-02-04 11:54:57 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
12 * Automated Options copyright 1992-2007 Bruce Korb
14 * Automated Options is free software.
15 * You may redistribute it and/or modify it under the terms of the
16 * GNU General Public License, as published by the Free Software
17 * Foundation; either version 2, or (at your option) any later version.
19 * Automated Options is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Automated Options. See the file "COPYING". If not,
26 * write to: The Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
30 * As a special exception, Bruce Korb gives permission for additional
31 * uses of the text contained in his release of AutoOpts.
33 * The exception is that, if you link the AutoOpts library with other
34 * files to produce an executable, this does not by itself cause the
35 * resulting executable to be covered by the GNU General Public License.
36 * Your use of that executable is in no way restricted on account of
37 * linking the AutoOpts library code into it.
39 * This exception does not however invalidate any other reasons why
40 * the executable file might be covered by the GNU General Public License.
42 * This exception applies only to the code released by Bruce Korb under
43 * the name AutoOpts. If you copy code from other sources under the
44 * General Public License into a copy of AutoOpts, as the General Public
45 * License permits, the exception does not apply to the code that you add
46 * in this way. To avoid misleading anyone as to the status of such
47 * modified files, you must delete this exception notice from them.
49 * If you write modifications of your own for AutoOpts, it is your choice
50 * whether to permit this exception to apply to your modifications.
51 * If you do not wish that, delete this exception notice.
54 tOptionLoadMode option_load_mode = OPTION_LOAD_UNCOOKED;
56 /* = = = START-STATIC-FORWARD = = = */
57 /* static forward declarations maintained by :mkfwd */
73 assembleArgValue( char* pzTxt, tOptionLoadMode mode );
74 /* = = = END-STATIC-FORWARD = = = */
76 /*=export_func optionMakePath
79 * what: translate and construct a path
80 * arg: + char* + pzBuf + The result buffer +
81 * arg: + int + bufSize + The size of this buffer +
82 * arg: + char const* + pzName + The input name +
83 * arg: + char const* + pzProgPath + The full path of the current program +
86 * ret-desc: AG_TRUE if the name was handled, otherwise AG_FALSE.
87 * If the name does not start with ``$'', then it is handled
88 * simply by copying the input name to the output buffer and
89 * resolving the name with either @code{canonicalize_file_name(3GLIBC)}
90 * or @code{realpath(3C)}.
94 * This routine will copy the @code{pzName} input name into the @code{pzBuf}
95 * output buffer, carefully not exceeding @code{bufSize} bytes. If the
96 * first character of the input name is a @code{'$'} character, then there
97 * is special handling:
99 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
100 * searching @code{$PATH} if necessary.
102 * @code{$@} is replaced with the AutoGen package data installation directory
103 * (aka @code{pkgdatadir}).
105 * @code{$NAME} is replaced by the contents of the @code{NAME} environment
106 * variable. If not found, the search fails.
108 * Please note: both @code{$$} and @code{$NAME} must be at the start of the
109 * @code{pzName} string and must either be the entire string or be followed
110 * by the @code{'/'} (backslash on windows) character.
112 * err: @code{AG_FALSE} is returned if:
114 * @bullet{} The input name exceeds @code{bufSize} bytes.
116 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
117 * and the next character is not '/'.
119 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
122 * @bullet{} @code{NAME} is not a known environment variable
124 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
125 * errors (cannot resolve the resulting path).
134 size_t name_len = strlen( pzName );
137 # define PKGDATADIR ""
140 tSCC pkgdatadir[] = PKGDATADIR;
142 ag_bool res = AG_TRUE;
144 if (bufSize <= name_len)
148 * IF not an environment variable, just copy the data
150 if (*pzName != '$') {
156 if ( (*(pzD++) = *(pzS++)) == NUL)
164 * IF the name starts with "$$", then it must be "$$" or
165 * it must start with "$$/". In either event, replace the "$$"
166 * with the path to the executable and append a "/" character.
168 else switch (pzName[1]) {
173 res = insertProgramPath( pzBuf, bufSize, pzName, pzProgPath );
177 if (pkgdatadir[0] == NUL)
180 if (name_len + sizeof (pkgdatadir) > bufSize)
183 strcpy(pzBuf, pkgdatadir);
184 strcpy(pzBuf + sizeof(pkgdatadir) - 1, pzName + 2);
188 res = insertEnvVal( pzBuf, bufSize, pzName, pzProgPath );
194 #if defined(HAVE_CANONICALIZE_FILE_NAME)
196 char* pz = canonicalize_file_name(pzBuf);
199 if (strlen(pz) < bufSize)
204 #elif defined(HAVE_REALPATH)
206 char z[ PATH_MAX+1 ];
208 if (realpath( pzBuf, z ) == NULL)
211 if (strlen(z) < bufSize)
241 * See if the path is included in the program name.
242 * If it is, we're done. Otherwise, we have to hunt
243 * for the program using "pathfind".
245 if (strchr( pzProgPath, DIRCH ) != NULL)
248 pzPath = pathfind( getenv( "PATH" ), (char*)pzProgPath, "rx" );
254 pz = strrchr( pzPath, DIRCH );
257 * IF we cannot find a directory name separator,
258 * THEN we do not have a path name to our executable file.
266 * Concatenate the file name to the end of the executable path.
267 * The result may be either a file or a directory.
269 if ((pz - pzPath)+1 + strlen(pzName) >= bufSize)
272 memcpy( pzBuf, pzPath, (size_t)((pz - pzPath)+1) );
273 strcpy( pzBuf + (pz - pzPath) + 1, pzName );
276 * If the "pzPath" path was gotten from "pathfind()", then it was
277 * allocated and we need to deallocate it.
279 if (pzPath != pzProgPath)
280 free( (void*)pzPath );
295 int ch = (int)*++pzName;
296 if (! ISNAMECHAR( ch ))
298 *(pzDir++) = (char)ch;
306 pzDir = getenv( pzBuf );
309 * Environment value not found -- skip the home list entry
314 if (strlen( pzDir ) + 1 + strlen( pzName ) >= bufSize)
317 sprintf( pzBuf, "%s%s", pzDir, pzName );
323 mungeString( char* pzTxt, tOptionLoadMode mode )
327 if (mode == OPTION_LOAD_KEEP)
330 if (isspace( (int)*pzTxt )) {
333 while (isspace( (int)*++pzS )) ;
334 while ((*(pzD++) = *(pzS++)) != NUL) ;
337 pzE = pzTxt + strlen( pzTxt );
339 while ((pzE > pzTxt) && isspace( (int)pzE[-1] )) pzE--;
342 if (mode == OPTION_LOAD_UNCOOKED)
357 (void)ao_string_cook( pzTxt, NULL );
362 assembleArgValue( char* pzTxt, tOptionLoadMode mode )
364 tSCC zBrk[] = " \t:=";
365 char* pzEnd = strpbrk( pzTxt, zBrk );
369 * Not having an argument to a configurable name is okay.
372 return pzTxt + strlen(pzTxt);
375 * If we are keeping all whitespace, then the modevalue starts with the
376 * character that follows the end of the configurable name, regardless
377 * of which character caused it.
379 if (mode == OPTION_LOAD_KEEP) {
385 * If the name ended on a white space character, remember that
386 * because we'll have to skip over an immediately following ':' or '='
387 * (and the white space following *that*).
389 space_break = isspace((int)*pzEnd);
391 while (isspace((int)*pzEnd)) pzEnd++;
392 if (space_break && ((*pzEnd == ':') || (*pzEnd == '=')))
393 while (isspace((int)*++pzEnd)) ;
400 * Load an option from a block of text. The text must start with the
401 * configurable/option name and be followed by its associated value.
402 * That value may be processed in any of several ways. See "tOptionLoadMode"
410 tDirection direction,
411 tOptionLoadMode load_mode )
413 while (isspace( (int)*pzLine )) pzLine++;
416 char* pzArg = assembleArgValue( pzLine, load_mode );
418 if (! SUCCESSFUL( longOptionFind( pOpts, pzLine, pOS )))
420 if (pOS->flags & OPTST_NO_INIT)
422 pOS->pzOptArg = pzArg;
425 switch (pOS->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) {
428 * The selected option has no immediate action.
429 * THEREFORE, if the direction is PRESETTING
430 * THEN we skip this option.
432 if (PRESETTING(direction))
437 if (PRESETTING(direction)) {
439 * We are in the presetting direction with an option we handle
440 * immediately for enablement, but normally for disablement.
441 * Therefore, skip if disabled.
443 if ((pOS->flags & OPTST_DISABLED) == 0)
447 * We are in the processing direction with an option we handle
448 * immediately for enablement, but normally for disablement.
449 * Therefore, skip if NOT disabled.
451 if ((pOS->flags & OPTST_DISABLED) != 0)
456 case OPTST_DISABLE_IMM:
457 if (PRESETTING(direction)) {
459 * We are in the presetting direction with an option we handle
460 * immediately for disablement, but normally for disablement.
461 * Therefore, skip if NOT disabled.
463 if ((pOS->flags & OPTST_DISABLED) != 0)
467 * We are in the processing direction with an option we handle
468 * immediately for disablement, but normally for disablement.
469 * Therefore, skip if disabled.
471 if ((pOS->flags & OPTST_DISABLED) == 0)
476 case OPTST_IMM|OPTST_DISABLE_IMM:
478 * The selected option is always for immediate action.
479 * THEREFORE, if the direction is PROCESSING
480 * THEN we skip this option.
482 if (PROCESSING(direction))
490 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
491 if (*pOS->pzOptArg != NUL)
493 pOS->pzOptArg = NULL;
495 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
496 if (*pOS->pzOptArg == NUL)
497 pOS->pzOptArg = NULL;
499 AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
500 pOS->flags |= OPTST_ALLOC_ARG;
504 if (*pOS->pzOptArg == NUL)
505 pOS->pzOptArg = zNil;
507 AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
508 pOS->flags |= OPTST_ALLOC_ARG;
513 tOptionLoadMode sv = option_load_mode;
514 option_load_mode = load_mode;
515 handleOption( pOpts, pOS );
516 option_load_mode = sv;
521 /*=export_func optionLoadLine
523 * what: process a string for an option name and value
525 * arg: tOptions*, pOpts, program options descriptor
526 * arg: char const*, pzLine, NUL-terminated text
530 * This is a client program callable routine for setting options from, for
531 * example, the contents of a file that they read in. Only one option may
532 * appear in the text. It will be treated as a normal (non-preset) option.
534 * When passed a pointer to the option struct and a string, it will find
535 * the option named by the first token on the string and set the option
536 * argument to the remainder of the string. The caller must NUL terminate
537 * the string. Any embedded new lines will be included in the option
538 * argument. If the input looks like one or more quoted strings, then the
539 * input will be "cooked". The "cooking" is identical to the string
540 * formation used in AutoGen definition files (@pxref{basic expression}),
541 * except that you may not use backquotes.
543 * err: Invalid options are silently ignored. Invalid option arguments
544 * will cause a warning to print, but the function should return.
551 tOptState st = OPTSTATE_INITIALIZER(SET);
553 AGDUPSTR( pz, pzLine, "user option line" );
554 loadOptionLine( pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED );
560 * c-file-style: "stroustrup"
561 * indent-tabs-mode: nil
563 * end of autoopts/load.c */