OSDN Git Service

Add optional openssl accelerated versions of hash functions, loosely based on
[android-x86/external-toybox.git] / scripts / mkflags.c
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 <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <ctype.h>
15
16 struct flag {
17   struct flag *next;
18   char *command;
19   struct flag *lopt;
20 };
21
22 // replace chopped out USE_BLAH() sections with low-ascii characters
23 // showing how many flags got skipped
24
25 char *mark_gaps(char *flags, char *all)
26 {
27   char *n, *new, c;
28
29   // Shell feeds in " " for blank args, leading space not meaningful.
30   while (isspace(*flags)) flags++;
31   while (isspace(*all)) all++;
32
33   n = new = strdup(all);
34   while (*all) {
35     if (*flags == *all) {
36       *(new++) = *(all++);
37       *flags++;
38       continue;
39     }
40
41     c = *(all++);
42     if (strchr("?&^-:#|@*; ", c));
43     else if (strchr("=<>", c)) while (isdigit(*all)) all++;
44     else if (c == '(') while(*(all++) != ')');
45     else *(new++) = 1;
46   }
47   *new = 0;
48
49   return n;
50 }
51
52 // Break down a command string into struct flag list.
53
54 struct flag *digest(char *string)
55 {
56   struct flag *list = NULL;
57   char *err = string;
58
59   while (*string) {
60     // Groups must be at end.
61     if (*string == '[') break;
62
63     // Longopts
64     if (*string == '(') {
65       struct flag *new = calloc(sizeof(struct flag), 1);
66
67       new->command = ++string;
68
69       // Attach longopt to previous short opt, if any.
70       if (list && list->command) {
71         new->next = list->lopt;
72         list->lopt = new;
73       } else {
74         struct flag *blank = calloc(sizeof(struct flag), 1);
75
76         blank->next = list;
77         blank->lopt = new;
78         list = blank;
79       }
80       // An empty longopt () would break this.
81       while (*++string != ')') if (*string == '-') *string = '_';
82       *(string++) = 0;
83       continue;
84     }
85
86     if (strchr("?&^-:#|@*; ", *string)) string++;
87     else if (strchr("=<>", *string)) {
88       if (!isdigit(string[1])) {
89         fprintf(stderr, "%c without number in '%s'", *string, err);
90         exit(1);
91       }
92       while (isdigit(*++string)) {
93         if (!list) {
94            string++;
95            break;
96         }
97       }
98     } else {
99       struct flag *new = calloc(sizeof(struct flag), 1);
100
101       new->command = string++;
102       new->next = list;
103       list = new;
104     }
105   }
106
107   return list;
108 }
109
110 int main(int argc, char *argv[])
111 {
112   char command[256], flags[1023], allflags[1024];
113   char *out, *outbuf = malloc(1024*1024);
114
115   // Yes, the output buffer is 1 megabyte with no bounds checking.
116   // See "intentionally crappy", above.
117   if (!(out = outbuf)) return 1;
118
119   printf("#undef FORCED_FLAG\n#undef FORCED_FLAGLL\n"
120     "#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1LL\n"
121     "#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0\n#endif\n\n");
122
123   for (;;) {
124     struct flag *flist, *aflist, *offlist;
125     char *mgaps;
126     unsigned bit;
127
128     *command = *flags = *allflags = 0;
129     bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
130                     command, flags, allflags);
131
132     if (getenv("DEBUG"))
133       fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
134         command, flags, allflags);
135
136     if (!*command) break;
137     if (bit != 3) {
138       fprintf(stderr, "\nError in %s (see generated/flags.raw)\n", command);
139       exit(1);
140     }
141
142     bit = 0;
143     printf("// %s %s %s\n", command, flags, allflags);
144     mgaps = mark_gaps(flags, allflags);
145     // If command disabled, use allflags for OLDTOY()
146     printf("#undef OPTSTR_%s\n#define OPTSTR_%s \"%s\"\n",
147             command, command, strcmp(flags, " ") ? mgaps : allflags);
148     free(mgaps);
149
150     flist = digest(flags);
151     offlist = aflist = digest(allflags);
152
153     printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
154            command, command, command);
155
156     while (offlist) {
157       struct flag *f = offlist->lopt;
158       while (f) {
159         printf("#undef FLAG_%s\n", f->command);
160         f = f->next;
161       }
162       if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
163       offlist = offlist->next;
164     }
165     printf("#endif\n\n");
166
167     sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
168             command, command);
169     out += strlen(out);
170
171     while (aflist) {
172       char *llstr = bit>31 ? "LL" : "";
173
174       // Output flag macro for bare longopts
175       if (aflist->lopt) {
176         if (flist && flist->lopt &&
177             !strcmp(flist->lopt->command, aflist->lopt->command))
178         {
179           sprintf(out, "#define FLAG_%s (1%s<<%d)\n", flist->lopt->command,
180             llstr, bit);
181           flist->lopt = flist->lopt->next;
182         } else sprintf(out, "#define FLAG_%s (FORCED_FLAG%s<<%d)\n",
183                        aflist->lopt->command, llstr, bit);
184         aflist->lopt = aflist->lopt->next;
185         if (!aflist->command) {
186           aflist = aflist->next;
187           bit++;
188           if (flist) flist = flist->next;
189         }
190       // Output normal flag macro
191       } else if (aflist->command) {
192         if (flist && flist->command && *aflist->command == *flist->command) {
193           if (aflist->command)
194             sprintf(out, "#define FLAG_%c (1%s<<%d)\n", *aflist->command,
195               llstr, bit);
196           flist = flist->next;
197         } else sprintf(out, "#define FLAG_%c (FORCED_FLAG%s<<%d)\n",
198                        *aflist->command, llstr, bit);
199         bit++;
200         aflist = aflist->next;
201       }
202       out += strlen(out);
203     }
204     out = stpcpy(out, "#endif\n\n");
205   }
206
207   if (fflush(0) && ferror(stdout)) return 1;
208
209   out = outbuf;
210   while (*out) {
211     int i = write(1, outbuf, strlen(outbuf));
212
213     if (i<0) return 1;
214     out += i;
215   }
216
217   return 0;
218 }