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

Diff for /mandoc/mandocdb.c between version 1.43 and 1.49.2.12

version 1.43, 2011/12/31 18:47:52 version 1.49.2.12, 2014/03/19 22:09:29
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 19 
Line 19 
 #include "config.h"  #include "config.h"
 #endif  #endif
   
 #include <sys/param.h>  
 #include <sys/types.h>  #include <sys/types.h>
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
 #include <dirent.h>  #include <dirent.h>
   #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <getopt.h>  #include <getopt.h>
   #include <limits.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdint.h>  #include <stdint.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
 #if defined(__linux__)  #if defined(__APPLE__)
   # include <libkern/OSByteOrder.h>
   #elif defined(__linux__)
 # include <endian.h>  # include <endian.h>
   #elif defined(__sun)
   # include <sys/byteorder.h>
   # include <sys/stat.h>
   #else
   # include <sys/endian.h>
   #endif
   
   #if defined(__linux__) || defined(__sun)
 # include <db_185.h>  # include <db_185.h>
 #elif defined(__APPLE__)  
 # include <libkern/OSByteOrder.h>  
 # include <db.h>  
 #else  #else
 # include <db.h>  # include <db.h>
 #endif  #endif
Line 58 
Line 66 
 /* Access to the mandoc database on disk. */  /* Access to the mandoc database on disk. */
   
 struct  mdb {  struct  mdb {
         char              idxn[MAXPATHLEN]; /* index db filename */          char              idxn[PATH_MAX]; /* index db filename */
         char              dbn[MAXPATHLEN]; /* keyword db filename */          char              dbn[PATH_MAX]; /* keyword db filename */
         DB               *idx; /* index recno database */          DB               *idx; /* index recno database */
         DB               *db; /* keyword btree database */          DB               *db; /* keyword btree database */
 };  };
Line 126  static void    index_merge(const struct of *, struct m
Line 134  static void    index_merge(const struct of *, struct m
                                 struct mdb *, struct recs *);                                  struct mdb *, struct recs *);
 static  void              index_prune(const struct of *, struct mdb *,  static  void              index_prune(const struct of *, struct mdb *,
                                 struct recs *);                                  struct recs *);
 static  void              ofile_argbuild(int, char *[], struct of **);  static  void              ofile_argbuild(int, char *[], struct of **,
                                   const char *);
 static  void              ofile_dirbuild(const char *, const char *,  static  void              ofile_dirbuild(const char *, const char *,
                                 const char *, int, struct of **);                                  const char *, int, struct of **);
 static  void              ofile_free(struct of *);  static  void              ofile_free(struct of *);
Line 279  static const struct mdoc_handler mdocs[MDOC_MAX] = {
Line 288  static const struct mdoc_handler mdocs[MDOC_MAX] = {
 };  };
   
 static  const char       *progname;  static  const char       *progname;
   static  int               mparse_options;  /* abort the parse early */
 static  int               use_all;  /* Use all directories and files. */  static  int               use_all;  /* Use all directories and files. */
 static  int               verb;  /* Output verbosity level. */  static  int               verb;  /* Output verbosity level. */
 static  int               warnings;  /* Potential problems in manuals. */  static  int               warnings;  /* Potential problems in manuals. */
Line 297  main(int argc, char *argv[])
Line 307  main(int argc, char *argv[])
         int              ch, i, flags;          int              ch, i, flags;
         DB              *hash; /* temporary keyword hashtable */          DB              *hash; /* temporary keyword hashtable */
         BTREEINFO        info; /* btree configuration */          BTREEINFO        info; /* btree configuration */
         size_t           sz1, sz2;          size_t           sz1, sz2, ipath;
         struct buf       buf, /* keyword buffer */          struct buf       buf, /* keyword buffer */
                          dbuf; /* description buffer */                           dbuf; /* description buffer */
         struct of       *of; /* list of files for processing */          struct of       *of; /* list of files for processing */
Line 319  main(int argc, char *argv[])
Line 329  main(int argc, char *argv[])
         hash = NULL;          hash = NULL;
         op = OP_DEFAULT;          op = OP_DEFAULT;
         dir = NULL;          dir = NULL;
           mparse_options = MPARSE_SO;
   
         while (-1 != (ch = getopt(argc, argv, "aC:d:tu:vW")))          while (-1 != (ch = getopt(argc, argv, "aC:d:Qtu:vW")))
                 switch (ch) {                  switch (ch) {
                 case ('a'):                  case ('a'):
                         use_all = 1;                          use_all = 1;
Line 343  main(int argc, char *argv[])
Line 354  main(int argc, char *argv[])
                         dir = optarg;                          dir = optarg;
                         op = OP_UPDATE;                          op = OP_UPDATE;
                         break;                          break;
                   case ('Q'):
                           mparse_options |= MPARSE_QUICK;
                           break;
                 case ('t'):                  case ('t'):
                         dup2(STDOUT_FILENO, STDERR_FILENO);                          dup2(STDOUT_FILENO, STDERR_FILENO);
                         if (op) {                          if (op) {
Line 382  main(int argc, char *argv[])
Line 396  main(int argc, char *argv[])
         }          }
   
         memset(&info, 0, sizeof(BTREEINFO));          memset(&info, 0, sizeof(BTREEINFO));
           info.lorder = 4321;
         info.flags = R_DUP;          info.flags = R_DUP;
   
         mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);          mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL, 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 394  main(int argc, char *argv[])
Line 409  main(int argc, char *argv[])
         buf.cp = mandoc_malloc(buf.size);          buf.cp = mandoc_malloc(buf.size);
         dbuf.cp = mandoc_malloc(dbuf.size);          dbuf.cp = mandoc_malloc(dbuf.size);
   
         flags = O_CREAT | O_RDWR;  
         if (OP_DEFAULT == op || OP_CONFFILE == op)  
                 flags |= O_TRUNC;  
   
         if (OP_TEST == op) {          if (OP_TEST == op) {
                 ofile_argbuild(argc, argv, &of);                  ofile_argbuild(argc, argv, &of, NULL);
                 if (NULL == of)                  if (NULL == of)
                         goto out;                          goto out;
                 index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs);                  index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs);
Line 407  main(int argc, char *argv[])
Line 418  main(int argc, char *argv[])
         }          }
   
         if (OP_UPDATE == op || OP_DELETE == op) {          if (OP_UPDATE == op || OP_DELETE == op) {
                 strlcat(mdb.dbn, dir, MAXPATHLEN);                  if (NULL == realpath(dir, pbuf)) {
                 strlcat(mdb.dbn, "/", MAXPATHLEN);                          perror(dir);
                 sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN);                          exit((int)MANDOCLEVEL_BADARG);
                   }
                   if (strlcat(pbuf, "/", PATH_MAX) >= PATH_MAX) {
                           fprintf(stderr, "%s: path too long\n", pbuf);
                           exit((int)MANDOCLEVEL_BADARG);
                   }
   
                 strlcat(mdb.idxn, dir, MAXPATHLEN);                  strlcat(mdb.dbn, pbuf, PATH_MAX);
                 strlcat(mdb.idxn, "/", MAXPATHLEN);                  sz1 = strlcat(mdb.dbn, MANDOC_DB, PATH_MAX);
                 sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN);  
   
                 if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {                  strlcat(mdb.idxn, pbuf, PATH_MAX);
                         fprintf(stderr, "%s: path too long\n", dir);                  sz2 = strlcat(mdb.idxn, MANDOC_IDX, PATH_MAX);
   
                   if (sz1 >= PATH_MAX || sz2 >= PATH_MAX) {
                           fprintf(stderr, "%s: path too long\n", mdb.idxn);
                         exit((int)MANDOCLEVEL_BADARG);                          exit((int)MANDOCLEVEL_BADARG);
                 }                  }
   
                   flags = O_CREAT | O_RDWR;
                 mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);                  mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);
                 mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);                  mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);
   
Line 431  main(int argc, char *argv[])
Line 450  main(int argc, char *argv[])
                         exit((int)MANDOCLEVEL_SYSERR);                          exit((int)MANDOCLEVEL_SYSERR);
                 }                  }
   
                 ofile_argbuild(argc, argv, &of);                  ofile_argbuild(argc, argv, &of, pbuf);
   
                 if (NULL == of)                  if (NULL == of)
                         goto out;                          goto out;
Line 475  main(int argc, char *argv[])
Line 494  main(int argc, char *argv[])
         } else          } else
                 manpath_parse(&dirs, dir, NULL, NULL);                  manpath_parse(&dirs, dir, NULL, NULL);
   
         for (i = 0; i < dirs.sz; i++) {          for (ipath = 0; ipath < dirs.sz; ipath++) {
                 mdb.idxn[0] = mdb.dbn[0] = '\0';  
   
                 strlcat(mdb.dbn, dirs.paths[i], MAXPATHLEN);                  /*
                 strlcat(mdb.dbn, "/", MAXPATHLEN);                   * Go to the root of the respective manual tree.
                 sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN);                   * This must work or no manuals may be found:
                    * They are indexed relative to the root.
                    */
   
                 strlcat(mdb.idxn, dirs.paths[i], MAXPATHLEN);                  if (-1 == chdir(dirs.paths[ipath])) {
                 strlcat(mdb.idxn, "/", MAXPATHLEN);                          perror(dirs.paths[ipath]);
                 sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN);                          exit((int)MANDOCLEVEL_SYSERR);
   
                 if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {  
                         fprintf(stderr, "%s: path too long\n",  
                                         dirs.paths[i]);  
                         exit((int)MANDOCLEVEL_BADARG);  
                 }                  }
   
                 if (mdb.db)                  /* Create a new database in two temporary files. */
                         (*mdb.db->close)(mdb.db);  
                 if (mdb.idx)  
                         (*mdb.idx->close)(mdb.idx);  
   
                 mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);                  flags = O_CREAT | O_EXCL | O_RDWR;
                 mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);                  while (NULL == mdb.db) {
                           strlcpy(mdb.dbn, MANDOC_DB, PATH_MAX);
                 if (NULL == mdb.db) {                          strlcat(mdb.dbn, ".XXXXXXXXXX", PATH_MAX);
                         perror(mdb.dbn);                          if (NULL == mktemp(mdb.dbn)) {
                         exit((int)MANDOCLEVEL_SYSERR);                                  perror(mdb.dbn);
                 } else if (NULL == mdb.idx) {                                  exit((int)MANDOCLEVEL_SYSERR);
                         perror(mdb.idxn);                          }
                         exit((int)MANDOCLEVEL_SYSERR);                          mdb.db = dbopen(mdb.dbn, flags, 0644,
                                           DB_BTREE, &info);
                           if (NULL == mdb.db && EEXIST != errno) {
                                   perror(mdb.dbn);
                                   exit((int)MANDOCLEVEL_SYSERR);
                           }
                 }                  }
                   while (NULL == mdb.idx) {
                           strlcpy(mdb.idxn, MANDOC_IDX, PATH_MAX);
                           strlcat(mdb.idxn, ".XXXXXXXXXX", PATH_MAX);
                           if (NULL == mktemp(mdb.idxn)) {
                                   perror(mdb.idxn);
                                   unlink(mdb.dbn);
                                   exit((int)MANDOCLEVEL_SYSERR);
                           }
                           mdb.idx = dbopen(mdb.idxn, flags, 0644,
                                           DB_RECNO, NULL);
                           if (NULL == mdb.idx && EEXIST != errno) {
                                   perror(mdb.idxn);
                                   unlink(mdb.dbn);
                                   exit((int)MANDOCLEVEL_SYSERR);
                           }
                   }
   
                 ofile_free(of);                  /*
                 of = NULL;                   * Search for manuals and fill the new database.
                    */
   
                 if (-1 == chdir(dirs.paths[i])) {                  ofile_dirbuild(".", "", "", 0, &of);
                         perror(dirs.paths[i]);  
                         exit((int)MANDOCLEVEL_SYSERR);                  if (NULL != of) {
                           index_merge(of, mp, &dbuf, &buf, hash,
                                &mdb, &recs);
                           ofile_free(of);
                           of = NULL;
                 }                  }
   
                 ofile_dirbuild(".", "", "", 0, &of);                  (*mdb.db->close)(mdb.db);
                 if (NULL == of)                  (*mdb.idx->close)(mdb.idx);
                         continue;                  mdb.db = NULL;
                   mdb.idx = NULL;
   
                 /*                  /*
                  * Go to the root of the respective manual tree.                   * Replace the old database with the new one.
                  * This must work or no manuals may be found (they're                   * This is not perfectly atomic,
                  * indexed relative to the root).                   * but i cannot think of a better way.
                  */                   */
   
                 if (-1 == chdir(dirs.paths[i])) {                  if (-1 == rename(mdb.dbn, MANDOC_DB)) {
                         perror(dirs.paths[i]);                          perror(MANDOC_DB);
                           unlink(mdb.dbn);
                           unlink(mdb.idxn);
                         exit((int)MANDOCLEVEL_SYSERR);                          exit((int)MANDOCLEVEL_SYSERR);
                 }                  }
                   if (-1 == rename(mdb.idxn, MANDOC_IDX)) {
                 index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs);                          perror(MANDOC_IDX);
                           unlink(MANDOC_DB);
                           unlink(MANDOC_IDX);
                           unlink(mdb.idxn);
                           exit((int)MANDOCLEVEL_SYSERR);
                   }
         }          }
   
 out:  out:
Line 554  out:
Line 600  out:
   
 usage:  usage:
         fprintf(stderr,          fprintf(stderr,
                 "usage: %s [-avvv] [-C file] | dir ... | -t file ...\n"                  "usage: %s [-aQvvv] [-C file] | dir ... | -t file ...\n"
                 "                        -d dir [file ...] | "                  "                        -d dir [file ...] | "
                 "-u dir [file ...]\n",                  "-u dir [file ...]\n",
                 progname);                  progname);
Line 570  index_merge(const struct of *of, struct mparse *mp,
Line 616  index_merge(const struct of *of, struct mparse *mp,
         recno_t          rec;          recno_t          rec;
         int              ch, skip;          int              ch, skip;
         DBT              key, val;          DBT              key, val;
           DB              *files;  /* temporary file name table */
         struct mdoc     *mdoc;          struct mdoc     *mdoc;
         struct man      *man;          struct man      *man;
         const char      *fn, *msec, *march, *mtitle;          const char      *fn, *msec, *march, *mtitle;
           char            *p;
         uint64_t         mask;          uint64_t         mask;
         size_t           sv;          size_t           sv;
         unsigned         seq;          unsigned         seq;
         uint64_t         vbuf[2];          uint64_t         vbuf[2];
         char             type;          char             type;
   
           static char      emptystring[] = "";
   
           if (warnings) {
                   files = NULL;
                   hash_reset(&files);
           }
   
         rec = 0;          rec = 0;
         for (of = of->first; of; of = of->next) {          for (of = of->first; of; of = of->next) {
                 fn = of->fname;                  fn = of->fname;
Line 615  index_merge(const struct of *of, struct mparse *mp,
Line 670  index_merge(const struct of *of, struct mparse *mp,
                 }                  }
   
                 /*                  /*
                  * By default, skip a file if the manual section                   * Check whether the manual section given in a file
                  * given in the file disagrees with the directory                   * agrees with the directory where the file is located.
                  * where the file is located.                   * Some manuals have suffixes like (3p) on their
                    * section number either inside the file or in the
                    * directory name, some are linked into more than one
                    * section, like encrypt(1) = makekey(8).  Do not skip
                    * manuals for such reasons.
                  */                   */
   
                 skip = 0;                  skip = 0;
                 assert(of->sec);                  assert(of->sec);
                 assert(msec);                  assert(msec);
                 if (strcasecmp(msec, of->sec)) {                  if (warnings)
                         if (warnings)                          if (strcasecmp(msec, of->sec))
                                 fprintf(stderr, "%s: "                                  fprintf(stderr, "%s: "
                                         "section \"%s\" manual "                                          "section \"%s\" manual "
                                         "in \"%s\" directory\n",                                          "in \"%s\" directory\n",
                                         fn, msec, of->sec);                                          fn, msec, of->sec);
                         skip = 1;  
                 }  
   
                 /*                  /*
                  * Manual page directories exist for each kernel                   * Manual page directories exist for each kernel
Line 649  index_merge(const struct of *of, struct mparse *mp,
Line 706  index_merge(const struct of *of, struct mparse *mp,
   
                 assert(of->arch);                  assert(of->arch);
                 assert(march);                  assert(march);
                 if (strcasecmp(march, of->arch)) {                  if (warnings)
                         if (warnings)                          if (strcasecmp(march, of->arch))
                                 fprintf(stderr, "%s: "                                  fprintf(stderr, "%s: "
                                         "architecture \"%s\" manual "                                          "architecture \"%s\" manual "
                                         "in \"%s\" directory\n",                                          "in \"%s\" directory\n",
                                         fn, march, of->arch);                                          fn, march, of->arch);
                         march = of->arch;  
                 }  
   
                 /*                  /*
                  * By default, skip a file if the title given                   * By default, skip a file if the title given
                  * in the file disagrees with the file name.                   * in the file disagrees with the file name.
                  * If both agree, use the file name as the title,                   * Do not warn, this happens for all MLINKs.
                  * because the one in the file usually is all caps.  
                  */                   */
   
                 assert(of->title);                  assert(of->title);
                 assert(mtitle);                  assert(mtitle);
                 if (strcasecmp(mtitle, of->title)) {                  if (strcasecmp(mtitle, of->title))
                         if (warnings)  
                                 fprintf(stderr, "%s: "  
                                         "title \"%s\" in file "  
                                         "but \"%s\" in filename\n",  
                                         fn, mtitle, of->title);  
                         skip = 1;                          skip = 1;
                 } else  
                         mtitle = of->title;  
   
                   /*
                    * Build a title string for the file.  If it matches
                    * the location of the file, remember the title as
                    * found; else, remember it as missing.
                    */
   
                   if (warnings) {
                           buf->len = 0;
                           buf_appendb(buf, mtitle, strlen(mtitle));
                           buf_appendb(buf, "(", 1);
                           buf_appendb(buf, msec, strlen(msec));
                           if ('\0' != *march) {
                                   buf_appendb(buf, "/", 1);
                                   buf_appendb(buf, march, strlen(march));
                           }
                           buf_appendb(buf, ")", 2);
                           for (p = buf->cp; '\0' != *p; p++)
                                   *p = tolower((unsigned char)*p);
                           key.data = buf->cp;
                           key.size = buf->len;
                           val.data = NULL;
                           val.size = 0;
                           if (0 == skip)
                                   val.data = emptystring;
                           else {
                                   ch = (*files->get)(files, &key, &val, 0);
                                   if (ch < 0) {
                                           perror("hash");
                                           exit((int)MANDOCLEVEL_SYSERR);
                                   } else if (ch > 0) {
                                           val.data = (void *)fn;
                                           val.size = strlen(fn) + 1;
                                   } else
                                           val.data = NULL;
                           }
                           if (NULL != val.data &&
                               (*files->put)(files, &key, &val, 0) < 0) {
                                   perror("hash");
                                   exit((int)MANDOCLEVEL_SYSERR);
                           }
                   }
   
                 if (skip && !use_all)                  if (skip && !use_all)
                         continue;                          continue;
   
                 /*                  /*
                  * 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
                  * nil-terminated description.  Since the description                   * nil-terminated description.  Use the actual
                  * may not be set, we set a sentinel to see if we're                   * location of the file, such that the user can find
                  * going to write a nil byte in its place.                   * it with man(1).  Since the description may not be
                    * set, we set a sentinel to see if we're going to
                    * write a nil byte in its place.
                  */                   */
   
                 dbuf->len = 0;                  dbuf->len = 0;
                 type = mdoc ? 'd' : (man ? 'a' : 'c');                  type = mdoc ? 'd' : (man ? 'a' : 'c');
                 buf_appendb(dbuf, &type, 1);                  buf_appendb(dbuf, &type, 1);
                 buf_appendb(dbuf, fn, strlen(fn) + 1);                  buf_appendb(dbuf, fn, strlen(fn) + 1);
                 buf_appendb(dbuf, msec, strlen(msec) + 1);                  buf_appendb(dbuf, of->sec, strlen(of->sec) + 1);
                 buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);                  buf_appendb(dbuf, of->title, strlen(of->title) + 1);
                 buf_appendb(dbuf, march, strlen(march) + 1);                  buf_appendb(dbuf, of->arch, strlen(of->arch) + 1);
   
                 sv = dbuf->len;                  sv = dbuf->len;
   
Line 718  index_merge(const struct of *of, struct mparse *mp,
Line 809  index_merge(const struct of *of, struct mparse *mp,
                         continue;                          continue;
   
                 /*                  /*
                    * Make sure the file name is always registered
                    * as an .Nm search key.
                    */
                   buf->len = 0;
                   buf_append(buf, of->title);
                   hash_put(hash, buf, TYPE_Nm);
   
                   /*
                  * Reclaim an empty index record, if available.                   * Reclaim an empty index record, if available.
                  * Use its record number for all new btree nodes.                   * Use its record number for all new btree nodes.
                  */                   */
Line 749  index_merge(const struct of *of, struct mparse *mp,
Line 848  index_merge(const struct of *of, struct mparse *mp,
                 }                  }
                 if (ch < 0) {                  if (ch < 0) {
                         perror("hash");                          perror("hash");
                           unlink(mdb->dbn);
                           unlink(mdb->idxn);
                         exit((int)MANDOCLEVEL_SYSERR);                          exit((int)MANDOCLEVEL_SYSERR);
                 }                  }
   
Line 771  index_merge(const struct of *of, struct mparse *mp,
Line 872  index_merge(const struct of *of, struct mparse *mp,
   
                 dbt_put(mdb->idx, mdb->idxn, &key, &val);                  dbt_put(mdb->idx, mdb->idxn, &key, &val);
         }          }
   
           /*
            * Iterate the remembered file titles and check that
            * all files can be found by their main title.
            */
   
           if (warnings) {
                   seq = R_FIRST;
                   while (0 == (*files->seq)(files, &key, &val, seq)) {
                           seq = R_NEXT;
                           if (val.size)
                                   fprintf(stderr, "%s: probably "
                                       "unreachable, title is %s\n",
                                       (char *)val.data, (char *)key.data);
                   }
                   (*files->close)(files);
           }
 }  }
   
 /*  /*
Line 1264  static int
Line 1382  static int
 pman_node(MAN_ARGS)  pman_node(MAN_ARGS)
 {  {
         const struct man_node *head, *body;          const struct man_node *head, *body;
         const char      *start, *sv;          char            *start, *sv, *title;
         size_t           sz;          size_t           sz, titlesz;
   
         if (NULL == n)          if (NULL == n)
                 return(0);                  return(0);
Line 1288  pman_node(MAN_ARGS)
Line 1406  pman_node(MAN_ARGS)
                                 NULL != (body = body->child) &&                                  NULL != (body = body->child) &&
                                 MAN_TEXT == body->type) {                                  MAN_TEXT == body->type) {
   
                         assert(body->string);                          title = NULL;
                         start = sv = body->string;                          titlesz = 0;
                           /*
                            * Suck the entire NAME section into memory.
                            * Yes, we might run away.
                            * But too many manuals have big, spread-out
                            * NAME sections over many lines.
                            */
                           for ( ; NULL != body; body = body->next) {
                                   if (MAN_TEXT != body->type)
                                           break;
                                   if (0 == (sz = strlen(body->string)))
                                           continue;
                                   title = mandoc_realloc
                                           (title, titlesz + sz + 1);
                                   memcpy(title + titlesz, body->string, sz);
                                   titlesz += sz + 1;
                                   title[(int)titlesz - 1] = ' ';
                           }
                           if (NULL == title)
                                   return(0);
   
                           title = mandoc_realloc(title, titlesz + 1);
                           title[(int)titlesz] = '\0';
   
                           /* Skip leading space.  */
   
                           sv = title;
                           while (isspace((unsigned char)*sv))
                                   sv++;
   
                           if (0 == (sz = strlen(sv))) {
                                   free(title);
                                   return(0);
                           }
   
                           /* Erase trailing space. */
   
                           start = &sv[sz - 1];
                           while (start > sv && isspace((unsigned char)*start))
                                   *start-- = '\0';
   
                           if (start == sv) {
                                   free(title);
                                   return(0);
                           }
   
                           start = sv;
   
                         /*                          /*
                          * Go through a special heuristic dance here.                           * Go through a special heuristic dance here.
                          * This is why -man manuals are great!                           * This is why -man manuals are great!
Line 1327  pman_node(MAN_ARGS)
Line 1491  pman_node(MAN_ARGS)
   
                         if (sv == start) {                          if (sv == start) {
                                 buf_append(buf, start);                                  buf_append(buf, start);
                                   free(title);
                                 return(1);                                  return(1);
                         }                          }
   
                         while (' ' == *start)                          while (isspace((unsigned char)*start))
                                 start++;                                  start++;
   
                         if (0 == strncmp(start, "-", 1))                          if (0 == strncmp(start, "-", 1))
Line 1352  pman_node(MAN_ARGS)
Line 1517  pman_node(MAN_ARGS)
                         buf_appendb(buf, start, sz);                          buf_appendb(buf, start, sz);
   
                         hash_put(hash, buf, TYPE_Nd);                          hash_put(hash, buf, TYPE_Nd);
                           free(title);
                 }                  }
         }          }
   
Line 1492  pformatted(DB *hash, struct buf *buf, 
Line 1658  pformatted(DB *hash, struct buf *buf, 
 }  }
   
 static void  static void
 ofile_argbuild(int argc, char *argv[], struct of **of)  ofile_argbuild(int argc, char *argv[], struct of **of,
                   const char *basedir)
 {  {
         char             buf[MAXPATHLEN];          char             buf[PATH_MAX];
           char             pbuf[PATH_MAX];
         const char      *sec, *arch, *title;          const char      *sec, *arch, *title;
         char            *p;          char            *relpath, *p;
         int              i, src_form;          int              i, src_form;
         struct of       *nof;          struct of       *nof;
   
         for (i = 0; i < argc; i++) {          for (i = 0; i < argc; i++) {
                   if (NULL == (relpath = realpath(argv[i], pbuf))) {
                           perror(argv[i]);
                           continue;
                   }
                   if (NULL != basedir) {
                           if (strstr(pbuf, basedir) != pbuf) {
                                   fprintf(stderr, "%s: file outside "
                                       "base directory %s\n",
                                       pbuf, basedir);
                                   continue;
                           }
                           relpath = pbuf + strlen(basedir);
                   }
   
                 /*                  /*
                  * Try to infer the manual section, architecture and                   * Try to infer the manual section, architecture and
Line 1509  ofile_argbuild(int argc, char *argv[], struct of **of)
Line 1690  ofile_argbuild(int argc, char *argv[], struct of **of)
                  *   cat<section>[/<arch>]/<title>.0                   *   cat<section>[/<arch>]/<title>.0
                  */                   */
   
                 if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {                  if (strlcpy(buf, relpath, sizeof(buf)) >= sizeof(buf)) {
                         fprintf(stderr, "%s: path too long\n", argv[i]);                          fprintf(stderr, "%s: path too long\n", relpath);
                         continue;                          continue;
                 }                  }
                 sec = arch = title = "";                  sec = arch = title = "";
Line 1546  ofile_argbuild(int argc, char *argv[], struct of **of)
Line 1727  ofile_argbuild(int argc, char *argv[], struct of **of)
                                 fprintf(stderr,                                  fprintf(stderr,
                                     "%s: cannot deduce title "                                      "%s: cannot deduce title "
                                     "from filename\n",                                      "from filename\n",
                                     argv[i]);                                      relpath);
                         title = buf;                          title = buf;
                 }                  }
   
Line 1555  ofile_argbuild(int argc, char *argv[], struct of **of)
Line 1736  ofile_argbuild(int argc, char *argv[], struct of **of)
                  */                   */
   
                 nof = mandoc_calloc(1, sizeof(struct of));                  nof = mandoc_calloc(1, sizeof(struct of));
                 nof->fname = mandoc_strdup(argv[i]);                  nof->fname = mandoc_strdup(relpath);
                 nof->sec = mandoc_strdup(sec);                  nof->sec = mandoc_strdup(sec);
                 nof->arch = mandoc_strdup(arch);                  nof->arch = mandoc_strdup(arch);
                 nof->title = mandoc_strdup(title);                  nof->title = mandoc_strdup(title);
Line 1565  ofile_argbuild(int argc, char *argv[], struct of **of)
Line 1746  ofile_argbuild(int argc, char *argv[], struct of **of)
                  * Add the structure to the list.                   * Add the structure to the list.
                  */                   */
   
                 if (verb > 1)  
                         printf("%s: scheduling\n", argv[i]);  
                 if (NULL == *of) {                  if (NULL == *of) {
                         *of = nof;                          *of = nof;
                         (*of)->first = nof;                          (*of)->first = nof;
Line 1582  ofile_argbuild(int argc, char *argv[], struct of **of)
Line 1761  ofile_argbuild(int argc, char *argv[], struct of **of)
  * Recursively build up a list of files to parse.   * Recursively build up a list of files to parse.
  * We use this instead of ftw() and so on because I don't want global   * We use this instead of ftw() and so on because I don't want global
  * variables hanging around.   * variables hanging around.
  * This ignores the whatis.db and whatis.index files, but assumes that   * This ignores the mandoc.db and mandoc.index files, but assumes that
  * everything else is a manual.   * everything else is a manual.
  * Pass in a pointer to a NULL structure for the first invocation.   * Pass in a pointer to a NULL structure for the first invocation.
  */   */
Line 1590  static void
Line 1769  static void
 ofile_dirbuild(const char *dir, const char* psec, const char *parch,  ofile_dirbuild(const char *dir, const char* psec, const char *parch,
                 int p_src_form, struct of **of)                  int p_src_form, struct of **of)
 {  {
         char             buf[MAXPATHLEN];          char             buf[PATH_MAX];
   #if defined(__sun)
           struct stat      sb;
   #endif
         size_t           sz;          size_t           sz;
         DIR             *d;          DIR             *d;
         const char      *fn, *sec, *arch;          const char      *fn, *sec, *arch;
Line 1613  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1795  ofile_dirbuild(const char *dir, const char* psec, cons
   
                 src_form = p_src_form;                  src_form = p_src_form;
   
   #if defined(__sun)
                   stat(dp->d_name, &sb);
                   if (S_IFDIR & sb.st_mode) {
   #else
                 if (DT_DIR == dp->d_type) {                  if (DT_DIR == dp->d_type) {
   #endif
                         sec = psec;                          sec = psec;
                         arch = parch;                          arch = parch;
   
Line 1656  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1843  ofile_dirbuild(const char *dir, const char* psec, cons
                         }                          }
   
                         buf[0] = '\0';                          buf[0] = '\0';
                         strlcat(buf, dir, MAXPATHLEN);                          strlcat(buf, dir, PATH_MAX);
                         strlcat(buf, "/", MAXPATHLEN);                          strlcat(buf, "/", PATH_MAX);
                         sz = strlcat(buf, fn, MAXPATHLEN);                          sz = strlcat(buf, fn, PATH_MAX);
   
                         if (MAXPATHLEN <= sz) {                          if (PATH_MAX <= sz) {
                                 if (warnings) fprintf(stderr, "%s/%s: "                                  if (warnings) fprintf(stderr, "%s/%s: "
                                     "path too long\n", dir, fn);                                      "path too long\n", dir, fn);
                                 continue;                                  continue;
                         }                          }
   
                         if (verb > 1)  
                                 printf("%s: scanning\n", buf);  
   
                         ofile_dirbuild(buf, sec, arch, src_form, of);                          ofile_dirbuild(buf, sec, arch, src_form, of);
                         continue;                          continue;
                 }                  }
   
   #if defined(__sun)
                   if (0 == S_IFREG & sb.st_mode) {
   #else
                 if (DT_REG != dp->d_type) {                  if (DT_REG != dp->d_type) {
   #endif
                         if (warnings)                          if (warnings)
                                 fprintf(stderr,                                  fprintf(stderr,
                                     "%s/%s: not a regular file\n",                                      "%s/%s: not a regular file\n",
Line 1731  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1919  ofile_dirbuild(const char *dir, const char* psec, cons
                 if (0 == use_all && MANDOC_FORM & src_form &&                  if (0 == use_all && MANDOC_FORM & src_form &&
                                 '\0' != *psec) {                                  '\0' != *psec) {
                         buf[0] = '\0';                          buf[0] = '\0';
                         strlcat(buf, dir, MAXPATHLEN);                          strlcat(buf, dir, PATH_MAX);
                         p = strrchr(buf, '/');                          p = strrchr(buf, '/');
                         if ('\0' != *parch && NULL != p)                          if ('\0' != *parch && NULL != p)
                                 for (p--; p > buf; p--)                                  for (p--; p > buf; p--)
Line 1743  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1931  ofile_dirbuild(const char *dir, const char* psec, cons
                                 p++;                                  p++;
                         if (0 == strncmp("cat", p, 3))                          if (0 == strncmp("cat", p, 3))
                                 memcpy(p, "man", 3);                                  memcpy(p, "man", 3);
                         strlcat(buf, "/", MAXPATHLEN);                          strlcat(buf, "/", PATH_MAX);
                         sz = strlcat(buf, fn, MAXPATHLEN);                          sz = strlcat(buf, fn, PATH_MAX);
                         if (sz >= MAXPATHLEN) {                          if (sz >= PATH_MAX) {
                                 if (warnings) fprintf(stderr,                                  if (warnings) fprintf(stderr,
                                     "%s/%s: path too long\n",                                      "%s/%s: path too long\n",
                                     dir, fn);                                      dir, fn);
Line 1754  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1942  ofile_dirbuild(const char *dir, const char* psec, cons
                         q = strrchr(buf, '.');                          q = strrchr(buf, '.');
                         if (NULL != q && p < q++) {                          if (NULL != q && p < q++) {
                                 *q = '\0';                                  *q = '\0';
                                 sz = strlcat(buf, psec, MAXPATHLEN);                                  sz = strlcat(buf, psec, PATH_MAX);
                                 if (sz >= MAXPATHLEN) {                                  if (sz >= PATH_MAX) {
                                         if (warnings) fprintf(stderr,                                          if (warnings) fprintf(stderr,
                                             "%s/%s: path too long\n",                                              "%s/%s: path too long\n",
                                             dir, fn);                                              dir, fn);
Line 1769  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1957  ofile_dirbuild(const char *dir, const char* psec, cons
                 buf[0] = '\0';                  buf[0] = '\0';
                 assert('.' == dir[0]);                  assert('.' == dir[0]);
                 if ('/' == dir[1]) {                  if ('/' == dir[1]) {
                         strlcat(buf, dir + 2, MAXPATHLEN);                          strlcat(buf, dir + 2, PATH_MAX);
                         strlcat(buf, "/", MAXPATHLEN);                          strlcat(buf, "/", PATH_MAX);
                 }                  }
                 sz = strlcat(buf, fn, MAXPATHLEN);                  sz = strlcat(buf, fn, PATH_MAX);
                 if (sz >= MAXPATHLEN) {                  if (sz >= PATH_MAX) {
                         if (warnings) fprintf(stderr,                          if (warnings) fprintf(stderr,
                             "%s/%s: path too long\n", dir, fn);                              "%s/%s: path too long\n", dir, fn);
                         continue;                          continue;
Line 1797  ofile_dirbuild(const char *dir, const char* psec, cons
Line 1985  ofile_dirbuild(const char *dir, const char* psec, cons
                 /*                  /*
                  * Add the structure to the list.                   * Add the structure to the list.
                  */                   */
   
                 if (verb > 1)  
                         printf("%s: scheduling\n", buf);  
   
                 if (NULL == *of) {                  if (NULL == *of) {
                         *of = nof;                          *of = nof;

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.49.2.12

CVSweb