2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
8 * option.c - command line option processing
11 * \<>) "Process command line options as defined in <option.h>.
12 * Return the number of argv[] elements used up by options,
13 * or -1 if an invalid option flag was given or an argument
14 * was supplied for an option that does not require one."
16 * 11/04/02 (seiwald) - const-ing for string literals
22 int getoptions (int argc
, char **argv
, const char *opts
, option
*optv
, char** targets
) {
28 memset((char *)optv
, '\0', sizeof(*optv
) * N_OPTS
);
30 for (i
= 0; i
< argc
; i
++) {
31 if (argv
[i
][0] == '-') {
33 printf("too many options (%d max)\n", N_OPTS
);
36 for (arg
= &argv
[i
][1]; *arg
; arg
++) {
37 for (f
= opts
; *f
; f
++) {
41 printf("Invalid option: -%c\n", *arg
);
48 optv
++->val
= &arg
[1];
50 } else if (++i
< argc
) {
51 optv
++->val
= argv
[i
];
54 printf("option: -%c needs argument\n", *f
);
59 /* something like VARNAME=.... is treated as an implicit '-s' flag */
60 if (argv
[i
][0] != '=' && strchr(argv
[i
],'=')) {
62 printf("too many options (%d max)\n", N_OPTS
);
66 optv
++->val
= argv
[i
];
69 printf("too many targets (%d max)\n", N_TARGETS
);
72 targets
[n
++] = argv
[i
];
81 * Name: getoptval() - find an option given its character
83 const char *getoptval (option
*optv
, char opt
, int subopt
) {
86 for (i
= 0; i
< N_OPTS
; i
++, optv
++) {
87 if (optv
->flag
== opt
&& !subopt
--) return optv
->val
;