utils.c 2.4 KB

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