-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.c
136 lines (117 loc) · 2.53 KB
/
options.c
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
/* This file is part of the software similarity tester SIM.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: options.c,v 1.10 2012-05-13 09:05:49 Gebruiker Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include "options.h"
static char options[128];
static void bad_option(
const char *progname, const struct option *optlist, char *msg, int c
);
static int opt_value(
const char *progname, const struct option *op,
const char *arg, const char *argv[]
);
static int do_arg(
const char *progname, const struct option *optlist,
const char *arg, const char *argv[]
);
int
do_options(
const char *progname, const struct option *optlist,
int argc, const char *argv[]
) {
int skips = 0;
while (argc > 0 && argv[0][0] == '-' && argv[0][1] != '\0') {
int consumed = do_arg(progname, optlist, &argv[0][1], argv);
argc -= consumed, argv += consumed, skips += consumed;
}
return skips;
}
void
set_option(char ch) {
options[(int)ch]++;
}
int
is_set_option(int ch) {
return options[ch];
}
static int
do_arg(
const char *progname, const struct option *optlist,
const char *arg, const char *argv[]
) {
int consumed = 0;
while (*arg) {
/* treat argument character */
char opc = *arg++;
const struct option *op;
for (op = optlist; op->op_char; op++) {
if (opc == op->op_char) {
set_option(opc);
if (op->op_indicator != ' ') {
consumed = opt_value(
progname, op, arg, argv
);
}
break;
}
}
if (!op->op_char) {
bad_option(progname, optlist,
"*option -%c unknown", opc
);
/*NOTREACHED*/
}
if (consumed) break;
}
if (!consumed) {
consumed = 1;
}
return consumed;
}
static int
opt_value(
const char *progname, const struct option *op,
const char *arg, const char *argv[]
) {
/* locate the option value */
if (*arg) {
/* argument is continuation of option */
*op->op_stringp = arg;
return 1;
}
else
if (argv[1]) {
/* argument follows option */
*op->op_stringp = argv[1];
return 2;
}
else {
bad_option(progname, (struct option *)0,
" option -%c requires another argument",
op->op_char
);
return 0;
/*NOTREACHED*/
}
}
static void
bad_option(
const char *progname, const struct option *optlist, char *msg, int c
) {
fprintf(stderr, "%s: ", progname);
fprintf(stderr, &msg[1], c);
fprintf(stderr, "\n");
if (msg[0] != ' ') {
const struct option *op;
fprintf(stderr, "Possible options are:\n");
for (op = optlist; op->op_char; op++) {
fprintf(stderr, "\t-%c%c\t%s\n",
op->op_char, op->op_indicator, op->op_text
);
}
}
exit(1);
}