blob: 001007ef41d58809c8c57566c7248ec44082d483 [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 }
Ashwini Sharma64f61642014-03-19 13:57:06 -050047 while (*++string != ')') if (*string == '-') *string = '_'; // An empty longopt () would break this.
Rob Landley207cada2013-10-03 03:18:00 -050048 *(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
72int main(int argc, char *argv[])
73{
74 char command[256], flags[1023], allflags[1024];
Rob Landley6ebe03d2014-02-24 23:34:43 -060075 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 Landley207cada2013-10-03 03:18:00 -050080
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 Landley6ebe03d2014-02-24 23:34:43 -060089
Rob Landley207cada2013-10-03 03:18:00 -050090 flist = digest(flags);
91 offlist = aflist = digest(allflags);
92
Rob Landleya2359012014-02-16 17:31:33 -060093 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
Rob Landley207cada2013-10-03 03:18:00 -050094 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 Landley6ebe03d2014-02-24 23:34:43 -0600107 sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
108 command, command);
109 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500110
111 while (aflist) {
112 if (aflist->lopt) {
113 if (flist && flist->lopt &&
114 !strcmp(flist->lopt->command, aflist->lopt->command))
115 {
Rob Landley6ebe03d2014-02-24 23:34:43 -0600116 sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500117 flist->lopt = flist->lopt->next;
Rob Landley6ebe03d2014-02-24 23:34:43 -0600118 } else sprintf(out, "#define FLAG_%s 0\n", aflist->lopt->command);
Rob Landley207cada2013-10-03 03:18:00 -0500119 aflist->lopt = aflist->lopt->next;
Ashwini Sharma882ca8b2014-04-09 07:40:02 -0500120 if (!aflist->command) {
121 aflist = aflist->next;
122 if (flist) {
123 flist = flist->next;
124 bit++;
125 }
126 }
Rob Landley5a2583a2014-02-08 10:53:26 -0600127 } else if (aflist->command) {
Rob Landley207cada2013-10-03 03:18:00 -0500128 if (flist && (!aflist->command || *aflist->command == *flist->command))
129 {
130 if (aflist->command)
Rob Landley6ebe03d2014-02-24 23:34:43 -0600131 sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit);
Rob Landley207cada2013-10-03 03:18:00 -0500132 bit++;
133 flist = flist->next;
Rob Landley6ebe03d2014-02-24 23:34:43 -0600134 } else sprintf(out, "#define FLAG_%c 0\n", *aflist->command);
Rob Landley207cada2013-10-03 03:18:00 -0500135 aflist = aflist->next;
136 }
Rob Landley6ebe03d2014-02-24 23:34:43 -0600137 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500138 }
Rob Landley6ebe03d2014-02-24 23:34:43 -0600139 sprintf(out, "#endif\n\n");
140 out += strlen(out);
Rob Landley207cada2013-10-03 03:18:00 -0500141 }
142
Rob Landley6ebe03d2014-02-24 23:34:43 -0600143 if (fflush(0) && ferror(stdout)) return 1;
144
145 out = outbuf;
146 while (*out) {
147 int i = write(1, outbuf, strlen(outbuf));
148
149 if (i<0) return 1;
150 out += i;
151 }
Rob Landley207cada2013-10-03 03:18:00 -0500152}