-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
663 lines (557 loc) · 16.8 KB
/
cli.c
File metadata and controls
663 lines (557 loc) · 16.8 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/**
* @file cli.c
* @author bsnacks000
* @brief
* @version 0.1.0
* @date 2024-05-17
*
* @copyright Copyright (c) 2024
*
*/
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
void cli_print_err(cli_err err) {
switch (err) {
case CLI_PARSE_FAILED_INT:
fprintf(stderr, "err: token parse failed for integer.\n");
break;
case CLI_PARSE_FAILED_BOOL:
fprintf(stderr, "err: token parse failed for boolean.\n");
break;
case CLI_PARSE_FAILED_FLOAT:
fprintf(stderr, "err: token parse failed for float.\n");
break;
case CLI_PARSE_FAILED_STR:
fprintf(stderr, "err: token parse failed for str: buf too small.\n");
break;
case CLI_FULL_REGISTRY:
fprintf(stderr, "err: registry full.\n");
break;
case CLI_NOT_FOUND:
fprintf(stderr, "err: token not found.\n");
break;
case CLI_NAME_REQUIRED:
fprintf(stderr, "err: option name not found.\n");
break;
case CLI_UNSEEN_REQ_OPTS:
fprintf(stderr, "err: unseen required options.\n");
break;
case CLI_OUT_OF_BOUNDS:
fprintf(stderr, "err: out of bounds during parse.\n");
break;
case CLI_ALREADY_SEEN:
fprintf(stderr, "err: option was already seen.\n");
break;
case CLI_ARG_COUNT:
fprintf(stderr, "err: misconfigured positional arguments.\n");
break;
case CLI_TOKEN_TOO_LONG:
fprintf(stderr, "err: token longer then allowed max.\n");
break;
case CLI_USAGE_STR_TOO_LONG:
fprintf(stderr, "err: usage string longer then allowed max.\n");
break;
default:
break;
}
}
struct cli_opt;
struct cli_arg;
typedef cli_err (*cli_opt_parser)(struct cli_opt* opt, const char* token);
typedef cli_err (*cli_arg_parser)(struct cli_arg* arg, const char* token);
// internal box to carry the string buffer size so we don't overflow :(
// this is allocated in the public API and deallocated here after the parse.
typedef struct str_box {
char* ptr;
size_t sz;
} str_box;
// an array manager to deal with the liftime for the above.
typedef struct str_boxes {
str_box** arr;
size_t cap;
size_t idx;
} str_boxes;
void str_boxes_init(str_boxes* b, size_t cap) {
str_box** arr = (str_box**)calloc(cap, sizeof(str_box));
CLI_CHECK_MEM_ALLOC(arr);
b->arr = arr;
b->cap = cap;
b->idx = 0;
}
// create a new str_box and return a reference via sb_val
cli_err str_boxes_add(str_boxes* b, char* ptr, size_t sz, str_box** sb_val) {
if (b->idx == b->cap) {
return CLI_FULL_REGISTRY;
}
str_box* sb = (str_box*)malloc(sizeof(str_box));
CLI_CHECK_MEM_ALLOC(sb);
sb->ptr = ptr;
sb->sz = sz;
// add the the arena.
b->arr[b->idx] = sb;
b->idx++;
*sb_val = sb;
return CLI_OK;
}
void str_boxes_cleanup(str_boxes* b) {
for (; (int)b->idx > 0; b->idx--) {
free(b->arr[b->idx - 1]);
}
free(b->arr);
}
typedef struct cli_opt {
const char* name; // name of the arg without `-` or `--` prefix.
const char* usage; // A usage statement for help
cli_opt_parser parser; // the parse function.
void* value; // generic pointer to target value to set on parse.
bool required; // whether this option must be parsed
bool seen; // set when opt is seen in the parser
bool is_flag; // Tells parser to pass NULL argument to parser
} cli_opt;
typedef struct cli_opts {
cli_opt** opts; // the flag options to be parsed
size_t cap; // capacity for option array
size_t idx; // the current idx into the option array
} cli_opts;
// flag opts API
// TODO mem error handling
void cli_opts_init(cli_opts* opts, size_t cap) {
cli_opt** opts_arr = (cli_opt**)calloc(cap, sizeof(cli_opt*));
CLI_CHECK_MEM_ALLOC(opts_arr);
opts->opts = opts_arr;
opts->idx = 0;
opts->cap = cap;
}
void cli_opts_cleanup(cli_opts* opts) {
for (; (int)opts->idx > 0; opts->idx--) {
free(opts->opts[opts->idx - 1]);
}
free(opts->opts);
}
cli_err cli_opts_add(cli_opts* opts,
const char* name,
const char* usage,
cli_opt_parser parser,
void* value,
bool required,
bool is_flag) {
if (name == NULL) {
return CLI_NAME_REQUIRED;
}
if (opts->idx == opts->cap) {
return CLI_FULL_REGISTRY;
}
if (strlen(name) + 1 > CLI_OPT_TOKEN_MAX_LEN) {
return CLI_TOKEN_TOO_LONG;
}
if (strlen(usage) + 1 > CLI_OPT_USAGE_MAX_LEN) {
return CLI_USAGE_STR_TOO_LONG;
}
cli_opt* o = (cli_opt*)malloc(sizeof(cli_opt));
CLI_CHECK_MEM_ALLOC(o);
o->name = name;
o->usage = usage;
o->parser = parser;
o->value = value;
o->required = required;
o->seen = false;
o->is_flag = is_flag;
opts->opts[opts->idx] = o;
opts->idx++; // current idx is always the len of the opts
return CLI_OK;
}
bool cli_opts_n_required_seen(cli_opts* opts) {
size_t count_req = 0;
size_t count_seen = 0;
for (size_t i = 0; i < opts->idx; i++) {
if (opts->opts[i]->required) {
count_req++;
if (opts->opts[i]->seen) {
count_seen++;
}
}
}
// printf("req: %zu - seen: %zu\n", count_req, count_seen);
return count_req == count_seen;
}
cli_opt* cli_opts_find(cli_opts* opts, const char* name) {
for (size_t i = 0; i < opts->idx; i++) {
if (strcmp(opts->opts[i]->name, name) == 0) {
return opts->opts[i];
}
}
return NULL;
}
void cli_opt_print_message(cli_opt* o, char* buf) {
size_t name_l = strlen(o->name);
size_t usage_l = strlen(o->usage);
int err =
snprintf(buf, name_l + usage_l + 5, "\t-%s\t\t%s\n", o->name, o->usage);
if (err < 0) {
fprintf(stderr, "Assertion Error: cli_opt_print_message failed: %d\n", err);
}
}
// flag arg API
// anything after `--` or any token after the flag parse finishes.
typedef struct cli_arg {
cli_arg_parser parser;
void* value;
} cli_arg;
typedef struct cli_args {
cli_arg** args;
size_t cap;
size_t idx;
} cli_args;
void cli_args_init(cli_args* args, size_t cap) {
cli_arg** args_arr = (cli_arg**)calloc(cap, sizeof(cli_arg*));
CLI_CHECK_MEM_ALLOC(args_arr);
args->args = args_arr;
args->idx = 0;
args->cap = cap;
}
void cli_args_cleanup(cli_args* args) {
for (; (int)args->idx > 0; args->idx--) {
free(args->args[args->idx - 1]);
}
free(args->args);
}
cli_err cli_args_add(cli_args* args, cli_arg_parser parser, void* value) {
if (args->idx == args->cap) {
return CLI_FULL_REGISTRY;
}
cli_arg* a = (cli_arg*)malloc(sizeof(cli_arg));
CLI_CHECK_MEM_ALLOC(a);
a->parser = parser;
a->value = value;
args->args[args->idx] = a;
args->idx++;
return CLI_OK;
}
// the main cli_parse function
// result type is used to report more info about the failed parse.
cli_err cli_parse_loop(cli_opts* opts, cli_args* args, int argc, char** argv) {
int argv_i = 1;
// if we've configured correctly we should always have help, h flags out of
// the box...
if (opts != NULL) {
while (argv_i < argc) {
char* token = argv[argv_i];
// printf("current token: %s\n", token);
// check an exact match on delimiter first
if ((strcmp(token, "--") == 0)) {
argv_i++;
break;
}
// move the token pointer based on whether we detect a flag prefix
// (--, -)
if ((strncmp(token, "--", 2)) == 0) {
token += 2;
} else if ((strncmp(token, "-", 1)) == 0) {
token += 1;
} else {
break;
}
// short circuit the parse if we encounter help ... we immediately
// break out of the parse and should exit with the usage message
if ((strcmp(token, "h") == 0) || (strcmp(token, "help") == 0)) {
return CLI_PRINT_HELP_AND_EXIT;
}
// safer then strcpy
char tok_[CLI_OPT_TOKEN_MAX_LEN] = "";
strncpy(tok_, token, CLI_OPT_TOKEN_MAX_LEN - 1);
tok_[CLI_OPT_TOKEN_MAX_LEN - 1] = '\0';
char* tok_str = tok_;
// sep on =
char* first = strsep(&tok_str, "=");
char* second = strsep(&tok_str, "=");
// first should be guarateed
// second will either be none or garbage if = is abused
// printf("[%s, %s]\n", first, second);
cli_opt* opt;
if ((opt = cli_opts_find(opts, first)) == NULL) {
return CLI_NOT_FOUND;
}
// check if we've seen this flag
if (opt->seen) {
return CLI_ALREADY_SEEN;
}
// set the seen flag for the opt...
opt->seen = true;
// handle case where opt->is_flag = true;
if (opt->is_flag) {
cli_err err = opt->parser(opt, NULL);
if (err != CLI_OK) {
return err;
}
// incr += 1 and skip to next iter
argv_i++;
continue;
}
// we have a valid token like --data=42 split -> data, 42
// it must be a value parser
if (second != NULL) {
cli_err err = opt->parser(opt, second);
if (err != CLI_OK) {
return err;
}
argv_i++;
} else {
// we have a single arg but we want to do a value lookup in the
// next index...
// check argv_i is not at the end so we don't reach over argv
if (argv_i + 1 == argc) {
return CLI_OUT_OF_BOUNDS;
}
cli_err err = opt->parser(opt, argv[argv_i + 1]);
if (err != CLI_OK) {
return err;
}
argv_i += 2;
}
}
// check that we have seen all required opts
// if the parse broke early
if (!cli_opts_n_required_seen(opts)) {
return CLI_UNSEEN_REQ_OPTS;
}
}
if (args != NULL) {
// check that argc - the current argv i == the number of registered
// positional args
if ((argc - argv_i) != (int)(args->idx)) {
return CLI_ARG_COUNT;
}
for (size_t i = 0; i < args->idx; i++, argv_i++) {
char* token = argv[argv_i];
cli_arg* arg = args->args[i];
cli_err err = arg->parser(arg, token);
if (err != CLI_OK) {
return err;
}
}
}
return CLI_OK;
}
/// these are some default parsers ... these should always be called from the
cli_err str_opt_parser(cli_opt* opt, const char* token) {
str_box* box = (str_box*)(opt->value);
size_t s = (sizeof(box->ptr) * box->sz) / sizeof(box->ptr);
if (s < strlen(token) + 1) {
return CLI_PARSE_FAILED_STR;
}
strncpy(box->ptr, token, box->sz);
return CLI_OK;
}
cli_err str_arg_parser(cli_arg* arg, const char* token) {
str_box* box = (str_box*)(arg->value);
size_t s = (sizeof(box->ptr) * box->sz) / sizeof(box->ptr);
if (s < strlen(token) + 1) {
return CLI_PARSE_FAILED_STR;
}
strncpy(box->ptr, token, box->sz);
return CLI_OK;
}
cli_err float_opt_parser(cli_opt* opt, const char* token) {
float* val = (float*)(opt->value);
char* endptr;
*val = (float)strtof(token, &endptr);
if (endptr == token) {
return CLI_PARSE_FAILED_FLOAT;
}
return CLI_OK;
}
cli_err float_arg_parser(cli_arg* arg, const char* token) {
float* val = (float*)(arg->value);
char* endptr;
*val = (float)strtof(token, &endptr);
if (endptr == token) {
return CLI_PARSE_FAILED_FLOAT;
}
return CLI_OK;
}
cli_err int_opt_parser(cli_opt* opt, const char* token) {
int* val = (int*)(opt->value);
char* endptr;
*val = (int)strtol(token, &endptr, 10);
if (endptr == token) {
return CLI_PARSE_FAILED_INT;
}
return CLI_OK;
}
cli_err int_arg_parser(cli_arg* arg, const char* token) {
int* val = (int*)(arg->value);
char* endptr;
*val = (int)strtol(token, &endptr, 10);
if (endptr == token) {
return CLI_PARSE_FAILED_INT;
}
return CLI_OK;
}
cli_err bool_opt_parser(cli_opt* opt, const char* arg) {
bool* val = (bool*)opt->value;
// most commonly handle a switch case like (--verbose) by passing null arg
if (arg == NULL) {
if (*val == true) {
*val = false;
} else {
*val = true;
}
return CLI_OK;
}
return CLI_PARSE_FAILED_BOOL;
}
cli_err noop_parser(cli_opt* opt, const char* token) {
CLI_UNUSED(opt);
CLI_UNUSED(token);
return CLI_OK;
}
// High level API
typedef struct cli_command {
const char* desc;
const char* usage;
cli_opts* opts;
cli_args* args;
int argc;
char** argv;
str_boxes* sb;
} cli_command;
cli_command* cli_command_new(void) {
cli_command* c = (cli_command*)malloc(sizeof(cli_command));
CLI_CHECK_MEM_ALLOC(c);
return c;
}
cli_err cli_init(cli_command* cli,
const char* desc,
const char* usage,
int argc,
char** argv) {
cli->desc = desc;
cli->usage = usage;
cli->argc = argc;
cli->argv = argv;
// if we have opts allocate the requested amount
// we should always allocate 2 for optional help message flag `-h, --help`
cli_opts* opts = (cli_opts*)malloc(sizeof(cli_opts));
CLI_CHECK_MEM_ALLOC(opts);
cli_opts_init(opts, CLI_MAX_OPTS);
cli->opts = opts;
// help is really just used as token to break out of the parse.
// since we always add them we can simply print info to stderr later if -h or
// --help is raised.
cli_opts_add(opts, "h", "", noop_parser, NULL, false, true);
cli_opts_add(opts, "help", "", noop_parser, NULL, false, true);
cli_args* args = (cli_args*)malloc(sizeof(cli_args));
CLI_CHECK_MEM_ALLOC(args);
cli_args_init(args, CLI_MAX_ARGS);
cli->args = args;
// if every opt + arg is a string we would at most have MAX args and opts.
str_boxes* sb = (str_boxes*)malloc(sizeof(str_boxes));
str_boxes_init(sb, CLI_MAX_ARGS + CLI_MAX_OPTS);
cli->sb = sb;
return CLI_OK;
}
void cli_cleanup(cli_command* cli) {
if (cli->opts != NULL) {
cli_opts_cleanup(cli->opts);
free(cli->opts);
}
if (cli->args != NULL) {
cli_args_cleanup(cli->args);
free(cli->args);
}
if (cli->sb != NULL) {
str_boxes_cleanup(cli->sb);
free(cli->sb);
}
}
void cli_command_destroy(cli_command* c) {
cli_cleanup(c);
free(c);
}
// high level API for adding options and arguments
cli_err cli_add_flag(cli_command* cli,
const char* name,
const char* usage,
bool* value) {
return cli_opts_add(cli->opts, name, usage, bool_opt_parser, (void*)value,
false, true);
}
cli_err cli_add_int_argument(cli_command* cli, int* value) {
return cli_args_add(cli->args, int_arg_parser, (void*)value);
}
cli_err cli_add_int_option(cli_command* cli,
const char* name,
const char* usage,
int* value,
bool required) {
return cli_opts_add(cli->opts, name, usage, int_opt_parser, (void*)value,
required, false);
}
cli_err cli_add_float_argument(cli_command* cli, float* value) {
return cli_args_add(cli->args, float_arg_parser, (void*)value);
}
cli_err cli_add_float_option(cli_command* cli,
const char* name,
const char* usage,
float* value,
bool required) {
return cli_opts_add(cli->opts, name, usage, float_opt_parser, (void*)value,
required, false);
}
cli_err cli_add_str_argument(cli_command* cli, char* value, size_t buf_size) {
cli_err err;
str_box* box = NULL;
if ((err = str_boxes_add(cli->sb, value, buf_size, &box)) != CLI_OK) {
return err;
}
return cli_args_add(cli->args, str_arg_parser, (void*)box);
}
cli_err cli_add_str_option(cli_command* cli,
const char* name,
const char* usage,
char* value,
bool required,
size_t buf_size) {
cli_err err;
str_box* box = NULL;
if ((err = str_boxes_add(cli->sb, value, buf_size, &box)) != CLI_OK) {
return err;
}
return cli_opts_add(cli->opts, name, usage, str_opt_parser, (void*)box,
required, false);
}
void cli_print_help_and_exit(cli_command* cli, int status) {
char buf[1024];
char initial[] =
"%s\n\nUsage:\n\t%s %s\nOptions:\n\t-h,--help\tPrint usage and exit.\n";
snprintf(buf, 1024, initial, cli->desc, cli->argv[0], cli->usage);
// check for options and append
if (cli->opts != NULL) {
for (size_t i = 0; i < cli->opts->idx; i++) {
const char* name = cli->opts->opts[i]->name;
// printf("NAME: %s\n", name);
if ((strcmp(name, "help") == 0) || (strcmp(name, "h") == 0)) {
continue;
}
char opt_buf[CLI_OPT_TOKEN_MAX_LEN + CLI_OPT_USAGE_MAX_LEN];
cli_opt_print_message(cli->opts->opts[i], opt_buf);
strcat(buf, opt_buf);
}
}
fprintf(stderr, "%s\n", buf);
exit(status);
}
cli_err cli_parse(cli_command* cli) {
cli_err err = cli_parse_loop(cli->opts, cli->args, cli->argc, cli->argv);
if (err == CLI_PRINT_HELP_AND_EXIT) {
cli_print_help_and_exit(cli, 0);
}
return err;
}