blob: 530952c4371047ed40fd7ae7db7f9ff9709189f5 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
nnoble9f312b42015-01-07 01:19:35 -080034#include <grpc/support/cmdline.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36#include <limits.h>
37#include <stdio.h>
38#include <string.h>
39
Craig Tiller485d7762015-01-23 12:54:05 -080040#include "src/core/support/string.h"
nnoble9f312b42015-01-07 01:19:35 -080041#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
44typedef enum { ARGTYPE_INT, ARGTYPE_BOOL, ARGTYPE_STRING } argtype;
45
46typedef struct arg {
47 const char *name;
48 const char *help;
49 argtype type;
50 void *value;
51 struct arg *next;
52} arg;
53
54struct gpr_cmdline {
55 const char *description;
56 arg *args;
57 const char *argv0;
58
59 const char *extra_arg_name;
60 const char *extra_arg_help;
61 void (*extra_arg)(void *user_data, const char *arg);
62 void *extra_arg_user_data;
63
64 void (*state)(gpr_cmdline *cl, char *arg);
65 arg *cur_arg;
66};
67
68static void normal_state(gpr_cmdline *cl, char *arg);
69
70gpr_cmdline *gpr_cmdline_create(const char *description) {
71 gpr_cmdline *cl = gpr_malloc(sizeof(gpr_cmdline));
72 memset(cl, 0, sizeof(gpr_cmdline));
73
74 cl->description = description;
75 cl->state = normal_state;
76
77 return cl;
78}
79
80void gpr_cmdline_destroy(gpr_cmdline *cl) {
81 while (cl->args) {
82 arg *a = cl->args;
83 cl->args = a->next;
84 gpr_free(a);
85 }
86 gpr_free(cl);
87}
88
89static void add_arg(gpr_cmdline *cl, const char *name, const char *help,
90 argtype type, void *value) {
91 arg *a;
92
93 for (a = cl->args; a; a = a->next) {
94 GPR_ASSERT(0 != strcmp(a->name, name));
95 }
96
97 a = gpr_malloc(sizeof(arg));
98 memset(a, 0, sizeof(arg));
99 a->name = name;
100 a->help = help;
101 a->type = type;
102 a->value = value;
103 a->next = cl->args;
104 cl->args = a;
105}
106
107void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name, const char *help,
108 int *value) {
109 add_arg(cl, name, help, ARGTYPE_INT, value);
110}
111
112void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help,
113 int *value) {
114 add_arg(cl, name, help, ARGTYPE_BOOL, value);
115}
116
117void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help,
118 char **value) {
119 add_arg(cl, name, help, ARGTYPE_STRING, value);
120}
121
122void gpr_cmdline_on_extra_arg(
123 gpr_cmdline *cl, const char *name, const char *help,
124 void (*on_extra_arg)(void *user_data, const char *arg), void *user_data) {
125 GPR_ASSERT(!cl->extra_arg);
126 GPR_ASSERT(on_extra_arg);
127
128 cl->extra_arg = on_extra_arg;
129 cl->extra_arg_user_data = user_data;
130 cl->extra_arg_name = name;
131 cl->extra_arg_help = help;
132}
133
Craig Tillere7023612015-05-28 08:00:14 -0700134/* recursively descend argument list, adding the last element
135 to s first - so that arguments are added in the order they were
136 added to the list by api calls */
137static void add_args_to_usage(gpr_strvec *s, arg *a) {
138 char *tmp;
139
140 if (!a) return;
141 add_args_to_usage(s, a->next);
142
143 switch (a->type) {
144 case ARGTYPE_BOOL:
145 gpr_asprintf(&tmp, " [--%s|--no-%s]", a->name, a->name);
146 gpr_strvec_add(s, tmp);
147 break;
148 case ARGTYPE_STRING:
149 gpr_asprintf(&tmp, " [--%s=string]", a->name);
150 gpr_strvec_add(s, tmp);
151 break;
152 case ARGTYPE_INT:
153 gpr_asprintf(&tmp, " [--%s=int]", a->name);
154 gpr_strvec_add(s, tmp);
155 break;
156 }
157}
158
159char *gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 /* TODO(ctiller): make this prettier */
Craig Tillere7023612015-05-28 08:00:14 -0700161 gpr_strvec s;
162 char *tmp;
163 const char *name = strrchr(argv0, '/');
164
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165 if (name) {
166 name++;
167 } else {
Craig Tillere7023612015-05-28 08:00:14 -0700168 name = argv0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 }
Craig Tillere7023612015-05-28 08:00:14 -0700170
171 gpr_strvec_init(&s);
172
173 gpr_asprintf(&tmp, "Usage: %s", name);
174 gpr_strvec_add(&s, tmp);
175 add_args_to_usage(&s, cl->args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176 if (cl->extra_arg) {
Craig Tillere7023612015-05-28 08:00:14 -0700177 gpr_asprintf(&tmp, " [%s...]", cl->extra_arg_name);
178 gpr_strvec_add(&s, tmp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800179 }
Craig Tillere7023612015-05-28 08:00:14 -0700180 gpr_strvec_add(&s, gpr_strdup("\n"));
181
182 tmp = gpr_strvec_flatten(&s, NULL);
183 gpr_strvec_destroy(&s);
184 return tmp;
185}
186
187static void print_usage_and_die(gpr_cmdline *cl) {
188 char *usage = gpr_cmdline_usage_string(cl, cl->argv0);
189 fprintf(stderr, "%s", usage);
190 gpr_free(usage);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800191 exit(1);
192}
193
194static void extra_state(gpr_cmdline *cl, char *arg) {
195 if (!cl->extra_arg) print_usage_and_die(cl);
196 cl->extra_arg(cl->extra_arg_user_data, arg);
197}
198
199static arg *find_arg(gpr_cmdline *cl, char *name) {
200 arg *a;
201
202 for (a = cl->args; a; a = a->next) {
203 if (0 == strcmp(a->name, name)) {
204 break;
205 }
206 }
207
208 if (!a) {
209 fprintf(stderr, "Unknown argument: %s\n", name);
210 print_usage_and_die(cl);
211 }
212
213 return a;
214}
215
216static void value_state(gpr_cmdline *cl, char *arg) {
217 long intval;
218 char *end;
219
220 GPR_ASSERT(cl->cur_arg);
221
222 switch (cl->cur_arg->type) {
223 case ARGTYPE_INT:
224 intval = strtol(arg, &end, 0);
225 if (*end || intval < INT_MIN || intval > INT_MAX) {
226 fprintf(stderr, "expected integer, got '%s' for %s\n", arg,
227 cl->cur_arg->name);
228 print_usage_and_die(cl);
229 }
230 *(int *)cl->cur_arg->value = intval;
231 break;
232 case ARGTYPE_BOOL:
233 if (0 == strcmp(arg, "1") || 0 == strcmp(arg, "true")) {
234 *(int *)cl->cur_arg->value = 1;
235 } else if (0 == strcmp(arg, "0") || 0 == strcmp(arg, "false")) {
236 *(int *)cl->cur_arg->value = 0;
237 } else {
238 fprintf(stderr, "expected boolean, got '%s' for %s\n", arg,
239 cl->cur_arg->name);
240 print_usage_and_die(cl);
241 }
242 break;
243 case ARGTYPE_STRING:
244 *(char **)cl->cur_arg->value = arg;
245 break;
246 }
247
248 cl->state = normal_state;
249}
250
251static void normal_state(gpr_cmdline *cl, char *arg) {
252 char *eq = NULL;
253 char *tmp = NULL;
254 char *arg_name = NULL;
255
256 if (0 == strcmp(arg, "-help") || 0 == strcmp(arg, "--help") ||
257 0 == strcmp(arg, "-h")) {
258 print_usage_and_die(cl);
259 }
260
261 cl->cur_arg = NULL;
262
263 if (arg[0] == '-') {
264 if (arg[1] == '-') {
265 if (arg[2] == 0) {
266 /* handle '--' to move to just extra args */
267 cl->state = extra_state;
268 return;
269 }
270 arg += 2;
271 } else {
272 arg += 1;
273 }
274 /* first byte of arg is now past the leading '-' or '--' */
275 if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-') {
276 /* arg is of the form '--no-foo' - it's a flag disable */
277 arg += 3;
278 cl->cur_arg = find_arg(cl, arg);
279 if (cl->cur_arg->type != ARGTYPE_BOOL) {
280 fprintf(stderr, "%s is not a flag argument\n", arg);
281 print_usage_and_die(cl);
282 }
283 *(int *)cl->cur_arg->value = 0;
284 return; /* early out */
285 }
286 eq = strchr(arg, '=');
287 if (eq != NULL) {
288 /* copy the string into a temp buffer and extract the name */
289 tmp = arg_name = gpr_malloc(eq - arg + 1);
290 memcpy(arg_name, arg, eq - arg);
291 arg_name[eq - arg] = 0;
292 } else {
293 arg_name = arg;
294 }
295 cl->cur_arg = find_arg(cl, arg_name);
296 if (eq != NULL) {
297 /* arg was of the type --foo=value, parse the value */
298 value_state(cl, eq + 1);
299 } else if (cl->cur_arg->type != ARGTYPE_BOOL) {
300 /* flag types don't have a '--foo value' variant, other types do */
301 cl->state = value_state;
302 } else {
303 /* flag parameter: just set the value */
304 *(int *)cl->cur_arg->value = 1;
305 }
306 } else {
307 extra_state(cl, arg);
308 }
309
310 gpr_free(tmp);
311}
312
313void gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv) {
314 int i;
315
316 GPR_ASSERT(argc >= 1);
317 cl->argv0 = argv[0];
318
319 for (i = 1; i < argc; i++) {
320 cl->state(cl, argv[i]);
321 }
Craig Tiller190d3602015-02-18 09:23:38 -0800322}