]> git.ozlabs.org Git - ccan/blob - ccan/opt/usage.c
353d59bc48542d8423ddc3c437ed36146488ec37
[ccan] / ccan / opt / usage.c
1 /* Licensed under GPLv2+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
3 #if HAVE_SYS_TERMIOS_H
4 #include <sys/ioctl.h>
5 #include <sys/termios.h> /* Required on Solaris for struct winsize */
6 #endif
7 #if HAVE_SYS_UNISTD_H
8 #include <sys/unistd.h> /* Required on Solaris for ioctl */
9 #endif
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdarg.h>
15 #include "private.h"
16
17 /* We only use this for pointer comparisons. */
18 const char opt_hidden[1];
19
20 #define MIN_DESC_WIDTH 40
21 #define MIN_TOTAL_WIDTH 50
22
23 static unsigned int get_columns(void)
24 {
25         int ws_col = 0;
26         const char *env = getenv("COLUMNS");
27
28         if (env)
29                 ws_col = atoi(env);
30
31 #ifdef TIOCGWINSZ
32         if (!ws_col)
33         {
34                 struct winsize w;
35                 if (ioctl(0, TIOCGWINSZ, &w) != -1)
36                         ws_col = w.ws_col;
37         }
38 #endif
39         if (!ws_col)
40                 ws_col = 80;
41
42         return ws_col;
43 }
44
45 /* Return number of chars of words to put on this line.
46  * Prefix is set to number to skip at start, maxlen is max width, returns
47  * length (after prefix) to put on this line.
48  * start is set if we start a new line in the source description. */
49 static size_t consume_words(const char *words, size_t maxlen, size_t *prefix,
50                             bool *start)
51 {
52         size_t oldlen, len;
53
54         /* Always swollow leading whitespace. */
55         *prefix = strspn(words, " \n");
56         words += *prefix;
57
58         /* Leading whitespace at start of line means literal. */
59         if (*start && *prefix) {
60                 oldlen = strcspn(words, "\n");
61         } else {
62                 /* Use at least one word, even if it takes us over maxlen. */
63                 oldlen = len = strcspn(words, " ");
64                 while (len <= maxlen) {
65                         oldlen = len;
66                         len += strspn(words+len, " ");
67                         if (words[len] == '\n')
68                                 break;
69                         len += strcspn(words+len, " \n");
70                         if (len == oldlen)
71                                 break;
72                 }
73         }
74
75         *start = (words[oldlen - 1] == '\n');
76         return oldlen;
77 }
78
79 static char *add_str_len(char *base, size_t *len, size_t *max,
80                          const char *str, size_t slen)
81 {
82         if (slen >= *max - *len)
83                 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
84         memcpy(base + *len, str, slen);
85         *len += slen;
86         return base;
87 }
88
89 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
90 {
91         return add_str_len(base, len, max, str, strlen(str));
92 }
93
94 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
95 {
96         if (indent >= *max - *len)
97                 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
98         memset(base + *len, ' ', indent);
99         *len += indent;
100         return base;
101 }
102
103 static char *add_desc(char *base, size_t *len, size_t *max,
104                       unsigned int indent, unsigned int width,
105                       const struct opt_table *opt)
106 {
107         size_t off, prefix, l;
108         const char *p;
109         bool same_line = false, start = true;
110
111         base = add_str(base, len, max, opt->names);
112         off = strlen(opt->names);
113         if (opt->type == OPT_HASARG
114             && !strchr(opt->names, ' ')
115             && !strchr(opt->names, '=')) {
116                 base = add_str(base, len, max, " <arg>");
117                 off += strlen(" <arg>");
118         }
119
120         /* Do we start description on next line? */
121         if (off + 2 > indent) {
122                 base = add_str(base, len, max, "\n");
123                 off = 0;
124         } else {
125                 base = add_indent(base, len, max, indent - off);
126                 off = indent;
127                 same_line = true;
128         }
129
130         /* Indent description. */
131         p = opt->desc;
132         while ((l = consume_words(p, width - indent, &prefix, &start)) != 0) {
133                 if (!same_line)
134                         base = add_indent(base, len, max, indent);
135                 p += prefix;
136                 base = add_str_len(base, len, max, p, l);
137                 base = add_str(base, len, max, "\n");
138                 off = indent + l;
139                 p += l;
140                 same_line = false;
141         }
142
143         /* Empty description?  Make it match normal case. */
144         if (same_line)
145                 base = add_str(base, len, max, "\n");
146
147         if (opt->show) {
148                 char buf[OPT_SHOW_LEN + sizeof("...")];
149                 strcpy(buf + OPT_SHOW_LEN, "...");
150                 opt->show(buf, opt->u.arg);
151
152                 /* If it doesn't fit on this line, indent. */
153                 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
154                     > width) {
155                         base = add_indent(base, len, max, indent);
156                 } else {
157                         /* Remove \n. */
158                         (*len)--;
159                 }
160
161                 base = add_str(base, len, max, " (default: ");
162                 base = add_str(base, len, max, buf);
163                 base = add_str(base, len, max, ")\n");
164         }
165         return base;
166 }
167
168 char *opt_usage(const char *argv0, const char *extra)
169 {
170         unsigned int i;
171         size_t max, len, width, indent;
172         char *ret;
173
174         width = get_columns();
175         if (width < MIN_TOTAL_WIDTH)
176                 width = MIN_TOTAL_WIDTH;
177
178         /* Figure out longest option. */
179         indent = 0;
180         for (i = 0; i < opt_count; i++) {
181                 size_t l;
182                 if (opt_table[i].desc == opt_hidden)
183                         continue;
184                 if (opt_table[i].type == OPT_SUBTABLE)
185                         continue;
186                 l = strlen(opt_table[i].names);
187                 if (opt_table[i].type == OPT_HASARG
188                     && !strchr(opt_table[i].names, ' ')
189                     && !strchr(opt_table[i].names, '='))
190                         l += strlen(" <arg>");
191                 if (l + 2 > indent)
192                         indent = l + 2;
193         }
194
195         /* Now we know how much to indent */
196         if (indent + MIN_DESC_WIDTH > width)
197                 indent = width - MIN_DESC_WIDTH;
198
199         len = max = 0;
200         ret = NULL;
201
202         ret = add_str(ret, &len, &max, "Usage: ");
203         ret = add_str(ret, &len, &max, argv0);
204
205         /* Find usage message from among registered options if necessary. */
206         if (!extra) {
207                 extra = "";
208                 for (i = 0; i < opt_count; i++) {
209                         if (opt_table[i].cb == (void *)opt_usage_and_exit
210                             && opt_table[i].u.carg) {
211                                 extra = opt_table[i].u.carg;
212                                 break;
213                         }
214                 }
215         }
216         ret = add_str(ret, &len, &max, " ");
217         ret = add_str(ret, &len, &max, extra);
218         ret = add_str(ret, &len, &max, "\n");
219
220         for (i = 0; i < opt_count; i++) {
221                 if (opt_table[i].desc == opt_hidden)
222                         continue;
223                 if (opt_table[i].type == OPT_SUBTABLE) {
224                         ret = add_str(ret, &len, &max, opt_table[i].desc);
225                         ret = add_str(ret, &len, &max, ":\n");
226                         continue;
227                 }
228                 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);
229         }
230         ret[len] = '\0';
231         return ret;
232 }
233
234 void opt_usage_exit_fail(const char *msg, ...)
235 {
236         va_list ap;
237
238         if (opt_argv0)
239                 fprintf(stderr, "%s: ", opt_argv0);
240         va_start(ap, msg);
241         vfprintf(stderr, msg, ap);
242         va_end(ap);
243         fprintf(stderr, "\n%s",
244                 opt_usage(opt_argv0 ? opt_argv0 : "<program>", NULL));
245         exit(1);
246 }