utils.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "config.h"
  2. #include <ccan/tap/tap.h>
  3. #include <stdarg.h>
  4. #include <stdlib.h>
  5. #include <ccan/opt/opt.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "utils.h"
  9. unsigned int test_cb_called;
  10. char *test_noarg(void *arg UNNEEDED)
  11. {
  12. test_cb_called++;
  13. return NULL;
  14. }
  15. char *test_arg(const char *optarg, const char *arg)
  16. {
  17. test_cb_called++;
  18. ok1(strcmp(optarg, arg) == 0);
  19. return NULL;
  20. }
  21. void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
  22. {
  23. strncpy(buf, arg, OPT_SHOW_LEN);
  24. }
  25. char *err_output = NULL;
  26. void save_err_output(const char *fmt, ...)
  27. {
  28. va_list ap;
  29. char *p;
  30. va_start(ap, fmt);
  31. /* Check return, for fascist gcc */
  32. if (vasprintf(&p, fmt, ap) == -1)
  33. p = NULL;
  34. va_end(ap);
  35. if (err_output) {
  36. err_output = realloc(err_output,
  37. strlen(err_output) + strlen(p) + 1);
  38. strcat(err_output, p);
  39. free(p);
  40. } else
  41. err_output = p;
  42. }
  43. void reset_options(void)
  44. {
  45. opt_free_table();
  46. free(err_output);
  47. err_output = NULL;
  48. }
  49. static bool allocated = false;
  50. bool parse_args(int *argc, char ***argv, ...)
  51. {
  52. char **a;
  53. va_list ap;
  54. va_start(ap, argv);
  55. *argc = 1;
  56. a = malloc(sizeof(*a) * (*argc + 1));
  57. a[0] = (*argv)[0];
  58. while ((a[*argc] = va_arg(ap, char *)) != NULL) {
  59. (*argc)++;
  60. a = realloc(a, sizeof(*a) * (*argc + 1));
  61. }
  62. if (allocated)
  63. free(*argv);
  64. *argv = a;
  65. allocated = true;
  66. return opt_parse(argc, *argv, save_err_output);
  67. }
  68. bool parse_early_args(int *argc, char ***argv, ...)
  69. {
  70. char **a;
  71. va_list ap;
  72. va_start(ap, argv);
  73. *argc = 1;
  74. a = malloc(sizeof(*a) * (*argc + 1));
  75. a[0] = (*argv)[0];
  76. while ((a[*argc] = va_arg(ap, char *)) != NULL) {
  77. (*argc)++;
  78. a = realloc(a, sizeof(*a) * (*argc + 1));
  79. }
  80. if (allocated)
  81. free(*argv);
  82. *argv = a;
  83. allocated = true;
  84. return opt_early_parse(*argc, *argv, save_err_output);
  85. }
  86. struct opt_table short_table[] = {
  87. /* Short opts, different args. */
  88. OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
  89. OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
  90. OPT_ENDTABLE
  91. };
  92. struct opt_table long_table[] = {
  93. /* Long opts, different args. */
  94. OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
  95. OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
  96. OPT_ENDTABLE
  97. };
  98. struct opt_table long_and_short_table[] = {
  99. /* Short and long, different args. */
  100. OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
  101. OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
  102. OPT_ENDTABLE
  103. };
  104. /* Sub-table test. */
  105. struct opt_table subtables[] = {
  106. /* Two short, and two long long, no description */
  107. OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
  108. /* Hidden option */
  109. OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
  110. OPT_SUBTABLE(short_table, NULL),
  111. OPT_SUBTABLE(long_table, "long table options"),
  112. OPT_SUBTABLE(long_and_short_table, NULL),
  113. OPT_ENDTABLE
  114. };