Mercurial > hg > toybox
annotate scripts/config2help.c @ 1566:62a7d617e1ce draft 0.5.1
Make md5sum and sha1sum work on big endian systems.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 19 Nov 2014 21:38:00 -0600 |
parents | 2014f64a141b |
children |
rev | line source |
---|---|
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 #include "toys.h" |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 // Humor toys.h |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 struct toy_context toys; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 char libbuf[4096], toybuf[4096]; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 void show_help(void) {;} |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 void toy_exec(char *argv[]) {;} |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 // Parse config files into data structures. |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 struct symbol { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
12 struct symbol *next; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
13 int enabled, help_indent; |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
14 char *name, *depends; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
15 struct double_list *help; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
16 } *sym; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
17 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
18 char *trim(char *s) |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
19 { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
20 while (isspace(*s)) s++; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
21 |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
22 return s; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
23 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
24 |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 char *keyword(char *name, char *line) |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 int len = strlen(name); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
29 line = trim(line); |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 if (strncmp(name, line, len)) return 0; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 line += len; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 if (*line && !isspace(*line)) return 0; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
33 line = trim(line); |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 return line; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
38 char *dlist_zap(struct double_list **help) |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
39 { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
40 struct double_list *dd = dlist_pop(help); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
41 char *s = dd->data; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
42 |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
43 free(dd); |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
44 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
45 return s; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
46 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
47 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
48 int zap_blank_lines(struct double_list **help) |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
49 { |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
50 int got = 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
51 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
52 while (*help) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
53 char *s; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
54 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
55 s = trim((*help)->data); |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
56 |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
57 if (*s) break; |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
58 got++; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
59 free(dlist_zap(help)); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
60 } |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
61 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
62 return got; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
63 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
64 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
65 // Collect "-a blah" description lines following a blank line (or start). |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
66 // Returns array of removed lines with *len entries (0 for none). |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
67 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
68 // Moves *help to new start of text (in case dash lines were at beginning). |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
69 // Sets *from to where dash lines removed from (in case they weren't). |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
70 // Discards blank lines before and after dashlines. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
71 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
72 // If no prefix, *help NULL. If no postfix, *from == *help |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
73 // if no dashlines returned *from == *help. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
74 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
75 char **grab_dashlines(struct double_list **help, struct double_list **from, |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
76 int *len) |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
77 { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
78 struct double_list *dd; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
79 char *s, **list; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
80 int count = 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
81 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
82 *len = 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
83 zap_blank_lines(help); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
84 *from = *help; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
85 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
86 // Find start of dash block. Must be at start or after blank line. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
87 for (;;) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
88 s = trim((*from)->data); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
89 if (*s == '-' && s[1] != '-' && !count) break; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
90 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
91 if (!*s) count = 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
92 else count++; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
93 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
94 *from = (*from)->next; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
95 if (*from == *help) return 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
96 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
97 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
98 // If there was whitespace before this, zap it. This can't take out *help |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
99 // because zap_blank_lines skipped blank lines, and we had to have at least |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
100 // one non-blank line (a dash line) to get this far. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
101 while (!*trim((*from)->prev->data)) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
102 *from = (*from)->prev; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
103 free(dlist_zap(from)); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
104 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
105 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
106 // Count number of dashlines, copy out to array, zap trailing whitespace |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
107 // If *help was at start of dashblock, move it with *from |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
108 count = 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
109 dd = *from; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
110 if (*help == *from) *help = 0; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
111 for (;;) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
112 if (*trim(dd->data) != '-') break; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
113 count++; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
114 if (*from == (dd = dd->next)) break; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
115 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
116 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
117 list = xmalloc(sizeof(char *)*count); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
118 *len = count; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
119 while (count) list[--count] = dlist_zap(from); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
120 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
121 return list; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
122 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
123 |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
124 void parse(char *filename) |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
125 { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
126 FILE *fp = xfopen(filename, "r"); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
127 struct symbol *new = 0; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
128 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
129 for (;;) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 char *s, *line = NULL; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 size_t len; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
133 // Read line, trim whitespace at right edge. |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
134 if (getline(&line, &len, fp) < 1) break; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
135 s = line+strlen(line); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
136 while (--s >= line) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
137 if (!isspace(*s)) break; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
138 *s = 0; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
139 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
140 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
141 // source or config keyword at left edge? |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
142 if (*line && !isspace(*line)) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
143 if ((s = keyword("config", line))) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 new = xzalloc(sizeof(struct symbol)); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
145 new->next = sym; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
146 new->name = s; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
147 sym = new; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
148 } else if ((s = keyword("source", line))) parse(s); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
149 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
150 continue; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
151 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
152 if (!new) continue; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
153 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
154 if (sym && sym->help_indent) { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
155 dlist_add(&(new->help), line); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
156 if (sym->help_indent < 0) { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
157 sym->help_indent = 0; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
158 while (isspace(line[sym->help_indent])) sym->help_indent++; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
159 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
160 } |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 else if ((s = keyword("depends", line)) && (s = keyword("on", s))) |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
162 new->depends = s; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
163 else if (keyword("help", line)) sym->help_indent = -1; |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 fclose(fp); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
167 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
168 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
169 int charsort(void *a, void *b) |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
170 { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
171 char *aa = a, *bb = b; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
172 |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
173 if (*aa < *bb) return -1; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
174 if (*aa > *bb) return 1; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
175 return 0; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
176 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
177 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
178 int dashsort(char **a, char **b) |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
179 { |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
180 char *aa = *a, *bb = *b; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
181 |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
182 if (aa[1] < bb[1]) return -1; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
183 if (aa[1] > bb[1]) return 1; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
184 return 0; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
185 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
186 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
187 int dashlinesort(char **a, char **b) |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
188 { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
189 return strcmp(*a, *b); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
190 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
191 |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
192 int main(int argc, char *argv[]) |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
193 { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
194 FILE *fp; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
195 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
196 if (argc != 3) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
197 fprintf(stderr, "usage: config2help Config.in .config\n"); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
198 exit(1); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
199 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
200 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
201 // Read Config.in |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
202 parse(argv[1]); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
203 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
204 // read .config |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
205 fp = xfopen(argv[2], "r"); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
206 for (;;) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
207 char *line = NULL; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
208 size_t len; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
209 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
210 if (getline(&line, &len, fp) < 1) break; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
211 if (!strncmp("CONFIG_", line, 7)) { |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
212 struct symbol *try; |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
213 char *s = line+7; |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
214 |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
215 for (try=sym; try; try=try->next) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
216 len = strlen(try->name); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
217 if (!strncmp(try->name, s, len) && s[len]=='=' && s[len+1]=='y') { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
218 try->enabled++; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
219 break; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
220 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
221 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
222 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
223 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
224 |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
225 // Collate help according to usage, depends, and .config |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
226 |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
227 // Loop through each entry, finding duplicate enabled "usage:" names |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
228 // This is in reverse order, so last entry gets collated with previous |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
229 // entry until we run out of matching pairs. |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
230 for (;;) { |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
231 struct symbol *throw = 0, *catch; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
232 char *this, *that, *cusage, *tusage, *name; |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
233 int len; |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
234 |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
235 // find a usage: name and collate all enabled entries with that name |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
236 for (catch = sym; catch; catch = catch->next) { |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
237 if (catch->enabled != 1) continue; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
238 if (catch->help && (that = keyword("usage:", catch->help->data))) { |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
239 struct double_list *cfrom, *tfrom, *anchor; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
240 char *try, **cdashlines, **tdashlines; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
241 int clen, tlen; |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
242 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
243 // Align usage: lines, finding a matching pair so we can suck help |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
244 // text out of throw into catch, copying from this to that |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
245 if (!throw) name = that; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
246 else if (strncmp(name, that, len) || !isspace(that[len])) continue; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
247 catch->enabled++; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
248 while (!isspace(*that) && *that) that++; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
249 if (!throw) len = that-name; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
250 that = trim(that); |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
251 if (!throw) { |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
252 throw = catch; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
253 this = that; |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
254 |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
255 continue; |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
256 } |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
257 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
258 // Grab option description lines to collate from catch and throw |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
259 tusage = dlist_zap(&throw->help); |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
260 tdashlines = grab_dashlines(&throw->help, &tfrom, &tlen); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
261 cusage = dlist_zap(&catch->help); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
262 cdashlines = grab_dashlines(&catch->help, &cfrom, &clen); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
263 anchor = catch->help; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
264 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
265 // If we've got both, collate and alphebetize |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
266 if (cdashlines && tdashlines) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
267 char **new = xmalloc(sizeof(char *)*(clen+tlen)); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
268 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
269 memcpy(new, cdashlines, sizeof(char *)*clen); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
270 memcpy(new+clen, tdashlines, sizeof(char *)*tlen); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
271 free(cdashlines); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
272 free(tdashlines); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
273 qsort(new, clen+tlen, sizeof(char *), (void *)dashlinesort); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
274 cdashlines = new; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
275 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
276 // If just one, make sure it's in catch. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
277 } else if (tdashlines) cdashlines = tdashlines; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
278 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
279 // If throw had a prefix, insert it before dashlines, with a |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
280 // blank line if catch had a prefix. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
281 if (tfrom && tfrom != throw->help) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
282 if (throw->help || catch->help) dlist_add(&cfrom, strdup("")); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
283 else { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
284 dlist_add(&cfrom, 0); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
285 anchor = cfrom->prev; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
286 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
287 while (throw->help && throw->help != tfrom) |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
288 dlist_add(&cfrom, dlist_zap(&throw->help)); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
289 if (cfrom && cfrom->prev->data && *trim(cfrom->prev->data)) |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
290 dlist_add(&cfrom, strdup("")); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
291 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
292 if (!anchor) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
293 dlist_add(&cfrom, 0); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
294 anchor = cfrom->prev; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
295 } |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
296 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
297 // Splice sorted lines back in place |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
298 if (cdashlines) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
299 tlen += clen; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
300 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
301 for (clen = 0; clen < tlen; clen++) |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
302 dlist_add(&cfrom, cdashlines[clen]); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
303 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
304 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
305 // If there were no dashlines, text would be considered prefix, so |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
306 // the list is definitely no longer empty, so discard placeholder. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
307 if (!anchor->data) dlist_zap(&anchor); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
308 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
309 // zap whitespace at end of catch help text |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
310 while (!*trim(anchor->prev->data)) { |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
311 anchor = anchor->prev; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
312 free(dlist_zap(&anchor)); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
313 } |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
314 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
315 // Append trailing lines. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
316 while (tfrom) dlist_add(&anchor, dlist_zap(&tfrom)); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
317 |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
318 // Collate first [-abc] option block in usage: lines |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
319 try = 0; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
320 if (*this == '[' && this[1] == '-' && this[2] != '-' && |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
321 *that == '[' && that[1] == '-' && that[2] != '-') |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
322 { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
323 char *from = this+2, *to = that+2; |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
324 int ff = strcspn(from, " ]"), tt = strcspn(to, " ]"); |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
325 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
326 if (from[ff] == ']' && to[tt] == ']') { |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
327 try = xmprintf("[-%.*s%.*s] ", ff, from, tt, to); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
328 qsort(try+2, ff+tt, 1, (void *)charsort); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
329 this = trim(this+ff+3); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
330 that = trim(that+tt+3); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
331 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
332 } |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
333 |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
334 // The list is definitely no longer empty, so discard placeholder. |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
335 if (!anchor->data) dlist_zap(&anchor); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
336 |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
337 // Add new collated line (and whitespace). |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
338 dlist_add(&anchor, xmprintf("%*cusage: %.*s %s%s%s%s", |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
339 catch->help_indent, ' ', len, name, try ? try : "", |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
340 this, *this ? " " : "", that)); |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
341 free(try); |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
342 dlist_add(&anchor, strdup("")); |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
343 free(cusage); |
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
344 free(tusage); |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
345 throw->enabled = 0; |
1188
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
346 throw = catch; |
cb62f7090473
More elaborate help text collating logic.
Rob Landley <rob@landley.net>
parents:
1184
diff
changeset
|
347 throw->help = anchor->prev->prev; |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
348 |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
349 throw = catch; |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
350 this = throw->help->data + throw->help_indent + 8 + len; |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
351 } |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
352 } |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
353 |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
354 // Did we find one? |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
355 |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
356 if (!throw) break; |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
357 } |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
358 |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
359 // Print out help #defines |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
360 while (sym) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
361 struct double_list *dd; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
362 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
363 if (sym->help) { |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
364 int i; |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
365 char *s = xstrdup(sym->name); |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
366 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
367 for (i = 0; s[i]; i++) s[i] = tolower(s[i]); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
368 printf("#define help_%s \"", s); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
369 free(s); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
370 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
371 dd = sym->help; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
372 for (;;) { |
1184
48334d7a2b55
Collate usage: lines in help text.
Rob Landley <rob@landley.net>
parents:
1182
diff
changeset
|
373 i = sym->help_indent; |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
374 |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
375 // Trim leading whitespace |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
376 s = dd->data; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
377 while (isspace(*s) && i) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
378 s++; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
379 i--; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
380 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
381 for (i=0; s[i]; i++) { |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
382 if (s[i] == '"' || s[i] == '\\') putchar('\\'); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
383 putchar(s[i]); |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
384 } |
1182
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
385 putchar('\\'); |
7079ea384a2e
Help text, collate usage blocks (badly).
Rob Landley <rob@landley.net>
parents:
1179
diff
changeset
|
386 putchar('n'); |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
387 dd = dd->next; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
388 if (dd == sym->help) break; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
389 } |
1242
2014f64a141b
generated/help.h is a lot easier to read with an extra newline between each help entry.
Rob Landley <rob@landley.net>
parents:
1202
diff
changeset
|
390 printf("\"\n\n"); |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
391 } |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
392 sym = sym->next; |
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
393 } |
1202
4f080cdb2f6e
Various cleanups found by Tom Sparrow's static analysis.
Rob Landley <rob@landley.net>
parents:
1188
diff
changeset
|
394 |
4f080cdb2f6e
Various cleanups found by Tom Sparrow's static analysis.
Rob Landley <rob@landley.net>
parents:
1188
diff
changeset
|
395 return 0; |
1179
260cce6c3e35
Replace python help converter with C implementation.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
396 } |