17 extern int optind
; /* undocumented variable used to reset getopt() */
19 #include "trueprint.h" /* needed for boolean type */
20 #include "utils.h" /* needed for strdup */
21 #include "debug.h" /* needed for dm macro */
25 /******************************************************************************
29 /* If there are more than MAX_OPTIONS options then this must be increased */
30 #define MAX_OPTIONS 256
32 /* This must specify a bit outside the range of MAX_OPTIONS */
33 #define OPT_FLAG 0x1000
35 #define SHORT_OPTION(opt) (short_options[('A'<=opt&&opt<='Z')?(opt-'A')*2:\
36 ('a'<=opt&&opt<='z')?(opt-'a')*2+1:\
37 ('0'<=opt&&opt<='9')?opt-'0'+52:\
40 #define PRINTUSAGE printf(gettext(" Usage: trueprint <options> <filename>\n Use -H for details\n"))
42 /******************************************************************************
58 typedef struct option_type
{
68 void (*handler
)(const char *p
, const char *s
);
69 void (*set_default
)(void);
74 void (*handler
)(const char *p
, const char *s
, char *value
);
81 boolean default_value
;
82 void (*handler
)(const char *p
, const char *s
, boolean value
);
83 void (*set_default
)(boolean value
);
84 char *true_help_string
;
85 char *false_help_string
;
92 void (*handler
)(const char *p
, const char *s
, char value
, char *valid_set
);
93 void (*set_default
)(char value
);
100 char choice1
; /* Also default */
102 void (*handler
)(const char *p
, const char *s
, char value
);
103 void (*set_default
)(char value
);
104 char *choice1_help_string
;
105 char *choice2_help_string
;
111 char *special_string
;
115 void (*handler
)(const char *p
, const char *s
, short value
, short min
, short max
);
116 void (*set_default
)(short value
);
118 char *special_help_string
;
124 char *special_string
;
128 void (*handler
)(const char *p
, const char *s
, int value
, int min
, int max
);
129 void (*set_default
)(int value
);
131 char *special_help_string
;
137 void (*handler
)(const char *p
, const char *s
, char *value
);
138 void (*set_default
)(char *value
);
143 char *not_set_string
;
146 boolean default_value
;
149 void (*handler
)(const char *p
, const char *s
, boolean value
, char *true_value
, char *false_value
);
150 void (*set_default
)(boolean value
, char *string_value
);
151 char *set_help_string
;
152 char *not_set_help_string
;
157 /******************************************************************************
160 static void set_option(int option
, char *prefix
, const char *option_name
, char *value
);
161 static void set_option_default(int option
);
163 /******************************************************************************
164 * Constant variables and data structures
167 static int short_options
[62];
169 static struct option_type option_list
[MAX_OPTIONS
+1];
170 static struct option long_options
[MAX_OPTIONS
+1];
171 static int next_option
;
172 static int next_long_option
;
173 static int this_option
;
175 /******************************************************************************
183 next_long_option
= 0;
186 /******************************************************************************
188 * handle_string_options
190 * This function takes options as a single string, parses them into
191 * a char[][], and calls handle_options.
194 handle_string_options(char *options
)
198 char arguments
[100][100];
200 int options_index
= -1;
202 boolean quoted
= FALSE
;
203 boolean ended
= FALSE
;
205 if (options
== (char *)0) return;
206 if (strlen(options
) == 0) return;
208 while (ended
== FALSE
)
210 switch (options
[++options_index
])
216 if (argv_index
== 0) opt_argc
+= 1;
217 arguments
[opt_argc
][argv_index
++] = options
[options_index
];
222 arguments
[opt_argc
][argv_index
] = '\0';
230 arguments
[opt_argc
][argv_index
] = '\0';
237 if (quoted
) quoted
= FALSE
;
243 if (argv_index
== 0) opt_argc
+= 1;
244 arguments
[opt_argc
][argv_index
++] = options
[options_index
];
250 for (argv_index
=0; argv_index
< opt_argc
; argv_index
++)
251 opt_argv
[argv_index
+1] = arguments
[argv_index
];
253 opt_argv
[0] = opt_argv
[1];
256 handle_options(opt_argc
, opt_argv
);
260 /******************************************************************************
264 * This function handles the options passed to the program, and also
265 * the defaults for each language. Since the options passed to the
266 * program gake priority and are also found first, this function
267 * will only assign a value to a flag if the flag has not previously
271 handle_options(int argc
, char **argv
)
276 int short_option_index
= 0;
277 int long_option_index
;
278 char short_option_list
[125]; /* 125 = (26+26+10)*2 + 1 */
281 * Since this function will be called twice, we need to initialise
282 * getopt_long() variables to make sure that it behaves properly during
288 * Set up the string of single-letter options and set the last long_option
289 * to all zeros - this will be done multiple times each time trueprint
290 * is invoked, but it's quick so it shouldn't hurt.
292 for (option_index
= 0; option_index
< next_long_option
; option_index
++)
294 if (option_list
[option_index
].letter
)
296 short_option_list
[short_option_index
++] = option_list
[option_index
].letter
;
297 if (option_list
[option_index
].need_string
)
298 short_option_list
[short_option_index
++] = ':';
302 long_options
[next_long_option
].name
= 0;
303 long_options
[next_long_option
].has_arg
= 0;
304 long_options
[next_long_option
].flag
= 0;
305 long_options
[next_long_option
].val
= 0;
308 * Loop through the options. This call will set this_option
309 * to the option number if it is looking at a long option.
312 getopt_long((unsigned int)argc
, argv
,
313 short_option_list
, long_options
, &long_option_index
)
318 fprintf(stderr
, gettext(CMD_NAME
": failed to parse options\n"));
321 else if (option
!= 0)
324 option_name
= xmalloc(2);
325 option_name
[0] = option
;
326 option_name
[1] = '\0';
327 option_index
= SHORT_OPTION(option
);
328 set_option(option_index
,"-",option_name
,optarg
);
332 set_option(this_option
, "--", long_options
[long_option_index
].name
,optarg
);
338 /******************************************************************************
339 * Option declaration functions
342 /******************************************************************************
346 * Calls handler if used.
347 * Uses either single-letter option or string long option or both
349 void noparm_option(char *c
, char *s
,
351 void (*handler
)(const char *p
, const char *s
),
352 void (*set_default
)(void),
356 int option_index
= next_option
++;
360 int long_option_index
= next_long_option
++;
361 long_options
[long_option_index
].name
= s
;
362 long_options
[long_option_index
].has_arg
= 0;
363 long_options
[long_option_index
].flag
= &this_option
;
364 long_options
[long_option_index
].val
= option_index
;
368 * This checks for an internal error, so it is OK to
369 * risk an out-of-bounds access
370 * before checking for the bounds limit.
372 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
378 if (default_opt
&& set_default
== NULL
)
383 option_list
[option_index
].letter
= *c
;
384 SHORT_OPTION(*c
) = option_index
;
387 option_list
[option_index
].letter
= 0;
389 option_list
[option_index
].type
= NOPARM
;
390 option_list
[option_index
].class = class;
391 option_list
[option_index
].need_string
= FALSE
;
392 option_list
[option_index
].set
= FALSE
;
393 option_list
[option_index
].t
.onoparm
.string
= s
;
394 option_list
[option_index
].t
.onoparm
.default_opt
= default_opt
;
395 option_list
[option_index
].t
.onoparm
.handler
= handler
;
396 option_list
[option_index
].t
.onoparm
.set_default
= set_default
;
397 option_list
[option_index
].t
.onoparm
.help_string
= help_string
;
400 /******************************************************************************
401 * optional_string_option
405 * Note that only the long option has the optional string.
406 * Calls handler if used.
407 * Uses either single-letter option or string long option or both
409 void optional_string_option(char *c
, char *s
,
410 void (*handler
)(const char *p
, const char *s
, char *value
),
414 int option_index
= next_option
++;
418 int long_option_index
= next_long_option
++;
419 long_options
[long_option_index
].name
= s
;
420 long_options
[long_option_index
].has_arg
= 2;
421 long_options
[long_option_index
].flag
= &this_option
;
422 long_options
[long_option_index
].val
= option_index
;
426 * This checks for an internal error, so it is OK to
427 * risk an out-of-bounds access
428 * before checking for the bounds limit.
430 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
438 option_list
[option_index
].letter
= *c
;
439 SHORT_OPTION(*c
) = option_index
;
442 option_list
[option_index
].letter
= 0;
444 option_list
[option_index
].type
= OPTIONAL
;
445 option_list
[option_index
].class = class;
446 option_list
[option_index
].need_string
= FALSE
;
447 option_list
[option_index
].set
= FALSE
;
448 option_list
[option_index
].t
.ooptional
.string
= s
;
449 option_list
[option_index
].t
.ooptional
.handler
= handler
;
450 option_list
[option_index
].t
.ooptional
.help_string
= help_string
;
453 /******************************************************************************
455 * -c = set option to TRUE
456 * --c = set option to FALSE
457 * --s1 = set option to TRUE
458 * --s2 = set option to FALSE
460 void boolean_option(char *c
, char *s1
, char *s2
,
461 boolean default_value
,
463 void (*handler
)(const char *p
, const char *s
, boolean value
),
464 void (*set_default
)(boolean value
),
466 char *true_help_string
,
467 char *false_help_string
)
469 int option_index
= next_option
++;
473 int long_option_index
= next_long_option
++;
474 option_list
[option_index
].letter
= *c
;
475 SHORT_OPTION(*c
) = option_index
;
476 long_options
[long_option_index
].name
= c
;
477 long_options
[long_option_index
].has_arg
= 0;
478 long_options
[long_option_index
].flag
= &this_option
;
479 long_options
[long_option_index
].val
= option_index
| OPT_FLAG
;
482 option_list
[option_index
].letter
= 0;
486 int long_option_index
= next_long_option
++;
487 long_options
[long_option_index
].name
= s1
;
488 long_options
[long_option_index
].has_arg
= 0;
489 long_options
[long_option_index
].flag
= &this_option
;
490 long_options
[long_option_index
].val
= option_index
;
494 int long_option_index
= next_long_option
++;
495 long_options
[long_option_index
].name
= s2
;
496 long_options
[long_option_index
].has_arg
= 0;
497 long_options
[long_option_index
].flag
= &this_option
;
498 long_options
[long_option_index
].val
= option_index
| OPT_FLAG
;
501 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
503 if ((var
== NULL
) && (handler
== NULL
))
505 if ((var
!= NULL
) && (handler
!= NULL
))
507 if ((handler
== NULL
) != (set_default
== NULL
))
510 option_list
[option_index
].type
= BOOLEAN
;
511 option_list
[option_index
].class = class;
512 option_list
[option_index
].need_string
= FALSE
;
513 option_list
[option_index
].set
= FALSE
;
514 option_list
[option_index
].t
.obool
.true_string
= s1
;
515 option_list
[option_index
].t
.obool
.false_string
= s2
;
516 option_list
[option_index
].t
.obool
.var
= var
;
517 option_list
[option_index
].t
.obool
.default_value
= default_value
;
518 option_list
[option_index
].t
.obool
.handler
= handler
;
519 option_list
[option_index
].t
.obool
.set_default
= set_default
;
520 option_list
[option_index
].t
.obool
.true_help_string
= true_help_string
;
521 option_list
[option_index
].t
.obool
.false_help_string
= false_help_string
;
524 /******************************************************************************
530 void choice_option(char *c
, char *s1
, char *s2
,
531 char choice1
, char choice2
,
533 void (*handler
)(const char *p
, const char *s
, char value
),
534 void (*set_default
)(char value
),
536 char *choice1_help_string
,
537 char *choice2_help_string
)
539 int option_index
= next_option
++;
543 option_list
[option_index
].letter
= *c
;
544 SHORT_OPTION(*c
) = option_index
;
547 option_list
[option_index
].letter
= 0;
551 int long_option_index
= next_long_option
++;
552 long_options
[long_option_index
].name
= s1
;
553 long_options
[long_option_index
].has_arg
= 0;
554 long_options
[long_option_index
].flag
= &this_option
;
555 long_options
[long_option_index
].val
= option_index
;
560 int long_option_index
= next_long_option
++;
561 long_options
[long_option_index
].name
= s2
;
562 long_options
[long_option_index
].has_arg
= 0;
563 long_options
[long_option_index
].flag
= &this_option
;
564 long_options
[long_option_index
].val
= option_index
| OPT_FLAG
;
567 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
569 if ((var
== NULL
) && (handler
== NULL
))
571 if ((var
!= NULL
) && (handler
!= NULL
))
573 if ((handler
== NULL
) != (set_default
== NULL
))
576 option_list
[option_index
].type
= CHOICE
;
577 option_list
[option_index
].class = class;
578 option_list
[option_index
].need_string
= TRUE
;
579 option_list
[option_index
].set
= FALSE
;
580 option_list
[option_index
].t
.ochoice
.var
= var
;
581 option_list
[option_index
].t
.ochoice
.choice1_string
= s1
;
582 option_list
[option_index
].t
.ochoice
.choice2_string
= s2
;
583 option_list
[option_index
].t
.ochoice
.choice1
= choice1
;
584 option_list
[option_index
].t
.ochoice
.choice2
= choice2
;
585 option_list
[option_index
].t
.ochoice
.handler
= handler
;
586 option_list
[option_index
].t
.ochoice
.set_default
= set_default
;
587 option_list
[option_index
].t
.ochoice
.choice1_help_string
= choice1_help_string
;
588 option_list
[option_index
].t
.ochoice
.choice2_help_string
= choice2_help_string
;
591 /******************************************************************************
596 void char_option(char *c
, char *s
,
600 void (*handler
)(const char *p
, const char *s
, char value
, char *var
),
601 void (*set_default
)(char value
),
605 int option_index
= next_option
++;
609 option_list
[option_index
].letter
= *c
;
610 SHORT_OPTION(*c
) = option_index
;
613 option_list
[option_index
].letter
= 0;
617 int long_option_index
= next_long_option
++;
618 long_options
[long_option_index
].name
= s
;
619 long_options
[long_option_index
].has_arg
= 1;
620 long_options
[long_option_index
].flag
= &this_option
;
621 long_options
[long_option_index
].val
= option_index
;
624 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
626 if ((var
== NULL
) && (handler
== NULL
))
628 if ((var
!= NULL
) && (handler
!= NULL
))
630 if ((handler
== NULL
) != (set_default
== NULL
))
633 option_list
[option_index
].type
= CHAR
;
634 option_list
[option_index
].class = class;
635 option_list
[option_index
].need_string
= TRUE
;
636 option_list
[option_index
].set
= FALSE
;
637 option_list
[option_index
].t
.ochar
.string
= s
;
638 option_list
[option_index
].t
.ochar
.var
= var
;
639 option_list
[option_index
].t
.ochar
.default_value
= default_value
;
640 option_list
[option_index
].t
.ochar
.valid_set
= valid_set
;
641 option_list
[option_index
].t
.ochar
.handler
= handler
;
642 option_list
[option_index
].t
.ochar
.set_default
= set_default
;
643 option_list
[option_index
].t
.ochar
.help_string
= help_string
;
646 /******************************************************************************
651 void short_option(char *c
, char *s
, short default_value
,
652 char *special_string
, short special_value
,
653 short min
, short max
,
655 void (*handler
)(const char *p
, const char *s
, short value
, short min
, short max
),
656 void (*set_default
)(short value
),
659 char *special_help_string
)
661 int option_index
= next_option
++;
665 option_list
[option_index
].letter
= *c
;
666 SHORT_OPTION(*c
) = option_index
;
669 option_list
[option_index
].letter
= 0;
673 int long_option_index
= next_long_option
++;
674 long_options
[long_option_index
].name
= s
;
675 long_options
[long_option_index
].has_arg
= 1;
676 long_options
[long_option_index
].flag
= &this_option
;
677 long_options
[long_option_index
].val
= option_index
;
682 int long_option_index
= next_long_option
++;
683 long_options
[long_option_index
].name
= special_string
;
684 long_options
[long_option_index
].has_arg
= 0;
685 long_options
[long_option_index
].flag
= &this_option
;
686 long_options
[long_option_index
].val
= option_index
| OPT_FLAG
;
689 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
691 if ((var
== NULL
) && (handler
== NULL
))
693 if ((var
!= NULL
) && (handler
!= NULL
))
695 if ((handler
== NULL
) != (set_default
== NULL
))
698 option_list
[option_index
].type
= SHORT
;
699 option_list
[option_index
].class = class;
700 option_list
[option_index
].need_string
= TRUE
;
701 option_list
[option_index
].set
= FALSE
;
702 option_list
[option_index
].t
.oshrt
.string
= s
;
703 option_list
[option_index
].t
.oshrt
.var
= var
;
704 option_list
[option_index
].t
.oshrt
.min
= min
;
705 option_list
[option_index
].t
.oshrt
.max
= max
;
706 option_list
[option_index
].t
.oshrt
.default_value
= default_value
;
707 option_list
[option_index
].t
.oshrt
.handler
= handler
;
708 option_list
[option_index
].t
.oshrt
.set_default
= set_default
;
709 option_list
[option_index
].t
.oshrt
.help_string
= help_string
;
710 option_list
[option_index
].t
.oshrt
.special_string
= special_string
;
711 option_list
[option_index
].t
.oshrt
.special_value
= special_value
;
712 option_list
[option_index
].t
.oshrt
.special_help_string
= special_help_string
;
715 /******************************************************************************
721 void int_option(char *c
, char *s
, int default_value
,
722 char *special_string
, int special_value
,
725 void (*handler
)(const char *p
, const char *s
, int value
, int min
, int max
),
726 void (*set_default
)(int value
),
729 char *special_help_string
)
731 int option_index
= next_option
++;
735 option_list
[option_index
].letter
= *c
;
736 SHORT_OPTION(*c
) = option_index
;
739 option_list
[option_index
].letter
= 0;
743 int long_option_index
= next_long_option
++;
744 long_options
[long_option_index
].name
= s
;
745 long_options
[long_option_index
].has_arg
= 1;
746 long_options
[long_option_index
].flag
= &this_option
;
747 long_options
[long_option_index
].val
= option_index
;
752 int long_option_index
= next_long_option
++;
753 long_options
[long_option_index
].name
= special_string
;
754 long_options
[long_option_index
].has_arg
= 0;
755 long_options
[long_option_index
].flag
= &this_option
;
756 long_options
[long_option_index
].val
= option_index
| OPT_FLAG
;
759 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
761 if ((var
== NULL
) && (handler
== NULL
))
763 if ((var
!= NULL
) && (handler
!= NULL
))
765 if ((handler
== NULL
) != (set_default
== NULL
))
768 option_list
[option_index
].type
= INT
;
769 option_list
[option_index
].class = class;
770 option_list
[option_index
].need_string
= TRUE
;
771 option_list
[option_index
].set
= FALSE
;
772 option_list
[option_index
].t
.oint
.string
= s
;
773 option_list
[option_index
].t
.oint
.var
= var
;
774 option_list
[option_index
].t
.oint
.min
= min
;
775 option_list
[option_index
].t
.oint
.max
= max
;
776 option_list
[option_index
].t
.oint
.default_value
= default_value
;
777 option_list
[option_index
].t
.oint
.handler
= handler
;
778 option_list
[option_index
].t
.oint
.set_default
= set_default
;
779 option_list
[option_index
].t
.oint
.help_string
= help_string
;
780 option_list
[option_index
].t
.oint
.special_string
= special_string
;
781 option_list
[option_index
].t
.oint
.special_value
= special_value
;
782 option_list
[option_index
].t
.oint
.special_help_string
= special_help_string
;
785 /******************************************************************************
790 void string_option(char *c
, char *s
,
793 void (*handler
)(const char *p
, const char *s
, char *value
),
794 void (*set_default
)(char *value
),
798 int option_index
= next_option
++;
802 option_list
[option_index
].letter
= *c
;
803 SHORT_OPTION(*c
) = option_index
;
806 option_list
[option_index
].letter
= 0;
810 int long_option_index
= next_long_option
++;
811 long_options
[long_option_index
].name
= s
;
812 long_options
[long_option_index
].has_arg
= 1;
813 long_options
[long_option_index
].flag
= &this_option
;
814 long_options
[long_option_index
].val
= option_index
;
817 if ((var
== NULL
) && (handler
== NULL
))
819 if ((var
!= NULL
) && (handler
!= NULL
))
821 if ((handler
== NULL
) != (set_default
== NULL
))
824 option_list
[option_index
].type
= STRING
;
825 option_list
[option_index
].class = class;
826 option_list
[option_index
].need_string
= TRUE
;
827 option_list
[option_index
].set
= FALSE
;
828 option_list
[option_index
].t
.ostrng
.string
= s
;
829 option_list
[option_index
].t
.ostrng
.var
= var
;
830 option_list
[option_index
].t
.ostrng
.default_value
= default_value
;
831 option_list
[option_index
].t
.ostrng
.handler
= handler
;
832 option_list
[option_index
].t
.ostrng
.set_default
= set_default
;
833 option_list
[option_index
].t
.ostrng
.help_string
= help_string
;
836 /******************************************************************************
843 void flag_string_option(char *c
, char *s1
, char *s2
,
844 boolean default_value
,
845 char *true_value
, char *false_value
,
847 void (*handler
)(const char *p
, const char *s
, boolean value
, char *true_value
, char *false_value
),
848 void (*set_default
)(boolean value
, char *string_value
),
850 char *set_help_string
,
851 char *not_set_help_string
)
853 int option_index
= next_option
++;
857 option_list
[option_index
].letter
= *c
;
858 SHORT_OPTION(*c
) = option_index
;
861 option_list
[option_index
].letter
= 0;
865 int long_option_index
= next_long_option
++;
866 long_options
[long_option_index
].name
= s1
;
867 long_options
[long_option_index
].has_arg
= 1;
868 long_options
[long_option_index
].flag
= &this_option
;
869 long_options
[long_option_index
].val
= option_index
;
874 int long_option_index
= next_long_option
++;
875 long_options
[long_option_index
].name
= s2
;
876 long_options
[long_option_index
].has_arg
= 0;
877 long_options
[long_option_index
].flag
= &this_option
;
878 long_options
[long_option_index
].val
= option_index
| OPT_FLAG
;
881 if ((next_option
> MAX_OPTIONS
) || (next_long_option
> MAX_OPTIONS
))
883 if ((var
== NULL
) && (handler
== NULL
))
885 if ((var
!= NULL
) && (handler
!= NULL
))
887 if ((handler
== NULL
) != (set_default
== NULL
))
890 option_list
[option_index
].type
= FLAG_STRING
;
891 option_list
[option_index
].class = class;
892 option_list
[option_index
].need_string
= FALSE
;
893 option_list
[option_index
].set
= FALSE
;
894 option_list
[option_index
].t
.oflg
.var
= var
;
895 option_list
[option_index
].t
.oflg
.set_string
= s1
;
896 option_list
[option_index
].t
.oflg
.not_set_string
= s2
;
897 option_list
[option_index
].t
.oflg
.default_value
= default_value
;
898 option_list
[option_index
].t
.oflg
.true_value
= true_value
;
899 option_list
[option_index
].t
.oflg
.false_value
= false_value
;
900 option_list
[option_index
].t
.oflg
.handler
= handler
;
901 option_list
[option_index
].t
.oflg
.set_default
= set_default
;
902 option_list
[option_index
].t
.oflg
.set_help_string
= set_help_string
;
903 option_list
[option_index
].t
.oflg
.not_set_help_string
= not_set_help_string
;
906 /******************************************************************************
907 * set_option - sets the option
910 void set_option(int index
, char *prefix
, const char *option_name
, char *value
) {
911 boolean flag_set
= ((index
& OPT_FLAG
) != 0);
912 struct option_type
*op
;
915 op
= &option_list
[index
-OPT_FLAG
];
917 op
= &option_list
[index
];
920 dm('o',3,"Trying to set option %s%s to %s\n", prefix
, option_name
, value
);
922 dm('o',3,"Trying to set option %s%s\n", prefix
, option_name
);
926 dm('o',3,"Option %s%s already set\n", prefix
, option_name
);
934 (*(op
->t
.onoparm
.handler
))(prefix
, option_name
);
938 (*(op
->t
.ooptional
.handler
))(prefix
, option_name
, value
);
944 *(op
->t
.obool
.var
) = (!flag_set
);
948 (*(op
->t
.obool
.handler
))(prefix
, option_name
, !flag_set
);
953 if (strlen(value
) != 1)
955 fprintf(stderr
, gettext(CMD_NAME
": must have one character parameter for %s%s flag, but got '%s'\n"),prefix
,option_name
,value
);
958 /* TODO: Need to check that c is in valid_set */
961 *(op
->t
.ochar
.var
) = *value
;
965 (*(op
->t
.ochar
.handler
))(prefix
, option_name
, *value
, op
->t
.ochar
.valid_set
);
970 if (strlen(prefix
) == 1)
972 /* This is a short option */
973 if (strlen(value
) != 1)
975 fprintf(stderr
, gettext(CMD_NAME
": must have one character parameter for %s%s flag, but got '%s'\n"),prefix
,option_name
,value
);
979 if ((*value
!= op
->t
.ochoice
.choice1
) && (*value
!= op
->t
.ochoice
.choice2
))
981 fprintf(stderr
, gettext(CMD_NAME
": option %s%s can only take %c or %c, not %c\n"),prefix
,option_name
,op
->t
.ochoice
.choice1
,op
->t
.ochoice
.choice2
,*value
);
985 if (op
->t
.ochoice
.var
)
987 *(op
->t
.ochoice
.var
) = *value
;
991 (*(op
->t
.ochoice
.handler
))(prefix
, option_name
, *value
);
996 /* This is a long option */
998 if (strcmp(option_name
,op
->t
.ochoice
.choice1_string
) == 0)
1000 /* First option selected */
1001 chosen_value
= op
->t
.ochoice
.choice1
;
1005 /* Second option selected */
1006 chosen_value
= op
->t
.ochoice
.choice2
;
1009 if (op
->t
.ochoice
.var
)
1011 *(op
->t
.ochoice
.var
) = chosen_value
;
1015 (*(op
->t
.ochoice
.handler
))(prefix
, option_name
, chosen_value
);
1024 if (op
->t
.oshrt
.var
)
1026 *(op
->t
.oshrt
.var
) = op
->t
.oshrt
.special_value
;
1030 (*(op
->t
.oshrt
.handler
))(prefix
, option_name
, op
->t
.oshrt
.special_value
, op
->t
.oshrt
.min
, op
->t
.oshrt
.max
);
1035 int intvalue
= atoi(value
);
1036 if ((intvalue
> op
->t
.oshrt
.max
) || (intvalue
< op
->t
.oshrt
.min
))
1038 fprintf(stderr
, gettext(CMD_NAME
": option %s%s not between %d and %d\n"),prefix
,option_name
,op
->t
.oshrt
.min
,op
->t
.oshrt
.max
);
1042 if (op
->t
.oshrt
.var
)
1044 *(op
->t
.oshrt
.var
) = (short)intvalue
;
1048 (*(op
->t
.oshrt
.handler
))(prefix
, option_name
, (short)intvalue
, op
->t
.oshrt
.min
, op
->t
.oshrt
.max
);
1058 *(op
->t
.oint
.var
) = op
->t
.oint
.special_value
;
1062 (*(op
->t
.oint
.handler
))(prefix
, option_name
, op
->t
.oint
.special_value
, op
->t
.oint
.min
, op
->t
.oint
.max
);
1067 int intvalue
= atoi(value
);
1068 if ((intvalue
> op
->t
.oint
.max
) || (intvalue
< op
->t
.oint
.min
))
1070 fprintf(stderr
, gettext(CMD_NAME
": option %s%s not between %d and %d\n"),prefix
,option_name
,op
->t
.oint
.min
,op
->t
.oint
.max
);
1075 *(op
->t
.oint
.var
) = intvalue
;
1079 (*(op
->t
.oint
.handler
))(prefix
, option_name
, intvalue
, op
->t
.oint
.min
, op
->t
.oint
.max
);
1085 if (op
->t
.ostrng
.var
)
1087 *(op
->t
.ostrng
.var
) = strdup(value
);
1091 (*(op
->t
.ostrng
.handler
))(prefix
, option_name
, value
);
1100 if (op
->t
.oflg
.true_value
)
1102 *(op
->t
.oflg
.var
) = strdup(op
->t
.oflg
.true_value
);
1106 *(op
->t
.oflg
.var
) = NULL
;
1111 if (op
->t
.oflg
.false_value
)
1113 *(op
->t
.oflg
.var
) = strdup(op
->t
.oflg
.false_value
);
1117 *(op
->t
.oflg
.var
) = NULL
;
1123 (*(op
->t
.oflg
.handler
))(prefix
, option_name
, !flag_set
, op
->t
.oflg
.true_value
, op
->t
.oflg
.false_value
);
1133 dm('o',3,"Succeeded - %s%s has not been set before\n",prefix
,option_name
);
1136 /******************************************************************************
1138 * set_option_default
1140 void set_option_default(int index
)
1142 option_type
*op
= &option_list
[index
];
1144 dm('o',3,"Trying to set option %d to default value\n", index
);
1148 dm('o',3,"Option %d already set\n", index
);
1156 if (op
->t
.onoparm
.default_opt
)
1157 (*(op
->t
.onoparm
.set_default
))();
1164 if (op
->t
.obool
.var
)
1166 *(op
->t
.obool
.var
) = op
->t
.obool
.default_value
;
1170 (*(op
->t
.obool
.set_default
))(op
->t
.obool
.default_value
);
1175 if (op
->t
.ochar
.var
)
1177 *(op
->t
.ochar
.var
) = op
->t
.ochar
.default_value
;
1181 (*(op
->t
.ochar
.set_default
))(op
->t
.ochar
.default_value
);
1186 if (op
->t
.ochoice
.var
)
1188 *(op
->t
.ochoice
.var
) = op
->t
.ochoice
.choice1
;
1192 (*(op
->t
.ochoice
.set_default
))(op
->t
.ochoice
.choice1
);
1197 if (op
->t
.oshrt
.var
)
1199 *(op
->t
.oshrt
.var
) = op
->t
.oshrt
.default_value
;
1203 (*(op
->t
.oshrt
.set_default
))(op
->t
.oshrt
.default_value
);
1210 *(op
->t
.oint
.var
) = op
->t
.oint
.default_value
;
1214 (*(op
->t
.oint
.set_default
))(op
->t
.oint
.default_value
);
1219 if (op
->t
.ostrng
.var
)
1221 if (op
->t
.ostrng
.default_value
)
1223 *(op
->t
.ostrng
.var
) = strdup(op
->t
.ostrng
.default_value
);
1227 *(op
->t
.ostrng
.var
) = NULL
;
1232 if (op
->t
.ostrng
.default_value
)
1234 (*(op
->t
.ostrng
.set_default
))(strdup(op
->t
.ostrng
.default_value
));
1238 (*(op
->t
.ostrng
.set_default
))(NULL
);
1246 if (op
->t
.oflg
.default_value
)
1248 if (op
->t
.oflg
.true_value
)
1250 *(op
->t
.oflg
.var
) = strdup(op
->t
.oflg
.true_value
);
1254 *(op
->t
.oflg
.var
) = NULL
;
1259 if (op
->t
.oflg
.false_value
)
1261 *(op
->t
.oflg
.var
) = strdup(op
->t
.oflg
.false_value
);
1265 *(op
->t
.oflg
.var
) = NULL
;
1271 if (op
->t
.oflg
.default_value
)
1273 if (op
->t
.oflg
.true_value
)
1275 (*(op
->t
.oflg
.set_default
))(1, strdup(op
->t
.oflg
.true_value
));
1279 (*(op
->t
.oflg
.set_default
))(1, NULL
);
1284 if (op
->t
.oflg
.false_value
)
1286 (*(op
->t
.oflg
.set_default
))(0, strdup(op
->t
.oflg
.false_value
));
1290 (*(op
->t
.oflg
.set_default
))(0, NULL
);
1300 dm('o',3,"Succeeded - option %d has not been set before\n",index
);
1303 /******************************************************************************
1307 void print_usage_msgs(option_class
class)
1313 printf(gettext("Miscellaneous options:\n"));
1315 case OPT_PAGE_FURNITURE
:
1316 printf(gettext("Page furniture options:\n"));
1318 case OPT_TEXT_FORMAT
:
1319 printf(gettext("Text formatting options:\n"));
1322 printf(gettext("Print selection options:\n"));
1324 case OPT_PAGE_FORMAT
:
1325 printf(gettext("Page format options:\n"));
1328 printf(gettext("Output options:\n"));
1332 for (option_index
=0; option_index
< next_option
; option_index
++)
1334 option_type
*op
= &option_list
[option_index
];
1335 if (class == op
->class)
1341 printf("-%c ",op
->letter
);
1342 if (op
->t
.onoparm
.string
)
1343 printf("--%s",op
->t
.onoparm
.string
);
1344 printf("\n %s\n",gettext(op
->t
.onoparm
.help_string
));
1349 printf("-%c ",op
->letter
);
1350 if (op
->t
.ooptional
.string
)
1351 printf("--%s[=<string>]",op
->t
.ooptional
.string
);
1352 printf("\n %s\n",gettext(op
->t
.ooptional
.help_string
));
1357 printf("-%c ",op
->letter
);
1358 if (op
->t
.obool
.true_string
)
1359 printf("--%s",op
->t
.obool
.true_string
);
1360 printf("\n %s\n",gettext(op
->t
.obool
.true_help_string
));
1362 printf("--%c ",op
->letter
);
1363 if (op
->t
.obool
.false_string
)
1364 printf("--%s",op
->t
.obool
.false_string
);
1365 printf("\n %s\n",gettext(op
->t
.obool
.false_help_string
));
1370 printf("-%c <char> ",op
->letter
);
1371 if (op
->t
.ochar
.string
)
1372 printf("--%s <char>",op
->t
.ochar
.string
);
1373 printf("\n %s\n",gettext(op
->t
.ochar
.help_string
));
1378 printf("-%c %c ",op
->letter
,op
->t
.ochoice
.choice1
);
1379 if (op
->t
.ochoice
.choice1_string
)
1380 printf("--%s",op
->t
.ochoice
.choice1_string
);
1381 printf("\n %s\n",gettext(op
->t
.ochoice
.choice1_help_string
));
1383 printf("-%c %c ",op
->letter
,op
->t
.ochoice
.choice2
);
1384 if (op
->t
.ochoice
.choice2_string
)
1385 printf("--%s",op
->t
.ochoice
.choice2_string
);
1386 printf("\n %s\n",gettext(op
->t
.ochoice
.choice2_help_string
));
1391 printf("-%c <number> ",op
->letter
);
1392 if (op
->t
.oshrt
.string
)
1393 printf("--%s=<number>",op
->t
.oshrt
.string
);
1394 printf("\n %s\n",gettext(op
->t
.oshrt
.help_string
));
1395 if (op
->t
.oshrt
.special_string
)
1396 printf("--%s\n %s\n", op
->t
.oshrt
.special_string
, gettext(op
->t
.oshrt
.special_help_string
));
1401 printf("-%c <number> ",op
->letter
);
1402 if (op
->t
.oint
.string
)
1403 printf("--%s=<number>",op
->t
.oint
.string
);
1404 printf("\n %s\n",gettext(op
->t
.oint
.help_string
));
1405 if (op
->t
.oint
.special_string
)
1406 printf("--%s\n %s\n", op
->t
.oint
.special_string
, gettext(op
->t
.oint
.special_help_string
));
1411 printf("-%c <string> ",op
->letter
);
1412 if (op
->t
.ostrng
.string
)
1413 printf("--%s=<string>",op
->t
.ostrng
.string
);
1414 printf("\n %s\n",gettext(op
->t
.ostrng
.help_string
));
1419 printf("-%c <string> ",op
->letter
);
1420 if (op
->t
.oflg
.set_string
)
1421 printf("--%s=<string>",op
->t
.oflg
.set_string
);
1422 printf("\n %s\n",gettext(op
->t
.oflg
.set_help_string
));
1424 printf("--%c ",op
->letter
);
1425 if (op
->t
.oflg
.set_string
)
1426 printf("--%s",op
->t
.oflg
.not_set_string
);
1427 printf("\n %s\n",gettext(op
->t
.oflg
.not_set_help_string
));
1433 /******************************************************************************
1435 * set_option_defaults
1437 void set_option_defaults(void)
1441 for (option_index
=0; option_index
< next_option
; option_index
++)
1443 set_option_default(option_index
);