[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.60

version 1.57, 2014/07/09 12:09:04 version 1.60, 2014/07/09 17:03:07
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 73  static void   pathgen(struct req *);
Line 74  static void   pathgen(struct req *);
 static  void             pg_index(const struct req *, char *);  static  void             pg_index(const struct req *, char *);
 static  void             pg_search(const struct req *, char *);  static  void             pg_search(const struct req *, char *);
 static  void             pg_show(const struct req *, char *);  static  void             pg_show(const struct req *, char *);
 static  void             resp_bad(void);  
 static  void             resp_baddb(void);  
 static  void             resp_error400(void);  
 static  void             resp_error404(const char *);  
 static  void             resp_begin_html(int, const char *);  static  void             resp_begin_html(int, const char *);
 static  void             resp_begin_http(int, const char *);  static  void             resp_begin_http(int, const char *);
 static  void             resp_end_html(void);  static  void             resp_end_html(void);
   static  void             resp_error_badrequest(const char *);
   static  void             resp_error_internal(void);
   static  void             resp_error_notfound(const char *);
 static  void             resp_index(const struct req *);  static  void             resp_index(const struct req *);
   static  void             resp_noresult(const struct req *,
                                   const char *);
 static  void             resp_search(const struct req *,  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 129  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 151  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 205  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 239  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 344  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 369  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 378  resp_searchform(const struct req *req)
Line 381  resp_searchform(const struct req *req)
         html_print(req->q.arch ? req->q.arch : "");          html_print(req->q.arch ? req->q.arch : "");
         printf("\">");          printf("\">");
         if (req->psz > 1) {          if (req->psz > 1) {
                 puts(", <SELECT NAME=\"manpath\">");                  puts(", in <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 392  resp_searchform(const struct req *req)
Line 395  resp_searchform(const struct req *req)
                 }                  }
                 puts("</SELECT>");                  puts("</SELECT>");
         }          }
         puts(".\n"          puts("&mdash;\n"
              "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"               "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
              "</FIELDSET>\n"               "</FIELDSET>\n"
              "</FORM>\n"               "</FORM>\n"
Line 405  resp_index(const struct req *req)
Line 408  resp_index(const struct req *req)
 {  {
   
         resp_begin_html(200, NULL);          resp_begin_html(200, NULL);
           puts("<H1>\n"
                "Online manuals with "
                "<A HREF=\"http://mdocml.bsd.lv/\">mandoc</A>\n"
                "</H1>");
         resp_searchform(req);          resp_searchform(req);
           puts("<P>\n"
                "The <A HREF=\"search?expr=Nm~^apropos$&amp;sec=1\">"
                "apropos</A> manual explains the query syntax.\n"
                "</P>");
         resp_end_html();          resp_end_html();
 }  }
   
 static void  static void
 resp_error400(void)  resp_noresult(const struct req *req, const char *msg)
 {  {
           resp_begin_html(200, NULL);
           resp_searchform(req);
           puts("<P>");
           puts(msg);
           puts("</P>");
           resp_end_html();
   }
   
         resp_begin_html(400, "Query Malformed");  static void
         printf("<H1>Malformed Query</H1>\n"  resp_error_badrequest(const char *msg)
                "<P>\n"  {
                "The query your entered was malformed.\n"  
                "Try again from the\n"          resp_begin_html(400, "Bad Request");
                "<A HREF=\"%s/index.html\">main page</A>.\n"          puts("<H1>Bad Request</H1>\n"
                "</P>", progname);               "<P>\n");
           puts(msg);
           printf("Try again from the\n"
                  "<A HREF=\"%s\">main page</A>.\n"
                  "</P>", scriptname);
         resp_end_html();          resp_end_html();
 }  }
   
 static void  static void
 resp_error404(const char *page)  resp_error_notfound(const char *page)
 {  {
   
         resp_begin_html(404, "Not Found");          resp_begin_html(404, "Not Found");
Line 436  resp_error404(const char *page)
Line 458  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();
 }  }
   
 static void  static void
 resp_bad(void)  resp_error_internal(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();
 }  }
   
 static void  static void
 resp_baddb(void)  
 {  
   
         resp_begin_html(500, "Internal Server Error");  
         puts("<P>Your database is broken.</P>");  
         resp_end_html();  
 }  
   
 static void  
 resp_search(const struct req *req, struct manpage *r, size_t sz)  resp_search(const struct req *req, struct manpage *r, size_t sz)
 {  {
         size_t           i;          size_t           i;
Line 470  resp_search(const struct req *req, struct manpage *r, 
Line 483  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");
                 return;                  return;
         }          }
   
           qsort(r, sz, sizeof(struct manpage), 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) {  
                 puts("<P>\n"  
                      "No results found.\n"  
                      "</P>\n"  
                      "</DIV>");  
                 resp_end_html();  
                 return;  
         }  
   
         qsort(r, sz, sizeof(struct manpage), cmp);  
   
         puts("<TABLE>");          puts("<TABLE>");
   
         for (i = 0; i < sz; i++) {          for (i = 0; i < sz; i++) {
                 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 534  catman(const struct req *req, const char *file)
Line 536  catman(const struct req *req, const char *file)
         int              italic, bold;          int              italic, bold;
   
         if (NULL == (f = fopen(file, "r"))) {          if (NULL == (f = fopen(file, "r"))) {
                 resp_baddb();                  resp_error_badrequest(
                       "You specified an invalid manual file.");
                 return;                  return;
         }          }
   
Line 672  format(const struct req *req, const char *file)
Line 675  format(const struct req *req, const char *file)
         char             opts[PATH_MAX + 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_error_badrequest(
                       "You specified an invalid manual file.");
                 return;                  return;
         }          }
   
         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);
   
         if (rc >= MANDOCLEVEL_FATAL) {          if (rc >= MANDOCLEVEL_FATAL) {
                 resp_baddb();                  fprintf(stderr, "fatal mandoc error: %s/%s\n",
                       req->q.manpath, file);
                   resp_error_internal();
                 return;                  return;
         }          }
   
         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) {
                 resp_baddb();                  fprintf(stderr, "fatal mandoc error: %s/%s\n",
                       req->q.manpath, file);
                   resp_error_internal();
                 mparse_free(mp);                  mparse_free(mp);
                 return;                  return;
         }          }
Line 720  pg_show(const struct req *req, char *path)
Line 728  pg_show(const struct req *req, char *path)
         char            *sub;          char            *sub;
   
         if (NULL == path || NULL == (sub = strchr(path, '/'))) {          if (NULL == path || NULL == (sub = strchr(path, '/'))) {
                 resp_error400();                  resp_error_badrequest(
                       "You did not specify a page to show.");
                 return;                  return;
         }          }
         *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.
          */           */
   
         if (-1 == chdir(path)) {          if (-1 == chdir(path)) {
                 perror(path);                  resp_error_badrequest(
                 resp_baddb();                      "You specified an invalid manpath.");
                 return;                  return;
         }          }
   
Line 760  pg_search(const struct req *req, char *path)
Line 769  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);                  resp_error_badrequest(
                 resp_search(req, NULL, 0);                      "You specified an invalid manpath.");
                 return;                  return;
         }          }
   
Line 798  pg_search(const struct req *req, char *path)
Line 807  pg_search(const struct req *req, char *path)
                         ep++;                          ep++;
         }          }
   
         if (mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))          if (0 == mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
                 resp_search(req, res, ressz);                  resp_noresult(req, "You entered an invalid query.");
           else if (0 == ressz)
                   resp_noresult(req, "No results found.");
         else          else
                 resp_baddb();                  resp_search(req, res, ressz);
   
         for (i = 0; i < sz; i++)          for (i = 0; i < sz; i++)
                 free(cp[i]);                  free(cp[i]);
Line 823  main(void)
Line 834  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",
                 resp_bad();                      mandir, strerror(errno));
                   resp_error_internal();
                 return(EXIT_FAILURE);                  return(EXIT_FAILURE);
         }          }
   
Line 856  main(void)
Line 868  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.
Line 902  main(void)
Line 914  main(void)
                 pg_show(&req, subpath);                  pg_show(&req, subpath);
                 break;                  break;
         default:          default:
                 resp_error404(path);                  resp_error_notfound(path);
                 break;                  break;
         }          }
   

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

CVSweb