Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / sntp / libopts / load.c
blobad6e0acbf92145527bca8a25bf4e9100695adac4
1 /* $NetBSD$ */
4 /*
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
10 * rc/ini file.
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 */
37 static ag_bool
38 insertProgramPath(
39 char* pzBuf,
40 int bufSize,
41 tCC* pzName,
42 tCC* pzProgPath );
44 static ag_bool
45 insertEnvVal(
46 char* pzBuf,
47 int bufSize,
48 tCC* pzName,
49 tCC* pzProgPath );
51 static char*
52 assembleArgValue( char* pzTxt, tOptionLoadMode mode );
53 /* = = = END-STATIC-FORWARD = = = */
55 /*=export_func optionMakePath
56 * private:
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 +
64 * ret-type: ag_bool
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)}.
71 * doc:
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:
77 * @*
78 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
79 * searching @code{$PATH} if necessary.
80 * @*
81 * @code{$@} is replaced with the AutoGen package data installation directory
82 * (aka @code{pkgdatadir}).
83 * @*
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:
92 * @*
93 * @bullet{} The input name exceeds @code{bufSize} bytes.
94 * @*
95 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
96 * and the next character is not '/'.
97 * @*
98 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
99 * was specified.
100 * @*
101 * @bullet{} @code{NAME} is not a known environment variable
102 * @*
103 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
104 * errors (cannot resolve the resulting path).
106 ag_bool
107 optionMakePath(
108 char* pzBuf,
109 int bufSize,
110 tCC* pzName,
111 tCC* pzProgPath )
113 size_t name_len = strlen( pzName );
115 # ifndef PKGDATADIR
116 # define PKGDATADIR ""
117 # endif
119 tSCC pkgdatadir[] = PKGDATADIR;
121 ag_bool res = AG_TRUE;
123 if (bufSize <= name_len)
124 return AG_FALSE;
127 * IF not an environment variable, just copy the data
129 if (*pzName != '$') {
130 tCC* pzS = pzName;
131 char* pzD = pzBuf;
132 int ct = bufSize;
134 for (;;) {
135 if ( (*(pzD++) = *(pzS++)) == NUL)
136 break;
137 if (--ct <= 0)
138 return AG_FALSE;
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]) {
148 case NUL:
149 return AG_FALSE;
151 case '$':
152 res = insertProgramPath( pzBuf, bufSize, pzName, pzProgPath );
153 break;
155 case '@':
156 if (pkgdatadir[0] == NUL)
157 return AG_FALSE;
159 if (name_len + sizeof (pkgdatadir) > bufSize)
160 return AG_FALSE;
162 strcpy(pzBuf, pkgdatadir);
163 strcpy(pzBuf + sizeof(pkgdatadir) - 1, pzName + 2);
164 break;
166 default:
167 res = insertEnvVal( pzBuf, bufSize, pzName, pzProgPath );
170 if (! res)
171 return AG_FALSE;
173 #if defined(HAVE_CANONICALIZE_FILE_NAME)
175 char* pz = canonicalize_file_name(pzBuf);
176 if (pz == NULL)
177 return AG_FALSE;
178 if (strlen(pz) < bufSize)
179 strcpy(pzBuf, pz);
180 free(pz);
183 #elif defined(HAVE_REALPATH)
185 char z[ PATH_MAX+1 ];
187 if (realpath( pzBuf, z ) == NULL)
188 return AG_FALSE;
190 if (strlen(z) < bufSize)
191 strcpy( pzBuf, z );
193 #endif
195 return AG_TRUE;
199 static ag_bool
200 insertProgramPath(
201 char* pzBuf,
202 int bufSize,
203 tCC* pzName,
204 tCC* pzProgPath )
206 tCC* pzPath;
207 tCC* pz;
208 int skip = 2;
210 switch (pzName[2]) {
211 case DIRCH:
212 skip = 3;
213 case NUL:
214 break;
215 default:
216 return AG_FALSE;
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)
225 pzPath = pzProgPath;
226 else {
227 pzPath = pathfind( getenv( "PATH" ), (char*)pzProgPath, "rx" );
229 if (pzPath == NULL)
230 return AG_FALSE;
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.
239 if (pz == NULL)
240 return AG_FALSE;
242 pzName += skip;
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)
249 return AG_FALSE;
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)
259 AGFREE(pzPath);
260 return AG_TRUE;
264 static ag_bool
265 insertEnvVal(
266 char* pzBuf,
267 int bufSize,
268 tCC* pzName,
269 tCC* pzProgPath )
271 char* pzDir = pzBuf;
273 for (;;) {
274 int ch = (int)*++pzName;
275 if (! IS_VALUE_NAME_CHAR(ch))
276 break;
277 *(pzDir++) = (char)ch;
280 if (pzDir == pzBuf)
281 return AG_FALSE;
283 *pzDir = NUL;
285 pzDir = getenv( pzBuf );
288 * Environment value not found -- skip the home list entry
290 if (pzDir == NULL)
291 return AG_FALSE;
293 if (strlen( pzDir ) + 1 + strlen( pzName ) >= bufSize)
294 return AG_FALSE;
296 sprintf( pzBuf, "%s%s", pzDir, pzName );
297 return AG_TRUE;
301 LOCAL void
302 mungeString( char* pzTxt, tOptionLoadMode mode )
304 char* pzE;
306 if (mode == OPTION_LOAD_KEEP)
307 return;
309 if (IS_WHITESPACE_CHAR(*pzTxt)) {
310 char* pzS = pzTxt;
311 char* pzD = pzTxt;
312 while (IS_WHITESPACE_CHAR(*++pzS)) ;
313 while ((*(pzD++) = *(pzS++)) != NUL) ;
314 pzE = pzD-1;
315 } else
316 pzE = pzTxt + strlen( pzTxt );
318 while ((pzE > pzTxt) && IS_WHITESPACE_CHAR(pzE[-1])) pzE--;
319 *pzE = NUL;
321 if (mode == OPTION_LOAD_UNCOOKED)
322 return;
324 switch (*pzTxt) {
325 default: return;
326 case '"':
327 case '\'': break;
330 switch (pzE[-1]) {
331 default: return;
332 case '"':
333 case '\'': break;
336 (void)ao_string_cook( pzTxt, NULL );
340 static char*
341 assembleArgValue( char* pzTxt, tOptionLoadMode mode )
343 tSCC zBrk[] = " \t\n:=";
344 char* pzEnd = strpbrk( pzTxt, zBrk );
345 int space_break;
348 * Not having an argument to a configurable name is okay.
350 if (pzEnd == NULL)
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) {
359 *(pzEnd++) = NUL;
360 return pzEnd;
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);
369 *(pzEnd++) = NUL;
370 while (IS_WHITESPACE_CHAR(*pzEnd)) pzEnd++;
371 if (space_break && ((*pzEnd == ':') || (*pzEnd == '=')))
372 while (IS_WHITESPACE_CHAR(*++pzEnd)) ;
374 return 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"
382 * in autoopts.h.
384 LOCAL void
385 loadOptionLine(
386 tOptions* pOpts,
387 tOptState* pOS,
388 char* pzLine,
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 )))
398 return;
399 if (pOS->flags & OPTST_NO_INIT)
400 return;
401 pOS->pzOptArg = pzArg;
404 switch (pOS->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) {
405 case 0:
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))
412 return;
413 break;
415 case OPTST_IMM:
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)
423 return;
424 } else {
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)
431 return;
433 break;
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)
443 return;
444 } else {
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)
451 return;
453 break;
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))
462 return;
463 break;
467 * Fix up the args.
469 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
470 if (*pOS->pzOptArg != NUL)
471 return;
472 pOS->pzOptArg = NULL;
474 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
475 if (*pOS->pzOptArg == NUL)
476 pOS->pzOptArg = NULL;
477 else {
478 AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
479 pOS->flags |= OPTST_ALLOC_ARG;
482 } else {
483 if (*pOS->pzOptArg == NUL)
484 pOS->pzOptArg = zNil;
485 else {
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
507 * doc:
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.
525 void
526 optionLoadLine(
527 tOptions* pOpts,
528 tCC* pzLine )
530 tOptState st = OPTSTATE_INITIALIZER(SET);
531 char* pz;
532 AGDUPSTR( pz, pzLine, "user option line" );
533 loadOptionLine( pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED );
534 AGFREE( pz );
537 * Local Variables:
538 * mode: C
539 * c-file-style: "stroustrup"
540 * indent-tabs-mode: nil
541 * End:
542 * end of autoopts/load.c */