=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.148 retrieving revision 1.152 diff -u -p -r1.148 -r1.152 --- mandoc/cgi.c 2017/02/22 16:20:01 1.148 +++ mandoc/cgi.c 2017/03/18 16:19:11 1.152 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.148 2017/02/22 16:20:01 schwarze Exp $ */ +/* $Id: cgi.c,v 1.152 2017/03/18 16:19:11 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze @@ -76,11 +76,12 @@ static void pg_error_badrequest(const char *); static void pg_error_internal(void); static void pg_index(const struct req *); static void pg_noresult(const struct req *, const char *); +static void pg_redirect(const struct req *, const char *); 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 *); @@ -345,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); @@ -356,10 +358,20 @@ resp_begin_html(int code, const char *msg) " \n" " \n" - " %s\n" + " ", + 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\n" "\n" "\n", - CSS_DIR, CUSTOMIZE_TITLE); + CUSTOMIZE_TITLE); resp_copy(MAN_DIR "/header.html"); } @@ -492,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("

\n" "This web interface is documented in the\n" @@ -509,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("

"); puts(msg); @@ -521,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("

Bad Request

\n" "

\n"); puts(msg); @@ -534,12 +546,29 @@ 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("

Internal Server Error

"); resp_end_html(); } static void +pg_redirect(const struct req *req, const char *name) +{ + printf("Status: 303 See Other\r\n"); + printf("Location: http://%s/", HTTP_HOST); + if (*scriptname != '\0') + printf("%s/", scriptname); + if (strcmp(req->q.manpath, req->p[0])) + printf("%s/", req->q.manpath); + if (req->q.arch != NULL) + printf("%s/", req->q.arch); + printf("%s", name); + if (req->q.sec != NULL) + printf(".%s", req->q.sec); + printf("\r\nContent-Type: text/html; charset=utf-8\r\n\r\n"); +} + +static void pg_searchres(const struct req *req, struct manpage *r, size_t sz) { char *arch, *archend; @@ -573,36 +602,13 @@ pg_searchres(const struct req *req, struct manpage *r, return; } - resp_begin_html(200, NULL); - resp_searchform(req, - req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY); - - if (sz > 1) { - puts(""); - for (i = 0; i < sz; i++) { - printf(" \n" - " \n" - " \n" - " "); - } - puts("
" - "", - scriptname, *scriptname == '\0' ? "" : "/", - req->q.manpath, r[i].file); - html_print(r[i].names); - printf(""); - html_print(r[i].output); - puts("
"); - } - /* * 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("
"); - iuse = 0; priouse = 20; archpriouse = 3; for (i = 0; i < sz; i++) { @@ -635,6 +641,36 @@ pg_searchres(const struct req *req, struct manpage *r, 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(""); + for (i = 0; i < sz; i++) { + printf(" \n" + " \n" + " \n" + " "); + } + puts("
" + "q.manpath, req->p[0])) + printf("%s/", req->q.manpath); + printf("%s\">", r[i].file); + html_print(r[i].names); + printf(""); + html_print(r[i].output); + puts("
"); + } + + if (req->q.equal || sz == 1) { + puts("
"); resp_show(req, r[iuse].file); } @@ -803,7 +839,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); @@ -886,7 +923,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(); @@ -956,9 +993,13 @@ pg_search(const struct req *req) } } - if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz)) + res = NULL; + ressz = 0; + if (req->isquery && req->q.equal && argc == 1) + pg_redirect(req, argv[0]); + else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0) pg_noresult(req, "You entered an invalid query."); - else if (0 == ressz) + else if (ressz == 0) pg_noresult(req, "No results found."); else pg_searchres(req, res, ressz);