1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
3 #ifdef HAVE_SYS_TERMIOS_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");
27 #ifdef HAVE_SYS_TERMIOS_H
31 if (ioctl(0, TIOCGWINSZ, &w) != -1)
41 /* Return number of chars of words to put on this line.
42 * Prefix is set to number to skip at start, maxlen is max width, returns
43 * length (after prefix) to put on this line. */
44 static size_t consume_words(const char *words, size_t maxlen, size_t *prefix)
48 /* Swallow leading whitespace. */
49 *prefix = strspn(words, " ");
52 /* Use at least one word, even if it takes us over maxlen. */
53 oldlen = len = strcspn(words, " ");
54 while (len <= maxlen) {
56 len += strspn(words+len, " ");
57 len += strcspn(words+len, " ");
65 static char *add_str_len(char *base, size_t *len, size_t *max,
66 const char *str, size_t slen)
68 if (slen >= *max - *len)
69 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
70 memcpy(base + *len, str, slen);
75 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
77 return add_str_len(base, len, max, str, strlen(str));
80 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
82 if (indent >= *max - *len)
83 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
84 memset(base + *len, ' ', indent);
89 static char *add_desc(char *base, size_t *len, size_t *max,
90 unsigned int indent, unsigned int width,
91 const struct opt_table *opt)
93 size_t off, prefix, l;
95 bool same_line = false;
97 base = add_str(base, len, max, opt->names);
98 off = strlen(opt->names);
99 if (opt->type == OPT_HASARG
100 && !strchr(opt->names, ' ')
101 && !strchr(opt->names, '=')) {
102 base = add_str(base, len, max, " <arg>");
103 off += strlen(" <arg>");
106 /* Do we start description on next line? */
107 if (off + 2 > indent) {
108 base = add_str(base, len, max, "\n");
111 base = add_indent(base, len, max, indent - off);
116 /* Indent description. */
118 while ((l = consume_words(p, width - indent, &prefix)) != 0) {
120 base = add_indent(base, len, max, indent);
122 base = add_str_len(base, len, max, p, l);
123 base = add_str(base, len, max, "\n");
129 /* Empty description? Make it match normal case. */
131 base = add_str(base, len, max, "\n");
134 char buf[OPT_SHOW_LEN + sizeof("...")];
135 strcpy(buf + OPT_SHOW_LEN, "...");
136 opt->show(buf, opt->u.arg);
138 /* If it doesn't fit on this line, indent. */
139 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
141 base = add_indent(base, len, max, indent);
147 base = add_str(base, len, max, " (default: ");
148 base = add_str(base, len, max, buf);
149 base = add_str(base, len, max, ")\n");
154 char *opt_usage(const char *argv0, const char *extra)
157 size_t max, len, width, indent;
160 width = get_columns();
161 if (width < MIN_TOTAL_WIDTH)
162 width = MIN_TOTAL_WIDTH;
164 /* Figure out longest option. */
166 for (i = 0; i < opt_count; i++) {
168 if (opt_table[i].desc == opt_hidden)
170 if (opt_table[i].type == OPT_SUBTABLE)
172 l = strlen(opt_table[i].names);
173 if (opt_table[i].type == OPT_HASARG
174 && !strchr(opt_table[i].names, ' ')
175 && !strchr(opt_table[i].names, '='))
176 l += strlen(" <arg>");
181 /* Now we know how much to indent */
182 if (indent + MIN_DESC_WIDTH > width)
183 indent = width - MIN_DESC_WIDTH;
188 ret = add_str(ret, &len, &max, "Usage: ");
189 ret = add_str(ret, &len, &max, argv0);
191 /* Find usage message from among registered options if necessary. */
194 for (i = 0; i < opt_count; i++) {
195 if (opt_table[i].cb == (void *)opt_usage_and_exit
196 && opt_table[i].u.carg) {
197 extra = opt_table[i].u.carg;
202 ret = add_str(ret, &len, &max, " ");
203 ret = add_str(ret, &len, &max, extra);
204 ret = add_str(ret, &len, &max, "\n");
206 for (i = 0; i < opt_count; i++) {
207 if (opt_table[i].desc == opt_hidden)
209 if (opt_table[i].type == OPT_SUBTABLE) {
210 ret = add_str(ret, &len, &max, opt_table[i].desc);
211 ret = add_str(ret, &len, &max, ":\n");
214 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);