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.
20 // replace chopped out USE_BLAH() sections with low-ascii characters
21 // showing how many flags got skipped
23 char *mark_gaps(char *flags, char *all)
27 // Shell feeds in " " for blank args, leading space not meaningful.
28 while (isspace(*flags)) flags++;
29 while (isspace(*all)) all++;
31 n = new = strdup(all);
40 if (strchr("?&^-:#|@*; ", c));
41 else if (strchr("=<>", c)) while (isdigit(*all)) all++;
42 else if (c == '(') while(*(all++) != ')');
50 // Break down a command string into struct flag list.
52 struct flag *digest(char *string)
54 struct flag *list = NULL;
58 // Groups must be at end.
59 if (*string == '[') break;
63 struct flag *new = calloc(sizeof(struct flag), 1);
65 new->command = ++string;
67 // Attach longopt to previous short opt, if any.
68 if (list && list->command) {
69 new->next = list->lopt;
72 struct flag *blank = calloc(sizeof(struct flag), 1);
78 // An empty longopt () would break this.
79 while (*++string != ')') if (*string == '-') *string = '_';
84 if (strchr("?&^-:#|@*; ", *string)) string++;
85 else if (strchr("=<>", *string)) {
86 if (!isdigit(string[1])) {
87 fprintf(stderr, "%c without number in '%s'", *string, err);
90 while (isdigit(*++string)) {
97 struct flag *new = calloc(sizeof(struct flag), 1);
99 new->command = string++;
108 int main(int argc, char *argv[])
110 char command[256], flags[1023], allflags[1024];
111 char *out, *outbuf = malloc(1024*1024);
113 // Yes, the output buffer is 1 megabyte with no bounds checking.
114 // See "intentionally crappy", above.
115 if (!(out = outbuf)) return 1;
117 printf("#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n"
118 "#else\n#define FORCED_FLAG 0\n#endif\n\n");
121 struct flag *flist, *aflist, *offlist;
122 char *gaps, *mgaps, c;
125 *command = *flags = *allflags = 0;
126 bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
127 command, flags, allflags);
130 fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
131 command, flags, allflags);
133 if (!*command) break;
135 fprintf(stderr, "\nError in %s (duplicate command?)\n", command);
140 printf("// %s %s %s\n", command, flags, allflags);
141 mgaps = mark_gaps(flags, allflags);
142 for (gaps = mgaps; *gaps == 1; gaps++);
148 printf("#undef OPTSTR_%s\n#define OPTSTR_%s %c%s%c\n",
149 command, command, c, gaps, c);
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",
175 if (flist && flist->lopt &&
176 !strcmp(flist->lopt->command, aflist->lopt->command))
178 sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit);
179 flist->lopt = flist->lopt->next;
180 } else sprintf(out, "#define FLAG_%s (FORCED_FLAG<<%d)\n",
181 aflist->lopt->command, bit);
182 aflist->lopt = aflist->lopt->next;
183 if (!aflist->command) {
184 aflist = aflist->next;
186 if (flist) flist = flist->next;
188 } else if (aflist->command) {
189 if (flist && (!flist->command || *aflist->command == *flist->command)) {
191 sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit);
193 } else sprintf(out, "#define FLAG_%c (FORCED_FLAG<<%d)\n",
194 *aflist->command, bit);
196 aflist = aflist->next;
200 out = stpcpy(out, "#endif\n\n");
203 if (fflush(0) && ferror(stdout)) return 1;
207 int i = write(1, outbuf, strlen(outbuf));