2013-10-03 01:18:00 -07:00
|
|
|
// Take three word input lines on stdin (the three space separated words are
|
|
|
|
// command name, option string with current config, option string from
|
|
|
|
// allyesconfig; space separated, the last two are and double quotes)
|
|
|
|
// and produce flag #defines to stdout.
|
|
|
|
|
|
|
|
// This is intentionally crappy code because we control the inputs. It leaks
|
|
|
|
// memory like a sieve and segfaults if malloc returns null, but does the job.
|
|
|
|
|
2015-08-04 19:40:56 -07:00
|
|
|
#include <unistd.h>
|
2013-10-03 01:18:00 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2015-08-04 19:40:56 -07:00
|
|
|
#include <ctype.h>
|
2013-10-03 01:18:00 -07:00
|
|
|
|
|
|
|
struct flag {
|
|
|
|
struct flag *next;
|
|
|
|
char *command;
|
|
|
|
struct flag *lopt;
|
|
|
|
};
|
|
|
|
|
2014-12-31 19:30:59 -08:00
|
|
|
// replace chopped out USE_BLAH() sections with low-ascii characters
|
|
|
|
// showing how many flags got skipped
|
|
|
|
|
|
|
|
char *mark_gaps(char *flags, char *all)
|
|
|
|
{
|
|
|
|
char *n, *new, c;
|
|
|
|
|
|
|
|
// Shell feeds in " " for blank args, leading space not meaningful.
|
|
|
|
while (isspace(*flags)) flags++;
|
|
|
|
while (isspace(*all)) all++;
|
|
|
|
|
|
|
|
n = new = strdup(all);
|
|
|
|
while (*all) {
|
|
|
|
if (*flags == *all) {
|
|
|
|
*(new++) = *(all++);
|
|
|
|
*flags++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = *(all++);
|
|
|
|
if (strchr("?&^-:#|@*; ", c));
|
|
|
|
else if (strchr("=<>", c)) while (isdigit(*all)) all++;
|
|
|
|
else if (c == '(') while(*(all++) != ')');
|
|
|
|
else *(new++) = 1;
|
|
|
|
}
|
|
|
|
*new = 0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-10-03 01:18:00 -07:00
|
|
|
// Break down a command string into struct flag list.
|
|
|
|
|
|
|
|
struct flag *digest(char *string)
|
|
|
|
{
|
|
|
|
struct flag *list = NULL;
|
2014-12-31 19:30:59 -08:00
|
|
|
char *err = string;
|
2013-10-03 01:18:00 -07:00
|
|
|
|
|
|
|
while (*string) {
|
|
|
|
// Groups must be at end.
|
|
|
|
if (*string == '[') break;
|
|
|
|
|
|
|
|
// Longopts
|
|
|
|
if (*string == '(') {
|
|
|
|
struct flag *new = calloc(sizeof(struct flag), 1);
|
|
|
|
|
|
|
|
new->command = ++string;
|
|
|
|
|
|
|
|
// Attach longopt to previous short opt, if any.
|
|
|
|
if (list && list->command) {
|
|
|
|
new->next = list->lopt;
|
|
|
|
list->lopt = new;
|
|
|
|
} else {
|
|
|
|
struct flag *blank = calloc(sizeof(struct flag), 1);
|
|
|
|
|
|
|
|
blank->next = list;
|
|
|
|
blank->lopt = new;
|
|
|
|
list = blank;
|
|
|
|
}
|
2014-05-10 11:06:31 -07:00
|
|
|
// An empty longopt () would break this.
|
|
|
|
while (*++string != ')') if (*string == '-') *string = '_';
|
2013-10-03 01:18:00 -07:00
|
|
|
*(string++) = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr("?&^-:#|@*; ", *string)) string++;
|
|
|
|
else if (strchr("=<>", *string)) {
|
2014-12-31 19:30:59 -08:00
|
|
|
if (!isdigit(string[1])) {
|
|
|
|
fprintf(stderr, "%c without number in '%s'", *string, err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-10-03 01:18:00 -07:00
|
|
|
while (isdigit(*++string)) {
|
|
|
|
if (!list) {
|
|
|
|
string++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct flag *new = calloc(sizeof(struct flag), 1);
|
|
|
|
|
|
|
|
new->command = string++;
|
|
|
|
new->next = list;
|
|
|
|
list = new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char command[256], flags[1023], allflags[1024];
|
2014-02-24 21:34:43 -08:00
|
|
|
char *out, *outbuf = malloc(1024*1024);
|
|
|
|
|
|
|
|
// Yes, the output buffer is 1 megabyte with no bounds checking.
|
|
|
|
// See "intentionally crappy", above.
|
|
|
|
if (!(out = outbuf)) return 1;
|
2013-10-03 01:18:00 -07:00
|
|
|
|
2016-01-18 13:45:56 -08:00
|
|
|
printf("#undef FORCED_FLAG\n#undef FORCED_FLAGLL\n"
|
|
|
|
"#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n#define FORCED_FLAGLL 1LL\n"
|
|
|
|
"#else\n#define FORCED_FLAG 0\n#define FORCED_FLAGLL 0\n#endif\n\n");
|
2014-12-31 19:30:59 -08:00
|
|
|
|
2013-10-03 01:18:00 -07:00
|
|
|
for (;;) {
|
|
|
|
struct flag *flist, *aflist, *offlist;
|
2014-12-31 19:30:59 -08:00
|
|
|
char *gaps, *mgaps, c;
|
2014-05-10 11:06:31 -07:00
|
|
|
unsigned bit;
|
2013-10-03 01:18:00 -07:00
|
|
|
|
2015-05-03 10:41:05 -07:00
|
|
|
*command = *flags = *allflags = 0;
|
2014-05-10 11:06:31 -07:00
|
|
|
bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
|
|
|
|
command, flags, allflags);
|
2013-10-03 01:18:00 -07:00
|
|
|
|
2015-05-03 10:41:05 -07:00
|
|
|
if (getenv("DEBUG"))
|
|
|
|
fprintf(stderr, "command=%s, flags=%s, allflags=%s\n",
|
|
|
|
command, flags, allflags);
|
|
|
|
|
2014-05-10 11:06:31 -07:00
|
|
|
if (!*command) break;
|
|
|
|
if (bit != 3) {
|
2015-08-30 02:42:49 -07:00
|
|
|
fprintf(stderr, "\nError in %s (see generated/flags.raw)\n", command);
|
2014-05-10 11:06:31 -07:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bit = 0;
|
2013-10-03 01:18:00 -07:00
|
|
|
printf("// %s %s %s\n", command, flags, allflags);
|
2014-12-31 19:30:59 -08:00
|
|
|
mgaps = mark_gaps(flags, allflags);
|
|
|
|
for (gaps = mgaps; *gaps == 1; gaps++);
|
|
|
|
if (*gaps) c = '"';
|
|
|
|
else {
|
|
|
|
c = ' ';
|
|
|
|
gaps = "0";
|
|
|
|
}
|
|
|
|
printf("#undef OPTSTR_%s\n#define OPTSTR_%s %c%s%c\n",
|
|
|
|
command, command, c, gaps, c);
|
|
|
|
free(mgaps);
|
2014-02-24 21:34:43 -08:00
|
|
|
|
2013-10-03 01:18:00 -07:00
|
|
|
flist = digest(flags);
|
|
|
|
offlist = aflist = digest(allflags);
|
|
|
|
|
2014-02-16 15:31:33 -08:00
|
|
|
printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
|
2013-10-03 01:18:00 -07:00
|
|
|
command, command, command);
|
|
|
|
|
|
|
|
while (offlist) {
|
|
|
|
struct flag *f = offlist->lopt;
|
|
|
|
while (f) {
|
|
|
|
printf("#undef FLAG_%s\n", f->command);
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
|
|
|
|
offlist = offlist->next;
|
|
|
|
}
|
|
|
|
printf("#endif\n\n");
|
|
|
|
|
2014-02-24 21:34:43 -08:00
|
|
|
sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
|
|
|
|
command, command);
|
|
|
|
out += strlen(out);
|
2013-10-03 01:18:00 -07:00
|
|
|
|
|
|
|
while (aflist) {
|
2015-12-10 13:57:08 -08:00
|
|
|
char *llstr = bit>31 ? "LL" : "";
|
|
|
|
|
|
|
|
// Output flag macro for bare longopts
|
2013-10-03 01:18:00 -07:00
|
|
|
if (aflist->lopt) {
|
|
|
|
if (flist && flist->lopt &&
|
|
|
|
!strcmp(flist->lopt->command, aflist->lopt->command))
|
|
|
|
{
|
2015-12-10 13:57:08 -08:00
|
|
|
sprintf(out, "#define FLAG_%s (1%s<<%d)\n", flist->lopt->command,
|
|
|
|
llstr, bit);
|
2013-10-03 01:18:00 -07:00
|
|
|
flist->lopt = flist->lopt->next;
|
2015-12-10 13:57:08 -08:00
|
|
|
} else sprintf(out, "#define FLAG_%s (FORCED_FLAG%s<<%d)\n",
|
|
|
|
aflist->lopt->command, llstr, bit);
|
2013-10-03 01:18:00 -07:00
|
|
|
aflist->lopt = aflist->lopt->next;
|
2014-04-09 05:40:02 -07:00
|
|
|
if (!aflist->command) {
|
|
|
|
aflist = aflist->next;
|
2014-12-31 19:30:59 -08:00
|
|
|
bit++;
|
|
|
|
if (flist) flist = flist->next;
|
2014-04-09 05:40:02 -07:00
|
|
|
}
|
2015-12-10 13:57:08 -08:00
|
|
|
// Output normal flag macro
|
2014-02-08 08:53:26 -08:00
|
|
|
} else if (aflist->command) {
|
2016-06-30 18:41:07 -07:00
|
|
|
if (flist && flist->command && *aflist->command == *flist->command) {
|
2013-10-03 01:18:00 -07:00
|
|
|
if (aflist->command)
|
2015-12-10 13:57:08 -08:00
|
|
|
sprintf(out, "#define FLAG_%c (1%s<<%d)\n", *aflist->command,
|
|
|
|
llstr, bit);
|
2013-10-03 01:18:00 -07:00
|
|
|
flist = flist->next;
|
2015-12-10 13:57:08 -08:00
|
|
|
} else sprintf(out, "#define FLAG_%c (FORCED_FLAG%s<<%d)\n",
|
|
|
|
*aflist->command, llstr, bit);
|
2014-12-31 19:30:59 -08:00
|
|
|
bit++;
|
2013-10-03 01:18:00 -07:00
|
|
|
aflist = aflist->next;
|
|
|
|
}
|
2014-02-24 21:34:43 -08:00
|
|
|
out += strlen(out);
|
2013-10-03 01:18:00 -07:00
|
|
|
}
|
2014-12-31 19:30:59 -08:00
|
|
|
out = stpcpy(out, "#endif\n\n");
|
2013-10-03 01:18:00 -07:00
|
|
|
}
|
|
|
|
|
2014-02-24 21:34:43 -08:00
|
|
|
if (fflush(0) && ferror(stdout)) return 1;
|
|
|
|
|
|
|
|
out = outbuf;
|
|
|
|
while (*out) {
|
|
|
|
int i = write(1, outbuf, strlen(outbuf));
|
|
|
|
|
|
|
|
if (i<0) return 1;
|
|
|
|
out += i;
|
|
|
|
}
|
2015-03-04 05:29:57 -08:00
|
|
|
|
|
|
|
return 0;
|
2013-10-03 01:18:00 -07:00
|
|
|
}
|