blob: a8e5216ef2cb806748850877027562b530554a0c [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 }
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
72int main(int argc, char *argv[])
73{
74 char command[256], flags[1023], allflags[1024];
Rob Landley207cada2013-10-03 03:18:00 -050075
76 for (;;) {
77 struct flag *flist, *aflist, *offlist;
78 unsigned bit = 0;
79
80 if (3 != fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
81 command, flags, allflags)) break;
82
83 printf("// %s %s %s\n", command, flags, allflags);
84 flist = digest(flags);
85 offlist = aflist = digest(allflags);
86
87
Rob Landleya2359012014-02-16 17:31:33 -060088 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
Rob Landley207cada2013-10-03 03:18:00 -050089 command, command, command);
90
91 while (offlist) {
92 struct flag *f = offlist->lopt;
93 while (f) {
94 printf("#undef FLAG_%s\n", f->command);
95 f = f->next;
96 }
97 if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
98 offlist = offlist->next;
99 }
100 printf("#endif\n\n");
101
Rob Landleya2359012014-02-16 17:31:33 -0600102 printf("#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
103 command, command);
Rob Landley207cada2013-10-03 03:18:00 -0500104
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 Landley5a2583a2014-02-08 10:53:26 -0600114 if (!aflist->command) aflist = aflist->next;
115 } else if (aflist->command) {
Rob Landley207cada2013-10-03 03:18:00 -0500116 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}