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]; |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 75 | char *out, *outbuf = malloc(1024*1024); |
| 76 | |
| 77 | // Yes, the output buffer is 1 megabyte with no bounds checking. |
| 78 | // See "intentionally crappy", above. |
| 79 | if (!(out = outbuf)) return 1; |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 80 | |
| 81 | for (;;) { |
| 82 | struct flag *flist, *aflist, *offlist; |
| 83 | unsigned bit = 0; |
| 84 | |
| 85 | if (3 != fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n", |
| 86 | command, flags, allflags)) break; |
| 87 | |
| 88 | printf("// %s %s %s\n", command, flags, allflags); |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 89 | |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 90 | flist = digest(flags); |
| 91 | offlist = aflist = digest(allflags); |
| 92 | |
Rob Landley | a235901 | 2014-02-16 17:31:33 -0600 | [diff] [blame] | 93 | printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n", |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 94 | command, command, command); |
| 95 | |
| 96 | while (offlist) { |
| 97 | struct flag *f = offlist->lopt; |
| 98 | while (f) { |
| 99 | printf("#undef FLAG_%s\n", f->command); |
| 100 | f = f->next; |
| 101 | } |
| 102 | if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command); |
| 103 | offlist = offlist->next; |
| 104 | } |
| 105 | printf("#endif\n\n"); |
| 106 | |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 107 | sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n", |
| 108 | command, command); |
| 109 | out += strlen(out); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 110 | |
| 111 | while (aflist) { |
| 112 | if (aflist->lopt) { |
| 113 | if (flist && flist->lopt && |
| 114 | !strcmp(flist->lopt->command, aflist->lopt->command)) |
| 115 | { |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 116 | sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 117 | flist->lopt = flist->lopt->next; |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 118 | } else sprintf(out, "#define FLAG_%s 0\n", aflist->lopt->command); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 119 | aflist->lopt = aflist->lopt->next; |
Rob Landley | 5a2583a | 2014-02-08 10:53:26 -0600 | [diff] [blame] | 120 | if (!aflist->command) aflist = aflist->next; |
| 121 | } else if (aflist->command) { |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 122 | if (flist && (!aflist->command || *aflist->command == *flist->command)) |
| 123 | { |
| 124 | if (aflist->command) |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 125 | sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 126 | bit++; |
| 127 | flist = flist->next; |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 128 | } else sprintf(out, "#define FLAG_%c 0\n", *aflist->command); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 129 | aflist = aflist->next; |
| 130 | } |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 131 | out += strlen(out); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 132 | } |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 133 | sprintf(out, "#endif\n\n"); |
| 134 | out += strlen(out); |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Rob Landley | 6ebe03d | 2014-02-24 23:34:43 -0600 | [diff] [blame^] | 137 | if (fflush(0) && ferror(stdout)) return 1; |
| 138 | |
| 139 | out = outbuf; |
| 140 | while (*out) { |
| 141 | int i = write(1, outbuf, strlen(outbuf)); |
| 142 | |
| 143 | if (i<0) return 1; |
| 144 | out += i; |
| 145 | } |
Rob Landley | 207cada | 2013-10-03 03:18:00 -0500 | [diff] [blame] | 146 | } |