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

Diff for /mandoc/cgi.c between version 1.57 and 1.58

version 1.57, 2014/07/09 12:09:04 version 1.58, 2014/07/09 14:18:59
Line 20 
Line 20 
 #endif  #endif
   
 #include <ctype.h>  #include <ctype.h>
   #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <limits.h>  #include <limits.h>
 #include <stdio.h>  #include <stdio.h>
Line 44  enum page {
Line 45  enum page {
  * A query as passed to the search function.   * A query as passed to the search function.
  */   */
 struct  query {  struct  query {
         const char      *manroot; /* manual root directory */          const char      *manpath; /* desired manual directory */
         const char      *arch; /* architecture */          const char      *arch; /* architecture */
         const char      *sec; /* manual section */          const char      *sec; /* manual section */
         const char      *expr; /* unparsed expression string */          const char      *expr; /* unparsed expression string */
Line 52  struct query {
Line 53  struct query {
 };  };
   
 struct  req {  struct  req {
         struct query     q;          struct query      q;
         char            **p; /* array of available manroots */          char            **p; /* array of available manpaths */
         size_t           psz; /* number of available manroots */          size_t            psz; /* number of available manpaths */
         enum page        page;          enum page         page;
 };  };
   
 static  void             catman(const struct req *, const char *);  static  void             catman(const struct req *, const char *);
Line 85  static void   resp_search(const struct req *,
Line 86  static void   resp_search(const struct req *,
                                 struct manpage *, size_t);                                  struct manpage *, size_t);
 static  void             resp_searchform(const struct req *);  static  void             resp_searchform(const struct req *);
   
 static  const char       *progname; /* cgi script name */  static  const char       *scriptname; /* CGI script name */
 static  const char       *cache; /* cache directory */  static  const char       *mandir; /* contains all manpath directories */
 static  const char       *css; /* css directory */  static  const char       *cssdir; /* css directory */
 static  const char       *host; /* hostname */  static  const char       *httphost; /* hostname used in the URIs */
   
 static  const char * const pages[PAGE__MAX] = {  static  const char * const pages[PAGE__MAX] = {
         "index", /* PAGE_INDEX */          "index", /* PAGE_INDEX */
Line 127  static void
Line 128  static void
 http_printquery(const struct req *req)  http_printquery(const struct req *req)
 {  {
   
         if (NULL != req->q.manroot) {          if (NULL != req->q.manpath) {
                 printf("&manpath=");                  printf("&manpath=");
                 http_print(req->q.manroot);                  http_print(req->q.manpath);
         }          }
         if (NULL != req->q.sec) {          if (NULL != req->q.sec) {
                 printf("&sec=");                  printf("&sec=");
Line 149  static void
Line 150  static void
 html_printquery(const struct req *req)  html_printquery(const struct req *req)
 {  {
   
         if (NULL != req->q.manroot) {          if (NULL != req->q.manpath) {
                 printf("&amp;manpath=");                  printf("&amp;manpath=");
                 html_print(req->q.manroot);                  html_print(req->q.manpath);
         }          }
         if (NULL != req->q.sec) {          if (NULL != req->q.sec) {
                 printf("&amp;sec=");                  printf("&amp;sec=");
Line 203  http_parse(struct req *req, char *p)
Line 204  http_parse(struct req *req, char *p)
         int              legacy;          int              legacy;
   
         memset(&req->q, 0, sizeof(struct query));          memset(&req->q, 0, sizeof(struct query));
         req->q.manroot = req->p[0];          req->q.manpath = req->p[0];
   
         legacy = -1;          legacy = -1;
         while ('\0' != *p) {          while ('\0' != *p) {
Line 237  http_parse(struct req *req, char *p)
Line 238  http_parse(struct req *req, char *p)
                 else if (0 == strcmp(key, "arch"))                  else if (0 == strcmp(key, "arch"))
                         req->q.arch = val;                          req->q.arch = val;
                 else if (0 == strcmp(key, "manpath"))                  else if (0 == strcmp(key, "manpath"))
                         req->q.manroot = val;                          req->q.manpath = val;
                 else if (0 == strcmp(key, "apropos"))                  else if (0 == strcmp(key, "apropos"))
                         legacy = 0 == strcmp(val, "0");                          legacy = 0 == strcmp(val, "0");
         }          }
Line 342  resp_begin_html(int code, const char *msg)
Line 343  resp_begin_html(int code, const char *msg)
                "<TITLE>System Manpage Reference</TITLE>\n"                 "<TITLE>System Manpage Reference</TITLE>\n"
                "</HEAD>\n"                 "</HEAD>\n"
                "<BODY>\n"                 "<BODY>\n"
                "<!-- Begin page content. //-->\n", css, css);                 "<!-- Begin page content. //-->\n",
                  cssdir, cssdir);
 }  }
   
 static void  static void
Line 366  resp_searchform(const struct req *req)
Line 368  resp_searchform(const struct req *req)
                "<INPUT TYPE=\"submit\" "                 "<INPUT TYPE=\"submit\" "
                " VALUE=\"Search\"> for manuals satisfying \n"                 " VALUE=\"Search\"> for manuals satisfying \n"
                "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",                 "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
                progname);                 scriptname);
         html_print(req->q.expr ? req->q.expr : "");          html_print(req->q.expr ? req->q.expr : "");
         printf("\">, section "          printf("\">, section "
                "<INPUT TYPE=\"text\""                 "<INPUT TYPE=\"text\""
Line 381  resp_searchform(const struct req *req)
Line 383  resp_searchform(const struct req *req)
                 puts(", <SELECT NAME=\"manpath\">");                  puts(", <SELECT NAME=\"manpath\">");
                 for (i = 0; i < (int)req->psz; i++) {                  for (i = 0; i < (int)req->psz; i++) {
                         printf("<OPTION ");                          printf("<OPTION ");
                         if (NULL == req->q.manroot ? 0 == i :                          if (NULL == req->q.manpath ? 0 == i :
                             0 == strcmp(req->q.manroot, req->p[i]))                              0 == strcmp(req->q.manpath, req->p[i]))
                                 printf("SELECTED=\"selected\" ");                                  printf("SELECTED=\"selected\" ");
                         printf("VALUE=\"");                          printf("VALUE=\"");
                         html_print(req->p[i]);                          html_print(req->p[i]);
Line 419  resp_error400(void)
Line 421  resp_error400(void)
                "The query your entered was malformed.\n"                 "The query your entered was malformed.\n"
                "Try again from the\n"                 "Try again from the\n"
                "<A HREF=\"%s/index.html\">main page</A>.\n"                 "<A HREF=\"%s/index.html\">main page</A>.\n"
                "</P>", progname);                 "</P>", scriptname);
         resp_end_html();          resp_end_html();
 }  }
   
Line 436  resp_error404(const char *page)
Line 438  resp_error404(const char *page)
         printf("</B>,\n"          printf("</B>,\n"
                "could not be found.\n"                 "could not be found.\n"
                "Try searching from the\n"                 "Try searching from the\n"
                "<A HREF=\"%s/index.html\">main page</A>.\n"                 "<A HREF=\"%s\">main page</A>.\n"
                "</P>", progname);                 "</P>", scriptname);
         resp_end_html();          resp_end_html();
 }  }
   
Line 445  static void
Line 447  static void
 resp_bad(void)  resp_bad(void)
 {  {
         resp_begin_html(500, "Internal Server Error");          resp_begin_html(500, "Internal Server Error");
         puts("<P>Generic badness happened.</P>");          puts("<P>Internal Server Error</P>");
         resp_end_html();          resp_end_html();
 }  }
   
Line 470  resp_search(const struct req *req, struct manpage *r, 
Line 472  resp_search(const struct req *req, struct manpage *r, 
                  */                   */
                 puts("Status: 303 See Other");                  puts("Status: 303 See Other");
                 printf("Location: http://%s%s/show/%s/%s?",                  printf("Location: http://%s%s/show/%s/%s?",
                     host, progname, req->q.manroot, r[0].file);                      httphost, scriptname, req->q.manpath, r[0].file);
                 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");
Line 499  resp_search(const struct req *req, struct manpage *r, 
Line 501  resp_search(const struct req *req, struct manpage *r, 
                 printf("<TR>\n"                  printf("<TR>\n"
                        "<TD CLASS=\"title\">\n"                         "<TD CLASS=\"title\">\n"
                        "<A HREF=\"%s/show/%s/%s?",                         "<A HREF=\"%s/show/%s/%s?",
                     progname, req->q.manroot, r[i].file);                      scriptname, req->q.manpath, r[i].file);
                 html_printquery(req);                  html_printquery(req);
                 printf("\">");                  printf("\">");
                 html_print(r[i].names);                  html_print(r[i].names);
Line 677  format(const struct req *req, const char *file)
Line 679  format(const struct req *req, const char *file)
         }          }
   
         mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,          mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,
             req->q.manroot);              req->q.manpath);
         rc = mparse_readfd(mp, fd, file);          rc = mparse_readfd(mp, fd, file);
         close(fd);          close(fd);
   
Line 688  format(const struct req *req, const char *file)
Line 690  format(const struct req *req, const char *file)
   
         snprintf(opts, sizeof(opts),          snprintf(opts, sizeof(opts),
             "fragment,man=%s/search?sec=%%S&expr=Nm~^%%N$",              "fragment,man=%s/search?sec=%%S&expr=Nm~^%%N$",
             progname);              scriptname);
   
         mparse_result(mp, &mdoc, &man, NULL);          mparse_result(mp, &mdoc, &man, NULL);
         if (NULL == man && NULL == mdoc) {          if (NULL == man && NULL == mdoc) {
Line 726  pg_show(const struct req *req, char *path)
Line 728  pg_show(const struct req *req, char *path)
         *sub++ = '\0';          *sub++ = '\0';
   
         /*          /*
          * Begin by chdir()ing into the manroot.           * Begin by chdir()ing into the manpath.
          * This way we can pick up the database files, which are           * This way we can pick up the database files, which are
          * relative to the manpath root.           * relative to the manpath root.
          */           */
Line 760  pg_search(const struct req *req, char *path)
Line 762  pg_search(const struct req *req, char *path)
          * relative to the manpath root.           * relative to the manpath root.
          */           */
   
         if (-1 == (chdir(req->q.manroot))) {          if (-1 == (chdir(req->q.manpath))) {
                 perror(req->q.manroot);                  perror(req->q.manpath);
                 resp_search(req, NULL, 0);                  resp_search(req, NULL, 0);
                 return;                  return;
         }          }
Line 823  main(void)
Line 825  main(void)
 {  {
         int              i;          int              i;
         struct req       req;          struct req       req;
         char            *p, *path, *subpath;          char            *querystring, *path, *subpath;
   
         /* Scan our run-time environment. */          /* Scan our run-time environment. */
   
         if (NULL == (cache = getenv("CACHE_DIR")))          if (NULL == (mandir = getenv("MAN_DIR")))
                 cache = "/cache/man.cgi";                  mandir = "/man";
   
         if (NULL == (progname = getenv("SCRIPT_NAME")))          if (NULL == (scriptname = getenv("SCRIPT_NAME")))
                 progname = "";                  scriptname = "";
   
         if (NULL == (css = getenv("CSS_DIR")))          if (NULL == (cssdir = getenv("CSS_DIR")))
                 css = "";                  cssdir = "";
   
         if (NULL == (host = getenv("HTTP_HOST")))          if (NULL == (httphost = getenv("HTTP_HOST")))
                 host = "localhost";                  httphost = "localhost";
   
         /*          /*
          * First we change directory into the cache directory so that           * First we change directory into the mandir so that
          * subsequent scanning for manpath directories is rooted           * subsequent scanning for manpath directories is rooted
          * relative to the same position.           * relative to the same position.
          */           */
   
         if (-1 == chdir(cache)) {          if (-1 == chdir(mandir)) {
                 perror(cache);                  fprintf(stderr, "MAN_DIR: %s: %s\n",
                       mandir, strerror(errno));
                 resp_bad();                  resp_bad();
                 return(EXIT_FAILURE);                  return(EXIT_FAILURE);
         }          }
Line 856  main(void)
Line 859  main(void)
   
         /* Next parse out the query string. */          /* Next parse out the query string. */
   
         if (NULL != (p = getenv("QUERY_STRING")))          if (NULL != (querystring = getenv("QUERY_STRING")))
                 http_parse(&req, p);                  http_parse(&req, querystring);
   
         /*          /*
          * Now juggle paths to extract information.           * Now juggle paths to extract information.

Legend:
Removed from v.1.57  
changed lines
  Added in v.1.58

CVSweb