Debian Codegraph @

converter/other/fiasco/params.c

raw
1 /*
2 * params.c: Parameter file and command line parsing
3 *
4 * Written by: Stefan Frank
5 * Ullrich Hafner
6 *
7 * This file is part of FIASCO (Fractal Image And Sequence COdec)
8 * Copyright (C) 1994-2000 Ullrich Hafner
9 */
10
11 /*
12 * $Date: 2000/07/15 17:24:21 $
13 * $Author: hafner $
14 * $Revision: 5.2 $
15 * $State: Exp $
16 */
17
18 #define _DEFAULT_SOURCE 1 /* New name for SVID & BSD source defines */
19 #define _BSD_SOURCE 1
20 /* Make sure strdup() is in string.h and strcaseeq() is in nstring.h */
21 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <math.h> /* strtod() on SUN sparc */
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <getopt.h> /* system or ../lib */
34
35 #include "pm_c_util.h"
36 #include "mallocvar.h"
37 #include "nstring.h"
38
39 #include "types.h"
40 #include "macros.h"
41 #include "bit-io.h"
42 #include "misc.h"
43 #include "fiasco.h"
44
45 #include "params.h"
46
47
48 static void
49 setParameter(param_t * const parameter,
50 const char * const value) {
51 /*----------------------------------------------------------------------------
52
53 Set value of 'parameter' to 'value'.
54
55 No return value.
56
57 Side effects:
58 'parameter.value' is changed accordingly
59
60 -----------------------------------------------------------------------------*/
61 assert (parameter);
62
63 switch (parameter->type) {
64 case PFLAG:
65 if (value != NULL && *value != '\0') {
66 if (strcaseeq (value, "TRUE"))
67 parameter->value.b = YES;
68 else if (strcaseeq (value, "FALSE"))
69 parameter->value.b = NO;
70 else if (strcaseeq (value, "YES"))
71 parameter->value.b = YES;
72 else if (strcaseeq (value, "NO"))
73 parameter->value.b = NO;
74 else {
75 long int data;
76 char *endptr;
77
78 data = strtol (value, &endptr, 0);
79 if (*endptr != '\0' || endptr == value)
80 pm_message("Invalid value `%s' converted to %d",
81 value, (int) data);
82 parameter->value.b = data ? YES : NO;
83 }
84 } else
85 parameter->value.b = !parameter->value.b;
86 break;
87 case PINT: {
88 long int data;
89 char *endptr;
90
91 data = strtol (value, &endptr, 0);
92 if (*endptr != '\0' || endptr == value)
93 pm_message("Invalid value `%s' converted to %d",
94 value, (int) data);
95 parameter->value.i = data;
96 } break;
97 case PFLOAT: {
98 double data;
99 char *endptr;
100
101 data = strtod (value, &endptr);
102 if (*endptr != '\0' || endptr == value)
103 pm_message("Invalid value `%s' converted to %f",
104 value, (double) data);
105 parameter->value.f = data;
106 } break;
107 case PSTR:
108 case POSTR:
109 parameter->value.s = value ? strdup (value) : NULL;
110 break;
111 default:
112 pm_error("Invalid parameter type for %s", parameter->name);
113 }
114 }
115
116
117
118 static int
119 getParameterIndex(const param_t * const params,
120 const char * const search_string) {
121 /*----------------------------------------------------------------------------
122 Search for parameter with name 'search_string' in parameter struct.
123
124 Return value: index of parameter or -1 if no matching parameter has been
125 found
126 -----------------------------------------------------------------------------*/
127 int n;
128 int index = -1;
129
130 assert (params && search_string);
131
132 for (n = 0; params [n].name != NULL; n++) {
133 if (strcaseeq (params [n].name, search_string)) {
134 index = n;
135 break;
136 }
137 }
138
139 return index;
140 }
141
142
143
144 static void
145 readParameterFile(param_t * const params,
146 FILE * const file) {
147 /*----------------------------------------------------------------------------
148
149 Read parameter settings from 'file'.
150
151 No return value.
152
153 Side effects:
154 'params [].value' are changed if specified in 'file'
155 -----------------------------------------------------------------------------*/
156 char buffer [MAXSTRLEN];
157 int n = 0;
158
159 assert (params && file);
160
161 while (fgets (buffer, MAXSTRLEN, file) != NULL) {
162 char *b; /* temporary variable */
163 char *name; /* parameter name */
164 char *value; /* parameter value */
165 int pind; /* current argument number */
166
167 b = strchr (buffer, '#');
168 if (b != NULL) /* Strip comments. */
169 *b = '\0';
170
171 b = strchr (buffer, '=');
172 if (b == NULL) /* Strip lines that contain no '=' */
173 continue;
174 *b = '\0'; /* Replace '=' by string terminator */
175
176 /* Extract value of parameter */
177 for (value = b + 1; ISSPACE (*value); value++)
178 ; /* Delete leading spaces */
179
180 for (b = value + strlen (value) - 1; b >= value && ISSPACE (*b); b--)
181 *b = '\0'; /* Delete trailing spaces. */
182
183 /* Extract parameter name */
184 for (name = buffer; ISSPACE (*name); name++)
185 ; /* Delete leading spaces */
186
187 for (b = name + strlen (name) - 1; b >= name && ISSPACE (*b); b--)
188 *b = '\0'; /* Delete trailing spaces. */
189
190 pind = getParameterIndex(params, name);
191 if (pind >= 0)
192 setParameter(&params[pind], value);
193
194 ++n;
195 }
196 }
197
198
199
200 static void
201 usage(const param_t * const params,
202 const char * const progname,
203 const char * const synopsis,
204 const char * const comment,
205 const char * const non_opt_string,
206 bool_t const show_all_options,
207 const char * const sys_file_name,
208 const char * const usr_file_name) {
209 /*----------------------------------------------------------------------------
210
211 Generates and prints command line description from param_t struct 'params'.
212 'progname' is the name of the executable, 'synopsis' a short program
213 description, and 'comment' some more advice.
214 If flag 'show_all_options' is set then print also options that are not
215 associated with a short option character.
216 'sys_file_name' and 'usr_file_name' are filenames to parameter files.
217
218 No return value.
219 -----------------------------------------------------------------------------*/
220 int i;
221 size_t width = 0;
222
223 fprintf (stderr, "Usage: %s [OPTION]...%s\n", progname,
224 non_opt_string ? non_opt_string : " ");
225 if (synopsis != NULL)
226 fprintf (stderr, "%s", synopsis);
227 fprintf (stderr, "\n\n");
228 fprintf (stderr, "Mandatory or optional arguments to long options "
229 "are mandatory or optional\nfor short options too. "
230 "Default values are surrounded by {}.\n");
231 for (i = 0; params [i].name != NULL; i++) {
232 if (params [i].optchar != '\0' || show_all_options) {
233 if (params [i].type == POSTR)
234 width = MAX(width, (strlen (params [i].name)
235 + strlen (params [i].argument_name) + 2));
236 else if (params [i].type != PFLAG)
237 width = MAX(width, (strlen (params [i].name)
238 + strlen (params [i].argument_name)));
239 else
240 width = MAX(width, (strlen (params [i].name)) - 1);
241 }
242 }
243 for (i = 0; params [i].name != NULL; i++) {
244 if (params [i].optchar != '\0' || show_all_options) {
245 if (params [i].optchar != '\0')
246 fprintf (stderr, " -%c, --", params [i].optchar);
247 else
248 fprintf (stderr, " --");
249
250 if (params [i].type == POSTR)
251 fprintf (stderr, "%s=[%s]%-*s ", params [i].name,
252 params [i].argument_name,
253 (unsigned)
254 MAX(0, (width - 2 - strlen (params [i].name)
255 - strlen (params [i].argument_name))), "");
256 else if (params [i].type != PFLAG)
257 fprintf (stderr, "%s=%-*s ", params [i].name,
258 (unsigned)(width - strlen (params [i].name)),
259 params [i].argument_name);
260 else
261 fprintf (stderr, "%-*s ",
262 (unsigned)(width + 1), params [i].name);
263
264 fprintf (stderr, params [i].use, params [i].argument_name);
265
266 switch (params [i].type) {
267 case PFLAG:
268 break;
269 case PINT:
270 fprintf (stderr, "{%d}", params [i].value.i);
271 break;
272 case PFLOAT:
273 fprintf (stderr, "{%.2f}", (double) params [i].value.f);
274 break;
275 case PSTR:
276 case POSTR:
277 if (params [i].value.s)
278 fprintf (stderr, "{%s}", params [i].value.s);
279 break;
280 default:
281 pm_error("type %d for %s invalid",
282 params [i].type, params [i].name);
283 }
284 fprintf (stderr, "\n");
285 }
286 }
287 fprintf (stderr, "\n");
288
289 fprintf (stderr, "Parameter initialization order:\n");
290 fprintf (stderr,
291 "1.) %s\n2.) $HOME/%s\t 3.) command line\t 4.) --config=file",
292 sys_file_name, usr_file_name);
293 fprintf (stderr, "\n\n");
294
295 if (comment != NULL)
296 fprintf (stderr, "%s\n", comment);
297
298 exit (1);
299 }
300
301
302
303 const void *
304 parameter_value(const param_t * const params,
305 const char * const name) {
306 /*----------------------------------------------------------------------------
307 Extract value of parameter 'name.' of the given parameters 'params'.
308
309 Return value: value of given parameter
310
311 -----------------------------------------------------------------------------*/
312 int pind = getParameterIndex(params, name);
313
314 if (pind < 0)
315 pm_error("Invalid parameter '%s'.", name);
316
317 if (params[pind].type == PSTR || params[pind].type == POSTR)
318 return params[pind].value.s;
319
320 return &(params[pind].value);
321 }
322
323
324
325 int
326 parseargs(param_t * const usr_params,
327 int argc,
328 const char ** const argv,
329 const char * const synopsis,
330 const char * const comment,
331 const char * const non_opt_string,
332 const char * const path,
333 const char * const sys_file_name,
334 const char * const usr_file_name) {
335 /*----------------------------------------------------------------------------
336 Perform the command line parsing.
337 List of allowed parameters is given by 'usr_params'.
338 Command line and number of parameters are given by 'argv' and 'argc'.
339 'synopsis' contains a brief description of the program and
340 'comment' may contain some additional advice.
341 Initialization order of parameters:
342 1.) Default values given by the param_t struct
343 2.) System parameter-file ('path'/'sys_file_name')
344 3.) User parameter-file ($HOME/'usr_file_name')
345 4.) Command line parameters
346 5.) Parameter-file forced by option -f (--config-file)
347
348 Return value:
349 index in ARGV of the first ARGV-element that is not an option.
350
351 Side effects:
352 the elements of ARGV are permuted
353 usr_params [].value is modified
354 -----------------------------------------------------------------------------*/
355 extern int optind; /* index in ARGV of the 1st element
356 that is not an option */
357 bool_t detailed_help = NO; /* NO if all parameters can be modified
358 with short options too */
359 unsigned n1; /* number of user parameters */
360 unsigned n2; /* number of system parameters */
361 bool_t read_config_file = NO; /* will override command line */
362 param_t *params; /* array of user and system params */
363 param_t *sys_params; /* array of system parameters */
364 param_t detailed_sys_params [] = /* detailed system parameters */
365 {
366 {"version", NULL, 'v', PFLAG, {0}, NULL,
367 "Print program version number, then exit."},
368 {"verbose", "NUM", 'V', PINT, {0}, "1",
369 "Set level of verbosity to `%s'."},
370 {"config", "FILE", 'f', PSTR, {0}, NULL,
371 "Load `%s' to initialize parameters."},
372 {"info", NULL, 'h', PFLAG, {0}, NULL,
373 "Print brief help, then exit."},
374 {"help", NULL, 'H', PFLAG, {0}, NULL,
375 "Print detailed help, then exit."},
376 {NULL, NULL, 0, PSTR, {0}, NULL, NULL }
377 };
378 param_t short_sys_params [] = /* short system parameters */
379 {
380 {"version", NULL, 'v', PFLAG, {0}, NULL,
381 "Print program version number, then exit."},
382 {"verbose", "NUM", 'V', PINT, {0}, "1",
383 "Set level of verbosity to `%s'."},
384 {"config", "FILE", 'f', PSTR, {0}, NULL,
385 "Load `%s' to initialize parameters."},
386 {"help", NULL, 'h', PFLAG, {0}, NULL,
387 "Print this help, then exit."},
388 {NULL, NULL, 0, PSTR, {0}, NULL, NULL }
389 };
390 const char * sys_path; /* path to system config file */
391
392 pm_asprintf(&sys_path, "%s/%s", path, sys_file_name);
393
394 /* Set parameters defaults */
395 {
396 param_t *p;
397
398 for (p = usr_params; p->name != NULL; p++)
399 {
400 setParameter(p, p->default_value);
401 if (p->optchar == '\0')
402 detailed_help = YES;
403 }
404
405 sys_params = detailed_help ? detailed_sys_params : short_sys_params;
406
407 for (p = sys_params; p->name != NULL; p++)
408 setParameter(p, p->default_value);
409 }
410 /* Append system command line option to user parameters */
411 for (n1 = 0; usr_params [n1].name != NULL; n1++)
412 ;
413 for (n2 = 0; sys_params [n2].name != NULL; n2++)
414 ;
415 MALLOCARRAY_NOFAIL(params, n1 + n2 + 1);
416
417 memcpy(params, usr_params, n1 * sizeof(param_t));
418 memcpy(params + n1, sys_params, (n2 + 1) * sizeof(param_t));
419
420 {
421 /* Try to open the system resource file 'path'/'sys_file_name' */
422
423 FILE *parameter_file = open_file(sys_path, NULL, READ_ACCESS);
424
425 if (parameter_file) {
426 readParameterFile(params, parameter_file);
427 fclose(parameter_file);
428 }
429 }
430 {
431 /* Try to read user resource file $HOME/'usr_file_name' */
432
433 FILE *parameter_file = open_file(usr_file_name, "HOME", READ_ACCESS);
434
435 if (parameter_file) {
436 readParameterFile(params, parameter_file);
437 fclose(parameter_file);
438 }
439 }
440 {
441 /* Parse command line options */
442
443 extern char *optarg; /* argument of current option */
444 struct option *long_options; /* array of long options */
445 int option_index = 0;
446 char optstr [MAXSTRLEN]; /* string containing the legitimate
447 option characters */
448 int optchar; /* found option character */
449
450 {
451 /* Build short option string for getopt_long (). */
452 param_t *p; /* counter */
453 char *ptr_optstr; /* pointer to position in string */
454
455 ptr_optstr = optstr;
456 for (p = params; p->name != NULL; p++)
457 if (p->optchar != '\0')
458 {
459 *ptr_optstr++ = p->optchar;
460 if (p->type == POSTR)
461 {
462 *ptr_optstr++ = ':';
463 *ptr_optstr++ = ':';
464 }
465 else if (p->type != PFLAG)
466 *ptr_optstr++ = ':';
467 }
468 *ptr_optstr = '\0';
469 }
470
471 {
472 /* Build long option string for getopt_long (). */
473
474 int i;
475
476 MALLOCARRAY_NOFAIL(long_options, n1 + n2 + 1);
477
478 for (i = 0; params [i].name != NULL; i++) {
479 long_options [i].name = params [i].name;
480 switch (params [i].type)
481 {
482 case PFLAG:
483 long_options [i].has_arg = 0;
484 break;
485 case POSTR:
486 long_options [i].has_arg = 2;
487 break;
488 case PINT:
489 case PSTR:
490 case PFLOAT:
491 default:
492 long_options [i].has_arg = 1;
493 break;
494 }
495 long_options [i].has_arg = params [i].type != PFLAG;
496 long_options [i].flag = NULL;
497 long_options [i].val = 0;
498 }
499 }
500
501 /* Parse command line */
502
503 while ((optchar = getopt_long(argc, (char **)argv, optstr,
504 long_options,
505 &option_index)) != EOF) {
506 int param_index = -1;
507
508 switch (optchar) {
509 case 0:
510 param_index = option_index;
511 break;
512 case ':':
513 if (detailed_help)
514 fprintf (stderr,
515 "Try `%s -h' or `%s --help' for "
516 "more information.\n",
517 argv [0], argv [0]);
518 else
519 fprintf (stderr, "Try `%s --help' for more information.\n",
520 argv [0]);
521 exit (2);
522 break;
523 case '?':
524 if (detailed_help)
525 fprintf (stderr,
526 "Try `%s -h' or `%s --help' "
527 "for more information.\n",
528 argv [0], argv [0]);
529 else
530 fprintf (stderr, "Try `%s --help' for more information.\n",
531 argv [0]);
532 exit (2);
533 break;
534 default: {
535 int i;
536
537 for (i = 0; params [i].name != NULL; i++) {
538 if (params [i].optchar == optchar) {
539 param_index = i;
540 break;
541 }
542 }
543 }
544 }
545 /* Check for system options */
546
547 if (param_index >= 0) {
548 setParameter(params + param_index, optarg ? optarg : "");
549 if (streq(params[param_index].name, "help"))
550 usage(params, argv [0], synopsis, comment, non_opt_string,
551 YES, sys_path, usr_file_name);
552 else if (streq(params[param_index].name, "info"))
553 usage(params, argv [0], synopsis, comment, non_opt_string,
554 NO, sys_path, usr_file_name);
555 else if (streq(params[param_index].name, "version")) {
556 fprintf(stderr, "%s " VERSION "\n", argv [0]);
557 {
558 /* Kludge for standard Netpbm version announcement */
559 const char * modifiedArgv[2];
560 int argc;
561 modifiedArgv[0] = argv[0];
562 modifiedArgv[1] = (char *) "--version";
563 argc = 2;
564 pm_proginit(&argc, modifiedArgv);
565 }
566 exit (2);
567 } else if (streq(params[param_index].name, "verbose"))
568 fiasco_set_verbosity(
569 * (fiasco_verbosity_e *) parameter_value(params,
570 "verbose"));
571 else if (streq(params[param_index].name, "config"))
572 read_config_file = YES;
573 param_index = -1; /* clear index flag */
574 }
575 }
576 free(long_options);
577 }
578
579 /* Read config-file if specified by option -f */
580 if (read_config_file) {
581 char * filename;
582
583 if ((filename = (char *) parameter_value(params, "config")) != NULL) {
584 FILE * parameter_file; /* input file */
585
586 pm_message("Options set in file `%s' will override"
587 " command line options.", filename);
588 parameter_file = open_file(filename, NULL, READ_ACCESS);
589 if (parameter_file != NULL) {
590 readParameterFile(params, parameter_file);
591 fclose(parameter_file);
592 } else
593 pm_error("Failed to open config file '%s'", filename);
594 } else
595 pm_error("Invalid config filename.");
596 }
597
598 memcpy(usr_params, params, n1 * sizeof (param_t)); /* fill user struct */
599 pm_strfree(sys_path);
600
601 return optind;
602 }
603
604
605
606 void
607 ask_and_set(param_t * const params,
608 const char * const name,
609 const char * const msg) {
610 /*----------------------------------------------------------------------------
611 Ask user (print given message 'msg') for missing mandatory
612 parameter 'name' of the given parameters 'params'.
613
614 No return value.
615
616 Side effects:
617 'params ['name'].value' is changed
618 -----------------------------------------------------------------------------*/
619 char answer[MAXSTRLEN];
620 int index = getParameterIndex(params, name);
621
622 if (index < 0)
623 pm_error("Invalid parameter '%s'.", name);
624
625 if (msg)
626 pm_message("%s", msg);
627
628 switch (params[index].type) {
629 case PFLAG: /* Unusual, at least. */
630 pm_message("Flags should be initialized and set on demand, "
631 "not request");
632 case PINT:
633 case PSTR:
634 case POSTR:
635 case PFLOAT:
636 scanf(MAXSTRLEN_SCANF, answer);
637 setParameter(&params [index], answer);
638 break;
639 default:
640 pm_error("Invalid parameter type for %s", name);
641 }
642 }
643
644
645
646 void
647 write_parameters(const param_t * const params,
648 FILE * const ofP) {
649 /*----------------------------------------------------------------------------
650 Report all parameter settings to file *ofP.
651
652 No return value.
653 -----------------------------------------------------------------------------*/
654 unsigned int pind;
655
656 assert(params);
657 assert(ofP);
658
659 fprintf(ofP, "Command line options/defaults:\n");
660
661 for (pind = 0; params[pind].name != NULL; ++pind) {
662
663 fprintf(ofP, "# %s = ", params[pind].name);
664
665 switch(params[pind].type) {
666 case PFLAG:
667 fprintf(ofP, "%s\n", params[pind].value.b ? "TRUE" : "FALSE");
668 break;
669 case PINT:
670 fprintf(ofP, "%d\n", params[pind].value.i);
671 break;
672 case PFLOAT:
673 fprintf(ofP, "%.4f\n", (double) params[pind].value.f);
674 break;
675 case PSTR:
676 case POSTR:
677 fprintf(ofP, "%s\n", params[pind].value.s);
678 break;
679 default:
680 pm_error("Invalid type %d for parameter %s",
681 params[pind].type, params[pind].name);
682 }
683 }
684 fputc ('\n', ofP);
685 }
686
687
688