forked from droundy/fac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharguments.c
More file actions
218 lines (205 loc) · 6.09 KB
/
arguments.c
File metadata and controls
218 lines (205 loc) · 6.09 KB
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
214
215
216
217
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "arguments.h"
enum argument_type {
NO_ARG,
INT_ARG,
STRING_ARG,
STRINGLIST_ARG
};
struct a {
const char *long_name;
char short_name;
const char *description;
const char *fieldname;
enum argument_type type;
const char **string_location;
const char ***stringlist_location;
int *int_location;
bool *bool_location;
};
int num_arguments = 0;
static struct a *args = 0;
static struct a * add_argument() {
num_arguments++;
args = (struct a *)realloc(args, num_arguments*sizeof(struct a));
return &args[num_arguments - 1];
}
void string_argument(const char *long_name, char short_name, char **output,
const char *description, const char *outname) {
struct a *a = add_argument();
a->long_name = long_name;
a->short_name = short_name;
a->string_location = (const char **)output;
a->type = STRING_ARG;
a->description = description;
a->fieldname = outname;
}
void string_argument_list(const char *long_name, char short_name, const char ***output,
const char *description, const char *outname) {
struct a *a = add_argument();
a->long_name = long_name;
a->short_name = short_name;
a->stringlist_location = output;
a->type = STRINGLIST_ARG;
a->description = description;
a->fieldname = outname;
}
void int_argument(const char *long_name, char short_name, int *output,
const char *description, const char *outname) {
struct a *a = add_argument();
a->long_name = long_name;
a->short_name = short_name;
a->int_location = output;
a->type = INT_ARG;
a->description = description;
a->fieldname = outname;
}
void no_argument(const char *long_name, char short_name, bool *output,
const char *description) {
struct a *a = add_argument();
a->long_name = long_name;
a->short_name = short_name;
a->type = NO_ARG;
a->bool_location = output;
a->description = description;
a->fieldname = 0;
}
static bool read_int(const char *str, int *val) {
char *end = 0;
long int v = strtol(str, &end, 0);
if (end != str + strlen(str)) {
return false;
}
if ((long)(int)v != v) {
// not in range
return false;
}
*val = v;
return true;
}
static void handle_arg(struct a *the_a, const char *the_arg) {
if (the_a->type == INT_ARG) {
if (!read_int(the_arg, the_a->int_location)) {
fprintf(stderr, "invalid integer argument --%s '%s'\n", the_a->long_name, the_arg);
exit(1);
}
} else if (the_a->type == STRING_ARG) {
*(the_a->string_location) = the_arg;
} else if (the_a->type == STRINGLIST_ARG) {
if (!*the_a->stringlist_location) {
*the_a->stringlist_location = malloc(sizeof(char **));
**the_a->stringlist_location = 0;
}
int num_strings;
for (num_strings=0; (*the_a->stringlist_location)[num_strings]; num_strings++) {
// counting how many strings we have already.
}
*the_a->stringlist_location = realloc(*(the_a->stringlist_location),
(num_strings+2)*sizeof(char **));
(*the_a->stringlist_location)[num_strings+1] = 0;
(*the_a->stringlist_location)[num_strings] = the_arg;
} else if (the_a->type == NO_ARG) {
*(the_a->bool_location) = true;
}
}
const char **parse_arguments_return_extras(const char **argv) {
argv++;
int num_output = 0;
const char **output = 0;
for (const char **here = argv; *here; here++) {
if (strcmp(*here, "--help") == 0) {
bool help_argument;
no_argument("help", 'h', &help_argument,
"show this help message");
printf("Usage: fac [OPTIONS] [TARGETS]\n");
for (int j=0; j<num_arguments; j++) {
int chars_written = 0;
if (args[j].short_name) {
chars_written += printf(" -%c,", args[j].short_name);
}
while (chars_written < 4) {
printf(" ");
chars_written++;
}
if (args[j].long_name) {
chars_written += printf(" --%s", args[j].long_name);
}
if (args[j].fieldname) {
chars_written += printf(" %s", args[j].fieldname);
}
while (chars_written < 32) {
printf(" ");
chars_written++;
}
printf(" %s\n", args[j].description);
}
exit(0);
} else if (strncmp(*here, "--", 2) == 0) {
const char *name = *here + 2;
const char *the_arg = 0;
struct a *the_a = 0;
for (int j=0; j<num_arguments; j++) {
if (strcmp(args[j].long_name, name) == 0) {
the_a = &args[j];
if (args[j].type != NO_ARG) {
here++;
the_arg = *here;
}
break;
}
int arglen = strlen(args[j].long_name);
if (args[j].type != NO_ARG &&
strncmp(args[j].long_name, name, arglen) == 0 &&
name[arglen] == '=') {
the_arg = name + arglen + 1;
the_a = &args[j];
break;
}
}
if (!the_a) {
fprintf(stderr, "invalid argument --%s\n", name);
exit(1);
}
handle_arg(the_a, the_arg);
} else if (strncmp(*here, "-", 1) == 0) {
for (int i=1; i<strlen(*here); i++) {
char c = (*here)[i];
struct a *the_a = 0;
const char *the_arg = 0;
for (int j=0; j<num_arguments; j++) {
if (args[j].short_name == c) {
the_a = &args[j];
break;
}
}
if (!the_a) {
fprintf(stderr, "invalid argument -%c\n", c);
}
if (the_a->type != NO_ARG) {
if (i < strlen(*here)-1) {
the_arg = *here + i+1;
i = strlen(*here);
}
}
if (!the_arg && the_a->type != NO_ARG) {
here++;
the_arg = *here;
}
handle_arg(the_a, the_arg);
if (the_arg) {
break;
}
}
} else {
num_output++;
output = (const char **)realloc(output, num_output*sizeof(char *));
output[num_output-1] = *here;
}
}
num_output++;
output = (const char **)realloc(output, num_output*sizeof(char *));
output[num_output-1] = 0;
return output;
}