]> git.cameronkatri.com Git - mandoc.git/blobdiff - cgi.c
Run the regression tests iteratively rather than recursively.
[mandoc.git] / cgi.c
diff --git a/cgi.c b/cgi.c
index 05a861d4effd730a7e74aa39a610c66e5ed03493..746c481ac5240ea0c5f82339ba9fc7f422d0eea5 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -1,4 +1,4 @@
-/*     $Id: cgi.c,v 1.149 2017/03/15 10:17:29 schwarze Exp $ */
+/*     $Id: cgi.c,v 1.156 2017/06/24 14:38:32 schwarze Exp $ */
 /*
  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze <schwarze@usta.de>
@@ -81,7 +81,7 @@ static        void             pg_search(const struct req *);
 static void             pg_searchres(const struct req *,
                                struct manpage *, size_t);
 static void             pg_show(struct req *, const char *);
-static void             resp_begin_html(int, const char *);
+static void             resp_begin_html(int, const char *, const char *);
 static void             resp_begin_http(int, const char *);
 static void             resp_catman(const struct req *, const char *);
 static void             resp_copy(const char *);
@@ -140,16 +140,16 @@ html_putchar(char c)
 {
 
        switch (c) {
-       case ('"'):
+       case '"':
                printf("&quot;");
                break;
-       case ('&'):
+       case '&':
                printf("&amp;");
                break;
-       case ('>'):
+       case '>':
                printf("&gt;");
                break;
-       case ('<'):
+       case '<':
                printf("&lt;");
                break;
        default:
@@ -346,8 +346,9 @@ resp_copy(const char *filename)
 }
 
 static void
-resp_begin_html(int code, const char *msg)
+resp_begin_html(int code, const char *msg, const char *file)
 {
+       char    *cp;
 
        resp_begin_http(code, msg);
 
@@ -357,10 +358,20 @@ resp_begin_html(int code, const char *msg)
               "  <meta charset=\"UTF-8\"/>\n"
               "  <link rel=\"stylesheet\" href=\"%s/mandoc.css\""
               " type=\"text/css\" media=\"all\">\n"
-              "  <title>%s</title>\n"
+              "  <title>",
+              CSS_DIR);
+       if (file != NULL) {
+               if ((cp = strrchr(file, '/')) != NULL)
+                       file = cp + 1;
+               if ((cp = strrchr(file, '.')) != NULL) {
+                       printf("%.*s(%s) - ", (int)(cp - file), file, cp + 1);
+               } else
+                       printf("%s - ", file);
+       }
+       printf("%s</title>\n"
               "</head>\n"
               "<body>\n",
-              CSS_DIR, CUSTOMIZE_TITLE);
+              CUSTOMIZE_TITLE);
 
        resp_copy(MAN_DIR "/header.html");
 }
@@ -493,7 +504,7 @@ static void
 pg_index(const struct req *req)
 {
 
-       resp_begin_html(200, NULL);
+       resp_begin_html(200, NULL, NULL);
        resp_searchform(req, FOCUS_QUERY);
        printf("<p>\n"
               "This web interface is documented in the\n"
@@ -510,7 +521,7 @@ pg_index(const struct req *req)
 static void
 pg_noresult(const struct req *req, const char *msg)
 {
-       resp_begin_html(200, NULL);
+       resp_begin_html(200, NULL, NULL);
        resp_searchform(req, FOCUS_QUERY);
        puts("<p>");
        puts(msg);
@@ -522,7 +533,7 @@ static void
 pg_error_badrequest(const char *msg)
 {
 
-       resp_begin_html(400, "Bad Request");
+       resp_begin_html(400, "Bad Request", NULL);
        puts("<h1>Bad Request</h1>\n"
             "<p>\n");
        puts(msg);
@@ -535,7 +546,7 @@ pg_error_badrequest(const char *msg)
 static void
 pg_error_internal(void)
 {
-       resp_begin_html(500, "Internal Server Error");
+       resp_begin_html(500, "Internal Server Error", NULL);
        puts("<p>Internal Server Error</p>");
        resp_end_html();
 }
@@ -543,8 +554,8 @@ pg_error_internal(void)
 static void
 pg_redirect(const struct req *req, const char *name)
 {
-       printf("Status: 303 See Other\r\n");
-       printf("Location: http://%s/", HTTP_HOST);
+       printf("Status: 303 See Other\r\n"
+           "Location: /");
        if (*scriptname != '\0')
                printf("%s/", scriptname);
        if (strcmp(req->q.manpath, req->p[0]))
@@ -580,47 +591,25 @@ pg_searchres(const struct req *req, struct manpage *r, size_t sz)
                 * If we have just one result, then jump there now
                 * without any delay.
                 */
-               printf("Status: 303 See Other\r\n");
-               printf("Location: http://%s/%s%s%s/%s",
-                   HTTP_HOST, scriptname,
-                   *scriptname == '\0' ? "" : "/",
-                   req->q.manpath, r[0].file);
-               printf("\r\n"
-                    "Content-Type: text/html; charset=utf-8\r\n"
-                    "\r\n");
+               printf("Status: 303 See Other\r\n"
+                   "Location: /");
+               if (*scriptname != '\0')
+                       printf("%s/", scriptname);
+               if (strcmp(req->q.manpath, req->p[0]))
+                       printf("%s/", req->q.manpath);
+               printf("%s\r\n"
+                   "Content-Type: text/html; charset=utf-8\r\n\r\n",
+                   r[0].file);
                return;
        }
 
-       resp_begin_html(200, NULL);
-       resp_searchform(req,
-           req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
-
-       if (sz > 1) {
-               puts("<table class=\"results\">");
-               for (i = 0; i < sz; i++) {
-                       printf("  <tr>\n"
-                              "    <td>"
-                              "<a class=\"Xr\" href=\"/%s%s%s/%s\">",
-                           scriptname, *scriptname == '\0' ? "" : "/",
-                           req->q.manpath, r[i].file);
-                       html_print(r[i].names);
-                       printf("</a></td>\n"
-                              "    <td><span class=\"Nd\">");
-                       html_print(r[i].output);
-                       puts("</span></td>\n"
-                            "  </tr>");
-               }
-               puts("</table>");
-       }
-
        /*
         * In man(1) mode, show one of the pages
         * even if more than one is found.
         */
 
+       iuse = 0;
        if (req->q.equal || sz == 1) {
-               puts("<hr>");
-               iuse = 0;
                priouse = 20;
                archpriouse = 3;
                for (i = 0; i < sz; i++) {
@@ -653,6 +642,36 @@ pg_searchres(const struct req *req, struct manpage *r, size_t sz)
                        priouse = prio;
                        iuse = i;
                }
+               resp_begin_html(200, NULL, r[iuse].file);
+       } else
+               resp_begin_html(200, NULL, NULL);
+
+       resp_searchform(req,
+           req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY);
+
+       if (sz > 1) {
+               puts("<table class=\"results\">");
+               for (i = 0; i < sz; i++) {
+                       printf("  <tr>\n"
+                              "    <td>"
+                              "<a class=\"Xr\" href=\"/");
+                       if (*scriptname != '\0')
+                               printf("%s/", scriptname);
+                       if (strcmp(req->q.manpath, req->p[0]))
+                               printf("%s/", req->q.manpath);
+                       printf("%s\">", r[i].file);
+                       html_print(r[i].names);
+                       printf("</a></td>\n"
+                              "    <td><span class=\"Nd\">");
+                       html_print(r[i].output);
+                       puts("</span></td>\n"
+                            "  </tr>");
+               }
+               puts("</table>");
+       }
+
+       if (req->q.equal || sz == 1) {
+               puts("<hr>");
                resp_show(req, r[iuse].file);
        }
 
@@ -813,7 +832,7 @@ resp_format(const struct req *req, const char *file)
 
        mchars_alloc();
        mp = mparse_alloc(MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1,
-           MANDOCLEVEL_BADARG, NULL, req->q.manpath);
+           MANDOCERR_MAX, NULL, MANDOC_OS_OTHER, req->q.manpath);
        mparse_readfd(mp, fd, file);
        close(fd);
 
@@ -821,7 +840,8 @@ resp_format(const struct req *req, const char *file)
        conf.fragment = 1;
        conf.style = mandoc_strdup(CSS_DIR "/mandoc.css");
        usepath = strcmp(req->q.manpath, req->p[0]);
-       mandoc_asprintf(&conf.man, "/%s%s%%N.%%S",
+       mandoc_asprintf(&conf.man, "/%s%s%s%s%%N.%%S",
+           scriptname, *scriptname == '\0' ? "" : "/",
            usepath ? req->q.manpath : "", usepath ? "/" : "");
 
        mparse_result(mp, &man, NULL);
@@ -904,7 +924,7 @@ pg_show(struct req *req, const char *fullpath)
                return;
        }
 
-       resp_begin_html(200, NULL);
+       resp_begin_html(200, NULL, file);
        resp_searchform(req, FOCUS_NONE);
        resp_show(req, file);
        resp_end_html();
@@ -1053,7 +1073,8 @@ main(void)
 
        if (*path != '\0') {
                parse_path_info(&req, path);
-               if (req.q.manpath == NULL || access(path, F_OK) == -1)
+               if (req.q.manpath == NULL || req.q.sec == NULL ||
+                   *req.q.query == '\0' || access(path, F_OK) == -1)
                        path = "";
        } else if ((querystring = getenv("QUERY_STRING")) != NULL)
                parse_query_string(&req, querystring);