blob: e0e833ce3f7d1193e93a3c96f01e3fc2f54060b2 [file] [log] [blame]
Rob Landley207cada2013-10-03 03:18:00 -05001// Take three word input lines on stdin (the three space separated words are
2// command name, option string with current config, option string from
3// allyesconfig; space separated, the last two are and double quotes)
4// and produce flag #defines to stdout.
5
6// This is intentionally crappy code because we control the inputs. It leaks
7// memory like a sieve and segfaults if malloc returns null, but does the job.
8
Isaac Dunham79191b62015-08-04 19:40:56 -07009#include <unistd.h>
Rob Landley207cada2013-10-03 03:18:00 -050010#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <errno.h>
Isaac Dunham79191b62015-08-04 19:40:56 -070014#include <ctype.h>
Rob Landley207cada2013-10-03 03:18:00 -050015
16struct flag {
17 struct flag *next;
18 char *command;
19 struct flag *lopt;
20};
21
Rob Landley0322afb2016-12-07 21:52:00 -060022int chrtype(char c)
23{
24 if (strchr("?&^-:#|@*; ", c)) return 1;
25 if (strchr("=<>", c)) return 2;
26
27 return 0;
28}
29
Rob Landleyf3e56f42014-12-31 21:30:59 -060030// replace chopped out USE_BLAH() sections with low-ascii characters
31// showing how many flags got skipped
32
33char *mark_gaps(char *flags, char *all)
34{
35 char *n, *new, c;
Rob Landley0322afb2016-12-07 21:52:00 -060036 int bare = 1;
Rob Landleyf3e56f42014-12-31 21:30:59 -060037
38 // Shell feeds in " " for blank args, leading space not meaningful.
39 while (isspace(*flags)) flags++;
40 while (isspace(*all)) all++;
41
42 n = new = strdup(all);
43 while (*all) {
Rob Landley0322afb2016-12-07 21:52:00 -060044 // --longopt parentheticals dealt with as a unit
45 if (*all == '(') {
46 int len = 0;
47
48 while (all[len++] != ')');
49 if (strncmp(flags, all, len)) {
50 // bare longopts need their own skip placeholders
51 if (bare) *(new++) = 1;
52 } else {
53 memcpy(new, all, len);
54 new += len;
55 flags += len;
56 }
57 all += len;
Rob Landleyc187be72016-12-07 23:05:33 -060058 continue;
Rob Landley0322afb2016-12-07 21:52:00 -060059 }
60 c = *(all++);
61 if (bare) bare = chrtype(c);
62 if (*flags == c) {
63 *(new++) = c;
Rob Landleyf3e56f42014-12-31 21:30:59 -060064 *flags++;
65 continue;
66 }
67
Rob Landley0322afb2016-12-07 21:52:00 -060068 c = chrtype(c);
69 if (!c) *(new++) = 1;
70 else if (c==2) while (isdigit(*all)) all++;
Rob Landleyf3e56f42014-12-31 21:30:59 -060071 }
72 *new = 0;
73
74 return n;
75}
76
Rob Landley207cada2013-10-03 03:18:00 -050077// Break down a command string into struct flag list.
78
79struct flag *digest(char *string)
80{
81 struct flag *list = NULL;
Rob Landleyf3e56f42014-12-31 21:30:59 -060082 char *err = string;
Rob Landley207cada2013-10-03 03:18:00 -050083
84 while (*string) {
85 // Groups must be at end.
86 if (*string == '[') break;
87
88 // Longopts
89 if (*string == '(') {
90 struct flag *new = calloc(sizeof(struct flag), 1);
91
92 new->command = ++string;
93
94 // Attach longopt to previous short opt, if any.
95 if (list && list->command) {
96 new->next = list->lopt;
97 list->lopt = new;
98 } else {
99 struct flag *blank = calloc(sizeof(struct flag), 1);
100
101 blank->next = list;
102 blank->lopt = new;
103 list = blank;
104 }
Rob Landley3a415412014-05-10 13:06:31 -0500105 // An empty longopt () would break this.
106 while (*++string != ')') if (*string == '-') *string = '_';
Rob Landley207cada2013-10-03 03:18:00 -0500107 *(string++) = 0;
108 continue;
109 }
110
111 if (strchr("?&^-:#|@*; ", *string)) string++;
112 else if (strchr("=<>", *string)) {
Rob Landleyf3e56f42014-12-31 21:30:59 -0600113 if (!isdigit(string[1])) {
114 fprintf(stderr, "%c without number in '%s'", *string, err);
115 exit(1);
116 }
Rob Landley207cada2013-10-03 03:18:00 -0500117 while (isdigit(*++string)) {
118 if (!list) {
119 string++;
120 break;
121 }
122 }
123 } else {
124 struct flag *new = calloc(sizeof(struct flag), 1);
125
126 new->command = string++;
127 new->next = list;
128 list = new;
129 }
130 }
131
132 return list;
133}
134
135int main(int argc, char *argv[])
136{
137 char command[256], flags[1023], allflags[1024];
Rob Landley6ebe03d2014-02-24 23:34:43 -0600138 char *out, *outbuf = malloc(1024*1024);
139
140 // Yes, the output buffer is 1 megabyte with no bounds checking.
141 // See "intentionally crappy", above.
142 if (!(out = outbuf)) return 1;
Rob Landley207cada2013-10-03 03:18:00 -0500143
Rob Landley36b7cb72016-01-18 15:45:56 -0600144 printf("#undef FORCED_FLAG\n#undef FORCED_FLAGLL\n"
145 "#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1LL\n"
146 "#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0\n#endif\n\n");
Rob Landleyf3e56f42014-12-31 21:30:59 -0600147
Rob Landley207cada2013-10-03 03:18:00 -0500148 for (;;) {
149 struct flag *flist, *aflist, *offlist;
Rob Landley48ea5592016-07-17 02:14:35 -0500150 char *mgaps = 0;
Rob Landley3a415412014-05-10 13:06:31 -0500151 unsigned bit;
Rob Landley207cada2013-10-03 03:18:00 -0500152
Rob Landley5640acb2015-05-03 12:41:05 -0500153 *command = *flags = *allflags = 0;
Rob Landley3a415412014-05-10 13:06:31 -0500154 bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
155 command, flags, allflags);
Rob Landley207cada2013-10-03 03:18:00 -0500156
Rob Landley5640acb2015-05-03 12:41:05 -0500157 if (getenv("DEBUG"))
158 fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
159 command, flags, allflags);
160
Rob Landley3a415412014-05-10 13:06:31 -0500161 if (!*command) break;
162 if (bit != 3) {
Rob Landley6fde0f92015-08-30 04:42:49 -0500163 fprintf(stderr, "\nError in %s (see generated/flags.raw)\n", command);
Rob Landley3a415412014-05-10 13:06:31 -0500164 exit(1);
165 }
166
167 bit = 0;
Rob Landley207cada2013-10-03 03:18:00 -0500168 printf("// %s %s %s\n", command, flags, allflags);
Rob Landley48ea5592016-07-17 02:14:35 -0500169 if (*flags != ' ') mgaps = mark_gaps(flags, allflags);
170 else if (*allflags != ' ') mgaps = allflags;
Rob Landleyadef5dc2016-07-15 04:45:08 -0500171 // If command disabled, use allflags for OLDTOY()
Rob Landley48ea5592016-07-17 02:14:35 -0500172 printf("#undef OPTSTR_%s\n#define OPTSTR_%s ", command, command);
173 if (mgaps) printf("\"%s\"\n", mgaps);
174 else printf("0\n");
175 if (mgaps != allflags) free(mgaps);
Rob Landley6ebe03d2014-02-24 23:34:43 -0600176
Rob Landley207cada2013-10-03 03:18:00 -0500177 flist = digest(flags);
178 offlist = aflist = digest(allflags);
179
Rob Landleya2359012014-02-16 17:31:33 -0600180 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
Rob Landley207cada2013-10-03 03:18:00 -0500181 command, command, command);
182
183 while (offlist) {
184 struct flag *f = offlist->lopt;
185 while (f) {
186 printf("#undef FLAG_%s\n", f->command);
187 f = f->next;
188 }
189 if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
190 offlist = offlist->next;
191 }
192 printf("#endif\n\n");
193
Rob Landley6ebe03d2014-02-24 23:34:43 -0600194 sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
195 command, command);
196 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500197
198 while (aflist) {
Rob Landleyaaecbba2015-12-10 15:57:08 -0600199 char *llstr = bit>31 ? "LL" : "";
200
201 // Output flag macro for bare longopts
Rob Landley207cada2013-10-03 03:18:00 -0500202 if (aflist->lopt) {
203 if (flist && flist->lopt &&
204 !strcmp(flist->lopt->command, aflist->lopt->command))
205 {
Rob Landleyaaecbba2015-12-10 15:57:08 -0600206 sprintf(out, "#define FLAG_%s (1%s<<%d)\n", flist->lopt->command,
207 llstr, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500208 flist->lopt = flist->lopt->next;
Rob Landleyaaecbba2015-12-10 15:57:08 -0600209 } else sprintf(out, "#define FLAG_%s (FORCED_FLAG%s<<%d)\n",
210 aflist->lopt->command, llstr, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500211 aflist->lopt = aflist->lopt->next;
Ashwini Sharma882ca8b2014-04-09 07:40:02 -0500212 if (!aflist->command) {
213 aflist = aflist->next;
Rob Landleyf3e56f42014-12-31 21:30:59 -0600214 bit++;
215 if (flist) flist = flist->next;
Ashwini Sharma882ca8b2014-04-09 07:40:02 -0500216 }
Rob Landleyaaecbba2015-12-10 15:57:08 -0600217 // Output normal flag macro
Rob Landley5a2583a2014-02-08 10:53:26 -0600218 } else if (aflist->command) {
Rob Landleyec0b4822016-06-30 20:41:07 -0500219 if (flist && flist->command && *aflist->command == *flist->command) {
Rob Landley207cada2013-10-03 03:18:00 -0500220 if (aflist->command)
Rob Landleyaaecbba2015-12-10 15:57:08 -0600221 sprintf(out, "#define FLAG_%c (1%s<<%d)\n", *aflist->command,
222 llstr, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500223 flist = flist->next;
Rob Landleyaaecbba2015-12-10 15:57:08 -0600224 } else sprintf(out, "#define FLAG_%c (FORCED_FLAG%s<<%d)\n",
225 *aflist->command, llstr, bit);
Rob Landleyf3e56f42014-12-31 21:30:59 -0600226 bit++;
Rob Landley207cada2013-10-03 03:18:00 -0500227 aflist = aflist->next;
228 }
Rob Landley6ebe03d2014-02-24 23:34:43 -0600229 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500230 }
Rob Landleyf3e56f42014-12-31 21:30:59 -0600231 out = stpcpy(out, "#endif\n\n");
Rob Landley207cada2013-10-03 03:18:00 -0500232 }
233
Rob Landley6ebe03d2014-02-24 23:34:43 -0600234 if (fflush(0) && ferror(stdout)) return 1;
235
236 out = outbuf;
237 while (*out) {
238 int i = write(1, outbuf, strlen(outbuf));
239
240 if (i<0) return 1;
241 out += i;
242 }
Hyejin Kim3b741472015-03-04 07:29:57 -0600243
244 return 0;
Rob Landley207cada2013-10-03 03:18:00 -0500245}