]>
git.cameronkatri.com Git - mandoc.git/blob - cgi.c
1 /* $Id: cgi.c,v 1.56 2014/07/09 11:34:46 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 Ingo Schwarze <schwarze@usta.de>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include <sys/types.h>
38 #include "mandoc_aux.h"
41 #include "mansearch.h"
51 * A query as passed to the search function.
54 const char *manroot
; /* manual root directory */
55 const char *arch
; /* architecture */
56 const char *sec
; /* manual section */
57 const char *expr
; /* unparsed expression string */
58 int legacy
; /* whether legacy mode */
63 char **p
; /* array of available manroots */
68 static void catman(const struct req
*, const char *);
69 static int cmp(const void *, const void *);
70 static void format(const struct req
*, const char *);
71 static void html_print(const char *);
72 static void html_printquery(const struct req
*);
73 static void html_putchar(char);
74 static int http_decode(char *);
75 static void http_parse(struct req
*, char *);
76 static void http_print(const char *);
77 static void http_putchar(char);
78 static void http_printquery(const struct req
*);
79 static void pathgen(struct req
*);
80 static void pg_index(const struct req
*, char *);
81 static void pg_search(const struct req
*, char *);
82 static void pg_show(const struct req
*, char *);
83 static void resp_bad(void);
84 static void resp_baddb(void);
85 static void resp_error400(void);
86 static void resp_error404(const char *);
87 static void resp_begin_html(int, const char *);
88 static void resp_begin_http(int, const char *);
89 static void resp_end_html(void);
90 static void resp_index(const struct req
*);
91 static void resp_search(const struct req
*,
92 struct manpage
*, size_t);
93 static void resp_searchform(const struct req
*);
95 static const char *progname
; /* cgi script name */
96 static const char *cache
; /* cache directory */
97 static const char *css
; /* css directory */
98 static const char *host
; /* hostname */
100 static const char * const pages
[PAGE__MAX
] = {
101 "index", /* PAGE_INDEX */
102 "search", /* PAGE_SEARCH */
103 "show", /* PAGE_SHOW */
107 * Print a character, escaping HTML along the way.
108 * This will pass non-ASCII straight to output: be warned!
128 putchar((unsigned char)c
);
133 http_printquery(const struct req
*req
)
136 if (NULL
!= req
->q
.manroot
) {
138 http_print(req
->q
.manroot
);
140 if (NULL
!= req
->q
.sec
) {
142 http_print(req
->q
.sec
);
144 if (NULL
!= req
->q
.arch
) {
146 http_print(req
->q
.arch
);
148 if (NULL
!= req
->q
.expr
) {
150 http_print(req
->q
.expr
? req
->q
.expr
: "");
156 html_printquery(const struct req
*req
)
159 if (NULL
!= req
->q
.manroot
) {
160 printf("&manpath=");
161 html_print(req
->q
.manroot
);
163 if (NULL
!= req
->q
.sec
) {
165 html_print(req
->q
.sec
);
167 if (NULL
!= req
->q
.arch
) {
168 printf("&arch=");
169 html_print(req
->q
.arch
);
171 if (NULL
!= req
->q
.expr
) {
172 printf("&expr=");
173 html_print(req
->q
.expr
? req
->q
.expr
: "");
178 http_print(const char *p
)
188 * Call through to html_putchar().
189 * Accepts NULL strings.
192 html_print(const char *p
)
202 * Parse out key-value pairs from an HTTP request variable.
203 * This can be either a cookie or a POST/GET string, although man.cgi
204 * uses only GET for simplicity.
207 http_parse(struct req
*req
, char *p
)
212 memset(&req
->q
, 0, sizeof(struct query
));
213 req
->q
.manroot
= req
->p
[0];
220 p
+= (int)strcspn(p
, ";&");
223 if (NULL
!= (val
= strchr(key
, '=')))
226 if ('\0' == *key
|| NULL
== val
|| '\0' == *val
)
229 /* Just abort handling. */
231 if ( ! http_decode(key
))
233 if (NULL
!= val
&& ! http_decode(val
))
236 if (0 == strcmp(key
, "expr"))
238 else if (0 == strcmp(key
, "query"))
240 else if (0 == strcmp(key
, "sec"))
242 else if (0 == strcmp(key
, "sektion"))
244 else if (0 == strcmp(key
, "arch"))
246 else if (0 == strcmp(key
, "manpath"))
247 req
->q
.manroot
= val
;
248 else if (0 == strcmp(key
, "apropos"))
249 legacy
= 0 == strcmp(val
, "0");
252 /* Test for old man.cgi compatibility mode. */
254 req
->q
.legacy
= legacy
> 0;
257 * Section "0" means no section when in legacy mode.
258 * For some man.cgi scripts, "default" arch is none.
261 if (req
->q
.legacy
&& NULL
!= req
->q
.sec
)
262 if (0 == strcmp(req
->q
.sec
, "0"))
264 if (req
->q
.legacy
&& NULL
!= req
->q
.arch
)
265 if (0 == strcmp(req
->q
.arch
, "default"))
273 if (isalnum((unsigned char)c
)) {
274 putchar((unsigned char)c
);
276 } else if (' ' == c
) {
284 * HTTP-decode a string. The standard explanation is that this turns
285 * "%4e+foo" into "n foo" in the regular way. This is done in-place
286 * over the allocated string.
296 for ( ; '\0' != *p
; p
++) {
298 if ('\0' == (hex
[0] = *(p
+ 1)))
300 if ('\0' == (hex
[1] = *(p
+ 2)))
302 if (1 != sscanf(hex
, "%x", &c
))
308 memmove(p
+ 1, p
+ 3, strlen(p
+ 3) + 1);
310 *p
= '+' == *p
? ' ' : *p
;
318 resp_begin_http(int code
, const char *msg
)
322 printf("Status: %d %s\n", code
, msg
);
324 puts("Content-Type: text/html; charset=utf-8\n"
325 "Cache-Control: no-cache\n"
333 resp_begin_html(int code
, const char *msg
)
336 resp_begin_http(code
, msg
);
338 printf("<!DOCTYPE HTML PUBLIC "
339 " \"-//W3C//DTD HTML 4.01//EN\""
340 " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
343 "<META HTTP-EQUIV=\"Content-Type\""
344 " CONTENT=\"text/html; charset=utf-8\">\n"
345 "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
346 " TYPE=\"text/css\" media=\"all\">\n"
347 "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
348 " TYPE=\"text/css\" media=\"all\">\n"
349 "<TITLE>System Manpage Reference</TITLE>\n"
352 "<!-- Begin page content. //-->\n", css
, css
);
364 resp_searchform(const struct req
*req
)
368 puts("<!-- Begin search form. //-->");
369 printf("<DIV ID=\"mancgi\">\n"
370 "<FORM ACTION=\"%s/search\" METHOD=\"get\">\n"
372 "<LEGEND>Search Parameters</LEGEND>\n"
373 "<INPUT TYPE=\"submit\" "
374 " VALUE=\"Search\"> for manuals satisfying \n"
375 "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
377 html_print(req
->q
.expr
? req
->q
.expr
: "");
378 printf("\">, section "
379 "<INPUT TYPE=\"text\""
380 " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
381 html_print(req
->q
.sec
? req
->q
.sec
: "");
383 "<INPUT TYPE=\"text\""
384 " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
385 html_print(req
->q
.arch
? req
->q
.arch
: "");
388 puts(", <SELECT NAME=\"manpath\">");
389 for (i
= 0; i
< (int)req
->psz
; i
++) {
391 if (NULL
== req
->q
.manroot
? 0 == i
:
392 0 == strcmp(req
->q
.manroot
, req
->p
[i
]))
393 printf("SELECTED=\"selected\" ");
395 html_print(req
->p
[i
]);
397 html_print(req
->p
[i
]);
403 "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
407 puts("<!-- End search form. //-->");
411 resp_index(const struct req
*req
)
414 resp_begin_html(200, NULL
);
415 resp_searchform(req
);
423 resp_begin_html(400, "Query Malformed");
424 printf("<H1>Malformed Query</H1>\n"
426 "The query your entered was malformed.\n"
427 "Try again from the\n"
428 "<A HREF=\"%s/index.html\">main page</A>.\n"
434 resp_error404(const char *page
)
437 resp_begin_html(404, "Not Found");
438 puts("<H1>Page Not Found</H1>\n"
440 "The page you're looking for, ");
444 "could not be found.\n"
445 "Try searching from the\n"
446 "<A HREF=\"%s/index.html\">main page</A>.\n"
454 resp_begin_html(500, "Internal Server Error");
455 puts("<P>Generic badness happened.</P>");
463 resp_begin_html(500, "Internal Server Error");
464 puts("<P>Your database is broken.</P>");
469 resp_search(const struct req
*req
, struct manpage
*r
, size_t sz
)
475 * If we have just one result, then jump there now
478 puts("Status: 303 See Other");
479 printf("Location: http://%s%s/show/%s/%s?",
480 host
, progname
, req
->q
.manroot
, r
[0].file
);
481 http_printquery(req
);
483 "Content-Type: text/html; charset=utf-8\n");
487 resp_begin_html(200, NULL
);
488 resp_searchform(req
);
490 puts("<DIV CLASS=\"results\">");
494 "No results found.\n"
501 qsort(r
, sz
, sizeof(struct manpage
), cmp
);
505 for (i
= 0; i
< sz
; i
++) {
507 "<TD CLASS=\"title\">\n"
508 "<A HREF=\"%s/show/%s/%s?",
509 progname
, req
->q
.manroot
, r
[i
].file
);
510 html_printquery(req
);
512 html_print(r
[i
].names
);
515 "<TD CLASS=\"desc\">");
516 html_print(r
[i
].output
);
528 pg_index(const struct req
*req
, char *path
)
535 catman(const struct req
*req
, const char *file
)
543 if (NULL
== (f
= fopen(file
, "r"))) {
548 resp_begin_html(200, NULL
);
549 resp_searchform(req
);
550 puts("<DIV CLASS=\"catman\">\n"
553 while (NULL
!= (p
= fgetln(f
, &len
))) {
555 for (i
= 0; i
< (int)len
- 1; i
++) {
557 * This means that the catpage is out of state.
558 * Ignore it and keep going (although the
562 if ('\b' == p
[i
] || '\n' == p
[i
])
566 * Print a regular character.
567 * Close out any bold/italic scopes.
568 * If we're in back-space mode, make sure we'll
569 * have something to enter when we backspace.
572 if ('\b' != p
[i
+ 1]) {
580 } else if (i
+ 2 >= (int)len
)
598 * Handle funny behaviour troff-isms.
599 * These grok'd from the original man2html.c.
602 if (('+' == p
[i
] && 'o' == p
[i
+ 2]) ||
603 ('o' == p
[i
] && '+' == p
[i
+ 2]) ||
604 ('|' == p
[i
] && '=' == p
[i
+ 2]) ||
605 ('=' == p
[i
] && '|' == p
[i
+ 2]) ||
606 ('*' == p
[i
] && '=' == p
[i
+ 2]) ||
607 ('=' == p
[i
] && '*' == p
[i
+ 2]) ||
608 ('*' == p
[i
] && '|' == p
[i
+ 2]) ||
609 ('|' == p
[i
] && '*' == p
[i
+ 2])) {
618 } else if (('|' == p
[i
] && '-' == p
[i
+ 2]) ||
619 ('-' == p
[i
] && '|' == p
[i
+ 1]) ||
620 ('+' == p
[i
] && '-' == p
[i
+ 1]) ||
621 ('-' == p
[i
] && '+' == p
[i
+ 1]) ||
622 ('+' == p
[i
] && '|' == p
[i
+ 1]) ||
623 ('|' == p
[i
] && '+' == p
[i
+ 1])) {
647 * Clean up the last character.
648 * We can get to a newline; don't print that.
656 if (i
== (int)len
- 1 && '\n' != p
[i
])
671 format(const struct req
*req
, const char *file
)
679 char opts
[PATH_MAX
+ 128];
681 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
686 mp
= mparse_alloc(MPARSE_SO
, MANDOCLEVEL_FATAL
, NULL
,
688 rc
= mparse_readfd(mp
, fd
, file
);
691 if (rc
>= MANDOCLEVEL_FATAL
) {
696 snprintf(opts
, sizeof(opts
),
697 "fragment,man=%s/search?sec=%%S&expr=Nm~^%%N$",
700 mparse_result(mp
, &mdoc
, &man
, NULL
);
701 if (NULL
== man
&& NULL
== mdoc
) {
707 resp_begin_html(200, NULL
);
708 resp_searchform(req
);
710 vp
= html_alloc(opts
);
725 pg_show(const struct req
*req
, char *path
)
729 if (NULL
== path
|| NULL
== (sub
= strchr(path
, '/'))) {
736 * Begin by chdir()ing into the manroot.
737 * This way we can pick up the database files, which are
738 * relative to the manpath root.
741 if (-1 == chdir(path
)) {
754 pg_search(const struct req
*req
, char *path
)
756 struct mansearch search
;
757 struct manpaths paths
;
760 const char *ep
, *start
;
765 * Begin by chdir()ing into the root of the manpath.
766 * This way we can pick up the database files, which are
767 * relative to the manpath root.
770 if (-1 == (chdir(req
->q
.manroot
))) {
771 perror(req
->q
.manroot
);
772 resp_search(req
, NULL
, 0);
776 search
.arch
= req
->q
.arch
;
777 search
.sec
= req
->q
.sec
;
778 search
.deftype
= TYPE_Nm
| TYPE_Nd
;
782 paths
.paths
= mandoc_malloc(sizeof(char *));
783 paths
.paths
[0] = mandoc_strdup(".");
786 * Poor man's tokenisation: just break apart by spaces.
787 * Yes, this is half-ass. But it works for now.
791 while (ep
&& isspace((unsigned char)*ep
))
796 while (ep
&& '\0' != *ep
) {
797 cp
= mandoc_reallocarray(cp
, sz
+ 1, sizeof(char *));
799 while ('\0' != *ep
&& ! isspace((unsigned char)*ep
))
801 cp
[sz
] = mandoc_malloc((ep
- start
) + 1);
802 memcpy(cp
[sz
], start
, ep
- start
);
803 cp
[sz
++][ep
- start
] = '\0';
804 while (isspace((unsigned char)*ep
))
808 if (mansearch(&search
, &paths
, sz
, cp
, "Nd", &res
, &ressz
))
809 resp_search(req
, res
, ressz
);
813 for (i
= 0; i
< sz
; i
++)
817 for (i
= 0; i
< (int)ressz
; i
++) {
824 free(paths
.paths
[0]);
833 char *p
, *path
, *subpath
;
835 /* Scan our run-time environment. */
837 if (NULL
== (cache
= getenv("CACHE_DIR")))
838 cache
= "/cache/man.cgi";
840 if (NULL
== (progname
= getenv("SCRIPT_NAME")))
843 if (NULL
== (css
= getenv("CSS_DIR")))
846 if (NULL
== (host
= getenv("HTTP_HOST")))
850 * First we change directory into the cache directory so that
851 * subsequent scanning for manpath directories is rooted
852 * relative to the same position.
855 if (-1 == chdir(cache
)) {
858 return(EXIT_FAILURE
);
861 memset(&req
, 0, sizeof(struct req
));
864 /* Next parse out the query string. */
866 if (NULL
!= (p
= getenv("QUERY_STRING")))
870 * Now juggle paths to extract information.
871 * We want to extract our filetype (the file suffix), the
872 * initial path component, then the trailing component(s).
873 * Start with leading subpath component.
876 subpath
= path
= NULL
;
877 req
.page
= PAGE__MAX
;
879 if (NULL
== (path
= getenv("PATH_INFO")) || '\0' == *path
)
880 req
.page
= PAGE_INDEX
;
882 if (NULL
!= path
&& '/' == *path
&& '\0' == *++path
)
883 req
.page
= PAGE_INDEX
;
885 /* Resolve subpath component. */
887 if (NULL
!= path
&& NULL
!= (subpath
= strchr(path
, '/')))
890 /* Map path into one we recognise. */
892 if (NULL
!= path
&& '\0' != *path
)
893 for (i
= 0; i
< (int)PAGE__MAX
; i
++)
894 if (0 == strcmp(pages
[i
], path
)) {
895 req
.page
= (enum page
)i
;
903 pg_index(&req
, subpath
);
906 pg_search(&req
, subpath
);
909 pg_show(&req
, subpath
);
916 for (i
= 0; i
< (int)req
.psz
; i
++)
919 return(EXIT_SUCCESS
);
923 cmp(const void *p1
, const void *p2
)
926 return(strcasecmp(((const struct manpage
*)p1
)->names
,
927 ((const struct manpage
*)p2
)->names
));
931 * Scan for indexable paths.
934 pathgen(struct req
*req
)
940 if (NULL
== (fp
= fopen("manpath.conf", "r")))
943 while (NULL
!= (dp
= fgetln(fp
, &dpsz
))) {
944 if ('\n' == dp
[dpsz
- 1])
946 req
->p
= mandoc_realloc(req
->p
,
947 (req
->psz
+ 1) * sizeof(char *));
948 req
->p
[req
->psz
++] = mandoc_strndup(dp
, dpsz
);