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");
76 *start = (words[oldlen - 1] == '\n');
80 static char *add_str_len(char *base, size_t *len, size_t *max,
81 const char *str, size_t slen)
83 if (slen >= *max - *len)
84 base = opt_alloc.realloc(base, *max = (*max * 2 + slen + 1));
85 memcpy(base + *len, str, slen);
90 static char *add_str(char *base, size_t *len, size_t *max, const char *str)
92 return add_str_len(base, len, max, str, strlen(str));
95 static char *add_indent(char *base, size_t *len, size_t *max, size_t indent)
97 if (indent >= *max - *len)
98 base = opt_alloc.realloc(base, *max = (*max * 2 + indent + 1));
99 memset(base + *len, ' ', indent);
104 static char *add_desc(char *base, size_t *len, size_t *max,
105 unsigned int indent, unsigned int width,
106 const struct opt_table *opt)
108 size_t off, prefix, l;
110 bool same_line = false, start = true;
112 base = add_str(base, len, max, opt->names);
113 off = strlen(opt->names);
114 if ((opt->type & OPT_HASARG)
115 && !strchr(opt->names, ' ')
116 && !strchr(opt->names, '=')) {
117 base = add_str(base, len, max, " <arg>");
118 off += strlen(" <arg>");
121 /* Do we start description on next line? */
122 if (off + 2 > indent) {
123 base = add_str(base, len, max, "\n");
126 base = add_indent(base, len, max, indent - off);
131 /* Indent description. */
133 while ((l = consume_words(p, width - indent, &prefix, &start)) != 0) {
135 base = add_indent(base, len, max, indent);
137 base = add_str_len(base, len, max, p, l);
138 base = add_str(base, len, max, "\n");
144 /* Empty description? Make it match normal case. */
146 base = add_str(base, len, max, "\n");
149 char buf[OPT_SHOW_LEN + sizeof("...")];
150 strcpy(buf + OPT_SHOW_LEN, "...");
151 opt->show(buf, opt->u.arg);
153 /* If it doesn't fit on this line, indent. */
154 if (off + strlen(" (default: ") + strlen(buf) + strlen(")")
156 base = add_indent(base, len, max, indent);
162 base = add_str(base, len, max, " (default: ");
163 base = add_str(base, len, max, buf);
164 base = add_str(base, len, max, ")\n");
169 char *opt_usage(const char *argv0, const char *extra)
172 size_t max, len, width, indent;
175 width = get_columns();
176 if (width < MIN_TOTAL_WIDTH)
177 width = MIN_TOTAL_WIDTH;
179 /* Figure out longest option. */
181 for (i = 0; i < opt_count; i++) {
183 if (opt_table[i].desc == opt_hidden)
185 if (opt_table[i].type == OPT_SUBTABLE)
187 l = strlen(opt_table[i].names);
188 if (opt_table[i].type == OPT_HASARG
189 && !strchr(opt_table[i].names, ' ')
190 && !strchr(opt_table[i].names, '='))
191 l += strlen(" <arg>");
196 /* Now we know how much to indent */
197 if (indent + MIN_DESC_WIDTH > width)
198 indent = width - MIN_DESC_WIDTH;
203 ret = add_str(ret, &len, &max, "Usage: ");
204 ret = add_str(ret, &len, &max, argv0);
206 /* Find usage message from among registered options if necessary. */
209 for (i = 0; i < opt_count; i++) {
210 if (opt_table[i].cb == (void *)opt_usage_and_exit
211 && opt_table[i].u.carg) {
212 extra = opt_table[i].u.carg;
217 ret = add_str(ret, &len, &max, " ");
218 ret = add_str(ret, &len, &max, extra);
219 ret = add_str(ret, &len, &max, "\n");
221 for (i = 0; i < opt_count; i++) {
222 if (opt_table[i].desc == opt_hidden)
224 if (opt_table[i].type == OPT_SUBTABLE) {
225 ret = add_str(ret, &len, &max, opt_table[i].desc);
226 ret = add_str(ret, &len, &max, ":\n");
229 ret = add_desc(ret, &len, &max, indent, width, &opt_table[i]);
235 void opt_usage_exit_fail(const char *msg, ...)
240 fprintf(stderr, "%s: ", opt_argv0);
242 vfprintf(stderr, msg, ap);
244 fprintf(stderr, "\n%s",
245 opt_usage(opt_argv0 ? opt_argv0 : "<program>", NULL));