]> ocean-lang.org Git - ocean/commitdiff
boot-strap: update bootstrap code.
authorNeilBrown <neil@brown.name>
Sat, 13 Nov 2021 08:04:02 +0000 (19:04 +1100)
committerNeilBrown <neil@brown.name>
Sat, 13 Nov 2021 22:50:58 +0000 (09:50 +1100)
Run
  ../md2c ../mdcode.mdc
in boot-strap to get the latest code there.  Just because.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/boot-strap/libmdcode.c
csrc/boot-strap/md2c.c
csrc/boot-strap/mdcode.h
csrc/boot-strap/mdcode.mk

index a91bd60f2817efc55c2c1645ec636d4dc667d9ee..cf5d85cc7937f3459f742d2192f5777209923daa 100644 (file)
@@ -1,23 +1,21 @@
-#line 101 "../mdcode.mdc"
+#line 106 "../mdcode.mdc"
 #define _GNU_SOURCE
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 #include "mdcode.h"
 #define _GNU_SOURCE
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 #include "mdcode.h"
-#line 470 "../mdcode.mdc"
+#line 493 "../mdcode.mdc"
 #include  <ctype.h>
 #include  <string.h>
 #include  <ctype.h>
 #include  <string.h>
-
-#line 187 "../mdcode.mdc"
+#line 194 "../mdcode.mdc"
 struct psection {
        struct section;
        struct code_node *last;
        int refcnt;
        int indent;
 };
 struct psection {
        struct section;
        struct code_node *last;
        int refcnt;
        int indent;
 };
-
-#line 224 "../mdcode.mdc"
+#line 231 "../mdcode.mdc"
 static void code_linearize(struct code_node *code)
 {
        struct code_node *t;
 static void code_linearize(struct code_node *code)
 {
        struct code_node *t;
@@ -37,8 +35,7 @@ static void code_linearize(struct code_node *code)
                        t->next = next;
                }
 }
                        t->next = next;
                }
 }
-
-#line 247 "../mdcode.mdc"
+#line 254 "../mdcode.mdc"
 void code_free(struct code_node *code)
 {
        while (code) {
 void code_free(struct code_node *code)
 {
        while (code) {
@@ -50,8 +47,7 @@ void code_free(struct code_node *code)
                free(this);
        }
 }
                free(this);
        }
 }
-
-#line 276 "../mdcode.mdc"
+#line 283 "../mdcode.mdc"
 static void code_add_text(struct psection *where, struct text txt,
                          int line_no, int needs_strip)
 {
 static void code_add_text(struct psection *where, struct text txt,
                          int line_no, int needs_strip)
 {
@@ -62,7 +58,13 @@ static void code_add_text(struct psection *where, struct text txt,
        n->code = txt;
        n->indent = 0;
        n->line_no = line_no;
        n->code = txt;
        n->indent = 0;
        n->line_no = line_no;
-       n->needs_strip = needs_strip;
+       if (needs_strip) {
+               if (txt.txt[0] == '\t')
+                       n->needs_strip = 8;
+               else
+                       n->needs_strip = 4;
+       } else
+               n->needs_strip = 0;
        n->next = NULL;
        n->child = NULL;
        if (where->last)
        n->next = NULL;
        n->child = NULL;
        if (where->last)
@@ -71,8 +73,7 @@ static void code_add_text(struct psection *where, struct text txt,
                where->code = n;
        where->last = n;
 }
                where->code = n;
        where->last = n;
 }
-
-#line 299 "../mdcode.mdc"
+#line 312 "../mdcode.mdc"
 void code_add_link(struct psection *where, struct psection *to,
                   int indent)
 {
 void code_add_link(struct psection *where, struct psection *to,
                   int indent)
 {
@@ -96,13 +97,17 @@ void code_add_link(struct psection *where, struct psection *to,
                where->code = n;
        where->last = n;
 }
                where->code = n;
        where->last = n;
 }
-
-#line 338 "../mdcode.mdc"
-static int text_cmp(struct text a, struct text b)
+#line 356 "../mdcode.mdc"
+int text_cmp(struct text a, struct text b)
 {
 {
-       if (a.len != b.len)
+       int len = a.len;
+       if (len > b.len)
+               len = b.len;
+       int cmp = strncmp(a.txt, b.txt, len);
+       if (cmp)
+               return cmp;
+       else
                return a.len - b.len;
                return a.len - b.len;
-       return strncmp(a.txt, b.txt, a.len);
 }
 
 static struct psection *section_find(struct psection **list, struct text name)
 }
 
 static struct psection *section_find(struct psection **list, struct text name)
@@ -127,8 +132,7 @@ static struct psection *section_find(struct psection **list, struct text name)
        new->indent = 0;
        return new;
 }
        new->indent = 0;
        return new;
 }
-
-#line 419 "../mdcode.mdc"
+#line 442 "../mdcode.mdc"
 static char *skip_lws(char *pos, char *end)
 {
        while (pos < end && (*pos == ' ' || *pos == '\t'))
 static char *skip_lws(char *pos, char *end)
 {
        while (pos < end && (*pos == ' ' || *pos == '\t'))
@@ -167,8 +171,7 @@ static char *skip_para(char *pos, char *end, int *line_no)
        }
        return pos;
 }
        }
        return pos;
 }
-
-#line 475 "../mdcode.mdc"
+#line 498 "../mdcode.mdc"
 static struct text take_header(char *pos, char *end)
 {
        struct text section;
 static struct text take_header(char *pos, char *end)
 {
        struct text section;
@@ -208,8 +211,7 @@ static int matches(char *start, char *pos, char *end)
        return (pos + strlen(start) < end &&
                strncmp(pos, start, strlen(start)) == 0);
 }
        return (pos + strlen(start) < end &&
                strncmp(pos, start, strlen(start)) == 0);
 }
-
-#line 547 "../mdcode.mdc"
+#line 575 "../mdcode.mdc"
 static int count_space(char *sol, char *p)
 {
        int c = 0;
 static int count_space(char *sol, char *p)
 {
        int c = 0;
@@ -223,7 +225,6 @@ static int count_space(char *sol, char *p)
        return c;
 }
 
        return c;
 }
 
-
 static char *take_code(char *pos, char *end, char *marker,
                       struct psection **table, struct text section,
                       int *line_nop)
 static char *take_code(char *pos, char *end, char *marker,
                       struct psection **table, struct text section,
                       int *line_nop)
@@ -284,6 +285,12 @@ static char *take_code(char *pos, char *end, char *marker,
                struct text txt;
                txt.txt = start;
                txt.len = pos - start;
                struct text txt;
                txt.txt = start;
                txt.len = pos - start;
+               /* strip trailing blank lines */
+               while (!marker && txt.len > 2 &&
+                      start[txt.len-1] == '\n' &&
+                      start[txt.len-2] == '\n')
+                       txt.len -= 1;
+
                code_add_text(sect, txt, start_line,
                              marker == NULL);
        }
                code_add_text(sect, txt, start_line,
                              marker == NULL);
        }
@@ -294,8 +301,7 @@ static char *take_code(char *pos, char *end, char *marker,
        *line_nop = line_no;
        return pos;
 }
        *line_nop = line_no;
        return pos;
 }
-
-#line 641 "../mdcode.mdc"
+#line 674 "../mdcode.mdc"
 static struct psection *code_find(char *pos, char *end)
 {
        struct psection *table = NULL;
 static struct psection *code_find(char *pos, char *end)
 {
        struct psection *table = NULL;
@@ -335,8 +341,7 @@ static struct psection *code_find(char *pos, char *end)
        }
        return table;
 }
        }
        return table;
 }
-
-#line 701 "../mdcode.mdc"
+#line 734 "../mdcode.mdc"
 struct section *code_extract(char *pos, char *end, code_err_fn error)
 {
        struct psection *table;
 struct section *code_extract(char *pos, char *end, code_err_fn error)
 {
        struct psection *table;
@@ -382,8 +387,7 @@ struct section *code_extract(char *pos, char *end, code_err_fn error)
        }
        return result;
 }
        }
        return result;
 }
-
-#line 782 "../mdcode.mdc"
+#line 814 "../mdcode.mdc"
 void code_node_print(FILE *out, struct code_node *node,
                      char *fname)
 {
 void code_node_print(FILE *out, struct code_node *node,
                      char *fname)
 {
@@ -397,7 +401,10 @@ void code_node_print(FILE *out, struct code_node *node,
                fprintf(out, "#line %d \"%s\"\n",
                        node->line_no, fname);
                while (len && *c) {
                fprintf(out, "#line %d \"%s\"\n",
                        node->line_no, fname);
                while (len && *c) {
-                       fprintf(out, "%*s", node->indent, "");
+                       if (node->indent >= 8)
+                               fprintf(out, "\t%*s", node->indent - 8, "");
+                       else
+                               fprintf(out, "%*s", node->indent, "");
                        if (node->needs_strip) {
                                if (*c == '\t' && len > 1) {
                                        c++;
                        if (node->needs_strip) {
                                if (*c == '\t' && len > 1) {
                                        c++;
@@ -415,6 +422,5 @@ void code_node_print(FILE *out, struct code_node *node,
                }
        }
 }
                }
        }
 }
-
-#line 110 "../mdcode.mdc"
+#line 115 "../mdcode.mdc"
 
 
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"
 
 #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>
 #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;
 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;
 }
        strncpy(name, sec, len);
        name[len] = 0;
 }
-
-#line 867 "../mdcode.mdc"
+#line 901 "../mdcode.mdc"
 static int errs;
 static void pr_err(char *msg)
 {
 static int errs;
 static void pr_err(char *msg)
 {
@@ -39,18 +37,33 @@ static void pr_err(char *msg)
        fprintf(stderr, "%s\n", 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;
 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;
        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);
        }
                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",
        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];
                (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;
                }
                                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",
                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);
 }
        }
        exit(!!errs);
 }
-
-#line 128 "../mdcode.mdc"
+#line 132 "../mdcode.mdc"
 
 
index 4ab95dc84d6f03eeec372238d867eeaede9ce488..32a07602bc456506b499ccff85d0f0b8b64cbcf0 100644 (file)
@@ -1,6 +1,6 @@
-#line 96 "../mdcode.mdc"
+#line 101 "../mdcode.mdc"
 #include <stdio.h>
 #include <stdio.h>
-#line 165 "../mdcode.mdc"
+#line 172 "../mdcode.mdc"
 struct text {
        char *txt;
        int len;
 struct text {
        char *txt;
        int len;
@@ -20,19 +20,15 @@ struct code_node {
        struct code_node *next;
        struct section *child;
 };
        struct code_node *next;
        struct section *child;
 };
-
-#line 698 "../mdcode.mdc"
+#line 731 "../mdcode.mdc"
 typedef void (*code_err_fn)(char *msg);
 typedef void (*code_err_fn)(char *msg);
-
-#line 261 "../mdcode.mdc"
+#line 268 "../mdcode.mdc"
 void code_free(struct code_node *code);
 void code_free(struct code_node *code);
-
-#line 749 "../mdcode.mdc"
+#line 352 "../mdcode.mdc"
+int text_cmp(struct text a, struct text b);
+#line 782 "../mdcode.mdc"
 struct section *code_extract(char *pos, char *end, code_err_fn error);
 struct section *code_extract(char *pos, char *end, code_err_fn error);
-
-
-#line 815 "../mdcode.mdc"
+#line 850 "../mdcode.mdc"
 void code_node_print(FILE *out, struct code_node *node, char *fname);
 void code_node_print(FILE *out, struct code_node *node, char *fname);
-
-#line 99 "../mdcode.mdc"
+#line 104 "../mdcode.mdc"
 
 
index 5cf9365f473997eecec21af3f5d6c4f10392a308..251b478327f58e9e22cd10148597082483137788 100644 (file)
@@ -1,23 +1,17 @@
-#line 88 "../mdcode.mdc"
+#line 93 "../mdcode.mdc"
 CFLAGS += -Wall -g
 all::
 mdcode.h libmdcode.c md2c.c mdcode.mk :  mdcode.mdc
        ./md2c mdcode.mdc
 CFLAGS += -Wall -g
 all::
 mdcode.h libmdcode.c md2c.c mdcode.mk :  mdcode.mdc
        ./md2c mdcode.mdc
-
-
-#line 113 "../mdcode.mdc"
+#line 118 "../mdcode.mdc"
 all :: libmdcode.o
 libmdcode.o : libmdcode.c mdcode.h
        $(CC) $(CFLAGS) -c libmdcode.c
 all :: libmdcode.o
 libmdcode.o : libmdcode.c mdcode.h
        $(CC) $(CFLAGS) -c libmdcode.c
-
-
-#line 131 "../mdcode.mdc"
+#line 135 "../mdcode.mdc"
 all :: md2c
 md2c : md2c.o libmdcode.o
        $(CC) $(CFLAGS) -o md2c md2c.o libmdcode.o
 md2c.o : md2c.c mdcode.h
        $(CC) $(CFLAGS) -c md2c.c
 all :: md2c
 md2c : md2c.o libmdcode.o
        $(CC) $(CFLAGS) -o md2c md2c.o libmdcode.o
 md2c.o : md2c.c mdcode.h
        $(CC) $(CFLAGS) -c md2c.c
-
-#line 200 "../mdcode.mdc"
+#line 207 "../mdcode.mdc"
 CFLAGS += -fplan9-extensions
 CFLAGS += -fplan9-extensions
-