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

Diff for /mandoc/catman.c between version 1.2 and 1.4

version 1.2, 2011/11/27 11:46:44 version 1.4, 2011/12/08 00:20:52
Line 51 
Line 51 
                 exit(EXIT_FAILURE); \                  exit(EXIT_FAILURE); \
         } while (/* CONSTCOND */0)          } while (/* CONSTCOND */0)
   
 static  int              indexhtml(char *);  static  int              indexhtml(char *, char *);
 #if 0  static  int              manup(const struct manpaths *, char *);
 static  int              jobstart(const char *, const char *, pid_t *);  
 static  int              jobwait(pid_t);  
 #endif  
 static  int              manup(const struct manpaths *, const char *);  
 static  int              mkpath(char *, mode_t, mode_t);  static  int              mkpath(char *, mode_t, mode_t);
 static  int              treecpy(char *, char *);  static  int              treecpy(char *, char *, char *);
 static  int              update(char *, char *);  static  int              update(char *, char *, char *);
 static  void             usage(void);  static  void             usage(void);
   
 static  const char      *progname;  static  const char      *progname;
Line 71  main(int argc, char *argv[])
Line 67  main(int argc, char *argv[])
 {  {
         int              ch;          int              ch;
         char            *aux, *base;          char            *aux, *base;
         const char      *dir;  
         struct manpaths  dirs;          struct manpaths  dirs;
           char             buf[MAXPATHLEN];
         extern char     *optarg;          extern char     *optarg;
         extern int       optind;          extern int       optind;
   
Line 83  main(int argc, char *argv[])
Line 79  main(int argc, char *argv[])
                 ++progname;                  ++progname;
   
         aux = base = NULL;          aux = base = NULL;
         dir = "/var/www/cache/man.cgi";          xstrlcpy(buf, "/var/www/cache/man.cgi", MAXPATHLEN);
   
         while (-1 != (ch = getopt(argc, argv, "fm:M:o:v")))          while (-1 != (ch = getopt(argc, argv, "fm:M:o:v")))
                 switch (ch) {                  switch (ch) {
Line 97  main(int argc, char *argv[])
Line 93  main(int argc, char *argv[])
                         base = optarg;                          base = optarg;
                         break;                          break;
                 case ('o'):                  case ('o'):
                         dir = optarg;                          xstrlcpy(buf, optarg, MAXPATHLEN);
                         break;                          break;
                 case ('v'):                  case ('v'):
                         verbose++;                          verbose++;
Line 117  main(int argc, char *argv[])
Line 113  main(int argc, char *argv[])
   
         memset(&dirs, 0, sizeof(struct manpaths));          memset(&dirs, 0, sizeof(struct manpaths));
         manpath_parse(&dirs, base, aux);          manpath_parse(&dirs, base, aux);
         ch = manup(&dirs, dir);          ch = manup(&dirs, buf);
         manpath_free(&dirs);          manpath_free(&dirs);
         return(ch ? EXIT_SUCCESS : EXIT_FAILURE);          return(ch ? EXIT_SUCCESS : EXIT_FAILURE);
 }  }
Line 196  out:
Line 192  out:
         return(rc);          return(rc);
 }  }
   
 #if 0  
 /*  /*
  * Clean up existing child.  
  * Return 1 if cleaned up fine (or none was started) and 0 otherwise.  
  */  
 static int  
 jobwait(pid_t pid)  
 {  
         int              st;  
   
         if (-1 == pid)  
                 return(1);  
   
         if (-1 == waitpid(pid, &st, 0)) {  
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
   
         return(WIFEXITED(st) && 0 == WEXITSTATUS(st));  
 }  
   
 /*  
  * Start a job (child process), first making sure that the prior one has  
  * finished.  
  * Return 1 if the prior child exited and the new one started, else 0.  
  */  
 static int  
 jobstart(const char *dst, const char *src, pid_t *pid)  
 {  
         int              fd;  
   
         if ( ! jobwait(*pid))  
                 return(0);  
   
         if (-1 == (*pid = fork())) {  
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         } else if (*pid > 0)  
                 return(1);  
   
         if (-1 == (fd = open(dst, O_WRONLY|O_TRUNC|O_CREAT, 0644))) {  
                 perror(dst);  
                 exit(EXIT_FAILURE);  
         }  
   
         if (-1 == dup2(fd, STDOUT_FILENO)) {  
                 perror(NULL);  
                 exit(EXIT_FAILURE);  
         }  
   
         execlp("mandoc", "mandoc", "-T", "html",  
                         "-O", "fragment",  
                         "-O", "man=man.cgi?expr=%N&sec=%S",  
                         src, (char *)NULL);  
   
         perror("mandoc");  
         exit(EXIT_FAILURE);  
         /* NOTREACHED */  
 }  
 #endif  
   
 /*  
  * Pass over the recno database and re-create HTML pages if they're   * Pass over the recno database and re-create HTML pages if they're
  * found to be out of date.   * found to be out of date.
  * Returns -1 on fatal error, 1 on success.   * Returns -1 on fatal error, 1 on success.
  */   */
 static int  static int
 indexhtml(char *dst)  indexhtml(char *base, char *dst)
 {  {
         DB              *db;          DB              *idx;
         DBT              key, val;          DBT              key, val;
         size_t           sz;          size_t           sz;
         int              c, rc;          int              c, rc;
         unsigned int     fl;          unsigned int     fl;
         const char      *f;          const char      *f, *cp;
         char            *d;          char            *d;
         char             fname[MAXPATHLEN];          char             fname[MAXPATHLEN];
         pid_t            pid;          pid_t            pid;
   
         sz = strlen(dst);          sz = strlen(base);
         pid = -1;          pid = -1;
   
         xstrlcpy(fname, dst, MAXPATHLEN);          xstrlcpy(fname, dst, MAXPATHLEN);
         xstrlcat(fname, "/mandoc.index", MAXPATHLEN);          xstrlcat(fname, "/mandoc.index", MAXPATHLEN);
   
         db = dbopen(fname, O_RDONLY, 0, DB_RECNO, NULL);          idx = dbopen(fname, O_RDONLY, 0, DB_RECNO, NULL);
         if (NULL == db) {          if (NULL == idx) {
                 perror(fname);                  perror(fname);
                 return(-1);                  return(-1);
         }          }
   
         fl = R_FIRST;          fl = R_FIRST;
         while (0 == (c = (*db->seq)(db, &key, &val, fl))) {          while (0 == (c = (*idx->seq)(idx, &key, &val, fl))) {
                 fl = R_NEXT;                  fl = R_NEXT;
                 f = (const char *)val.data;                  cp = (const char *)val.data;
                   if (0 == val.size)
                           continue;
                   if (NULL == (f = memchr(cp, '\0', val.size)))
                           break;
                   if (++f - cp >= (int)val.size)
                           break;
                   if (NULL == memchr(f, '\0', val.size - (f - cp)))
                           break;
   
                 dst[(int)sz] = '\0';                  base[(int)sz] = '\0';
   
                 xstrlcat(dst, "/", MAXPATHLEN);                  xstrlcat(base, "/", MAXPATHLEN);
                 xstrlcat(dst, f, MAXPATHLEN);                  xstrlcat(base, f, MAXPATHLEN);
                 /*xstrlcat(dst, ".html", MAXPATHLEN);*/  
   
                 if (-1 == (rc = isnewer(dst, f))) {                  if (-1 == (rc = isnewer(base, f))) {
                         fprintf(stderr, "%s: Manpage missing\n", f);                          fprintf(stderr, "%s: File missing\n", f);
                         break;                          break;
                 } else if (0 == rc)                  } else if (0 == rc)
                         continue;                          continue;
   
                 d = strrchr(dst, '/');                  d = strrchr(base, '/');
                 assert(NULL != d);                  assert(NULL != d);
                 *d = '\0';                  *d = '\0';
   
                 if (-1 == mkpath(dst, 0755, 0755)) {                  if (-1 == mkpath(base, 0755, 0755)) {
                         perror(dst);                          perror(base);
                         break;                          break;
                 }                  }
   
                 *d = '/';                  *d = '/';
   
                 if ( ! filecpy(dst, f))                  if ( ! filecpy(base, f))
                         break;                          break;
   
                 /*if ( ! jobstart(dst, f, &pid))  
                         break;*/  
                 if (verbose)                  if (verbose)
                         printf("%s\n", dst);                          printf("%s\n", base);
         }          }
   
         (*db->close)(db);          (*idx->close)(idx);
   
         if (c < 0)          if (c < 0)
                 perror(fname);                  perror(fname);
         /*if ( ! jobwait(pid))          else if (0 == c)
                 c = -1;*/                  fprintf(stderr, "%s: Corrupt index\n", fname);
   
         return(1 == c ? 1 : -1);          return(1 == c ? 1 : -1);
 }  }
Line 340  indexhtml(char *dst)
Line 279  indexhtml(char *dst)
  * Return -1 on fatal error and 1 if the update went well.   * Return -1 on fatal error and 1 if the update went well.
  */   */
 static int  static int
 update(char *dst, char *src)  update(char *base, char *dst, char *src)
 {  {
         size_t           dsz, ssz;          size_t           dsz, ssz;
   
Line 367  update(char *dst, char *src)
Line 306  update(char *dst, char *src)
   
         dst[(int)dsz] = '\0';          dst[(int)dsz] = '\0';
   
         return(indexhtml(dst));          return(indexhtml(base, dst));
 }  }
   
 /*  /*
Line 377  update(char *dst, char *src)
Line 316  update(char *dst, char *src)
  * shouldn't be listed), and 1 if the update went well.   * shouldn't be listed), and 1 if the update went well.
  */   */
 static int  static int
 treecpy(char *dst, char *src)  treecpy(char *base, char *dst, char *src)
 {  {
         size_t           dsz, ssz;          size_t           dsz, ssz;
         int              rc;          int              rc;
Line 394  treecpy(char *dst, char *src)
Line 333  treecpy(char *dst, char *src)
         dst[(int)dsz] = src[(int)ssz] = '\0';          dst[(int)dsz] = src[(int)ssz] = '\0';
   
         if (1 == rc)          if (1 == rc)
                 return(update(dst, src));                  return(update(base, dst, src));
   
         xstrlcat(src, "/mandoc.db", MAXPATHLEN);          xstrlcat(src, "/mandoc.db", MAXPATHLEN);
         xstrlcat(dst, "/mandoc.db", MAXPATHLEN);          xstrlcat(dst, "/mandoc.db", MAXPATHLEN);
Line 406  treecpy(char *dst, char *src)
Line 345  treecpy(char *dst, char *src)
   
         dst[(int)dsz] = src[(int)ssz] = '\0';          dst[(int)dsz] = src[(int)ssz] = '\0';
   
         return(update(dst, src));          return(update(base, dst, src));
 }  }
   
 /*  /*
Line 416  treecpy(char *dst, char *src)
Line 355  treecpy(char *dst, char *src)
  * Returns 1 on success, 0 on failure.   * Returns 1 on success, 0 on failure.
  */   */
 static int  static int
 manup(const struct manpaths *dirs, const char *dir)  manup(const struct manpaths *dirs, char *base)
 {  {
         char             dst[MAXPATHLEN],          char             dst[MAXPATHLEN],
                          src[MAXPATHLEN];                           src[MAXPATHLEN];
Line 425  manup(const struct manpaths *dirs, const char *dir)
Line 364  manup(const struct manpaths *dirs, const char *dir)
         size_t           sz;          size_t           sz;
         FILE            *f;          FILE            *f;
   
         xstrlcpy(dst, dir, MAXPATHLEN);          /* Create the path and file for the catman.conf file. */
         xstrlcat(dst, "/etc", MAXPATHLEN);  
   
           sz = strlen(base);
           xstrlcpy(dst, base, MAXPATHLEN);
           xstrlcat(dst, "/etc", MAXPATHLEN);
         if (-1 == mkpath(dst, 0755, 0755)) {          if (-1 == mkpath(dst, 0755, 0755)) {
                 perror(dst);                  perror(dst);
                 return(0);                  return(0);
         }          }
   
         xstrlcat(dst, "/man.conf", MAXPATHLEN);          xstrlcat(dst, "/catman.conf", MAXPATHLEN);
   
         if (verbose)  
                 printf("%s\n", dst);  
   
         if (NULL == (f = fopen(dst, "w"))) {          if (NULL == (f = fopen(dst, "w"))) {
                 perror(dst);                  perror(dst);
                 return(0);                  return(0);
         }          } else if (verbose)
                   printf("%s\n", dst);
   
         xstrlcpy(dst, dir, MAXPATHLEN);  
         sz = strlen(dst);  
   
         for (i = 0; i < dirs->sz; i++) {          for (i = 0; i < dirs->sz; i++) {
                 path = dirs->paths[i];                  path = dirs->paths[i];
                   dst[(int)sz] = base[(int)sz] = '\0';
                 dst[(int)sz] = '\0';  
                 xstrlcat(dst, path, MAXPATHLEN);                  xstrlcat(dst, path, MAXPATHLEN);
   
                 if (-1 == mkpath(dst, 0755, 0755)) {                  if (-1 == mkpath(dst, 0755, 0755)) {
                         perror(dst);                          perror(dst);
                         break;                          break;
                 }                  }
   
                 xstrlcpy(src, path, MAXPATHLEN);                  xstrlcpy(src, path, MAXPATHLEN);
                   if (-1 == (c = treecpy(base, dst, src)))
                 if (-1 == (c = treecpy(dst, src)))  
                         break;                          break;
                 else if (0 == c)                  else if (0 == c)
                         continue;                          continue;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.4

CVSweb