version 1.17, 2011/12/08 00:48:28 |
version 1.22, 2011/12/10 00:06:34 |
|
|
PAGE__MAX |
PAGE__MAX |
}; |
}; |
|
|
|
/* |
|
* A query as passed to the search function. |
|
* See kval_query() on how this is parsed. |
|
*/ |
|
struct query { |
|
const char *arch; /* architecture */ |
|
const char *sec; /* manual section */ |
|
const char *expr; /* unparsed expression string */ |
|
int whatis; /* whether whatis mode */ |
|
int legacy; /* whether legacy mode */ |
|
}; |
|
|
struct kval { |
struct kval { |
char *key; |
char *key; |
char *val; |
char *val; |
Line 72 static void format(const char *); |
|
Line 84 static void format(const char *); |
|
static void html_print(const char *); |
static void html_print(const char *); |
static void html_putchar(char); |
static void html_putchar(char); |
static int kval_decode(char *); |
static int kval_decode(char *); |
static void kval_parse(struct kval **, size_t *, char *); |
|
static void kval_free(struct kval *, size_t); |
static void kval_free(struct kval *, size_t); |
|
static void kval_parse(struct kval **, size_t *, char *); |
|
static void kval_query(struct query *, |
|
const struct kval *, size_t); |
static void pg_index(const struct manpaths *, |
static void pg_index(const struct manpaths *, |
const struct req *, char *); |
const struct req *, char *); |
static void pg_search(const struct manpaths *, |
static void pg_search(const struct manpaths *, |
Line 102 static const char * const pages[PAGE__MAX] = { |
|
Line 116 static const char * const pages[PAGE__MAX] = { |
|
}; |
}; |
|
|
/* |
/* |
|
* Initialise and parse a query structure from input. |
|
* This accomodates for mdocml's man.cgi and also for legacy man.cgi |
|
* input keys ("sektion" and "apropos"). |
|
* Note that legacy mode has some quirks: if apropos legacy mode is |
|
* detected, we unset the section and architecture string. |
|
*/ |
|
static void |
|
kval_query(struct query *q, const struct kval *fields, size_t sz) |
|
{ |
|
int i, legacy; |
|
|
|
memset(q, 0, sizeof(struct query)); |
|
q->whatis = 1; |
|
legacy = -1; |
|
|
|
for (i = 0; i < (int)sz; i++) |
|
if (0 == strcmp(fields[i].key, "expr")) |
|
q->expr = fields[i].val; |
|
else if (0 == strcmp(fields[i].key, "query")) |
|
q->expr = fields[i].val; |
|
else if (0 == strcmp(fields[i].key, "sec")) |
|
q->sec = fields[i].val; |
|
else if (0 == strcmp(fields[i].key, "sektion")) |
|
q->sec = fields[i].val; |
|
else if (0 == strcmp(fields[i].key, "arch")) |
|
q->arch = fields[i].val; |
|
else if (0 == strcmp(fields[i].key, "apropos")) |
|
legacy = 0 == strcmp |
|
(fields[i].val, "0"); |
|
else if (0 == strcmp(fields[i].key, "op")) |
|
q->whatis = 0 == strcasecmp |
|
(fields[i].val, "whatis"); |
|
|
|
/* Test for old man.cgi compatibility mode. */ |
|
|
|
if (legacy == 0) { |
|
q->whatis = 0; |
|
q->legacy = 1; |
|
} else if (legacy > 0) { |
|
q->legacy = 1; |
|
q->whatis = 1; |
|
} |
|
|
|
/* Section "0" means no section when in legacy mode. */ |
|
|
|
if (q->legacy && NULL != q->sec && 0 == strcmp(q->sec, "0")) |
|
q->sec = NULL; |
|
} |
|
|
|
/* |
* This is just OpenBSD's strtol(3) suggestion. |
* This is just OpenBSD's strtol(3) suggestion. |
* I use it instead of strtonum(3) for portability's sake. |
* I use it instead of strtonum(3) for portability's sake. |
*/ |
*/ |
Line 124 atou(const char *buf, unsigned *v) |
|
Line 188 atou(const char *buf, unsigned *v) |
|
return(1); |
return(1); |
} |
} |
|
|
|
/* |
|
* Print a character, escaping HTML along the way. |
|
* This will pass non-ASCII straight to output: be warned! |
|
*/ |
static void |
static void |
html_putchar(char c) |
html_putchar(char c) |
{ |
{ |
Line 148 html_putchar(char c) |
|
Line 216 html_putchar(char c) |
|
} |
} |
|
|
/* |
/* |
* Print a word, escaping HTML along the way. |
* Call through to html_putchar(). |
* This will pass non-ASCII straight to output: be warned! |
* Accepts NULL strings. |
*/ |
*/ |
static void |
static void |
html_print(const char *p) |
html_print(const char *p) |
Line 277 resp_begin_http(int code, const char *msg) |
|
Line 345 resp_begin_http(int code, const char *msg) |
|
if (200 != code) |
if (200 != code) |
printf("Status: %d %s\n", code, msg); |
printf("Status: %d %s\n", code, msg); |
|
|
puts("Content-Type: text/html; charset=utf-8" "\n" |
puts("Content-Type: text/html; charset=utf-8\n" |
"Cache-Control: no-cache" "\n" |
"Cache-Control: no-cache\n" |
"Pragma: no-cache" "\n" |
"Pragma: no-cache\n" |
""); |
""); |
|
|
fflush(stdout); |
fflush(stdout); |
Line 291 resp_begin_html(int code, const char *msg) |
|
Line 359 resp_begin_html(int code, const char *msg) |
|
|
|
resp_begin_http(code, msg); |
resp_begin_http(code, msg); |
|
|
puts("<!DOCTYPE HTML PUBLIC " "\n" |
puts("<!DOCTYPE HTML PUBLIC " |
" \"-//W3C//DTD HTML 4.01//EN\"" "\n" |
" \"-//W3C//DTD HTML 4.01//EN\"" |
" \"http://www.w3.org/TR/html4/strict.dtd\">" "\n" |
" \"http://www.w3.org/TR/html4/strict.dtd\">\n" |
"<HTML>" "\n" |
"<HTML>\n" |
" <HEAD>" "\n" |
"<HEAD>\n" |
" <META HTTP-EQUIV=\"Content-Type\" " "\n" |
"<META HTTP-EQUIV=\"Content-Type\"" |
" CONTENT=\"text/html; charset=utf-8\">" "\n" |
" CONTENT=\"text/html; charset=utf-8\">\n" |
" <LINK REL=\"stylesheet\" HREF=\"/man.cgi.css\"" "\n" |
"<LINK REL=\"stylesheet\" HREF=\"/man.cgi.css\"" |
" TYPE=\"text/css\" media=\"all\">" "\n" |
" TYPE=\"text/css\" media=\"all\">\n" |
" <TITLE>System Manpage Reference</TITLE>" "\n" |
"<TITLE>System Manpage Reference</TITLE>\n" |
" </HEAD>" "\n" |
"</HEAD>\n" |
" <BODY>" "\n" |
"<BODY>\n" |
"<!-- Begin page content. //-->"); |
"<!-- Begin page content. //-->"); |
} |
} |
|
|
|
|
resp_end_html(void) |
resp_end_html(void) |
{ |
{ |
|
|
puts(" </BODY>\n</HTML>"); |
puts("</BODY>\n" |
|
"</HTML>"); |
} |
} |
|
|
static void |
static void |
resp_searchform(const struct req *req) |
resp_searchform(const struct req *req) |
{ |
{ |
int i; |
struct query q; |
const char *expr, *sec, *arch; |
|
|
|
expr = sec = arch = ""; |
kval_query(&q, req->fields, req->fieldsz); |
|
|
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("<!-- Begin search form. //-->"); |
puts("<!-- Begin search form. //-->"); |
printf("<FORM ACTION=\""); |
printf("<FORM ACTION=\""); |
html_print(progname); |
html_print(progname); |
printf("/search.html\" METHOD=\"get\">\n"); |
printf("/search.html\" METHOD=\"get\">\n"); |
printf("<FIELDSET>\n" |
printf("<FIELDSET>\n" |
"<LEGEND>Search Parameters</LEGEND>\n" |
"<LEGEND>Search Parameters</LEGEND>\n" |
"<INPUT TYPE=\"submit\" NAME=\"op\" " |
"<INPUT TYPE=\"submit\" NAME=\"op\"" |
"VALUE=\"Whatis\"> or \n" |
" VALUE=\"Whatis\"> or \n" |
"<INPUT TYPE=\"submit\" NAME=\"op\" " |
"<INPUT TYPE=\"submit\" NAME=\"op\"" |
"VALUE=\"apropos\"> for manuals satisfying \n" |
" VALUE=\"apropos\"> for manuals satisfying \n" |
"<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\""); |
"<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\""); |
html_print(expr); |
html_print(q.expr ? q.expr : ""); |
printf("\">, section " |
printf("\">, section " |
"<INPUT TYPE=\"text\" " |
"<INPUT TYPE=\"text\"" |
"SIZE=\"4\" NAME=\"sec\" VALUE=\""); |
" SIZE=\"4\" NAME=\"sec\" VALUE=\""); |
html_print(sec); |
html_print(q.sec ? q.sec : ""); |
printf("\">, arch " |
printf("\">, arch " |
"<INPUT TYPE=\"text\" " |
"<INPUT TYPE=\"text\"" |
"SIZE=\"8\" NAME=\"arch\" VALUE=\""); |
" SIZE=\"8\" NAME=\"arch\" VALUE=\""); |
html_print(arch); |
html_print(q.arch ? q.arch : ""); |
puts("\">.\n" |
puts("\">.\n" |
"<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n" |
"<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n" |
"</FIELDSET>\n" |
"</FIELDSET>\n" |
"</FORM>\n" |
"</FORM>"); |
"<!-- End search form. //-->"); |
puts("<!-- End search form. //-->"); |
} |
} |
|
|
static void |
static void |
Line 379 resp_error400(void) |
|
Line 432 resp_error400(void) |
|
resp_begin_html(400, "Query Malformed"); |
resp_begin_html(400, "Query Malformed"); |
printf("<H1>Malformed Query</H1>\n" |
printf("<H1>Malformed Query</H1>\n" |
"<P>\n" |
"<P>\n" |
" The query your entered was malformed.\n" |
"The query your entered was malformed.\n" |
" Try again from the\n" |
"Try again from the\n" |
" <A HREF=\"%s/index.html\">main page</A>\n" |
"<A HREF=\"%s/index.html\">main page</A>.\n" |
"</P>", progname); |
"</P>", progname); |
resp_end_html(); |
resp_end_html(); |
} |
} |
Line 393 resp_error404(const char *page) |
|
Line 446 resp_error404(const char *page) |
|
resp_begin_html(404, "Not Found"); |
resp_begin_html(404, "Not Found"); |
puts("<H1>Page Not Found</H1>\n" |
puts("<H1>Page Not Found</H1>\n" |
"<P>\n" |
"<P>\n" |
" The page you're looking for, "); |
"The page you're looking for, "); |
printf(" <B>"); |
printf("<B>"); |
html_print(page); |
html_print(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/index.html\">main page</A>.\n" |
"</P>", progname); |
"</P>", progname); |
resp_end_html(); |
resp_end_html(); |
} |
} |
Line 424 resp_baddb(void) |
|
Line 477 resp_baddb(void) |
|
static void |
static void |
resp_search(struct res *r, size_t sz, void *arg) |
resp_search(struct res *r, size_t sz, void *arg) |
{ |
{ |
int i; |
int i; |
|
struct query q; |
|
const struct req *req; |
|
|
if (1 == sz) { |
if (1 == sz) { |
/* |
/* |
Line 442 resp_search(struct res *r, size_t sz, void *arg) |
|
Line 497 resp_search(struct res *r, size_t sz, void *arg) |
|
qsort(r, sz, sizeof(struct res), cmp); |
qsort(r, sz, sizeof(struct res), cmp); |
|
|
resp_begin_html(200, NULL); |
resp_begin_html(200, NULL); |
resp_searchform((const struct req *)arg); |
|
|
|
|
req = (const struct req *)arg; |
|
resp_searchform(req); |
|
kval_query(&q, req->fields, req->fieldsz); |
|
|
if (0 == sz) { |
if (0 == sz) { |
puts("<P>No results found.</P>"); |
printf("<P>\n" |
|
"No %s results found.", |
|
q.whatis ? "whatis" : "apropos"); |
|
if (q.whatis) { |
|
printf("(Try <A HREF=\""); |
|
html_print(progname); |
|
printf("/search.html?op=apropos&expr="); |
|
html_print(q.expr ? q.expr : ""); |
|
printf("&sec="); |
|
html_print(q.sec ? q.sec : ""); |
|
printf("&arch="); |
|
html_print(q.arch ? q.arch : ""); |
|
puts("\">apropos</A>?)"); |
|
} |
|
puts("</P>"); |
resp_end_html(); |
resp_end_html(); |
return; |
return; |
} |
} |
Line 454 resp_search(struct res *r, size_t sz, void *arg) |
|
Line 526 resp_search(struct res *r, size_t sz, void *arg) |
|
"<TABLE>"); |
"<TABLE>"); |
|
|
for (i = 0; i < (int)sz; i++) { |
for (i = 0; i < (int)sz; i++) { |
printf("<TR><TD CLASS=\"title\"><A HREF=\""); |
printf("<TR>\n" |
|
"<TD CLASS=\"title\">\n" |
|
"<A HREF=\""); |
html_print(progname); |
html_print(progname); |
printf("/show/%u/%u.html\">", r[i].volume, r[i].rec); |
printf("/show/%u/%u.html\">", r[i].volume, r[i].rec); |
html_print(r[i].title); |
html_print(r[i].title); |
Line 464 resp_search(struct res *r, size_t sz, void *arg) |
|
Line 538 resp_search(struct res *r, size_t sz, void *arg) |
|
putchar('/'); |
putchar('/'); |
html_print(r[i].arch); |
html_print(r[i].arch); |
} |
} |
printf(")</A></TD><TD CLASS=\"desc\">"); |
printf(")</A>\n" |
|
"</TD>\n" |
|
"<TD CLASS=\"desc\">"); |
html_print(r[i].desc); |
html_print(r[i].desc); |
puts("</TD></TR>"); |
puts("</TD>\n" |
|
"</TR>"); |
} |
} |
|
|
puts("</TABLE>"); |
puts("</TABLE>"); |
|
|
resp_end_html(); |
resp_end_html(); |
} |
} |
|
|
Line 497 catman(const char *file) |
|
Line 573 catman(const char *file) |
|
} |
} |
|
|
resp_begin_http(200, NULL); |
resp_begin_http(200, NULL); |
puts("<!DOCTYPE HTML PUBLIC " "\n" |
puts("<!DOCTYPE HTML PUBLIC " |
" \"-//W3C//DTD HTML 4.01//EN\"" "\n" |
" \"-//W3C//DTD HTML 4.01//EN\"" |
" \"http://www.w3.org/TR/html4/strict.dtd\">" "\n" |
" \"http://www.w3.org/TR/html4/strict.dtd\">\n" |
"<HTML>" "\n" |
"<HTML>\n" |
" <HEAD>" "\n" |
"<HEAD>\n" |
" <META HTTP-EQUIV=\"Content-Type\" " "\n" |
"<META HTTP-EQUIV=\"Content-Type\"" |
" CONTENT=\"text/html; charset=utf-8\">" "\n" |
" CONTENT=\"text/html; charset=utf-8\">\n" |
" <LINK REL=\"stylesheet\" HREF=\"/catman.css\"" "\n" |
"<LINK REL=\"stylesheet\" HREF=\"/catman.css\"" |
" TYPE=\"text/css\" media=\"all\">" "\n" |
" TYPE=\"text/css\" media=\"all\">\n" |
" <TITLE>System Manpage Reference</TITLE>" "\n" |
"<TITLE>System Manpage Reference</TITLE>\n" |
" </HEAD>" "\n" |
"</HEAD>\n" |
" <BODY>" "\n" |
"<BODY>\n" |
"<!-- Begin page content. //-->"); |
"<!-- Begin page content. //-->\n" |
|
"<PRE>"); |
|
|
puts("<PRE>"); |
|
while (NULL != (p = fgetln(f, &len))) { |
while (NULL != (p = fgetln(f, &len))) { |
bold = italic = 0; |
bold = italic = 0; |
for (i = 0; i < (int)len - 1; i++) { |
for (i = 0; i < (int)len - 1; i++) { |
Line 731 pg_show(const struct manpaths *ps, const struct req *r |
|
Line 807 pg_show(const struct manpaths *ps, const struct req *r |
|
resp_baddb(); |
resp_baddb(); |
else { |
else { |
strlcpy(file, cache, MAXPATHLEN); |
strlcpy(file, cache, MAXPATHLEN); |
strlcpy(file, "/", MAXPATHLEN); |
strlcat(file, "/", MAXPATHLEN); |
strlcat(file, fn, MAXPATHLEN); |
strlcat(file, fn, MAXPATHLEN); |
if (0 == strcmp(cp, "cat")) |
if (0 == strcmp(cp, "cat")) |
catman(file); |
catman(file); |
|
|
pg_search(const struct manpaths *ps, const struct req *req, char *path) |
pg_search(const struct manpaths *ps, const struct req *req, char *path) |
{ |
{ |
size_t tt; |
size_t tt; |
int i, sz, rc, whatis; |
int i, sz, rc; |
const char *ep, *start; |
const char *ep, *start; |
char **cp; |
char **cp; |
struct opts opt; |
struct opts opt; |
struct expr *expr; |
struct expr *expr; |
|
struct query q; |
|
|
expr = NULL; |
kval_query(&q, req->fields, req->fieldsz); |
cp = NULL; |
|
ep = NULL; |
|
sz = 0; |
|
whatis = 1; |
|
|
|
memset(&opt, 0, sizeof(struct opts)); |
memset(&opt, 0, sizeof(struct opts)); |
|
|
for (sz = i = 0; i < (int)req->fieldsz; i++) |
ep = q.expr; |
if (0 == strcmp(req->fields[i].key, "expr")) |
opt.arch = q.arch; |
ep = req->fields[i].val; |
opt.cat = q.sec; |
else if (0 == strcmp(req->fields[i].key, "query")) |
rc = -1; |
ep = req->fields[i].val; |
sz = 0; |
else if (0 == strcmp(req->fields[i].key, "sec")) |
cp = NULL; |
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. |
* Poor man's tokenisation. |
* Just break apart by spaces. |
* Just break apart by spaces. |
Line 802 pg_search(const struct manpaths *ps, const struct req |
|
Line 860 pg_search(const struct manpaths *ps, const struct req |
|
ep++; |
ep++; |
} |
} |
|
|
rc = -1; |
|
|
|
/* |
/* |
* Pump down into apropos backend. |
* Pump down into apropos backend. |
* The resp_search() function is called with the results. |
* The resp_search() function is called with the results. |
*/ |
*/ |
|
|
expr = whatis ? termcomp(sz, cp, &tt) : |
expr = q.whatis ? termcomp(sz, cp, &tt) : |
exprcomp(sz, cp, &tt); |
exprcomp(sz, cp, &tt); |
|
|
if (NULL != expr) |
if (NULL != expr) |
rc = apropos_search |
rc = apropos_search |