]>
git.cameronkatri.com Git - mandoc.git/blob - cgi.c
1 /* $Id: cgi.c,v 1.11 2011/12/07 11:52:36 kristaps Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/param.h>
37 #include "apropos_db.h"
68 static int atou(const char *, unsigned *);
69 static void catman(const char *);
70 static void format(const char *);
71 static void html_print(const char *);
72 static void html_putchar(char);
73 static int kval_decode(char *);
74 static void kval_parse(struct kval
**, size_t *, char *);
75 static void kval_free(struct kval
*, size_t);
76 static void pg_index(const struct manpaths
*,
77 const struct req
*, char *);
78 static void pg_search(const struct manpaths
*,
79 const struct req
*, char *);
80 static void pg_show(const struct manpaths
*,
81 const struct req
*, char *);
82 static void resp_bad(void);
83 static void resp_baddb(void);
84 static void resp_error400(void);
85 static void resp_error404(const char *);
86 static void resp_begin_html(int, const char *);
87 static void resp_begin_http(int, const char *);
88 static void resp_end_html(void);
89 static void resp_index(const struct req
*);
90 static void resp_search(struct res
*, size_t, void *);
91 static void resp_searchform(const struct req
*);
93 static const char *progname
;
94 static const char *cache
;
95 static const char *host
;
97 static const char * const pages
[PAGE__MAX
] = {
98 "index", /* PAGE_INDEX */
99 "search", /* PAGE_SEARCH */
100 "show", /* PAGE_SHOW */
104 * This is just OpenBSD's strtol(3) suggestion.
105 * I use it instead of strtonum(3) for portability's sake.
108 atou(const char *buf
, unsigned *v
)
114 lval
= strtol(buf
, &ep
, 10);
115 if (buf
[0] == '\0' || *ep
!= '\0')
117 if ((errno
== ERANGE
&& (lval
== LONG_MAX
||
118 lval
== LONG_MIN
)) ||
119 (lval
> UINT_MAX
|| lval
< 0))
122 *v
= (unsigned int)lval
;
144 putchar((unsigned char)c
);
150 * Print a word, escaping HTML along the way.
151 * This will pass non-ASCII straight to output: be warned!
154 html_print(const char *p
)
164 kval_free(struct kval
*p
, size_t sz
)
168 for (i
= 0; i
< (int)sz
; i
++) {
176 * Parse out key-value pairs from an HTTP request variable.
177 * This can be either a cookie or a POST/GET string, although man.cgi
178 * uses only GET for simplicity.
181 kval_parse(struct kval
**kv
, size_t *kvsz
, char *p
)
188 while (p
&& '\0' != *p
) {
195 if (NULL
!= (p
= strchr(p
, '='))) {
199 sz
= strcspn(p
, ";&");
207 sz
= strcspn(p
, ";&");
216 if ('\0' == *key
|| '\0' == *val
)
219 /* Just abort handling. */
221 if ( ! kval_decode(key
))
223 if ( ! kval_decode(val
))
226 if (*kvsz
+ 1 >= cur
) {
229 (*kv
, cur
* sizeof(struct kval
));
232 (*kv
)[(int)*kvsz
].key
= mandoc_strdup(key
);
233 (*kv
)[(int)*kvsz
].val
= mandoc_strdup(val
);
239 * HTTP-decode a string. The standard explanation is that this turns
240 * "%4e+foo" into "n foo" in the regular way. This is done in-place
241 * over the allocated string.
251 for ( ; '\0' != *p
; p
++) {
253 if ('\0' == (hex
[0] = *(p
+ 1)))
255 if ('\0' == (hex
[1] = *(p
+ 2)))
257 if (1 != sscanf(hex
, "%x", &c
))
263 memmove(p
+ 1, p
+ 3, strlen(p
+ 3) + 1);
265 *p
= '+' == *p
? ' ' : *p
;
273 resp_begin_http(int code
, const char *msg
)
277 printf("Status: %d %s\n", code
, msg
);
279 puts("Content-Type: text/html; charset=utf-8" "\n"
280 "Cache-Control: no-cache" "\n"
281 "Pragma: no-cache" "\n"
288 resp_begin_html(int code
, const char *msg
)
291 resp_begin_http(code
, msg
);
293 puts("<!DOCTYPE HTML PUBLIC " "\n"
294 " \"-//W3C//DTD HTML 4.01//EN\"" "\n"
295 " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n"
298 " <META HTTP-EQUIV=\"Content-Type\" " "\n"
299 " CONTENT=\"text/html; charset=utf-8\">" "\n"
300 " <TITLE>System Manpage Reference</TITLE>" "\n"
303 "<!-- Begin page content. //-->");
310 puts(" </BODY>\n</HTML>");
314 resp_searchform(const struct req
*req
)
317 const char *expr
, *sec
, *arch
;
319 expr
= sec
= arch
= "";
321 for (i
= 0; i
< (int)req
->fieldsz
; i
++)
322 if (0 == strcmp(req
->fields
[i
].key
, "expr"))
323 expr
= req
->fields
[i
].val
;
324 else if (0 == strcmp(req
->fields
[i
].key
, "sec"))
325 sec
= req
->fields
[i
].val
;
326 else if (0 == strcmp(req
->fields
[i
].key
, "arch"))
327 arch
= req
->fields
[i
].val
;
329 puts("<!-- Begin search form. //-->");
330 printf("<FORM ACTION=\"");
331 html_print(progname
);
332 printf("/search.html\" METHOD=\"get\">\n");
334 " <INPUT TYPE=\"submit\" VALUE=\"Search:\">");
335 printf(" Terms: <INPUT TYPE=\"text\" "
336 "SIZE=\"60\" NAME=\"expr\" VALUE=\"");
339 printf(" Section: <INPUT TYPE=\"text\" "
340 "SIZE=\"4\" NAME=\"sec\" VALUE=\"");
343 printf(" Arch: <INPUT TYPE=\"text\" "
344 "SIZE=\"8\" NAME=\"arch\" VALUE=\"");
347 puts(" </FIELDSET>\n</FORM>\n<!-- End search form. //-->");
351 resp_index(const struct req
*req
)
354 resp_begin_html(200, NULL
);
355 resp_searchform(req
);
363 resp_begin_html(400, "Query Malformed");
364 puts("<H1>Malformed Query</H1>\n"
366 " The query your entered was malformed.\n"
367 " Try again from the\n"
368 " <A HREF=\"/index.html\">main page</A>\n"
374 resp_error404(const char *page
)
377 resp_begin_html(404, "Not Found");
378 puts("<H1>Page Not Found</H1>\n"
380 " The page you're looking for, ");
384 " could not be found.\n"
385 " Try searching from the\n"
386 " <A HREF=\"/index.html\">main page</A>\n"
394 resp_begin_html(500, "Internal Server Error");
395 puts("<P>Generic badness happened.</P>");
403 resp_begin_html(500, "Internal Server Error");
404 puts("<P>Your database is broken.</P>");
409 resp_search(struct res
*r
, size_t sz
, void *arg
)
415 * If we have just one result, then jump there now
418 puts("Status: 303 See Other");
419 printf("Location: http://%s%s/show/%u/%u.html\n",
421 r
[0].volume
, r
[0].rec
);
422 puts("Content-Type: text/html; charset=utf-8\n");
426 resp_begin_http(200, NULL
);
427 puts("<!DOCTYPE HTML PUBLIC " "\n"
428 " \"-//W3C//DTD HTML 4.01//EN\"" "\n"
429 " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n"
432 " <META HTTP-EQUIV=\"Content-Type\" " "\n"
433 " CONTENT=\"text/html; charset=utf-8\">" "\n"
434 " <LINK REL=\"stylesheet\" HREF=\"/catman.css\"" "\n"
435 " TYPE=\"text/css\" media=\"all\">" "\n"
436 " <TITLE>System Manpage Reference</TITLE>" "\n"
439 "<!-- Begin page content. //-->");
441 resp_searchform((const struct req
*)arg
);
444 puts("<P>No results found.</P>");
446 for (i
= 0; i
< (int)sz
; i
++) {
447 printf("<P><A HREF=\"");
448 html_print(progname
);
449 printf("/show/%u/%u.html\">", r
[i
].volume
, r
[i
].rec
);
450 html_print(r
[i
].title
);
452 html_print(r
[i
].cat
);
453 if (r
[i
].arch
&& '\0' != *r
[i
].arch
) {
455 html_print(r
[i
].arch
);
458 html_print(r
[i
].desc
);
467 pg_index(const struct manpaths
*ps
, const struct req
*req
, char *path
)
474 catman(const char *file
)
482 if (NULL
== (f
= fopen(file
, "r"))) {
487 resp_begin_html(200, NULL
);
490 while (NULL
!= (p
= fgetln(f
, &len
))) {
492 for (i
= 0; i
< (int)len
- 1; i
++) {
494 * This means that the catpage is out of state.
495 * Ignore it and keep going (although the
499 if ('\b' == p
[i
] || '\n' == p
[i
])
503 * Print a regular character.
504 * Close out any bold/italic scopes.
505 * If we're in back-space mode, make sure we'll
506 * have something to enter when we backspace.
509 if ('\b' != p
[i
+ 1]) {
517 } else if (i
+ 2 >= (int)len
)
535 * Handle funny behaviour troff-isms.
536 * These grok'd from the original man2html.c.
539 if (('+' == p
[i
] && 'o' == p
[i
+ 2]) ||
540 ('o' == p
[i
] && '+' == p
[i
+ 2]) ||
541 ('|' == p
[i
] && '=' == p
[i
+ 2]) ||
542 ('=' == p
[i
] && '|' == p
[i
+ 2]) ||
543 ('*' == p
[i
] && '=' == p
[i
+ 2]) ||
544 ('=' == p
[i
] && '*' == p
[i
+ 2]) ||
545 ('*' == p
[i
] && '|' == p
[i
+ 2]) ||
546 ('|' == p
[i
] && '*' == p
[i
+ 2])) {
555 } else if (('|' == p
[i
] && '-' == p
[i
+ 2]) ||
556 ('-' == p
[i
] && '|' == p
[i
+ 1]) ||
557 ('+' == p
[i
] && '-' == p
[i
+ 1]) ||
558 ('-' == p
[i
] && '+' == p
[i
+ 1]) ||
559 ('+' == p
[i
] && '|' == p
[i
+ 1]) ||
560 ('|' == p
[i
] && '+' == p
[i
+ 1])) {
584 * Clean up the last character.
585 * We can get to a newline; don't print that.
593 if (i
== (int)len
- 1 && '\n' != p
[i
])
607 format(const char *file
)
615 char opts
[MAXPATHLEN
+ 128];
617 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
622 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
623 rc
= mparse_readfd(mp
, fd
, file
);
626 if (rc
>= MANDOCLEVEL_FATAL
) {
631 snprintf(opts
, sizeof(opts
), "style=/man.css,"
632 "man=%s/search.html?sec=%%S&expr=%%N,"
633 /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
636 mparse_result(mp
, &mdoc
, &man
);
637 vp
= html_alloc(opts
);
640 resp_begin_http(200, NULL
);
642 } else if (NULL
!= man
) {
643 resp_begin_http(200, NULL
);
653 pg_show(const struct manpaths
*ps
, const struct req
*req
, char *path
)
656 char file
[MAXPATHLEN
];
659 unsigned int vol
, rec
;
666 } else if (NULL
== (sub
= strrchr(path
, '/'))) {
672 if ( ! (atou(path
, &vol
) && atou(sub
, &rec
))) {
675 } else if (vol
>= (unsigned int)ps
->sz
) {
680 strlcpy(file
, ps
->paths
[vol
], MAXPATHLEN
);
681 strlcat(file
, "/mandoc.index", MAXPATHLEN
);
683 /* Open the index recno(3) database. */
685 idx
= dbopen(file
, O_RDONLY
, 0, DB_RECNO
, NULL
);
694 if (0 != (rc
= (*idx
->get
)(idx
, &key
, &val
, 0))) {
695 rc
< 0 ? resp_baddb() : resp_error400();
699 cp
= (char *)val
.data
;
701 if (NULL
== (fn
= memchr(cp
, '\0', val
.size
)))
703 else if (++fn
- cp
>= (int)val
.size
)
705 else if (NULL
== memchr(fn
, '\0', val
.size
- (fn
- cp
)))
708 strlcpy(file
, ps
->paths
[vol
], MAXPATHLEN
);
709 strlcat(file
, "/", MAXPATHLEN
);
710 strlcat(file
, fn
, MAXPATHLEN
);
711 if (0 == strcmp(cp
, "cat"))
721 pg_search(const struct manpaths
*ps
, const struct req
*req
, char *path
)
725 const char *ep
, *start
;
735 memset(&opt
, 0, sizeof(struct opts
));
737 for (sz
= i
= 0; i
< (int)req
->fieldsz
; i
++)
738 if (0 == strcmp(req
->fields
[i
].key
, "expr"))
739 ep
= req
->fields
[i
].val
;
740 else if (0 == strcmp(req
->fields
[i
].key
, "sec"))
741 opt
.cat
= req
->fields
[i
].val
;
742 else if (0 == strcmp(req
->fields
[i
].key
, "arch"))
743 opt
.arch
= req
->fields
[i
].val
;
746 * Poor man's tokenisation.
747 * Just break apart by spaces.
748 * Yes, this is half-ass. But it works for now.
751 while (ep
&& isspace((unsigned char)*ep
))
754 while (ep
&& '\0' != *ep
) {
755 cp
= mandoc_realloc(cp
, (sz
+ 1) * sizeof(char *));
757 while ('\0' != *ep
&& ! isspace((unsigned char)*ep
))
759 cp
[sz
] = mandoc_malloc((ep
- start
) + 1);
760 memcpy(cp
[sz
], start
, ep
- start
);
761 cp
[sz
++][ep
- start
] = '\0';
762 while (isspace((unsigned char)*ep
))
769 * Pump down into apropos backend.
770 * The resp_search() function is called with the results.
773 if (NULL
!= (expr
= exprcomp(sz
, cp
, &tt
)))
775 (ps
->sz
, ps
->paths
, &opt
,
776 expr
, tt
, (void *)req
, resp_search
);
778 /* ...unless errors occured. */
783 resp_search(NULL
, 0, (void *)req
);
785 for (i
= 0; i
< sz
; i
++)
797 char *p
, *path
, *subpath
;
798 struct manpaths paths
;
800 /* HTTP init: read and parse the query string. */
802 progname
= getenv("SCRIPT_NAME");
803 if (NULL
== progname
)
806 cache
= getenv("CACHE_DIR");
808 cache
= "/cache/man.cgi";
810 if (-1 == chdir(cache
)) {
812 return(EXIT_FAILURE
);
815 host
= getenv("HTTP_HOST");
819 memset(&req
, 0, sizeof(struct req
));
821 if (NULL
!= (p
= getenv("QUERY_STRING")))
822 kval_parse(&req
.fields
, &req
.fieldsz
, p
);
824 /* Resolve leading subpath component. */
826 subpath
= path
= NULL
;
827 req
.page
= PAGE__MAX
;
829 if (NULL
== (path
= getenv("PATH_INFO")) || '\0' == *path
)
830 req
.page
= PAGE_INDEX
;
832 if (NULL
!= path
&& '/' == *path
&& '\0' == *++path
)
833 req
.page
= PAGE_INDEX
;
835 /* Strip file suffix. */
837 if (NULL
!= path
&& NULL
!= (p
= strrchr(path
, '.')))
838 if (NULL
!= p
&& NULL
== strchr(p
, '/'))
841 /* Resolve subpath component. */
843 if (NULL
!= path
&& NULL
!= (subpath
= strchr(path
, '/')))
846 /* Map path into one we recognise. */
848 if (NULL
!= path
&& '\0' != *path
)
849 for (i
= 0; i
< (int)PAGE__MAX
; i
++)
850 if (0 == strcmp(pages
[i
], path
)) {
851 req
.page
= (enum page
)i
;
855 /* Initialise MANPATH. */
857 memset(&paths
, 0, sizeof(struct manpaths
));
858 manpath_manconf("etc/catman.conf", &paths
);
864 pg_index(&paths
, &req
, subpath
);
867 pg_search(&paths
, &req
, subpath
);
870 pg_show(&paths
, &req
, subpath
);
877 manpath_free(&paths
);
878 kval_free(req
.fields
, req
.fieldsz
);
880 return(EXIT_SUCCESS
);