X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fdoc_extract-core.c;h=781ce2bdbcc8d5a08baac318f5e419d14fe46781;hp=2862eedc3b91a839749e34fe2f0ac516532a1eb6;hb=3d917ba6dffe2029608a3d4c870dfdb4033ca4c9;hpb=ad3f309e3c13d6b88864aab146895c9df9bc6e5b diff --git a/tools/doc_extract-core.c b/tools/doc_extract-core.c index 2862eedc..781ce2bd 100644 --- a/tools/doc_extract-core.c +++ b/tools/doc_extract-core.c @@ -11,16 +11,18 @@ #include #include #include +#include #include "doc_extract.h" #include "tools.h" -static char **grab_doc(char **lines, unsigned int num) +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); + 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++) { @@ -39,6 +41,7 @@ static char **grab_doc(char **lines, unsigned int num) ret[num++] = talloc_strdup(ret, lines[i]+2); else errx(1, "Malformed line %u", i); + (*linemap)[num-1] = i; } } ret[num] = NULL; @@ -50,28 +53,17 @@ static bool is_blank(const char *line) return line && line[strspn(line, " \t\n")] == '\0'; } -static bool is_section(const char *line, bool one_liner) +static char *is_section(const void *ctx, const char *line, char **value) { - unsigned int len = 0; + char *secname; /* Any number of upper case words separated by spaces, ending in : */ - for (;;) { - if (!isupper(line[len])) - return false; - len += strspn(line+len, IDENT_CHARS); - if (line[len] == ':') - break; - - if (line[len] != ' ') - return false; - len++; - } + if (!strreg(ctx, line, + "^([A-Z][a-zA-Z0-9_]*( [A-Z][a-zA-Z0-9_]*)*):[ \t\n]*(.*)", + &secname, NULL, value)) + return NULL; - /* If it can be a one-liner, a space is sufficient.*/ - if (one_liner) - return (line[len+1] == ' ' || line[len+1] == '\t'); - - return line[len] == ':' && is_blank(line+len+1); + return secname; } /* Summary line is form ' - ' (spaces for 'struct foo -') */ @@ -82,9 +74,10 @@ static unsigned int is_summary_line(const char *line) id_len = strspn(line, IDENT_CHARS" "); if (id_len == 0) return 0; + if (strspn(line, " ") == id_len) + return 0; if (!strstarts(line + id_len-1, " - ")) return 0; - return id_len - 1; } @@ -100,7 +93,8 @@ static bool empty_section(struct doc_section *d) static struct doc_section *new_section(struct list_head *list, const char *function, - const char *type) + const char *type, + unsigned int srcline) { struct doc_section *d; char *lowertype; @@ -122,6 +116,7 @@ static struct doc_section *new_section(struct list_head *list, d->type = lowertype; d->lines = NULL; d->num_lines = 0; + d->srcline = srcline; list_add_tail(list, &d->list); return d; @@ -134,9 +129,10 @@ 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) +struct list_head *extract_doc_sections(char **rawlines) { - char **lines = grab_doc(rawlines, num); + unsigned int *linemap; + char **lines = grab_doc(rawlines, &linemap); const char *function = NULL; struct doc_section *curr = NULL; unsigned int i; @@ -147,27 +143,25 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num) for (i = 0; lines[i]; i++) { unsigned funclen; + char *type, *extra; funclen = is_summary_line(lines[i]); if (funclen) { function = talloc_strndup(list, lines[i], funclen); - curr = new_section(list, function, "summary"); + curr = new_section(list, function, "summary", + linemap[i]); add_line(curr, lines[i] + funclen + 3); - curr = new_section(list, function, "description"); - } else if (is_section(lines[i], false)) { - char *type = talloc_strndup(curr, lines[i], - strcspn(lines[i], ":")); - curr = new_section(list, function, type); - } else if (is_section(lines[i], true)) { - unsigned int sectlen = strcspn(lines[i], ":"); - char *type = talloc_strndup(curr, lines[i], sectlen); - curr = new_section(list, function, type); - add_line(curr, lines[i] + sectlen + 1 - + strspn(lines[i] + sectlen + 1, " \t")); + curr = new_section(list, function, "description", + linemap[i]); + } else if ((type = is_section(list, lines[i], &extra)) != NULL){ + curr = new_section(list, function, type, linemap[i]); + if (!streq(extra, "")) { + add_line(curr, extra); + curr = NULL; + } } else { - if (!curr) - continue; - add_line(curr, lines[i]); + if (curr) + add_line(curr, lines[i]); } } talloc_free(lines);