=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.144 retrieving revision 1.168 diff -u -p -r1.144 -r1.168 --- mandoc/cgi.c 2017/01/21 01:20:31 1.144 +++ mandoc/cgi.c 2019/10/01 17:54:14 1.168 @@ -1,7 +1,7 @@ -/* $Id: cgi.c,v 1.144 2017/01/21 01:20:31 schwarze Exp $ */ +/* $Id: cgi.c,v 1.168 2019/10/01 17:54:14 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons - * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze + * Copyright (c) 2014-2019 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,7 +21,9 @@ #include #include +#if HAVE_ERR #include +#endif #include #include #include @@ -36,6 +38,7 @@ #include "roff.h" #include "mdoc.h" #include "man.h" +#include "mandoc_parse.h" #include "main.h" #include "manconf.h" #include "mansearch.h" @@ -67,18 +70,21 @@ 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 parse_manpath_conf(struct req *); static void parse_path_info(struct req *req, const char *path); static void parse_query_string(struct req *, const char *); 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_noresult(const struct req *, int, const char *, + 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 *); @@ -87,6 +93,7 @@ static void resp_format(const struct req *, const ch static void resp_searchform(const struct req *, enum focus); static void resp_show(const struct req *, const char *); static void set_query_attr(char **, char **); +static int validate_arch(const char *); static int validate_filename(const char *); static int validate_manpath(const struct req *, const char *); static int validate_urifrag(const char *); @@ -113,7 +120,7 @@ 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", + "amd64", "alpha", "armv7", "arm64", "hppa", "i386", "landisk", "loongson", "luna88k", "macppc", "mips64", "octeon", "sgi", "socppc", "sparc64", @@ -137,16 +144,16 @@ html_putchar(char c) { switch (c) { - case ('"'): + case '"': printf("""); break; - case ('&'): + case '&': printf("&"); break; - case ('>'): + case '>': printf(">"); break; - case ('<'): + case '<': printf("<"); break; default: @@ -313,6 +320,18 @@ http_decode(char *p) } static void +http_encode(const char *p) +{ + for (; *p != '\0'; p++) { + if (isalnum((unsigned char)*p) == 0 && + strchr("-._~", *p) == NULL) + printf("%%%2.2X", (unsigned char)*p); + else + putchar(*p); + } +} + +static void resp_begin_http(int code, const char *msg) { @@ -343,8 +362,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); @@ -352,12 +372,24 @@ resp_begin_html(int code, const char *msg) "\n" "\n" " \n" + " \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"); } @@ -384,7 +416,7 @@ resp_searchform(const struct req *req, enum focus focu /* Write query input box. */ - printf(" q.query != NULL) html_print(req->q.query); printf( "\" size=\"40\""); @@ -420,7 +452,7 @@ resp_searchform(const struct req *req, enum focus focu printf(" selected=\"selected\""); puts(">All Architectures"); for (i = 0; i < arch_MAX; i++) { - printf("