=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.168 retrieving revision 1.177 diff -u -p -r1.168 -r1.177 --- mandoc/cgi.c 2019/10/01 17:54:14 1.168 +++ mandoc/cgi.c 2022/07/04 16:20:42 1.177 @@ -1,7 +1,8 @@ -/* $Id: cgi.c,v 1.168 2019/10/01 17:54:14 schwarze Exp $ */ +/* $Id: cgi.c,v 1.177 2022/07/04 16:20:42 schwarze Exp $ */ /* + * Copyright (c) 2014-2019, 2021 Ingo Schwarze * Copyright (c) 2011, 2012 Kristaps Dzonsons - * Copyright (c) 2014-2019 Ingo Schwarze + * Copyright (c) 2022 Anna Vyalkova * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,6 +15,8 @@ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Implementation of the man.cgi(8) program. */ #include "config.h" @@ -70,9 +73,9 @@ enum focus { static void html_print(const char *); static void html_putchar(char); static int http_decode(char *); -static void http_encode(const char *p); +static void http_encode(const char *); static void parse_manpath_conf(struct req *); -static void parse_path_info(struct req *req, const char *path); +static void parse_path_info(struct req *, const char *); static void parse_query_string(struct req *, const char *); static void pg_error_badrequest(const char *); static void pg_error_internal(void); @@ -120,16 +123,18 @@ static const char *const sec_names[] = { static const int sec_MAX = sizeof(sec_names) / sizeof(char *); static const char *const arch_names[] = { - "amd64", "alpha", "armv7", "arm64", - "hppa", "i386", "landisk", - "loongson", "luna88k", "macppc", "mips64", - "octeon", "sgi", "socppc", "sparc64", + "amd64", "alpha", "armv7", "arm64", + "hppa", "i386", "landisk", "loongson", + "luna88k", "macppc", "mips64", "octeon", + "powerpc64", "riscv64", "sparc64", + "amiga", "arc", "armish", "arm32", "atari", "aviion", "beagle", "cats", "hppa64", "hp300", "ia64", "mac68k", "mvme68k", "mvme88k", "mvmeppc", "palm", "pc532", "pegasos", - "pmax", "powerpc", "solbourne", "sparc", + "pmax", "powerpc", "sgi", "socppc", + "solbourne", "sparc", "sun3", "vax", "wgrisc", "x68k", "zaurus" }; @@ -340,6 +345,8 @@ resp_begin_http(int code, const char *msg) printf("Content-Type: text/html; charset=utf-8\r\n" "Cache-Control: no-cache\r\n" + "Content-Security-Policy: default-src 'none'; " + "style-src 'self' 'unsafe-inline'\r\n" "Pragma: no-cache\r\n" "\r\n"); @@ -364,7 +371,8 @@ resp_copy(const char *filename) static void resp_begin_html(int code, const char *msg, const char *file) { - char *cp; + const char *name, *sec, *cp; + int namesz, secsz; resp_begin_http(code, msg); @@ -379,12 +387,27 @@ resp_begin_html(int code, const char *msg, const char " ", 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); + cp = strrchr(file, '/'); + name = cp == NULL ? file : cp + 1; + cp = strrchr(name, '.'); + namesz = cp == NULL ? strlen(name) : cp - name; + sec = NULL; + if (cp != NULL && cp[1] != '0') { + sec = cp + 1; + secsz = strlen(sec); + } else if (name - file > 1) { + for (cp = name - 2; cp >= file; cp--) { + if (*cp < '1' || *cp > '9') + continue; + sec = cp; + secsz = name - cp - 1; + break; + } + } + printf("%.*s", namesz, name); + if (sec != NULL) + printf("(%.*s)", secsz, sec); + fputs(" - ", stdout); } printf("%s\n" "\n" @@ -409,7 +432,9 @@ resp_searchform(const struct req *req, enum focus focu { int i; - printf("
\n" + printf("
\n" + "\n" "
\n" " Manual Page Search Parameters\n", scriptname); @@ -434,7 +459,7 @@ resp_searchform(const struct req *req, enum focus focu /* Write section selector. */ - puts(" "); for (i = 0; i < sec_MAX; i++) { printf("
\n" - ""); + "\n" + "
"); } static int @@ -534,13 +560,17 @@ pg_index(const struct req *req) resp_begin_html(200, NULL, NULL); resp_searchform(req, FOCUS_QUERY); - printf("

\n" + printf("

\n" + "

\n" "This web interface is documented in the\n" - "man.cgi(8)\n" + "man.cgi(8)\n" "manual, and the\n" - "apropos(1)\n" + "apropos(1)\n" "manual explains the query syntax.\n" - "

\n", + "

\n" + "
\n", scriptname, *scriptname == '\0' ? "" : "/", scriptname, *scriptname == '\0' ? "" : "/"); resp_end_html(); @@ -552,9 +582,11 @@ pg_noresult(const struct req *req, int code, const cha { resp_begin_html(code, http_msg, NULL); resp_searchform(req, FOCUS_QUERY); - puts("

"); + puts("

"); + puts("

"); puts(user_msg); puts("

"); + puts("
"); resp_end_html(); } @@ -563,12 +595,14 @@ pg_error_badrequest(const char *msg) { resp_begin_html(400, "Bad Request", NULL); - puts("

Bad Request

\n" - "

\n"); + puts("

\n" + "

Bad Request

\n" + "

"); puts(msg); printf("Try again from the\n" "main page.\n" - "

", scriptname); + "

\n" + "
", scriptname); resp_end_html(); } @@ -576,7 +610,7 @@ static void pg_error_internal(void) { resp_begin_html(500, "Internal Server Error", NULL); - puts("

Internal Server Error

"); + puts("

Internal Server Error

"); resp_end_html(); } @@ -681,6 +715,7 @@ pg_searchres(const struct req *req, struct manpage *r, req->q.equal || sz == 1 ? FOCUS_NONE : FOCUS_QUERY); if (sz > 1) { + puts(""); } if (req->q.equal || sz == 1) { @@ -720,7 +756,9 @@ resp_catman(const struct req *req, const char *file) int italic, bold; if ((f = fopen(file, "r")) == NULL) { - puts("

You specified an invalid manual file.

"); + puts("

\n" + " You specified an invalid manual file.\n" + "

"); return; } @@ -856,8 +894,10 @@ resp_format(const struct req *req, const char *file) int fd; int usepath; - if (-1 == (fd = open(file, O_RDONLY, 0))) { - puts("

You specified an invalid manual file.

"); + if (-1 == (fd = open(file, O_RDONLY))) { + puts("

\n" + " You specified an invalid manual file.\n" + "

"); return; }