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

Diff for /mandoc/Attic/makewhatis.c between version 1.8 and 1.14

version 1.8, 2011/06/22 09:00:14 version 1.14, 2011/07/01 10:46:32
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
   #define TYPE_XREF         0x200
   #define TYPE_PATH         0x400
   
 /* Buffer for storing growable data. */  /* Buffer for storing growable data. */
   
Line 72  struct buf {
Line 75  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);
Line 87  static void    pmdoc_Fn(MDOC_ARGS);
Line 92  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 129  static const pmdoc_nf   mdocs[MDOC_MAX] = {
Line 136  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 236  main(int argc, char *argv[])
Line 243  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 263  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 309  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 326  main(int argc, char *argv[])
Line 337  main(int argc, char *argv[])
         }          }
   
         /*          /*
          * 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 348  main(int argc, char *argv[])
Line 359  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)                  if (hash)
                         (*hash->close)(hash);                          (*hash->close)(hash);
   
Line 364  main(int argc, char *argv[])
Line 377  main(int argc, char *argv[])
                         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 391  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 422  main(int argc, char *argv[])
Line 432  main(int argc, char *argv[])
                         val.size = sizeof(vbuf);                          val.size = sizeof(vbuf);
                         val.data = vbuf;                          val.data = vbuf;
   
                         printf("Added: %s (%zu): 0x%x\n",                          if (verb > 1)
                                         (char *)key.data, key.size,                                  printf("%s: Keyword %s (%zu): 0x%x\n",
                                           fn, (char *)key.data, key.size,
                                         *(int *)val.data);                                          *(int *)val.data);
   
                         dbt_put(db, fbbuf, &key, &val);                          dbt_put(db, fbbuf, &key, &val);
Line 449  main(int argc, char *argv[])
Line 460  main(int argc, char *argv[])
                 val.data = dbuf.cp;                  val.data = dbuf.cp;
                 val.size = dbuf.len;                  val.size = dbuf.len;
   
                 printf("Indexed: %s\n", fn);                  if (verb > 0)
                           printf("%s: Indexed\n", fn);
   
                 dbt_put(idx, ibbuf, &key, &val);                  dbt_put(idx, ibbuf, &key, &val);
                 rec++;                  rec++;
Line 457  main(int argc, char *argv[])
Line 469  main(int argc, char *argv[])
   
         (*db->close)(db);          (*db->close)(db);
         (*idx->close)(idx);          (*idx->close)(idx);
   
         if (hash)          if (hash)
                 (*hash->close)(hash);                  (*hash->close)(hash);
   
Line 513  buf_append(struct buf *buf, const char *cp)
Line 526  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 521  pmdoc_An(MDOC_ARGS)
Line 561  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);
 }  }
   
Line 582  pmdoc_Cd(MDOC_ARGS)
Line 619  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 645  pmdoc_St(MDOC_ARGS)
Line 679  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 699  pmdoc_Fo(MDOC_ARGS)
Line 752  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_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 738  pmdoc_Nm(MDOC_ARGS)
Line 789  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 924  static void
Line 972  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.8  
changed lines
  Added in v.1.14

CVSweb