2022-12-01 23:53:36 -08:00
|
|
|
// Read three word input lines from stdin and produce flag #defines to stdout.
|
2022-05-17 07:22:20 -07:00
|
|
|
// The three words on each input line are command name, option string with
|
2018-10-21 19:36:51 -07:00
|
|
|
// current config, option string from allyesconfig. The three are space
|
|
|
|
// separated and the last two are in double quotes.
|
2013-10-03 01:18:00 -07:00
|
|
|
|
|
|
|
// 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 {
|
2022-05-17 07:22:20 -07:00
|
|
|
struct flag *next, *lopt;
|
2013-10-03 01:18:00 -07:00
|
|
|
char *command;
|
|
|
|
};
|
|
|
|
|
2016-12-07 19:52:00 -08:00
|
|
|
int chrtype(char c)
|
|
|
|
{
|
2018-10-21 19:36:51 -07:00
|
|
|
// Does this populate a GLOBALS() variable?
|
2020-11-14 21:39:18 -08:00
|
|
|
if (strchr("^-:#|@*; %.", c)) return 1;
|
2018-10-21 19:36:51 -07:00
|
|
|
|
|
|
|
// Is this followed by a numeric argument in optstr?
|
2016-12-07 19:52:00 -08:00
|
|
|
if (strchr("=<>", c)) return 2;
|
|
|
|
|
2020-11-14 21:39:18 -08:00
|
|
|
if (strchr("?&0", c)) return 3;
|
|
|
|
|
2016-12-07 19:52:00 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-31 19:30:59 -08:00
|
|
|
// replace chopped out USE_BLAH() sections with low-ascii characters
|
2020-11-14 21:39:18 -08:00
|
|
|
// showing how many flags got skipped so FLAG_ macros stay constant
|
2014-12-31 19:30:59 -08:00
|
|
|
|
|
|
|
char *mark_gaps(char *flags, char *all)
|
|
|
|
{
|
|
|
|
char *n, *new, c;
|
2022-05-17 07:22:20 -07:00
|
|
|
int bare = 1;
|
2014-12-31 19:30:59 -08:00
|
|
|
|
|
|
|
// Shell feeds in " " for blank args, leading space not meaningful.
|
|
|
|
while (isspace(*flags)) flags++;
|
|
|
|
while (isspace(*all)) all++;
|
|
|
|
|
|
|
|
n = new = strdup(all);
|
|
|
|
while (*all) {
|
2016-12-07 19:52:00 -08:00
|
|
|
// --longopt parentheticals dealt with as a unit
|
|
|
|
if (*all == '(') {
|
|
|
|
int len = 0;
|
|
|
|
|
2020-11-14 21:39:18 -08:00
|
|
|
while (all[len]) if (all[len++] == ')') break;
|
2016-12-07 19:52:00 -08:00
|
|
|
if (strncmp(flags, all, len)) {
|
|
|
|
// bare longopts need their own skip placeholders
|
|
|
|
if (bare) *(new++) = 1;
|
|
|
|
} else {
|
|
|
|
memcpy(new, all, len);
|
|
|
|
new += len;
|
|
|
|
flags += len;
|
|
|
|
}
|
|
|
|
all += len;
|
2016-12-07 21:05:33 -08:00
|
|
|
continue;
|
2016-12-07 19:52:00 -08:00
|
|
|
}
|
|
|
|
c = *(all++);
|
2020-11-14 21:39:18 -08:00
|
|
|
if (bare && !chrtype(c)) bare = 0;
|
2016-12-07 19:52:00 -08:00
|
|
|
if (*flags == c) {
|
|
|
|
*(new++) = c;
|
2019-01-19 14:50:23 -08:00
|
|
|
flags++;
|
2014-12-31 19:30:59 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-07 19:52:00 -08:00
|
|
|
c = chrtype(c);
|
2020-11-14 21:39:18 -08:00
|
|
|
if (!c || (!bare && c==3)) *(new++) = 1;
|
2016-12-07 19:52:00 -08:00
|
|
|
else if (c==2) while (isdigit(*all)) all++;
|
2014-12-31 19:30:59 -08:00
|
|
|
}
|
|
|
|
*new = 0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:36:51 -07:00
|
|
|
// Break down a command string into linked list of "struct flag".
|
2013-10-03 01:18:00 -07:00
|
|
|
|
|
|
|
struct flag *digest(char *string)
|
|
|
|
{
|
2020-11-14 21:39:18 -08:00
|
|
|
struct flag *list = 0;
|
2018-10-21 19:36:51 -07:00
|
|
|
char *err = string, c;
|
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);
|
|
|
|
|
2022-05-17 07:22:20 -07:00
|
|
|
if (string[1]==')') {
|
|
|
|
fprintf(stderr, "empty () longopt in '%s'", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-10-03 01:18:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-21 19:36:51 -07:00
|
|
|
c = chrtype(*string);
|
2020-11-14 21:39:18 -08:00
|
|
|
if (c == 1 || (c == 3 && !list)) string++;
|
2018-10-21 19:36:51 -07:00
|
|
|
else if (c == 2) {
|
2018-03-19 09:23:24 -07:00
|
|
|
if (string[1]=='-') 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);
|
|
|
|
|
2022-05-17 07:22:20 -07:00
|
|
|
if (string[0]=='~' && string[1]!='(') {
|
|
|
|
fprintf(stderr, "~ without (longopt) in '%s'", err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-10-03 01:18:00 -07:00
|
|
|
new->command = string++;
|
|
|
|
new->next = list;
|
|
|
|
list = new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-12-26 10:07:14 -08:00
|
|
|
// Parse C-style octal escape
|
|
|
|
void octane(char *from)
|
|
|
|
{
|
|
|
|
unsigned char *to = (void *)from;
|
|
|
|
|
|
|
|
while (*from) {
|
|
|
|
if (*from == '\\') {
|
|
|
|
*to = 0;
|
|
|
|
while (isdigit(*++from)) *to = (8**to)+*from-'0';
|
|
|
|
to++;
|
|
|
|
} else *to++ = *from++;
|
|
|
|
}
|
|
|
|
*to = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-03 01:18:00 -07:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-02-21 09:09:29 -08:00
|
|
|
char command[256], flags[1024], 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
|
|
|
|
2022-12-01 23:53:36 -08:00
|
|
|
printf("#undef FORCED_FLAG\n#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1LL\n"
|
|
|
|
"#else\n#define FORCED_FLAG 0LL\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;
|
2016-07-17 00:14:35 -07:00
|
|
|
char *mgaps = 0;
|
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);
|
2018-12-26 10:07:14 -08:00
|
|
|
octane(flags);
|
|
|
|
octane(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);
|
2016-07-17 00:14:35 -07:00
|
|
|
if (*flags != ' ') mgaps = mark_gaps(flags, allflags);
|
|
|
|
else if (*allflags != ' ') mgaps = allflags;
|
2016-07-15 02:45:08 -07:00
|
|
|
// If command disabled, use allflags for OLDTOY()
|
2016-07-17 00:14:35 -07:00
|
|
|
printf("#undef OPTSTR_%s\n#define OPTSTR_%s ", command, command);
|
|
|
|
if (mgaps) printf("\"%s\"\n", mgaps);
|
|
|
|
else printf("0\n");
|
|
|
|
if (mgaps != allflags) 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) {
|
2018-12-26 10:07:14 -08:00
|
|
|
char *s = (char []){0, 0, 0, 0};
|
|
|
|
|
2022-05-17 07:22:20 -07:00
|
|
|
if (!offlist->command || *offlist->command=='~')
|
|
|
|
s = offlist->lopt->command;
|
2018-12-26 10:07:14 -08:00
|
|
|
else {
|
|
|
|
*s = *offlist->command;
|
|
|
|
if (127 < (unsigned char)*s) sprintf(s, "X%02X", 127&*s);
|
2013-10-03 01:18:00 -07:00
|
|
|
}
|
2018-12-26 10:07:14 -08:00
|
|
|
printf("#undef FLAG_%s\n", s);
|
2013-10-03 01:18:00 -07:00
|
|
|
offlist = offlist->next;
|
|
|
|
}
|
|
|
|
printf("#endif\n\n");
|
|
|
|
|
2022-02-16 03:12:42 -08:00
|
|
|
sprintf(out, "#ifdef FOR_%s\n#define CLEANUP_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
|
|
|
|
command, command, command);
|
2014-02-24 21:34:43 -08:00
|
|
|
out += strlen(out);
|
2013-10-03 01:18:00 -07:00
|
|
|
|
|
|
|
while (aflist) {
|
2022-12-01 23:53:36 -08:00
|
|
|
char *s = (char []){0, 0, 0, 0};
|
2018-12-25 19:24:07 -08:00
|
|
|
int enabled = 0;
|
2015-12-10 13:57:08 -08:00
|
|
|
|
|
|
|
// Output flag macro for bare longopts
|
2022-05-17 07:22:20 -07:00
|
|
|
if (!aflist->command || *aflist->command=='~') {
|
2018-12-25 19:24:07 -08:00
|
|
|
s = aflist->lopt->command;
|
2013-10-03 01:18:00 -07:00
|
|
|
if (flist && flist->lopt &&
|
2018-12-25 19:24:07 -08:00
|
|
|
!strcmp(flist->lopt->command, aflist->lopt->command)) enabled++;
|
2015-12-10 13:57:08 -08:00
|
|
|
// Output normal flag macro
|
2018-12-25 19:24:07 -08:00
|
|
|
} else {
|
|
|
|
*s = *aflist->command;
|
2018-12-26 10:07:14 -08:00
|
|
|
if (127 < (unsigned char)*s) sprintf(s, "X%02X", 127&*s);
|
2018-12-25 19:24:07 -08:00
|
|
|
if (flist && flist->command && *aflist->command == *flist->command)
|
|
|
|
enabled++;
|
2013-10-03 01:18:00 -07:00
|
|
|
}
|
2022-12-01 23:53:36 -08:00
|
|
|
out += sprintf(out, "#define FLAG_%s (%s<<%d)\n",
|
|
|
|
s, enabled ? "1LL" : "FORCED_FLAG", bit++);
|
2018-12-25 19:24:07 -08:00
|
|
|
aflist = aflist->next;
|
|
|
|
if (enabled) flist = flist->next;
|
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
|
|
|
}
|