-
Notifications
You must be signed in to change notification settings - Fork 0
/
getconfopt.c
82 lines (76 loc) · 1.57 KB
/
getconfopt.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
#include "config.h"
#include "die.h"
#include "getconf.h"
#include "getconfopt.h"
#include "str.h"
static void init(struct option *options)
{
struct option *o;
for (o = options; o->type != 0; ++o)
o->isset = 0;
}
static int parse(int argc,char *argv[],struct option *options)
{
int i;
int j;
int ch;
struct option *o;
for (i = 1; i < argc && argv[i][0] == '-'; ++i) {
if (argv[i][1] == 0
|| (argv[i][1] == '-' && argv[i][2] == 0))
break;
for (j = 1; argv[i][j] != 0; ++j) {
ch = argv[i][j];
for (o = options; o->type != 0; ++o) {
if (ch == o->ch) {
if (!o->type->needsarg)
(void)o->type->parse(o,0);
else {
/* Look for -oVALUE */
if (argv[i][j+1] != 0) {
++j;
if (!o->type->parse(o,argv[i]+j))
die_usage();
}
/* Look for -o VALUE */
else if (i + 1 < argc) {
++i;
if (!o->type->parse(o,argv[i]))
die_usage();
}
else
die_usage();
j = str_len(argv[i]) - 1;
}
o->isset = 1;
break;
}
}
if (o->type == 0)
die_usage();
}
}
return i;
}
static void post(struct option *options)
{
struct option *o;
for (o = options; o->type != 0; ++o)
if (o->filename && !o->isset)
o->type->fetch(o);
}
int getconfopt(int argc,char *argv[],struct option *options,
int dochdir,const char **dir)
{
int optind;
init(options);
optind = parse(argc,argv,options);
if (dir != 0)
*dir = argv[optind];
if (dochdir > 0
|| (dochdir < 0 && argv[optind] != 0)) {
startup(argv[optind++]);
post(options);
}
return optind;
}