fixed pkg-config rule
[k8jam.git] / src / option.c
blobf10698d41f49b144f184339d743462e7e137f51c
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
6 /*
7 * option.c - command line option processing
9 * {o >o
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
17 #include "jam.h"
18 #include "option.h"
21 int getoptions (int argc, char **argv, const char *opts, option *optv, char** targets) {
22 char *arg;
23 const char *f;
24 int i, n;
25 int optc = N_OPTS;
27 memset((char *)optv, '\0', sizeof(*optv)*N_OPTS);
28 n = 0;
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; }
35 optv->flag = *f;
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; }
41 } else {
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; }
45 optv->flag = 's';
46 optv++->val = argv[i];
47 } else {
48 if (n >= N_TARGETS) { printf("too many targets (%d max)\n", N_TARGETS); return -1; }
49 targets[n++] = argv[i];
53 return n;
58 * Name: getoptval() - find an option given its character
60 const char *getoptval (option *optv, char opt, int subopt) {
61 int i;
63 for (i = 0; i < N_OPTS; ++i, ++optv) if (optv->flag == opt && !subopt--) return optv->val;
64 return 0;