[BACK]Return to makewhatis.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/Attic/makewhatis.c between version 1.3 and 1.19

version 1.3, 2011/06/21 13:05:14 version 1.19, 2011/07/11 21:56:06
Line 21 
Line 21 
 #include <sys/param.h>  #include <sys/param.h>
   
 #include <assert.h>  #include <assert.h>
 #ifdef __linux__  
 # include <db_185.h>  
 #else  
 # include <db.h>  
 #endif  
 #include <fcntl.h>  #include <fcntl.h>
 #include <getopt.h>  #include <getopt.h>
 #include <stdio.h>  #include <stdio.h>
Line 33 
Line 28 
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   
   #ifdef __linux__
   # include <db_185.h>
   #else
   # include <db.h>
   #endif
   
 #include "man.h"  #include "man.h"
 #include "mdoc.h"  #include "mdoc.h"
 #include "mandoc.h"  #include "mandoc.h"
Line 42 
Line 43 
 #define MANDOC_BUFSZ      BUFSIZ  #define MANDOC_BUFSZ      BUFSIZ
 #define MANDOC_FLAGS      O_CREAT|O_TRUNC|O_RDWR  #define MANDOC_FLAGS      O_CREAT|O_TRUNC|O_RDWR
   
 #define TYPE_NAME       0x01  /* Bit-fields.  See makewhatis.1. */
 #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  
   
   #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
   #define TYPE_ENV          0x800
   #define TYPE_ERR          0x1000
   
   /* Buffer for storing growable data. */
   
 struct  buf {  struct  buf {
         char             *cp;          char             *cp;
         size_t            len;          size_t            len;
         size_t            size;          size_t            size;
 };  };
   
   /* Operation we're going to perform. */
   
   enum    op {
           OP_NEW = 0, /* new database */
           OP_UPDATE, /* update entries in existing database */
           OP_DELETE /* delete entries from existing database */
   };
   
 #define MAN_ARGS          DB *hash, \  #define MAN_ARGS          DB *hash, \
                           struct buf *buf, \                            struct buf *buf, \
                           DBT *rval, size_t *rsz, \                            struct buf *dbuf, \
                           const struct man_node *n                            const struct man_node *n
 #define MDOC_ARGS         DB *hash, \  #define MDOC_ARGS         DB *hash, \
                           struct buf *buf, \                            struct buf *buf, \
                           DBT *rval, size_t *rsz, \                            struct buf *dbuf, \
                           const struct mdoc_node *n, \                            const struct mdoc_node *n, \
                           const struct mdoc_meta *m                            const struct mdoc_meta *m
   
 static  void              dbt_append(DBT *, size_t *, const char *);  static  void              buf_appendmdoc(struct buf *,
 static  void              dbt_appendb(DBT *, size_t *,                                  const struct mdoc_node *, int);
   static  void              buf_append(struct buf *, const char *);
   static  void              buf_appendb(struct buf *,
                                 const void *, size_t);                                  const void *, size_t);
 static  void              dbt_init(DBT *, size_t *);  
 static  void              dbt_put(DB *, const char *, DBT *, DBT *);  static  void              dbt_put(DB *, const char *, DBT *, DBT *);
 static  void              hash_put(DB *, const struct buf *, int);  static  void              hash_put(DB *, const struct buf *, int);
 static  void              usage(void);  static  void              hash_reset(DB **);
   static  void              op_delete(const char *, int, DB *,
                                   const char *, DB *, const char *);
 static  int               pman_node(MAN_ARGS);  static  int               pman_node(MAN_ARGS);
 static  void              pmdoc_node(MDOC_ARGS);  static  void              pmdoc_node(MDOC_ARGS);
 static  void              pmdoc_An(MDOC_ARGS);  static  void              pmdoc_An(MDOC_ARGS);
 static  void              pmdoc_Cd(MDOC_ARGS);  static  void              pmdoc_Cd(MDOC_ARGS);
   static  void              pmdoc_Er(MDOC_ARGS);
   static  void              pmdoc_Ev(MDOC_ARGS);
 static  void              pmdoc_Fd(MDOC_ARGS);  static  void              pmdoc_Fd(MDOC_ARGS);
 static  void              pmdoc_In(MDOC_ARGS);  static  void              pmdoc_In(MDOC_ARGS);
 static  void              pmdoc_Fn(MDOC_ARGS);  static  void              pmdoc_Fn(MDOC_ARGS);
 static  void              pmdoc_Fo(MDOC_ARGS);  static  void              pmdoc_Fo(MDOC_ARGS);
 static  void              pmdoc_Nd(MDOC_ARGS);  static  void              pmdoc_Nd(MDOC_ARGS);
 static  void              pmdoc_Nm(MDOC_ARGS);  static  void              pmdoc_Nm(MDOC_ARGS);
   static  void              pmdoc_Pa(MDOC_ARGS);
 static  void              pmdoc_St(MDOC_ARGS);  static  void              pmdoc_St(MDOC_ARGS);
 static  void              pmdoc_Vt(MDOC_ARGS);  static  void              pmdoc_Vt(MDOC_ARGS);
   static  void              pmdoc_Xr(MDOC_ARGS);
   static  void              usage(void);
   
 typedef void            (*pmdoc_nf)(MDOC_ARGS);  typedef void            (*pmdoc_nf)(MDOC_ARGS);
   
 static  const char       *progname;  
   
 static  const pmdoc_nf    mdocs[MDOC_MAX] = {  static  const pmdoc_nf    mdocs[MDOC_MAX] = {
         NULL, /* Ap */          NULL, /* Ap */
         NULL, /* Dd */          NULL, /* Dd */
Line 113  static const pmdoc_nf   mdocs[MDOC_MAX] = {
Line 136  static const pmdoc_nf   mdocs[MDOC_MAX] = {
         pmdoc_Cd, /* Cd */          pmdoc_Cd, /* Cd */
         NULL, /* Cm */          NULL, /* Cm */
         NULL, /* Dv */          NULL, /* Dv */
         NULL, /* Er */          pmdoc_Er, /* Er */
         NULL, /* Ev */          pmdoc_Ev, /* Ev */
         NULL, /* Ex */          NULL, /* Ex */
         NULL, /* Fa */          NULL, /* Fa */
         pmdoc_Fd, /* Fd */          pmdoc_Fd, /* Fd */
Line 128  static const pmdoc_nf   mdocs[MDOC_MAX] = {
Line 151  static const pmdoc_nf   mdocs[MDOC_MAX] = {
         pmdoc_Nm, /* Nm */          pmdoc_Nm, /* Nm */
         NULL, /* Op */          NULL, /* Op */
         NULL, /* Ot */          NULL, /* Ot */
         NULL, /* Pa */          pmdoc_Pa, /* Pa */
         NULL, /* Rv */          NULL, /* Rv */
         pmdoc_St, /* St */          pmdoc_St, /* St */
         pmdoc_Vt, /* Va */          pmdoc_Vt, /* Va */
         pmdoc_Vt, /* Vt */          pmdoc_Vt, /* Vt */
         NULL, /* Xr */          pmdoc_Xr, /* Xr */
         NULL, /* %A */          NULL, /* %A */
         NULL, /* %B */          NULL, /* %B */
         NULL, /* %D */          NULL, /* %D */
Line 217  static const pmdoc_nf   mdocs[MDOC_MAX] = {
Line 240  static const pmdoc_nf   mdocs[MDOC_MAX] = {
         NULL, /* Ta */          NULL, /* Ta */
 };  };
   
   static  const char       *progname;
   
 int  int
 main(int argc, char *argv[])  main(int argc, char *argv[])
 {  {
         struct mparse   *mp; /* parse sequence */          struct mparse   *mp; /* parse sequence */
         struct mdoc     *mdoc; /* resulting mdoc */          struct mdoc     *mdoc; /* resulting mdoc */
         struct man      *man; /* resulting man */          struct man      *man; /* resulting man */
           enum op          op;
         char            *fn; /* current file being parsed */          char            *fn; /* current file being parsed */
         const char      *msec, /* manual section */          const char      *msec, /* manual section */
                         *mtitle, /* manual title */                          *mtitle, /* manual title */
                         *arch, /* manual architecture */                          *arch, /* manual architecture */
                         *dir; /* result dir (default: cwd) */                          *dir; /* result dir (default: cwd) */
         char             ibuf[MAXPATHLEN], /* index fname */          char             ibuf[MAXPATHLEN], /* index fname */
                          ibbuf[MAXPATHLEN], /* index backup fname */  
                          fbuf[MAXPATHLEN],  /* btree fname */                           fbuf[MAXPATHLEN],  /* btree fname */
                          fbbuf[MAXPATHLEN], /* btree backup fname */  
                          vbuf[8]; /* stringified record number */                           vbuf[8]; /* stringified record number */
         int              ch, seq;          int              ch, seq, verb, i;
         DB              *idx, /* index database */          DB              *idx, /* index database */
                         *db, /* keyword database */                          *db, /* keyword database */
                         *hash; /* temporary keyword hashtable */                          *hash; /* temporary keyword hashtable */
         DBT              rkey, rval, /* recno entries */          DBT              key, val;
                          key, val; /* persistent keyword entries */          enum mandocerr   ec;
         size_t           sv, rsz;          size_t           sv;
         BTREEINFO        info; /* btree configuration */          BTREEINFO        info; /* btree configuration */
         recno_t          rec; /* current record number */          recno_t          rec, /* current record number */
         struct buf       buf; /* keyword buffer */                           maxrec;
           recno_t         *recs;
           size_t           recsz;
           struct buf       buf, /* keyword buffer */
                            dbuf; /* description buffer */
         extern int       optind;          extern int       optind;
         extern char     *optarg;          extern char     *optarg;
   
Line 253  main(int argc, char *argv[])
Line 281  main(int argc, char *argv[])
                 ++progname;                  ++progname;
   
         dir = "";          dir = "";
           verb = 0;
           db = idx = NULL;
           mp = NULL;
           hash = NULL;
           recs = NULL;
           recsz = 0;
           op = OP_NEW;
           ec = MANDOCLEVEL_SYSERR;
   
         while (-1 != (ch = getopt(argc, argv, "d:")))          memset(&buf, 0, sizeof(struct buf));
           memset(&dbuf, 0, sizeof(struct buf));
   
           while (-1 != (ch = getopt(argc, argv, "d:ruv")))
                 switch (ch) {                  switch (ch) {
                 case ('d'):                  case ('d'):
                         dir = optarg;                          dir = optarg;
                         break;                          break;
                   case ('r'):
                           op = OP_DELETE;
                           break;
                   case ('u'):
                           op = OP_UPDATE;
                           break;
                   case ('v'):
                           verb++;
                           break;
                 default:                  default:
                         usage();                          usage();
                         return((int)MANDOCLEVEL_BADARG);                          return((int)MANDOCLEVEL_BADARG);
Line 267  main(int argc, char *argv[])
Line 315  main(int argc, char *argv[])
         argc -= optind;          argc -= optind;
         argv += optind;          argv += optind;
   
         /*  
          * Set up temporary file-names into which we're going to write  
          * all of our data (both for the index and database).  These  
          * will be securely renamed to the real file-names after we've  
          * written all of our data.  
          */  
   
         ibuf[0] = ibuf[MAXPATHLEN - 2] =          ibuf[0] = ibuf[MAXPATHLEN - 2] =
                 ibbuf[0] = ibbuf[MAXPATHLEN - 2] =                  fbuf[0] = fbuf[MAXPATHLEN - 2] = '\0';
                 fbuf[0] = fbuf[MAXPATHLEN - 2] =  
                 fbbuf[0] = fbbuf[MAXPATHLEN - 2] = '\0';  
   
         strlcat(fbuf, dir, MAXPATHLEN);          strlcat(fbuf, dir, MAXPATHLEN);
         strlcat(fbuf, MANDOC_DB, MAXPATHLEN);          strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
   
         strlcat(fbbuf, fbuf, MAXPATHLEN);  
         strlcat(fbbuf, "~", MAXPATHLEN);  
   
         strlcat(ibuf, dir, MAXPATHLEN);          strlcat(ibuf, dir, MAXPATHLEN);
         strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);          strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
   
         strlcat(ibbuf, ibuf, MAXPATHLEN);  
         strlcat(ibbuf, "~", MAXPATHLEN);  
   
         if ('\0' != fbuf[MAXPATHLEN - 2] ||          if ('\0' != fbuf[MAXPATHLEN - 2] ||
                         '\0' != fbbuf[MAXPATHLEN - 2] ||                          '\0' != ibuf[MAXPATHLEN - 2]) {
                         '\0' != ibuf[MAXPATHLEN - 2] ||                  fprintf(stderr, "%s: Path too long\n", dir);
                         '\0' != ibbuf[MAXPATHLEN - 2]) {                  goto out;
                 fprintf(stderr, "%s: Path too long\n", progname);  
                 exit((int)MANDOCLEVEL_SYSERR);  
         }          }
   
         /*          /*
          * For the keyword database, open a BTREE database that allows           * For the keyword database, open a BTREE database that allows
          * duplicates.           * duplicates.
          * For the index database, use a standard RECNO database type.           * For the index database, use a standard RECNO database type.
          * For the temporary keyword hashtable, use the HASH database           * Truncate the database if we're creating a new one.
          * type.  
          */           */
   
         hash = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL);  
         if (NULL == hash) {  
                 perror("hash");  
                 exit((int)MANDOCLEVEL_SYSERR);  
         }  
   
         memset(&info, 0, sizeof(BTREEINFO));          memset(&info, 0, sizeof(BTREEINFO));
         info.flags = R_DUP;          info.flags = R_DUP;
         db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);  
   
         if (NULL == db) {          if (OP_NEW == op) {
                 perror(fbbuf);                  db = dbopen(fbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
                 (*hash->close)(hash);                  idx = dbopen(ibuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
                 exit((int)MANDOCLEVEL_SYSERR);          } else {
                   db = dbopen(fbuf, O_CREAT|O_RDWR, 0644, DB_BTREE, &info);
                   idx = dbopen(ibuf, O_CREAT|O_RDWR, 0644, DB_RECNO, NULL);
         }          }
   
         idx = dbopen(ibbuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);  
   
         if (NULL == db) {          if (NULL == db) {
                 perror(ibbuf);                  perror(fbuf);
                 (*db->close)(db);                  goto out;
                 (*hash->close)(hash);          } else if (NULL == db) {
                 exit((int)MANDOCLEVEL_SYSERR);                  perror(ibuf);
                   goto out;
         }          }
   
         /*          /*
          * Try parsing the manuals given on the command line.  If we           * If we're going to delete or update a database, remove the
          * totally fail, then just keep on going.  Take resulting trees           * entries now.  This doesn't actually remove them; it only sets
          * and push them down into the database code.           * their record value lengths to zero.
          * Use the auto-parser and don't report any errors.  
          */           */
   
         mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);          if (OP_DELETE == op || OP_UPDATE == op)
                   for (i = 0; i < argc; i++)
                           op_delete(argv[i], verb, idx, ibuf, db, fbuf);
   
         memset(&key, 0, sizeof(DBT));          if (OP_DELETE == op) {
         memset(&val, 0, sizeof(DBT));                  ec = MANDOCLEVEL_OK;
         memset(&rkey, 0, sizeof(DBT));                  goto out;
         memset(&rval, 0, sizeof(DBT));          }
   
         rkey.size = sizeof(recno_t);          /*
            * Compile a list of all available "empty" records to use.  This
            * keeps the size of the database small.
            */
   
         rec = 1;          if (OP_UPDATE == op) {
         rsz = 0;                  i = 0;
                   seq = R_FIRST;
                   while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                           seq = R_NEXT;
                           maxrec = *(recno_t *)key.data;
                           if (val.size > 0)
                                   continue;
                           if ((size_t)i >= recsz) {
                                   recsz += 1024;
                                   recs = mandoc_realloc
                                           (recs, recsz * sizeof(recno_t));
                           }
                           recs[i++] = maxrec;
                   }
                   if (ch < 0) {
                           perror(ibuf);
                           exit((int)MANDOCLEVEL_SYSERR);
                   }
                   recsz = (size_t)i;
                   maxrec++;
                   assert(recsz < maxrec);
           } else
                   maxrec = 0;
   
         memset(&buf, 0, sizeof(struct buf));          /*
            * Add records to the database.
            * 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.
            */
   
         buf.size = MANDOC_BUFSZ;          mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
   
           buf.size = dbuf.size = MANDOC_BUFSZ;
         buf.cp = mandoc_malloc(buf.size);          buf.cp = mandoc_malloc(buf.size);
           dbuf.cp = mandoc_malloc(dbuf.size);
   
         while (NULL != (fn = *argv++)) {          for (rec = 0, i = 0; i < argc; i++) {
                   fn = argv[i];
                   if (OP_UPDATE == op) {
                           if (recsz > 0) {
                                   --recsz;
                                   rec = recs[(int)recsz];
                           } else if (maxrec > 0) {
                                   rec = maxrec;
                                   maxrec = 0;
                           } else
                                   rec++;
                   } else
                           rec++;
   
                 mparse_reset(mp);                  mparse_reset(mp);
                   hash_reset(&hash);
   
                 /* Parse and get (non-empty) AST. */  
   
                 if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {                  if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
                         fprintf(stderr, "%s: Parse failure\n", fn);                          fprintf(stderr, "%s: Parse failure\n", fn);
                         continue;                          continue;
                 }                  }
   
                 mparse_result(mp, &mdoc, &man);                  mparse_result(mp, &mdoc, &man);
                 if (NULL == mdoc && NULL == man)                  if (NULL == mdoc && NULL == man)
                         continue;                          continue;
   
                 /* Manual section: can be empty string. */  
   
                 msec = NULL != mdoc ?                  msec = NULL != mdoc ?
                         mdoc_meta(mdoc)->msec :                          mdoc_meta(mdoc)->msec : man_meta(man)->msec;
                         man_meta(man)->msec;  
                 mtitle = NULL != mdoc ?                  mtitle = NULL != mdoc ?
                         mdoc_meta(mdoc)->title :                          mdoc_meta(mdoc)->title : man_meta(man)->title;
                         man_meta(man)->title;  
                 arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL;                  arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL;
   
                 assert(msec);                  if (NULL == arch)
                 assert(mtitle);                          arch = "";
   
                 /*                  /*
                  * The index record value consists of a nil-terminated                   * The index record value consists of a nil-terminated
Line 390  main(int argc, char *argv[])
Line 458  main(int argc, char *argv[])
                  * going to write a nil byte in its place.                   * going to write a nil byte in its place.
                  */                   */
   
                 dbt_init(&rval, &rsz);                  dbuf.len = 0;
                 dbt_appendb(&rval, &rsz, fn, strlen(fn) + 1);                  buf_appendb(&dbuf, fn, strlen(fn) + 1);
                 dbt_appendb(&rval, &rsz, msec, strlen(msec) + 1);                  buf_appendb(&dbuf, msec, strlen(msec) + 1);
                 dbt_appendb(&rval, &rsz, mtitle, strlen(mtitle) + 1);                  buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1);
                 dbt_appendb(&rval, &rsz, arch ? arch : "",                  buf_appendb(&dbuf, arch, strlen(arch) + 1);
                                 arch ? strlen(arch) + 1 : 1);  
   
                 sv = rval.size;                  sv = dbuf.len;
   
                 /* Fix the record number in the btree value. */                  /* Fix the record number in the btree value. */
   
                 if (mdoc)                  if (mdoc)
                         pmdoc_node(hash, &buf, &rval,                          pmdoc_node(hash, &buf, &dbuf,
                                         &rsz, mdoc_node(mdoc),                                  mdoc_node(mdoc), mdoc_meta(mdoc));
                                         mdoc_meta(mdoc));  
                 else                  else
                         pman_node(hash, &buf, &rval,                          pman_node(hash, &buf, &dbuf, man_node(man));
                                         &rsz, man_node(man));  
   
                 /*                  /*
                  * Copy from the in-memory hashtable of pending keywords                   * Copy from the in-memory hashtable of pending keywords
Line 419  main(int argc, char *argv[])
Line 484  main(int argc, char *argv[])
   
                 seq = R_FIRST;                  seq = R_FIRST;
                 while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {                  while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
                           seq = R_NEXT;
   
                         memcpy(vbuf, val.data, sizeof(uint32_t));                          memcpy(vbuf, val.data, sizeof(uint32_t));
                         val.size = sizeof(vbuf);                          val.size = sizeof(vbuf);
                         val.data = vbuf;                          val.data = vbuf;
                         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 (verb > 1)
                                   printf("Indexed: %s, %s, 0x%x\n",
                                           fn, (char *)key.data,
                                           *(int *)val.data);
                           dbt_put(db, fbuf, &key, &val);
                   }
                 if (ch < 0) {                  if (ch < 0) {
                         perror("hash");                          perror("hash");
                         exit((int)MANDOCLEVEL_SYSERR);                          exit((int)MANDOCLEVEL_SYSERR);
Line 443  main(int argc, char *argv[])
Line 506  main(int argc, char *argv[])
                  * set, put an empty one in now.                   * set, put an empty one in now.
                  */                   */
   
                 if (rval.size == sv)                  if (dbuf.len == sv)
                         dbt_appendb(&rval, &rsz, "", 1);                          buf_appendb(&dbuf, "", 1);
   
                 rkey.data = &rec;                  key.data = &rec;
                 dbt_put(idx, ibbuf, &rkey, &rval);                  key.size = sizeof(recno_t);
   
                 printf("Indexed: %s\n", fn);                  val.data = dbuf.cp;
                 rec++;                  val.size = dbuf.len;
         }  
   
         (*db->close)(db);                  if (verb > 0)
         (*idx->close)(idx);                          printf("Indexed: %s\n", fn);
         (*hash->close)(hash);  
   
         mparse_free(mp);                  dbt_put(idx, ibuf, &key, &val);
           }
   
         free(rval.data);          ec = MANDOCLEVEL_OK;
   out:
           if (db)
                   (*db->close)(db);
           if (idx)
                   (*idx->close)(idx);
           if (hash)
                   (*hash->close)(hash);
           if (mp)
                   mparse_free(mp);
   
         free(buf.cp);          free(buf.cp);
           free(dbuf.cp);
           free(recs);
   
         /* Atomically replace the file with our temporary one. */          return((int)ec);
   
         if (-1 == rename(fbbuf, fbuf))  
                 perror(fbuf);  
         if (-1 == rename(ibbuf, ibuf))  
                 perror(fbuf);  
   
         return((int)MANDOCLEVEL_OK);  
 }  }
   
 /*  
  * Initialise the stored database key whose data buffer is shared  
  * between uses (as the key must sometimes be constructed from an array  
  * of  
  */  
 static void  static void
 dbt_init(DBT *key, size_t *ksz)  op_delete(const char *fn, int verb, DB *idx,
                   const char *ibuf, DB *db, const char *fbuf)
 {  {
           int              ch;
           DBT              key, val;
           recno_t          rec;
           unsigned int     seq, sseq;
   
         if (0 == *ksz) {          seq = R_FIRST;
                 assert(0 == key->size);          while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
                 assert(NULL == key->data);                  seq = R_NEXT;
                 key->data = mandoc_malloc(MANDOC_BUFSZ);                  if (0 == val.size)
                 *ksz = MANDOC_BUFSZ;                          continue;
         }                  if (strcmp((char *)val.data, fn))
                           continue;
   
         key->size = 0;                  rec = *(recno_t *)key.data;
 }  
   
 /*                  sseq = R_FIRST;
  * Append a binary value to a database entry.  This can be invoked                  while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
  * multiple times; the buffer is automatically resized.                          sseq = R_NEXT;
  */                          assert(8 == val.size);
 static void                          if (rec != *(recno_t *)(val.data + 4))
 dbt_appendb(DBT *key, size_t *ksz, const void *cp, size_t sz)                                  continue;
 {                          if (verb > 1)
                                   printf("Deleted: %s, %s\n",
                                           fn, (char *)key.data);
                           ch = (*db->del)(db, &key, R_CURSOR);
                           if (ch < 0)
                                   break;
                   }
                   if (ch < 0) {
                           perror(fbuf);
                           exit((int)MANDOCLEVEL_SYSERR);
                   }
   
         assert(key->data);                  val.size = 0;
                   if (verb)
         /* Overshoot by MANDOC_BUFSZ. */                          printf("Deleted: %s\n", fn);
                   ch = (*idx->put)
         while (key->size + sz >= *ksz) {                          (idx, &key, &val, R_CURSOR);
                 *ksz = key->size + sz + MANDOC_BUFSZ;                  if (ch < 0)
                 key->data = mandoc_realloc(key->data, *ksz);                          break;
         }          }
           if (ch < 0) {
         memcpy(key->data + (int)key->size, cp, sz);                  perror(ibuf);
         key->size += sz;                  exit((int)MANDOCLEVEL_SYSERR);
           }
 }  }
   
   /*
    * Grow the buffer (if necessary) and copy in a binary string.
    */
 static void  static void
 buf_appendb(struct buf *buf, const void *cp, size_t sz)  buf_appendb(struct buf *buf, const void *cp, size_t sz)
 {  {
Line 528  buf_appendb(struct buf *buf, const void *cp, size_t sz
Line 609  buf_appendb(struct buf *buf, const void *cp, size_t sz
 }  }
   
 /*  /*
  * Append a nil-terminated string to the database entry.  This can be   * Append a nil-terminated string to the buffer.
  * invoked multiple times.  The database entry will be nil-terminated as   * This can be invoked multiple times.
  * well; if invoked multiple times, a space is put between strings.   * The buffer string will be nil-terminated.
    * If invoked multiple times, a space is put between strings.
  */   */
 static void  static void
 dbt_append(DBT *key, size_t *ksz, const char *cp)  buf_append(struct buf *buf, const char *cp)
 {  {
         size_t           sz;          size_t           sz;
   
         if (0 == (sz = strlen(cp)))          if (0 == (sz = strlen(cp)))
                 return;                  return;
   
         assert(key->data);          if (buf->len)
                   buf->cp[(int)buf->len - 1] = ' ';
   
         if (key->size)          buf_appendb(buf, cp, sz + 1);
                 ((char *)key->data)[(int)key->size - 1] = ' ';  
   
         dbt_appendb(key, ksz, 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  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)))          for ( ; n; n = n->next) {
                 return;                  if (n->child)
                           buf_appendmdoc(buf, n->child, f);
   
         if (buf->len)                  if (MDOC_TEXT == n->type && f) {
                 buf->cp[(int)buf->len - 1] = ' ';                          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 */  /* ARGSUSED */
Line 570  pmdoc_An(MDOC_ARGS)
Line 663  pmdoc_An(MDOC_ARGS)
         if (SEC_AUTHORS != n->sec)          if (SEC_AUTHORS != n->sec)
                 return;                  return;
   
         for (n = n->child; n; n = n->next)          buf_appendmdoc(buf, n->child, 0);
                 if (MDOC_TEXT == n->type)  
                         buf_append(buf, n->string);  
   
         hash_put(hash, buf, TYPE_AUTHOR);          hash_put(hash, buf, TYPE_AUTHOR);
 }  }
   
   static void
   hash_reset(DB **db)
   {
           DB              *hash;
   
           if (NULL != (hash = *db))
                   (*hash->close)(hash);
   
           *db = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL);
           if (NULL == *db) {
                   perror("hash");
                   exit((int)MANDOCLEVEL_SYSERR);
           }
   }
   
 /* ARGSUSED */  /* ARGSUSED */
 static void  static void
 pmdoc_Fd(MDOC_ARGS)  pmdoc_Fd(MDOC_ARGS)
Line 631  pmdoc_Cd(MDOC_ARGS)
Line 736  pmdoc_Cd(MDOC_ARGS)
         if (SEC_SYNOPSIS != n->sec)          if (SEC_SYNOPSIS != n->sec)
                 return;                  return;
   
         for (n = n->child; n; n = n->next)          buf_appendmdoc(buf, n->child, 0);
                 if (MDOC_TEXT == n->type)  
                         buf_append(buf, n->string);  
   
         hash_put(hash, buf, TYPE_CONFIG);          hash_put(hash, buf, TYPE_CONFIG);
 }  }
   
Line 694  pmdoc_St(MDOC_ARGS)
Line 796  pmdoc_St(MDOC_ARGS)
   
 /* ARGSUSED */  /* ARGSUSED */
 static void  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)  pmdoc_Vt(MDOC_ARGS)
 {  {
         const char      *start;          const char      *start;
Line 748  pmdoc_Fo(MDOC_ARGS)
Line 869  pmdoc_Fo(MDOC_ARGS)
 static void  static void
 pmdoc_Nd(MDOC_ARGS)  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_Er(MDOC_ARGS)
   {
   
           if (SEC_ERRORS != n->sec)
                   return;
   
         for (first = 1, n = n->child; n; n = n->next) {          buf_appendmdoc(buf, n->child, 0);
                 if (MDOC_TEXT != n->type)          hash_put(hash, buf, TYPE_ERR);
                         continue;  
                 if (first)  
                         dbt_appendb(rval, rsz, n->string, strlen(n->string) + 1);  
                 else  
                         dbt_append(rval, rsz, n->string);  
                 first = 0;  
         }  
 }  }
   
 /* ARGSUSED */  /* ARGSUSED */
 static void  static void
   pmdoc_Ev(MDOC_ARGS)
   {
   
           if (SEC_ENVIRONMENT != n->sec)
                   return;
   
           buf_appendmdoc(buf, n->child, 0);
           hash_put(hash, buf, TYPE_ENV);
   }
   
   /* 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)  pmdoc_Nm(MDOC_ARGS)
 {  {
   
         if (SEC_NAME == n->sec) {          if (SEC_NAME == n->sec) {
                 for (n = n->child; n; n = n->next)                  buf_appendmdoc(buf, n->child, 0);
                         if (MDOC_TEXT == n->type)  
                                 buf_append(buf, n->string);  
                 hash_put(hash, buf, TYPE_NAME);                  hash_put(hash, buf, TYPE_NAME);
                 return;                  return;
         } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)          } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
Line 778  pmdoc_Nm(MDOC_ARGS)
Line 930  pmdoc_Nm(MDOC_ARGS)
         if (NULL == n->child)          if (NULL == n->child)
                 buf_append(buf, m->name);                  buf_append(buf, m->name);
   
         for (n = n->child; n; n = n->next)          buf_appendmdoc(buf, n->child, 0);
                 if (MDOC_TEXT == n->type)  
                         buf_append(buf, n->string);  
   
         hash_put(hash, buf, TYPE_UTILITY);          hash_put(hash, buf, TYPE_UTILITY);
 }  }
   
Line 791  hash_put(DB *db, const struct buf *buf, int mask)
Line 940  hash_put(DB *db, const struct buf *buf, int mask)
         DBT              key, val;          DBT              key, val;
         int              rc;          int              rc;
   
         key.data = buf->cp;          if (buf->len < 2)
         if (0 == (key.size = buf->len))  
                 return;                  return;
   
           key.data = buf->cp;
           key.size = buf->len;
   
         if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {          if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
                 perror("hash");                  perror("hash");
                 exit((int)MANDOCLEVEL_SYSERR);                  exit((int)MANDOCLEVEL_SYSERR);
Line 804  hash_put(DB *db, const struct buf *buf, int mask)
Line 955  hash_put(DB *db, const struct buf *buf, int mask)
         val.data = &mask;          val.data = &mask;
         val.size = sizeof(int);          val.size = sizeof(int);
   
         /*fprintf(stderr, "Hashing: [%s] (0x%x)\n",  
                         (char *)key.data, mask);*/  
   
         if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {          if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
                 perror("hash");                  perror("hash");
                 exit((int)MANDOCLEVEL_SYSERR);                  exit((int)MANDOCLEVEL_SYSERR);
Line 817  static void
Line 965  static void
 dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)  dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
 {  {
   
         if (0 == key->size)          assert(key->size);
                 return;  
   
         assert(key->data);  
         assert(val->size);          assert(val->size);
         assert(val->data);  
   
         if (0 == (*db->put)(db, key, val, 0))          if (0 == (*db->put)(db, key, val, 0))
                 return;                  return;
Line 857  pmdoc_node(MDOC_ARGS)
Line 1001  pmdoc_node(MDOC_ARGS)
                         break;                          break;
   
                 buf->len = 0;                  buf->len = 0;
                 (*mdocs[n->tok])(hash, buf, rval, rsz, n, m);                  (*mdocs[n->tok])(hash, buf, dbuf, n, m);
                 break;                  break;
         default:          default:
                 break;                  break;
         }          }
   
         pmdoc_node(hash, buf, rval, rsz, n->child, m);          pmdoc_node(hash, buf, dbuf, n->child, m);
         pmdoc_node(hash, buf, rval, rsz, n->next, m);          pmdoc_node(hash, buf, dbuf, n->next, m);
 }  }
   
 static int  static int
Line 930  pman_node(MAN_ARGS)
Line 1074  pman_node(MAN_ARGS)
                                         start++;                                          start++;
                         }                          }
   
                           buf->len = 0;
   
                         if (sv == start) {                          if (sv == start) {
                                 buf->len = 0;  
                                 buf_append(buf, start);                                  buf_append(buf, start);
                                 return(1);                                  return(1);
                         }                          }
Line 951  pman_node(MAN_ARGS)
Line 1096  pman_node(MAN_ARGS)
                         while (' ' == *start)                          while (' ' == *start)
                                 start++;                                  start++;
   
                         dbt_appendb(rval, rsz, start, strlen(start) + 1);                          sz = strlen(start) + 1;
                           buf_appendb(dbuf, start, sz);
                           buf_appendb(buf, start, sz);
   
                           hash_put(hash, buf, TYPE_DESC);
                 }                  }
         }          }
   
         if (pman_node(hash, buf, rval, rsz, n->child))          if (pman_node(hash, buf, dbuf, n->child))
                 return(1);                  return(1);
         if (pman_node(hash, buf, rval, rsz, n->next))          if (pman_node(hash, buf, dbuf, n->next))
                 return(1);                  return(1);
   
         return(0);          return(0);
Line 967  static void
Line 1116  static void
 usage(void)  usage(void)
 {  {
   
         fprintf(stderr, "usage: %s "          fprintf(stderr, "usage: %s [-ruv] [-d path] [file...]\n",
                         "[-d path] "  
                         "[file...]\n",  
                         progname);                          progname);
 }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.19

CVSweb