-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
196 lines (180 loc) · 4.2 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
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
/* liboptions by Lukas Joeressen.
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* - May you do good and not evil.
* - May you find forgiveness for yourself and forgive others.
* - May you share freely, never taking more than you give. */
#include "options.h"
#include <stdio.h>
#include <stdlib.h>
#define NEED_ARGUMENT_LONG "Option '--\0' needs an argument."
#define NEED_ARGUMENT_SHORT "Option '-\0' needs an argument."
#define UNRECOGNISED_LONG "Unrecognised option '--\0'."
#define UNRECOGNISED_SHORT "Unrecognised option '-\0'."
#define UNEXPECTED_ARGUMENT "Option '--\0' expects no argument."
static int fprintfs(FILE* f, const char *format, const char *str)
{
while (*format) putc(*(format++), f);
format++;
while (*str) putc(*(str++), f);
while (*format) putc(*(format++), f);
return 0;
}
static int fprintfc(FILE* f, const char *format, char c)
{
while (*format) putc(*(format++), f);
format++;
putc(c, f);
while (*format) putc(*(format++), f);
return 0;
}
static int str_equal(const char *a, const char *b)
{
if (!a || !b) return 0;
while (*a && *b && *a == *b) {++a; ++b;}
return *a == *b;
}
static option_t *lookup_short(option_t **o, char n)
{
while (*o) {
if ((*o)->shortopt == n) {
return *o;
}
++o;
}
fprintfc(stderr, UNRECOGNISED_SHORT "\n", n);
exit(1);
return 0;
}
static option_t *lookup_long(option_t **o, char *n)
{
while (*o) {
if ((*o)->longopt && str_equal((*o)->longopt, n)) {
return *o;
}
++o;
}
fprintfs(stderr, UNRECOGNISED_LONG "\n", n);
exit(1);
return 0;
}
static char *find_and_null_eq(char *s)
{
while (*s) {
if (*s == '=') {
*s = 0;
return s + 1;
}
++s;
}
return 0;
}
#define HAS_PARAMETER(o) ((o)->type & OPTION_PARAMETER)
#define HAS_DEFAULT(o) ((o)->type & OPTION_DEFAULT)
#define HAS_CALLBACK(o) ((o)->type & OPTION_CALLBACK)
static void apply_option(option_t *o, char *arg, int l)
{
char s[2] = {0};
if (!arg && HAS_DEFAULT(o)) {
arg = o->arg_default;
}
if (!arg && HAS_PARAMETER(o) && !HAS_DEFAULT(o)) {
if (l) {
fprintfs(stderr, NEED_ARGUMENT_LONG "\n", o->longopt);
} else {
fprintfc(stderr, NEED_ARGUMENT_SHORT "\n", o->shortopt);
}
exit(1);
}
if (HAS_CALLBACK(o)) {
if (l) {
o->callback(o->longopt, arg, 1);
} else {
s[0] = o->shortopt;
o->callback(s, arg, 0);
}
} else if (HAS_PARAMETER(o)) {
if (o->arg_adr) {
*(o->arg_adr) = arg;
}
} else {
if (o->flag_adr) {
*(o->flag_adr) = o->flag_value;
}
}
}
int parse_options(int *argc, char ***argv, option_t **options)
{
int c = 1, i = 1, j;
char x;
char *a, *v;
char **_argv = *argv;
option_t *o = 0;
int l = 0;
while (i < *argc) {
if (_argv[i][0] == '-' && _argv[i][1]) {
if (o) {
apply_option(o, 0, l);
o = 0;
}
if (_argv[i][1] == '-' && !_argv[i][2]) {
/* End of options */
++i;
while (i < *argc) {
_argv[c] = _argv[i];
++c;
++i;
}
} else if (_argv[i][1] == '-') {
/* Longopt */
l = 1;
a = _argv[i] + 2;
v = find_and_null_eq(a);
o = lookup_long(options, a);
if (v || !HAS_PARAMETER(o)) {
if (v && !HAS_PARAMETER(o)) {
fprintfs(stderr, UNEXPECTED_ARGUMENT "\n", o->longopt);
exit(1);
}
apply_option(o, v, l);
o = 0;
}
} else {
/* Shortopt */
l = 0;
j = 1;
while ((x = _argv[i][j])) {
o = lookup_short(options, x);
if (!HAS_PARAMETER(o)) {
apply_option(o, 0, l);
o = 0;
} else {
if (_argv[i][j + 1]) {
apply_option(o, _argv[i] + j + 1, l);
o = 0;
}
break;
}
++j;
}
}
} else {
if (o) {
apply_option(o, _argv[i], l);
o = 0;
} else {
_argv[c] = _argv[i];
++c;
}
}
++i;
}
if (o) apply_option(o, 0, l);
/* Clean argument vector. */
for (i = c; i < *argc; ++i)
_argv[i] = 0;
*argc = c;
return 0;
}