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, " ");
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 len += strcspn(words+len, " ");
66 static char *add_str_len(char *base, size_t *len, size_t *max,
67 const char *str, size_t slen)
69 if (slen >= *max - *len)
70 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
71 memcpy(base + *len, str, slen);
76 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
78 return add_str_len(base, len, max, str, strlen(str));
81 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
83 if (indent >= *max - *len)
84 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
85 memset(base + *len, ' ', indent);
90 static char *add_desc(char *base, size_t *len, size_t *max,
91 unsigned int indent, unsigned int width,
92 const struct opt_table *opt)
94 size_t off, prefix, l;
96 bool same_line = false;
98 base = add_str(base, len, max, opt->names);
99 off = strlen(opt->names);
100 if (opt->type == OPT_HASARG
101 && !strchr(opt->names, ' ')
102 && !strchr(opt->names, '=')) {
103 base = add_str(base, len, max, " <arg>");
104 off += strlen(" <arg>");
107 /* Do we start description on next line? */
108 if (off + 2 > indent) {
109 base = add_str(base, len, max, "\n");
112 base = add_indent(base, len, max, indent - off);
117 /* Indent description. */
119 while ((l = consume_words(p, width - indent, &prefix)) != 0) {
121 base = add_indent(base, len, max, indent);
123 base = add_str_len(base, len, max, p, l);
124 base = add_str(base, len, max, "\n");
130 /* Empty description? Make it match normal case. */
132 base = add_str(base, len, max, "\n");
135 char buf[OPT_SHOW_LEN + sizeof("...")];
136 strcpy(buf + OPT_SHOW_LEN, "...");
137 opt->show(buf, opt->u.arg);
139 /* If it doesn't fit on this line, indent. */
140 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
142 base = add_indent(base, len, max, indent);
148 base = add_str(base, len, max, " (default: ");
149 base = add_str(base, len, max, buf);
150 base = add_str(base, len, max, ")\n");
155 char *opt_usage(const char *argv0, const char *extra)
158 size_t max, len, width, indent;
161 width = get_columns();
162 if (width < MIN_TOTAL_WIDTH)
163 width = MIN_TOTAL_WIDTH;
165 /* Figure out longest option. */
167 for (i = 0; i < opt_count; i++) {
169 if (opt_table[i].desc == opt_hidden)
171 if (opt_table[i].type == OPT_SUBTABLE)
173 l = strlen(opt_table[i].names);
174 if (opt_table[i].type == OPT_HASARG
175 && !strchr(opt_table[i].names, ' ')
176 && !strchr(opt_table[i].names, '='))
177 l += strlen(" <arg>");
182 /* Now we know how much to indent */
183 if (indent + MIN_DESC_WIDTH > width)
184 indent = width - MIN_DESC_WIDTH;
189 ret = add_str(ret, &len, &max, "Usage: ");
190 ret = add_str(ret, &len, &max, argv0);
192 /* Find usage message from among registered options if necessary. */
195 for (i = 0; i < opt_count; i++) {
196 if (opt_table[i].cb == (void *)opt_usage_and_exit
197 && opt_table[i].u.carg) {
198 extra = opt_table[i].u.carg;
203 ret = add_str(ret, &len, &max, " ");
204 ret = add_str(ret, &len, &max, extra);
205 ret = add_str(ret, &len, &max, "\n");
207 for (i = 0; i < opt_count; i++) {
208 if (opt_table[i].desc == opt_hidden)
210 if (opt_table[i].type == OPT_SUBTABLE) {
211 ret = add_str(ret, &len, &max, opt_table[i].desc);
212 ret = add_str(ret, &len, &max, ":\n");
215 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);