blob: 23cb83e839486a084559ee94d733ccb975aeaaf3 [file] [log] [blame]
Rob Landley207cada2013-10-03 03:18:00 -05001// 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
14struct 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
22struct 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 }
Rob Landley3a415412014-05-10 13:06:31 -050047 // An empty longopt () would break this.
48 while (*++string != ')') if (*string == '-') *string = '_';
Rob Landley207cada2013-10-03 03:18:00 -050049 *(string++) = 0;
50 continue;
51 }
52
53 if (strchr("?&^-:#|@*; ", *string)) string++;
54 else if (strchr("=<>", *string)) {
55 while (isdigit(*++string)) {
56 if (!list) {
57 string++;
58 break;
59 }
60 }
61 } else {
62 struct flag *new = calloc(sizeof(struct flag), 1);
63
64 new->command = string++;
65 new->next = list;
66 list = new;
67 }
68 }
69
70 return list;
71}
72
73int main(int argc, char *argv[])
74{
75 char command[256], flags[1023], allflags[1024];
Rob Landley6ebe03d2014-02-24 23:34:43 -060076 char *out, *outbuf = malloc(1024*1024);
77
78 // Yes, the output buffer is 1 megabyte with no bounds checking.
79 // See "intentionally crappy", above.
80 if (!(out = outbuf)) return 1;
Rob Landley207cada2013-10-03 03:18:00 -050081
82 for (;;) {
83 struct flag *flist, *aflist, *offlist;
Rob Landley3a415412014-05-10 13:06:31 -050084 unsigned bit;
Rob Landley207cada2013-10-03 03:18:00 -050085
Rob Landley3a415412014-05-10 13:06:31 -050086 *command = 0;
87 bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
88 command, flags, allflags);
Rob Landley207cada2013-10-03 03:18:00 -050089
Rob Landley3a415412014-05-10 13:06:31 -050090 if (!*command) break;
91 if (bit != 3) {
92 fprintf(stderr, "\nError in %s (duplicate command?)\n", command);
93 exit(1);
94 }
95
96 bit = 0;
Rob Landley207cada2013-10-03 03:18:00 -050097 printf("// %s %s %s\n", command, flags, allflags);
Rob Landley6ebe03d2014-02-24 23:34:43 -060098
Rob Landley207cada2013-10-03 03:18:00 -050099 flist = digest(flags);
100 offlist = aflist = digest(allflags);
101
Rob Landleya2359012014-02-16 17:31:33 -0600102 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
Rob Landley207cada2013-10-03 03:18:00 -0500103 command, command, command);
104
105 while (offlist) {
106 struct flag *f = offlist->lopt;
107 while (f) {
108 printf("#undef FLAG_%s\n", f->command);
109 f = f->next;
110 }
111 if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
112 offlist = offlist->next;
113 }
114 printf("#endif\n\n");
115
Rob Landley6ebe03d2014-02-24 23:34:43 -0600116 sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
117 command, command);
118 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500119
120 while (aflist) {
121 if (aflist->lopt) {
122 if (flist && flist->lopt &&
123 !strcmp(flist->lopt->command, aflist->lopt->command))
124 {
Rob Landley6ebe03d2014-02-24 23:34:43 -0600125 sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500126 flist->lopt = flist->lopt->next;
Rob Landley6ebe03d2014-02-24 23:34:43 -0600127 } else sprintf(out, "#define FLAG_%s 0\n", aflist->lopt->command);
Rob Landley207cada2013-10-03 03:18:00 -0500128 aflist->lopt = aflist->lopt->next;
Ashwini Sharma882ca8b2014-04-09 07:40:02 -0500129 if (!aflist->command) {
130 aflist = aflist->next;
131 if (flist) {
132 flist = flist->next;
133 bit++;
134 }
135 }
Rob Landley5a2583a2014-02-08 10:53:26 -0600136 } else if (aflist->command) {
Rob Landley207cada2013-10-03 03:18:00 -0500137 if (flist && (!aflist->command || *aflist->command == *flist->command))
138 {
139 if (aflist->command)
Rob Landley6ebe03d2014-02-24 23:34:43 -0600140 sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500141 bit++;
142 flist = flist->next;
Rob Landley6ebe03d2014-02-24 23:34:43 -0600143 } else sprintf(out, "#define FLAG_%c 0\n", *aflist->command);
Rob Landley207cada2013-10-03 03:18:00 -0500144 aflist = aflist->next;
145 }
Rob Landley6ebe03d2014-02-24 23:34:43 -0600146 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500147 }
Rob Landley6ebe03d2014-02-24 23:34:43 -0600148 sprintf(out, "#endif\n\n");
149 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500150 }
151
Rob Landley6ebe03d2014-02-24 23:34:43 -0600152 if (fflush(0) && ferror(stdout)) return 1;
153
154 out = outbuf;
155 while (*out) {
156 int i = write(1, outbuf, strlen(outbuf));
157
158 if (i<0) return 1;
159 out += i;
160 }
Rob Landley207cada2013-10-03 03:18:00 -0500161}