1 /* Licensed under GPLv2+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
5 #include <sys/termios.h> /* Required on Solaris for struct winsize */
7 #include <sys/unistd.h> /* Required on Solaris for ioctl */
15 /* We only use this for pointer comparisons. */
16 const char opt_hidden[1];
18 #define MIN_DESC_WIDTH 40
19 #define MIN_TOTAL_WIDTH 50
21 static unsigned int get_columns(void)
24 const char *env = getenv("COLUMNS");
33 if (ioctl(0, TIOCGWINSZ, &w) != -1)
43 /* Return number of chars of words to put on this line.
44 * Prefix is set to number to skip at start, maxlen is max width, returns
45 * length (after prefix) to put on this line.
46 * start is set if we start a new line in the source description. */
47 static size_t consume_words(const char *words, size_t maxlen, size_t *prefix,
52 /* Always swollow leading whitespace. */
53 *prefix = strspn(words, " \n");
56 /* Leading whitespace at start of line means literal. */
57 if (*start && *prefix) {
58 oldlen = strcspn(words, "\n");
60 /* Use at least one word, even if it takes us over maxlen. */
61 oldlen = len = strcspn(words, " ");
62 while (len <= maxlen) {
64 len += strspn(words+len, " ");
65 if (words[len] == '\n')
67 len += strcspn(words+len, " \n");
73 *start = (words[oldlen - 1] == '\n');
77 static char *add_str_len(char *base, size_t *len, size_t *max,
78 const char *str, size_t slen)
80 if (slen >= *max - *len)
81 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
82 memcpy(base + *len, str, slen);
87 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
89 return add_str_len(base, len, max, str, strlen(str));
92 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
94 if (indent >= *max - *len)
95 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
96 memset(base + *len, ' ', indent);
101 static char *add_desc(char *base, size_t *len, size_t *max,
102 unsigned int indent, unsigned int width,
103 const struct opt_table *opt)
105 size_t off, prefix, l;
107 bool same_line = false, start = true;
109 base = add_str(base, len, max, opt->names);
110 off = strlen(opt->names);
111 if (opt->type == OPT_HASARG
112 && !strchr(opt->names, ' ')
113 && !strchr(opt->names, '=')) {
114 base = add_str(base, len, max, " <arg>");
115 off += strlen(" <arg>");
118 /* Do we start description on next line? */
119 if (off + 2 > indent) {
120 base = add_str(base, len, max, "\n");
123 base = add_indent(base, len, max, indent - off);
128 /* Indent description. */
130 while ((l = consume_words(p, width - indent, &prefix, &start)) != 0) {
132 base = add_indent(base, len, max, indent);
134 base = add_str_len(base, len, max, p, l);
135 base = add_str(base, len, max, "\n");
141 /* Empty description? Make it match normal case. */
143 base = add_str(base, len, max, "\n");
146 char buf[OPT_SHOW_LEN + sizeof("...")];
147 strcpy(buf + OPT_SHOW_LEN, "...");
148 opt->show(buf, opt->u.arg);
150 /* If it doesn't fit on this line, indent. */
151 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
153 base = add_indent(base, len, max, indent);
159 base = add_str(base, len, max, " (default: ");
160 base = add_str(base, len, max, buf);
161 base = add_str(base, len, max, ")\n");
166 char *opt_usage(const char *argv0, const char *extra)
169 size_t max, len, width, indent;
172 width = get_columns();
173 if (width < MIN_TOTAL_WIDTH)
174 width = MIN_TOTAL_WIDTH;
176 /* Figure out longest option. */
178 for (i = 0; i < opt_count; i++) {
180 if (opt_table[i].desc == opt_hidden)
182 if (opt_table[i].type == OPT_SUBTABLE)
184 l = strlen(opt_table[i].names);
185 if (opt_table[i].type == OPT_HASARG
186 && !strchr(opt_table[i].names, ' ')
187 && !strchr(opt_table[i].names, '='))
188 l += strlen(" <arg>");
193 /* Now we know how much to indent */
194 if (indent + MIN_DESC_WIDTH > width)
195 indent = width - MIN_DESC_WIDTH;
200 ret = add_str(ret, &len, &max, "Usage: ");
201 ret = add_str(ret, &len, &max, argv0);
203 /* Find usage message from among registered options if necessary. */
206 for (i = 0; i < opt_count; i++) {
207 if (opt_table[i].cb == (void *)opt_usage_and_exit
208 && opt_table[i].u.carg) {
209 extra = opt_table[i].u.carg;
214 ret = add_str(ret, &len, &max, " ");
215 ret = add_str(ret, &len, &max, extra);
216 ret = add_str(ret, &len, &max, "\n");
218 for (i = 0; i < opt_count; i++) {
219 if (opt_table[i].desc == opt_hidden)
221 if (opt_table[i].type == OPT_SUBTABLE) {
222 ret = add_str(ret, &len, &max, opt_table[i].desc);
223 ret = add_str(ret, &len, &max, ":\n");
226 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);
232 void opt_usage_exit_fail(const char *msg, ...)
237 fprintf(stderr, "%s: ", opt_argv0);
239 vfprintf(stderr, msg, ap);
241 fprintf(stderr, "\n%s",
242 opt_usage(opt_argv0 ? opt_argv0 : "<program>", NULL));