changeset 1209:f170f978e81e draft

Put all FOR_xxx blocks after all CLEANUP_xxx in generated/flags.h so the usages don't have to be in alphabetical order.
author Rob Landley <rob@landley.net>
date Mon, 24 Feb 2014 23:34:43 -0600
parents c86b1579c383
children 1c4c893d4e72
files scripts/mkflags.c
diffstat 1 files changed, 25 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/mkflags.c	Sun Feb 23 20:11:06 2014 -0600
+++ b/scripts/mkflags.c	Mon Feb 24 23:34:43 2014 -0600
@@ -72,6 +72,11 @@
 int main(int argc, char *argv[])
 {
   char command[256], flags[1023], allflags[1024];
+  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;
 
   for (;;) {
     struct flag *flist, *aflist, *offlist;
@@ -81,10 +86,10 @@
                     command, flags, allflags)) break;
 
     printf("// %s %s %s\n", command, flags, allflags);
+
     flist = digest(flags);
     offlist = aflist = digest(allflags);
 
-
     printf("#ifdef CLEANUP_%s\n#undef CLEANUP_%s\n#undef FOR_%s\n",
            command, command, command);
 
@@ -99,32 +104,43 @@
     }
     printf("#endif\n\n");
 
-    printf("#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
-           command, command);
+    sprintf(out, "#ifdef FOR_%s\n#ifndef TT\n#define TT this.%s\n#endif\n",
+            command, command);
+    out += strlen(out);
 
     while (aflist) {
       if (aflist->lopt) {
         if (flist && flist->lopt &&
             !strcmp(flist->lopt->command, aflist->lopt->command))
         {
-          printf("#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit);
+          sprintf(out, "#define FLAG_%s (1<<%d)\n", flist->lopt->command, bit);
           flist->lopt = flist->lopt->next;
-        } else printf("#define FLAG_%s 0\n", aflist->lopt->command);
+        } else sprintf(out, "#define FLAG_%s 0\n", aflist->lopt->command);
         aflist->lopt = aflist->lopt->next;
         if (!aflist->command) aflist = aflist->next;
       } else if (aflist->command) {
         if (flist && (!aflist->command || *aflist->command == *flist->command))
         {
           if (aflist->command)
-            printf("#define FLAG_%c (1<<%d)\n", *aflist->command, bit);
+            sprintf(out, "#define FLAG_%c (1<<%d)\n", *aflist->command, bit);
           bit++;
           flist = flist->next;
-        } else printf("#define FLAG_%c 0\n", *aflist->command);
+        } else sprintf(out, "#define FLAG_%c 0\n", *aflist->command);
         aflist = aflist->next;
       }
+      out += strlen(out);
     }
-    printf("#endif\n\n");
+    sprintf(out, "#endif\n\n");
+    out += strlen(out);
   }
 
-  return fflush(0) && ferror(stdout);
+  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;
+  }
 }