X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=tools%2Fdoc_extract-core.c;h=aa0a26d829197ef351a8643527fddb9ca6dd768a;hb=48d28d7fb09829cf7b4dbca020bcae2bf9b93f4c;hp=a8d9335b0c7a9a46ce94340801d09da1835cab28;hpb=7beaa3448fa8e6015798c1609f33d96e8986063d;p=ccan-lca-2011.git diff --git a/tools/doc_extract-core.c b/tools/doc_extract-core.c index a8d9335..aa0a26d 100644 --- a/tools/doc_extract-core.c +++ b/tools/doc_extract-core.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "doc_extract.h" #include "tools.h" @@ -26,8 +27,10 @@ static char **grab_doc(char **lines, unsigned int num) for (i = 0; lines[i]; i++) { if (streq(lines[i], "/**")) { printing = true; - if (num != 0) - talloc_append_string(ret[num-1], "\n"); + if (num != 0) { + ret[num-1] = talloc_append_string(ret[num-1], + "\n"); + } } else if (streq(lines[i], " */")) printing = false; else if (printing) { @@ -48,34 +51,41 @@ 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; + char *secname; - if (!isupper(line[0])) - return false; - len = strspn(line, IDENT_CHARS); - if (line[len] != ':') - return false; + /* Any number of upper case words separated by spaces, ending in : */ + 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 ' - ' */ -static bool is_summary_line(const char *line) +/* Summary line is form ' - ' (spaces for 'struct foo -') */ +static unsigned int is_summary_line(const char *line) { unsigned int id_len; - id_len = strspn(line, IDENT_CHARS); + id_len = strspn(line, IDENT_CHARS" "); if (id_len == 0) - return false; - if (!strstarts(line + id_len, " - ")) - return false; + return 0; + if (strspn(line, " ") == id_len) + return 0; + if (!strstarts(line + id_len-1, " - ")) + return 0; + return id_len - 1; +} + +static bool empty_section(struct doc_section *d) +{ + unsigned int i; + for (i = 0; i < d->num_lines; i++) + if (!is_blank(d->lines[i])) + return false; return true; } @@ -83,11 +93,27 @@ static struct doc_section *new_section(struct list_head *list, const char *function, const char *type) { - struct doc_section *d = talloc(list, struct doc_section); + struct doc_section *d; + char *lowertype; + unsigned int i; + + /* If previous section was empty, delete it. */ + d = list_tail(list, struct doc_section, list); + if (d && empty_section(d)) { + list_del(&d->list); + talloc_free(d); + } + + d = talloc(list, struct doc_section); d->function = function; - d->type = type; + lowertype = talloc_size(d, strlen(type) + 1); + /* Canonicalize type to lower case. */ + for (i = 0; i < strlen(type)+1; i++) + lowertype[i] = tolower(type[i]); + d->type = lowertype; d->lines = NULL; d->num_lines = 0; + list_add_tail(list, &d->list); return d; } @@ -111,26 +137,24 @@ struct list_head *extract_doc_sections(char **rawlines, unsigned int num) list_head_init(list); for (i = 0; lines[i]; i++) { - if (is_summary_line(lines[i])) { - function = talloc_strndup(list, lines[i], - strcspn(lines[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"); - add_line(curr, strstr(lines[i], " - ") + 3); + 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); + } else if ((type = is_section(list, lines[i], &extra)) != NULL){ curr = new_section(list, function, type); - add_line(curr, lines[i] + sectlen + 1 - + strspn(lines[i] + sectlen + 1, " \t")); + 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);