usage.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <ccan/opt/opt.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include "private.h"
  7. /* We only use this for pointer comparisons. */
  8. const char opt_hidden[1];
  9. static unsigned write_short_options(char *str)
  10. {
  11. unsigned int i, num = 0;
  12. const char *p;
  13. for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
  14. if (opt_table[i].desc != opt_hidden)
  15. str[num++] = *p;
  16. }
  17. return num;
  18. }
  19. #define OPT_SPACE_PAD " "
  20. /* FIXME: Get all purdy. */
  21. char *opt_usage(const char *argv0, const char *extra)
  22. {
  23. unsigned int i, num, len;
  24. char *ret, *p;
  25. if (!extra) {
  26. extra = "";
  27. for (i = 0; i < opt_count; i++) {
  28. if (opt_table[i].cb == (void *)opt_usage_and_exit
  29. && opt_table[i].u.carg) {
  30. extra = opt_table[i].u.carg;
  31. break;
  32. }
  33. }
  34. }
  35. /* An overestimate of our length. */
  36. len = strlen("Usage: %s ") + strlen(argv0)
  37. + strlen("[-%.*s]") + opt_num_short + 1
  38. + strlen(" ") + strlen(extra)
  39. + strlen("\n");
  40. for (i = 0; i < opt_count; i++) {
  41. if (opt_table[i].type == OPT_SUBTABLE) {
  42. len += strlen("\n") + strlen(opt_table[i].desc)
  43. + strlen(":\n");
  44. } else if (opt_table[i].desc != opt_hidden) {
  45. len += strlen(opt_table[i].names) + strlen(" <arg>");
  46. len += strlen(OPT_SPACE_PAD)
  47. + strlen(opt_table[i].desc) + 1;
  48. if (opt_table[i].show) {
  49. len += strlen("(default: %s)")
  50. + OPT_SHOW_LEN + sizeof("...");
  51. }
  52. len += strlen("\n");
  53. }
  54. }
  55. p = ret = malloc(len);
  56. if (!ret)
  57. return NULL;
  58. p += sprintf(p, "Usage: %s", argv0);
  59. p += sprintf(p, " [-");
  60. num = write_short_options(p);
  61. if (num) {
  62. p += num;
  63. p += sprintf(p, "]");
  64. } else {
  65. /* Remove start of single-entry options */
  66. p -= 3;
  67. }
  68. if (extra)
  69. p += sprintf(p, " %s", extra);
  70. p += sprintf(p, "\n");
  71. for (i = 0; i < opt_count; i++) {
  72. if (opt_table[i].desc == opt_hidden)
  73. continue;
  74. if (opt_table[i].type == OPT_SUBTABLE) {
  75. p += sprintf(p, "%s:\n", opt_table[i].desc);
  76. continue;
  77. }
  78. len = sprintf(p, "%s", opt_table[i].names);
  79. if (opt_table[i].type == OPT_HASARG
  80. && !strchr(opt_table[i].names, ' ')
  81. && !strchr(opt_table[i].names, '='))
  82. len += sprintf(p + len, " <arg>");
  83. len += sprintf(p + len, "%.*s",
  84. len < strlen(OPT_SPACE_PAD)
  85. ? (unsigned)strlen(OPT_SPACE_PAD) - len : 1,
  86. OPT_SPACE_PAD);
  87. len += sprintf(p + len, "%s", opt_table[i].desc);
  88. if (opt_table[i].show) {
  89. char buf[OPT_SHOW_LEN + sizeof("...")];
  90. strcpy(buf + OPT_SHOW_LEN, "...");
  91. opt_table[i].show(buf, opt_table[i].u.arg);
  92. len += sprintf(p + len, " (default: %s)", buf);
  93. }
  94. p += len;
  95. p += sprintf(p, "\n");
  96. }
  97. *p = '\0';
  98. return ret;
  99. }