File: options.c

package info (click to toggle)
nn 6.5.1-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,472 kB
  • ctags: 4,518
  • sloc: ansic: 35,126; sh: 1,489; makefile: 214; awk: 138
file content (213 lines) | stat: -rw-r--r-- 3,932 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 *	(c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
 *
 *	Generic option parsing
 */

#include "config.h"
#include "options.h"

/* options.c */

static void dump_options __APROTO((int type, char *tail));
static void error __APROTO((char *message, int option_letter));

static char **save_argv, *usage_mesg;
static struct option_descr *save_optd;

extern int atoi();


char *program_name(av)
char **av;
{
    char *cp;

    /* skip "/path/" part of program name */
    if ((cp = strrchr(*av, '/')))
	return cp + 1;
    else
	return *av;
}

static void
dump_options(type, tail)
int type;
char *tail;
{
    register struct option_descr *od;
    int any = 0;

    for (od = save_optd; od->option_letter; od++) {
	if (od->option_type != type) continue;
	fprintf(stderr, any ? "%c" : " -%c", od->option_letter );
	any++;
    }

    if (any && tail && tail[0]) {
	fprintf(stderr, "%s", tail);
    }
}

static void error(message, option_letter)
char *message, option_letter;
{
    char *prog_name = program_name(save_argv);

    fprintf(stderr, "%s: ", prog_name);
    fprintf(stderr, message, option_letter);
    fputc('\n', stderr);

    fprintf(stderr, "usage: %s", prog_name);

    dump_options(1, "");
    dump_options(2, " STR");
    dump_options(3, " [STR]");
    dump_options(4, " NUM");
    dump_options(5, " [NUM]");

    if (usage_mesg) fprintf(stderr, usage_mesg);
    fputc(NL, stderr);

    nn_exit(9);
}

int
parse_options(ac, av, envname, options, usage)
int ac;
char **av, *envname;
struct option_descr options[];
char *usage;
{
    register char *cp, opt;
    register struct option_descr *od;
    int files;
    char **names;
    char *envinit;

    save_argv = av;
    save_optd = options;

    if (options == NULL) return 0;

    usage_mesg = usage;

    --ac;
    names = ++av;
    files = 0;

    envinit = envname ? getenv(envname) : NULL;
    cp = envinit;

 next_option:

    if (envinit) {
	while (*cp && isspace(*cp)) cp++;
	if (*cp == '-') {
	    cp++;
	    goto next_option;
	}
	if (*cp == NUL) {
	    envinit = NULL;
	    goto next_option;
	}
    } else
	if (cp == NULL || *cp == NUL) {
	    if ((cp = *av++) == NULL) {
		*names = NULL;
		return files;
	    }
	    ac--;

	    if (*cp != '-') {
		*names++ = cp;
		cp = NULL;
		files++;
		goto next_option;
	    }

	    cp++; /* skip - */
	}

    opt = *cp++;

    for (od = options; od->option_letter; od++) {
	if (od->option_letter != opt) continue;

	switch (od->option_type) {

	 case 1:	/* BOOL_OPTION */

	    *((int *)(od->option_address)) = !*((int *)(od->option_address));
	    goto next_option;

	 case 2:	/* STRING_OPTION */
	 case 3:	/* OPTIONAL_STRING */

	    /* -oSTR or -o STR */

	    while (*cp && isspace(*cp)) cp++;

	    if (*cp == NUL) {
		if (envinit || ac == 0) {
		    if (od->option_type == 3) goto opt_str;
		    error("missing string argumet to -%c", opt);
		}
		cp = *av++;
		ac--;
	    }

	    if (od->option_type == 3 && *cp == '-') {
		cp++;
		goto opt_str;
	    }

	    *(od->option_address) = cp;

	    if (envinit) {
		while (*cp && !isspace(*cp)) cp++;
		if (*cp) *cp++ = NUL;
	    } else
		cp = NULL;

	    goto next_option;

	 opt_str:
	    *(od->option_address) = od->option_default;
	    goto next_option;

	 case 4:
	 case 5:

	    /* -oN or -o N */

	    while (*cp && isspace(*cp)) cp++;

	    if (*cp) {
		if (!isdigit(*cp)) {
		    if (od->option_type == 5) goto opt_int;
		    error("non-numeric argument to -%c", opt);
		}
	    } else {
		if (envinit || ac == 0 || !isdigit(**av)) {
		    if (od->option_type == 5) goto opt_int;
		    error("missing argument to -%c", opt);
		}

		cp = *av++;
		ac--;
	    }
	    *((int *)(od->option_address)) = atoi(cp);
	    while (isdigit(*cp)) cp++;
	    goto next_option;

	 opt_int:
	    *((int *)(od->option_address)) = (int)(od->option_default);
	    goto next_option;
	}
    }

    error("unknown option '-%c'", opt);
    /*NOTREACHED*/
    return 0;
}