=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.108 retrieving revision 1.114 diff -u -p -r1.108 -r1.114 --- mandoc/cgi.c 2015/04/18 16:34:25 1.108 +++ mandoc/cgi.c 2015/11/05 20:55:41 1.114 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.108 2015/04/18 16:34:25 schwarze Exp $ */ +/* $Id: cgi.c,v 1.114 2015/11/05 20:55:41 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014, 2015 Ingo Schwarze @@ -33,6 +33,8 @@ #include "mandoc_aux.h" #include "mandoc.h" #include "roff.h" +#include "mdoc.h" +#include "man.h" #include "main.h" #include "manconf.h" #include "mansearch.h" @@ -75,6 +77,7 @@ static void pg_searchres(const struct req *, static void pg_show(struct req *, const char *); static void resp_begin_html(int, const char *); static void resp_begin_http(int, const char *); +static void resp_copy(const char *); static void resp_end_html(void); static void resp_searchform(const struct req *); static void resp_show(const struct req *, const char *); @@ -332,13 +335,13 @@ http_decode(char *p) for ( ; '\0' != *p; p++, q++) { if ('%' == *p) { if ('\0' == (hex[0] = *(p + 1))) - return(0); + return 0; if ('\0' == (hex[1] = *(p + 2))) - return(0); + return 0; if (1 != sscanf(hex, "%x", &c)) - return(0); + return 0; if ('\0' == c) - return(0); + return 0; *q = (char)c; p += 2; @@ -347,7 +350,7 @@ http_decode(char *p) } *q = '\0'; - return(1); + return 1; } static void @@ -366,6 +369,20 @@ resp_begin_http(int code, const char *msg) } static void +resp_copy(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); + } +} + +static void resp_begin_html(int code, const char *msg) { @@ -375,21 +392,23 @@ resp_begin_html(int code, const char *msg) "\n" "\n" "\n" - "\n" - "\n" "%s\n" "\n" "\n" "\n", - CSS_DIR, CSS_DIR, CUSTOMIZE_TITLE); + CSS_DIR, CUSTOMIZE_TITLE); + + resp_copy(MAN_DIR "/header.html"); } static void resp_end_html(void) { + resp_copy(MAN_DIR "/footer.html"); + puts("\n" ""); } @@ -399,7 +418,6 @@ resp_searchform(const struct req *req) { int i; - puts(CUSTOMIZE_BEGIN); puts(""); printf("
\n" "
\n" @@ -499,10 +517,10 @@ validate_urifrag(const char *frag) if ( ! (isalnum((unsigned char)*frag) || '-' == *frag || '.' == *frag || '/' == *frag || '_' == *frag)) - return(0); + return 0; frag++; } - return(1); + return 1; } static int @@ -511,13 +529,13 @@ validate_manpath(const struct req *req, const char* ma size_t i; if ( ! strcmp(manpath, "mandoc")) - return(1); + return 1; for (i = 0; i < req->psz; i++) if ( ! strcmp(manpath, req->p[i])) - return(1); + return 1; - return(0); + return 0; } static int @@ -527,8 +545,8 @@ validate_filename(const char *file) if ('.' == file[0] && '/' == file[1]) file += 2; - return ( ! (strstr(file, "../") || strstr(file, "/..") || - (strncmp(file, "man", 3) && strncmp(file, "cat", 3)))); + return ! (strstr(file, "../") || strstr(file, "/..") || + (strncmp(file, "man", 3) && strncmp(file, "cat", 3))); } static void @@ -819,7 +837,6 @@ format(const struct req *req, const char *file) { struct manoutput conf; struct mparse *mp; - struct mchars *mchars; struct roff_man *man; void *vp; int fd; @@ -830,9 +847,8 @@ format(const struct req *req, const char *file) return; } - mchars = mchars_alloc(); - mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, - mchars, req->q.manpath); + mchars_alloc(); + mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_BADARG, NULL, req->q.manpath); mparse_readfd(mp, fd, file); close(fd); @@ -852,20 +868,23 @@ format(const struct req *req, const char *file) req->q.manpath, file); pg_error_internal(); mparse_free(mp); - mchars_free(mchars); + mchars_free(); return; } - vp = html_alloc(mchars, &conf); + vp = html_alloc(&conf); - if (man->macroset == MACROSET_MDOC) + if (man->macroset == MACROSET_MDOC) { + mdoc_validate(man); html_mdoc(vp, man); - else + } else { + man_validate(man); html_man(vp, man); + } html_free(vp); mparse_free(mp); - mchars_free(mchars); + mchars_free(); free(conf.man); } @@ -1031,7 +1050,7 @@ main(void) if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) { fprintf(stderr, "setitimer: %s\n", strerror(errno)); pg_error_internal(); - return(EXIT_FAILURE); + return EXIT_FAILURE; } /* Scan our run-time environment. */ @@ -1043,7 +1062,7 @@ main(void) fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n", scriptname); pg_error_internal(); - return(EXIT_FAILURE); + return EXIT_FAILURE; } /* @@ -1056,7 +1075,7 @@ main(void) fprintf(stderr, "MAN_DIR: %s: %s\n", MAN_DIR, strerror(errno)); pg_error_internal(); - return(EXIT_FAILURE); + return EXIT_FAILURE; } memset(&req, 0, sizeof(struct req)); @@ -1072,13 +1091,13 @@ main(void) else if ( ! validate_manpath(&req, req.q.manpath)) { pg_error_badrequest( "You specified an invalid manpath."); - return(EXIT_FAILURE); + return EXIT_FAILURE; } if ( ! (NULL == req.q.arch || validate_urifrag(req.q.arch))) { pg_error_badrequest( "You specified an invalid architecture."); - return(EXIT_FAILURE); + return EXIT_FAILURE; } /* Dispatch to the three different pages. */ @@ -1103,7 +1122,7 @@ main(void) for (i = 0; i < (int)req.psz; i++) free(req.p[i]); free(req.p); - return(EXIT_SUCCESS); + return EXIT_SUCCESS; } /*