=================================================================== RCS file: /cvs/mandoc/Attic/makewhatis.c,v retrieving revision 1.3 retrieving revision 1.14 diff -u -p -r1.3 -r1.14 --- mandoc/Attic/makewhatis.c 2011/06/21 13:05:14 1.3 +++ mandoc/Attic/makewhatis.c 2011/07/01 10:46:32 1.14 @@ -1,4 +1,4 @@ -/* $Id: makewhatis.c,v 1.3 2011/06/21 13:05:14 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" @@ -42,16 +43,22 @@ #define MANDOC_BUFSZ BUFSIZ #define MANDOC_FLAGS O_CREAT|O_TRUNC|O_RDWR -#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__MAX TYPE_CONFIG +/* 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_XREF 0x200 +#define TYPE_PATH 0x400 + +/* Buffer for storing growable data. */ + struct buf { char *cp; size_t len; @@ -60,21 +67,21 @@ struct buf { #define MAN_ARGS DB *hash, \ struct buf *buf, \ - DBT *rval, size_t *rsz, \ + struct buf *dbuf, \ const struct man_node *n #define MDOC_ARGS DB *hash, \ struct buf *buf, \ - DBT *rval, size_t *rsz, \ + struct buf *dbuf, \ const struct mdoc_node *n, \ const struct mdoc_meta *m -static void dbt_append(DBT *, size_t *, const char *); -static void dbt_appendb(DBT *, size_t *, +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); -static void dbt_init(DBT *, size_t *); static void dbt_put(DB *, const char *, DBT *, DBT *); static void hash_put(DB *, const struct buf *, int); -static void usage(void); static int pman_node(MAN_ARGS); static void pmdoc_node(MDOC_ARGS); static void pmdoc_An(MDOC_ARGS); @@ -85,13 +92,14 @@ 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); -static const char *progname; - static const pmdoc_nf mdocs[MDOC_MAX] = { NULL, /* Ap */ NULL, /* Dd */ @@ -128,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 */ @@ -217,6 +225,8 @@ static const pmdoc_nf mdocs[MDOC_MAX] = { NULL, /* Ta */ }; +static const char *progname; + int main(int argc, char *argv[]) { @@ -233,16 +243,16 @@ 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 */ - DBT rkey, rval, /* recno entries */ - key, val; /* persistent keyword entries */ - size_t sv, rsz; + DBT key, val; + size_t sv; BTREEINFO info; /* btree configuration */ recno_t rec; /* current record number */ - struct buf buf; /* keyword buffer */ + struct buf buf, /* keyword buffer */ + dbuf; /* description buffer */ extern int optind; extern char *optarg; @@ -253,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); @@ -295,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); } @@ -303,23 +317,14 @@ main(int argc, char *argv[]) * For the keyword database, open a BTREE database that allows * duplicates. * For the index database, use a standard RECNO database type. - * For the temporary keyword hashtable, use the HASH database - * type. */ - hash = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL); - if (NULL == hash) { - perror("hash"); - exit((int)MANDOCLEVEL_SYSERR); - } - memset(&info, 0, sizeof(BTREEINFO)); info.flags = R_DUP; db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info); if (NULL == db) { perror(fbbuf); - (*hash->close)(hash); exit((int)MANDOCLEVEL_SYSERR); } @@ -328,49 +333,56 @@ main(int argc, char *argv[]) if (NULL == db) { perror(ibbuf); (*db->close)(db); - (*hash->close)(hash); exit((int)MANDOCLEVEL_SYSERR); } /* - * 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. */ mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL); - memset(&key, 0, sizeof(DBT)); - memset(&val, 0, sizeof(DBT)); - memset(&rkey, 0, sizeof(DBT)); - memset(&rval, 0, sizeof(DBT)); - - rkey.size = sizeof(recno_t); - rec = 1; - rsz = 0; + hash = NULL; memset(&buf, 0, sizeof(struct buf)); + memset(&dbuf, 0, sizeof(struct buf)); - buf.size = MANDOC_BUFSZ; + buf.size = dbuf.size = MANDOC_BUFSZ; + buf.cp = mandoc_malloc(buf.size); + dbuf.cp = mandoc_malloc(dbuf.size); while (NULL != (fn = *argv++)) { mparse_reset(mp); + /* Initialise the in-memory hash of keywords. */ + + if (hash) + (*hash->close)(hash); + + hash = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL); + + if (NULL == hash) { + perror("hash"); + exit((int)MANDOCLEVEL_SYSERR); + } + /* Parse and get (non-empty) AST. */ if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) { 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; @@ -379,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 @@ -390,24 +399,22 @@ main(int argc, char *argv[]) * going to write a nil byte in its place. */ - dbt_init(&rval, &rsz); - dbt_appendb(&rval, &rsz, fn, strlen(fn) + 1); - dbt_appendb(&rval, &rsz, msec, strlen(msec) + 1); - dbt_appendb(&rval, &rsz, mtitle, strlen(mtitle) + 1); - dbt_appendb(&rval, &rsz, arch ? arch : "", + dbuf.len = 0; + buf_appendb(&dbuf, fn, strlen(fn) + 1); + buf_appendb(&dbuf, msec, strlen(msec) + 1); + buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1); + buf_appendb(&dbuf, arch ? arch : "", arch ? strlen(arch) + 1 : 1); - sv = rval.size; + sv = dbuf.len; /* Fix the record number in the btree value. */ if (mdoc) - pmdoc_node(hash, &buf, &rval, - &rsz, mdoc_node(mdoc), - mdoc_meta(mdoc)); + pmdoc_node(hash, &buf, &dbuf, + mdoc_node(mdoc), mdoc_meta(mdoc)); else - pman_node(hash, &buf, &rval, - &rsz, man_node(man)); + pman_node(hash, &buf, &dbuf, man_node(man)); /* * Copy from the in-memory hashtable of pending keywords @@ -419,18 +426,19 @@ main(int argc, char *argv[]) seq = R_FIRST; while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) { + seq = R_NEXT; + memcpy(vbuf, val.data, sizeof(uint32_t)); val.size = sizeof(vbuf); val.data = vbuf; + + 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); - /*fprintf(stderr, "Recording: %s (0x%x)\n", - (char *)key.data, - *(int *)val.data);*/ - if ((*hash->del)(hash, &key, 0) < 0) { - perror("hash"); - exit((int)MANDOCLEVEL_SYSERR); - } - seq = R_NEXT; + } if (ch < 0) { @@ -443,24 +451,32 @@ main(int argc, char *argv[]) * set, put an empty one in now. */ - if (rval.size == sv) - dbt_appendb(&rval, &rsz, "", 1); + if (dbuf.len == sv) + buf_appendb(&dbuf, "", 1); - rkey.data = &rec; - dbt_put(idx, ibbuf, &rkey, &rval); + key.data = &rec; + key.size = sizeof(recno_t); - printf("Indexed: %s\n", fn); + val.data = dbuf.cp; + val.size = dbuf.len; + + if (verb > 0) + printf("%s: Indexed\n", fn); + + dbt_put(idx, ibbuf, &key, &val); rec++; } (*db->close)(db); (*idx->close)(idx); - (*hash->close)(hash); + if (hash) + (*hash->close)(hash); + mparse_free(mp); - free(rval.data); free(buf.cp); + free(dbuf.cp); /* Atomically replace the file with our temporary one. */ @@ -473,46 +489,9 @@ main(int argc, char *argv[]) } /* - * Initialise the stored database key whose data buffer is shared - * between uses (as the key must sometimes be constructed from an array - * of + * Grow the buffer (if necessary) and copy in a binary string. */ static void -dbt_init(DBT *key, size_t *ksz) -{ - - if (0 == *ksz) { - assert(0 == key->size); - assert(NULL == key->data); - key->data = mandoc_malloc(MANDOC_BUFSZ); - *ksz = MANDOC_BUFSZ; - } - - key->size = 0; -} - -/* - * Append a binary value to a database entry. This can be invoked - * multiple times; the buffer is automatically resized. - */ -static void -dbt_appendb(DBT *key, size_t *ksz, const void *cp, size_t sz) -{ - - assert(key->data); - - /* Overshoot by MANDOC_BUFSZ. */ - - while (key->size + sz >= *ksz) { - *ksz = key->size + sz + MANDOC_BUFSZ; - key->data = mandoc_realloc(key->data, *ksz); - } - - memcpy(key->data + (int)key->size, cp, sz); - key->size += sz; -} - -static void buf_appendb(struct buf *buf, const void *cp, size_t sz) { @@ -528,38 +507,50 @@ buf_appendb(struct buf *buf, const void *cp, size_t sz } /* - * Append a nil-terminated string to the database entry. This can be - * invoked multiple times. The database entry will be nil-terminated as - * well; if invoked multiple times, a space is put between strings. + * Append a nil-terminated string to the buffer. + * This can be invoked multiple times. + * The buffer string will be nil-terminated. + * If invoked multiple times, a space is put between strings. */ static void -dbt_append(DBT *key, size_t *ksz, const char *cp) +buf_append(struct buf *buf, const char *cp) { size_t sz; if (0 == (sz = strlen(cp))) return; - assert(key->data); + if (buf->len) + buf->cp[(int)buf->len - 1] = ' '; - if (key->size) - ((char *)key->data)[(int)key->size - 1] = ' '; - - dbt_appendb(key, ksz, cp, sz + 1); + 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_append(struct buf *buf, const char *cp) +buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f) { - size_t sz; - if (0 == (sz = strlen(cp))) - return; + for ( ; n; n = n->next) { + if (n->child) + buf_appendmdoc(buf, n->child, f); - if (buf->len) - buf->cp[(int)buf->len - 1] = ' '; + 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); - buf_appendb(buf, cp, sz + 1); + } } /* ARGSUSED */ @@ -570,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); } @@ -631,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); } @@ -694,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; @@ -748,17 +752,26 @@ pmdoc_Fo(MDOC_ARGS) static void pmdoc_Nd(MDOC_ARGS) { - int first; + + if (MDOC_BODY != n->type) + return; + + 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; - for (first = 1, n = n->child; n; n = n->next) { - if (MDOC_TEXT != n->type) - continue; - if (first) - dbt_appendb(rval, rsz, n->string, strlen(n->string) + 1); - else - dbt_append(rval, rsz, n->string); - first = 0; - } + buf_appendmdoc(buf, n->child, 0); + hash_put(hash, buf, TYPE_PATH); } /* ARGSUSED */ @@ -767,9 +780,7 @@ 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) @@ -778,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); } @@ -791,10 +799,12 @@ hash_put(DB *db, const struct buf *buf, int mask) DBT key, val; int rc; - key.data = buf->cp; - if (0 == (key.size = buf->len)) + if (buf->len < 2) return; + key.data = buf->cp; + key.size = buf->len; + if ((rc = (*db->get)(db, &key, &val, 0)) < 0) { perror("hash"); exit((int)MANDOCLEVEL_SYSERR); @@ -804,9 +814,6 @@ hash_put(DB *db, const struct buf *buf, int mask) val.data = &mask; val.size = sizeof(int); - /*fprintf(stderr, "Hashing: [%s] (0x%x)\n", - (char *)key.data, mask);*/ - if ((rc = (*db->put)(db, &key, &val, 0)) < 0) { perror("hash"); exit((int)MANDOCLEVEL_SYSERR); @@ -817,12 +824,8 @@ static void dbt_put(DB *db, const char *dbn, DBT *key, DBT *val) { - if (0 == key->size) - return; - - assert(key->data); + assert(key->size); assert(val->size); - assert(val->data); if (0 == (*db->put)(db, key, val, 0)) return; @@ -857,14 +860,14 @@ pmdoc_node(MDOC_ARGS) break; buf->len = 0; - (*mdocs[n->tok])(hash, buf, rval, rsz, n, m); + (*mdocs[n->tok])(hash, buf, dbuf, n, m); break; default: break; } - pmdoc_node(hash, buf, rval, rsz, n->child, m); - pmdoc_node(hash, buf, rval, rsz, n->next, m); + pmdoc_node(hash, buf, dbuf, n->child, m); + pmdoc_node(hash, buf, dbuf, n->next, m); } static int @@ -951,13 +954,15 @@ pman_node(MAN_ARGS) while (' ' == *start) start++; - dbt_appendb(rval, rsz, start, strlen(start) + 1); + sz = strlen(start) + 1; + buf_appendb(dbuf, start, sz); + buf_appendb(buf, start, sz); } } - if (pman_node(hash, buf, rval, rsz, n->child)) + if (pman_node(hash, buf, dbuf, n->child)) return(1); - if (pman_node(hash, buf, rval, rsz, n->next)) + if (pman_node(hash, buf, dbuf, n->next)) return(1); return(0); @@ -967,8 +972,6 @@ static void usage(void) { - fprintf(stderr, "usage: %s " - "[-d path] " - "[file...]\n", + fprintf(stderr, "usage: %s [-v] [-d path] [file...]\n", progname); }