MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
getarg.c
1 /* -*- c-basic-offset: 4; -*- */
2 /*
3  * Copyright (C) 2003-2005 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
4  * (Royal Institute of Technology, Stockholm, Sweden).
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <ndb_global.h>
36 
37 #include "getarg.h"
38 #include <basestring_vsnprintf.h>
39 
40 #ifndef HAVE_STRLCPY
41 static size_t
42 strlcpy (char *dst, const char *src, size_t dst_sz)
43 {
44  size_t n;
45  char *p;
46  for (p = dst, n = 0;
47  n + 1 < dst_sz && *src != '\0';
48  ++p, ++src, ++n)
49  *p = *src;
50  *p = '\0';
51  if (*src == '\0')
52  return n;
53  else
54  return n + strlen (src);
55 }
56 #endif
57 #ifndef HAVE_STRLCAT
58 static size_t
59 strlcat (char *dst, const char *src, size_t dst_sz)
60 {
61  size_t len = strlen(dst);
62  return len + strlcpy (dst + len, src, dst_sz - len);
63 }
64 #endif
65 
66 #define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag)
67 
68 #ifndef max
69 #define max(a, b) (a) > (b) ? (a) : (b)
70 #endif
71 
72 #ifdef HAVE___PROGNAME
73 extern char *__progname;
74 #endif
75 
76 #ifndef TRUE
77 #define TRUE 1
78 #endif
79 
80 #ifndef FALSE
81 #define FALSE 0
82 #endif
83 
84 char *
85 strupr(char *str)
86 {
87  char *s;
88 
89  for(s = str; *s; s++)
90  *s = toupper(*s);
91  return str;
92 }
93 
94 static size_t
95 print_arg (char *string, size_t len, int mdoc, int longp, struct getargs *arg)
96 {
97  const char *s;
98 
99  *string = '\0';
100 
101  if (ISFLAG(*arg) || (!longp && arg->type == arg_counter))
102  return 0;
103 
104  if(mdoc){
105  if(longp)
106  strlcat(string, "= Ns", len);
107  strlcat(string, " Ar ", len);
108  }else
109  if (longp)
110  strlcat (string, "=", len);
111  else
112  strlcat (string, " ", len);
113 
114  if (arg->arg_help)
115  s = arg->arg_help;
116  else if (arg->type == arg_integer || arg->type == arg_counter)
117  s = "integer";
118  else if (arg->type == arg_string)
119  s = "string";
120  else if (arg->type == arg_double)
121  s = "float";
122  else
123  s = "<undefined>";
124 
125  strlcat(string, s, len);
126  return 1 + strlen(s);
127 }
128 
129 #ifdef GETARGMANDOC
130 static void
131 mandoc_template(struct getargs *args,
132  size_t num_args,
133  const char *progname,
134  const char *extra_string)
135 {
136  size_t i;
137  char timestr[64], cmd[64];
138  char buf[128];
139  const char *p;
140  time_t t;
141 
142  printf(".\\\" Things to fix:\n");
143  printf(".\\\" * correct section, and operating system\n");
144  printf(".\\\" * remove Op from mandatory flags\n");
145  printf(".\\\" * use better macros for arguments (like .Pa for files)\n");
146  printf(".\\\"\n");
147  t = time(NULL);
148  strftime(timestr, sizeof(timestr), "%B %e, %Y", localtime(&t));
149  printf(".Dd %s\n", timestr);
150  p = strrchr(progname, '/');
151  if(p) p++; else p = progname;
152  strlcpy(cmd, p, sizeof(cmd));
153  strupr(cmd);
154 
155  printf(".Dt %s SECTION\n", cmd);
156  printf(".Os OPERATING_SYSTEM\n");
157  printf(".Sh NAME\n");
158  printf(".Nm %s\n", p);
159  printf(".Nd\n");
160  printf("in search of a description\n");
161  printf(".Sh SYNOPSIS\n");
162  printf(".Nm\n");
163  for(i = 0; i < num_args; i++){
164  /* we seem to hit a limit on number of arguments if doing
165  short and long flags with arguments -- split on two lines */
166  if(ISFLAG(args[i]) ||
167  args[i].short_name == 0 || args[i].long_name == NULL) {
168  printf(".Op ");
169 
170  if(args[i].short_name) {
171  print_arg(buf, sizeof(buf), 1, 0, args + i);
172  printf("Fl %c%s", args[i].short_name, buf);
173  if(args[i].long_name)
174  printf(" | ");
175  }
176  if(args[i].long_name) {
177  print_arg(buf, sizeof(buf), 1, 1, args + i);
178  printf("Fl -%s%s%s",
179  args[i].type == arg_negative_flag ? "no-" : "",
180  args[i].long_name, buf);
181  }
182  printf("\n");
183  } else {
184  print_arg(buf, sizeof(buf), 1, 0, args + i);
185  printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf);
186  print_arg(buf, sizeof(buf), 1, 1, args + i);
187  printf(".Fl -%s%s Oc\n.Xc\n", args[i].long_name, buf);
188  }
189  /*
190  if(args[i].type == arg_strings)
191  fprintf (stderr, "...");
192  */
193  }
194  if (extra_string && *extra_string)
195  printf (".Ar %s\n", extra_string);
196  printf(".Sh DESCRIPTION\n");
197  printf("Supported options:\n");
198  printf(".Bl -tag -width Ds\n");
199  for(i = 0; i < num_args; i++){
200  printf(".It Xo\n");
201  if(args[i].short_name){
202  printf(".Fl %c", args[i].short_name);
203  print_arg(buf, sizeof(buf), 1, 0, args + i);
204  printf("%s", buf);
205  if(args[i].long_name)
206  printf(" Ns ,");
207  printf("\n");
208  }
209  if(args[i].long_name){
210  printf(".Fl -%s%s",
211  args[i].type == arg_negative_flag ? "no-" : "",
212  args[i].long_name);
213  print_arg(buf, sizeof(buf), 1, 1, args + i);
214  printf("%s\n", buf);
215  }
216  printf(".Xc\n");
217  if(args[i].help)
218  printf("%s\n", args[i].help);
219  /*
220  if(args[i].type == arg_strings)
221  fprintf (stderr, "...");
222  */
223  }
224  printf(".El\n");
225  printf(".\\\".Sh ENVIRONMENT\n");
226  printf(".\\\".Sh FILES\n");
227  printf(".\\\".Sh EXAMPLES\n");
228  printf(".\\\".Sh DIAGNOSTICS\n");
229  printf(".\\\".Sh SEE ALSO\n");
230  printf(".\\\".Sh STANDARDS\n");
231  printf(".\\\".Sh HISTORY\n");
232  printf(".\\\".Sh AUTHORS\n");
233  printf(".\\\".Sh BUGS\n");
234 }
235 #endif /* GETARGMANDOC */
236 
237 static int
238 check_column(FILE *f, int col, int len, int columns)
239 {
240  if(col + len > columns) {
241  fprintf(f, "\n");
242  col = fprintf(f, " ");
243  }
244  return col;
245 }
246 
247 void
248 arg_printusage (struct getargs *args,
249  size_t num_args,
250  const char *progname,
251  const char *extra_string)
252 {
253  unsigned int i;
254  size_t max_len = 0;
255  char buf[128];
256  int col = 0, columns;
257 
258 #ifdef HAVE___PROGNAME
259  if (progname == NULL)
260  progname = __progname;
261 #endif
262  if (progname == NULL)
263  progname = "";
264 
265 #ifdef GETARGMANDOC
266  if(getenv("GETARGMANDOC")){
267  mandoc_template(args, num_args, progname, extra_string);
268  return;
269  }
270 #endif
271 
272  columns = 80; /* Always assume that the window is 80 chars wide */
273  col = 0;
274  col += fprintf (stderr, "Usage: %s", progname);
275  for (i = 0; i < num_args; ++i) {
276  size_t len = 0;
277 
278  if (args[i].long_name) {
279  buf[0] = '\0';
280  strlcat(buf, "[--", sizeof(buf));
281  len += 2;
282  if(args[i].type == arg_negative_flag) {
283  strlcat(buf, "no-", sizeof(buf));
284  len += 3;
285  }
286  strlcat(buf, args[i].long_name, sizeof(buf));
287  len += strlen(args[i].long_name);
288  len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
289  0, 1, &args[i]);
290  strlcat(buf, "]", sizeof(buf));
291  if(args[i].type == arg_strings)
292  strlcat(buf, "...", sizeof(buf));
293  col = check_column(stderr, col, strlen(buf) + 1, columns);
294  col += fprintf(stderr, " %s", buf);
295  }
296  if (args[i].short_name) {
297  basestring_snprintf(buf, sizeof(buf), "[-%c", args[i].short_name);
298  len += 2;
299  len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf),
300  0, 0, &args[i]);
301  strlcat(buf, "]", sizeof(buf));
302  if(args[i].type == arg_strings)
303  strlcat(buf, "...", sizeof(buf));
304  col = check_column(stderr, col, strlen(buf) + 1, columns);
305  col += fprintf(stderr, " %s", buf);
306  }
307  if (args[i].long_name && args[i].short_name)
308  len += 2; /* ", " */
309  max_len = max(max_len, len);
310  }
311  if (extra_string) {
312  col = check_column(stderr, col, strlen(extra_string) + 1, columns);
313  fprintf (stderr, " %s\n", extra_string);
314  } else
315  fprintf (stderr, "\n");
316  for (i = 0; i < num_args; ++i) {
317  if (args[i].help) {
318  size_t count = 0;
319 
320  if (args[i].short_name) {
321  count += fprintf (stderr, "-%c", args[i].short_name);
322  print_arg (buf, sizeof(buf), 0, 0, &args[i]);
323  count += fprintf(stderr, "%s", buf);
324  }
325  if (args[i].short_name && args[i].long_name)
326  count += fprintf (stderr, ", ");
327  if (args[i].long_name) {
328  count += fprintf (stderr, "--");
329  if (args[i].type == arg_negative_flag)
330  count += fprintf (stderr, "no-");
331  count += fprintf (stderr, "%s", args[i].long_name);
332  print_arg (buf, sizeof(buf), 0, 1, &args[i]);
333  count += fprintf(stderr, "%s", buf);
334  }
335  while(count++ <= max_len)
336  putc (' ', stderr);
337  fprintf (stderr, "%s\n", args[i].help);
338  }
339  }
340 }
341 
342 static void
343 add_string(getarg_strings *s, char *value)
344 {
345  s->strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings));
346  s->strings[s->num_strings] = value;
347  s->num_strings++;
348 }
349 
350 static int
351 arg_match_long(struct getargs *args, size_t num_args,
352  char *argv, int argc, const char **rargv, int *optind)
353 {
354  unsigned int i;
355  const char *optarg = NULL;
356  int negate = 0;
357  int partial_match = 0;
358  struct getargs *partial = NULL;
359  struct getargs *current = NULL;
360  int argv_len;
361  char *p;
362 
363  argv_len = strlen(argv);
364  p = strchr (argv, '=');
365  if (p != NULL)
366  argv_len = p - argv;
367 
368  for (i = 0; i < num_args; ++i) {
369  if(args[i].long_name) {
370  int len = strlen(args[i].long_name);
371  char *p = argv;
372  int p_len = argv_len;
373  negate = 0;
374 
375  for (;;) {
376  if (strncmp (args[i].long_name, p, p_len) == 0) {
377  if(p_len == len)
378  current = &args[i];
379  else {
380  ++partial_match;
381  partial = &args[i];
382  }
383  optarg = p + p_len;
384  } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) {
385  negate = !negate;
386  p += 3;
387  p_len -= 3;
388  continue;
389  }
390  break;
391  }
392  if (current)
393  break;
394  }
395  }
396  if (current == NULL) {
397  if (partial_match == 1)
398  current = partial;
399  else
400  return ARG_ERR_NO_MATCH;
401  }
402 
403  if(*optarg == '\0'
404  && !ISFLAG(*current)
405  && current->type != arg_collect
406  && current->type != arg_counter)
407  return ARG_ERR_NO_MATCH;
408  switch(current->type){
409  case arg_integer:
410  {
411  int tmp;
412  if(sscanf(optarg + 1, "%d", &tmp) != 1)
413  return ARG_ERR_BAD_ARG;
414  *(int*)current->value = tmp;
415  return 0;
416  }
417  case arg_string:
418  {
419  *(char**)current->value = (char*)optarg + 1;
420  return 0;
421  }
422  case arg_strings:
423  {
424  add_string((getarg_strings*)current->value, (char*)optarg + 1);
425  return 0;
426  }
427  case arg_flag:
428  case arg_negative_flag:
429  {
430  int *flag = current->value;
431  if(*optarg == '\0' ||
432  strcmp(optarg + 1, "yes") == 0 ||
433  strcmp(optarg + 1, "true") == 0){
434  *flag = !negate;
435  return 0;
436  } else if (*optarg && strcmp(optarg + 1, "maybe") == 0) {
437  *flag = rand() & 1;
438  } else {
439  *flag = negate;
440  return 0;
441  }
442  return ARG_ERR_BAD_ARG;
443  }
444  case arg_counter :
445  {
446  int val;
447 
448  if (*optarg == '\0')
449  val = 1;
450  else {
451  char *endstr;
452 
453  val = strtol (optarg, &endstr, 0);
454  if (endstr == optarg)
455  return ARG_ERR_BAD_ARG;
456  }
457  *(int *)current->value += val;
458  return 0;
459  }
460  case arg_double:
461  {
462  double tmp;
463  if(sscanf(optarg + 1, "%lf", &tmp) != 1)
464  return ARG_ERR_BAD_ARG;
465  *(double*)current->value = tmp;
466  return 0;
467  }
468  case arg_collect:{
469  struct getarg_collect_info *c = current->value;
470  int o = argv - rargv[*optind];
471  return (*c->func)(FALSE, argc, rargv, optind, &o, c->data);
472  }
473 
474  default:
475  abort ();
476  return ARG_ERR_BAD_ARG;
477  }
478 }
479 
480 static int
481 arg_match_short (struct getargs *args, size_t num_args,
482  char *argv, int argc, const char **rargv, int *optind)
483 {
484  int j, k;
485 
486  for(j = 1; j > 0 && j < (int)strlen(rargv[*optind]); j++) {
487  for(k = 0; k < (int)num_args; k++) {
488  char *optarg;
489 
490  if(args[k].short_name == 0)
491  continue;
492  if(argv[j] == args[k].short_name) {
493  if(args[k].type == arg_flag) {
494  *(int*)args[k].value = 1;
495  break;
496  }
497  if(args[k].type == arg_negative_flag) {
498  *(int*)args[k].value = 0;
499  break;
500  }
501  if(args[k].type == arg_counter) {
502  ++*(int *)args[k].value;
503  break;
504  }
505  if(args[k].type == arg_collect) {
506  struct getarg_collect_info *c = args[k].value;
507 
508  if((*c->func)(TRUE, argc, rargv, optind, &j, c->data))
509  return ARG_ERR_BAD_ARG;
510  break;
511  }
512 
513  if(argv[j + 1])
514  optarg = &argv[j + 1];
515  else {
516  ++*optind;
517  optarg = (char *) rargv[*optind];
518  }
519  if(optarg == NULL) {
520  --*optind;
521  return ARG_ERR_NO_ARG;
522  }
523  if(args[k].type == arg_integer) {
524  int tmp;
525  if(sscanf(optarg, "%d", &tmp) != 1)
526  return ARG_ERR_BAD_ARG;
527  *(int*)args[k].value = tmp;
528  return 0;
529  } else if(args[k].type == arg_string) {
530  *(char**)args[k].value = optarg;
531  return 0;
532  } else if(args[k].type == arg_strings) {
533  add_string((getarg_strings*)args[k].value, optarg);
534  return 0;
535  } else if(args[k].type == arg_double) {
536  double tmp;
537  if(sscanf(optarg, "%lf", &tmp) != 1)
538  return ARG_ERR_BAD_ARG;
539  *(double*)args[k].value = tmp;
540  return 0;
541  }
542  return ARG_ERR_BAD_ARG;
543  }
544  }
545  if (k == (int)num_args)
546  return ARG_ERR_NO_MATCH;
547  }
548  return 0;
549 }
550 
551 int
552 getarg(struct getargs *args, size_t num_args,
553  int argc, const char **argv, int *optind)
554 {
555  int i;
556  int ret = 0;
557 
558  srand ((unsigned int)time(NULL));
559  (*optind)++;
560  for(i = *optind; i < argc; i++) {
561  if(argv[i][0] != '-')
562  break;
563  if(argv[i][1] == '-'){
564  if(argv[i][2] == 0){
565  i++;
566  break;
567  }
568  ret = arg_match_long (args, num_args, (char *) argv[i] + 2,
569  argc, argv, &i);
570  } else {
571  ret = arg_match_short (args, num_args, (char *) argv[i],
572  argc, argv, &i);
573  }
574  if(ret)
575  break;
576  }
577  *optind = i;
578  return ret;
579 }
580 
581 
582 #if TEST
583 int foo_flag = 2;
584 int flag1 = 0;
585 int flag2 = 0;
586 int bar_int;
587 char *baz_string;
588 
589 struct getargs args[] = {
590  { NULL, '1', arg_flag, &flag1, "one", NULL },
591  { NULL, '2', arg_flag, &flag2, "two", NULL },
592  { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL },
593  { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"},
594  { "baz", 'x', arg_string, &baz_string, "baz", "name" },
595 };
596 
597 int main(int argc, char **argv)
598 {
599  int optind = 0;
600  while(getarg(args, 5, argc, argv, &optind))
601  printf("Bad arg: %s\n", argv[optind]);
602  printf("flag1 = %d\n", flag1);
603  printf("flag2 = %d\n", flag2);
604  printf("foo_flag = %d\n", foo_flag);
605  printf("bar_int = %d\n", bar_int);
606  printf("baz_flag = %s\n", baz_string);
607  arg_printusage (args, 5, argv[0], "nothing here");
608 }
609 #endif