tdb2: unify tdb1_transaction_start etc. into tdb_transaction_start.
[ccan] / tools / doc_extract-core.c
index d7490598719f113ed0aa72738efb6c87c9734c91..8695a7e16fa2e2437c1ca1671b2eada3134f2b76 100644 (file)
@@ -1,4 +1,7 @@
 /* This merely extracts, doesn't do XML or anything. */
+#include <ccan/talloc/talloc.h>
+#include <ccan/str/str.h>
+#include <ccan/str_talloc/str_talloc.h>
 #include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <stdbool.h>
 #include <ctype.h>
-#include <ccan/talloc/talloc.h>
-#include <ccan/str/str.h>
-#include <ccan/str_talloc/str_talloc.h>
 #include "doc_extract.h"
 #include "tools.h"
 
-static char **grab_doc(char **lines, unsigned int num, unsigned int **linemap)
+static char **grab_doc(char **lines, unsigned int **linemap)
 {
        char **ret;
-       unsigned int i;
+       unsigned int i, num;
        bool printing = false;
 
-       ret = talloc_array(NULL, char *, num+1);
-       *linemap = talloc_array(ret, unsigned int, num);
+       ret = talloc_array(NULL, char *, talloc_array_length(lines));
+       *linemap = talloc_array(ret, unsigned int, talloc_array_length(lines));
 
        num = 0;
        for (i = 0; lines[i]; i++) {
@@ -129,10 +129,67 @@ static void add_line(struct doc_section *curr, const char *line)
        curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
 }
 
-struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
+/* We convert tabs to spaces here. */
+static void add_detabbed_line(struct doc_section *curr, const char *rawline)
+{
+       unsigned int i, eff_i, len, off = 0;
+       char *line;
+
+       /* Worst-case alloc: 8 spaces per tab. */
+       line = talloc_array(curr, char, strlen(rawline) +
+                           strcount(rawline, "\t") * 7 + 1);
+       len = 0;
+
+       /* We keep track of the *effective* offset of i. */
+       for (i = eff_i = 0; i < strlen(rawline); i++) {
+               if (rawline[i] == '\t') {
+                       do {
+                               line[len++] = ' ';
+                               eff_i++;
+                       } while (eff_i % 8 != 0);
+               } else {
+                       line[len++] = rawline[i];
+                       if (off == 0 && rawline[i] == '*')
+                               off = i + 1;
+                       eff_i++;
+               }
+       }
+       line[len] = '\0';
+
+       add_line(curr, line + off);
+       talloc_free(line);
+}
+
+/* Not very efficient: we could track prefix length while doing
+ * add_detabbed_line */
+static void trim_lines(struct doc_section *curr)
+{
+       unsigned int i, trim = -1;
+
+       /* Get minimum whitespace prefix. */
+       for (i = 0; i < curr->num_lines; i++) {
+               unsigned int prefix = strspn(curr->lines[i], " ");
+               /* Ignore blank lines */
+               if (curr->lines[i][prefix] == '\0')
+                       continue;
+               if (prefix < trim)
+                       trim = prefix;
+       }
+
+       /* Now trim it. */
+       for (i = 0; i < curr->num_lines; i++) {
+               unsigned int prefix = strspn(curr->lines[i], " ");
+               if (prefix < trim)
+                       curr->lines[i] += prefix;
+               else
+                       curr->lines[i] += trim;
+       }
+}
+
+struct list_head *extract_doc_sections(char **rawlines)
 {
        unsigned int *linemap;
-       char **lines = grab_doc(rawlines, num, &linemap);
+       char **lines = grab_doc(rawlines, &linemap);
        const char *function = NULL;
        struct doc_section *curr = NULL;
        unsigned int i;
@@ -161,9 +218,13 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
                        }
                } else {
                        if (curr)
-                               add_line(curr, lines[i]);
+                               add_detabbed_line(curr, rawlines[linemap[i]]);
                }
        }
+
+       list_for_each(list, curr, list)
+               trim_lines(curr);
+
        talloc_free(lines);
        return list;
 }