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 */
8 #include <sys/unistd.h> /* Required on Solaris for ioctl */
17 /* We only use this for pointer comparisons. */
18 const char opt_hidden[1];
20 #define MIN_DESC_WIDTH 40
21 #define MIN_TOTAL_WIDTH 50
23 static unsigned int get_columns(void)
26 const char *env = getenv("COLUMNS");
35 if (ioctl(0, TIOCGWINSZ, &w) != -1)
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,
54 /* Always swollow leading whitespace. */
55 *prefix = strspn(words, " \n");
58 /* Leading whitespace at start of line means literal. */
59 if (*start && *prefix) {
60 oldlen = strcspn(words, "\n");
62 /* Use at least one word, even if it takes us over maxlen. */
63 oldlen = len = strcspn(words, " ");
64 while (len <= maxlen) {
66 len += strspn(words+len, " ");
67 if (words[len] == '\n')
69 len += strcspn(words+len, " \n");
75 *start = (words[oldlen - 1] == '\n');
79 static char *add_str_len(char *base, size_t *len, size_t *max,
80 const char *str, size_t slen)
82 if (slen >= *max - *len)
83 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
84 memcpy(base + *len, str, slen);
89 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
91 return add_str_len(base, len, max, str, strlen(str));
94 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
96 if (indent >= *max - *len)
97 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
98 memset(base + *len, ' ', indent);
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)
107 size_t off, prefix, l;
109 bool same_line = false, start = true;
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>");
120 /* Do we start description on next line? */
121 if (off + 2 > indent) {
122 base = add_str(base, len, max, "\n");
125 base = add_indent(base, len, max, indent - off);
130 /* Indent description. */
132 while ((l = consume_words(p, width - indent, &prefix, &start)) != 0) {
134 base = add_indent(base, len, max, indent);
136 base = add_str_len(base, len, max, p, l);
137 base = add_str(base, len, max, "\n");
143 /* Empty description? Make it match normal case. */
145 base = add_str(base, len, max, "\n");
148 char buf[OPT_SHOW_LEN + sizeof("...")];
149 strcpy(buf + OPT_SHOW_LEN, "...");
150 opt->show(buf, opt->u.arg);
152 /* If it doesn't fit on this line, indent. */
153 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
155 base = add_indent(base, len, max, indent);
161 base = add_str(base, len, max, " (default: ");
162 base = add_str(base, len, max, buf);
163 base = add_str(base, len, max, ")\n");
168 char *opt_usage(const char *argv0, const char *extra)
171 size_t max, len, width, indent;
174 width = get_columns();
175 if (width < MIN_TOTAL_WIDTH)
176 width = MIN_TOTAL_WIDTH;
178 /* Figure out longest option. */
180 for (i = 0; i < opt_count; i++) {
182 if (opt_table[i].desc == opt_hidden)
184 if (opt_table[i].type == OPT_SUBTABLE)
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>");
195 /* Now we know how much to indent */
196 if (indent + MIN_DESC_WIDTH > width)
197 indent = width - MIN_DESC_WIDTH;
202 ret = add_str(ret, &len, &max, "Usage: ");
203 ret = add_str(ret, &len, &max, argv0);
205 /* Find usage message from among registered options if necessary. */
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;
216 ret = add_str(ret, &len, &max, " ");
217 ret = add_str(ret, &len, &max, extra);
218 ret = add_str(ret, &len, &max, "\n");
220 for (i = 0; i < opt_count; i++) {
221 if (opt_table[i].desc == opt_hidden)
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");
228 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);
234 void opt_usage_exit_fail(const char *msg, ...)
239 fprintf(stderr, "%s: ", opt_argv0);
241 vfprintf(stderr, msg, ap);
243 fprintf(stderr, "\n%s",
244 opt_usage(opt_argv0 ? opt_argv0 : "<program>", NULL));