=================================================================== RCS file: /cvs/mandoc/Attic/makewhatis.c,v retrieving revision 1.10 retrieving revision 1.14 diff -u -p -r1.10 -r1.14 --- mandoc/Attic/makewhatis.c 2011/06/22 10:36:36 1.10 +++ mandoc/Attic/makewhatis.c 2011/07/01 10:46:32 1.14 @@ -1,4 +1,4 @@ -/* $Id: makewhatis.c,v 1.10 2011/06/22 10:36:36 kristaps Exp $ */ +/* $Id: makewhatis.c,v 1.14 2011/07/01 10:46:32 kristaps Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * @@ -54,6 +54,8 @@ #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. */ @@ -73,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); @@ -88,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); @@ -130,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 */ @@ -520,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) @@ -528,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); } @@ -589,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); } @@ -652,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; @@ -706,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) @@ -745,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); }