:q— work/mdocml-1.13.3/cgi.c 2015-03-13 12:38:38.000000000 +0000 +++ cgi.c 2015-10-22 18:35:21.000000000 +0000 @@ -17,6 +17,7 @@ */ #include "config.h" +#include #include #include @@ -52,6 +53,8 @@ struct req { struct query q; char **p; /* array of available manpaths */ size_t psz; /* number of available manpaths */ + char *header; /* custom HTML header */ + char *footer; /* custom HTML footer */ }; static void catman(const struct req *, const char *); @@ -63,6 +66,8 @@ static void http_parse(struct req *, c static void http_print(const char *); static void http_putchar(char); static void http_printquery(const struct req *, const char *); +static void internalize_file(char **, const char *); +static void htmlgen(struct req *); static void pathgen(struct req *); static void pg_error_badrequest(const char *); static void pg_error_internal(void); @@ -76,6 +81,7 @@ static void resp_begin_html(int, const static void resp_begin_http(int, const char *); static void resp_end_html(void); static void resp_searchform(const struct req *); +static void resp_footer(const struct req *); static void resp_show(const struct req *, const char *); static void set_query_attr(char **, char **); static int validate_filename(const char *); @@ -398,7 +404,10 @@ resp_searchform(const struct req *req) { int i; + /* Write the customized HTML header */ puts(CUSTOMIZE_BEGIN); + if (req->header != NULL) + puts(req->header); puts(""); printf("
\n" "
\n" @@ -490,6 +499,16 @@ resp_searchform(const struct req *req) puts(""); } +static void +resp_footer(const struct req *req) +{ + + /* Write the customized HTML footer */ + if (req->footer != NULL) + puts(req->footer); + puts(CUSTOMIZE_END); +} + static int validate_urifrag(const char *frag) { @@ -544,6 +563,7 @@ pg_index(const struct req *req) "manual explains the query syntax.\n" "

\n", scriptname, scriptname); + resp_footer(req); resp_end_html(); } @@ -555,6 +575,7 @@ pg_noresult(const struct req *req, const puts("

"); puts(msg); puts("

"); + resp_footer(req); resp_end_html(); } @@ -678,6 +699,7 @@ pg_searchres(const struct req *req, stru resp_show(req, r[iuse].file); } + resp_footer(req); resp_end_html(); } @@ -931,6 +953,7 @@ pg_show(struct req *req, const char *ful resp_begin_html(200, NULL); resp_searchform(req); resp_show(req, file); + resp_footer(req); resp_end_html(); } @@ -1060,6 +1083,7 @@ main(void) memset(&req, 0, sizeof(struct req)); pathgen(&req); + htmlgen(&req); /* Next parse out the query string. */ @@ -1102,10 +1126,68 @@ main(void) for (i = 0; i < (int)req.psz; i++) free(req.p[i]); free(req.p); + free(req.header); + free(req.footer); return(EXIT_SUCCESS); } /* + * Read and store content of a file located in MAN_DIR + */ +static void +internalize_file(char **buf, const char *name) +{ + FILE *fp; + struct stat st; + + if (NULL == (fp = fopen(name, "r"))) { + fprintf(stderr, "%s/%s: %s\n", + MAN_DIR, name, strerror(errno)); + goto error; + } + + if (fstat(fileno(fp), &st) != 0) { + fprintf(stderr, "%s/%s: %s\n", + MAN_DIR, name, strerror(errno)); + goto error; + } + + *buf = mandoc_calloc(1, st.st_size + 1); + if (fread(*buf, sizeof(char), st.st_size, fp) < st.st_size) { + if (ferror(fp)) { + fprintf(stderr, "%s/%s: %s\n", + MAN_DIR, name, strerror(errno)); + goto error; + } + if (feof(fp)) { + fprintf(stderr, "%s/%s: %s\n", + MAN_DIR, name, "premature EOF"); + goto error; + } + } + fclose(fp); + return; + +error: + if (fp != NULL) + fclose(fp); + free(*buf); + *buf = NULL; + return; +} + +/* + * Scan for customized HTML header and footer + */ +static void +htmlgen(struct req *req) +{ + + internalize_file(&req->header, "begin.html"); + internalize_file(&req->footer, "end.html"); +} + +/* * Scan for indexable paths. */ static void