-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnot_enough_cli.c
More file actions
227 lines (199 loc) · 5.71 KB
/
not_enough_cli.c
File metadata and controls
227 lines (199 loc) · 5.71 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
#include "not_enough_cli.h"
#include <bits/getopt_ext.h>
#include <err.h>
#include <getopt.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OPT_ARG_STR "[=ARG]"
#define REQ_ARG_STR "=ARG"
#define SKIP_WS(s) ((s) += strspn((s), " \n\t\r\f\v"))
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define STRLITLEN(s) (ARRAY_SIZE(s) - 1)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
typedef struct option opt;
static char opts_set = 0;
static opt *options_long_ = NULL;
static char *options_short = NULL;
static char **options_help_ = NULL;
static char *prog_name_ = NULL;
static int smallest_option = 0;
static int largest_option = 0;
static int option_count = 0;
static int *options = NULL;
void nac_set_opt(char opt) { options[((unsigned)opt) - smallest_option]++; }
int nac_get_opt(char opt) { return options[((unsigned)opt) - smallest_option]; }
void nac_set_opts(char *prog_name, opt *options_long, char **options_help) {
if (opts_set) {
errx(EXIT_FAILURE, "not_enough_cli: cannot set opts more than once");
}
opts_set = 1;
options_long_ = options_long;
options_help_ = options_help;
prog_name_ = prog_name;
smallest_option = 255;
largest_option = 0;
option_count = 0;
int options_short_len = 1;
for (opt *opt = options_long_; opt->name != NULL; opt++) {
smallest_option = MIN(smallest_option, opt->val);
largest_option = MAX(largest_option, opt->val);
options_short_len += 1 + (unsigned)opt->has_arg;
option_count++;
}
if (option_count <= 0) {
errx(EXIT_FAILURE, "not_enough_cli: number of options cannot be 0");
}
options = calloc(largest_option - smallest_option + 1, sizeof(int));
options_short = malloc(options_short_len + 1);
char *s = options_short;
*s++ = ':';
for (opt *opt = options_long_; opt->name != NULL; opt++) {
*s++ = (char)opt->val;
switch (opt->has_arg) {
case optional_argument:
*s++ = ':';
case required_argument:
*s++ = ':';
}
}
*s++ = '\0';
}
void nac_cleanup() {
free(options);
free(options_short);
options = NULL;
options_short = NULL;
}
void nac_print_usage_header(FILE *fout, char *opts) {
fprintf(fout, "Usage: %s %s\n", prog_name_, opts);
}
void nac_print_options(FILE *fout) {
int longest_opt_len = 0;
for (opt *opt = options_long_; opt->name != NULL; opt++) {
int opt_len = strlen(opt->name);
switch (opt->has_arg) {
case no_argument:
break;
case optional_argument:
opt_len += STRLITLEN(OPT_ARG_STR);
break;
case required_argument:
opt_len += STRLITLEN(REQ_ARG_STR);
break;
}
if (opt_len > longest_opt_len) {
longest_opt_len = opt_len;
}
}
for (opt *opt = options_long_; opt->name != NULL; opt++) {
fprintf(fout, " -%c, --%s", opt->val, opt->name);
int opt_len = strlen(opt->name);
switch (opt->has_arg) {
case no_argument:
break;
case optional_argument:
opt_len += fprintf(fout, OPT_ARG_STR);
break;
case required_argument:
opt_len += fprintf(fout, REQ_ARG_STR);
break;
}
fprintf(fout, "%*s %s\n", longest_opt_len - opt_len, "",
options_help_[opt->val]);
}
}
static const char *opt_get_long(char short_opt) {
for (opt *opt = options_long_; opt->name != NULL; opt++) {
if (opt->val == short_opt) {
return opt->name;
}
}
errx(EXIT_FAILURE,
"not_enough_cli: cannot get long version of non-existing short option "
"'%c'",
short_opt);
}
static char *opt_format(char short_opt) {
static char bufs[2][32];
static unsigned buf_index;
char *buf = bufs[buf_index++];
const char *long_opt = opt_get_long(short_opt);
snprintf(buf, 32, "%s%s%s-%c", long_opt[0] != '\0' ? "--" : "", long_opt,
long_opt[0] != '\0' ? "/" : "", short_opt);
return buf;
}
void nac_opt_check_excl(char *opts) {
for (; *opts != '\0'; opts++) {
if (!nac_get_opt(*opts)) {
continue;
}
for (int i = smallest_option; i <= largest_option; i++) {
if (i == *opts) {
continue;
}
if (nac_get_opt(i)) {
errx(EXIT_FAILURE, "%s can be given only by itself", opt_format(*opts));
}
}
}
}
void nac_opt_check_mut_excl(char *opts, char *other_opts) {
while (*opts != '\0') {
if (!nac_get_opt(*opts)) {
return;
}
while (*other_opts != '\0') {
if (nac_get_opt((unsigned)*other_opts)) {
errx(EXIT_FAILURE, "%s and %s are mutually exclusive",
opt_format(*opts), opt_format(*other_opts));
}
other_opts++;
}
opts++;
}
}
void nac_opt_check_max_once(char *opts) {
while (*opts != '\0') {
if (nac_get_opt(*opts) > 1) {
errx(EXIT_FAILURE, "%s cannot be used multiple times", opt_format(*opts));
}
opts++;
}
}
char *nac_optarg_trimmed() { return SKIP_WS(optarg); }
_Noreturn void nac_missing_arg(int opt) {
errx(EXIT_FAILURE, "\"%s\" requires an argument",
opt_format(opt == ':' ? optopt : opt));
}
_Noreturn static void nac_invalid_opt(char **argv) {
const char *invalid_opt = (argv)[optind - 1];
if (invalid_opt != NULL && strncmp(invalid_opt, "--", 2) == 0) {
fprintf(stderr, "\"%s\": invalid option", invalid_opt + 2);
} else {
fprintf(stderr, "'%c': invalid option", optopt);
}
fputs("; use --help or -h for help\n", stderr);
exit(EXIT_FAILURE);
}
void nac_simple_parse_args(int *argc, char ***argv, void (*cb)(char)) {
for (;;) {
int opt = getopt_long(*argc, *argv, options_short, options_long_, NULL);
if (opt == -1) {
break;
}
switch (opt) {
case ':':
nac_missing_arg(opt);
case '?':
nac_invalid_opt(*argv);
default:
nac_set_opt(opt);
cb(opt);
}
}
*argc -= optind;
*argv += optind;
}