-
Notifications
You must be signed in to change notification settings - Fork 78
/
argp.c
172 lines (152 loc) · 5.53 KB
/
argp.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
/*
* file: argp.c
* description: minimal replacement for GNU Argp library
* Copyright 2011 Peter Desnoyers, Northeastern University
* Copyright 2022 Matthias Wirth
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "argp.h"
char *cleanarg(char *s)
{
char *v = strrchr(s, '/');
return v ? v+1 : s;
}
void argp_help(struct argp_state *state)
{
printf("Usage: %s [OPTIONS...] %s\n%s\n\n",
state->name, state->argp->arg_doc, state->argp->prog_doc);
struct argp_option *opt;
for (opt = state->argp->options; opt->name != NULL || opt->doc != NULL; opt++) {
if (opt->flags == OPTION_HIDDEN)
continue;
if (opt->name == NULL) {
printf("\n%s\n", opt->doc); // help category names
} else {
char tmp[80], *p = tmp;
p += sprintf(tmp, "--%s", opt->name);
if (opt->arg)
sprintf(p, "=%s", opt->arg);
printf(" %-*s%s\n", state->maxlen + 8, tmp, opt->doc);
}
}
printf(" --%-*s%s\n", state->maxlen+6, "help", "Give this help list");
printf(" --%-*s%s\n", state->maxlen+6, "usage",
"Give a short usage message");
printf("\nReport bugs to %s\n", argp_program_bug_address);
}
void argp_usage(struct argp_state *state)
{
char buf[64 * 1024], *p = buf, *col0 = buf;
p += sprintf(p, "Usage: %s", state->name);
int indent = p-buf;
struct argp_option *opt;
for (opt = state->argp->options; opt->name != NULL || opt->doc != NULL; opt++) {
if (opt->flags == OPTION_HIDDEN)
continue;
if (opt->name != NULL) {
p += sprintf(p, " [--%s%s%s]", opt->name, opt->arg ? "=":"",
opt->arg ? opt->arg : "");
if (p-col0 > (78-state->maxlen)) {
p += sprintf(p, "\n");
col0 = p;
p += sprintf(p, "%*s", indent, "");
}
}
}
sprintf(p, " %s\n", state->argp->arg_doc);
printf("%s", buf);
}
int argp_parse(struct argp *argp, int argc, char **argv, int flags, int tmp, void *input)
{
__VARNOTUSED(flags);
__VARNOTUSED(tmp);
int n_opts = 0;
struct argp_state state = {.name = cleanarg(argv[0]),
.input = input, .argp = argp};
state.arg_num = 0;
/* calculate max "--opt=var" length */
int max = 0;
struct argp_option *opt;
for (opt = argp->options; opt->name != NULL || opt->doc != NULL; opt++) {
if (opt->name != NULL) {
int m = strlen(opt->name) + (opt->arg ? 1+strlen(opt->arg) : 0);
max = (max < m) ? m : max;
}
n_opts++;
}
state.maxlen = max+2;
struct option *long_opts = calloc((n_opts+3) * sizeof(*long_opts), 1);
int argp_opt_index;
int long_count = 0;
for (opt = argp->options; opt->name != NULL || opt->doc != NULL; opt++) {
if (opt->name != NULL) {
//fprintf(stderr, "%d %s\n", opt->key, opt->name);
int has_arg = opt->arg != NULL;
long_opts[long_count].name = opt->name;
long_opts[long_count].has_arg = has_arg ? required_argument : no_argument;
long_opts[long_count].flag = &argp_opt_index;
long_opts[long_count].val = (opt - argp->options); // opt index
long_count++;
}
}
// deal with version / usage / help stuff
if (argc >= 2) {
if (strcmp(argv[1], "-V") == 0 || strcmp(argv[1], "--version") == 0) {
fprintf(stderr, "%s\n", argp_program_version);
exit(argc == 2 ? 0 : 1);
}
if (strcmp(argv[1], "-?") == 0 || strcmp(argv[1], "--help") == 0) {
argp_help(&state);
exit(argc == 2 ? 0 : 1);
}
if (strcmp(argv[1], "--usage") == 0) {
argp_usage(&state);
exit(argc == 2 ? 0 : 1);
}
}
/* we only accept long arguments - return value is zero, and 'long_index'
* gives us the index into 'long_opts[]'
*/
int long_index;
int c;
optind = 1;
while (argp_opt_index = -1, (c = getopt_long(argc, argv, "", long_opts, &long_index)) != -1) {
if (c == '?') {
fprintf(stderr, "Try `%s --help' or `%s --usage' for more information.\n", argv[0], argv[0]);
return 1;
}
if (c != 0) {
fprintf(stderr, "argp.c: unexpected getopt_long return value: %d %c\n", c, c);
fprintf(stderr, "Try `%s --help' or `%s --usage' for more information.\n", argv[0], argv[0]);
return 1;
}
if (argp_opt_index == -1) {
fprintf(stderr, "argp.c: unexpected code path re3Oovei\n");
fprintf(stderr, "Try `%s --help' or `%s --usage' for more information.\n", argv[0], argv[0]);
return 1;
}
//int index = optind - 1;
//fprintf(stderr, "option %d: %s %s\n", index, argv[index], optarg);
int res = argp->parser(argp->options[argp_opt_index].key, optarg, &state);
if (res == ARGP_ERR_UNKNOWN) {
return 1;
}
}
while (optind < argc) {
int res = argp->parser(ARGP_KEY_ARG, argv[optind++], &state);
if (res == ARGP_ERR_UNKNOWN) {
return 1;
}
}
argp->parser(ARGP_KEY_END, NULL, &state);
free(long_opts);
return 0;
}