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

Diff for /mandoc/mandocdb.c between version 1.204 and 1.218

version 1.204, 2015/10/20 02:01:31 version 1.218, 2016/07/12 05:18:38
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org>   * Copyright (c) 2011-2016 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 23 
Line 23 
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
   #if HAVE_ERR
 #include <err.h>  #include <err.h>
   #endif
 #include <errno.h>  #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #if HAVE_FTS  #if HAVE_FTS
Line 31 
Line 33 
 #else  #else
 #include "compat_fts.h"  #include "compat_fts.h"
 #endif  #endif
 #include <getopt.h>  
 #include <limits.h>  #include <limits.h>
   #if HAVE_SANDBOX_INIT
   #include <sandbox.h>
   #endif
 #include <stddef.h>  #include <stddef.h>
 #include <stdio.h>  #include <stdio.h>
 #include <stdint.h>  #include <stdint.h>
Line 154  static void  parse_man(struct mpage *, const struct ro
Line 158  static void  parse_man(struct mpage *, const struct ro
                         const struct roff_node *);                          const struct roff_node *);
 static  void     parse_mdoc(struct mpage *, const struct roff_meta *,  static  void     parse_mdoc(struct mpage *, const struct roff_meta *,
                         const struct roff_node *);                          const struct roff_node *);
 static  int      parse_mdoc_body(struct mpage *, const struct roff_meta *,  
                         const struct roff_node *);  
 static  int      parse_mdoc_head(struct mpage *, const struct roff_meta *,  static  int      parse_mdoc_head(struct mpage *, const struct roff_meta *,
                         const struct roff_node *);                          const struct roff_node *);
 static  int      parse_mdoc_Fd(struct mpage *, const struct roff_meta *,  static  int      parse_mdoc_Fd(struct mpage *, const struct roff_meta *,
Line 171  static int  parse_mdoc_Nm(struct mpage *, const struct
Line 173  static int  parse_mdoc_Nm(struct mpage *, const struct
                         const struct roff_node *);                          const struct roff_node *);
 static  int      parse_mdoc_Sh(struct mpage *, const struct roff_meta *,  static  int      parse_mdoc_Sh(struct mpage *, const struct roff_meta *,
                         const struct roff_node *);                          const struct roff_node *);
   static  int      parse_mdoc_Va(struct mpage *, const struct roff_meta *,
                           const struct roff_node *);
 static  int      parse_mdoc_Xr(struct mpage *, const struct roff_meta *,  static  int      parse_mdoc_Xr(struct mpage *, const struct roff_meta *,
                         const struct roff_node *);                          const struct roff_node *);
 static  void     putkey(const struct mpage *, char *, uint64_t);  static  void     putkey(const struct mpage *, char *, uint64_t);
Line 183  static int  set_basedir(const char *, int);
Line 187  static int  set_basedir(const char *, int);
 static  int      treescan(void);  static  int      treescan(void);
 static  size_t   utf8(unsigned int, char [7]);  static  size_t   utf8(unsigned int, char [7]);
   
 extern  char            *__progname;  
   
 static  char             tempfilename[32];  static  char             tempfilename[32];
 static  int              nodb; /* no database changes */  static  int              nodb; /* no database changes */
 static  int              mparse_options; /* abort the parse early */  static  int              mparse_options; /* abort the parse early */
Line 242  static const struct mdoc_handler mdocs[MDOC_MAX] = {
Line 244  static const struct mdoc_handler mdocs[MDOC_MAX] = {
         { NULL, TYPE_Pa },  /* Pa */          { NULL, TYPE_Pa },  /* Pa */
         { NULL, 0 },  /* Rv */          { NULL, 0 },  /* Rv */
         { NULL, TYPE_St },  /* St */          { NULL, TYPE_St },  /* St */
         { NULL, TYPE_Va },  /* Va */          { parse_mdoc_Va, TYPE_Va },  /* Va */
         { parse_mdoc_body, TYPE_Va },  /* Vt */          { parse_mdoc_Va, TYPE_Vt },  /* Vt */
         { parse_mdoc_Xr, 0 },  /* Xr */          { parse_mdoc_Xr, 0 },  /* Xr */
         { NULL, 0 },  /* %A */          { NULL, 0 },  /* %A */
         { NULL, 0 },  /* %B */          { NULL, 0 },  /* %B */
Line 335  mandocdb(int argc, char *argv[])
Line 337  mandocdb(int argc, char *argv[])
 {  {
         struct manconf    conf;          struct manconf    conf;
         struct mparse    *mp;          struct mparse    *mp;
         const char       *path_arg;          const char       *path_arg, *progname;
         size_t            j, sz;          size_t            j, sz;
         int               ch, i;          int               ch, i;
   
   #if HAVE_PLEDGE
           if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) {
                   warn("pledge");
                   return (int)MANDOCLEVEL_SYSERR;
           }
   #endif
   
   #if HAVE_SANDBOX_INIT
           if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1) {
                   warnx("sandbox_init");
                   return (int)MANDOCLEVEL_SYSERR;
           }
   #endif
   
         memset(&conf, 0, sizeof(conf));          memset(&conf, 0, sizeof(conf));
         memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));          memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
   
Line 412  mandocdb(int argc, char *argv[])
Line 428  mandocdb(int argc, char *argv[])
         argc -= optind;          argc -= optind;
         argv += optind;          argv += optind;
   
   #if HAVE_PLEDGE
           if (nodb) {
                   if (pledge("stdio rpath", NULL) == -1) {
                           warn("pledge");
                           return (int)MANDOCLEVEL_SYSERR;
                   }
           }
   #endif
   
         if (OP_CONFFILE == op && argc > 0) {          if (OP_CONFFILE == op && argc > 0) {
                 warnx("-C: Too many arguments");                  warnx("-C: Too many arguments");
                 goto usage;                  goto usage;
Line 437  mandocdb(int argc, char *argv[])
Line 462  mandocdb(int argc, char *argv[])
                          * The existing database is usable.  Process                           * The existing database is usable.  Process
                          * all files specified on the command-line.                           * all files specified on the command-line.
                          */                           */
   #if HAVE_PLEDGE
                           if (!nodb) {
                                   if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) {
                                           warn("pledge");
                                           exitcode = (int)MANDOCLEVEL_SYSERR;
                                           goto out;
                                   }
                           }
   #endif
                         use_all = 1;                          use_all = 1;
                         for (i = 0; i < argc; i++)                          for (i = 0; i < argc; i++)
                                 filescan(argv[i]);                                  filescan(argv[i]);
Line 526  out:
Line 560  out:
         ohash_delete(&mlinks);          ohash_delete(&mlinks);
         return exitcode;          return exitcode;
 usage:  usage:
           progname = getprogname();
         fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"          fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n"
                         "       %s [-aDnpQ] [-Tutf8] dir ...\n"                          "       %s [-aDnpQ] [-Tutf8] dir ...\n"
                         "       %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"                          "       %s [-DnpQ] [-Tutf8] -d dir [file ...]\n"
                         "       %s [-Dnp] -u dir [file ...]\n"                          "       %s [-Dnp] -u dir [file ...]\n"
                         "       %s [-Q] -t file ...\n",                          "       %s [-Q] -t file ...\n",
                        __progname, __progname, __progname,                          progname, progname, progname, progname, progname);
                        __progname, __progname);  
   
         return (int)MANDOCLEVEL_BADARG;          return (int)MANDOCLEVEL_BADARG;
 }  }
Line 549  usage:
Line 583  usage:
  *   or   *   or
  *   [./]cat<section>[/<arch>]/<name>.0   *   [./]cat<section>[/<arch>]/<name>.0
  *   *
  * TODO: accomodate for multi-language directories.   * TODO: accommodate for multi-language directories.
  */   */
 static int  static int
 treescan(void)  treescan(void)
Line 1109  mpages_merge(struct mparse *mp)
Line 1143  mpages_merge(struct mparse *mp)
                 man = NULL;                  man = NULL;
                 sodest = NULL;                  sodest = NULL;
   
                 mparse_open(mp, &fd, mlink->file);                  if ((fd = mparse_open(mp, mlink->file)) == -1) {
                 if (fd == -1) {  
                         say(mlink->file, "&open");                          say(mlink->file, "&open");
                         goto nextpage;                          goto nextpage;
                 }                  }
Line 1121  mpages_merge(struct mparse *mp)
Line 1154  mpages_merge(struct mparse *mp)
                  */                   */
                 if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {                  if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) {
                         mparse_readfd(mp, fd, mlink->file);                          mparse_readfd(mp, fd, mlink->file);
                           close(fd);
                         mparse_result(mp, &man, &sodest);                          mparse_result(mp, &man, &sodest);
                 }                  }
   
Line 1176  mpages_merge(struct mparse *mp)
Line 1210  mpages_merge(struct mparse *mp)
                             mpage->arch == NULL ? "" : mpage->arch);                              mpage->arch == NULL ? "" : mpage->arch);
                         mpage->title = mandoc_strdup(man->meta.title);                          mpage->title = mandoc_strdup(man->meta.title);
                 } else if (man != NULL && man->macroset == MACROSET_MAN) {                  } else if (man != NULL && man->macroset == MACROSET_MAN) {
                           man_validate(man);
                         mpage->form = FORM_SRC;                          mpage->form = FORM_SRC;
                         mpage->sec = mandoc_strdup(man->meta.msec);                          mpage->sec = mandoc_strdup(man->meta.msec);
                         mpage->arch = mandoc_strdup(mlink->arch);                          mpage->arch = mandoc_strdup(mlink->arch);
Line 1268  parse_cat(struct mpage *mpage, int fd)
Line 1303  parse_cat(struct mpage *mpage, int fd)
 {  {
         FILE            *stream;          FILE            *stream;
         char            *line, *p, *title;          char            *line, *p, *title;
         size_t           len, plen, titlesz;          size_t           linesz, plen, titlesz;
           ssize_t          len;
           int              offs;
   
         stream = (-1 == fd) ?          stream = (-1 == fd) ?
             fopen(mpage->mlinks->file, "r") :              fopen(mpage->mlinks->file, "r") :
Line 1281  parse_cat(struct mpage *mpage, int fd)
Line 1318  parse_cat(struct mpage *mpage, int fd)
                 return;                  return;
         }          }
   
           line = NULL;
           linesz = 0;
   
         /* Skip to first blank line. */          /* Skip to first blank line. */
   
         while (NULL != (line = fgetln(stream, &len)))          while (getline(&line, &linesz, stream) != -1)
                 if ('\n' == *line)                  if (*line == '\n')
                         break;                          break;
   
         /*          /*
Line 1292  parse_cat(struct mpage *mpage, int fd)
Line 1332  parse_cat(struct mpage *mpage, int fd)
          * is the first section header.  Skip to it.           * is the first section header.  Skip to it.
          */           */
   
         while (NULL != (line = fgetln(stream, &len)))          while (getline(&line, &linesz, stream) != -1)
                 if ('\n' != *line && ' ' != *line)                  if (*line != '\n' && *line != ' ')
                         break;                          break;
   
         /*          /*
Line 1306  parse_cat(struct mpage *mpage, int fd)
Line 1346  parse_cat(struct mpage *mpage, int fd)
         titlesz = 0;          titlesz = 0;
         title = NULL;          title = NULL;
   
         while (NULL != (line = fgetln(stream, &len))) {          while ((len = getline(&line, &linesz, stream)) != -1) {
                 if (' ' != *line || '\n' != line[len - 1])                  if (*line != ' ')
                         break;                          break;
                 while (len > 0 && isspace((unsigned char)*line)) {                  offs = 0;
                         line++;                  while (isspace((unsigned char)line[offs]))
                         len--;                          offs++;
                 }                  if (line[offs] == '\0')
                 if (1 == len)  
                         continue;                          continue;
                 title = mandoc_realloc(title, titlesz + len);                  title = mandoc_realloc(title, titlesz + len - offs);
                 memcpy(title + titlesz, line, len);                  memcpy(title + titlesz, line + offs, len - offs);
                 titlesz += len;                  titlesz += len - offs;
                 title[titlesz - 1] = ' ';                  title[titlesz - 1] = ' ';
         }          }
           free(line);
   
         /*          /*
          * If no page content can be found, or the input line           * If no page content can be found, or the input line
Line 1337  parse_cat(struct mpage *mpage, int fd)
Line 1377  parse_cat(struct mpage *mpage, int fd)
                 return;                  return;
         }          }
   
         title = mandoc_realloc(title, titlesz + 1);          title[titlesz - 1] = '\0';
         title[titlesz] = '\0';  
   
         /*          /*
          * Skip to the first dash.           * Skip to the first dash.
Line 1416  parse_man(struct mpage *mpage, const struct roff_meta 
Line 1455  parse_man(struct mpage *mpage, const struct roff_meta 
         char             byte;          char             byte;
         size_t           sz;          size_t           sz;
   
         if (NULL == n)          if (n == NULL)
                 return;                  return;
   
         /*          /*
Line 1428  parse_man(struct mpage *mpage, const struct roff_meta 
Line 1467  parse_man(struct mpage *mpage, const struct roff_meta 
   
         if (n->type == ROFFT_BODY && n->tok == MAN_SH) {          if (n->type == ROFFT_BODY && n->tok == MAN_SH) {
                 body = n;                  body = n;
                 assert(body->parent);                  if ((head = body->parent->head) != NULL &&
                 if (NULL != (head = body->parent->head) &&                      (head = head->child) != NULL &&
                     1 == head->nchild &&                      head->next == NULL &&
                     NULL != (head = (head->child)) &&  
                     head->type == ROFFT_TEXT &&                      head->type == ROFFT_TEXT &&
                     0 == strcmp(head->string, "NAME") &&                      strcmp(head->string, "NAME") == 0 &&
                     NULL != body->child) {                      body->child != NULL) {
   
                         /*                          /*
                          * Suck the entire NAME section into memory.                           * Suck the entire NAME section into memory.
Line 1659  parse_mdoc_Fo(struct mpage *mpage, const struct roff_m
Line 1697  parse_mdoc_Fo(struct mpage *mpage, const struct roff_m
 }  }
   
 static int  static int
   parse_mdoc_Va(struct mpage *mpage, const struct roff_meta *meta,
           const struct roff_node *n)
   {
           char *cp;
   
           if (n->type != ROFFT_ELEM && n->type != ROFFT_BODY)
                   return 0;
   
           if (n->child != NULL &&
               n->child->next == NULL &&
               n->child->type == ROFFT_TEXT)
                   return 1;
   
           cp = NULL;
           deroff(&cp, n);
           if (cp != NULL) {
                   putkey(mpage, cp, TYPE_Vt | (n->tok == MDOC_Va ||
                       n->type == ROFFT_BODY ? TYPE_Va : 0));
                   free(cp);
           }
   
           return 0;
   }
   
   static int
 parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta,  parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta,
         const struct roff_node *n)          const struct roff_node *n)
 {  {
Line 1724  parse_mdoc_head(struct mpage *mpage, const struct roff
Line 1787  parse_mdoc_head(struct mpage *mpage, const struct roff
 {  {
   
         return n->type == ROFFT_HEAD;          return n->type == ROFFT_HEAD;
 }  
   
 static int  
 parse_mdoc_body(struct mpage *mpage, const struct roff_meta *meta,  
         const struct roff_node *n)  
 {  
   
         return n->type == ROFFT_BODY;  
 }  }
   
 /*  /*

Legend:
Removed from v.1.204  
changed lines
  Added in v.1.218

CVSweb