64
|
1 |
# HG changeset patch |
|
2 |
# Parent 92fa48ef53c909928706ab4c51518953339a38e4 |
|
3 |
Unified command option parsing |
|
4 |
|
|
5 |
diff -r 92fa48ef53c9 mcabber/mcabber/utils.c |
|
6 |
--- a/mcabber/mcabber/utils.c Sun Jan 27 00:40:37 2013 +0200 |
|
7 |
+++ b/mcabber/mcabber/utils.c Sun Feb 24 04:24:14 2013 +0200 |
|
8 |
@@ -555,6 +555,311 @@ |
|
9 |
*str = tolower(*str); |
|
10 |
} |
|
11 |
|
|
12 |
+// FURTHER TODO: |
|
13 |
+// Allow to specify catchall argument in the middle of string (requires some reverse parser) |
|
14 |
+// Better error messages (caller frees them) |
|
15 |
+// --help generates error with short usage, based on info in options struct |
|
16 |
+ |
|
17 |
+// in_space -> in_space, in_optstart, in_argstart |
|
18 |
+// in_optstart -> in_shortoptend, in_longoptstart, in_argstart ('-') |
|
19 |
+// in_shortoptend -> in_space, error |
|
20 |
+// in_longoptstart -> in_longopt, in_space, in_argstart ('---') |
|
21 |
+// in_longopt -> in_longopt, in_space, error |
|
22 |
+// in_argstart -> in_arg, success |
|
23 |
+// in_arg -> in_arg, in_space, error |
|
24 |
+ |
|
25 |
+// arguments: rw buffer in utf8, end of buffer pointer, options description struct |
|
26 |
+static const char *cmdopts_parse_internal(gchar *arg, gchar *e, cmdopts_t *options) |
|
27 |
+{ |
|
28 |
+ // parser state |
|
29 |
+ enum { |
|
30 |
+ in_space, |
|
31 |
+ in_optstart, |
|
32 |
+ in_shortoptstart, |
|
33 |
+ in_shortoptend, |
|
34 |
+ in_longoptstart, |
|
35 |
+ in_longopt, |
|
36 |
+ in_argstart, |
|
37 |
+ in_arg, |
|
38 |
+ } state = in_space; |
|
39 |
+ // current pointer, start of object pointer |
|
40 |
+ gchar *p, *s; |
|
41 |
+ // |
|
42 |
+ gboolean quotes = FALSE; |
|
43 |
+ gboolean opts_ended = FALSE; |
|
44 |
+ // option, for which argument is currently parsed |
|
45 |
+ cmdopt_t *option = NULL; |
|
46 |
+ // argument, that we currently parse |
|
47 |
+ cmdarg_t *argument = NULL; |
|
48 |
+ // flags of option/argument |
|
49 |
+ guint flags = 0; |
|
50 |
+ // error message to return |
|
51 |
+ const char *error = NULL; |
|
52 |
+ |
|
53 |
+ p = arg; |
|
54 |
+ // we allow parser to do one extra run on final '\0' |
|
55 |
+ while (p <= e && error == NULL) { |
|
56 |
+ if (state == in_space) { // space between args/options |
|
57 |
+ if (*p == ' ' || *p == '\0') { // still space |
|
58 |
+ p ++; |
|
59 |
+ } else if (*p == '-' && !opts_ended) { // option |
|
60 |
+ state = in_optstart; |
|
61 |
+ p ++; |
|
62 |
+ } else { // argument |
|
63 |
+ if (!option) { |
|
64 |
+ opts_ended = TRUE; |
|
65 |
+ } |
|
66 |
+ s = p; |
|
67 |
+ state = in_argstart; |
|
68 |
+ } |
|
69 |
+ } else if (state == in_optstart) { // long/short option |
|
70 |
+ if (*p == ' ' || *p == '\0') { // argument '-' |
|
71 |
+ opts_ended = TRUE; |
|
72 |
+ s = p - 1; |
|
73 |
+ state = in_argstart; |
|
74 |
+ } else if (*p == '-') { // long option |
|
75 |
+ state = in_longoptstart; |
|
76 |
+ p ++; |
|
77 |
+ } else { // short option |
|
78 |
+ s = p; |
|
79 |
+ state = in_shortoptend; |
|
80 |
+ p ++; |
|
81 |
+ } |
|
82 |
+ } else if (state == in_shortoptend) { // short option |
|
83 |
+ if (*p == ' ' || *p == '\0') { // option really ended |
|
84 |
+ gboolean found = FALSE; |
|
85 |
+ option = options -> opts; |
|
86 |
+ if (option) { |
|
87 |
+ do { |
|
88 |
+ if (option -> shortopt == *s) { |
|
89 |
+ found = TRUE; |
|
90 |
+ break; |
|
91 |
+ } |
|
92 |
+ } while ((!(option++ -> flags & CMDOPT_LAST)) && !found); |
|
93 |
+ } |
|
94 |
+ if (found) { // option is known |
|
95 |
+ if (option -> flags & CMDOPT_SWITCH) { // it is switch |
|
96 |
+ if (option -> flags & CMDOPT_CATCHALL) { |
|
97 |
+ option -> value.swc ++; |
|
98 |
+ } else { |
|
99 |
+ option -> value.swc = !option -> value.swc; |
|
100 |
+ } |
|
101 |
+ option = NULL; |
|
102 |
+ } else { // it is option |
|
103 |
+ if (*p == '\0') { |
|
104 |
+ error = "Short option argument not specified"; |
|
105 |
+ } |
|
106 |
+ } |
|
107 |
+ state = in_space; |
|
108 |
+ p ++; |
|
109 |
+ } else { // option is unknown |
|
110 |
+ error = "Unknown short option"; |
|
111 |
+ } |
|
112 |
+ } else { // short option not ended |
|
113 |
+ error = "Extra characters at short option end"; |
|
114 |
+ } |
|
115 |
+ } else if (state == in_longoptstart) { // long option initialization |
|
116 |
+ if (*p == ' ' || *p == '\0') { // end of options '--' |
|
117 |
+ opts_ended = TRUE; |
|
118 |
+ state = in_space; |
|
119 |
+ p ++; |
|
120 |
+ } else if (*p == '-') { // argument, starting with '---' |
|
121 |
+ opts_ended = TRUE; |
|
122 |
+ s = p - 2; |
|
123 |
+ state = in_argstart; |
|
124 |
+ } else { // it is long option |
|
125 |
+ s = p; |
|
126 |
+ state = in_longopt; |
|
127 |
+ p ++; |
|
128 |
+ } |
|
129 |
+ } else if (state == in_longopt) { // long option name |
|
130 |
+ if (*p == ' ' || *p == '\0') { // long option ended |
|
131 |
+ gboolean found = FALSE; |
|
132 |
+ gboolean eof = *p == '\0'; |
|
133 |
+ *p = '\0'; |
|
134 |
+ option = options -> opts; |
|
135 |
+ if (option) { |
|
136 |
+ do { |
|
137 |
+ if (!g_strcmp0 (option -> longopt, s)) { |
|
138 |
+ found = TRUE; |
|
139 |
+ break; |
|
140 |
+ } |
|
141 |
+ } while ((!(option++ -> flags & CMDOPT_LAST)) && !found); |
|
142 |
+ } |
|
143 |
+ if (found) { // option is known |
|
144 |
+ if (option -> flags & CMDOPT_SWITCH) { // it is switch |
|
145 |
+ if (option -> flags & CMDOPT_CATCHALL) { |
|
146 |
+ option -> value.swc ++; |
|
147 |
+ } else { |
|
148 |
+ option -> value.swc = !option -> value.swc; |
|
149 |
+ } |
|
150 |
+ option = NULL; |
|
151 |
+ } else { // it is option |
|
152 |
+ if (eof) { |
|
153 |
+ error = "Long option argument not specified"; |
|
154 |
+ } |
|
155 |
+ } |
|
156 |
+ state = in_space; |
|
157 |
+ p ++; |
|
158 |
+ } else { // option is unknown |
|
159 |
+ error = "Unknown long option"; |
|
160 |
+ } |
|
161 |
+ } else { // still long option |
|
162 |
+ p ++; |
|
163 |
+ } |
|
164 |
+ } else if (state == in_argstart) { // option/command argument initialization |
|
165 |
+ if (option) { |
|
166 |
+ flags = option -> flags & ~CMDOPT_CATCHALL; // catchall in options indicates multi-options |
|
167 |
+ } else { |
|
168 |
+ if (!argument) { |
|
169 |
+ argument = options -> args; |
|
170 |
+ } |
|
171 |
+ if (!argument) { // no need to parse arguments at all |
|
172 |
+ break; |
|
173 |
+ } |
|
174 |
+ flags = argument -> flags; |
|
175 |
+ if ((flags & CMDOPT_CATCHALL) && (flags & CMDOPT_PLAIN)) { // can finish right away |
|
176 |
+ argument -> value.arg = s; |
|
177 |
+ break; |
|
178 |
+ } |
|
179 |
+ } |
|
180 |
+ quotes = FALSE; |
|
181 |
+ state = in_arg; |
|
182 |
+ } else if (state == in_arg) { // option/command argument value |
|
183 |
+ if (*p == '\0' && quotes) { // end of line in quotes |
|
184 |
+ error = "Unfinished quoted argument"; |
|
185 |
+ } else if ((*p == ' ' && (!quotes) && !(flags & CMDOPT_CATCHALL)) || *p == '\0') { // argument ended |
|
186 |
+ if (*p != '\0') { |
|
187 |
+ *p = '\0'; |
|
188 |
+ p ++; |
|
189 |
+ } |
|
190 |
+ if (option) { // option argument |
|
191 |
+ if (option -> flags & CMDOPT_CATCHALL) { // multi-value option |
|
192 |
+ option -> value.multiopt = g_slist_append (option -> value.multiopt, s); |
|
193 |
+ } else { // single-value option |
|
194 |
+ option -> value.opt = s; |
|
195 |
+ } |
|
196 |
+ option = NULL; |
|
197 |
+ } else { // command argument |
|
198 |
+ if (argument -> flags & CMDOPT_SUBCOMMAND) { |
|
199 |
+ gboolean found = FALSE; |
|
200 |
+ subcmd_t *subcommand = options -> cmds; |
|
201 |
+ if (subcommand) { |
|
202 |
+ do { |
|
203 |
+ if (!g_strcmp0(s, subcommand -> name)) { |
|
204 |
+ found = TRUE; |
|
205 |
+ break; |
|
206 |
+ } |
|
207 |
+ } while (!(subcommand++ -> flags & CMDOPT_LAST)); |
|
208 |
+ } |
|
209 |
+ if (found) { |
|
210 |
+ argument -> value.cmd = subcommand; |
|
211 |
+ error = cmdopts_parse_internal(p, e, subcommand -> options); |
|
212 |
+ break; |
|
213 |
+ } else { |
|
214 |
+ error = "Unknown subcommand"; |
|
215 |
+ } |
|
216 |
+ } else { |
|
217 |
+ argument -> value.arg = s; |
|
218 |
+ if (argument -> flags & CMDOPT_LAST) { // last argument |
|
219 |
+ break; |
|
220 |
+ } |
|
221 |
+ argument ++; |
|
222 |
+ } |
|
223 |
+ } |
|
224 |
+ state = in_space; |
|
225 |
+ p ++; |
|
226 |
+ } else if (*p == '\\' && !(flags & CMDOPT_PLAIN)) { // next char escape |
|
227 |
+ memmove(p, p+1, e-(p+1)); |
|
228 |
+ e --; |
|
229 |
+ if (*p == '\0') { |
|
230 |
+ error = "Escape at the end of line"; |
|
231 |
+ } |
|
232 |
+ p ++; |
|
233 |
+ } else if (*p == '"' && !(flags & CMDOPT_PLAIN)) { // quotation start/end |
|
234 |
+ memmove(p, p+1, e-(p+1)); |
|
235 |
+ e --; |
|
236 |
+ quotes = !quotes; |
|
237 |
+ } else { // still argument |
|
238 |
+ p ++; |
|
239 |
+ } |
|
240 |
+ } |
|
241 |
+ } |
|
242 |
+ |
|
243 |
+ // check required flags on options |
|
244 |
+ if (error == NULL && options -> opts) { |
|
245 |
+ option = options -> opts; |
|
246 |
+ do { |
|
247 |
+ if (option -> flags & CMDOPT_REQUIRED) { |
|
248 |
+ if (option -> flags & CMDOPT_SWITCH) { |
|
249 |
+ // no way to check trigger switches, but no point in it as well |
|
250 |
+ if (option -> flags & CMDOPT_CATCHALL && option -> value.swc == 0) { |
|
251 |
+ error = "Required switch is not specified"; |
|
252 |
+ break; |
|
253 |
+ } |
|
254 |
+ } else { |
|
255 |
+ if ((option -> flags & CMDOPT_CATCHALL && option -> value.multiopt == NULL) || |
|
256 |
+ ((!(option -> flags & CMDOPT_CATCHALL)) && option -> value.opt == NULL)) { |
|
257 |
+ error = "Required option is not specified"; |
|
258 |
+ break; |
|
259 |
+ } |
|
260 |
+ } |
|
261 |
+ } |
|
262 |
+ } while (!(option++ -> flags & CMDOPT_LAST)); |
|
263 |
+ } |
|
264 |
+ |
|
265 |
+ // check required flags on arguments |
|
266 |
+ if (error == NULL && options -> args) { |
|
267 |
+ argument = options -> args; |
|
268 |
+ do { |
|
269 |
+ if (argument -> flags & CMDOPT_REQUIRED) { |
|
270 |
+ if (argument -> flags & CMDOPT_SUBCOMMAND && argument -> value.cmd == NULL) { |
|
271 |
+ error = "Subcommand is not specified"; |
|
272 |
+ break; |
|
273 |
+ } |
|
274 |
+ } else { |
|
275 |
+ if ((!(argument -> flags & CMDOPT_SUBCOMMAND)) && argument -> value.arg == NULL) { |
|
276 |
+ error = "Required argument is not specified"; |
|
277 |
+ break; |
|
278 |
+ } |
|
279 |
+ } |
|
280 |
+ } while (!(argument++ -> flags & CMDOPT_LAST)); |
|
281 |
+ } |
|
282 |
+ |
|
283 |
+ return error; |
|
284 |
+} |
|
285 |
+ |
|
286 |
+const char *cmdopts_parse(const char *arg, cmdopts_t *options) |
|
287 |
+{ |
|
288 |
+ gchar *utf8 = to_utf8(arg); |
|
289 |
+ gchar *e; |
|
290 |
+ |
|
291 |
+ for (e = utf8; *e; e++); |
|
292 |
+ options -> freeme = utf8; |
|
293 |
+ return cmdopts_parse_internal(utf8, e, options); |
|
294 |
+} |
|
295 |
+ |
|
296 |
+void cmdopts_free(cmdopts_t *options) |
|
297 |
+{ |
|
298 |
+ cmdopt_t *option = options -> opts; |
|
299 |
+ subcmd_t *subcommand = options -> cmds; |
|
300 |
+ if (option) { |
|
301 |
+ do { |
|
302 |
+ if ((option -> flags & (CMDOPT_CATCHALL|CMDOPT_SWITCH)) == CMDOPT_CATCHALL) { |
|
303 |
+ g_slist_free(option -> value.multiopt); |
|
304 |
+ option -> value.multiopt = NULL; |
|
305 |
+ } |
|
306 |
+ } while (!(option++ -> flags & CMDOPT_LAST)); |
|
307 |
+ } |
|
308 |
+ if (subcommand) { |
|
309 |
+ do { |
|
310 |
+ cmdopts_free(subcommand -> options); |
|
311 |
+ } while (!(subcommand++ -> flags & CMDOPT_LAST)); |
|
312 |
+ } |
|
313 |
+ g_free(options -> freeme); |
|
314 |
+ options -> freeme = NULL; |
|
315 |
+} |
|
316 |
+ |
|
317 |
// strip_arg_special_chars(string) |
|
318 |
// Remove quotes and backslashes before an escaped quote |
|
319 |
// Only quotes need a backslash |
|
320 |
diff -r 92fa48ef53c9 mcabber/mcabber/utils.h |
|
321 |
--- a/mcabber/mcabber/utils.h Sun Jan 27 00:40:37 2013 +0200 |
|
322 |
+++ b/mcabber/mcabber/utils.h Sun Feb 24 04:24:14 2013 +0200 |
|
323 |
@@ -43,6 +43,93 @@ |
|
324 |
char **split_arg(const char *arg, unsigned int n, int dontstriplast); |
|
325 |
void free_arg_lst(char **arglst); |
|
326 |
|
|
327 |
+// error cmdopts_parse (argstring, optionlist) |
|
328 |
+// Function parses command argument string according to provided list of |
|
329 |
+// options and arguments. If in this process it encounters an error, it |
|
330 |
+// returns error string (that should be displayed and g_free'd afterwards). |
|
331 |
+// Note: For now returned error is constant string, that shouldn't be freed, |
|
332 |
+// but we're getting there. |
|
333 |
+// After processing you should free freeme and any GSList values of catchall |
|
334 |
+// options (only lists itself, not values). For your convenience, there is |
|
335 |
+// cmdopts_free(), that does exactly that. |
|
336 |
+// The function recognizes four kinds of expressions: |
|
337 |
+// - Options with arguments in a form '-f bar' or '--foo bar' |
|
338 |
+// - Switches without arguments in a form '-f' or '--foo' |
|
339 |
+// - End-of-options marker '--' |
|
340 |
+// - Individual arguments ('-' and '---' are considered arguments too) |
|
341 |
+// To define command line syntax, you pass cmdopts_t struct, that contains |
|
342 |
+// two contiguous lists of cmdopt_t and cmdarg_t structs accordingly. The |
|
343 |
+// last struct in list must have CMDOPT_LAST flag set. |
|
344 |
+// You can specify your own default values, they will be replaced/appended |
|
345 |
+// if needed. |
|
346 |
+// You can omit specifying longopt or shortopt (put NULL or '\0' there). |
|
347 |
+// Note: returned values and arguments are already converted to utf8. |
|
348 |
+ |
|
349 |
+// Flags: |
|
350 |
+// Only applies to options, defined if option does not have argument. |
|
351 |
+#define CMDOPT_SWITCH ( 0<<1 ) |
|
352 |
+// Don't process quotes and escapes in argument (applies to option arguments too). |
|
353 |
+#define CMDOPT_PLAIN ( 1<<1 ) |
|
354 |
+// For options - put all encountered values into GSList value.multiopt |
|
355 |
+// instead of overwriting value.opt. |
|
356 |
+// For switches - increment value.swc instead of logical flipping. |
|
357 |
+// For arguments - grab the rest of the line without splitting on spaces. |
|
358 |
+// Implicitly last argument. |
|
359 |
+#define CMDOPT_CATCHALL ( 2<<1 ) |
|
360 |
+// Option/argument must have value. |
|
361 |
+#define CMDOPT_REQUIRED ( 3<<1 ) |
|
362 |
+// Last entry in struct sequence. |
|
363 |
+#define CMDOPT_LAST ( 4<<1 ) |
|
364 |
+// Argument only, argument is the name for subcommand. |
|
365 |
+// Implicitly last argument. |
|
366 |
+#define CMDOPT_SUBCOMMAND ( 5<<1 ) |
|
367 |
+ |
|
368 |
+// thoughts about future: |
|
369 |
+// command struct contains cmdopts |
|
370 |
+// cmdopt/cmdarg struct contains argument type, that implies completion id and argument correctness checks |
|
371 |
+// cmdopt/cmdarg struct contains default value |
|
372 |
+// when building completion for command, we allow options (if not before --) |
|
373 |
+// would be good to have 'subcommands' mcabber commands |
|
374 |
+// |
|
375 |
+// so, the process of command execution looks like: |
|
376 |
+// - we walk through the options, set default values |
|
377 |
+// - we parse argument string, populating options |
|
378 |
+// - we check for required options availability |
|
379 |
+// - we call callback |
|
380 |
+// - we free resources |
|
381 |
+typedef struct cmdopts_struct cmdopts_t; |
|
382 |
+typedef struct { |
|
383 |
+ guint flags; |
|
384 |
+ const char *name; |
|
385 |
+ cmdopts_t *options; |
|
386 |
+} subcmd_t; |
|
387 |
+typedef struct { |
|
388 |
+ guint flags; |
|
389 |
+ char shortopt; |
|
390 |
+ const char *longopt; |
|
391 |
+ union { |
|
392 |
+ GSList *multiopt; |
|
393 |
+ gchar *opt; |
|
394 |
+ guint swc; |
|
395 |
+ } value; |
|
396 |
+} cmdopt_t; |
|
397 |
+typedef struct { |
|
398 |
+ guint flags; |
|
399 |
+ union { |
|
400 |
+ gchar *arg; |
|
401 |
+ subcmd_t *cmd; |
|
402 |
+ } value; |
|
403 |
+} cmdarg_t; |
|
404 |
+struct cmdopts_struct { |
|
405 |
+ cmdopt_t *opts; |
|
406 |
+ cmdarg_t *args; |
|
407 |
+ subcmd_t *cmds; |
|
408 |
+ gchar *freeme; |
|
409 |
+}; |
|
410 |
+ |
|
411 |
+const char *cmdopts_parse (const char *arg, cmdopts_t *options); |
|
412 |
+void cmdopts_free(cmdopts_t *options); |
|
413 |
+ |
|
414 |
void replace_nl_with_dots(char *bufstr); |
|
415 |
char *ut_expand_tabs(const char *text); |
|
416 |
char *ut_unescape_tabs_cr(const char *text); |