blob: d2f8d3810ecfec5c0d1543c8499fe13cfc194d68 [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
134static void print_usage_and_die(gpr_cmdline *cl) {
135 /* TODO(ctiller): make this prettier */
136 arg *a;
137 const char *name = strrchr(cl->argv0, '/');
138 if (name) {
139 name++;
140 } else {
141 name = cl->argv0;
142 }
143 fprintf(stderr, "Usage: %s", name);
144 for (a = cl->args; a; a = a->next) {
145 switch (a->type) {
146 case ARGTYPE_BOOL:
147 fprintf(stderr, " [--%s|--no-%s]", a->name, a->name);
148 break;
149 case ARGTYPE_STRING:
150 fprintf(stderr, " [--%s=string]", a->name);
151 break;
152 case ARGTYPE_INT:
153 fprintf(stderr, " [--%s=int]", a->name);
154 break;
155 }
156 }
157 if (cl->extra_arg) {
158 fprintf(stderr, " [%s...]", cl->extra_arg_name);
159 }
160 fprintf(stderr, "\n");
161 exit(1);
162}
163
164static void extra_state(gpr_cmdline *cl, char *arg) {
165 if (!cl->extra_arg) print_usage_and_die(cl);
166 cl->extra_arg(cl->extra_arg_user_data, arg);
167}
168
169static arg *find_arg(gpr_cmdline *cl, char *name) {
170 arg *a;
171
172 for (a = cl->args; a; a = a->next) {
173 if (0 == strcmp(a->name, name)) {
174 break;
175 }
176 }
177
178 if (!a) {
179 fprintf(stderr, "Unknown argument: %s\n", name);
180 print_usage_and_die(cl);
181 }
182
183 return a;
184}
185
186static void value_state(gpr_cmdline *cl, char *arg) {
187 long intval;
188 char *end;
189
190 GPR_ASSERT(cl->cur_arg);
191
192 switch (cl->cur_arg->type) {
193 case ARGTYPE_INT:
194 intval = strtol(arg, &end, 0);
195 if (*end || intval < INT_MIN || intval > INT_MAX) {
196 fprintf(stderr, "expected integer, got '%s' for %s\n", arg,
197 cl->cur_arg->name);
198 print_usage_and_die(cl);
199 }
200 *(int *)cl->cur_arg->value = intval;
201 break;
202 case ARGTYPE_BOOL:
203 if (0 == strcmp(arg, "1") || 0 == strcmp(arg, "true")) {
204 *(int *)cl->cur_arg->value = 1;
205 } else if (0 == strcmp(arg, "0") || 0 == strcmp(arg, "false")) {
206 *(int *)cl->cur_arg->value = 0;
207 } else {
208 fprintf(stderr, "expected boolean, got '%s' for %s\n", arg,
209 cl->cur_arg->name);
210 print_usage_and_die(cl);
211 }
212 break;
213 case ARGTYPE_STRING:
214 *(char **)cl->cur_arg->value = arg;
215 break;
216 }
217
218 cl->state = normal_state;
219}
220
221static void normal_state(gpr_cmdline *cl, char *arg) {
222 char *eq = NULL;
223 char *tmp = NULL;
224 char *arg_name = NULL;
225
226 if (0 == strcmp(arg, "-help") || 0 == strcmp(arg, "--help") ||
227 0 == strcmp(arg, "-h")) {
228 print_usage_and_die(cl);
229 }
230
231 cl->cur_arg = NULL;
232
233 if (arg[0] == '-') {
234 if (arg[1] == '-') {
235 if (arg[2] == 0) {
236 /* handle '--' to move to just extra args */
237 cl->state = extra_state;
238 return;
239 }
240 arg += 2;
241 } else {
242 arg += 1;
243 }
244 /* first byte of arg is now past the leading '-' or '--' */
245 if (arg[0] == 'n' && arg[1] == 'o' && arg[2] == '-') {
246 /* arg is of the form '--no-foo' - it's a flag disable */
247 arg += 3;
248 cl->cur_arg = find_arg(cl, arg);
249 if (cl->cur_arg->type != ARGTYPE_BOOL) {
250 fprintf(stderr, "%s is not a flag argument\n", arg);
251 print_usage_and_die(cl);
252 }
253 *(int *)cl->cur_arg->value = 0;
254 return; /* early out */
255 }
256 eq = strchr(arg, '=');
257 if (eq != NULL) {
258 /* copy the string into a temp buffer and extract the name */
259 tmp = arg_name = gpr_malloc(eq - arg + 1);
260 memcpy(arg_name, arg, eq - arg);
261 arg_name[eq - arg] = 0;
262 } else {
263 arg_name = arg;
264 }
265 cl->cur_arg = find_arg(cl, arg_name);
266 if (eq != NULL) {
267 /* arg was of the type --foo=value, parse the value */
268 value_state(cl, eq + 1);
269 } else if (cl->cur_arg->type != ARGTYPE_BOOL) {
270 /* flag types don't have a '--foo value' variant, other types do */
271 cl->state = value_state;
272 } else {
273 /* flag parameter: just set the value */
274 *(int *)cl->cur_arg->value = 1;
275 }
276 } else {
277 extra_state(cl, arg);
278 }
279
280 gpr_free(tmp);
281}
282
283void gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv) {
284 int i;
285
286 GPR_ASSERT(argc >= 1);
287 cl->argv0 = argv[0];
288
289 for (i = 1; i < argc; i++) {
290 cl->state(cl, argv[i]);
291 }
Craig Tiller06059952015-02-18 08:34:56 -0800292}