2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
7 * option.c - command line option processing
10 * \<>) "Process command line options as defined in <option.h>.
11 * Return the number of argv[] elements used up by options,
12 * or -1 if an invalid option flag was given or an argument
13 * was supplied for an option that does not require one."
15 * 11/04/02 (seiwald) - const-ing for string literals
21 int getoptions (int argc
, char **argv
, const char *opts
, option
*optv
, char** targets
) {
27 memset((char *)optv
, '\0', sizeof(*optv
)*N_OPTS
);
29 for (i
= 0; i
< argc
; ++i
) {
30 if (argv
[i
][0] == '-') {
31 if (!optc
--) { printf("too many options (%d max)\n", N_OPTS
); return -1; }
32 for (arg
= &argv
[i
][1]; *arg
; ++arg
) {
33 for (f
= opts
; *f
; ++f
) if (*f
== *arg
) break;
34 if (!*f
) { printf("invalid option: -%c\n", *arg
); return -1; }
36 if (f
[1] != ':') { optv
++->val
= "true"; }
37 else if (arg
[1]) { optv
++->val
= &arg
[1]; break; }
38 else if (++i
< argc
) { optv
++->val
= argv
[i
]; break; }
39 else { printf("option: -%c needs argument\n", *f
); return -1; }
42 /* something like VARNAME=.... is treated as an implicit '-s' flag */
43 if (argv
[i
][0] != '=' && strchr(argv
[i
],'=')) {
44 if (!optc
--) { printf("too many options (%d max)\n", N_OPTS
); return -1; }
46 optv
++->val
= argv
[i
];
48 if (n
>= N_TARGETS
) { printf("too many targets (%d max)\n", N_TARGETS
); return -1; }
49 targets
[n
++] = argv
[i
];
58 * Name: getoptval() - find an option given its character
60 const char *getoptval (option
*optv
, char opt
, int subopt
) {
63 for (i
= 0; i
< N_OPTS
; ++i
, ++optv
) if (optv
->flag
== opt
&& !subopt
--) return optv
->val
;