]> ocean-lang.org Git - ocean/blobdiff - csrc/boot-strap/libmdcode.c
boot-strap: update bootstrap code.
[ocean] / csrc / boot-strap / libmdcode.c
index 78a4262d8d9873a9e8cf9493bbf1850c4cab9c43..cf5d85cc7937f3459f742d2192f5777209923daa 100644 (file)
@@ -1,23 +1,21 @@
-#line 100 "../mdcode.mdc"
+#line 106 "../mdcode.mdc"
 #define _GNU_SOURCE
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 #include "mdcode.h"
-#line 461 "../mdcode.mdc"
+#line 493 "../mdcode.mdc"
 #include  <ctype.h>
 #include  <string.h>
-
-#line 179 "../mdcode.mdc"
+#line 194 "../mdcode.mdc"
 struct psection {
        struct section;
        struct code_node *last;
        int refcnt;
        int indent;
 };
-
-#line 216 "../mdcode.mdc"
+#line 231 "../mdcode.mdc"
 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;
                }
 }
-
-#line 239 "../mdcode.mdc"
+#line 254 "../mdcode.mdc"
 void code_free(struct code_node *code)
 {
        while (code) {
@@ -50,10 +47,9 @@ void code_free(struct code_node *code)
                free(this);
        }
 }
-
-#line 268 "../mdcode.mdc"
+#line 283 "../mdcode.mdc"
 static void code_add_text(struct psection *where, struct text txt,
-                         int line_no)
+                         int line_no, int needs_strip)
 {
        struct code_node *n;
        if (txt.len == 0)
@@ -62,6 +58,13 @@ static void code_add_text(struct psection *where, struct text txt,
        n->code = txt;
        n->indent = 0;
        n->line_no = line_no;
+       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)
@@ -70,8 +73,7 @@ static void code_add_text(struct psection *where, struct text txt,
                where->code = n;
        where->last = n;
 }
-
-#line 290 "../mdcode.mdc"
+#line 312 "../mdcode.mdc"
 void code_add_link(struct psection *where, struct psection *to,
                   int indent)
 {
@@ -95,13 +97,17 @@ void code_add_link(struct psection *where, struct psection *to,
                where->code = n;
        where->last = n;
 }
-
-#line 329 "../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 strncmp(a.txt, b.txt, a.len);
 }
 
 static struct psection *section_find(struct psection **list, struct text name)
@@ -126,8 +132,7 @@ static struct psection *section_find(struct psection **list, struct text name)
        new->indent = 0;
        return new;
 }
-
-#line 410 "../mdcode.mdc"
+#line 442 "../mdcode.mdc"
 static char *skip_lws(char *pos, char *end)
 {
        while (pos < end && (*pos == ' ' || *pos == '\t'))
@@ -166,8 +171,7 @@ static char *skip_para(char *pos, char *end, int *line_no)
        }
        return pos;
 }
-
-#line 466 "../mdcode.mdc"
+#line 498 "../mdcode.mdc"
 static struct text take_header(char *pos, char *end)
 {
        struct text section;
@@ -207,8 +211,7 @@ static int matches(char *start, char *pos, char *end)
        return (pos + strlen(start) < end &&
                strncmp(pos, start, strlen(start)) == 0);
 }
-
-#line 538 "../mdcode.mdc"
+#line 575 "../mdcode.mdc"
 static int count_space(char *sol, char *p)
 {
        int c = 0;
@@ -222,7 +225,6 @@ static int count_space(char *sol, char *p)
        return c;
 }
 
-
 static char *take_code(char *pos, char *end, char *marker,
                       struct psection **table, struct text section,
                       int *line_nop)
@@ -266,7 +268,8 @@ static char *take_code(char *pos, char *end, char *marker,
                        struct text txt;
                        txt.txt = start;
                        txt.len = pos - start;
-                       code_add_text(sect, txt, start_line);
+                       code_add_text(sect, txt, start_line,
+                                     marker == NULL);
                }
                ref = take_header(t, end);
                if (ref.len) {
@@ -282,7 +285,14 @@ static char *take_code(char *pos, char *end, char *marker,
                struct text txt;
                txt.txt = start;
                txt.len = pos - start;
-               code_add_text(sect, txt, start_line);
+               /* 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);
        }
        if (marker) {
                pos = skip_line(pos, end);
@@ -291,8 +301,7 @@ static char *take_code(char *pos, char *end, char *marker,
        *line_nop = line_no;
        return pos;
 }
-
-#line 630 "../mdcode.mdc"
+#line 674 "../mdcode.mdc"
 static struct psection *code_find(char *pos, char *end)
 {
        struct psection *table = NULL;
@@ -332,8 +341,7 @@ static struct psection *code_find(char *pos, char *end)
        }
        return table;
 }
-
-#line 690 "../mdcode.mdc"
+#line 734 "../mdcode.mdc"
 struct section *code_extract(char *pos, char *end, code_err_fn error)
 {
        struct psection *table;
@@ -379,6 +387,40 @@ struct section *code_extract(char *pos, char *end, code_err_fn error)
        }
        return result;
 }
+#line 814 "../mdcode.mdc"
+void code_node_print(FILE *out, struct code_node *node,
+                     char *fname)
+{
+       for (; node; node = node->next) {
+               char *c = node->code.txt;
+               int len = node->code.len;
+
+               if (!len)
+                       continue;
 
-#line 109 "../mdcode.mdc"
+               fprintf(out, "#line %d \"%s\"\n",
+                       node->line_no, fname);
+               while (len && *c) {
+                       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++;
+                                       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 115 "../mdcode.mdc"