]> ocean-lang.org Git - ocean/blobdiff - csrc/boot-strap/md2c.c
boot-strap: update bootstrap code.
[ocean] / csrc / boot-strap / md2c.c
index 2b18737fcfd19e8e2e3221e32b01c964065a8a0f..30e5a6994cb88ffd3d0ee052e2364c0126f783c2 100644 (file)
@@ -1,53 +1,16 @@
-#line 119 "../mdcode.mdc"
+#line 124 "../mdcode.mdc"
 #include <unistd.h>
 #include <stdlib.h>
+#include <stdio.h>
 
 #include "mdcode.h"
 
-#line 849 "../mdcode.mdc"
+#line 894 "../mdcode.mdc"
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/mman.h>
 #include <string.h>
-#include <stdio.h>
-
-#line 771 "../mdcode.mdc"
-static void code_print(FILE *out, struct code_node *node,
-                      char *fname)
-{
-       for (; node; node = node->next) {
-               char *c = node->code.txt;
-               int len = node->code.len;
-               int undent = 0;
-
-               if (!len)
-                       continue;
-
-               fprintf(out, "#line %d \"%s\"\n",
-                       node->line_no, fname);
-               if (*c == ' ' || *c == '\t')
-                       undent = 1;
-               while (len && *c) {
-                       fprintf(out, "%*s", node->indent, "");
-                       if (undent) {
-                               if (*c == '\t' && len > 1) {
-                                       c++;
-                                       len--;
-                               } else if (strncmp(c, "    ", 4) == 0 && len > 4) {
-                                       c += 4;
-                                       len-= 4;
-                               }
-                       }
-                       do {
-                               fputc(*c, out);
-                               c++;
-                               len--;
-                       } while (len && c[-1] != '\n');
-               }
-       }
-}
-
-#line 821 "../mdcode.mdc"
+#line 867 "../mdcode.mdc"
 static void copy_fname(char *name, int space, struct text t)
 {
        char *sec = t.txt;
@@ -66,8 +29,7 @@ static void copy_fname(char *name, int space, struct text t)
        strncpy(name, sec, len);
        name[len] = 0;
 }
-
-#line 857 "../mdcode.mdc"
+#line 901 "../mdcode.mdc"
 static int errs;
 static void pr_err(char *msg)
 {
@@ -75,18 +37,33 @@ static void pr_err(char *msg)
        fprintf(stderr, "%s\n", msg);
 }
 
+static char *strnchr(char *haystack, int len, char needle)
+{
+       while (len > 0 && *haystack && *haystack != needle) {
+               haystack++;
+               len--;
+       }
+       return len > 0 && *haystack == needle ? haystack : NULL;
+}
+
 int main(int argc, char *argv[])
 {
        int fd;
        size_t len;
        char *file;
+       struct text section = {NULL, 0};
        struct section *table, *s, *prev;
 
        errs = 0;
-       if (argc != 2) {
-               fprintf(stderr, "Usage: mdcode file.mdc\n");
+       if (argc != 2 && argc != 3) {
+               fprintf(stderr, "Usage: mdcode file.mdc [section]\n");
                exit(2);
        }
+       if (argc == 3) {
+               section.txt = argv[2];
+               section.len = strlen(argv[2]);
+       }
+
        fd = open(argv[1], O_RDONLY);
        if (fd < 0) {
                fprintf(stderr, "mdcode: cannot open %s: %s\n",
@@ -101,14 +78,25 @@ int main(int argc, char *argv[])
                (code_free(s->code), prev = s, s = s->next, free(prev))) {
                FILE *fl;
                char fname[1024];
-               if (strncmp(s->section.txt, "Example:", 8) == 0)
-                       continue;
-               if (strncmp(s->section.txt, "File:", 5) != 0) {
-                       fprintf(stderr, "Unreferenced section is not a file name: %.*s\n",
+               char *spc = strnchr(s->section.txt, s->section.len, ' ');
+
+               if (spc > s->section.txt && spc[-1] == ':') {
+                       if (strncmp(s->section.txt, "File: ", 6) != 0 &&
+                           (section.txt == NULL ||
+                            text_cmp(s->section, section) != 0))
+                               /* Ignore this section */
+                               continue;
+               } else {
+                       fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n",
                                s->section.len, s->section.txt);
                        errs++;
                        continue;
                }
+               if (section.txt) {
+                       if (text_cmp(s->section, section) == 0)
+                               code_node_print(stdout, s->code, argv[1]);
+                       break;
+               }
                copy_fname(fname, sizeof(fname), s->section);
                if (fname[0] == 0) {
                        fprintf(stderr, "Missing file name at:%.*s\n",
@@ -123,11 +111,10 @@ int main(int argc, char *argv[])
                        errs++;
                        continue;
                }
-               code_print(fl, s->code, argv[1]);
+               code_node_print(fl, s->code, argv[1]);
                fclose(fl);
        }
        exit(!!errs);
 }
-
-#line 126 "../mdcode.mdc"
+#line 132 "../mdcode.mdc"