=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.168 retrieving revision 1.180 diff -u -p -r1.168 -r1.180 --- mandoc/cgi.c 2019/10/01 17:54:14 1.168 +++ mandoc/cgi.c 2022/07/06 17:21:04 1.180 @@ -1,7 +1,8 @@ -/* $Id: cgi.c,v 1.168 2019/10/01 17:54:14 schwarze Exp $ */ +/* $Id: cgi.c,v 1.180 2022/07/06 17:21:04 schwarze Exp $ */ /* + * Copyright (c) 2014-2019, 2021, 2022 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); @@ -84,10 +87,10 @@ 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 *, const char *); +static int 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 *); +static int resp_copy(const char *, const char *); static void resp_end_html(void); static void resp_format(const struct req *, const char *); static void resp_searchform(const struct req *, enum focus); @@ -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,31 +345,38 @@ 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"); fflush(stdout); } -static void -resp_copy(const char *filename) +static int +resp_copy(const char *element, const char *filename) { char buf[4096]; ssize_t sz; int fd; - if ((fd = open(filename, O_RDONLY)) != -1) { - fflush(stdout); - while ((sz = read(fd, buf, sizeof(buf))) > 0) - write(STDOUT_FILENO, buf, sz); - close(fd); - } + if ((fd = open(filename, O_RDONLY)) == -1) + return 0; + + if (element != NULL) + printf("<%s>\n", element); + fflush(stdout); + while ((sz = read(fd, buf, sizeof(buf))) > 0) + write(STDOUT_FILENO, buf, sz); + close(fd); + return 1; } -static void +static int 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,27 +391,42 @@ 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" "\n", CUSTOMIZE_TITLE); - resp_copy(MAN_DIR "/header.html"); + return resp_copy("header", MAN_DIR "/header.html"); } static void resp_end_html(void) { + if (resp_copy("footer", MAN_DIR "/footer.html")) + puts(""); - resp_copy(MAN_DIR "/footer.html"); - puts("\n" ""); } @@ -409,20 +436,22 @@ resp_searchform(const struct req *req, enum focus focu { int i; - printf("
\n" + printf("\n" "
\n" " Manual Page Search Parameters\n", scriptname); /* Write query input box. */ - printf(" Search query:\n" + " q.query != NULL) html_print(req->q.query); - printf( "\" size=\"40\""); + printf("\" size=\"40\""); if (focus == FOCUS_QUERY) printf(" autofocus"); - puts(">"); + puts(">\n "); /* Write submission buttons. */ @@ -434,7 +463,7 @@ resp_searchform(const struct req *req, enum focus focu /* Write section selector. */ - puts(" "); for (i = 0; i < sec_MAX; i++) { printf("