Annotation of mandoc/cgi.c, Revision 1.10
1.10 ! kristaps 1: /* $Id: cgi.c,v 1.9 2011/12/04 22:52:50 kristaps Exp $ */
1.6 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
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.
8: *
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.
16: */
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
21: #include <sys/param.h>
22: #include <sys/wait.h>
23:
1.1 kristaps 24: #include <assert.h>
1.6 kristaps 25: #include <ctype.h>
26: #include <errno.h>
1.1 kristaps 27: #include <fcntl.h>
1.6 kristaps 28: #include <limits.h>
1.1 kristaps 29: #include <regex.h>
30: #include <stdio.h>
31: #include <stdarg.h>
1.5 kristaps 32: #include <stdint.h>
1.1 kristaps 33: #include <stdlib.h>
34: #include <string.h>
1.6 kristaps 35: #include <unistd.h>
1.1 kristaps 36:
1.6 kristaps 37: #include "apropos_db.h"
1.4 schwarze 38: #include "mandoc.h"
1.8 kristaps 39: #include "mdoc.h"
40: #include "man.h"
41: #include "main.h"
1.6 kristaps 42: #include "manpath.h"
43:
44: #ifdef __linux__
45: # include <db_185.h>
46: #else
47: # include <db.h>
48: #endif
1.1 kristaps 49:
50: enum page {
51: PAGE_INDEX,
52: PAGE_SEARCH,
1.6 kristaps 53: PAGE_SHOW,
1.1 kristaps 54: PAGE__MAX
55: };
56:
57: struct kval {
58: char *key;
59: char *val;
60: };
61:
62: struct req {
1.6 kristaps 63: struct kval *fields;
1.1 kristaps 64: size_t fieldsz;
65: enum page page;
66: };
67:
1.6 kristaps 68: static int atou(const char *, unsigned *);
1.9 kristaps 69: static void catman(const char *);
1.8 kristaps 70: static void format(const char *);
1.6 kristaps 71: static void html_print(const char *);
1.10 ! kristaps 72: static void html_putchar(char);
1.1 kristaps 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);
1.6 kristaps 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 *);
1.7 kristaps 82: static void resp_bad(void);
1.6 kristaps 83: static void resp_baddb(void);
1.10 ! kristaps 84: static void resp_error400(void);
! 85: static void resp_error404(const char *);
1.6 kristaps 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 *);
92:
93: static const char *progname;
1.7 kristaps 94: static const char *cache;
1.6 kristaps 95: static const char *host;
1.1 kristaps 96:
97: static const char * const pages[PAGE__MAX] = {
98: "index", /* PAGE_INDEX */
99: "search", /* PAGE_SEARCH */
1.6 kristaps 100: "show", /* PAGE_SHOW */
1.1 kristaps 101: };
102:
1.6 kristaps 103: /*
104: * This is just OpenBSD's strtol(3) suggestion.
105: * I use it instead of strtonum(3) for portability's sake.
106: */
107: static int
108: atou(const char *buf, unsigned *v)
109: {
110: char *ep;
111: long lval;
112:
113: errno = 0;
114: lval = strtol(buf, &ep, 10);
115: if (buf[0] == '\0' || *ep != '\0')
116: return(0);
117: if ((errno == ERANGE && (lval == LONG_MAX ||
118: lval == LONG_MIN)) ||
119: (lval > UINT_MAX || lval < 0))
120: return(0);
121:
122: *v = (unsigned int)lval;
123: return(1);
124: }
1.1 kristaps 125:
1.10 ! kristaps 126: static void
! 127: html_putchar(char c)
! 128: {
! 129:
! 130: switch (c) {
! 131: case ('"'):
! 132: printf(""e;");
! 133: break;
! 134: case ('&'):
! 135: printf("&");
! 136: break;
! 137: case ('>'):
! 138: printf(">");
! 139: break;
! 140: case ('<'):
! 141: printf("<");
! 142: break;
! 143: default:
! 144: putchar((unsigned char)c);
! 145: break;
! 146: }
! 147: }
! 148:
1.6 kristaps 149: /*
150: * Print a word, escaping HTML along the way.
151: * This will pass non-ASCII straight to output: be warned!
152: */
1.1 kristaps 153: static void
1.6 kristaps 154: html_print(const char *p)
1.1 kristaps 155: {
1.6 kristaps 156:
157: if (NULL == p)
158: return;
1.1 kristaps 159: while ('\0' != *p)
1.10 ! kristaps 160: html_putchar(*p++);
1.1 kristaps 161: }
162:
163: static void
164: kval_free(struct kval *p, size_t sz)
165: {
166: int i;
167:
168: for (i = 0; i < (int)sz; i++) {
169: free(p[i].key);
170: free(p[i].val);
171: }
172: free(p);
173: }
174:
175: /*
176: * Parse out key-value pairs from an HTTP request variable.
1.6 kristaps 177: * This can be either a cookie or a POST/GET string, although man.cgi
178: * uses only GET for simplicity.
1.1 kristaps 179: */
180: static void
181: kval_parse(struct kval **kv, size_t *kvsz, char *p)
182: {
183: char *key, *val;
184: size_t sz, cur;
185:
186: cur = 0;
187:
188: while (p && '\0' != *p) {
189: while (' ' == *p)
190: p++;
191:
192: key = p;
193: val = NULL;
194:
195: if (NULL != (p = strchr(p, '='))) {
196: *p++ = '\0';
197: val = p;
198:
199: sz = strcspn(p, ";&");
200: /* LINTED */
201: p += sz;
202:
203: if ('\0' != *p)
204: *p++ = '\0';
205: } else {
206: p = key;
207: sz = strcspn(p, ";&");
208: /* LINTED */
209: p += sz;
210:
211: if ('\0' != *p)
212: p++;
213: continue;
214: }
215:
216: if ('\0' == *key || '\0' == *val)
217: continue;
218:
219: /* Just abort handling. */
220:
221: if ( ! kval_decode(key))
222: return;
223: if ( ! kval_decode(val))
224: return;
225:
226: if (*kvsz + 1 >= cur) {
227: cur++;
228: *kv = mandoc_realloc
229: (*kv, cur * sizeof(struct kval));
230: }
231:
232: (*kv)[(int)*kvsz].key = mandoc_strdup(key);
233: (*kv)[(int)*kvsz].val = mandoc_strdup(val);
234: (*kvsz)++;
235: }
236: }
237:
238: /*
1.6 kristaps 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.
1.1 kristaps 242: */
243: static int
244: kval_decode(char *p)
245: {
246: char hex[3];
247: int c;
248:
249: hex[2] = '\0';
250:
251: for ( ; '\0' != *p; p++) {
252: if ('%' == *p) {
253: if ('\0' == (hex[0] = *(p + 1)))
254: return(0);
255: if ('\0' == (hex[1] = *(p + 2)))
256: return(0);
257: if (1 != sscanf(hex, "%x", &c))
258: return(0);
259: if ('\0' == c)
260: return(0);
261:
262: *p = (char)c;
263: memmove(p + 1, p + 3, strlen(p + 3) + 1);
264: } else
265: *p = '+' == *p ? ' ' : *p;
266: }
267:
268: *p = '\0';
269: return(1);
270: }
271:
1.6 kristaps 272: static void
273: resp_begin_http(int code, const char *msg)
274: {
275:
276: if (200 != code)
277: printf("Status: %d %s\n", code, msg);
278:
279: puts("Content-Type: text/html; charset=utf-8" "\n"
280: "Cache-Control: no-cache" "\n"
281: "Pragma: no-cache" "\n"
282: "");
283:
284: fflush(stdout);
285: }
286:
287: static void
288: resp_begin_html(int code, const char *msg)
289: {
290:
291: resp_begin_http(code, msg);
292:
293: puts("<!DOCTYPE HTML PUBLIC " "\n"
294: " \"-//W3C//DTD HTML 4.01//EN\"" "\n"
295: " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n"
296: "<HTML>" "\n"
297: " <HEAD>" "\n"
1.10 ! kristaps 298: " <META HTTP-EQUIV=\"Content-Type\" " "\n"
! 299: " CONTENT=\"text/html; charset=utf-8\">" "\n"
1.6 kristaps 300: " <TITLE>System Manpage Reference</TITLE>" "\n"
301: " </HEAD>" "\n"
302: " <BODY>" "\n"
303: "<!-- Begin page content. //-->");
304: }
305:
306: static void
307: resp_end_html(void)
308: {
309:
310: puts(" </BODY>\n</HTML>");
311: }
312:
313: static void
314: resp_searchform(const struct req *req)
315: {
316: int i;
317: const char *expr, *sec, *arch;
318:
319: expr = sec = arch = "";
320:
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;
328:
329: puts("<!-- Begin search form. //-->");
330: printf("<FORM ACTION=\"");
331: html_print(progname);
1.9 kristaps 332: printf("/search.html\" METHOD=\"get\">\n");
1.10 ! kristaps 333: puts(" <FIELDSET>\n"
1.6 kristaps 334: " <INPUT TYPE=\"submit\" VALUE=\"Search:\">");
335: printf(" Terms: <INPUT TYPE=\"text\" "
336: "SIZE=\"60\" NAME=\"expr\" VALUE=\"");
337: html_print(expr);
338: puts("\">");
339: printf(" Section: <INPUT TYPE=\"text\" "
340: "SIZE=\"4\" NAME=\"sec\" VALUE=\"");
341: html_print(sec);
342: puts("\">");
343: printf(" Arch: <INPUT TYPE=\"text\" "
344: "SIZE=\"8\" NAME=\"arch\" VALUE=\"");
345: html_print(arch);
346: puts("\">");
347: puts(" </FIELDSET>\n</FORM>\n<!-- End search form. //-->");
348: }
349:
350: static void
351: resp_index(const struct req *req)
352: {
353:
354: resp_begin_html(200, NULL);
355: resp_searchform(req);
356: resp_end_html();
357: }
358:
359: static void
1.10 ! kristaps 360: resp_error400(void)
1.9 kristaps 361: {
362:
1.10 ! kristaps 363: resp_begin_html(400, "Query Malformed");
! 364: puts("<H1>Malformed Query</H1>\n"
! 365: "<P>\n"
! 366: " The query your entered was malformed.\n"
! 367: " Try again from the\n"
! 368: " <A HREF=\"/index.html\">main page</A>\n"
! 369: "</P>");
1.9 kristaps 370: resp_end_html();
371: }
372:
373: static void
1.10 ! kristaps 374: resp_error404(const char *page)
1.6 kristaps 375: {
376:
377: resp_begin_html(404, "Not Found");
1.10 ! kristaps 378: puts("<H1>Page Not Found</H1>\n"
! 379: "<P>\n"
! 380: " The page you're looking for, ");
! 381: printf(" <B>");
! 382: html_print(page);
! 383: puts("</B>,\n"
! 384: " could not be found.\n"
! 385: " Try searching from the\n"
! 386: " <A HREF=\"/index.html\">main page</A>\n"
! 387: "</P>");
1.6 kristaps 388: resp_end_html();
389: }
1.1 kristaps 390:
391: static void
1.7 kristaps 392: resp_bad(void)
393: {
394: resp_begin_html(500, "Internal Server Error");
395: puts("<P>Generic badness happened.</P>");
396: resp_end_html();
397: }
398:
399: static void
1.6 kristaps 400: resp_baddb(void)
1.1 kristaps 401: {
402:
1.6 kristaps 403: resp_begin_html(500, "Internal Server Error");
404: puts("<P>Your database is broken.</P>");
405: resp_end_html();
1.1 kristaps 406: }
407:
408: static void
1.6 kristaps 409: resp_search(struct res *r, size_t sz, void *arg)
1.1 kristaps 410: {
411: int i;
412:
1.6 kristaps 413: if (1 == sz) {
414: /*
415: * If we have just one result, then jump there now
416: * without any delay.
417: */
418: puts("Status: 303 See Other");
419: printf("Location: http://%s%s/show/%u/%u.html\n",
420: host, progname,
421: r[0].volume, r[0].rec);
422: puts("Content-Type: text/html; charset=utf-8\n");
423: return;
424: }
425:
426: resp_begin_html(200, NULL);
427: resp_searchform((const struct req *)arg);
428:
429: if (0 == sz)
430: puts("<P>No results found.</P>");
1.1 kristaps 431:
432: for (i = 0; i < (int)sz; i++) {
1.6 kristaps 433: printf("<P><A HREF=\"");
434: html_print(progname);
435: printf("/show/%u/%u.html\">", r[i].volume, r[i].rec);
436: html_print(r[i].title);
1.1 kristaps 437: putchar('(');
1.6 kristaps 438: html_print(r[i].cat);
439: if (r[i].arch && '\0' != *r[i].arch) {
440: putchar('/');
441: html_print(r[i].arch);
442: }
443: printf(")</A> ");
444: html_print(r[i].desc);
445: puts("</P>");
1.1 kristaps 446: }
1.6 kristaps 447:
448: resp_end_html();
449: }
450:
451: /* ARGSUSED */
452: static void
453: pg_index(const struct manpaths *ps, const struct req *req, char *path)
454: {
455:
456: resp_index(req);
1.1 kristaps 457: }
458:
459: static void
1.9 kristaps 460: catman(const char *file)
461: {
1.10 ! kristaps 462: FILE *f;
! 463: size_t len;
! 464: int i;
! 465: char *p;
! 466: int italic, bold;
1.9 kristaps 467:
1.10 ! kristaps 468: if (NULL == (f = fopen(file, "r"))) {
1.9 kristaps 469: resp_baddb();
470: return;
471: }
472:
1.10 ! kristaps 473: resp_begin_html(200, NULL);
! 474:
! 475: puts("<PRE>");
! 476: while (NULL != (p = fgetln(f, &len))) {
! 477: bold = italic = 0;
! 478: for (i = 0; i < (int)len - 1; i++) {
! 479: /*
! 480: * This means that the catpage is out of state.
! 481: * Ignore it and keep going (although the
! 482: * catpage is bogus).
! 483: */
! 484:
! 485: if ('\b' == p[i] || '\n' == p[i])
! 486: continue;
! 487:
! 488: /*
! 489: * Print a regular character.
! 490: * Close out any bold/italic scopes.
! 491: * If we're in back-space mode, make sure we'll
! 492: * have something to enter when we backspace.
! 493: */
! 494:
! 495: if ('\b' != p[i + 1]) {
! 496: if (italic)
! 497: printf("</I>");
! 498: if (bold)
! 499: printf("</B>");
! 500: italic = bold = 0;
! 501: html_putchar(p[i]);
! 502: continue;
! 503: } else if (i + 2 >= (int)len)
! 504: continue;
! 505:
! 506: /* Italic mode. */
! 507:
! 508: if ('_' == p[i]) {
! 509: if (bold)
! 510: printf("</B>");
! 511: if ( ! italic)
! 512: printf("<I>");
! 513: bold = 0;
! 514: italic = 1;
! 515: i += 2;
! 516: html_putchar(p[i]);
! 517: continue;
! 518: }
! 519:
! 520: /*
! 521: * Handle funny behaviour troff-isms.
! 522: * These grok'd from the original man2html.c.
! 523: */
! 524:
! 525: if (('+' == p[i] && 'o' == p[i + 2]) ||
! 526: ('o' == p[i] && '+' == p[i + 2]) ||
! 527: ('|' == p[i] && '=' == p[i + 2]) ||
! 528: ('=' == p[i] && '|' == p[i + 2]) ||
! 529: ('*' == p[i] && '=' == p[i + 2]) ||
! 530: ('=' == p[i] && '*' == p[i + 2]) ||
! 531: ('*' == p[i] && '|' == p[i + 2]) ||
! 532: ('|' == p[i] && '*' == p[i + 2])) {
! 533: if (italic)
! 534: printf("</I>");
! 535: if (bold)
! 536: printf("</B>");
! 537: italic = bold = 0;
! 538: putchar('*');
! 539: i += 2;
! 540: continue;
! 541: } else if (('|' == p[i] && '-' == p[i + 2]) ||
! 542: ('-' == p[i] && '|' == p[i + 1]) ||
! 543: ('+' == p[i] && '-' == p[i + 1]) ||
! 544: ('-' == p[i] && '+' == p[i + 1]) ||
! 545: ('+' == p[i] && '|' == p[i + 1]) ||
! 546: ('|' == p[i] && '+' == p[i + 1])) {
! 547: if (italic)
! 548: printf("</I>");
! 549: if (bold)
! 550: printf("</B>");
! 551: italic = bold = 0;
! 552: putchar('+');
! 553: i += 2;
! 554: continue;
! 555: }
! 556:
! 557: /* Bold mode. */
! 558:
! 559: if (italic)
! 560: printf("</I>");
! 561: if ( ! bold)
! 562: printf("<B>");
! 563: bold = 1;
! 564: italic = 0;
! 565: i += 2;
! 566: html_putchar(p[i]);
! 567: }
! 568:
! 569: /*
! 570: * Clean up the last character.
! 571: * We can get to a newline; don't print that.
! 572: */
1.9 kristaps 573:
1.10 ! kristaps 574: if (italic)
! 575: printf("</I>");
! 576: if (bold)
! 577: printf("</B>");
1.9 kristaps 578:
1.10 ! kristaps 579: if (i == (int)len - 1 && '\n' != p[i])
! 580: html_putchar(p[i]);
1.9 kristaps 581:
1.10 ! kristaps 582: putchar('\n');
! 583: }
! 584:
! 585: puts("</PRE>\n"
! 586: "</BODY>\n"
! 587: "</HTML>");
! 588:
! 589: fclose(f);
1.9 kristaps 590: }
591:
592: static void
1.8 kristaps 593: format(const char *file)
1.7 kristaps 594: {
1.8 kristaps 595: struct mparse *mp;
596: int fd;
597: struct mdoc *mdoc;
598: struct man *man;
599: void *vp;
600: enum mandoclevel rc;
1.10 ! kristaps 601: char opts[MAXPATHLEN + 128];
1.7 kristaps 602:
1.8 kristaps 603: if (-1 == (fd = open(file, O_RDONLY, 0))) {
604: resp_baddb();
1.7 kristaps 605: return;
606: }
607:
1.8 kristaps 608: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
609: rc = mparse_readfd(mp, fd, file);
610: close(fd);
1.7 kristaps 611:
1.8 kristaps 612: if (rc >= MANDOCLEVEL_FATAL) {
1.7 kristaps 613: resp_baddb();
614: return;
615: }
616:
1.10 ! kristaps 617: snprintf(opts, sizeof(opts), "style=/style.css,"
! 618: "man=%s/search.html?sec=%%S&expr=%%N,"
! 619: "includes=/cgi-bin/man.cgi/usr/include/%%I",
! 620: progname);
! 621:
1.8 kristaps 622: mparse_result(mp, &mdoc, &man);
1.10 ! kristaps 623: vp = html_alloc(opts);
1.7 kristaps 624:
1.8 kristaps 625: if (NULL != mdoc) {
626: resp_begin_http(200, NULL);
627: html_mdoc(vp, mdoc);
628: } else if (NULL != man) {
629: resp_begin_http(200, NULL);
630: html_man(vp, man);
631: } else
632: resp_baddb();
1.7 kristaps 633:
1.8 kristaps 634: html_free(vp);
635: mparse_free(mp);
1.7 kristaps 636: }
637:
638: static void
1.6 kristaps 639: pg_show(const struct manpaths *ps, const struct req *req, char *path)
1.1 kristaps 640: {
1.6 kristaps 641: char *sub;
1.7 kristaps 642: char file[MAXPATHLEN];
1.9 kristaps 643: const char *fn, *cp;
1.6 kristaps 644: int rc;
645: unsigned int vol, rec;
1.9 kristaps 646: DB *idx;
1.6 kristaps 647: DBT key, val;
648:
649: if (NULL == path) {
1.10 ! kristaps 650: resp_error400();
1.6 kristaps 651: return;
652: } else if (NULL == (sub = strrchr(path, '/'))) {
1.10 ! kristaps 653: resp_error400();
1.6 kristaps 654: return;
655: } else
656: *sub++ = '\0';
657:
658: if ( ! (atou(path, &vol) && atou(sub, &rec))) {
1.10 ! kristaps 659: resp_error400();
1.6 kristaps 660: return;
661: } else if (vol >= (unsigned int)ps->sz) {
1.10 ! kristaps 662: resp_error400();
1.6 kristaps 663: return;
664: }
665:
666: strlcpy(file, ps->paths[vol], MAXPATHLEN);
667: strlcat(file, "/mandoc.index", MAXPATHLEN);
668:
669: /* Open the index recno(3) database. */
670:
1.9 kristaps 671: idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
672: if (NULL == idx) {
1.6 kristaps 673: resp_baddb();
674: return;
675: }
676:
677: key.data = &rec;
678: key.size = 4;
679:
1.9 kristaps 680: if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
1.10 ! kristaps 681: rc < 0 ? resp_baddb() : resp_error400();
1.9 kristaps 682: goto out;
1.6 kristaps 683: }
684:
1.9 kristaps 685: cp = (char *)val.data;
1.6 kristaps 686:
1.9 kristaps 687: if (NULL == (fn = memchr(cp, '\0', val.size)))
688: resp_baddb();
689: else if (++fn - cp >= (int)val.size)
690: resp_baddb();
691: else if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
692: resp_baddb();
693: else {
694: strlcpy(file, ps->paths[vol], MAXPATHLEN);
695: strlcat(file, "/", MAXPATHLEN);
696: strlcat(file, fn, MAXPATHLEN);
697: if (0 == strcmp(cp, "cat"))
698: catman(file);
699: else
700: format(file);
701: }
702: out:
703: (*idx->close)(idx);
1.6 kristaps 704: }
705:
706: static void
707: pg_search(const struct manpaths *ps, const struct req *req, char *path)
708: {
709: size_t tt;
710: int i, sz, rc;
711: const char *ep, *start;
712: char **cp;
713: struct opts opt;
714: struct expr *expr;
715:
716: expr = NULL;
717: cp = NULL;
718: ep = NULL;
719: sz = 0;
1.1 kristaps 720:
721: memset(&opt, 0, sizeof(struct opts));
1.6 kristaps 722:
723: for (sz = i = 0; i < (int)req->fieldsz; i++)
724: if (0 == strcmp(req->fields[i].key, "expr"))
725: ep = req->fields[i].val;
726: else if (0 == strcmp(req->fields[i].key, "sec"))
727: opt.cat = req->fields[i].val;
728: else if (0 == strcmp(req->fields[i].key, "arch"))
729: opt.arch = req->fields[i].val;
730:
731: /*
732: * Poor man's tokenisation.
733: * Just break apart by spaces.
734: * Yes, this is half-ass. But it works for now.
735: */
736:
737: while (ep && isspace((unsigned char)*ep))
738: ep++;
739:
740: while (ep && '\0' != *ep) {
741: cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
742: start = ep;
743: while ('\0' != *ep && ! isspace((unsigned char)*ep))
744: ep++;
745: cp[sz] = mandoc_malloc((ep - start) + 1);
746: memcpy(cp[sz], start, ep - start);
747: cp[sz++][ep - start] = '\0';
748: while (isspace((unsigned char)*ep))
749: ep++;
750: }
751:
752: rc = -1;
753:
754: /*
755: * Pump down into apropos backend.
756: * The resp_search() function is called with the results.
757: */
758:
759: if (NULL != (expr = exprcomp(sz, cp, &tt)))
760: rc = apropos_search
761: (ps->sz, ps->paths, &opt,
762: expr, tt, (void *)req, resp_search);
763:
764: /* ...unless errors occured. */
765:
766: if (0 == rc)
767: resp_baddb();
768: else if (-1 == rc)
1.10 ! kristaps 769: resp_search(NULL, 0, (void *)req);
1.6 kristaps 770:
771: for (i = 0; i < sz; i++)
772: free(cp[i]);
773:
774: free(cp);
775: exprfree(expr);
1.1 kristaps 776: }
777:
778: int
779: main(void)
780: {
781: int i;
782: struct req req;
1.6 kristaps 783: char *p, *path, *subpath;
784: struct manpaths paths;
785:
786: /* HTTP init: read and parse the query string. */
787:
788: progname = getenv("SCRIPT_NAME");
789: if (NULL == progname)
790: progname = "";
791:
1.7 kristaps 792: cache = getenv("CACHE_DIR");
793: if (NULL == cache)
794: cache = "/cache/man.cgi";
795:
1.8 kristaps 796: if (-1 == chdir(cache)) {
797: resp_bad();
798: return(EXIT_FAILURE);
1.7 kristaps 799: }
800:
1.6 kristaps 801: host = getenv("HTTP_HOST");
802: if (NULL == host)
803: host = "localhost";
1.1 kristaps 804:
805: memset(&req, 0, sizeof(struct req));
806:
807: if (NULL != (p = getenv("QUERY_STRING")))
808: kval_parse(&req.fields, &req.fieldsz, p);
809:
1.6 kristaps 810: /* Resolve leading subpath component. */
1.1 kristaps 811:
1.6 kristaps 812: subpath = path = NULL;
1.1 kristaps 813: req.page = PAGE__MAX;
814:
815: if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
816: req.page = PAGE_INDEX;
1.6 kristaps 817:
1.1 kristaps 818: if (NULL != path && '/' == *path && '\0' == *++path)
819: req.page = PAGE_INDEX;
820:
1.6 kristaps 821: /* Strip file suffix. */
822:
823: if (NULL != path && NULL != (p = strrchr(path, '.')))
824: if (NULL != p && NULL == strchr(p, '/'))
825: *p++ = '\0';
826:
827: /* Resolve subpath component. */
1.1 kristaps 828:
829: if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6 kristaps 830: *subpath++ = '\0';
1.1 kristaps 831:
1.6 kristaps 832: /* Map path into one we recognise. */
1.1 kristaps 833:
834: if (NULL != path && '\0' != *path)
835: for (i = 0; i < (int)PAGE__MAX; i++)
836: if (0 == strcmp(pages[i], path)) {
837: req.page = (enum page)i;
838: break;
839: }
840:
1.6 kristaps 841: /* Initialise MANPATH. */
842:
843: memset(&paths, 0, sizeof(struct manpaths));
1.9 kristaps 844: manpath_manconf("etc/catman.conf", &paths);
1.6 kristaps 845:
846: /* Route pages. */
847:
1.1 kristaps 848: switch (req.page) {
849: case (PAGE_INDEX):
1.6 kristaps 850: pg_index(&paths, &req, subpath);
1.1 kristaps 851: break;
852: case (PAGE_SEARCH):
1.6 kristaps 853: pg_search(&paths, &req, subpath);
854: break;
855: case (PAGE_SHOW):
856: pg_show(&paths, &req, subpath);
1.1 kristaps 857: break;
858: default:
1.10 ! kristaps 859: resp_error404(path);
1.1 kristaps 860: break;
861: }
862:
1.6 kristaps 863: manpath_free(&paths);
1.1 kristaps 864: kval_free(req.fields, req.fieldsz);
1.6 kristaps 865:
1.1 kristaps 866: return(EXIT_SUCCESS);
867: }
CVSweb