=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.6 retrieving revision 1.13 diff -u -p -r1.6 -r1.13 --- mandoc/cgi.c 2011/11/23 10:01:04 1.6 +++ mandoc/cgi.c 2011/12/07 15:12:34 1.13 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.6 2011/11/23 10:01:04 kristaps Exp $ */ +/* $Id: cgi.c,v 1.13 2011/12/07 15:12:34 kristaps Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons * @@ -36,6 +36,9 @@ #include "apropos_db.h" #include "mandoc.h" +#include "mdoc.h" +#include "man.h" +#include "main.h" #include "manpath.h" #ifdef __linux__ @@ -63,7 +66,10 @@ struct req { }; static int atou(const char *, unsigned *); +static void catman(const char *); +static void format(const char *); static void html_print(const char *); +static void html_putchar(char); static int kval_decode(char *); static void kval_parse(struct kval **, size_t *, char *); static void kval_free(struct kval *, size_t); @@ -73,9 +79,10 @@ static void pg_search(const struct manpaths *, const struct req *, char *); static void pg_show(const struct manpaths *, const struct req *, char *); +static void resp_bad(void); static void resp_baddb(void); -static void resp_badexpr(const struct req *); -static void resp_badmanual(void); +static void resp_error400(void); +static void resp_error404(const char *); static void resp_begin_html(int, const char *); static void resp_begin_http(int, const char *); static void resp_end_html(void); @@ -84,6 +91,7 @@ static void resp_search(struct res *, size_t, void * static void resp_searchform(const struct req *); static const char *progname; +static const char *cache; static const char *host; static const char * const pages[PAGE__MAX] = { @@ -115,6 +123,29 @@ atou(const char *buf, unsigned *v) return(1); } +static void +html_putchar(char c) +{ + + switch (c) { + case ('"'): + printf(""e;"); + break; + case ('&'): + printf("&"); + break; + case ('>'): + printf(">"); + break; + case ('<'): + printf("<"); + break; + default: + putchar((unsigned char)c); + break; + } +} + /* * Print a word, escaping HTML along the way. * This will pass non-ASCII straight to output: be warned! @@ -122,29 +153,11 @@ atou(const char *buf, unsigned *v) static void html_print(const char *p) { - char c; if (NULL == p) return; - while ('\0' != *p) - switch ((c = *p++)) { - case ('"'): - printf(""e;"); - break; - case ('&'): - printf("&"); - break; - case ('>'): - printf(">"); - break; - case ('<'): - printf("<"); - break; - default: - putchar((unsigned char)c); - break; - } + html_putchar(*p++); } static void @@ -282,6 +295,10 @@ resp_begin_html(int code, const char *msg) " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n" "" "\n" " " "\n" + " " "\n" + " " "\n" " System Manpage Reference" "\n" " " "\n" " " "\n" @@ -306,30 +323,43 @@ resp_searchform(const struct req *req) for (i = 0; i < (int)req->fieldsz; i++) if (0 == strcmp(req->fields[i].key, "expr")) expr = req->fields[i].val; + else if (0 == strcmp(req->fields[i].key, "query")) + expr = req->fields[i].val; else if (0 == strcmp(req->fields[i].key, "sec")) sec = req->fields[i].val; + else if (0 == strcmp(req->fields[i].key, "sektion")) + sec = req->fields[i].val; else if (0 == strcmp(req->fields[i].key, "arch")) arch = req->fields[i].val; + if (NULL != sec && 0 == strcmp(sec, "0")) + sec = NULL; + puts(""); printf("
\n"); - puts("
" "\n" - " "); - printf(" Terms: \n"); + puts("
\n" + " or \n" + " for manuals satisfying \n" + ""); - printf(" Section: , section " + ""); - printf(" Arch: , arch " + ""); - puts("
\n\n"); + puts("\">.\n" + "\n" + "
\n" + "\n" + ""); } static void @@ -342,25 +372,46 @@ resp_index(const struct req *req) } static void -resp_badmanual(void) +resp_error400(void) { - resp_begin_html(404, "Not Found"); - puts("

Requested manual not found.

"); + resp_begin_html(400, "Query Malformed"); + printf("

Malformed Query

\n" + "

\n" + " The query your entered was malformed.\n" + " Try again from the\n" + " main page\n" + "

", progname); resp_end_html(); } static void -resp_badexpr(const struct req *req) +resp_error404(const char *page) { - resp_begin_html(200, NULL); - resp_searchform(req); - puts("

Your search didn't work.

"); + resp_begin_html(404, "Not Found"); + puts("

Page Not Found

\n" + "

\n" + " The page you're looking for, "); + printf(" "); + html_print(page); + printf(",\n" + " could not be found.\n" + " Try searching from the\n" + " main page\n" + "

", progname); resp_end_html(); } static void +resp_bad(void) +{ + resp_begin_html(500, "Internal Server Error"); + puts("

Generic badness happened.

"); + resp_end_html(); +} + +static void resp_baddb(void) { @@ -421,30 +472,222 @@ pg_index(const struct manpaths *ps, const struct req * } static void +catman(const char *file) +{ + FILE *f; + size_t len; + int i; + char *p; + int italic, bold; + + if (NULL == (f = fopen(file, "r"))) { + resp_baddb(); + return; + } + + resp_begin_http(200, NULL); + puts("" "\n" + "" "\n" + " " "\n" + " " "\n" + " " "\n" + " System Manpage Reference" "\n" + " " "\n" + " " "\n" + ""); + + puts("
");
+	while (NULL != (p = fgetln(f, &len))) {
+		bold = italic = 0;
+		for (i = 0; i < (int)len - 1; i++) {
+			/* 
+			 * This means that the catpage is out of state.
+			 * Ignore it and keep going (although the
+			 * catpage is bogus).
+			 */
+
+			if ('\b' == p[i] || '\n' == p[i])
+				continue;
+
+			/*
+			 * Print a regular character.
+			 * Close out any bold/italic scopes.
+			 * If we're in back-space mode, make sure we'll
+			 * have something to enter when we backspace.
+			 */
+
+			if ('\b' != p[i + 1]) {
+				if (italic)
+					printf("");
+				if (bold)
+					printf("");
+				italic = bold = 0;
+				html_putchar(p[i]);
+				continue;
+			} else if (i + 2 >= (int)len)
+				continue;
+
+			/* Italic mode. */
+
+			if ('_' == p[i]) {
+				if (bold)
+					printf("");
+				if ( ! italic)
+					printf("");
+				bold = 0;
+				italic = 1;
+				i += 2;
+				html_putchar(p[i]);
+				continue;
+			}
+
+			/* 
+			 * Handle funny behaviour troff-isms.
+			 * These grok'd from the original man2html.c.
+			 */
+
+			if (('+' == p[i] && 'o' == p[i + 2]) ||
+					('o' == p[i] && '+' == p[i + 2]) ||
+					('|' == p[i] && '=' == p[i + 2]) ||
+					('=' == p[i] && '|' == p[i + 2]) ||
+					('*' == p[i] && '=' == p[i + 2]) ||
+					('=' == p[i] && '*' == p[i + 2]) ||
+					('*' == p[i] && '|' == p[i + 2]) ||
+					('|' == p[i] && '*' == p[i + 2]))  {
+				if (italic)
+					printf("");
+				if (bold)
+					printf("");
+				italic = bold = 0;
+				putchar('*');
+				i += 2;
+				continue;
+			} else if (('|' == p[i] && '-' == p[i + 2]) ||
+					('-' == p[i] && '|' == p[i + 1]) ||
+					('+' == p[i] && '-' == p[i + 1]) ||
+					('-' == p[i] && '+' == p[i + 1]) ||
+					('+' == p[i] && '|' == p[i + 1]) ||
+					('|' == p[i] && '+' == p[i + 1]))  {
+				if (italic)
+					printf("");
+				if (bold)
+					printf("");
+				italic = bold = 0;
+				putchar('+');
+				i += 2;
+				continue;
+			}
+
+			/* Bold mode. */
+			
+			if (italic)
+				printf("");
+			if ( ! bold)
+				printf("");
+			bold = 1;
+			italic = 0;
+			i += 2;
+			html_putchar(p[i]);
+		}
+
+		/* 
+		 * Clean up the last character.
+		 * We can get to a newline; don't print that. 
+		 */
+
+		if (italic)
+			printf("");
+		if (bold)
+			printf("");
+
+		if (i == (int)len - 1 && '\n' != p[i])
+			html_putchar(p[i]);
+
+		putchar('\n');
+	}
+
+	puts("
\n" + "\n" + ""); + + fclose(f); +} + +static void +format(const char *file) +{ + struct mparse *mp; + int fd; + struct mdoc *mdoc; + struct man *man; + void *vp; + enum mandoclevel rc; + char opts[MAXPATHLEN + 128]; + + if (-1 == (fd = open(file, O_RDONLY, 0))) { + resp_baddb(); + return; + } + + mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL); + rc = mparse_readfd(mp, fd, file); + close(fd); + + if (rc >= MANDOCLEVEL_FATAL) { + resp_baddb(); + return; + } + + snprintf(opts, sizeof(opts), "style=/man.css," + "man=%s/search.html?sec=%%S&expr=%%N," + /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/, + progname); + + mparse_result(mp, &mdoc, &man); + vp = html_alloc(opts); + + if (NULL != mdoc) { + resp_begin_http(200, NULL); + html_mdoc(vp, mdoc); + } else if (NULL != man) { + resp_begin_http(200, NULL); + html_man(vp, man); + } else + resp_baddb(); + + html_free(vp); + mparse_free(mp); +} + +static void pg_show(const struct manpaths *ps, const struct req *req, char *path) { - pid_t pid; char *sub; - char file[MAXPATHLEN], cmd[MAXPATHLEN]; + char file[MAXPATHLEN]; + const char *fn, *cp; int rc; unsigned int vol, rec; - DB *db; + DB *idx; DBT key, val; if (NULL == path) { - resp_badmanual(); + resp_error400(); return; } else if (NULL == (sub = strrchr(path, '/'))) { - resp_badmanual(); + resp_error400(); return; } else *sub++ = '\0'; if ( ! (atou(path, &vol) && atou(sub, &rec))) { - resp_badmanual(); + resp_error400(); return; } else if (vol >= (unsigned int)ps->sz) { - resp_badmanual(); + resp_error400(); return; } @@ -453,8 +696,8 @@ pg_show(const struct manpaths *ps, const struct req *r /* Open the index recno(3) database. */ - db = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL); - if (NULL == db) { + idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL); + if (NULL == idx) { resp_baddb(); return; } @@ -462,49 +705,37 @@ pg_show(const struct manpaths *ps, const struct req *r key.data = &rec; key.size = 4; - if (0 != (rc = (*db->get)(db, &key, &val, 0))) { - rc < 0 ? resp_baddb() : resp_badmanual(); - (*db->close)(db); - return; + if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) { + rc < 0 ? resp_baddb() : resp_error400(); + goto out; } - /* Extra filename: the first nil-terminated entry. */ + cp = (char *)val.data; - strlcpy(file, ps->paths[vol], MAXPATHLEN); - strlcat(file, "/", MAXPATHLEN); - strlcat(file, (char *)val.data, MAXPATHLEN); - - (*db->close)(db); - - strlcpy(cmd, "man=", MAXPATHLEN); - strlcat(cmd, progname, MAXPATHLEN); - strlcat(cmd, "/search?expr=%N&sec=%S", MAXPATHLEN); - - /* Get ready to call the child mandoc(1) process. */ - - if (-1 == (pid = fork())) - exit(EXIT_FAILURE); - - if (pid > 0) { - waitpid(pid, NULL, 0); - return; + if (NULL == (fn = memchr(cp, '\0', val.size))) + resp_baddb(); + else if (++fn - cp >= (int)val.size) + resp_baddb(); + else if (NULL == memchr(fn, '\0', val.size - (fn - cp))) + resp_baddb(); + else { + strlcpy(file, ps->paths[vol], MAXPATHLEN); + strlcat(file, "/", MAXPATHLEN); + strlcat(file, fn, MAXPATHLEN); + if (0 == strcmp(cp, "cat")) + catman(file); + else + format(file); } - - dup2(STDOUT_FILENO, STDERR_FILENO); - - puts("Content-Type: text/html; charset=utf-8\n"); - - fflush(stdout); - - execlp("mandoc", "mandoc", "-T", - "html", "-O", cmd, file, (char *)NULL); +out: + (*idx->close)(idx); } static void pg_search(const struct manpaths *ps, const struct req *req, char *path) { size_t tt; - int i, sz, rc; + int i, sz, rc, whatis; const char *ep, *start; char **cp; struct opts opt; @@ -514,17 +745,31 @@ pg_search(const struct manpaths *ps, const struct req cp = NULL; ep = NULL; sz = 0; + whatis = 0; memset(&opt, 0, sizeof(struct opts)); for (sz = i = 0; i < (int)req->fieldsz; i++) if (0 == strcmp(req->fields[i].key, "expr")) ep = req->fields[i].val; + else if (0 == strcmp(req->fields[i].key, "query")) + ep = req->fields[i].val; else if (0 == strcmp(req->fields[i].key, "sec")) opt.cat = req->fields[i].val; + else if (0 == strcmp(req->fields[i].key, "sektion")) + opt.cat = req->fields[i].val; else if (0 == strcmp(req->fields[i].key, "arch")) opt.arch = req->fields[i].val; + else if (0 == strcmp(req->fields[i].key, "apropos")) + whatis = 0 == strcmp + (req->fields[i].val, "0"); + else if (0 == strcmp(req->fields[i].key, "op")) + whatis = 0 == strcasecmp + (req->fields[i].val, "whatis"); + if (NULL != opt.cat && 0 == strcmp(opt.cat, "0")) + opt.cat = NULL; + /* * Poor man's tokenisation. * Just break apart by spaces. @@ -553,7 +798,10 @@ pg_search(const struct manpaths *ps, const struct req * The resp_search() function is called with the results. */ - if (NULL != (expr = exprcomp(sz, cp, &tt))) + expr = whatis ? termcomp(sz, cp, &tt) : + exprcomp(sz, cp, &tt); + + if (NULL != expr) rc = apropos_search (ps->sz, ps->paths, &opt, expr, tt, (void *)req, resp_search); @@ -563,7 +811,7 @@ pg_search(const struct manpaths *ps, const struct req if (0 == rc) resp_baddb(); else if (-1 == rc) - resp_badexpr(req); + resp_search(NULL, 0, (void *)req); for (i = 0; i < sz; i++) free(cp[i]); @@ -586,6 +834,15 @@ main(void) if (NULL == progname) progname = ""; + cache = getenv("CACHE_DIR"); + if (NULL == cache) + cache = "/cache/man.cgi"; + + if (-1 == chdir(cache)) { + resp_bad(); + return(EXIT_FAILURE); + } + host = getenv("HTTP_HOST"); if (NULL == host) host = "localhost"; @@ -629,7 +886,7 @@ main(void) /* Initialise MANPATH. */ memset(&paths, 0, sizeof(struct manpaths)); - manpath_parse(&paths, NULL, NULL); + manpath_manconf("etc/catman.conf", &paths); /* Route pages. */ @@ -644,6 +901,7 @@ main(void) pg_show(&paths, &req, subpath); break; default: + resp_error404(path); break; }