Repository navigation
Expand file tree
/
Copy pathoptions.c
More file actions
454 lines (422 loc) · 18.4 KB
/
Copy pathoptions.c
File metadata and controls
454 lines (422 loc) · 18.4 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
#include "options.h"
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
static void fail(char *error, size_t size, const char *format, ...) {
va_list arguments;
va_start(arguments, format);
vsnprintf(error, size, format, arguments);
va_end(arguments);
}
static int same_name(const char *candidate, const char *name, size_t length) {
return candidate != NULL && strlen(candidate) == length &&
strncmp(candidate, name, length) == 0;
}
static const option_t *find_long(const option_t *table, size_t count, const char *name,
size_t length) {
for (size_t i = 0; i < count; i++)
if (same_name(table[i].name, name, length) || same_name(table[i].alias, name, length))
return &table[i];
return NULL;
}
/* Levenshtein, small and iterative, so a typo can be answered with the name the
* user probably meant instead of a bare refusal. */
static size_t edit_distance(const char *a, const char *b) {
size_t la = strlen(a), lb = strlen(b);
size_t previous[64], current[64];
if (lb + 1 > 64) return 64;
for (size_t j = 0; j <= lb; j++) previous[j] = j;
for (size_t i = 1; i <= la; i++) {
current[0] = i;
for (size_t j = 1; j <= lb; j++) {
size_t cost = a[i - 1] == b[j - 1] ? 0 : 1;
size_t best = previous[j] + 1;
if (current[j - 1] + 1 < best) best = current[j - 1] + 1;
if (previous[j - 1] + cost < best) best = previous[j - 1] + cost;
current[j] = best;
}
memcpy(previous, current, (lb + 1) * sizeof(*previous));
}
return previous[lb];
}
static const char *nearest_name(const option_t *table, size_t count, const char *name,
size_t length) {
char wanted[64];
const char *best = NULL;
size_t best_distance = 3; /* Further than two edits away is a different word. */
if (length + 1 > sizeof(wanted)) return NULL;
memcpy(wanted, name, length);
wanted[length] = '\0';
for (size_t i = 0; i < count; i++) {
size_t distance = edit_distance(wanted, table[i].name);
if (distance < best_distance) {
best_distance = distance;
best = table[i].name;
}
}
return best;
}
static const option_t *find_short(const option_t *table, size_t count, char shorthand) {
for (size_t i = 0; i < count; i++)
if (table[i].shorthand && table[i].shorthand == shorthand) return &table[i];
return NULL;
}
/* Long names read better in messages, so errors quote those even for -n. */
static options_status_t assign(const option_t *option, const char *text, char *error,
size_t error_size) {
if (option->kind == OPTION_FLAG || option->kind == OPTION_OFF) {
*(int *)option->target = option->kind == OPTION_FLAG;
return OPTIONS_OK;
}
if (option->kind == OPTION_STRING) {
*(const char **)option->target = text;
return OPTIONS_OK;
}
if (option->kind == OPTION_ENUM) {
for (int i = 0; option->names[i] != NULL; i++)
if (strcmp(option->names[i], text) == 0) {
*(int *)option->target = i;
return OPTIONS_OK;
}
size_t at = (size_t)snprintf(error, error_size, "--%s must be one of", option->name);
for (int i = 0; option->names[i] != NULL && at < error_size; i++)
at += (size_t)snprintf(error + at, error_size - at, "%s %s", i ? "," : "",
option->names[i]);
return OPTIONS_ERROR;
}
char *end;
errno = 0;
double value = strtod(text, &end);
/* strtod reads "nan" and "inf" as numbers. A NaN passes every comparison
* below by failing it, and would then be cast to an int, which is undefined. */
if (errno == ERANGE || end == text || *end != '\0' || !isfinite(value)) {
fail(error, error_size, "--%s wants a number, not '%s'", option->name, text);
return OPTIONS_ERROR;
}
if (value < option->minimum || value > option->maximum) {
if (option->kind == OPTION_INT)
fail(error, error_size, "--%s must be between %d and %d", option->name,
(int)option->minimum, (int)option->maximum);
else
fail(error, error_size, "--%s must be between %g and %g", option->name, option->minimum,
option->maximum);
return OPTIONS_ERROR;
}
if (option->kind == OPTION_INT) {
if (value != (double)(int)value) {
fail(error, error_size, "--%s wants a whole number, not '%s'", option->name, text);
return OPTIONS_ERROR;
}
*(int *)option->target = (int)value;
} else {
*(double *)option->target = value;
}
return OPTIONS_OK;
}
options_status_t options_parse(const option_t *table, size_t count, int argc, char **argv,
char *error, size_t error_size) {
if (table == NULL || argv == NULL || error == NULL || error_size == 0) return OPTIONS_ERROR;
error[0] = '\0';
for (int i = 1; i < argc; i++) {
const char *argument = argv[i];
if (strcmp(argument, "--") == 0) {
if (i + 1 < argc) {
fail(error, error_size, "unexpected argument '%s'", argv[i + 1]);
return OPTIONS_ERROR;
}
return OPTIONS_OK;
}
if (strcmp(argument, "-h") == 0) return OPTIONS_HELP;
if (strcmp(argument, "--help") == 0) return OPTIONS_HELP_FULL;
if (strcmp(argument, "--completion") == 0) {
if (i + 1 >= argc) {
fail(error, error_size, "--completion wants bash, zsh or fish");
return OPTIONS_ERROR;
}
snprintf(error, error_size, "%s", argv[i + 1]);
return OPTIONS_COMPLETION;
}
if (strcmp(argument, "-V") == 0 || strcmp(argument, "--version") == 0)
return OPTIONS_VERSION;
if (argument[0] != '-' || argument[1] == '\0') {
fail(error, error_size, "unexpected argument '%s'", argument);
return OPTIONS_ERROR;
}
if (argument[1] == '-') { /* Long form. */
const char *name = argument + 2;
const char *equals = strchr(name, '=');
size_t length = equals ? (size_t)(equals - name) : strlen(name);
/* --no-NAME clears a flag, which is how the off switches read. */
if (length > 3 && strncmp(name, "no-", 3) == 0) {
const option_t *option = find_long(table, count, name + 3, length - 3);
if (option != NULL && option->kind == OPTION_FLAG) {
if (equals) {
fail(error, error_size, "--%.*s takes no value", (int)length, name);
return OPTIONS_ERROR;
}
*(int *)option->target = 0;
continue;
}
}
const option_t *option = find_long(table, count, name, length);
if (option == NULL) {
const char *meant = nearest_name(table, count, name, length);
if (meant != NULL)
fail(error, error_size, "unknown option '--%.*s', did you mean '--%s'?",
(int)length, name, meant);
else
fail(error, error_size, "unknown option '--%.*s'", (int)length, name);
return OPTIONS_ERROR;
}
if (option->kind == OPTION_FLAG || option->kind == OPTION_OFF) {
if (equals) {
fail(error, error_size, "--%s takes no value", option->name);
return OPTIONS_ERROR;
}
*(int *)option->target = option->kind == OPTION_FLAG;
continue;
}
const char *text = equals ? equals + 1 : (++i < argc ? argv[i] : NULL);
if (text == NULL) {
fail(error, error_size, "--%s wants a value", option->name);
return OPTIONS_ERROR;
}
options_status_t status = assign(option, text, error, error_size);
if (status != OPTIONS_OK) return status;
continue;
}
/* Short form, and flags cluster: -qv, -qn800, -qn 800. */
for (const char *c = argument + 1; *c != '\0'; c++) {
/* h and V are not in the table — they are answered before any of it
* is read — so they have to be answered here too, or `-help`, which
* is a cluster of h e l p, is met with "unknown option '-h'": the
* program denying its own flag over the commonest typo there is. */
if (*c == 'h') return OPTIONS_HELP;
if (*c == 'V') return OPTIONS_VERSION;
const option_t *option = find_short(table, count, *c);
if (option == NULL) {
fail(error, error_size, "unknown option '-%c'", *c);
return OPTIONS_ERROR;
}
if (option->kind == OPTION_FLAG || option->kind == OPTION_OFF) {
*(int *)option->target = option->kind == OPTION_FLAG;
continue;
}
const char *text = c[1] != '\0' ? c + 1 : (++i < argc ? argv[i] : NULL);
if (text == NULL) {
fail(error, error_size, "-%c wants a value", *c);
return OPTIONS_ERROR;
}
options_status_t status = assign(option, text, error, error_size);
if (status != OPTIONS_OK) return status;
break; /* The rest of the cluster was the value. */
}
}
return OPTIONS_OK;
}
/* Exactly as wide as render_option writes it, so the two cannot disagree: two
* of indent, four for the shorthand slot whether or not there is one, the two
* dashes, the name, and the metavar with its space. */
static size_t option_width(const option_t *option) {
size_t width = 2 + 4 + 2 + strlen(option->name);
if (option->kind != OPTION_FLAG && option->kind != OPTION_OFF && option->metavar != NULL)
width += 1 + strlen(option->metavar);
return width;
}
static void render_option(FILE *out, size_t column, char shorthand, const char *name,
const char *metavar, const char *help) {
char left[96];
int at = snprintf(left, sizeof(left), " ");
if (shorthand)
at += snprintf(left + at, sizeof(left) - (size_t)at, "-%c, ", shorthand);
else
at += snprintf(left + at, sizeof(left) - (size_t)at, " ");
at += snprintf(left + at, sizeof(left) - (size_t)at, "--%s", name);
if (metavar != NULL) snprintf(left + at, sizeof(left) - (size_t)at, " %s", metavar);
fprintf(out, "%-*s%s\n", (int)column, left, help);
}
void options_usage(FILE *out, const char *program, const char *tagline,
const option_example_t *examples, const option_t *table, size_t count,
int everything) {
size_t column = strlen(" -V, --version");
for (size_t i = 0; i < count; i++) {
size_t width = option_width(&table[i]);
if (width > column) column = width;
}
column += 2;
if (tagline != NULL) fprintf(out, "%s\n\n", tagline);
fprintf(out, "Usage: %s [OPTIONS]\n", program);
const char *group = NULL;
for (size_t i = 0; i <= count; i++) {
const option_t *option = i < count ? &table[i] : NULL;
if (option != NULL && !everything && !option->essential) continue;
const char *next = option ? option->group : "General";
if (group == NULL || strcmp(group, next) != 0) {
fprintf(out, "\n%s\n", next);
group = next;
}
if (option == NULL) break;
render_option(
out, column, option->shorthand, option->name,
option->kind == OPTION_FLAG || option->kind == OPTION_OFF ? NULL : option->metavar,
option->help);
}
if (everything) {
render_option(out, column, 'h', "help", NULL, "the one-screen help");
render_option(out, column, 0, "completion", "SHELL", "completions for bash, zsh or fish");
render_option(out, column, 'V', "version", NULL, "print the version and quit");
} else {
render_option(out, column, 0, "help", NULL, "every option, grouped");
render_option(out, column, 'V', "version", NULL, "print the version and quit");
}
if (examples != NULL && everything) {
size_t widest = 0;
for (size_t i = 0; examples[i].command != NULL; i++) {
size_t length = strlen(examples[i].command);
if (length > widest) widest = length;
}
fprintf(out, "\nExamples\n");
for (size_t i = 0; examples[i].command != NULL; i++)
fprintf(out, " %-*s %s\n", (int)widest, examples[i].command, examples[i].what);
}
}
/* Help text goes inside a single quoted zsh spec, as the [description]: a quote
* would end the spec, as "each other's" did, and a bracket would end the
* description. */
static void zsh_description(FILE *out, const char *text) {
for (; *text != '\0'; text++) {
if (*text == '\'')
fputs("'\\''", out);
else if (*text == '[' || *text == ']')
fprintf(out, "\\%c", *text);
else
fputc(*text, out);
}
}
/* And inside a double quoted fish string, where these four mean something. */
static void fish_description(FILE *out, const char *text) {
for (; *text != '\0'; text++) {
if (*text == '"' || *text == '\\' || *text == '$') fputc('\\', out);
fputc(*text, out);
}
}
static int takes_value(const option_t *option) {
return option->kind != OPTION_FLAG && option->kind != OPTION_OFF;
}
/* What follows an option that takes a value in a zsh spec: its names when it is
* one of a list, a file when it is a file, and a bare message otherwise. */
static void zsh_value(FILE *out, const option_t *option) {
if (!takes_value(option)) return;
if (option->kind == OPTION_ENUM && option->names != NULL) {
fputs(":name:(", out);
for (int k = 0; option->names[k] != NULL; k++)
fprintf(out, "%s%s", k ? " " : "", option->names[k]);
fputc(')', out);
} else if (option->metavar != NULL && strcmp(option->metavar, "FILE") == 0) {
fputs(":file:_files", out);
} else {
fputs(":value:", out);
}
}
/* The switches every table gets without being in it, as the parser answers them
* and --help lists them: one list, so no shell can be left without one again. */
static const struct {
char shorthand;
const char *name;
const char *help;
} SPECIAL[] = {
{'h', NULL, "the one-screen help"},
{0, "help", "every option, grouped"},
{0, "completion", "completions for bash, zsh or fish"},
{'V', "version", "print the version and quit"},
};
enum { SPECIAL_COUNT = sizeof(SPECIAL) / sizeof(*SPECIAL) };
int options_completion(FILE *out, const char *shell, const char *program, const option_t *table,
size_t count) {
if (shell == NULL) return 0;
if (strcmp(shell, "bash") == 0) {
fprintf(out, "# %s completions for bash\ncomplete -W \"", program);
for (size_t i = 0; i < count; i++) {
fprintf(out, "--%s ", table[i].name);
if (table[i].shorthand) fprintf(out, "-%c ", table[i].shorthand);
}
for (size_t i = 0; i < SPECIAL_COUNT; i++) {
if (SPECIAL[i].shorthand) fprintf(out, "-%c ", SPECIAL[i].shorthand);
if (SPECIAL[i].name != NULL) fprintf(out, "--%s ", SPECIAL[i].name);
}
fprintf(out, "\" %s\n", program);
return 1;
}
if (strcmp(shell, "zsh") == 0) {
fprintf(out, "#compdef %s\n_arguments \\\n", program);
for (size_t i = 0; i < count; i++) {
fprintf(out, " '--%s[", table[i].name);
zsh_description(out, table[i].help);
fputc(']', out);
zsh_value(out, &table[i]);
fputs("' \\\n", out);
if (table[i].shorthand) {
fprintf(out, " '-%c[", table[i].shorthand);
zsh_description(out, table[i].help);
fputc(']', out);
zsh_value(out, &table[i]);
fputs("' \\\n", out);
}
}
for (size_t i = 0; i < SPECIAL_COUNT; i++) {
const char *more = i + 1 < SPECIAL_COUNT ? " \\" : "";
if (SPECIAL[i].shorthand)
fprintf(out, " '-%c[%s]'%s\n", SPECIAL[i].shorthand, SPECIAL[i].help,
SPECIAL[i].name != NULL ? " \\" : more);
if (SPECIAL[i].name != NULL)
fprintf(out, " '--%s[%s]%s'%s\n", SPECIAL[i].name, SPECIAL[i].help,
strcmp(SPECIAL[i].name, "completion") == 0 ? ":shell:(bash zsh fish)" : "",
more);
}
return 1;
}
if (strcmp(shell, "fish") == 0) {
for (size_t i = 0; i < count; i++) {
fprintf(out, "complete -c %s -l %s", program, table[i].name);
if (table[i].shorthand) fprintf(out, " -s %c", table[i].shorthand);
if (takes_value(&table[i])) fprintf(out, " -r");
if (table[i].kind == OPTION_ENUM && table[i].names != NULL) {
fprintf(out, " -f -a \"");
for (int k = 0; table[i].names[k] != NULL; k++)
fprintf(out, "%s%s", k ? " " : "", table[i].names[k]);
fprintf(out, "\"");
}
fprintf(out, " -d \"");
fish_description(out, table[i].help);
fprintf(out, "\"\n");
}
for (size_t i = 0; i < SPECIAL_COUNT; i++) {
fprintf(out, "complete -c %s", program);
if (SPECIAL[i].name != NULL) fprintf(out, " -l %s", SPECIAL[i].name);
if (SPECIAL[i].shorthand) fprintf(out, " -s %c", SPECIAL[i].shorthand);
if (SPECIAL[i].name != NULL && strcmp(SPECIAL[i].name, "completion") == 0)
fprintf(out, " -r -f -a \"bash zsh fish\"");
fprintf(out, " -d \"%s\"\n", SPECIAL[i].help);
}
return 1;
}
return 0;
}
const char *options_status_string(options_status_t status) {
switch (status) {
case OPTIONS_OK:
return "ok";
case OPTIONS_HELP:
case OPTIONS_HELP_FULL:
return "help requested";
case OPTIONS_COMPLETION:
return "completions requested";
case OPTIONS_VERSION:
return "version requested";
case OPTIONS_ERROR:
return "invalid arguments";
}
return "unknown error";
}