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

Diff for /mandoc/cgi.c between version 1.36 and 1.47

version 1.36, 2011/12/16 18:37:12 version 1.47, 2014/01/05 20:41:04
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  *   *
  * 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 18 
Line 18 
 #include "config.h"  #include "config.h"
 #endif  #endif
   
 #include <sys/param.h>  
 #include <sys/wait.h>  #include <sys/wait.h>
   
 #include <assert.h>  #include <assert.h>
Line 35 
Line 34 
 #include <string.h>  #include <string.h>
 #include <unistd.h>  #include <unistd.h>
   
   #if defined(__sun)
   /* for stat() */
   #include <fcntl.h>
   #include <sys/types.h>
   #include <sys/stat.h>
   #endif
   
 #include "apropos_db.h"  #include "apropos_db.h"
 #include "mandoc.h"  #include "mandoc.h"
 #include "mdoc.h"  #include "mdoc.h"
 #include "man.h"  #include "man.h"
 #include "main.h"  #include "main.h"
 #include "manpath.h"  #include "manpath.h"
   #include "mandocdb.h"
   
 #ifdef __linux__  #if defined(__linux__) || defined(__sun)
 # include <db_185.h>  # include <db_185.h>
 #else  #else
 # include <db.h>  # include <db.h>
Line 68  struct query {
Line 75  struct query {
         const char      *sec; /* manual section */          const char      *sec; /* manual section */
         const char      *expr; /* unparsed expression string */          const char      *expr; /* unparsed expression string */
         int              manroot; /* manroot index (or -1)*/          int              manroot; /* manroot index (or -1)*/
         int              whatis; /* whether whatis mode */  
         int              legacy; /* whether legacy mode */          int              legacy; /* whether legacy mode */
 };  };
   
Line 134  atou(const char *buf, unsigned *v)
Line 140  atou(const char *buf, unsigned *v)
                 return(0);                  return(0);
         if ((errno == ERANGE && (lval == LONG_MAX ||          if ((errno == ERANGE && (lval == LONG_MAX ||
                                         lval == LONG_MIN)) ||                                          lval == LONG_MIN)) ||
                         (lval > UINT_MAX || lval < 0))                          (lval > INT_MAX || lval < 0))
                 return(0);                  return(0);
   
         *v = (unsigned int)lval;          *v = (unsigned int)lval;
Line 229  http_parse(struct req *req, char *p)
Line 235  http_parse(struct req *req, char *p)
   
         memset(&req->q, 0, sizeof(struct query));          memset(&req->q, 0, sizeof(struct query));
   
         req->q.whatis = 1;  
         legacy = -1;          legacy = -1;
         manroot = NULL;          manroot = NULL;
   
Line 267  http_parse(struct req *req, char *p)
Line 272  http_parse(struct req *req, char *p)
                         manroot = val;                          manroot = val;
                 else if (0 == strcmp(key, "apropos"))                  else if (0 == strcmp(key, "apropos"))
                         legacy = 0 == strcmp(val, "0");                          legacy = 0 == strcmp(val, "0");
                 else if (0 == strcmp(key, "op"))  
                         req->q.whatis = 0 == strcasecmp(val, "whatis");  
         }          }
   
         /* Test for old man.cgi compatibility mode. */          /* Test for old man.cgi compatibility mode. */
   
         if (legacy == 0) {          req->q.legacy = legacy > 0;
                 req->q.whatis = 0;  
                 req->q.legacy = 1;  
         } else if (legacy > 0) {  
                 req->q.legacy = 1;  
                 req->q.whatis = 1;  
         }  
   
         /*          /*
          * Section "0" means no section when in legacy mode.           * Section "0" means no section when in legacy mode.
Line 407  resp_searchform(const struct req *req)
Line 404  resp_searchform(const struct req *req)
                "<FORM ACTION=\"%s/search.html\" METHOD=\"get\">\n"                 "<FORM ACTION=\"%s/search.html\" METHOD=\"get\">\n"
                "<FIELDSET>\n"                 "<FIELDSET>\n"
                "<LEGEND>Search Parameters</LEGEND>\n"                 "<LEGEND>Search Parameters</LEGEND>\n"
                "<INPUT TYPE=\"submit\" NAME=\"op\""                 "<INPUT TYPE=\"submit\" "
                " VALUE=\"Whatis\"> or \n"                 " VALUE=\"Search\"> for manuals satisfying \n"
                "<INPUT TYPE=\"submit\" NAME=\"op\""  
                " VALUE=\"apropos\"> for manuals satisfying \n"  
                "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",                 "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
                progname);                 progname);
         html_print(req->q.expr ? req->q.expr : "");          html_print(req->q.expr ? req->q.expr : "");
Line 506  resp_baddb(void)
Line 501  resp_baddb(void)
 static void  static void
 resp_search(struct res *r, size_t sz, void *arg)  resp_search(struct res *r, size_t sz, void *arg)
 {  {
         int               i;          size_t           i, matched;
         const struct req *req;          const struct req *req;
   
         req = (const struct req *)arg;          req = (const struct req *)arg;
         assert(req->q.manroot >= 0);  
           if (sz > 0)
                   assert(req->q.manroot >= 0);
   
           for (matched = i = 0; i < sz; i++)
                   if (r[i].matched)
                           matched++;
   
         if (1 == sz) {          if (1 == matched) {
                   for (i = 0; i < sz; i++)
                           if (r[i].matched)
                                   break;
                 /*                  /*
                  * If we have just one result, then jump there now                   * If we have just one result, then jump there now
                  * without any delay.                   * without any delay.
Line 520  resp_search(struct res *r, size_t sz, void *arg)
Line 524  resp_search(struct res *r, size_t sz, void *arg)
                 puts("Status: 303 See Other");                  puts("Status: 303 See Other");
                 printf("Location: http://%s%s/show/%d/%u/%u.html?",                  printf("Location: http://%s%s/show/%d/%u/%u.html?",
                                 host, progname, req->q.manroot,                                  host, progname, req->q.manroot,
                                 r[0].volume, r[0].rec);                                  r[i].volume, r[i].rec);
                 http_printquery(req);                  http_printquery(req);
                 puts("\n"                  puts("\n"
                      "Content-Type: text/html; charset=utf-8\n");                       "Content-Type: text/html; charset=utf-8\n");
                 return;                  return;
         }          }
   
         qsort(r, sz, sizeof(struct res), cmp);  
   
         resp_begin_html(200, NULL);          resp_begin_html(200, NULL);
         resp_searchform(req);          resp_searchform(req);
   
         puts("<DIV CLASS=\"results\">");          puts("<DIV CLASS=\"results\">");
   
         if (0 == sz) {          if (0 == matched) {
                 printf("<P>\n"                  puts("<P>\n"
                        "No %s results found.\n",                       "No results found.\n"
                        req->q.whatis ? "whatis" : "apropos");                       "</P>\n"
                 if (req->q.whatis) {                       "</DIV>");
                         printf("(Try "  
                                "<A HREF=\"%s/search.html?op=apropos",  
                                progname);  
                         html_printquery(req);  
                         puts("\">apropos</A>?)");  
                 }  
                 puts("</P>");  
                 puts("</DIV>");  
                 resp_end_html();                  resp_end_html();
                 return;                  return;
         }          }
   
           qsort(r, sz, sizeof(struct res), cmp);
   
         puts("<TABLE>");          puts("<TABLE>");
   
         for (i = 0; i < (int)sz; i++) {          for (i = 0; i < sz; i++) {
                   if ( ! r[i].matched)
                           continue;
                 printf("<TR>\n"                  printf("<TR>\n"
                        "<TD CLASS=\"title\">\n"                         "<TD CLASS=\"title\">\n"
                        "<A HREF=\"%s/show/%d/%u/%u.html?",                         "<A HREF=\"%s/show/%d/%u/%u.html?",
Line 734  format(const struct req *req, const char *file)
Line 732  format(const struct req *req, const char *file)
         struct man      *man;          struct man      *man;
         void            *vp;          void            *vp;
         enum mandoclevel rc;          enum mandoclevel rc;
         char             opts[MAXPATHLEN + 128];          char             opts[PATH_MAX + 128];
   
         if (-1 == (fd = open(file, O_RDONLY, 0))) {          if (-1 == (fd = open(file, O_RDONLY, 0))) {
                 resp_baddb();                  resp_baddb();
                 return;                  return;
         }          }
   
         mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);          mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL, 0);
         rc = mparse_readfd(mp, fd, file);          rc = mparse_readfd(mp, fd, file);
         close(fd);          close(fd);
   
Line 751  format(const struct req *req, const char *file)
Line 749  format(const struct req *req, const char *file)
         }          }
   
         snprintf(opts, sizeof(opts), "fragment,"          snprintf(opts, sizeof(opts), "fragment,"
                         "man=%s/search.html?sec=%%S&expr=%%N,"                          "man=%s/search.html?sec=%%S&expr=Nm~^%%N$,"
                         /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,                          /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
                         progname);                          progname);
   
Line 785  pg_show(const struct req *req, char *path)
Line 783  pg_show(const struct req *req, char *path)
         struct manpaths  ps;          struct manpaths  ps;
         size_t           sz;          size_t           sz;
         char            *sub;          char            *sub;
         char             file[MAXPATHLEN];          char             file[PATH_MAX];
         const char      *cp;          const char      *cp;
         int              rc, catm;          int              rc, catm;
         unsigned int     vol, rec, mr;          unsigned int     vol, rec, mr;
Line 839  pg_show(const struct req *req, char *path)
Line 837  pg_show(const struct req *req, char *path)
                 goto out;                  goto out;
         }          }
   
         sz = strlcpy(file, ps.paths[vol], MAXPATHLEN);          sz = strlcpy(file, ps.paths[vol], PATH_MAX);
         assert(sz < MAXPATHLEN);          assert(sz < PATH_MAX);
         strlcat(file, "/mandoc.index", MAXPATHLEN);          strlcat(file, "/", PATH_MAX);
           strlcat(file, MANDOC_IDX, PATH_MAX);
   
         /* Open the index recno(3) database. */          /* Open the index recno(3) database. */
   
Line 870  pg_show(const struct req *req, char *path)
Line 869  pg_show(const struct req *req, char *path)
                 resp_baddb();                  resp_baddb();
         else {          else {
                 file[(int)sz] = '\0';                  file[(int)sz] = '\0';
                 strlcat(file, "/", MAXPATHLEN);                  strlcat(file, "/", PATH_MAX);
                 strlcat(file, cp, MAXPATHLEN);                  strlcat(file, cp, PATH_MAX);
                 if (catm)                  if (catm)
                         catman(req, file);                          catman(req, file);
                 else                  else
Line 886  out:
Line 885  out:
 static void  static void
 pg_search(const struct req *req, char *path)  pg_search(const struct req *req, char *path)
 {  {
         size_t            tt;          size_t            tt, ressz;
         struct manpaths   ps;          struct manpaths   ps;
         int               i, sz, rc;          int               i, sz, rc;
         const char       *ep, *start;          const char       *ep, *start;
           struct res      *res;
         char            **cp;          char            **cp;
         struct opts       opt;          struct opts       opt;
         struct expr      *expr;          struct expr      *expr;
Line 907  pg_search(const struct req *req, char *path)
Line 907  pg_search(const struct req *req, char *path)
         rc       = -1;          rc       = -1;
         sz       = 0;          sz       = 0;
         cp       = NULL;          cp       = NULL;
           ressz    = 0;
           res      = NULL;
   
         /*          /*
          * Begin by chdir()ing into the root of the manpath.           * Begin by chdir()ing into the root of the manpath.
Line 949  pg_search(const struct req *req, char *path)
Line 951  pg_search(const struct req *req, char *path)
          * The resp_search() function is called with the results.           * The resp_search() function is called with the results.
          */           */
   
         expr = req->q.whatis ?          expr = req->q.legacy ?
                 termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt);                  termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt);
   
         if (NULL != expr)          if (NULL != expr)
                 rc = apropos_search                  rc = apropos_search
                         (ps.sz, ps.paths, &opt,                          (ps.sz, ps.paths, &opt, expr, tt,
                          expr, tt, (void *)req, resp_search);                           (void *)req, &ressz, &res, resp_search);
   
         /* ...unless errors occured. */          /* ...unless errors occured. */
   
         if (0 == rc)          if (0 == rc)
                 resp_baddb();                  resp_baddb();
         else if (-1 == rc)          else if (-1 == rc)
                 resp_search(NULL, 0, (void *)req);                  resp_search(NULL, 0, NULL);
   
         for (i = 0; i < sz; i++)          for (i = 0; i < sz; i++)
                 free(cp[i]);                  free(cp[i]);
   
         free(cp);          free(cp);
           resfree(res, ressz);
         exprfree(expr);          exprfree(expr);
         manpath_free(&ps);          manpath_free(&ps);
 }  }
Line 976  int
Line 979  int
 main(void)  main(void)
 {  {
         int              i;          int              i;
         char             buf[MAXPATHLEN];          char             buf[PATH_MAX];
         DIR             *cwd;          DIR             *cwd;
         struct req       req;          struct req       req;
         char            *p, *path, *subpath;          char            *p, *path, *subpath;
Line 1013  main(void)
Line 1016  main(void)
   
         memset(&req, 0, sizeof(struct req));          memset(&req, 0, sizeof(struct req));
   
         strlcpy(buf, ".", MAXPATHLEN);          strlcpy(buf, ".", PATH_MAX);
         pathgen(cwd, buf, &req);          pathgen(cwd, buf, &req);
         closedir(cwd);          closedir(cwd);
   
Line 1101  static int
Line 1104  static int
 pathstop(DIR *dir)  pathstop(DIR *dir)
 {  {
         struct dirent   *d;          struct dirent   *d;
   #if defined(__sun)
           struct stat      sb;
   #endif
   
         while (NULL != (d = readdir(dir)))          while (NULL != (d = readdir(dir))) {
   #if defined(__sun)
                   stat(d->d_name, &sb);
                   if (S_IFREG & sb.st_mode)
   #else
                 if (DT_REG == d->d_type)                  if (DT_REG == d->d_type)
   #endif
                         if (0 == strcmp(d->d_name, "catman.conf"))                          if (0 == strcmp(d->d_name, "catman.conf"))
                                 return(1);                                  return(1);
     }
   
         return(0);          return(0);
 }  }
Line 1122  pathgen(DIR *dir, char *path, struct req *req)
Line 1134  pathgen(DIR *dir, char *path, struct req *req)
         DIR             *cd;          DIR             *cd;
         int              rc;          int              rc;
         size_t           sz, ssz;          size_t           sz, ssz;
   #if defined(__sun)
           struct stat      sb;
   #endif
   
         sz = strlcat(path, "/", MAXPATHLEN);          sz = strlcat(path, "/", PATH_MAX);
         if (sz >= MAXPATHLEN) {          if (sz >= PATH_MAX) {
                 fprintf(stderr, "%s: Path too long", path);                  fprintf(stderr, "%s: Path too long", path);
                 return;                  return;
         }          }
Line 1137  pathgen(DIR *dir, char *path, struct req *req)
Line 1152  pathgen(DIR *dir, char *path, struct req *req)
   
         rc = 0;          rc = 0;
         while (0 == rc && NULL != (d = readdir(dir))) {          while (0 == rc && NULL != (d = readdir(dir))) {
                 if (DT_DIR != d->d_type || strcmp(d->d_name, "etc"))  #if defined(__sun)
                   stat(d->d_name, &sb);
                   if (!(S_IFDIR & sb.st_mode)
   #else
                   if (DT_DIR != d->d_type
   #endif
           || strcmp(d->d_name, "etc"))
                         continue;                          continue;
   
                 path[(int)sz] = '\0';                  path[(int)sz] = '\0';
                 ssz = strlcat(path, d->d_name, MAXPATHLEN);                  ssz = strlcat(path, d->d_name, PATH_MAX);
   
                 if (ssz >= MAXPATHLEN) {                  if (ssz >= PATH_MAX) {
                         fprintf(stderr, "%s: Path too long", path);                          fprintf(stderr, "%s: Path too long", path);
                         return;                          return;
                 } else if (NULL == (cd = opendir(path))) {                  } else if (NULL == (cd = opendir(path))) {
Line 1186  pathgen(DIR *dir, char *path, struct req *req)
Line 1207  pathgen(DIR *dir, char *path, struct req *req)
   
         rewinddir(dir);          rewinddir(dir);
         while (NULL != (d = readdir(dir))) {          while (NULL != (d = readdir(dir))) {
                 if (DT_DIR != d->d_type || '.' == d->d_name[0])  #if defined(__sun)
                   stat(d->d_name, &sb);
                   if (!(S_IFDIR & sb.st_mode)
   #else
                   if (DT_DIR != d->d_type
   #endif
           || '.' == d->d_name[0])
                         continue;                          continue;
   
                 path[(int)sz] = '\0';                  path[(int)sz] = '\0';
                 ssz = strlcat(path, d->d_name, MAXPATHLEN);                  ssz = strlcat(path, d->d_name, PATH_MAX);
   
                 if (ssz >= MAXPATHLEN) {                  if (ssz >= PATH_MAX) {
                         fprintf(stderr, "%s: Path too long", path);                          fprintf(stderr, "%s: Path too long", path);
                         return;                          return;
                 } else if (NULL == (cd = opendir(path))) {                  } else if (NULL == (cd = opendir(path))) {

Legend:
Removed from v.1.36  
changed lines
  Added in v.1.47

CVSweb