This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
argv.c
158 lines (137 loc) · 3.37 KB
/
argv.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
#include "argv.h"
#include <stdlib.h>
void *(*argv_realloc)(void*, size_t) = realloc;
#include <string.h>
enum argv_p_state
{
_ARGV_S_ARGUMENT,
/* Value for -interface */
_ARGV_S_INTERFACE_VAL,
/* Value for -db */
_ARGV_S_DB_VAL,
/* Value for -user */
_ARGV_S_USER_VAL,
/* Value for -group */
_ARGV_S_GROUP_VAL,
/* First value for -iprange */
_ARGV_S_IPRANGE_VAL_1,
/* Second value for -iprange */
_ARGV_S_IPRANGE_VAL_2,
/* Value for -router */
_ARGV_S_ROUTERS_VAL,
/* Value for -nameserver */
_ARGV_S_NAMESERVERS_VAL,
/* Value for -prefixlen */
_ARGV_S_PREFIXLEN_VAL,
/* Value for -leasetime */
_ARGV_S_LEASETIME_VAL,
/* Value for -gc */
_ARGV_S_GC_VAL
};
bool argv_parse(int argc, char **argv, struct argv *out)
{
enum argv_p_state state = _ARGV_S_ARGUMENT;
out->argv = argv;
out->argc = argc;
out->arg0 = argv[0];
for (int i = 1; i < argc; ++i)
{
char *arg = argv[i];
switch (state)
{
case _ARGV_S_ARGUMENT:
if (!strcmp(arg, "-interface"))
state = _ARGV_S_INTERFACE_VAL;
else if (!strcmp(arg, "-db"))
state = _ARGV_S_DB_VAL;
else if (!strcmp(arg, "-user"))
state = _ARGV_S_USER_VAL;
else if (!strcmp(arg, "-group"))
state = _ARGV_S_GROUP_VAL;
else if (!strcmp(arg, "-iprange"))
state = _ARGV_S_IPRANGE_VAL_1;
else if (!strcmp(arg, "-router"))
state = _ARGV_S_ROUTERS_VAL;
else if (!strcmp(arg, "-nameserver"))
state = _ARGV_S_NAMESERVERS_VAL;
else if (!strcmp(arg, "-gc"))
state = _ARGV_S_GC_VAL;
else if (!strcmp(arg, "-allocate"))
out->allocate = true;
else if (!strncmp(arg, "-h", 2))
out->help = true;
else if (!strncmp(arg, "-v", 2))
out->version = true;
else if (!strncmp(arg, "-d", 2))
out->debug = true;
else if (!strcmp(arg, "-new"))
out->_new = true;
else if (!strcmp(arg, "-prefixlen"))
state = _ARGV_S_PREFIXLEN_VAL;
else if (!strcmp(arg, "-leasetime"))
state = _ARGV_S_LEASETIME_VAL;
else
{
out->argerror = i;
return false;
}
break;
case _ARGV_S_INTERFACE_VAL:
out->interface = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_DB_VAL:
out->db = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_USER_VAL:
out->user = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_GROUP_VAL:
out->group = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_IPRANGE_VAL_1:
out->iprange[0] = arg;
state = _ARGV_S_IPRANGE_VAL_2;
break;
case _ARGV_S_IPRANGE_VAL_2:
out->iprange[1] = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_ROUTERS_VAL:
out->routers = argv_realloc(
out->routers,
++out->routers_cnt * sizeof(char*));
out->routers[out->routers_cnt - 1] = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_NAMESERVERS_VAL:
out->nameservers = argv_realloc(
out->nameservers,
++out->nameservers_cnt * sizeof(char*));
out->nameservers[out->nameservers_cnt - 1] = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_LEASETIME_VAL:
out->leasetime = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_PREFIXLEN_VAL:
out->prefixlen = arg;
state = _ARGV_S_ARGUMENT;
break;
case _ARGV_S_GC_VAL:
out->gc = arg;
state = _ARGV_S_ARGUMENT;
break;
}
}
if (state != _ARGV_S_ARGUMENT)
{
out->argerror = -1;
return false;
}
return true;
}