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

Diff for /mandoc/Attic/makewhatis.c between version 1.6 and 1.10

version 1.6, 2011/06/21 14:16:05 version 1.10, 2011/06/22 10:36:36
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 44 
Line 45 
   
 /* Bit-fields.  See makewhatis.1. */  /* Bit-fields.  See makewhatis.1. */
   
 #define TYPE_NAME       0x01  #define TYPE_NAME         0x01
 #define TYPE_FUNCTION   0x02  #define TYPE_FUNCTION     0x02
 #define TYPE_UTILITY    0x04  #define TYPE_UTILITY      0x04
 #define TYPE_INCLUDES   0x08  #define TYPE_INCLUDES     0x08
 #define TYPE_VARIABLE   0x10  #define TYPE_VARIABLE     0x10
 #define TYPE_STANDARD   0x20  #define TYPE_STANDARD     0x20
 #define TYPE_AUTHOR     0x40  #define TYPE_AUTHOR       0x40
 #define TYPE_CONFIG     0x80  #define TYPE_CONFIG       0x80
 #define TYPE_DESC       0x100  #define TYPE_DESC         0x100
   
 /* Buffer for storing growable data. */  /* Buffer for storing growable data. */
   
Line 236  main(int argc, char *argv[])
Line 237  main(int argc, char *argv[])
                          fbuf[MAXPATHLEN],  /* btree fname */                           fbuf[MAXPATHLEN],  /* btree fname */
                          fbbuf[MAXPATHLEN], /* btree backup fname */                           fbbuf[MAXPATHLEN], /* btree backup fname */
                          vbuf[8]; /* stringified record number */                           vbuf[8]; /* stringified record number */
         int              ch, seq;          int              ch, seq, verb;
         DB              *idx, /* index database */          DB              *idx, /* index database */
                         *db, /* keyword database */                          *db, /* keyword database */
                         *hash; /* temporary keyword hashtable */                          *hash; /* temporary keyword hashtable */
         DBT              key, val;          DBT              key, val;
         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 */          struct buf       buf, /* keyword buffer */
Line 256  main(int argc, char *argv[])
Line 257  main(int argc, char *argv[])
                 ++progname;                  ++progname;
   
         dir = "";          dir = "";
           verb = 0;
   
         while (-1 != (ch = getopt(argc, argv, "d:")))          while (-1 != (ch = getopt(argc, argv, "d:v")))
                 switch (ch) {                  switch (ch) {
                 case ('d'):                  case ('d'):
                         dir = optarg;                          dir = optarg;
                         break;                          break;
                   case ('v'):
                           verb++;
                           break;
                 default:                  default:
                         usage();                          usage();
                         return((int)MANDOCLEVEL_BADARG);                          return((int)MANDOCLEVEL_BADARG);
Line 298  main(int argc, char *argv[])
Line 303  main(int argc, char *argv[])
                         '\0' != fbbuf[MAXPATHLEN - 2] ||                          '\0' != fbbuf[MAXPATHLEN - 2] ||
                         '\0' != ibuf[MAXPATHLEN - 2] ||                          '\0' != ibuf[MAXPATHLEN - 2] ||
                         '\0' != ibbuf[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);                  exit((int)MANDOCLEVEL_SYSERR);
         }          }
   
Line 306  main(int argc, char *argv[])
Line 311  main(int argc, char *argv[])
          * 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  
          * 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);          db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
   
         if (NULL == db) {          if (NULL == db) {
                 perror(fbbuf);                  perror(fbbuf);
                 (*hash->close)(hash);  
                 exit((int)MANDOCLEVEL_SYSERR);                  exit((int)MANDOCLEVEL_SYSERR);
         }          }
   
Line 331  main(int argc, char *argv[])
Line 327  main(int argc, char *argv[])
         if (NULL == db) {          if (NULL == db) {
                 perror(ibbuf);                  perror(ibbuf);
                 (*db->close)(db);                  (*db->close)(db);
                 (*hash->close)(hash);  
                 exit((int)MANDOCLEVEL_SYSERR);                  exit((int)MANDOCLEVEL_SYSERR);
         }          }
   
         /*          /*
          * Try parsing the manuals given on the command line.  If we           * Try parsing each manual given on the command line.
          * totally fail, then just keep on going.  Take resulting trees           * If we fail, then emit an error and keep on going.
          * and push them down into the database code.           * Take resulting trees and push them down into the database code.
          * Use the auto-parser and don't report any errors.           * Use the auto-parser and don't report any errors.
          */           */
   
         mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);          mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
   
         rec = 1;          rec = 1;
         rsz = 0;          hash = NULL;
   
         memset(&buf, 0, sizeof(struct buf));          memset(&buf, 0, sizeof(struct buf));
         memset(&dbuf, 0, sizeof(struct buf));          memset(&dbuf, 0, sizeof(struct buf));
Line 358  main(int argc, char *argv[])
Line 353  main(int argc, char *argv[])
         while (NULL != (fn = *argv++)) {          while (NULL != (fn = *argv++)) {
                 mparse_reset(mp);                  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. */                  /* 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;
Line 378  main(int argc, char *argv[])
Line 385  main(int argc, char *argv[])
                         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);  
                 assert(mtitle);  
   
                 /*                  /*
                  * The index record value consists of a nil-terminated                   * The index record value consists of a nil-terminated
                  * filename, a nil-terminated manual section, and a                   * filename, a nil-terminated manual section, and a
Line 416  main(int argc, char *argv[])
Line 420  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;
   
                           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);                          dbt_put(db, fbbuf, &key, &val);
   
                         if ((*hash->del)(hash, &key, 0) < 0) {  
                                 perror("hash");  
                                 exit((int)MANDOCLEVEL_SYSERR);  
                         }  
                         seq = R_NEXT;  
                 }                  }
   
                 if (ch < 0) {                  if (ch < 0) {
Line 447  main(int argc, char *argv[])
Line 454  main(int argc, char *argv[])
                 val.data = dbuf.cp;                  val.data = dbuf.cp;
                 val.size = dbuf.len;                  val.size = dbuf.len;
   
                   if (verb > 0)
                           printf("%s: Indexed\n", fn);
   
                 dbt_put(idx, ibbuf, &key, &val);                  dbt_put(idx, ibbuf, &key, &val);
                 rec++;                  rec++;
         }          }
   
         (*db->close)(db);          (*db->close)(db);
         (*idx->close)(idx);          (*idx->close)(idx);
         (*hash->close)(hash);  
   
           if (hash)
                   (*hash->close)(hash);
   
         mparse_free(mp);          mparse_free(mp);
   
         free(buf.cp);          free(buf.cp);
Line 746  hash_put(DB *db, const struct buf *buf, int mask)
Line 758  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 ((key.size = buf->len) < 2)  
                 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 760  hash_put(DB *db, const struct buf *buf, int mask)
Line 773  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 773  static void
Line 783  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)  
                 return;  
   
         assert(key->size);          assert(key->size);
         assert(val->size);          assert(val->size);
   
Line 924  static void
Line 931  static void
 usage(void)  usage(void)
 {  {
   
         fprintf(stderr, "usage: %s [-d path] [file...]\n", progname);          fprintf(stderr, "usage: %s [-v] [-d path] [file...]\n",
                           progname);
 }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.10

CVSweb