Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 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. |
| 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 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <errno.h> |
| 13 | |
| 14 | struct flag { |
| 15 | struct flag *next; |
| 16 | char *command; |
| 17 | struct flag *lopt; |
| 18 | }; |
| 19 | |
| 20 | // Break down a command string into struct flag list. |
| 21 | |
| 22 | struct flag *digest(char *string) |
| 23 | { |
| 24 | struct flag *list = NULL; |
| 25 | |
| 26 | while (*string) { |
| 27 | // Groups must be at end. |
| 28 | if (*string == '[') break; |
| 29 | |
| 30 | // Longopts |
| 31 | if (*string == '(') { |
| 32 | struct flag *new = calloc(sizeof(struct flag), 1); |
| 33 | |
| 34 | new->command = ++string; |
| 35 | |
| 36 | // Attach longopt to previous short opt, if any. |
| 37 | if (list && list->command) { |
| 38 | new->next = list->lopt; |
| 39 | list->lopt = new; |
| 40 | } else { |
| 41 | struct flag *blank = calloc(sizeof(struct flag), 1); |
| 42 | |
| 43 | blank->next = list; |
| 44 | blank->lopt = new; |
| 45 | list = blank; |
| 46 | } |
| 47 | while (*++string != ')'); // An empty longopt () would break this. |
| 48 | *(string++) = 0; |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | if (strchr("?&^-:#|@*; ", *string)) string++; |
| 53 | else if (strchr("=<>", *string)) { |
| 54 | while (isdigit(*++string)) { |
| 55 | if (!list) { |
| 56 | string++; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | } else { |
| 61 | struct flag *new = calloc(sizeof(struct flag), 1); |
| 62 | |
| 63 | new->command = string++; |
| 64 | new->next = list; |
| 65 | list = new; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return list; |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char *argv[]) |
| 73 | { |
| 74 | char command[256], flags[1023], allflags[1024]; |
| 75 | unsigned bit; |
| 76 | |
| 77 | for (;;) { |
| 78 | struct flag *flist, *aflist, *offlist; |
| 79 | unsigned bit = 0; |
| 80 | |
| 81 | if (3 != fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n", |
| 82 | command, flags, allflags)) break; |
| 83 | |
| 84 | printf("// %s %s %s\n", command, flags, allflags); |
| 85 | flist = digest(flags); |
| 86 | offlist = aflist = digest(allflags); |
| 87 | |
| 88 | |
| 89 | printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n#undef TT\n", |
| 90 | command, command, command); |
| 91 | |
| 92 | while (offlist) { |
| 93 | struct flag *f = offlist->lopt; |
| 94 | while (f) { |
| 95 | printf("#undef FLAG_%s\n", f->command); |
| 96 | f = f->next; |
| 97 | } |
| 98 | if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command); |
| 99 | offlist = offlist->next; |
| 100 | } |
| 101 | printf("#endif\n\n"); |
| 102 | |
| 103 | printf("#ifdef FOR_%s\n#define TT this.%s\n", command, command); |
| 104 | |
| 105 | while (aflist) { |
| 106 | if (aflist->lopt) { |
| 107 | if (flist && flist->lopt && |
| 108 | !strcmp(flist->lopt->command, aflist->lopt->command)) |
| 109 | { |
| 110 | printf("#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit); |
| 111 | flist->lopt = flist->lopt->next; |
| 112 | } else printf("#define FLAG_%s 0\n", aflist->lopt->command); |
| 113 | aflist->lopt = aflist->lopt->next; |
Rob Landley | 5a2583a | 2014-02-08 10:53:26 -0600 | [diff] [blame^] | 114 | if (!aflist->command) aflist = aflist->next; |
| 115 | } else if (aflist->command) { |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 116 | if (flist && (!aflist->command || *aflist->command == *flist->command)) |
| 117 | { |
| 118 | if (aflist->command) |
| 119 | printf("#define FLAG_%c (1<<%d)\n", *aflist->command, bit); |
| 120 | bit++; |
| 121 | flist = flist->next; |
| 122 | } else printf("#define FLAG_%c 0\n", *aflist->command); |
| 123 | aflist = aflist->next; |
| 124 | } |
| 125 | } |
| 126 | printf("#endif\n\n"); |
| 127 | } |
| 128 | |
| 129 | return fflush(0) && ferror(stdout); |
| 130 | } |