version 1.117, 2016/03/17 21:22:59 |
version 1.123, 2016/04/15 00:14:17 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@usta.de> |
* Copyright (c) 2014, 2015, 2016 Ingo Schwarze <schwarze@usta.de> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
struct query q; |
struct query q; |
char **p; /* array of available manpaths */ |
char **p; /* array of available manpaths */ |
size_t psz; /* number of available manpaths */ |
size_t psz; /* number of available manpaths */ |
|
int isquery; /* QUERY_STRING used, not PATH_INFO */ |
}; |
}; |
|
|
static void catman(const struct req *, const char *); |
static void catman(const struct req *, const char *); |
Line 84 static int validate_filename(const char *); |
|
Line 85 static int validate_filename(const char *); |
|
static int validate_manpath(const struct req *, const char *); |
static int validate_manpath(const struct req *, const char *); |
static int validate_urifrag(const char *); |
static int validate_urifrag(const char *); |
|
|
static const char *scriptname; /* CGI script name */ |
static const char *scriptname = SCRIPT_NAME; |
|
|
static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; |
static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9}; |
static const char *const sec_numbers[] = { |
static const char *const sec_numbers[] = { |
Line 188 http_parse(struct req *req, const char *qs) |
|
Line 189 http_parse(struct req *req, const char *qs) |
|
char *key, *val; |
char *key, *val; |
size_t keysz, valsz; |
size_t keysz, valsz; |
|
|
|
req->isquery = 1; |
req->q.manpath = NULL; |
req->q.manpath = NULL; |
req->q.arch = NULL; |
req->q.arch = NULL; |
req->q.sec = NULL; |
req->q.sec = NULL; |
Line 370 resp_searchform(const struct req *req) |
|
Line 372 resp_searchform(const struct req *req) |
|
|
|
puts("<!-- Begin search form. //-->"); |
puts("<!-- Begin search form. //-->"); |
printf("<DIV ID=\"mancgi\">\n" |
printf("<DIV ID=\"mancgi\">\n" |
"<FORM ACTION=\"%s\" METHOD=\"get\">\n" |
"<FORM ACTION=\"/%s\" METHOD=\"get\">\n" |
"<FIELDSET>\n" |
"<FIELDSET>\n" |
"<LEGEND>Manual Page Search Parameters</LEGEND>\n", |
"<LEGEND>Manual Page Search Parameters</LEGEND>\n", |
scriptname); |
scriptname); |
Line 507 pg_index(const struct req *req) |
|
Line 509 pg_index(const struct req *req) |
|
resp_searchform(req); |
resp_searchform(req); |
printf("<P>\n" |
printf("<P>\n" |
"This web interface is documented in the\n" |
"This web interface is documented in the\n" |
"<A HREF=\"%s/mandoc/man8/man.cgi.8\">man.cgi</A>\n" |
"<A HREF=\"/%s%smandoc/man8/man.cgi.8\">man.cgi</A>\n" |
"manual, and the\n" |
"manual, and the\n" |
"<A HREF=\"%s/mandoc/man1/apropos.1\">apropos</A>\n" |
"<A HREF=\"/%s%smandoc/man1/apropos.1\">apropos</A>\n" |
"manual explains the query syntax.\n" |
"manual explains the query syntax.\n" |
"</P>\n", |
"</P>\n", |
scriptname, scriptname); |
scriptname, *scriptname == '\0' ? "" : "/", |
|
scriptname, *scriptname == '\0' ? "" : "/"); |
resp_end_html(); |
resp_end_html(); |
} |
} |
|
|
Line 536 pg_error_badrequest(const char *msg) |
|
Line 539 pg_error_badrequest(const char *msg) |
|
"<P>\n"); |
"<P>\n"); |
puts(msg); |
puts(msg); |
printf("Try again from the\n" |
printf("Try again from the\n" |
"<A HREF=\"%s\">main page</A>.\n" |
"<A HREF=\"/%s\">main page</A>.\n" |
"</P>", scriptname); |
"</P>", scriptname); |
resp_end_html(); |
resp_end_html(); |
} |
} |
|
|
pg_searchres(const struct req *req, struct manpage *r, size_t sz) |
pg_searchres(const struct req *req, struct manpage *r, size_t sz) |
{ |
{ |
char *arch, *archend; |
char *arch, *archend; |
size_t i, iuse, isec; |
const char *sec; |
|
size_t i, iuse; |
int archprio, archpriouse; |
int archprio, archpriouse; |
int prio, priouse; |
int prio, priouse; |
char sec; |
|
|
|
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
if (validate_filename(r[i].file)) |
if (validate_filename(r[i].file)) |
Line 567 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 570 pg_searchres(const struct req *req, struct manpage *r, |
|
return; |
return; |
} |
} |
|
|
if (1 == sz) { |
if (req->isquery && sz == 1) { |
/* |
/* |
* If we have just one result, then jump there now |
* If we have just one result, then jump there now |
* without any delay. |
* without any delay. |
*/ |
*/ |
printf("Status: 303 See Other\r\n"); |
printf("Status: 303 See Other\r\n"); |
printf("Location: http://%s%s/%s/%s", |
printf("Location: http://%s/%s%s%s/%s", |
HTTP_HOST, scriptname, req->q.manpath, r[0].file); |
HTTP_HOST, scriptname, |
|
*scriptname == '\0' ? "" : "/", |
|
req->q.manpath, r[0].file); |
printf("\r\n" |
printf("\r\n" |
"Content-Type: text/html; charset=utf-8\r\n" |
"Content-Type: text/html; charset=utf-8\r\n" |
"\r\n"); |
"\r\n"); |
Line 583 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 588 pg_searchres(const struct req *req, struct manpage *r, |
|
|
|
resp_begin_html(200, NULL); |
resp_begin_html(200, NULL); |
resp_searchform(req); |
resp_searchform(req); |
puts("<DIV CLASS=\"results\">"); |
|
puts("<TABLE>"); |
|
|
|
for (i = 0; i < sz; i++) { |
if (sz > 1) { |
printf("<TR>\n" |
puts("<DIV CLASS=\"results\">"); |
"<TD CLASS=\"title\">\n" |
puts("<TABLE>"); |
"<A HREF=\"%s/%s/%s", |
|
scriptname, req->q.manpath, r[i].file); |
|
printf("\">"); |
|
html_print(r[i].names); |
|
printf("</A>\n" |
|
"</TD>\n" |
|
"<TD CLASS=\"desc\">"); |
|
html_print(r[i].output); |
|
puts("</TD>\n" |
|
"</TR>"); |
|
} |
|
|
|
puts("</TABLE>\n" |
for (i = 0; i < sz; i++) { |
"</DIV>"); |
printf("<TR>\n" |
|
"<TD CLASS=\"title\">\n" |
|
"<A HREF=\"/%s%s%s/%s", |
|
scriptname, *scriptname == '\0' ? "" : "/", |
|
req->q.manpath, r[i].file); |
|
printf("\">"); |
|
html_print(r[i].names); |
|
printf("</A>\n" |
|
"</TD>\n" |
|
"<TD CLASS=\"desc\">"); |
|
html_print(r[i].output); |
|
puts("</TD>\n" |
|
"</TR>"); |
|
} |
|
|
|
puts("</TABLE>\n" |
|
"</DIV>"); |
|
} |
|
|
/* |
/* |
* In man(1) mode, show one of the pages |
* In man(1) mode, show one of the pages |
* even if more than one is found. |
* even if more than one is found. |
*/ |
*/ |
|
|
if (req->q.equal) { |
if (req->q.equal || sz == 1) { |
puts("<HR>"); |
puts("<HR>"); |
iuse = 0; |
iuse = 0; |
priouse = 10; |
priouse = 20; |
archpriouse = 3; |
archpriouse = 3; |
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
isec = strcspn(r[i].file, "123456789"); |
sec = r[i].file; |
sec = r[i].file[isec]; |
sec += strcspn(sec, "123456789"); |
if ('\0' == sec) |
if (sec[0] == '\0') |
continue; |
continue; |
prio = sec_prios[sec - '1']; |
prio = sec_prios[sec[0] - '1']; |
if (NULL == req->q.arch) { |
if (sec[1] != '/') |
|
prio += 10; |
|
if (req->q.arch == NULL) { |
archprio = |
archprio = |
(NULL == (arch = strchr( |
((arch = strchr(sec + 1, '/')) |
r[i].file + isec, '/'))) ? 3 : |
== NULL) ? 3 : |
(NULL == (archend = strchr( |
((archend = strchr(arch + 1, '/')) |
arch + 1, '/'))) ? 0 : |
== NULL) ? 0 : |
strncmp(arch, "amd64/", |
strncmp(arch, "amd64/", |
archend - arch) ? 2 : 1; |
archend - arch) ? 2 : 1; |
if (archprio < archpriouse) { |
if (archprio < archpriouse) { |
Line 808 format(const struct req *req, const char *file) |
|
Line 819 format(const struct req *req, const char *file) |
|
memset(&conf, 0, sizeof(conf)); |
memset(&conf, 0, sizeof(conf)); |
conf.fragment = 1; |
conf.fragment = 1; |
usepath = strcmp(req->q.manpath, req->p[0]); |
usepath = strcmp(req->q.manpath, req->p[0]); |
mandoc_asprintf(&conf.man, "%s?query=%%N&sec=%%S%s%s%s%s", |
mandoc_asprintf(&conf.man, "/%s%s%%N.%%S", |
scriptname, |
usepath ? req->q.manpath : "", usepath ? "/" : ""); |
req->q.arch ? "&arch=" : "", |
|
req->q.arch ? req->q.arch : "", |
|
usepath ? "&manpath=" : "", |
|
usepath ? req->q.manpath : ""); |
|
|
|
mparse_result(mp, &man, NULL); |
mparse_result(mp, &man, NULL); |
if (man == NULL) { |
if (man == NULL) { |
|
|
return EXIT_FAILURE; |
return EXIT_FAILURE; |
} |
} |
|
|
/* Scan our run-time environment. */ |
|
|
|
if (NULL == (scriptname = getenv("SCRIPT_NAME"))) |
|
scriptname = ""; |
|
|
|
if ( ! validate_urifrag(scriptname)) { |
|
fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n", |
|
scriptname); |
|
pg_error_internal(); |
|
return EXIT_FAILURE; |
|
} |
|
|
|
/* |
/* |
* First we change directory into the MAN_DIR so that |
* First we change directory into the MAN_DIR so that |
* subsequent scanning for manpath directories is rooted |
* subsequent scanning for manpath directories is rooted |
|
|
} |
} |
|
|
memset(&req, 0, sizeof(struct req)); |
memset(&req, 0, sizeof(struct req)); |
|
req.q.equal = 1; |
pathgen(&req); |
pathgen(&req); |
|
|
/* Parse the path info and the query string. */ |
/* Parse the path info and the query string. */ |
Line 1090 path_parse(struct req *req, const char *path) |
|
Line 1086 path_parse(struct req *req, const char *path) |
|
{ |
{ |
int dir_done; |
int dir_done; |
|
|
|
req->isquery = 0; |
req->q.equal = 1; |
req->q.equal = 1; |
req->q.manpath = mandoc_strdup(path); |
req->q.manpath = mandoc_strdup(path); |
|
|