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.
24 if (strchr("?&^-:#|@*; ", c)) return 1;
25 if (strchr("=<>", c)) return 2;
30 // replace chopped out USE_BLAH() sections with low-ascii characters
31 // showing how many flags got skipped
33 char *mark_gaps(char *flags, char *all)
38 // Shell feeds in " " for blank args, leading space not meaningful.
39 while (isspace(*flags)) flags++;
40 while (isspace(*all)) all++;
42 n = new = strdup(all);
44 // --longopt parentheticals dealt with as a unit
48 while (all[len++] != ')');
49 if (strncmp(flags, all, len)) {
50 // bare longopts need their own skip placeholders
51 if (bare) *(new++) = 1;
53 memcpy(new, all, len);
61 if (bare) bare = chrtype(c);
70 else if (c==2) while (isdigit(*all)) all++;
77 // Break down a command string into struct flag list.
79 struct flag *digest(char *string)
81 struct flag *list = NULL;
85 // Groups must be at end.
86 if (*string == '[') break;
90 struct flag *new = calloc(sizeof(struct flag), 1);
92 new->command = ++string;
94 // Attach longopt to previous short opt, if any.
95 if (list && list->command) {
96 new->next = list->lopt;
99 struct flag *blank = calloc(sizeof(struct flag), 1);
105 // An empty longopt () would break this.
106 while (*++string != ')') if (*string == '-') *string = '_';
111 if (strchr("?&^-:#|@*; ", *string)) string++;
112 else if (strchr("=<>", *string)) {
113 if (!isdigit(string[1])) {
114 fprintf(stderr, "%c without number in '%s'", *string, err);
117 while (isdigit(*++string)) {
124 struct flag *new = calloc(sizeof(struct flag), 1);
126 new->command = string++;
135 int main(int argc, char *argv[])
137 char command[256], flags[1023], allflags[1024];
138 char *out, *outbuf = malloc(1024*1024);
140 // Yes, the output buffer is 1 megabyte with no bounds checking.
141 // See "intentionally crappy", above.
142 if (!(out = outbuf)) return 1;
144 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");
149 struct flag *flist, *aflist, *offlist;
153 *command = *flags = *allflags = 0;
154 bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
155 command, flags, allflags);
158 fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
159 command, flags, allflags);
161 if (!*command) break;
163 fprintf(stderr, "\nError in %s (see generated/flags.raw)\n", command);
168 printf("// %s %s %s\n", command, flags, allflags);
169 if (*flags != ' ') mgaps = mark_gaps(flags, allflags);
170 else if (*allflags != ' ') mgaps = allflags;
171 // If command disabled, use allflags for OLDTOY()
172 printf("#undef OPTSTR_%s\n#define OPTSTR_%s ", command, command);
173 if (mgaps) printf("\"%s\"\n", mgaps);
175 if (mgaps != allflags) free(mgaps);
177 flist = digest(flags);
178 offlist = aflist = digest(allflags);
180 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
181 command, command, command);
184 struct flag *f = offlist->lopt;
186 printf("#undef FLAG_%s\n", f->command);
189 if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
190 offlist = offlist->next;
192 printf("#endif\n\n");
194 sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
199 char *llstr = bit>31 ? "LL" : "";
201 // Output flag macro for bare longopts
203 if (flist && flist->lopt &&
204 !strcmp(flist->lopt->command, aflist->lopt->command))
206 sprintf(out, "#define FLAG_%s (1%s<<%d)\n", flist->lopt->command,
208 flist->lopt = flist->lopt->next;
209 } else sprintf(out, "#define FLAG_%s (FORCED_FLAG%s<<%d)\n",
210 aflist->lopt->command, llstr, bit);
211 aflist->lopt = aflist->lopt->next;
212 if (!aflist->command) {
213 aflist = aflist->next;
215 if (flist) flist = flist->next;
217 // Output normal flag macro
218 } else if (aflist->command) {
219 if (flist && flist->command && *aflist->command == *flist->command) {
221 sprintf(out, "#define FLAG_%c (1%s<<%d)\n", *aflist->command,
224 } else sprintf(out, "#define FLAG_%c (FORCED_FLAG%s<<%d)\n",
225 *aflist->command, llstr, bit);
227 aflist = aflist->next;
231 out = stpcpy(out, "#endif\n\n");
234 if (fflush(0) && ferror(stdout)) return 1;
238 int i = write(1, outbuf, strlen(outbuf));