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