14 #include <sys/socket.h>
15 struct statvfs {int i;};
16 #include "lib/portability.h"
19 // Humor toys.h (lie through our teeth, C's linker doesn't care).
20 char toys[4096], libbuf[4096], toybuf[4096];
21 void show_help(FILE *out) {;}
22 void toy_exec(char *argv[]) {;}
23 void toy_init(void *which, char *argv[]) {;}
25 // Parse config files into data structures.
29 int enabled, help_indent;
31 struct double_list *help;
34 // remove leading spaces
37 while (isspace(*s)) s++;
42 // if line starts with name (as whole word) return pointer after it, else NULL
43 char *keyword(char *name, char *line)
45 int len = strlen(name);
48 if (strncmp(name, line, len)) return 0;
50 if (*line && !isspace(*line)) return 0;
56 // dlist_pop() freeing wrapper structure for you.
57 char *dlist_zap(struct double_list **help)
59 struct double_list *dd = dlist_pop(help);
67 int zap_blank_lines(struct double_list **help)
74 s = trim((*help)->data);
78 free(dlist_zap(help));
84 // Collect "-a blah" description lines following a blank line (or start).
85 // Returns array of removed lines with *len entries (0 for none).
87 // Moves *help to new start of text (in case dash lines were at beginning).
88 // Sets *from to where dash lines removed from (in case they weren't).
89 // Discards blank lines before and after dashlines.
91 // If no prefix, *help NULL. If no postfix, *from == *help
92 // if no dashlines returned *from == *help.
94 char **grab_dashlines(struct double_list **help, struct double_list **from,
97 struct double_list *dd;
102 zap_blank_lines(help);
105 // Find start of dash block. Must be at start or after blank line.
107 s = trim((*from)->data);
108 if (*s == '-' && s[1] != '-' && !count) break;
113 *from = (*from)->next;
114 if (*from == *help) return 0;
117 // If there was whitespace before this, zap it. This can't take out *help
118 // because zap_blank_lines skipped blank lines, and we had to have at least
119 // one non-blank line (a dash line) to get this far.
120 while (!*trim((*from)->prev->data)) {
121 *from = (*from)->prev;
122 free(dlist_zap(from));
125 // Count number of dashlines, copy out to array, zap trailing whitespace
126 // If *help was at start of dashblock, move it with *from
129 if (*help == *from) *help = 0;
131 if (*trim(dd->data) != '-') break;
133 if (*from == (dd = dd->next)) break;
136 list = xmalloc(sizeof(char *)*count);
138 while (count) list[--count] = dlist_zap(from);
143 void parse(char *filename)
145 FILE *fp = xfopen(filename, "r");
146 struct symbol *new = 0;
149 char *s, *line = NULL;
152 // Read line, trim whitespace at right edge.
153 if (getline(&line, &len, fp) < 1) break;
154 s = line+strlen(line);
155 while (--s >= line) {
156 if (!isspace(*s)) break;
160 // source or config keyword at left edge?
161 if (*line && !isspace(*line)) {
162 if ((s = keyword("config", line))) {
163 new = xzalloc(sizeof(struct symbol));
167 } else if ((s = keyword("source", line))) parse(s);
173 if (sym && sym->help_indent) {
174 dlist_add(&(new->help), line);
175 if (sym->help_indent < 0) {
176 sym->help_indent = 0;
177 while (isspace(line[sym->help_indent])) sym->help_indent++;
180 else if ((s = keyword("depends", line)) && (s = keyword("on", s)))
182 else if (keyword("help", line)) sym->help_indent = -1;
188 int charsort(void *a, void *b)
190 char *aa = a, *bb = b;
192 if (*aa < *bb) return -1;
193 if (*aa > *bb) return 1;
197 int dashsort(char **a, char **b)
199 char *aa = *a, *bb = *b;
201 if (aa[1] < bb[1]) return -1;
202 if (aa[1] > bb[1]) return 1;
206 int dashlinesort(char **a, char **b)
208 return strcmp(*a, *b);
211 int main(int argc, char *argv[])
216 fprintf(stderr, "usage: config2help Config.in .config\n");
224 fp = xfopen(argv[2], "r");
229 if (getline(&line, &len, fp) < 1) break;
230 if (!strncmp("CONFIG_", line, 7)) {
234 for (try=sym; try; try=try->next) {
235 len = strlen(try->name);
236 if (!strncmp(try->name, s, len) && s[len]=='=' && s[len+1]=='y') {
244 // Collate help according to usage, depends, and .config
246 // Loop through each entry, finding duplicate enabled "usage:" names
247 // This is in reverse order, so last entry gets collated with previous
248 // entry until we run out of matching pairs.
250 struct symbol *throw = 0, *catch;
251 char *this, *that, *cusage, *tusage, *name;
254 // find a usage: name and collate all enabled entries with that name
255 for (catch = sym; catch; catch = catch->next) {
256 if (catch->enabled != 1) continue;
257 if (catch->help && (that = keyword("usage:", catch->help->data))) {
258 struct double_list *cfrom, *tfrom, *anchor;
259 char *try, **cdashlines, **tdashlines;
262 // Align usage: lines, finding a matching pair so we can suck help
263 // text out of throw into catch, copying from this to that
264 if (!throw) name = that;
265 else if (strncmp(name, that, len) || !isspace(that[len])) continue;
267 while (!isspace(*that) && *that) that++;
268 if (!throw) len = that-name;
277 // Grab option description lines to collate from catch and throw
278 tusage = dlist_zap(&throw->help);
279 tdashlines = grab_dashlines(&throw->help, &tfrom, &tlen);
280 cusage = dlist_zap(&catch->help);
281 cdashlines = grab_dashlines(&catch->help, &cfrom, &clen);
282 anchor = catch->help;
284 // If we've got both, collate and alphebetize
285 if (cdashlines && tdashlines) {
286 char **new = xmalloc(sizeof(char *)*(clen+tlen));
288 memcpy(new, cdashlines, sizeof(char *)*clen);
289 memcpy(new+clen, tdashlines, sizeof(char *)*tlen);
292 qsort(new, clen+tlen, sizeof(char *), (void *)dashlinesort);
295 // If just one, make sure it's in catch.
296 } else if (tdashlines) cdashlines = tdashlines;
298 // If throw had a prefix, insert it before dashlines, with a
299 // blank line if catch had a prefix.
300 if (tfrom && tfrom != throw->help) {
301 if (throw->help || catch->help) dlist_add(&cfrom, strdup(""));
303 dlist_add(&cfrom, 0);
304 anchor = cfrom->prev;
306 while (throw->help && throw->help != tfrom)
307 dlist_add(&cfrom, dlist_zap(&throw->help));
308 if (cfrom && cfrom->prev->data && *trim(cfrom->prev->data))
309 dlist_add(&cfrom, strdup(""));
312 dlist_add(&cfrom, 0);
313 anchor = cfrom->prev;
316 // Splice sorted lines back in place
320 for (clen = 0; clen < tlen; clen++)
321 dlist_add(&cfrom, cdashlines[clen]);
324 // If there were no dashlines, text would be considered prefix, so
325 // the list is definitely no longer empty, so discard placeholder.
326 if (!anchor->data) dlist_zap(&anchor);
328 // zap whitespace at end of catch help text
329 while (!*trim(anchor->prev->data)) {
330 anchor = anchor->prev;
331 free(dlist_zap(&anchor));
334 // Append trailing lines.
335 while (tfrom) dlist_add(&anchor, dlist_zap(&tfrom));
337 // Collate first [-abc] option block in usage: lines
339 if (*this == '[' && this[1] == '-' && this[2] != '-' &&
340 *that == '[' && that[1] == '-' && that[2] != '-')
342 char *from = this+2, *to = that+2;
343 int ff = strcspn(from, " ]"), tt = strcspn(to, " ]");
345 if (from[ff] == ']' && to[tt] == ']') {
346 try = xmprintf("[-%.*s%.*s] ", ff, from, tt, to);
347 qsort(try+2, ff+tt, 1, (void *)charsort);
348 this = trim(this+ff+3);
349 that = trim(that+tt+3);
353 // The list is definitely no longer empty, so discard placeholder.
354 if (!anchor->data) dlist_zap(&anchor);
356 // Add new collated line (and whitespace).
357 dlist_add(&anchor, xmprintf("%*cusage: %.*s %s%s%s%s",
358 catch->help_indent, ' ', len, name, try ? try : "",
359 this, *this ? " " : "", that));
361 dlist_add(&anchor, strdup(""));
366 throw->help = anchor->prev->prev;
369 this = throw->help->data + throw->help_indent + 8 + len;
378 // Print out help #defines
380 struct double_list *dd;
384 char *s = xstrdup(sym->name);
386 for (i = 0; s[i]; i++) s[i] = tolower(s[i]);
387 printf("#define HELP_%s \"", s);
392 i = sym->help_indent;
394 // Trim leading whitespace
396 while (isspace(*s) && i) {
400 for (i=0; s[i]; i++) {
401 if (s[i] == '"' || s[i] == '\\') putchar('\\');
407 if (dd == sym->help) break;