annotate scripts/mkflags.c @ 1634:5fac2769a159 draft

Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags). This means the flag space is no longer packed, but leaves gaps where the zeroes go. (Actual flag bit positions are the same for all configs.) Since the option parsing needs to know where the holes are, the OPTSTR values are now generated as part of flags.h with ascii 1 values for the disabled values. (So generated/oldflags.h went away.) This also means that the option string argument for OLDTOY() went away, it now uses the same arguments as the NEWTOY() it references.
author Rob Landley <rob@landley.net>
date Wed, 31 Dec 2014 21:30:59 -0600
parents 23817e1675f2
children a580ecd4a78a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 // Take three word input lines on stdin (the three space separated words are
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 // command name, option string with current config, option string from
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 // allyesconfig; space separated, the last two are and double quotes)
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 // and produce flag #defines to stdout.
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
5
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 // This is intentionally crappy code because we control the inputs. It leaks
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 // memory like a sieve and segfaults if malloc returns null, but does the job.
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 #include <stdio.h>
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 #include <stdlib.h>
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 #include <string.h>
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 #include <errno.h>
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
13
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 struct flag {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 struct flag *next;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 char *command;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 struct flag *lopt;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 };
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
20 // replace chopped out USE_BLAH() sections with low-ascii characters
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
21 // showing how many flags got skipped
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
22
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
23 char *mark_gaps(char *flags, char *all)
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
24 {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
25 char *n, *new, c;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
26
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
27 // Shell feeds in " " for blank args, leading space not meaningful.
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
28 while (isspace(*flags)) flags++;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
29 while (isspace(*all)) all++;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
30
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
31 n = new = strdup(all);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
32 while (*all) {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
33 if (*flags == *all) {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
34 *(new++) = *(all++);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
35 *flags++;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
36 continue;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
37 }
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
38
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
39 c = *(all++);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
40 if (strchr("?&^-:#|@*; ", c));
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
41 else if (strchr("=<>", c)) while (isdigit(*all)) all++;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
42 else if (c == '(') while(*(all++) != ')');
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
43 else *(new++) = 1;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
44 }
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
45 *new = 0;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
46
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
47 return n;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
48 }
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
49
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 // Break down a command string into struct flag list.
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 struct flag *digest(char *string)
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 struct flag *list = NULL;
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
55 char *err = string;
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
56
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 while (*string) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 // Groups must be at end.
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 if (*string == '[') break;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
60
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 // Longopts
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 if (*string == '(') {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 struct flag *new = calloc(sizeof(struct flag), 1);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
64
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 new->command = ++string;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
66
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 // Attach longopt to previous short opt, if any.
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 if (list && list->command) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 new->next = list->lopt;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 list->lopt = new;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 } else {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 struct flag *blank = calloc(sizeof(struct flag), 1);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
73
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 blank->next = list;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 blank->lopt = new;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 list = blank;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 }
1277
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
78 // An empty longopt () would break this.
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
79 while (*++string != ')') if (*string == '-') *string = '_';
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 *(string++) = 0;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 continue;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
83
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 if (strchr("?&^-:#|@*; ", *string)) string++;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 else if (strchr("=<>", *string)) {
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
86 if (!isdigit(string[1])) {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
87 fprintf(stderr, "%c without number in '%s'", *string, err);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
88 exit(1);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
89 }
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 while (isdigit(*++string)) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 if (!list) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 string++;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 break;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 } else {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 struct flag *new = calloc(sizeof(struct flag), 1);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
98
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 new->command = string++;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 new->next = list;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 list = new;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
104
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 return list;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
107
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 int main(int argc, char *argv[])
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
109 {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 char command[256], flags[1023], allflags[1024];
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
111 char *out, *outbuf = malloc(1024*1024);
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
112
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
113 // Yes, the output buffer is 1 megabyte with no bounds checking.
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
114 // See "intentionally crappy", above.
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
115 if (!(out = outbuf)) return 1;
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
116
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
117 printf("#ifdef FORCE_FLAGS\n#define FORCED_FLAG 1\n"
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
118 "#else\n#define FORCED_FLAG 0\n#endif\n\n");
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
119
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 for (;;) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 struct flag *flist, *aflist, *offlist;
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
122 char *gaps, *mgaps, c;
1277
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
123 unsigned bit;
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
124
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
125 *command = 0;
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
126 bit = fscanf(stdin, "%255s \"%1023[^\"]\" \"%1023[^\"]\"\n",
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
127 command, flags, allflags);
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
128
1277
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
129 if (!*command) break;
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
130 if (bit != 3) {
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
131 fprintf(stderr, "\nError in %s (duplicate command?)\n", command);
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
132 exit(1);
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
133 }
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
134
1277
23817e1675f2 Catch duplicate command name (which breaks the build already, but doesn't identify the culprit).
Rob Landley <rob@landley.net>
parents: 1243
diff changeset
135 bit = 0;
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 printf("// %s %s %s\n", command, flags, allflags);
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
137 mgaps = mark_gaps(flags, allflags);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
138 for (gaps = mgaps; *gaps == 1; gaps++);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
139 if (*gaps) c = '"';
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
140 else {
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
141 c = ' ';
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
142 gaps = "0";
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
143 }
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
144 printf("#undef OPTSTR_%s\n#define OPTSTR_%s %c%s%c\n",
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
145 command, command, c, gaps, c);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
146 free(mgaps);
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
147
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 flist = digest(flags);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 offlist = aflist = digest(allflags);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
150
1203
2a68f22aa286 Make CLEANUP transitions work, so multiple NEWTOY() can exist in the same file.
Rob Landley <rob@landley.net>
parents: 1202
diff changeset
151 printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 command, command, command);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
153
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 while (offlist) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 struct flag *f = offlist->lopt;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
156 while (f) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 printf("#undef FLAG_%s\n", f->command);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 f = f->next;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
160 if (offlist->command) printf("#undef FLAG_%c\n", *offlist->command);
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 offlist = offlist->next;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 printf("#endif\n\n");
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
164
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
165 sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
166 command, command);
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
167 out += strlen(out);
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
168
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 while (aflist) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 if (aflist->lopt) {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 if (flist && flist->lopt &&
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 !strcmp(flist->lopt->command, aflist->lopt->command))
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 {
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
174 sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit);
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 flist->lopt = flist->lopt->next;
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
176 } else sprintf(out, "#define FLAG_%s (FORCED_FLAG<<%d)\n",
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
177 aflist->lopt->command, bit);
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 aflist->lopt = aflist->lopt->next;
1243
eae29e8e2bc8 _mkflags_ had an issue for generating FLAG_xxxx macros for long options.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1225
diff changeset
179 if (!aflist->command) {
eae29e8e2bc8 _mkflags_ had an issue for generating FLAG_xxxx macros for long options.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1225
diff changeset
180 aflist = aflist->next;
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
181 bit++;
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
182 if (flist) flist = flist->next;
1243
eae29e8e2bc8 _mkflags_ had an issue for generating FLAG_xxxx macros for long options.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents: 1225
diff changeset
183 }
1198
ce31c87e1862 Fix segfault with single build of a command with bare longopts.
Rob Landley <rob@landley.net>
parents: 1076
diff changeset
184 } else if (aflist->command) {
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 if (flist && (!aflist->command || *aflist->command == *flist->command))
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 {
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 if (aflist->command)
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
188 sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit);
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 flist = flist->next;
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
190 } else sprintf(out, "#define FLAG_%c (FORCED_FLAG<<%d)\n",
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
191 *aflist->command, bit);
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
192 bit++;
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 aflist = aflist->next;
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 }
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
195 out += strlen(out);
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 }
1634
5fac2769a159 Redo option parsing infrastructure so #define FORCE_FLAGS can unzero flag macros for a disabled command (needed when multiple commands share infrastructure with a common set of flags).
Rob Landley <rob@landley.net>
parents: 1277
diff changeset
197 out = stpcpy(out, "#endif\n\n");
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 }
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
199
1209
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
200 if (fflush(0) && ferror(stdout)) return 1;
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
201
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
202 out = outbuf;
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
203 while (*out) {
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
204 int i = write(1, outbuf, strlen(outbuf));
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
205
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
206 if (i<0) return 1;
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
207 out += i;
f170f978e81e Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
Rob Landley <rob@landley.net>
parents: 1203
diff changeset
208 }
1076
1c15ba60aa64 Switch flag generation from shell to C.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 }