KJam is working on windoze again (damn it! i can't code for windoze without KJam!)
[k8jam.git] / option.c
blobef942e2203ee3098415e5bfe9145d28e522fa8d8
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * option.c - command line option processing
10 * {o >o
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
19 # include "jam.h"
20 # include "option.h"
22 int getoptions (int argc, char **argv, const char *opts, option *optv, char** targets) {
23 char *arg;
24 const char *f;
25 int i, n;
26 int optc = N_OPTS;
28 memset((char *)optv, '\0', sizeof(*optv) * N_OPTS);
29 n = 0;
30 for (i = 0; i < argc; i++) {
31 if (argv[i][0] == '-') {
32 if (!optc--) {
33 printf("too many options (%d max)\n", N_OPTS);
34 return -1;
36 for (arg = &argv[i][1]; *arg; arg++) {
37 for (f = opts; *f; f++) {
38 if(*f == *arg) break;
40 if (!*f) {
41 printf("Invalid option: -%c\n", *arg);
42 return -1;
44 optv->flag = *f;
45 if (f[1] != ':') {
46 optv++->val = "true";
47 } else if (arg[1]) {
48 optv++->val = &arg[1];
49 break;
50 } else if (++i < argc) {
51 optv++->val = argv[i];
52 break;
53 } else {
54 printf("option: -%c needs argument\n", *f);
55 return -1;
58 } else {
59 /* something like VARNAME=.... is treated as an implicit '-s' flag */
60 if (argv[i][0] != '=' && strchr(argv[i],'=')) {
61 if (!optc--) {
62 printf("too many options (%d max)\n", N_OPTS);
63 return -1;
65 optv->flag = 's';
66 optv++->val = argv[i];
67 } else {
68 if (n >= N_TARGETS) {
69 printf("too many targets (%d max)\n", N_TARGETS);
70 return -1;
72 targets[n++] = argv[i];
76 return n;
81 * Name: getoptval() - find an option given its character
83 const char *getoptval (option *optv, char opt, int subopt) {
84 int i;
86 for (i = 0; i < N_OPTS; i++, optv++) {
87 if (optv->flag == opt && !subopt--) return optv->val;
89 return 0;