-/* $Id: cgi.c,v 1.53 2014/07/09 08:53:28 schwarze Exp $ */
+/* $Id: cgi.c,v 1.61 2014/07/10 00:52:50 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014 Ingo Schwarze <schwarze@usta.de>
#endif
#include <ctype.h>
-#include <dirent.h>
+#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
-#if defined(__sun)
-/* for stat() */
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#endif
-
#include "mandoc.h"
#include "mandoc_aux.h"
#include "main.h"
* A query as passed to the search function.
*/
struct query {
- const char *manroot; /* manual root directory */
+ const char *manpath; /* desired manual directory */
const char *arch; /* architecture */
const char *sec; /* manual section */
const char *expr; /* unparsed expression string */
};
struct req {
- struct query q;
- char **p; /* array of available manroots */
- size_t psz;
- enum page page;
+ struct query q;
+ char **p; /* array of available manpaths */
+ size_t psz; /* number of available manpaths */
+ enum page page;
};
static void catman(const struct req *, const char *);
static void http_print(const char *);
static void http_putchar(char);
static void http_printquery(const struct req *);
-static void pathgen(DIR *, struct req *);
+static void pathgen(struct req *);
static void pg_index(const struct req *, char *);
static void pg_search(const struct req *, char *);
static void pg_show(const struct req *, char *);
-static void resp_bad(void);
-static void resp_baddb(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);
+static void resp_error_badrequest(const char *);
+static void resp_error_internal(void);
+static void resp_error_notfound(const char *);
static void resp_index(const struct req *);
+static void resp_noresult(const struct req *,
+ const char *);
static void resp_search(const struct req *,
struct manpage *, size_t);
static void resp_searchform(const struct req *);
-static const char *progname; /* cgi script name */
-static const char *cache; /* cache directory */
-static const char *css; /* css directory */
-static const char *host; /* hostname */
+static const char *scriptname; /* CGI script name */
+static const char *mandir; /* contains all manpath directories */
+static const char *cssdir; /* css directory */
+static const char *httphost; /* hostname used in the URIs */
static const char * const pages[PAGE__MAX] = {
"index", /* PAGE_INDEX */
break;
}
}
+
static void
http_printquery(const struct req *req)
{
- if (NULL != req->q.manroot) {
+ if (NULL != req->q.manpath) {
printf("&manpath=");
- http_print(req->q.manroot);
+ http_print(req->q.manpath);
}
if (NULL != req->q.sec) {
printf("&sec=");
}
}
-
static void
html_printquery(const struct req *req)
{
- if (NULL != req->q.manroot) {
+ if (NULL != req->q.manpath) {
printf("&manpath=");
- html_print(req->q.manroot);
+ html_print(req->q.manpath);
}
if (NULL != req->q.sec) {
printf("&sec=");
int legacy;
memset(&req->q, 0, sizeof(struct query));
- req->q.manroot = req->p[0];
+ req->q.manpath = req->p[0];
legacy = -1;
while ('\0' != *p) {
else if (0 == strcmp(key, "arch"))
req->q.arch = val;
else if (0 == strcmp(key, "manpath"))
- req->q.manroot = val;
+ req->q.manpath = val;
else if (0 == strcmp(key, "apropos"))
legacy = 0 == strcmp(val, "0");
}
"<TITLE>System Manpage Reference</TITLE>\n"
"</HEAD>\n"
"<BODY>\n"
- "<!-- Begin page content. //-->\n", css, css);
+ "<!-- Begin page content. //-->\n",
+ cssdir, cssdir);
}
static void
"<FIELDSET>\n"
"<LEGEND>Search Parameters</LEGEND>\n"
"<INPUT TYPE=\"submit\" "
- " VALUE=\"Search\"> for manuals satisfying \n"
+ " VALUE=\"Search\"> for manuals matching \n"
"<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
- progname);
+ scriptname);
html_print(req->q.expr ? req->q.expr : "");
printf("\">, section "
"<INPUT TYPE=\"text\""
html_print(req->q.arch ? req->q.arch : "");
printf("\">");
if (req->psz > 1) {
- puts(", <SELECT NAME=\"manpath\">");
+ puts(", in <SELECT NAME=\"manpath\">");
for (i = 0; i < (int)req->psz; i++) {
printf("<OPTION ");
- if (NULL == req->q.manroot ? 0 == i :
- 0 == strcmp(req->q.manroot, req->p[i]))
+ if (NULL == req->q.manpath ? 0 == i :
+ 0 == strcmp(req->q.manpath, req->p[i]))
printf("SELECTED=\"selected\" ");
printf("VALUE=\"");
html_print(req->p[i]);
}
puts("</SELECT>");
}
- puts(".\n"
+ puts("—\n"
"<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
"</FIELDSET>\n"
"</FORM>\n"
{
resp_begin_html(200, NULL);
+ puts("<H1>\n"
+ "Online manuals with "
+ "<A HREF=\"http://mdocml.bsd.lv/\">mandoc</A>\n"
+ "</H1>");
resp_searchform(req);
+ puts("<P>\n"
+ "This web interface is documented in the "
+ "<A HREF=\"search?expr=Nm~^man\\.cgi$&sec=8\">"
+ "man.cgi</A> manual, and the "
+ "<A HREF=\"search?expr=Nm~^apropos$&sec=1\">"
+ "apropos</A> manual explains the query syntax.\n"
+ "</P>");
resp_end_html();
}
static void
-resp_error400(void)
+resp_noresult(const struct req *req, const char *msg)
{
+ resp_begin_html(200, NULL);
+ resp_searchform(req);
+ puts("<P>");
+ puts(msg);
+ puts("</P>");
+ resp_end_html();
+}
- resp_begin_html(400, "Query Malformed");
- printf("<H1>Malformed Query</H1>\n"
- "<P>\n"
- "The query your entered was malformed.\n"
- "Try again from the\n"
- "<A HREF=\"%s/index.html\">main page</A>.\n"
- "</P>", progname);
+static void
+resp_error_badrequest(const char *msg)
+{
+
+ resp_begin_html(400, "Bad Request");
+ puts("<H1>Bad Request</H1>\n"
+ "<P>\n");
+ puts(msg);
+ printf("Try again from the\n"
+ "<A HREF=\"%s\">main page</A>.\n"
+ "</P>", scriptname);
resp_end_html();
}
static void
-resp_error404(const char *page)
+resp_error_notfound(const char *page)
{
resp_begin_html(404, "Not Found");
printf("</B>,\n"
"could not be found.\n"
"Try searching from the\n"
- "<A HREF=\"%s/index.html\">main page</A>.\n"
- "</P>", progname);
+ "<A HREF=\"%s\">main page</A>.\n"
+ "</P>", scriptname);
resp_end_html();
}
static void
-resp_bad(void)
+resp_error_internal(void)
{
resp_begin_html(500, "Internal Server Error");
- puts("<P>Generic badness happened.</P>");
- resp_end_html();
-}
-
-static void
-resp_baddb(void)
-{
-
- resp_begin_html(500, "Internal Server Error");
- puts("<P>Your database is broken.</P>");
+ puts("<P>Internal Server Error</P>");
resp_end_html();
}
*/
puts("Status: 303 See Other");
printf("Location: http://%s%s/show/%s/%s?",
- host, progname, req->q.manroot, r[0].file);
+ httphost, scriptname, req->q.manpath, r[0].file);
http_printquery(req);
puts("\n"
"Content-Type: text/html; charset=utf-8\n");
return;
}
+ qsort(r, sz, sizeof(struct manpage), cmp);
+
resp_begin_html(200, NULL);
resp_searchform(req);
-
puts("<DIV CLASS=\"results\">");
-
- if (0 == sz) {
- puts("<P>\n"
- "No results found.\n"
- "</P>\n"
- "</DIV>");
- resp_end_html();
- return;
- }
-
- qsort(r, sz, sizeof(struct manpage), cmp);
-
puts("<TABLE>");
for (i = 0; i < sz; i++) {
printf("<TR>\n"
"<TD CLASS=\"title\">\n"
"<A HREF=\"%s/show/%s/%s?",
- progname, req->q.manroot, r[i].file);
+ scriptname, req->q.manpath, r[i].file);
html_printquery(req);
printf("\">");
html_print(r[i].names);
int italic, bold;
if (NULL == (f = fopen(file, "r"))) {
- resp_baddb();
+ resp_error_badrequest(
+ "You specified an invalid manual file.");
return;
}
char opts[PATH_MAX + 128];
if (-1 == (fd = open(file, O_RDONLY, 0))) {
- resp_baddb();
+ resp_error_badrequest(
+ "You specified an invalid manual file.");
return;
}
- mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL, NULL);
+ mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,
+ req->q.manpath);
rc = mparse_readfd(mp, fd, file);
close(fd);
if (rc >= MANDOCLEVEL_FATAL) {
- resp_baddb();
+ fprintf(stderr, "fatal mandoc error: %s/%s\n",
+ req->q.manpath, file);
+ resp_error_internal();
return;
}
snprintf(opts, sizeof(opts),
"fragment,man=%s/search?sec=%%S&expr=Nm~^%%N$",
- progname);
+ scriptname);
mparse_result(mp, &mdoc, &man, NULL);
if (NULL == man && NULL == mdoc) {
- resp_baddb();
+ fprintf(stderr, "fatal mandoc error: %s/%s\n",
+ req->q.manpath, file);
+ resp_error_internal();
mparse_free(mp);
return;
}
char *sub;
if (NULL == path || NULL == (sub = strchr(path, '/'))) {
- resp_error400();
+ resp_error_badrequest(
+ "You did not specify a page to show.");
return;
}
*sub++ = '\0';
/*
- * Begin by chdir()ing into the manroot.
+ * Begin by chdir()ing into the manpath.
* This way we can pick up the database files, which are
* relative to the manpath root.
*/
if (-1 == chdir(path)) {
- perror(path);
- resp_baddb();
+ resp_error_badrequest(
+ "You specified an invalid manpath.");
return;
}
* relative to the manpath root.
*/
- if (-1 == (chdir(req->q.manroot))) {
- perror(req->q.manroot);
- resp_search(req, NULL, 0);
+ if (-1 == (chdir(req->q.manpath))) {
+ resp_error_badrequest(
+ "You specified an invalid manpath.");
return;
}
ep++;
}
- if (mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
- resp_search(req, res, ressz);
+ if (0 == mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
+ resp_noresult(req, "You entered an invalid query.");
+ else if (0 == ressz)
+ resp_noresult(req, "No results found.");
else
- resp_baddb();
+ resp_search(req, res, ressz);
for (i = 0; i < sz; i++)
free(cp[i]);
main(void)
{
int i;
- DIR *cwd;
struct req req;
- char *p, *path, *subpath;
+ char *querystring, *path, *subpath;
/* Scan our run-time environment. */
- if (NULL == (cache = getenv("CACHE_DIR")))
- cache = "/cache/man.cgi";
+ if (NULL == (mandir = getenv("MAN_DIR")))
+ mandir = "/man";
- if (NULL == (progname = getenv("SCRIPT_NAME")))
- progname = "";
+ if (NULL == (scriptname = getenv("SCRIPT_NAME")))
+ scriptname = "";
- if (NULL == (css = getenv("CSS_DIR")))
- css = "";
+ if (NULL == (cssdir = getenv("CSS_DIR")))
+ cssdir = "";
- if (NULL == (host = getenv("HTTP_HOST")))
- host = "localhost";
+ if (NULL == (httphost = getenv("HTTP_HOST")))
+ httphost = "localhost";
/*
- * First we change directory into the cache directory so that
+ * First we change directory into the mandir so that
* subsequent scanning for manpath directories is rooted
* relative to the same position.
*/
- if (-1 == chdir(cache)) {
- perror(cache);
- resp_bad();
- return(EXIT_FAILURE);
- } else if (NULL == (cwd = opendir(cache))) {
- perror(cache);
- resp_bad();
+ if (-1 == chdir(mandir)) {
+ fprintf(stderr, "MAN_DIR: %s: %s\n",
+ mandir, strerror(errno));
+ resp_error_internal();
return(EXIT_FAILURE);
}
memset(&req, 0, sizeof(struct req));
-
- pathgen(cwd, &req);
- closedir(cwd);
+ pathgen(&req);
/* Next parse out the query string. */
- if (NULL != (p = getenv("QUERY_STRING")))
- http_parse(&req, p);
+ if (NULL != (querystring = getenv("QUERY_STRING")))
+ http_parse(&req, querystring);
/*
* Now juggle paths to extract information.
pg_show(&req, subpath);
break;
default:
- resp_error404(path);
+ resp_error_notfound(path);
break;
}
* Scan for indexable paths.
*/
static void
-pathgen(DIR *dir, struct req *req)
+pathgen(struct req *req)
{
- struct dirent *d;
-#if defined(__sun)
- struct stat sb;
-#endif
+ FILE *fp;
+ char *dp;
+ size_t dpsz;
- while (NULL != (d = readdir(dir))) {
-#if defined(__sun)
- stat(d->d_name, &sb);
- if (!(S_IFDIR & sb.st_mode)
-#else
- if (DT_DIR != d->d_type
-#endif
- || '.' != d->d_name[0]) {
- req->p = mandoc_realloc(req->p,
- (req->psz + 1) * sizeof(char *));
- req->p[req->psz++] = mandoc_strdup(d->d_name);
- }
+ if (NULL == (fp = fopen("manpath.conf", "r")))
+ return;
+
+ while (NULL != (dp = fgetln(fp, &dpsz))) {
+ if ('\n' == dp[dpsz - 1])
+ dpsz--;
+ req->p = mandoc_realloc(req->p,
+ (req->psz + 1) * sizeof(char *));
+ req->p[req->psz++] = mandoc_strndup(dp, dpsz);
}
}