]> ocean-lang.org Git - ocean/blobdiff - csrc/boot-strap/md2c.c
boot-strap: update bootstrap code.
[ocean] / csrc / boot-strap / md2c.c
index 71e377179adb2f45c73e4f8a427add82ec425cd8..30e5a6994cb88ffd3d0ee052e2364c0126f783c2 100644 (file)
@@ -1,17 +1,16 @@
-#line 120 "../mdcode.mdc"
+#line 124 "../mdcode.mdc"
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 #include "mdcode.h"
 
-#line 860 "../mdcode.mdc"
+#line 894 "../mdcode.mdc"
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/mman.h>
 #include <string.h>
-
-#line 832 "../mdcode.mdc"
+#line 867 "../mdcode.mdc"
 static void copy_fname(char *name, int space, struct text t)
 {
        char *sec = t.txt;
@@ -30,8 +29,7 @@ static void copy_fname(char *name, int space, struct text t)
        strncpy(name, sec, len);
        name[len] = 0;
 }
-
-#line 867 "../mdcode.mdc"
+#line 901 "../mdcode.mdc"
 static int errs;
 static void pr_err(char *msg)
 {
@@ -39,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",
@@ -65,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",
@@ -92,6 +116,5 @@ int main(int argc, char *argv[])
        }
        exit(!!errs);
 }
-
-#line 128 "../mdcode.mdc"
+#line 132 "../mdcode.mdc"