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

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

version 1.10, 2011/06/22 10:36:36 version 1.18, 2011/07/11 09:36:15
Line 54 
Line 54 
 #define TYPE_AUTHOR       0x40  #define TYPE_AUTHOR       0x40
 #define TYPE_CONFIG       0x80  #define TYPE_CONFIG       0x80
 #define TYPE_DESC         0x100  #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. */  /* Buffer for storing growable data. */
   
Line 73  struct buf {
Line 77  struct buf {
                           const struct mdoc_node *n, \                            const struct mdoc_node *n, \
                           const struct mdoc_meta *m                            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_append(struct buf *, const char *);
 static  void              buf_appendb(struct buf *,  static  void              buf_appendb(struct buf *,
                                 const void *, size_t);                                  const void *, 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              hash_reset(DB **);
 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);  static  void              usage(void);
   
 typedef void            (*pmdoc_nf)(MDOC_ARGS);  typedef void            (*pmdoc_nf)(MDOC_ARGS);
Line 115  static const pmdoc_nf   mdocs[MDOC_MAX] = {
Line 126  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 130  static const pmdoc_nf   mdocs[MDOC_MAX] = {
Line 141  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 233  main(int argc, char *argv[])
Line 244  main(int argc, char *argv[])
                         *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, verb;          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;
           enum mandocerr   ec;
         size_t           sv;          size_t           sv;
         BTREEINFO        info; /* btree configuration */          BTREEINFO        info; /* btree configuration */
         recno_t          rec; /* current record number */          recno_t          rec; /* current record number */
Line 258  main(int argc, char *argv[])
Line 268  main(int argc, char *argv[])
   
         dir = "";          dir = "";
         verb = 0;          verb = 0;
           db = idx = NULL;
           mp = NULL;
           hash = NULL;
           ec = MANDOCLEVEL_SYSERR;
   
           memset(&buf, 0, sizeof(struct buf));
           memset(&dbuf, 0, sizeof(struct buf));
   
         while (-1 != (ch = getopt(argc, argv, "d:v")))          while (-1 != (ch = getopt(argc, argv, "d:v")))
                 switch (ch) {                  switch (ch) {
                 case ('d'):                  case ('d'):
Line 275  main(int argc, char *argv[])
Line 292  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] ||  
                         '\0' != ibbuf[MAXPATHLEN - 2]) {  
                 fprintf(stderr, "%s: Path too long\n", dir);                  fprintf(stderr, "%s: Path too long\n", dir);
                 exit((int)MANDOCLEVEL_SYSERR);                  goto out;
         }          }
   
         /*          /*
Line 315  main(int argc, char *argv[])
Line 315  main(int argc, char *argv[])
   
         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) {          db = dbopen(fbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
                 perror(fbbuf);          idx = dbopen(ibuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
                 exit((int)MANDOCLEVEL_SYSERR);  
         }  
   
         idx = dbopen(ibbuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);  
   
         if (NULL == db) {          if (NULL == db) {
                 perror(ibbuf);                  perror(fbuf);
                 (*db->close)(db);                  goto out;
                 exit((int)MANDOCLEVEL_SYSERR);          } else if (NULL == db) {
                   perror(ibuf);
                   goto out;
         }          }
   
         /*          /*
Line 339  main(int argc, char *argv[])
Line 336  main(int argc, char *argv[])
   
         mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);          mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
   
         rec = 1;  
         hash = NULL;  
   
         memset(&buf, 0, sizeof(struct buf));  
         memset(&dbuf, 0, sizeof(struct buf));  
   
         buf.size = dbuf.size = MANDOC_BUFSZ;          buf.size = dbuf.size = MANDOC_BUFSZ;
   
         buf.cp = mandoc_malloc(buf.size);          buf.cp = mandoc_malloc(buf.size);
         dbuf.cp = mandoc_malloc(dbuf.size);          dbuf.cp = mandoc_malloc(dbuf.size);
   
           rec = 1;
   
         while (NULL != (fn = *argv++)) {          while (NULL != (fn = *argv++)) {
                 mparse_reset(mp);                  mparse_reset(mp);
                   hash_reset(&hash);
   
                 /* 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) {                  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;
   
                 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 ?
                 arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL;                          mdoc_meta(mdoc)->arch : "";
   
                 /*                  /*
                  * The index record value consists of a nil-terminated                   * The index record value consists of a nil-terminated
Line 397  main(int argc, char *argv[])
Line 374  main(int argc, char *argv[])
                 buf_appendb(&dbuf, fn, strlen(fn) + 1);                  buf_appendb(&dbuf, fn, strlen(fn) + 1);
                 buf_appendb(&dbuf, msec, strlen(msec) + 1);                  buf_appendb(&dbuf, msec, strlen(msec) + 1);
                 buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1);                  buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1);
                 buf_appendb(&dbuf, arch ? arch : "",                  buf_appendb(&dbuf, arch, strlen(arch) + 1);
                                 arch ? strlen(arch) + 1 : 1);  
   
                 sv = dbuf.len;                  sv = dbuf.len;
   
Line 427  main(int argc, char *argv[])
Line 403  main(int argc, char *argv[])
                         val.data = vbuf;                          val.data = vbuf;
   
                         if (verb > 1)                          if (verb > 1)
                                 printf("%s: Keyword %s (%zu): 0x%x\n",                                  printf("%s: Keyword %s: 0x%x\n",
                                         fn, (char *)key.data, key.size,                                          fn, (char *)key.data,
                                         *(int *)val.data);                                          *(int *)val.data);
   
                         dbt_put(db, fbbuf, &key, &val);                          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 457  main(int argc, char *argv[])
Line 432  main(int argc, char *argv[])
                 if (verb > 0)                  if (verb > 0)
                         printf("%s: Indexed\n", fn);                          printf("%s: Indexed\n", fn);
   
                 dbt_put(idx, ibbuf, &key, &val);                  dbt_put(idx, ibuf, &key, &val);
                 rec++;                  rec++;
         }          }
   
         (*db->close)(db);          ec = MANDOCLEVEL_OK;
         (*idx->close)(idx);  
   
   out:
           if (db)
                   (*db->close)(db);
           if (idx)
                   (*idx->close)(idx);
         if (hash)          if (hash)
                 (*hash->close)(hash);                  (*hash->close)(hash);
           if (mp)
                   mparse_free(mp);
   
         mparse_free(mp);  
   
         free(buf.cp);          free(buf.cp);
         free(dbuf.cp);          free(dbuf.cp);
   
         /* 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);  
 }  }
   
 /*  /*
Line 520  buf_append(struct buf *buf, const char *cp)
Line 492  buf_append(struct buf *buf, const char *cp)
         buf_appendb(buf, 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_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 */  /* ARGSUSED */
 static void  static void
 pmdoc_An(MDOC_ARGS)  pmdoc_An(MDOC_ARGS)
Line 528  pmdoc_An(MDOC_ARGS)
Line 527  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 589  pmdoc_Cd(MDOC_ARGS)
Line 600  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 652  pmdoc_St(MDOC_ARGS)
Line 660  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 706  pmdoc_Fo(MDOC_ARGS)
Line 733  pmdoc_Fo(MDOC_ARGS)
 static void  static void
 pmdoc_Nd(MDOC_ARGS)  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) {          if (MDOC_BODY != n->type)
                         sz = strlen(n->string) + 1;                  return;
                         buf_appendb(dbuf, n->string, sz);  
                         buf_appendb(buf, n->string, sz);  
                 } else {  
                         buf_append(dbuf, n->string);  
                         buf_append(buf, n->string);  
                 }  
   
                 first = 0;          buf_appendmdoc(dbuf, n->child, 1);
         }          buf_appendmdoc(buf, n->child, 0);
   
         hash_put(hash, buf, TYPE_DESC);          hash_put(hash, buf, TYPE_DESC);
 }  }
   
 /* ARGSUSED */  /* ARGSUSED */
 static void  static void
   pmdoc_Er(MDOC_ARGS)
   {
   
           if (SEC_ERRORS != n->sec)
                   return;
   
           buf_appendmdoc(buf, n->child, 0);
           hash_put(hash, buf, TYPE_ERR);
   }
   
   /* ARGSUSED */
   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 745  pmdoc_Nm(MDOC_ARGS)
Line 794  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 892  pman_node(MAN_ARGS)
Line 938  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 916  pman_node(MAN_ARGS)
Line 963  pman_node(MAN_ARGS)
                         sz = strlen(start) + 1;                          sz = strlen(start) + 1;
                         buf_appendb(dbuf, start, sz);                          buf_appendb(dbuf, start, sz);
                         buf_appendb(buf, start, sz);                          buf_appendb(buf, start, sz);
   
                           hash_put(hash, buf, TYPE_DESC);
                 }                  }
         }          }
   

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

CVSweb