bumped version
[gnutls.git] / src / libopts / load.c
blob1ea780806131a9f8e06fabf985095b7832ce3011
2 /**
3 * \file load.c
4 * Time-stamp: "2012-03-31 13:13:34 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
8 * rc/ini file.
10 * This file is part of AutoOpts, a companion to AutoGen.
11 * AutoOpts is free software.
12 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
14 * AutoOpts is available under any one of two licenses. The license
15 * in use must be one of these two and the choice is under the control
16 * of the user of the license.
18 * The GNU Lesser General Public License, version 3 or later
19 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
21 * The Modified Berkeley Software Distribution License
22 * See the file "COPYING.mbsd"
24 * These files have the following md5sums:
26 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
27 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
28 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
31 /* = = = START-STATIC-FORWARD = = = */
32 static bool
33 add_prog_path(char * pzBuf, int bufSize, char const * pzName,
34 char const * pzProgPath);
36 static bool
37 add_env_val(char * pzBuf, int bufSize, char const * pzName);
39 static char *
40 assemble_arg_val(char * pzTxt, tOptionLoadMode mode);
41 /* = = = END-STATIC-FORWARD = = = */
43 /*=export_func optionMakePath
44 * private:
46 * what: translate and construct a path
47 * arg: + char* + pzBuf + The result buffer +
48 * arg: + int + bufSize + The size of this buffer +
49 * arg: + char const* + pzName + The input name +
50 * arg: + char const* + pzProgPath + The full path of the current program +
52 * ret-type: bool
53 * ret-desc: true if the name was handled, otherwise false.
54 * If the name does not start with ``$'', then it is handled
55 * simply by copying the input name to the output buffer and
56 * resolving the name with either
57 * @code{canonicalize_file_name(3GLIBC)} or @code{realpath(3C)}.
59 * doc:
61 * This routine will copy the @code{pzName} input name into the
62 * @code{pzBuf} output buffer, not exceeding @code{bufSize} bytes. If the
63 * first character of the input name is a @code{'$'} character, then there
64 * is special handling:
65 * @*
66 * @code{$$} is replaced with the directory name of the @code{pzProgPath},
67 * searching @code{$PATH} if necessary.
68 * @*
69 * @code{$@} is replaced with the AutoGen package data installation directory
70 * (aka @code{pkgdatadir}).
71 * @*
72 * @code{$NAME} is replaced by the contents of the @code{NAME} environment
73 * variable. If not found, the search fails.
75 * Please note: both @code{$$} and @code{$NAME} must be at the start of the
76 * @code{pzName} string and must either be the entire string or be followed
77 * by the @code{'/'} (backslash on windows) character.
79 * err: @code{false} is returned if:
80 * @*
81 * @bullet{} The input name exceeds @code{bufSize} bytes.
82 * @*
83 * @bullet{} @code{$$}, @code{$@@} or @code{$NAME} is not the full string
84 * and the next character is not '/'.
85 * @*
86 * @bullet{} libopts was built without PKGDATADIR defined and @code{$@@}
87 * was specified.
88 * @*
89 * @bullet{} @code{NAME} is not a known environment variable
90 * @*
91 * @bullet{} @code{canonicalize_file_name} or @code{realpath} return
92 * errors (cannot resolve the resulting path).
93 =*/
94 bool
95 optionMakePath(char * pzBuf, int bufSize, char const * pzName,
96 char const * pzProgPath)
98 size_t name_len = strlen(pzName);
100 if (((size_t)bufSize <= name_len) || (name_len == 0))
101 return false;
104 * IF not an environment variable, just copy the data
106 if (*pzName != '$') {
107 char const* pzS = pzName;
108 char* pzD = pzBuf;
109 int ct = bufSize;
111 for (;;) {
112 if ( (*(pzD++) = *(pzS++)) == NUL)
113 break;
114 if (--ct <= 0)
115 return false;
120 * IF the name starts with "$$", then it must be "$$" or
121 * it must start with "$$/". In either event, replace the "$$"
122 * with the path to the executable and append a "/" character.
124 else switch (pzName[1]) {
125 case NUL:
126 return false;
128 case '$':
129 if (! add_prog_path(pzBuf, bufSize, pzName, pzProgPath))
130 return false;
131 break;
133 case '@':
134 if (program_pkgdatadir[0] == NUL)
135 return false;
137 if (snprintf(pzBuf, bufSize, "%s%s", program_pkgdatadir, pzName + 2)
138 >= bufSize)
139 return false;
140 break;
142 default:
143 if (! add_env_val(pzBuf, bufSize, pzName))
144 return false;
147 #if defined(HAVE_CANONICALIZE_FILE_NAME)
149 char * pz = canonicalize_file_name(pzBuf);
150 if (pz == NULL)
151 return false;
153 name_len = strlen(pz);
154 if (name_len >= (size_t)bufSize) {
155 free(pz);
156 return false;
159 memcpy(pzBuf, pz, name_len + 1);
160 free(pz);
163 #elif defined(HAVE_REALPATH)
165 char z[PATH_MAX+1];
167 if (realpath(pzBuf, z) == NULL)
168 return false;
170 name_len = strlen(z);
171 if (name_len >= bufSize)
172 return false;
174 memcpy(pzBuf, z, name_len + 1);
176 #endif
178 return true;
181 static bool
182 add_prog_path(char * pzBuf, int bufSize, char const * pzName,
183 char const * pzProgPath)
185 char const* pzPath;
186 char const* pz;
187 int skip = 2;
189 switch (pzName[2]) {
190 case DIRCH:
191 skip = 3;
192 case NUL:
193 break;
194 default:
195 return false;
199 * See if the path is included in the program name.
200 * If it is, we're done. Otherwise, we have to hunt
201 * for the program using "pathfind".
203 if (strchr(pzProgPath, DIRCH) != NULL)
204 pzPath = pzProgPath;
205 else {
206 pzPath = pathfind(getenv("PATH"), (char*)pzProgPath, "rx");
208 if (pzPath == NULL)
209 return false;
212 pz = strrchr(pzPath, DIRCH);
215 * IF we cannot find a directory name separator,
216 * THEN we do not have a path name to our executable file.
218 if (pz == NULL)
219 return false;
221 pzName += skip;
224 * Concatenate the file name to the end of the executable path.
225 * The result may be either a file or a directory.
227 if ((pz - pzPath)+1 + strlen(pzName) >= (unsigned)bufSize)
228 return false;
230 memcpy(pzBuf, pzPath, (size_t)((pz - pzPath)+1));
231 strcpy(pzBuf + (pz - pzPath) + 1, pzName);
234 * If the "pzPath" path was gotten from "pathfind()", then it was
235 * allocated and we need to deallocate it.
237 if (pzPath != pzProgPath)
238 AGFREE(pzPath);
239 return true;
243 static bool
244 add_env_val(char * pzBuf, int bufSize, char const * pzName)
246 char * pzDir = pzBuf;
248 for (;;) {
249 int ch = (int)*++pzName;
250 if (! IS_VALUE_NAME_CHAR(ch))
251 break;
252 *(pzDir++) = (char)ch;
255 if (pzDir == pzBuf)
256 return false;
258 *pzDir = NUL;
260 pzDir = getenv(pzBuf);
263 * Environment value not found -- skip the home list entry
265 if (pzDir == NULL)
266 return false;
268 if (strlen(pzDir) + 1 + strlen(pzName) >= (unsigned)bufSize)
269 return false;
271 sprintf(pzBuf, "%s%s", pzDir, pzName);
272 return true;
276 LOCAL void
277 mungeString(char* pzTxt, tOptionLoadMode mode)
279 char * pzE;
281 if (mode == OPTION_LOAD_KEEP)
282 return;
284 if (IS_WHITESPACE_CHAR(*pzTxt)) {
285 char * pzS = SPN_WHITESPACE_CHARS(pzTxt+1);
286 size_t l = strlen(pzS) + 1;
287 memmove(pzTxt, pzS, l);
288 pzE = pzTxt + l - 1;
290 } else
291 pzE = pzTxt + strlen(pzTxt);
293 pzE = SPN_WHITESPACE_BACK(pzTxt, pzE);
294 *pzE = NUL;
296 if (mode == OPTION_LOAD_UNCOOKED)
297 return;
299 switch (*pzTxt) {
300 default: return;
301 case '"':
302 case '\'': break;
305 switch (pzE[-1]) {
306 default: return;
307 case '"':
308 case '\'': break;
311 (void)ao_string_cook(pzTxt, NULL);
315 static char *
316 assemble_arg_val(char * pzTxt, tOptionLoadMode mode)
318 char* pzEnd = strpbrk(pzTxt, ARG_BREAK_STR);
319 int space_break;
322 * Not having an argument to a configurable name is okay.
324 if (pzEnd == NULL)
325 return pzTxt + strlen(pzTxt);
328 * If we are keeping all whitespace, then the modevalue starts with the
329 * character that follows the end of the configurable name, regardless
330 * of which character caused it.
332 if (mode == OPTION_LOAD_KEEP) {
333 *(pzEnd++) = NUL;
334 return pzEnd;
338 * If the name ended on a white space character, remember that
339 * because we'll have to skip over an immediately following ':' or '='
340 * (and the white space following *that*).
342 space_break = IS_WHITESPACE_CHAR(*pzEnd);
343 *(pzEnd++) = NUL;
345 pzEnd = SPN_WHITESPACE_CHARS(pzEnd);
346 if (space_break && ((*pzEnd == ':') || (*pzEnd == '=')))
347 pzEnd = SPN_WHITESPACE_CHARS(pzEnd+1);
349 return pzEnd;
354 * Load an option from a block of text. The text must start with the
355 * configurable/option name and be followed by its associated value.
356 * That value may be processed in any of several ways. See "tOptionLoadMode"
357 * in autoopts.h.
359 LOCAL void
360 loadOptionLine(
361 tOptions* pOpts,
362 tOptState* pOS,
363 char* pzLine,
364 tDirection direction,
365 tOptionLoadMode load_mode )
367 pzLine = SPN_WHITESPACE_CHARS(pzLine);
370 char* pzArg = assemble_arg_val(pzLine, load_mode);
372 if (! SUCCESSFUL(opt_find_long(pOpts, pzLine, pOS)))
373 return;
374 if (pOS->flags & OPTST_NO_INIT)
375 return;
376 pOS->pzOptArg = pzArg;
379 switch (pOS->flags & (OPTST_IMM|OPTST_DISABLE_IMM)) {
380 case 0:
382 * The selected option has no immediate action.
383 * THEREFORE, if the direction is PRESETTING
384 * THEN we skip this option.
386 if (PRESETTING(direction))
387 return;
388 break;
390 case OPTST_IMM:
391 if (PRESETTING(direction)) {
393 * We are in the presetting direction with an option we handle
394 * immediately for enablement, but normally for disablement.
395 * Therefore, skip if disabled.
397 if ((pOS->flags & OPTST_DISABLED) == 0)
398 return;
399 } else {
401 * We are in the processing direction with an option we handle
402 * immediately for enablement, but normally for disablement.
403 * Therefore, skip if NOT disabled.
405 if ((pOS->flags & OPTST_DISABLED) != 0)
406 return;
408 break;
410 case OPTST_DISABLE_IMM:
411 if (PRESETTING(direction)) {
413 * We are in the presetting direction with an option we handle
414 * immediately for disablement, but normally for disablement.
415 * Therefore, skip if NOT disabled.
417 if ((pOS->flags & OPTST_DISABLED) != 0)
418 return;
419 } else {
421 * We are in the processing direction with an option we handle
422 * immediately for disablement, but normally for disablement.
423 * Therefore, skip if disabled.
425 if ((pOS->flags & OPTST_DISABLED) == 0)
426 return;
428 break;
430 case OPTST_IMM|OPTST_DISABLE_IMM:
432 * The selected option is always for immediate action.
433 * THEREFORE, if the direction is PROCESSING
434 * THEN we skip this option.
436 if (PROCESSING(direction))
437 return;
438 break;
442 * Fix up the args.
444 if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
445 if (*pOS->pzOptArg != NUL)
446 return;
447 pOS->pzOptArg = NULL;
449 } else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
450 if (*pOS->pzOptArg == NUL)
451 pOS->pzOptArg = NULL;
452 else {
453 AGDUPSTR(pOS->pzOptArg, pOS->pzOptArg, "option argument");
454 pOS->flags |= OPTST_ALLOC_ARG;
457 } else {
458 if (*pOS->pzOptArg == NUL)
459 pOS->pzOptArg = zNil;
460 else {
461 AGDUPSTR(pOS->pzOptArg, pOS->pzOptArg, "option argument");
462 pOS->flags |= OPTST_ALLOC_ARG;
467 tOptionLoadMode sv = option_load_mode;
468 option_load_mode = load_mode;
469 handle_opt(pOpts, pOS);
470 option_load_mode = sv;
475 /*=export_func optionLoadLine
477 * what: process a string for an option name and value
479 * arg: tOptions*, pOpts, program options descriptor
480 * arg: char const*, pzLine, NUL-terminated text
482 * doc:
484 * This is a client program callable routine for setting options from, for
485 * example, the contents of a file that they read in. Only one option may
486 * appear in the text. It will be treated as a normal (non-preset) option.
488 * When passed a pointer to the option struct and a string, it will find
489 * the option named by the first token on the string and set the option
490 * argument to the remainder of the string. The caller must NUL terminate
491 * the string. Any embedded new lines will be included in the option
492 * argument. If the input looks like one or more quoted strings, then the
493 * input will be "cooked". The "cooking" is identical to the string
494 * formation used in AutoGen definition files (@pxref{basic expression}),
495 * except that you may not use backquotes.
497 * err: Invalid options are silently ignored. Invalid option arguments
498 * will cause a warning to print, but the function should return.
500 void
501 optionLoadLine(tOptions * pOpts, char const * pzLine)
503 tOptState st = OPTSTATE_INITIALIZER(SET);
504 char* pz;
505 AGDUPSTR(pz, pzLine, "user option line");
506 loadOptionLine(pOpts, &st, pz, DIRECTION_PROCESS, OPTION_LOAD_COOKED);
507 AGFREE(pz);
510 * Local Variables:
511 * mode: C
512 * c-file-style: "stroustrup"
513 * indent-tabs-mode: nil
514 * End:
515 * end of autoopts/load.c */