-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargsparse.c
386 lines (347 loc) · 9.84 KB
/
argsparse.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* @file argsparse.c
* @author Tuomas Lahtinen (tuomas123lahtinen@gmail.com)
* @brief Generic command line arguments definition utility
*
* @copyright Copyright (c) 2023
*
*/
#include "argsparse.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <getopt.h>
typedef struct _argparse_argument_linked
{
HARGPARSE_ARG argument;
void* next;
} t_argparse_argument_linked;
typedef struct _argparse_argument_linked* HARGPARSE_ARG_LINKED;
typedef struct _argparse_struct
{
char shortopts[ARGSPARSE_MAX_ARGUMENTS * 2];
int count;
HARGPARSE_ARG_LINKED arguments;
const char* title;
} t_argparse_struct;
static HARGPARSE_ARG_LINKED delete_argument(HARGPARSE_ARG_LINKED arg);
static int parse_value(HARGPARSE_VALUE* ref, tArgsparseValuetype type, const char* value);
HARGPARSE_HANDLE argsparse_create(const char* title)
{
HARGPARSE_HANDLE p = calloc(1, sizeof(t_argparse_struct));
p->title = title;
p->arguments = NULL;
}
void argsparse_delete(HARGPARSE_HANDLE handle)
{
HARGPARSE_ARG_LINKED next = handle->arguments;
while (next)
{
next = delete_argument(next);
}
free (handle);
}
HARGPARSE_ARG argsparse_argument_allocate(const char* name, const char* desc)
{
HARGPARSE_ARG p = calloc(1, sizeof(t_argparse_argument));
p->name = strdup(name);
p->description = strdup(desc);
return p;
}
HARGPARSE_ARG argsparse_create_argument(tArgsparseValuetype type, const char* name, const char* desc, const char* value)
{
HARGPARSE_ARG p = argsparse_argument_allocate(name, desc);
p->type = type;
parse_value(&p->value, type, value);
return p;
}
char* argsparse_get_shortopts(HARGPARSE_HANDLE handle)
{
return (handle) ? handle->shortopts : NULL;
}
const char* argsparse_get_title(HARGPARSE_HANDLE handle)
{
return (handle) ? handle->title : NULL;
}
static int argument_compare_name(HARGPARSE_ARG arg, void* data)
{
const char* name = (const char*)data;
return strcmp(name, arg->name);
}
static int argument_compare_short_name(HARGPARSE_ARG arg, void* data)
{
int name = *((int*)data);
return (name == arg->name_short) ? 0 : 1;
}
HARGPARSE_ARG argsparse_argument_get(HARGPARSE_HANDLE handle, int(*cmp)(HARGPARSE_ARG, void*), void* cmpdata)
{
HARGPARSE_ARG ret = NULL;
HARGPARSE_ARG_LINKED linked = ((t_argparse_struct*)handle)->arguments;
while (linked && cmp)
{
HARGPARSE_ARG argument = linked->argument;
if ((*cmp)(argument, cmpdata) == 0)
{
ret = argument;
break;
}
linked = linked->next;
}
return ret;
}
HARGPARSE_ARG argsparse_argument_by_name(HARGPARSE_HANDLE handle, const char* name)
{
return argsparse_argument_get(handle, argument_compare_name, (void*)name);
}
HARGPARSE_ARG argsparse_argument_by_short_name(HARGPARSE_HANDLE handle, int shortname)
{
return argsparse_argument_get(handle, argument_compare_short_name, (void*)&shortname);
}
static void do_option_long(int idx, HARGPARSE_ARG arg, void* data)
{
struct option* options = (struct option*)data;
options[idx].name = arg->name;
options[idx].has_arg = arg->type != ARGSPARSE_TYPE_NONE;
options[idx].val = arg->name_short;
}
int argsparse_argument_action(HARGPARSE_HANDLE handle, void (*action)(int, HARGPARSE_ARG, void*), void* actiondata)
{
int ret = 0;
t_argparse_argument_linked* iterator = ((t_argparse_struct*)handle)->arguments;
while (iterator)
{
// invoke caller action
if (action)
(*action)(ret, iterator->argument, actiondata);
ret++;
iterator = iterator->next;
}
return ret;
}
int argsparse_argument_count(HARGPARSE_HANDLE handle)
{
return argsparse_argument_action(handle, NULL, NULL);
}
static const char* FindEndTerminator(const char* str)
{
const char* end = NULL;
char terminators[] = {' ', '\0'};
for (int i = 0; str && end == NULL && i < sizeof(terminators); i++)
{
end = strchr(str, terminators[i]);
}
return end;
}
static int parse_value(HARGPARSE_VALUE* ref, tArgsparseValuetype type, const char* value)
{
int ret = 0;
if (type == ARGSPARSE_TYPE_NONE)
return ret;
if (*ref == NULL)
*ref = calloc(1, sizeof(t_argparse_value));
switch (type)
{
case ARGSPARSE_TYPE_DOUBLE:
(*ref)->doublevalue = value ? atof(value) : -1.0;
break;
case ARGSPARSE_TYPE_INT:
(*ref)->intvalue = value ? atoi(value) : -1;
break;
case ARGSPARSE_TYPE_STRING:
{
const char* end = FindEndTerminator(value);
if (end)
{
int len = end - value;
if (len > 0 && len < ARGSPARSE_VALUE_STRING_SIZE)
{
strncpy((*ref)->stringvalue, value, len);
(*ref)->stringvalue[len] = 0;
}
else
ret = -1;
}
else
ret = -1;
}
break;
default:
ret = -1;
break;
}
return ret;
}
int set_short_option(char c, HARGPARSE_HANDLE handle, HARGPARSE_ARG arg)
{
int ret = -1;
char* used = handle->shortopts;
// iterate used until terminating 0
while (*used)
{
// used - break the loop and move to next longname character
if (*used == c)
{
break;
}
used++;
}
// unused - set value and break
if (*used == 0)
{
*used = c;
*(used + 1) = (arg->type == ARGSPARSE_TYPE_NONE) ? 0 : ':';
arg->name_short = c;
ret = 0;
}
return ret;
}
void generate_short_name(HARGPARSE_HANDLE handle, HARGPARSE_ARG arg)
{
char* longname = arg->name;
char c;
// iterate characters of longname as long as necessary
while(c = *longname)
{
if (set_short_option(c, handle, arg))
{
longname++;
continue;
}
break;
}
// none of the longname characters accepted
if (!c)
{
// iterate characters from 'a' until the short option is set
c = 'a';
while (1)
{
if (set_short_option(c, handle, arg))
{
c++;
continue;
}
break;
}
}
}
/// @brief Add argument moves argument ownership to handle
/// @param handle Handle to allocated arguments structure
/// @param argument Handle to allocated argument
int argsparse_put_argument(HARGPARSE_HANDLE handle, HARGPARSE_ARG* href)
{
if (handle->count >= ARGSPARSE_MAX_ARGUMENTS)
{
// didn't take ownership
return -1;
}
handle->count++;
HARGPARSE_ARG arg = *href;
HARGPARSE_ARG_LINKED linked = calloc(1, sizeof(t_argparse_argument_linked));
linked->argument = arg;
generate_short_name(handle, arg);
if (handle->arguments)
{
HARGPARSE_ARG_LINKED next = handle->arguments;
while (next->next)
{
next = next->next;
}
next->next = linked;
}
else
handle->arguments = linked;
// take ownership
*href = NULL;
return 0;
}
void argsparse_add_int(HARGPARSE_HANDLE handle, const char* option, const char* description, int value)
{
}
void argsparse_add_double(HARGPARSE_HANDLE handle, const char* option, const char* description, double value)
{
}
void AddArgumentTypeOnOff(HARGPARSE_HANDLE handle, const char* option, const char* description, const char* value)
{
}
void argsparse_add_str(HARGPARSE_HANDLE handle, const char* option, const char* description, const char* value)
{
}
int argsparse_add_flag(HARGPARSE_HANDLE handle, const char* name, const char* description)
{
HARGPARSE_ARG p = argsparse_argument_allocate(name, description);
p->type = ARGSPARSE_TYPE_NONE;
int err = argsparse_put_argument(handle, &p);
if (err)
free (p);
return err;
}
int argsparse_parse_args(HARGPARSE_HANDLE handle, char* const *argv, int argc)
{
int count = 0;
struct option *long_options = calloc(count, sizeof(struct option));
// {
// /* These options set a flag. */
// {"verbose", no_argument, &verbose_flag, 1},
// {"brief", no_argument, &verbose_flag, 0},
// /* These options don’t set a flag.
// We distinguish them by their indices. */
// {"file", required_argument, 0, 'f'},
// {0, 0, 0, 0}
// };
/* getopt_long stores the option index here. */
int option_index = 0;
argsparse_argument_action(handle, do_option_long, (void*)long_options);
int c;
while(1)
{
c = getopt_long(argc, argv,
handle->shortopts,
(const struct option *)long_options,
&option_index);
/* Detect the end of the options. */
if (c == -1)
break;
HARGPARSE_ARG arg = argsparse_argument_by_short_name(handle, c);
if (arg == NULL)
{
printf ("invalid option -%c", c);
argsparse_usage(handle);
exit(1);
}
else
{
int err = parse_value(&arg->value, arg->type, optarg);
if (!err)
{
arg->parsed = 1;
count++;
}
}
}
return count;
}
void argsparse_usage(HARGPARSE_HANDLE handle)
{
}
static HARGPARSE_ARG_LINKED delete_argument(HARGPARSE_ARG_LINKED del)
{
HARGPARSE_ARG_LINKED ret = del != NULL ? del->next : NULL;
del->next = NULL;
if (del != NULL)
{
HARGPARSE_ARG arg = del->argument;
del->argument = NULL;
if (arg->value)
free (arg->value);
arg->value = NULL;
free (arg->name);
arg->name = NULL;
free (arg->description);
arg->description = NULL;
free(arg);
free(del);
}
return ret;
}