utils.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2011 Rusty Russell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU General Public License as published by the Free Software
  6. * Foundation; either version 2 of the License, or (at your option) any later
  7. * version. See LICENSE for more details.
  8. */
  9. #include "config.h"
  10. #include <ccan/tap/tap.h>
  11. #include <stdarg.h>
  12. #include <stdlib.h>
  13. #include <ccan/opt/opt.h>
  14. #include <getopt.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include "utils.h"
  18. unsigned int test_cb_called;
  19. char *test_noarg(void *arg)
  20. {
  21. test_cb_called++;
  22. return NULL;
  23. }
  24. char *test_arg(const char *optarg, const char *arg)
  25. {
  26. test_cb_called++;
  27. ok1(strcmp(optarg, arg) == 0);
  28. return NULL;
  29. }
  30. void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
  31. {
  32. strncpy(buf, arg, OPT_SHOW_LEN);
  33. }
  34. char *err_output = NULL;
  35. void save_err_output(const char *fmt, ...)
  36. {
  37. va_list ap;
  38. char *p;
  39. va_start(ap, fmt);
  40. /* Check return, for fascist gcc */
  41. if (vasprintf(&p, fmt, ap) == -1)
  42. p = NULL;
  43. va_end(ap);
  44. if (err_output) {
  45. err_output = realloc(err_output,
  46. strlen(err_output) + strlen(p) + 1);
  47. strcat(err_output, p);
  48. free(p);
  49. } else
  50. err_output = p;
  51. }
  52. static bool allocated = false;
  53. bool parse_args(int *argc, char ***argv, ...)
  54. {
  55. char **a;
  56. va_list ap;
  57. va_start(ap, argv);
  58. *argc = 1;
  59. a = malloc(sizeof(*a) * (*argc + 1));
  60. a[0] = (*argv)[0];
  61. while ((a[*argc] = va_arg(ap, char *)) != NULL) {
  62. (*argc)++;
  63. a = realloc(a, sizeof(*a) * (*argc + 1));
  64. }
  65. if (allocated)
  66. free(*argv);
  67. *argv = a;
  68. allocated = true;
  69. /* Re-set before parsing. */
  70. optind = 0;
  71. return opt_parse(argc, *argv, save_err_output);
  72. }
  73. struct opt_table short_table[] = {
  74. /* Short opts, different args. */
  75. OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
  76. OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
  77. OPT_ENDTABLE
  78. };
  79. struct opt_table long_table[] = {
  80. /* Long opts, different args. */
  81. OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
  82. OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
  83. OPT_ENDTABLE
  84. };
  85. struct opt_table long_and_short_table[] = {
  86. /* Short and long, different args. */
  87. OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
  88. OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
  89. OPT_ENDTABLE
  90. };
  91. /* Sub-table test. */
  92. struct opt_table subtables[] = {
  93. /* Two short, and two long long, no description */
  94. OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
  95. /* Hidden option */
  96. OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
  97. OPT_SUBTABLE(short_table, NULL),
  98. OPT_SUBTABLE(long_table, "long table options"),
  99. OPT_SUBTABLE(long_and_short_table, NULL),
  100. OPT_ENDTABLE
  101. };