=================================================================== RCS file: /cvs/mandoc/Attic/makewhatis.c,v retrieving revision 1.9 retrieving revision 1.14 diff -u -p -r1.9 -r1.14 --- mandoc/Attic/makewhatis.c 2011/06/22 09:10:36 1.9 +++ mandoc/Attic/makewhatis.c 2011/07/01 10:46:32 1.14 @@ -1,4 +1,4 @@ -/* $Id: makewhatis.c,v 1.9 2011/06/22 09:10:36 kristaps Exp $ */ +/* $Id: makewhatis.c,v 1.14 2011/07/01 10:46:32 kristaps Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * @@ -21,11 +21,6 @@ #include #include -#ifdef __linux__ -# include -#else -# include -#endif #include #include #include @@ -33,6 +28,12 @@ #include #include +#ifdef __linux__ +# include +#else +# include +#endif + #include "man.h" #include "mdoc.h" #include "mandoc.h" @@ -44,15 +45,17 @@ /* Bit-fields. See makewhatis.1. */ -#define TYPE_NAME 0x01 -#define TYPE_FUNCTION 0x02 -#define TYPE_UTILITY 0x04 -#define TYPE_INCLUDES 0x08 -#define TYPE_VARIABLE 0x10 -#define TYPE_STANDARD 0x20 -#define TYPE_AUTHOR 0x40 -#define TYPE_CONFIG 0x80 -#define TYPE_DESC 0x100 +#define TYPE_NAME 0x01 +#define TYPE_FUNCTION 0x02 +#define TYPE_UTILITY 0x04 +#define TYPE_INCLUDES 0x08 +#define TYPE_VARIABLE 0x10 +#define TYPE_STANDARD 0x20 +#define TYPE_AUTHOR 0x40 +#define TYPE_CONFIG 0x80 +#define TYPE_DESC 0x100 +#define TYPE_XREF 0x200 +#define TYPE_PATH 0x400 /* Buffer for storing growable data. */ @@ -72,6 +75,8 @@ struct buf { const struct mdoc_node *n, \ const struct mdoc_meta *m +static void buf_appendmdoc(struct buf *, + const struct mdoc_node *, int); static void buf_append(struct buf *, const char *); static void buf_appendb(struct buf *, const void *, size_t); @@ -87,8 +92,10 @@ static void pmdoc_Fn(MDOC_ARGS); static void pmdoc_Fo(MDOC_ARGS); static void pmdoc_Nd(MDOC_ARGS); static void pmdoc_Nm(MDOC_ARGS); +static void pmdoc_Pa(MDOC_ARGS); static void pmdoc_St(MDOC_ARGS); static void pmdoc_Vt(MDOC_ARGS); +static void pmdoc_Xr(MDOC_ARGS); static void usage(void); typedef void (*pmdoc_nf)(MDOC_ARGS); @@ -129,12 +136,12 @@ static const pmdoc_nf mdocs[MDOC_MAX] = { pmdoc_Nm, /* Nm */ NULL, /* Op */ NULL, /* Ot */ - NULL, /* Pa */ + pmdoc_Pa, /* Pa */ NULL, /* Rv */ pmdoc_St, /* St */ pmdoc_Vt, /* Va */ pmdoc_Vt, /* Vt */ - NULL, /* Xr */ + pmdoc_Xr, /* Xr */ NULL, /* %A */ NULL, /* %B */ NULL, /* %D */ @@ -236,7 +243,7 @@ main(int argc, char *argv[]) fbuf[MAXPATHLEN], /* btree fname */ fbbuf[MAXPATHLEN], /* btree backup fname */ vbuf[8]; /* stringified record number */ - int ch, seq; + int ch, seq, verb; DB *idx, /* index database */ *db, /* keyword database */ *hash; /* temporary keyword hashtable */ @@ -256,12 +263,16 @@ main(int argc, char *argv[]) ++progname; dir = ""; + verb = 0; - while (-1 != (ch = getopt(argc, argv, "d:"))) + while (-1 != (ch = getopt(argc, argv, "d:v"))) switch (ch) { case ('d'): dir = optarg; break; + case ('v'): + verb++; + break; default: usage(); return((int)MANDOCLEVEL_BADARG); @@ -298,7 +309,7 @@ main(int argc, char *argv[]) '\0' != fbbuf[MAXPATHLEN - 2] || '\0' != ibuf[MAXPATHLEN - 2] || '\0' != ibbuf[MAXPATHLEN - 2]) { - fprintf(stderr, "%s: Path too long\n", progname); + fprintf(stderr, "%s: Path too long\n", dir); exit((int)MANDOCLEVEL_SYSERR); } @@ -326,9 +337,9 @@ main(int argc, char *argv[]) } /* - * Try parsing the manuals given on the command line. If we - * totally fail, then just keep on going. Take resulting trees - * and push them down into the database code. + * Try parsing each manual given on the command line. + * If we fail, then emit an error and keep on going. + * Take resulting trees and push them down into the database code. * Use the auto-parser and don't report any errors. */ @@ -348,6 +359,8 @@ main(int argc, char *argv[]) while (NULL != (fn = *argv++)) { mparse_reset(mp); + /* Initialise the in-memory hash of keywords. */ + if (hash) (*hash->close)(hash); @@ -364,12 +377,12 @@ main(int argc, char *argv[]) fprintf(stderr, "%s: Parse failure\n", fn); continue; } + mparse_result(mp, &mdoc, &man); + if (NULL == mdoc && NULL == man) continue; - /* Manual section: can be empty string. */ - msec = NULL != mdoc ? mdoc_meta(mdoc)->msec : man_meta(man)->msec; @@ -378,9 +391,6 @@ main(int argc, char *argv[]) man_meta(man)->title; arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL; - assert(msec); - assert(mtitle); - /* * The index record value consists of a nil-terminated * filename, a nil-terminated manual section, and a @@ -422,8 +432,9 @@ main(int argc, char *argv[]) val.size = sizeof(vbuf); val.data = vbuf; - printf("Added: %s (%zu): 0x%x\n", - (char *)key.data, key.size, + if (verb > 1) + printf("%s: Keyword %s (%zu): 0x%x\n", + fn, (char *)key.data, key.size, *(int *)val.data); dbt_put(db, fbbuf, &key, &val); @@ -449,7 +460,8 @@ main(int argc, char *argv[]) val.data = dbuf.cp; val.size = dbuf.len; - printf("Indexed: %s\n", fn); + if (verb > 0) + printf("%s: Indexed\n", fn); dbt_put(idx, ibbuf, &key, &val); rec++; @@ -457,6 +469,7 @@ main(int argc, char *argv[]) (*db->close)(db); (*idx->close)(idx); + if (hash) (*hash->close)(hash); @@ -513,6 +526,33 @@ buf_append(struct buf *buf, const char *cp) buf_appendb(buf, cp, sz + 1); } +/* + * Recursively add all text from a given node. + * This is optimised for general mdoc nodes in this context, which do + * not consist of subexpressions and having a recursive call for n->next + * would be wasteful. + * The "f" variable should be 0 unless called from pmdoc_Nd for the + * description buffer, which does not start at the beginning of the + * buffer. + */ +static void +buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f) +{ + + for ( ; n; n = n->next) { + if (n->child) + buf_appendmdoc(buf, n->child, f); + + if (MDOC_TEXT == n->type && f) { + f = 0; + buf_appendb(buf, n->string, + strlen(n->string) + 1); + } else if (MDOC_TEXT == n->type) + buf_append(buf, n->string); + + } +} + /* ARGSUSED */ static void pmdoc_An(MDOC_ARGS) @@ -521,10 +561,7 @@ pmdoc_An(MDOC_ARGS) if (SEC_AUTHORS != n->sec) return; - for (n = n->child; n; n = n->next) - if (MDOC_TEXT == n->type) - buf_append(buf, n->string); - + buf_appendmdoc(buf, n->child, 0); hash_put(hash, buf, TYPE_AUTHOR); } @@ -582,10 +619,7 @@ pmdoc_Cd(MDOC_ARGS) if (SEC_SYNOPSIS != n->sec) return; - for (n = n->child; n; n = n->next) - if (MDOC_TEXT == n->type) - buf_append(buf, n->string); - + buf_appendmdoc(buf, n->child, 0); hash_put(hash, buf, TYPE_CONFIG); } @@ -645,6 +679,25 @@ pmdoc_St(MDOC_ARGS) /* ARGSUSED */ static void +pmdoc_Xr(MDOC_ARGS) +{ + + if (NULL == (n = n->child)) + return; + + buf_appendb(buf, n->string, strlen(n->string)); + + if (NULL != (n = n->next)) { + buf_appendb(buf, ".", 1); + buf_appendb(buf, n->string, strlen(n->string) + 1); + } else + buf_appendb(buf, ".", 2); + + hash_put(hash, buf, TYPE_XREF); +} + +/* ARGSUSED */ +static void pmdoc_Vt(MDOC_ARGS) { const char *start; @@ -699,37 +752,35 @@ pmdoc_Fo(MDOC_ARGS) static void pmdoc_Nd(MDOC_ARGS) { - int first; - size_t sz; - - for (first = 1, n = n->child; n; n = n->next) { - if (MDOC_TEXT != n->type) - continue; - if (first) { - sz = strlen(n->string) + 1; - buf_appendb(dbuf, n->string, sz); - buf_appendb(buf, n->string, sz); - } else { - buf_append(dbuf, n->string); - buf_append(buf, n->string); - } + if (MDOC_BODY != n->type) + return; - first = 0; - } + buf_appendmdoc(dbuf, n->child, 1); + buf_appendmdoc(buf, n->child, 0); hash_put(hash, buf, TYPE_DESC); } /* ARGSUSED */ static void +pmdoc_Pa(MDOC_ARGS) +{ + + if (SEC_FILES != n->sec) + return; + + buf_appendmdoc(buf, n->child, 0); + hash_put(hash, buf, TYPE_PATH); +} + +/* ARGSUSED */ +static void pmdoc_Nm(MDOC_ARGS) { if (SEC_NAME == n->sec) { - for (n = n->child; n; n = n->next) - if (MDOC_TEXT == n->type) - buf_append(buf, n->string); + buf_appendmdoc(buf, n->child, 0); hash_put(hash, buf, TYPE_NAME); return; } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type) @@ -738,10 +789,7 @@ pmdoc_Nm(MDOC_ARGS) if (NULL == n->child) buf_append(buf, m->name); - for (n = n->child; n; n = n->next) - if (MDOC_TEXT == n->type) - buf_append(buf, n->string); - + buf_appendmdoc(buf, n->child, 0); hash_put(hash, buf, TYPE_UTILITY); } @@ -924,5 +972,6 @@ static void usage(void) { - fprintf(stderr, "usage: %s [-d path] [file...]\n", progname); + fprintf(stderr, "usage: %s [-v] [-d path] [file...]\n", + progname); }