1 /* Licensed under GPLv3+ - 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 */
14 /* We only use this for pointer comparisons. */
15 const char opt_hidden[1];
17 #define MIN_DESC_WIDTH 40
18 #define MIN_TOTAL_WIDTH 50
20 static unsigned int get_columns(void)
23 const char *env = getenv("COLUMNS");
32 if (ioctl(0, TIOCGWINSZ, &w) != -1)
42 /* Return number of chars of words to put on this line.
43 * Prefix is set to number to skip at start, maxlen is max width, returns
44 * length (after prefix) to put on this line. */
45 static size_t consume_words(const char *words, size_t maxlen, size_t *prefix)
49 /* Swallow leading whitespace. */
50 *prefix = strspn(words, " \n");
53 /* Use at least one word, even if it takes us over maxlen. */
54 oldlen = len = strcspn(words, " ");
55 while (len <= maxlen) {
57 len += strspn(words+len, " ");
58 if (words[len] == '\n')
60 len += strcspn(words+len, " \n");
68 static char *add_str_len(char *base, size_t *len, size_t *max,
69 const char *str, size_t slen)
71 if (slen >= *max - *len)
72 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
73 memcpy(base + *len, str, slen);
78 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
80 return add_str_len(base, len, max, str, strlen(str));
83 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
85 if (indent >= *max - *len)
86 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
87 memset(base + *len, ' ', indent);
92 static char *add_desc(char *base, size_t *len, size_t *max,
93 unsigned int indent, unsigned int width,
94 const struct opt_table *opt)
96 size_t off, prefix, l;
98 bool same_line = false;
100 base = add_str(base, len, max, opt->names);
101 off = strlen(opt->names);
102 if (opt->type == OPT_HASARG
103 && !strchr(opt->names, ' ')
104 && !strchr(opt->names, '=')) {
105 base = add_str(base, len, max, " <arg>");
106 off += strlen(" <arg>");
109 /* Do we start description on next line? */
110 if (off + 2 > indent) {
111 base = add_str(base, len, max, "\n");
114 base = add_indent(base, len, max, indent - off);
119 /* Indent description. */
121 while ((l = consume_words(p, width - indent, &prefix)) != 0) {
123 base = add_indent(base, len, max, indent);
125 base = add_str_len(base, len, max, p, l);
126 base = add_str(base, len, max, "\n");
132 /* Empty description? Make it match normal case. */
134 base = add_str(base, len, max, "\n");
137 char buf[OPT_SHOW_LEN + sizeof("...")];
138 strcpy(buf + OPT_SHOW_LEN, "...");
139 opt->show(buf, opt->u.arg);
141 /* If it doesn't fit on this line, indent. */
142 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
144 base = add_indent(base, len, max, indent);
150 base = add_str(base, len, max, " (default: ");
151 base = add_str(base, len, max, buf);
152 base = add_str(base, len, max, ")\n");
157 char *opt_usage(const char *argv0, const char *extra)
160 size_t max, len, width, indent;
163 width = get_columns();
164 if (width < MIN_TOTAL_WIDTH)
165 width = MIN_TOTAL_WIDTH;
167 /* Figure out longest option. */
169 for (i = 0; i < opt_count; i++) {
171 if (opt_table[i].desc == opt_hidden)
173 if (opt_table[i].type == OPT_SUBTABLE)
175 l = strlen(opt_table[i].names);
176 if (opt_table[i].type == OPT_HASARG
177 && !strchr(opt_table[i].names, ' ')
178 && !strchr(opt_table[i].names, '='))
179 l += strlen(" <arg>");
184 /* Now we know how much to indent */
185 if (indent + MIN_DESC_WIDTH > width)
186 indent = width - MIN_DESC_WIDTH;
191 ret = add_str(ret, &len, &max, "Usage: ");
192 ret = add_str(ret, &len, &max, argv0);
194 /* Find usage message from among registered options if necessary. */
197 for (i = 0; i < opt_count; i++) {
198 if (opt_table[i].cb == (void *)opt_usage_and_exit
199 && opt_table[i].u.carg) {
200 extra = opt_table[i].u.carg;
205 ret = add_str(ret, &len, &max, " ");
206 ret = add_str(ret, &len, &max, extra);
207 ret = add_str(ret, &len, &max, "\n");
209 for (i = 0; i < opt_count; i++) {
210 if (opt_table[i].desc == opt_hidden)
212 if (opt_table[i].type == OPT_SUBTABLE) {
213 ret = add_str(ret, &len, &max, opt_table[i].desc);
214 ret = add_str(ret, &len, &max, ":\n");
217 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);