Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libopts / load.c
blob8a15c201b9d3d497c061642208854b8a10013c00
1 /* $NetBSD$ */
4 /*
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
10 * rc/ini file.
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 */
60 static ag_bool
61 insertProgramPath(
62 char* pzBuf,
63 int bufSize,
64 tCC* pzName,
65 tCC* pzProgPath );
67 static ag_bool
68 insertEnvVal(
69 char* pzBuf,
70 int bufSize,
71 tCC* pzName,
72 tCC* pzProgPath );
74 static char*
75 assembleArgValue( char* pzTxt, tOptionLoadMode mode );
76 /* = = = END-STATIC-FORWARD = = = */
78 /*=export_func optionMakePath
79 * private:
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 +
87 * ret-type: ag_bool
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)}.
94 * doc:
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:
100 * @*
101 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
102 * searching @code{$PATH} if necessary.
103 * @*
104 * @code{$@} is replaced with the AutoGen package data installation directory
105 * (aka @code{pkgdatadir}).
106 * @*
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:
115 * @*
116 * @bullet{} The input name exceeds @code{bufSize} bytes.
117 * @*
118 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
119 * and the next character is not '/'.
120 * @*
121 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
122 * was specified.
123 * @*
124 * @bullet{} @code{NAME} is not a known environment variable
125 * @*
126 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
127 * errors (cannot resolve the resulting path).
129 ag_bool
130 optionMakePath(
131 char* pzBuf,
132 int bufSize,
133 tCC* pzName,
134 tCC* pzProgPath )
136 size_t name_len = strlen( pzName );
138 # ifndef PKGDATADIR
139 # define PKGDATADIR ""
140 # endif
142 tSCC pkgdatadir[] = PKGDATADIR;
144 ag_bool res = AG_TRUE;
146 if (bufSize <= name_len)
147 return AG_FALSE;
150 * IF not an environment variable, just copy the data
152 if (*pzName != '$') {
153 tCC* pzS = pzName;
154 char* pzD = pzBuf;
155 int ct = bufSize;
157 for (;;) {
158 if ( (*(pzD++) = *(pzS++)) == NUL)
159 break;
160 if (--ct <= 0)
161 return AG_FALSE;
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]) {
171 case NUL:
172 return AG_FALSE;
174 case '$':
175 res = insertProgramPath( pzBuf, bufSize, pzName, pzProgPath );
176 break;
178 case '@':
179 if (pkgdatadir[0] == NUL)
180 return AG_FALSE;
182 if (name_len + sizeof (pkgdatadir) > bufSize)
183 return AG_FALSE;
185 strcpy(pzBuf, pkgdatadir);
186 strcpy(pzBuf + sizeof(pkgdatadir) - 1, pzName + 2);
187 break;
189 default:
190 res = insertEnvVal( pzBuf, bufSize, pzName, pzProgPath );
193 if (! res)
194 return AG_FALSE;
196 #if defined(HAVE_CANONICALIZE_FILE_NAME)
198 char* pz = canonicalize_file_name(pzBuf);
199 if (pz == NULL)
200 return AG_FALSE;
201 if (strlen(pz) < bufSize)
202 strcpy(pzBuf, pz);
203 free(pz);
206 #elif defined(HAVE_REALPATH)
208 char z[ PATH_MAX+1 ];
210 if (realpath( pzBuf, z ) == NULL)
211 return AG_FALSE;
213 if (strlen(z) < bufSize)
214 strcpy( pzBuf, z );
216 #endif
218 return AG_TRUE;
222 static ag_bool
223 insertProgramPath(
224 char* pzBuf,
225 int bufSize,
226 tCC* pzName,
227 tCC* pzProgPath )
229 tCC* pzPath;
230 tCC* pz;
231 int skip = 2;
233 switch (pzName[2]) {
234 case DIRCH:
235 skip = 3;
236 case NUL:
237 break;
238 default:
239 return AG_FALSE;
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)
248 pzPath = pzProgPath;
249 else {
250 pzPath = pathfind( getenv( "PATH" ), (char*)pzProgPath, "rx" );
252 if (pzPath == NULL)
253 return AG_FALSE;
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.
262 if (pz == NULL)
263 return AG_FALSE;
265 pzName += skip;
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)
272 return AG_FALSE;
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 );
283 return AG_TRUE;
287 static ag_bool
288 insertEnvVal(
289 char* pzBuf,
290 int bufSize,
291 tCC* pzName,
292 tCC* pzProgPath )
294 char* pzDir = pzBuf;
296 for (;;) {
297 int ch = (int)*++pzName;
298 if (! ISNAMECHAR( ch ))
299 break;
300 *(pzDir++) = (char)ch;
303 if (pzDir == pzBuf)
304 return AG_FALSE;
306 *pzDir = NUL;
308 pzDir = getenv( pzBuf );
311 * Environment value not found -- skip the home list entry
313 if (pzDir == NULL)
314 return AG_FALSE;
316 if (strlen( pzDir ) + 1 + strlen( pzName ) >= bufSize)
317 return AG_FALSE;
319 sprintf( pzBuf, "%s%s", pzDir, pzName );
320 return AG_TRUE;
324 LOCAL void
325 mungeString( char* pzTxt, tOptionLoadMode mode )
327 char* pzE;
329 if (mode == OPTION_LOAD_KEEP)
330 return;
332 if (isspace( (int)*pzTxt )) {
333 char* pzS = pzTxt;
334 char* pzD = pzTxt;
335 while (isspace( (int)*++pzS )) ;
336 while ((*(pzD++) = *(pzS++)) != NUL) ;
337 pzE = pzD-1;
338 } else
339 pzE = pzTxt + strlen( pzTxt );
341 while ((pzE > pzTxt) && isspace( (int)pzE[-1] )) pzE--;
342 *pzE = NUL;
344 if (mode == OPTION_LOAD_UNCOOKED)
345 return;
347 switch (*pzTxt) {
348 default: return;
349 case '"':
350 case '\'': break;
353 switch (pzE[-1]) {
354 default: return;
355 case '"':
356 case '\'': break;
359 (void)ao_string_cook( pzTxt, NULL );
363 static char*
364 assembleArgValue( char* pzTxt, tOptionLoadMode mode )
366 tSCC zBrk[] = " \t:=";
367 char* pzEnd = strpbrk( pzTxt, zBrk );
368 int space_break;
371 * Not having an argument to a configurable name is okay.
373 if (pzEnd == NULL)
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) {
382 *(pzEnd++) = NUL;
383 return pzEnd;
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);
392 *(pzEnd++) = NUL;
393 while (isspace((int)*pzEnd)) pzEnd++;
394 if (space_break && ((*pzEnd == ':') || (*pzEnd == '=')))
395 while (isspace((int)*++pzEnd)) ;
397 return 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"
405 * in autoopts.h.
407 LOCAL void
408 loadOptionLine(
409 tOptions* pOpts,
410 tOptState* pOS,
411 char* pzLine,
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 )))
421 return;
422 if (pOS->flags & OPTST_NO_INIT)
423 return;
424 pOS->pzOptArg = pzArg;
427 switch (pOS->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) {
428 case 0:
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))
435 return;
436 break;
438 case OPTST_IMM:
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)
446 return;
447 } else {
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)
454 return;
456 break;
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)
466 return;
467 } else {
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)
474 return;
476 break;
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))
485 return;
486 break;
490 * Fix up the args.
492 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
493 if (*pOS->pzOptArg != NUL)
494 return;
495 pOS->pzOptArg = NULL;
497 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
498 if (*pOS->pzOptArg == NUL)
499 pOS->pzOptArg = NULL;
500 else {
501 AGDUPSTR( pOS->pzOptArg, pOS->pzOptArg, "option argument" );
502 pOS->flags |= OPTST_ALLOC_ARG;
505 } else {
506 if (*pOS->pzOptArg == NUL)
507 pOS->pzOptArg = zNil;
508 else {
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
530 * doc:
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.
548 void
549 optionLoadLine(
550 tOptions* pOpts,
551 tCC* pzLine )
553 tOptState st = OPTSTATE_INITIALIZER(SET);
554 char* pz;
555 AGDUPSTR( pz, pzLine, "user option line" );
556 loadOptionLine( pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED );
557 AGFREE( pz );
560 * Local Variables:
561 * mode: C
562 * c-file-style: "stroustrup"
563 * indent-tabs-mode: nil
564 * End:
565 * end of autoopts/load.c */