3
* $Id: load.c,v 4.14 2006/09/28 01:26:33 bkorb Exp $
4
* Time-stamp: "2006-09-24 15:23:01 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-2006 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
/* = = = START-STATIC-FORWARD = = = */
55
/* static forward declarations maintained by :mkfwd */
71
assembleArgValue( char* pzTxt, tOptionLoadMode mode );
72
/* = = = END-STATIC-FORWARD = = = */
74
/*=export_func optionMakePath
77
* what: translate and construct a path
78
* arg: + char* + pzBuf + The result buffer +
79
* arg: + int + bufSize + The size of this buffer +
80
* arg: + char const* + pzName + The input name +
81
* arg: + char const* + pzProgPath + The full path of the current program +
84
* ret-desc: AG_TRUE if the name was handled, otherwise AG_FALSE.
85
* If the name does not start with ``$'', then it is handled
86
* simply by copying the input name to the output buffer and
87
* resolving the name with either @code{canonicalize_file_name(3GLIBC)}
88
* or @code{realpath(3C)}.
92
* This routine will copy the @code{pzName} input name into the @code{pzBuf}
93
* output buffer, carefully not exceeding @code{bufSize} bytes. If the
94
* first character of the input name is a @code{'$'} character, then there
95
* is special handling:
97
* @code{$$} is replaced with the directory name of the @code{pzProgPath},
98
* searching @code{$PATH} if necessary.
100
* @code{$NAME} is replaced by the contents of the @code{NAME} environment
103
* Please note: both @code{$$} and @code{$NAME} must be at the start of the
104
* @code{pzName} string and must either be the entire string or be followed
105
* by the @code{'/'} (backslash on windows) character.
107
* err: @code{AG_FALSE} is returned if:
109
* @bullet{} The input name exceeds @code{bufSize} bytes.
111
* @bullet{} @code{$$} or @code{$NAME} is not the full string and
112
* the next character is not '/'.
114
* @bullet{} @code{NAME} is not a known environment variable
116
* @bullet{} @code{canonicalize_file_name} or @code{realpath} return
117
* errors (cannot resolve the resulting path).
126
ag_bool res = AG_TRUE;
128
if (bufSize <= strlen( pzName ))
132
* IF not an environment variable, just copy the data
134
if (*pzName != '$') {
140
if ( (*(pzD++) = *(pzS++)) == NUL)
148
* IF the name starts with "$$", then it must be "$$" or
149
* it must start with "$$/". In either event, replace the "$$"
150
* with the path to the executable and append a "/" character.
152
else if (pzName[1] == '$')
153
res = insertProgramPath( pzBuf, bufSize, pzName, pzProgPath );
155
res = insertEnvVal( pzBuf, bufSize, pzName, pzProgPath );
160
#if defined(HAVE_CANONICALIZE_FILE_NAME)
162
char* pz = canonicalize_file_name(pzBuf);
165
if (strlen(pz) < bufSize)
170
#elif defined(HAVE_REALPATH)
172
char z[ PATH_MAX+1 ];
174
if (realpath( pzBuf, z ) == NULL)
177
if (strlen(z) < bufSize)
207
* See if the path is included in the program name.
208
* If it is, we're done. Otherwise, we have to hunt
209
* for the program using "pathfind".
211
if (strchr( pzProgPath, DIRCH ) != NULL)
214
pzPath = pathfind( getenv( "PATH" ), (char*)pzProgPath, "rx" );
220
pz = strrchr( pzPath, DIRCH );
223
* IF we cannot find a directory name separator,
224
* THEN we do not have a path name to our executable file.
232
* Concatenate the file name to the end of the executable path.
233
* The result may be either a file or a directory.
235
if ((pz - pzPath)+1 + strlen(pzName) >= bufSize)
238
memcpy( pzBuf, pzPath, (unsigned)((pz - pzPath)+1) );
239
strcpy( pzBuf + (pz - pzPath) + 1, pzName );
242
* If the "pzPath" path was gotten from "pathfind()", then it was
243
* allocated and we need to deallocate it.
245
if (pzPath != pzProgPath)
246
free( (void*)pzPath );
261
int ch = (int)*++pzName;
262
if (! ISNAMECHAR( ch ))
264
*(pzDir++) = (char)ch;
272
pzDir = getenv( pzBuf );
275
* Environment value not found -- skip the home list entry
280
if (strlen( pzDir ) + 1 + strlen( pzName ) >= bufSize)
283
sprintf( pzBuf, "%s%s", pzDir, pzName );
289
mungeString( char* pzTxt, tOptionLoadMode mode )
293
if (mode == OPTION_LOAD_KEEP)
296
if (isspace( (int)*pzTxt )) {
299
while (isspace( (int)*++pzS )) ;
300
while ((*(pzD++) = *(pzS++)) != NUL) ;
303
pzE = pzTxt + strlen( pzTxt );
305
while ((pzE > pzTxt) && isspace( (int)pzE[-1] )) pzE--;
308
if (mode == OPTION_LOAD_UNCOOKED)
323
(void)ao_string_cook( pzTxt, NULL );
328
assembleArgValue( char* pzTxt, tOptionLoadMode mode )
330
tSCC zBrk[] = " \t:=";
331
char* pzEnd = strpbrk( pzTxt, zBrk );
335
* Not having an argument to a configurable name is okay.
338
return pzTxt + strlen(pzTxt);
341
* If we are keeping all whitespace, then the value starts with the
342
* character that follows the end of the configurable name, regardless
343
* of which character caused it.
345
if (mode == OPTION_LOAD_KEEP) {
351
* If the name ended on a white space character, remember that
352
* because we'll have to skip over an immediately following ':' or '='
353
* (and the white space following *that*).
355
space_break = isspace((int)*pzEnd);
357
while (isspace((int)*pzEnd)) pzEnd++;
358
if (space_break && ((*pzEnd == ':') || (*pzEnd == '=')))
361
mungeString( pzEnd, mode );
367
* Load an option from a block of text. The text must start with the
368
* configurable/option name and be followed by its associated value.
369
* That value may be processed in any of several ways. See "tOptionLoadMode"
377
tDirection direction,
378
tOptionLoadMode load_mode )
380
while (isspace( (int)*pzLine )) pzLine++;
383
char* pzArg = assembleArgValue( pzLine, load_mode );
385
if (! SUCCESSFUL( longOptionFind( pOpts, pzLine, pOS )))
387
if (pOS->flags & OPTST_NO_INIT)
389
pOS->pzOptArg = pzArg;
392
switch (pOS->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) {
395
* The selected option has no immediate action.
396
* THEREFORE, if the direction is PRESETTING
397
* THEN we skip this option.
399
if (PRESETTING(direction))
404
if (PRESETTING(direction)) {
406
* We are in the presetting direction with an option we handle
407
* immediately for enablement, but normally for disablement.
408
* Therefore, skip if disabled.
410
if ((pOS->flags & OPTST_DISABLED) == 0)
414
* We are in the processing direction with an option we handle
415
* immediately for enablement, but normally for disablement.
416
* Therefore, skip if NOT disabled.
418
if ((pOS->flags & OPTST_DISABLED) != 0)
423
case OPTST_DISABLE_IMM:
424
if (PRESETTING(direction)) {
426
* We are in the presetting direction with an option we handle
427
* immediately for disablement, but normally for disablement.
428
* Therefore, skip if NOT disabled.
430
if ((pOS->flags & OPTST_DISABLED) != 0)
434
* We are in the processing direction with an option we handle
435
* immediately for disablement, but normally for disablement.
436
* Therefore, skip if disabled.
438
if ((pOS->flags & OPTST_DISABLED) == 0)
443
case OPTST_IMM|OPTST_DISABLE_IMM:
445
* The selected option is always for immediate action.
446
* THEREFORE, if the direction is PROCESSING
447
* THEN we skip this option.
449
if (PROCESSING(direction))
457
if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
458
if (*pOS->pzOptArg != NUL)
460
pOS->pzOptArg = NULL;
462
} else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
463
if (*pOS->pzOptArg == NUL)
464
pOS->pzOptArg = NULL;
465
else AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
468
if (*pOS->pzOptArg == NUL)
469
pOS->pzOptArg = zNil;
470
else AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
473
handleOption( pOpts, pOS );
477
/*=export_func optionLoadLine
479
* what: process a string for an option name and value
481
* arg: tOptions*, pOpts, program options descriptor
482
* arg: char const*, pzLine, NUL-terminated text
486
* This is a client program callable routine for setting options from, for
487
* example, the contents of a file that they read in. Only one option may
488
* appear in the text. It will be treated as a normal (non-preset) option.
490
* When passed a pointer to the option struct and a string, it will find
491
* the option named by the first token on the string and set the option
492
* argument to the remainder of the string. The caller must NUL terminate
493
* the string. Any embedded new lines will be included in the option
494
* argument. If the input looks like one or more quoted strings, then the
495
* input will be "cooked". The "cooking" is identical to the string
496
* formation used in AutoGen definition files (@pxref{basic expression}),
497
* except that you may not use backquotes.
499
* err: Invalid options are silently ignored. Invalid option arguments
500
* will cause a warning to print, but the function should return.
507
tOptState st = OPTSTATE_INITIALIZER(SET);
509
AGDUPSTR( pz, pzLine, "user option line" );
510
loadOptionLine( pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED );
516
* c-file-style: "stroustrup"
517
* indent-tabs-mode: nil
519
* end of autoopts/load.c */