1 // 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.
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.
22 // replace chopped out USE_BLAH() sections with low-ascii characters
23 // showing how many flags got skipped
25 char *mark_gaps(char *flags, char *all)
29 // Shell feeds in " " for blank args, leading space not meaningful.
30 while (isspace(*flags)) flags++;
31 while (isspace(*all)) all++;
33 n = new = strdup(all);
42 if (strchr("?&^-:#|@*; ", c));
43 else if (strchr("=<>", c)) while (isdigit(*all)) all++;
44 else if (c == '(') while(*(all++) != ')');
52 // Break down a command string into struct flag list.
54 struct flag *digest(char *string)
56 struct flag *list = NULL;
60 // Groups must be at end.
61 if (*string == '[') break;
65 struct flag *new = calloc(sizeof(struct flag), 1);
67 new->command = ++string;
69 // Attach longopt to previous short opt, if any.
70 if (list && list->command) {
71 new->next = list->lopt;
74 struct flag *blank = calloc(sizeof(struct flag), 1);
80 // An empty longopt () would break this.
81 while (*++string != ')') if (*string == '-') *string = '_';
86 if (strchr("?&^-:#|@*; ", *string)) string++;
87 else if (strchr("=<>", *string)) {
88 if (!isdigit(string[1])) {
89 fprintf(stderr, "%c without number in '%s'", *string, err);
92 while (isdigit(*++string)) {
99 struct flag *new = calloc(sizeof(struct flag), 1);
101 new->command = string++;
110 int main(int argc, char *argv[])
112 char command[256], flags[1023], allflags[1024];
113 char *out, *outbuf = malloc(1024*1024);
115 // Yes, the output buffer is 1 megabyte with no bounds checking.
116 // See "intentionally crappy", above.
117 if (!(out = outbuf)) return 1;
119 printf("#undef FORCED_FLAG\n#undef FORCED_FLAGLL\n"
120 "#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1LL\n"
121 "#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0\n#endif\n\n");
124 struct flag *flist, *aflist, *offlist;
128 *command = *flags = *allflags = 0;
129 bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
130 command, flags, allflags);
133 fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
134 command, flags, allflags);
136 if (!*command) break;
138 fprintf(stderr, "\nError in %s (see generated/flags.raw)\n", command);
143 printf("// %s %s %s\n", command, flags, allflags);
144 if (*flags != ' ') mgaps = mark_gaps(flags, allflags);
145 else if (*allflags != ' ') mgaps = allflags;
146 // If command disabled, use allflags for OLDTOY()
147 printf("#undef OPTSTR_%s\n#define OPTSTR_%s ", command, command);
148 if (mgaps) printf("\"%s\"\n", mgaps);
150 if (mgaps != allflags) free(mgaps);
152 flist = digest(flags);
153 offlist = aflist = digest(allflags);
155 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
156 command, command, command);
159 struct flag *f = offlist->lopt;
161 printf("#undef FLAG_%s\n", f->command);
164 if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
165 offlist = offlist->next;
167 printf("#endif\n\n");
169 sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
174 char *llstr = bit>31 ? "LL" : "";
176 // Output flag macro for bare longopts
178 if (flist && flist->lopt &&
179 !strcmp(flist->lopt->command, aflist->lopt->command))
181 sprintf(out, "#define FLAG_%s (1%s<<%d)\n", flist->lopt->command,
183 flist->lopt = flist->lopt->next;
184 } else sprintf(out, "#define FLAG_%s (FORCED_FLAG%s<<%d)\n",
185 aflist->lopt->command, llstr, bit);
186 aflist->lopt = aflist->lopt->next;
187 if (!aflist->command) {
188 aflist = aflist->next;
190 if (flist) flist = flist->next;
192 // Output normal flag macro
193 } else if (aflist->command) {
194 if (flist && flist->command && *aflist->command == *flist->command) {
196 sprintf(out, "#define FLAG_%c (1%s<<%d)\n", *aflist->command,
199 } else sprintf(out, "#define FLAG_%c (FORCED_FLAG%s<<%d)\n",
200 *aflist->command, llstr, bit);
202 aflist = aflist->next;
206 out = stpcpy(out, "#endif\n\n");
209 if (fflush(0) && ferror(stdout)) return 1;
213 int i = write(1, outbuf, strlen(outbuf));