Annotation of mandoc/cgi.c, Revision 1.12
1.12 ! kristaps 1: /* $Id: cgi.c,v 1.11 2011/12/07 11:52:36 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.12 ! kristaps 300: " <LINK REL=\"stylesheet\" HREF=\"/man.cgi.css\"" "\n"
! 301: " TYPE=\"text/css\" media=\"all\">" "\n"
1.6 kristaps 302: " <TITLE>System Manpage Reference</TITLE>" "\n"
303: " </HEAD>" "\n"
304: " <BODY>" "\n"
305: "<!-- Begin page content. //-->");
306: }
307:
308: static void
309: resp_end_html(void)
310: {
311:
312: puts(" </BODY>\n</HTML>");
313: }
314:
315: static void
316: resp_searchform(const struct req *req)
317: {
318: int i;
319: const char *expr, *sec, *arch;
320:
321: expr = sec = arch = "";
322:
323: for (i = 0; i < (int)req->fieldsz; i++)
324: if (0 == strcmp(req->fields[i].key, "expr"))
325: expr = req->fields[i].val;
326: else if (0 == strcmp(req->fields[i].key, "sec"))
327: sec = req->fields[i].val;
328: else if (0 == strcmp(req->fields[i].key, "arch"))
329: arch = req->fields[i].val;
330:
331: puts("<!-- Begin search form. //-->");
332: printf("<FORM ACTION=\"");
333: html_print(progname);
1.9 kristaps 334: printf("/search.html\" METHOD=\"get\">\n");
1.12 ! kristaps 335: puts("<FIELDSET>\n"
! 336: "<INPUT TYPE=\"submit\" NAME=\"op\" "
! 337: "VALUE=\"Whatis\"> or \n"
! 338: "<INPUT TYPE=\"submit\" NAME=\"op\" "
! 339: "VALUE=\"apropos\"> for manuals satisfying \n"
! 340: "<INPUT TYPE=\"text\" SIZE=\"40\" "
! 341: "NAME=\"expr\" VALUE=\"");
1.6 kristaps 342: html_print(expr);
1.12 ! kristaps 343: puts("\">, section "
! 344: "<INPUT TYPE=\"text\" "
! 345: "SIZE=\"4\" NAME=\"sec\" VALUE=\"");
1.6 kristaps 346: html_print(sec);
1.12 ! kristaps 347: puts("\">, arch "
! 348: "<INPUT TYPE=\"text\" "
! 349: "SIZE=\"8\" NAME=\"arch\" VALUE=\"");
1.6 kristaps 350: html_print(arch);
1.12 ! kristaps 351: puts("\">.\n"
! 352: "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
! 353: "</FIELDSET>\n"
! 354: "</FORM>\n"
! 355: "<!-- End search form. //-->");
1.6 kristaps 356: }
357:
358: static void
359: resp_index(const struct req *req)
360: {
361:
362: resp_begin_html(200, NULL);
363: resp_searchform(req);
364: resp_end_html();
365: }
366:
367: static void
1.10 kristaps 368: resp_error400(void)
1.9 kristaps 369: {
370:
1.10 kristaps 371: resp_begin_html(400, "Query Malformed");
1.12 ! kristaps 372: printf("<H1>Malformed Query</H1>\n"
! 373: "<P>\n"
! 374: " The query your entered was malformed.\n"
! 375: " Try again from the\n"
! 376: " <A HREF=\"%s/index.html\">main page</A>\n"
! 377: "</P>", progname);
1.9 kristaps 378: resp_end_html();
379: }
380:
381: static void
1.10 kristaps 382: resp_error404(const char *page)
1.6 kristaps 383: {
384:
385: resp_begin_html(404, "Not Found");
1.10 kristaps 386: puts("<H1>Page Not Found</H1>\n"
387: "<P>\n"
388: " The page you're looking for, ");
389: printf(" <B>");
390: html_print(page);
1.12 ! kristaps 391: printf("</B>,\n"
! 392: " could not be found.\n"
! 393: " Try searching from the\n"
! 394: " <A HREF=\"%s/index.html\">main page</A>\n"
! 395: "</P>", progname);
1.6 kristaps 396: resp_end_html();
397: }
1.1 kristaps 398:
399: static void
1.7 kristaps 400: resp_bad(void)
401: {
402: resp_begin_html(500, "Internal Server Error");
403: puts("<P>Generic badness happened.</P>");
404: resp_end_html();
405: }
406:
407: static void
1.6 kristaps 408: resp_baddb(void)
1.1 kristaps 409: {
410:
1.6 kristaps 411: resp_begin_html(500, "Internal Server Error");
412: puts("<P>Your database is broken.</P>");
413: resp_end_html();
1.1 kristaps 414: }
415:
416: static void
1.6 kristaps 417: resp_search(struct res *r, size_t sz, void *arg)
1.1 kristaps 418: {
419: int i;
420:
1.6 kristaps 421: if (1 == sz) {
422: /*
423: * If we have just one result, then jump there now
424: * without any delay.
425: */
426: puts("Status: 303 See Other");
427: printf("Location: http://%s%s/show/%u/%u.html\n",
428: host, progname,
429: r[0].volume, r[0].rec);
430: puts("Content-Type: text/html; charset=utf-8\n");
431: return;
432: }
433:
1.12 ! kristaps 434: resp_begin_html(200, NULL);
1.6 kristaps 435: resp_searchform((const struct req *)arg);
436:
437: if (0 == sz)
438: puts("<P>No results found.</P>");
1.1 kristaps 439:
440: for (i = 0; i < (int)sz; i++) {
1.6 kristaps 441: printf("<P><A HREF=\"");
442: html_print(progname);
443: printf("/show/%u/%u.html\">", r[i].volume, r[i].rec);
444: html_print(r[i].title);
1.1 kristaps 445: putchar('(');
1.6 kristaps 446: html_print(r[i].cat);
447: if (r[i].arch && '\0' != *r[i].arch) {
448: putchar('/');
449: html_print(r[i].arch);
450: }
451: printf(")</A> ");
452: html_print(r[i].desc);
453: puts("</P>");
1.1 kristaps 454: }
1.6 kristaps 455:
456: resp_end_html();
457: }
458:
459: /* ARGSUSED */
460: static void
461: pg_index(const struct manpaths *ps, const struct req *req, char *path)
462: {
463:
464: resp_index(req);
1.1 kristaps 465: }
466:
467: static void
1.9 kristaps 468: catman(const char *file)
469: {
1.10 kristaps 470: FILE *f;
471: size_t len;
472: int i;
473: char *p;
474: int italic, bold;
1.9 kristaps 475:
1.10 kristaps 476: if (NULL == (f = fopen(file, "r"))) {
1.9 kristaps 477: resp_baddb();
478: return;
479: }
480:
1.12 ! kristaps 481: resp_begin_http(200, NULL);
! 482: puts("<!DOCTYPE HTML PUBLIC " "\n"
! 483: " \"-//W3C//DTD HTML 4.01//EN\"" "\n"
! 484: " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n"
! 485: "<HTML>" "\n"
! 486: " <HEAD>" "\n"
! 487: " <META HTTP-EQUIV=\"Content-Type\" " "\n"
! 488: " CONTENT=\"text/html; charset=utf-8\">" "\n"
! 489: " <LINK REL=\"stylesheet\" HREF=\"/catman.css\"" "\n"
! 490: " TYPE=\"text/css\" media=\"all\">" "\n"
! 491: " <TITLE>System Manpage Reference</TITLE>" "\n"
! 492: " </HEAD>" "\n"
! 493: " <BODY>" "\n"
! 494: "<!-- Begin page content. //-->");
1.10 kristaps 495:
496: puts("<PRE>");
497: while (NULL != (p = fgetln(f, &len))) {
498: bold = italic = 0;
499: for (i = 0; i < (int)len - 1; i++) {
500: /*
501: * This means that the catpage is out of state.
502: * Ignore it and keep going (although the
503: * catpage is bogus).
504: */
505:
506: if ('\b' == p[i] || '\n' == p[i])
507: continue;
508:
509: /*
510: * Print a regular character.
511: * Close out any bold/italic scopes.
512: * If we're in back-space mode, make sure we'll
513: * have something to enter when we backspace.
514: */
515:
516: if ('\b' != p[i + 1]) {
517: if (italic)
518: printf("</I>");
519: if (bold)
520: printf("</B>");
521: italic = bold = 0;
522: html_putchar(p[i]);
523: continue;
524: } else if (i + 2 >= (int)len)
525: continue;
526:
527: /* Italic mode. */
528:
529: if ('_' == p[i]) {
530: if (bold)
531: printf("</B>");
532: if ( ! italic)
533: printf("<I>");
534: bold = 0;
535: italic = 1;
536: i += 2;
537: html_putchar(p[i]);
538: continue;
539: }
540:
541: /*
542: * Handle funny behaviour troff-isms.
543: * These grok'd from the original man2html.c.
544: */
545:
546: if (('+' == p[i] && 'o' == p[i + 2]) ||
547: ('o' == p[i] && '+' == p[i + 2]) ||
548: ('|' == p[i] && '=' == p[i + 2]) ||
549: ('=' == p[i] && '|' == p[i + 2]) ||
550: ('*' == p[i] && '=' == p[i + 2]) ||
551: ('=' == p[i] && '*' == p[i + 2]) ||
552: ('*' == p[i] && '|' == p[i + 2]) ||
553: ('|' == p[i] && '*' == p[i + 2])) {
554: if (italic)
555: printf("</I>");
556: if (bold)
557: printf("</B>");
558: italic = bold = 0;
559: putchar('*');
560: i += 2;
561: continue;
562: } else if (('|' == p[i] && '-' == p[i + 2]) ||
563: ('-' == p[i] && '|' == p[i + 1]) ||
564: ('+' == p[i] && '-' == p[i + 1]) ||
565: ('-' == p[i] && '+' == p[i + 1]) ||
566: ('+' == p[i] && '|' == p[i + 1]) ||
567: ('|' == p[i] && '+' == p[i + 1])) {
568: if (italic)
569: printf("</I>");
570: if (bold)
571: printf("</B>");
572: italic = bold = 0;
573: putchar('+');
574: i += 2;
575: continue;
576: }
577:
578: /* Bold mode. */
579:
580: if (italic)
581: printf("</I>");
582: if ( ! bold)
583: printf("<B>");
584: bold = 1;
585: italic = 0;
586: i += 2;
587: html_putchar(p[i]);
588: }
589:
590: /*
591: * Clean up the last character.
592: * We can get to a newline; don't print that.
593: */
1.9 kristaps 594:
1.10 kristaps 595: if (italic)
596: printf("</I>");
597: if (bold)
598: printf("</B>");
1.9 kristaps 599:
1.10 kristaps 600: if (i == (int)len - 1 && '\n' != p[i])
601: html_putchar(p[i]);
1.9 kristaps 602:
1.10 kristaps 603: putchar('\n');
604: }
605:
606: puts("</PRE>\n"
607: "</BODY>\n"
608: "</HTML>");
609:
610: fclose(f);
1.9 kristaps 611: }
612:
613: static void
1.8 kristaps 614: format(const char *file)
1.7 kristaps 615: {
1.8 kristaps 616: struct mparse *mp;
617: int fd;
618: struct mdoc *mdoc;
619: struct man *man;
620: void *vp;
621: enum mandoclevel rc;
1.10 kristaps 622: char opts[MAXPATHLEN + 128];
1.7 kristaps 623:
1.8 kristaps 624: if (-1 == (fd = open(file, O_RDONLY, 0))) {
625: resp_baddb();
1.7 kristaps 626: return;
627: }
628:
1.8 kristaps 629: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
630: rc = mparse_readfd(mp, fd, file);
631: close(fd);
1.7 kristaps 632:
1.8 kristaps 633: if (rc >= MANDOCLEVEL_FATAL) {
1.7 kristaps 634: resp_baddb();
635: return;
636: }
637:
1.11 kristaps 638: snprintf(opts, sizeof(opts), "style=/man.css,"
1.10 kristaps 639: "man=%s/search.html?sec=%%S&expr=%%N,"
1.11 kristaps 640: /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
1.10 kristaps 641: progname);
642:
1.8 kristaps 643: mparse_result(mp, &mdoc, &man);
1.10 kristaps 644: vp = html_alloc(opts);
1.7 kristaps 645:
1.8 kristaps 646: if (NULL != mdoc) {
647: resp_begin_http(200, NULL);
648: html_mdoc(vp, mdoc);
649: } else if (NULL != man) {
650: resp_begin_http(200, NULL);
651: html_man(vp, man);
652: } else
653: resp_baddb();
1.7 kristaps 654:
1.8 kristaps 655: html_free(vp);
656: mparse_free(mp);
1.7 kristaps 657: }
658:
659: static void
1.6 kristaps 660: pg_show(const struct manpaths *ps, const struct req *req, char *path)
1.1 kristaps 661: {
1.6 kristaps 662: char *sub;
1.7 kristaps 663: char file[MAXPATHLEN];
1.9 kristaps 664: const char *fn, *cp;
1.6 kristaps 665: int rc;
666: unsigned int vol, rec;
1.9 kristaps 667: DB *idx;
1.6 kristaps 668: DBT key, val;
669:
670: if (NULL == path) {
1.10 kristaps 671: resp_error400();
1.6 kristaps 672: return;
673: } else if (NULL == (sub = strrchr(path, '/'))) {
1.10 kristaps 674: resp_error400();
1.6 kristaps 675: return;
676: } else
677: *sub++ = '\0';
678:
679: if ( ! (atou(path, &vol) && atou(sub, &rec))) {
1.10 kristaps 680: resp_error400();
1.6 kristaps 681: return;
682: } else if (vol >= (unsigned int)ps->sz) {
1.10 kristaps 683: resp_error400();
1.6 kristaps 684: return;
685: }
686:
687: strlcpy(file, ps->paths[vol], MAXPATHLEN);
688: strlcat(file, "/mandoc.index", MAXPATHLEN);
689:
690: /* Open the index recno(3) database. */
691:
1.9 kristaps 692: idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
693: if (NULL == idx) {
1.6 kristaps 694: resp_baddb();
695: return;
696: }
697:
698: key.data = &rec;
699: key.size = 4;
700:
1.9 kristaps 701: if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
1.10 kristaps 702: rc < 0 ? resp_baddb() : resp_error400();
1.9 kristaps 703: goto out;
1.6 kristaps 704: }
705:
1.9 kristaps 706: cp = (char *)val.data;
1.6 kristaps 707:
1.9 kristaps 708: if (NULL == (fn = memchr(cp, '\0', val.size)))
709: resp_baddb();
710: else if (++fn - cp >= (int)val.size)
711: resp_baddb();
712: else if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
713: resp_baddb();
714: else {
715: strlcpy(file, ps->paths[vol], MAXPATHLEN);
716: strlcat(file, "/", MAXPATHLEN);
717: strlcat(file, fn, MAXPATHLEN);
718: if (0 == strcmp(cp, "cat"))
719: catman(file);
720: else
721: format(file);
722: }
723: out:
724: (*idx->close)(idx);
1.6 kristaps 725: }
726:
727: static void
728: pg_search(const struct manpaths *ps, const struct req *req, char *path)
729: {
730: size_t tt;
1.12 ! kristaps 731: int i, sz, rc, whatis;
1.6 kristaps 732: const char *ep, *start;
733: char **cp;
734: struct opts opt;
735: struct expr *expr;
736:
737: expr = NULL;
738: cp = NULL;
739: ep = NULL;
740: sz = 0;
1.12 ! kristaps 741: whatis = 0;
1.1 kristaps 742:
743: memset(&opt, 0, sizeof(struct opts));
1.6 kristaps 744:
745: for (sz = i = 0; i < (int)req->fieldsz; i++)
746: if (0 == strcmp(req->fields[i].key, "expr"))
747: ep = req->fields[i].val;
748: else if (0 == strcmp(req->fields[i].key, "sec"))
749: opt.cat = req->fields[i].val;
750: else if (0 == strcmp(req->fields[i].key, "arch"))
751: opt.arch = req->fields[i].val;
1.12 ! kristaps 752: else if (0 == strcmp(req->fields[i].key, "op"))
! 753: whatis = 0 == strcasecmp
! 754: (req->fields[i].val, "whatis");
1.6 kristaps 755:
756: /*
757: * Poor man's tokenisation.
758: * Just break apart by spaces.
759: * Yes, this is half-ass. But it works for now.
760: */
761:
762: while (ep && isspace((unsigned char)*ep))
763: ep++;
764:
765: while (ep && '\0' != *ep) {
766: cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
767: start = ep;
768: while ('\0' != *ep && ! isspace((unsigned char)*ep))
769: ep++;
770: cp[sz] = mandoc_malloc((ep - start) + 1);
771: memcpy(cp[sz], start, ep - start);
772: cp[sz++][ep - start] = '\0';
773: while (isspace((unsigned char)*ep))
774: ep++;
775: }
776:
777: rc = -1;
778:
779: /*
780: * Pump down into apropos backend.
781: * The resp_search() function is called with the results.
782: */
783:
1.12 ! kristaps 784: expr = whatis ? termcomp(sz, cp, &tt) :
! 785: exprcomp(sz, cp, &tt);
! 786:
! 787: if (NULL != expr)
1.6 kristaps 788: rc = apropos_search
789: (ps->sz, ps->paths, &opt,
790: expr, tt, (void *)req, resp_search);
791:
792: /* ...unless errors occured. */
793:
794: if (0 == rc)
795: resp_baddb();
796: else if (-1 == rc)
1.10 kristaps 797: resp_search(NULL, 0, (void *)req);
1.6 kristaps 798:
799: for (i = 0; i < sz; i++)
800: free(cp[i]);
801:
802: free(cp);
803: exprfree(expr);
1.1 kristaps 804: }
805:
806: int
807: main(void)
808: {
809: int i;
810: struct req req;
1.6 kristaps 811: char *p, *path, *subpath;
812: struct manpaths paths;
813:
814: /* HTTP init: read and parse the query string. */
815:
816: progname = getenv("SCRIPT_NAME");
817: if (NULL == progname)
818: progname = "";
819:
1.7 kristaps 820: cache = getenv("CACHE_DIR");
821: if (NULL == cache)
822: cache = "/cache/man.cgi";
823:
1.8 kristaps 824: if (-1 == chdir(cache)) {
825: resp_bad();
826: return(EXIT_FAILURE);
1.7 kristaps 827: }
828:
1.6 kristaps 829: host = getenv("HTTP_HOST");
830: if (NULL == host)
831: host = "localhost";
1.1 kristaps 832:
833: memset(&req, 0, sizeof(struct req));
834:
835: if (NULL != (p = getenv("QUERY_STRING")))
836: kval_parse(&req.fields, &req.fieldsz, p);
837:
1.6 kristaps 838: /* Resolve leading subpath component. */
1.1 kristaps 839:
1.6 kristaps 840: subpath = path = NULL;
1.1 kristaps 841: req.page = PAGE__MAX;
842:
843: if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
844: req.page = PAGE_INDEX;
1.6 kristaps 845:
1.1 kristaps 846: if (NULL != path && '/' == *path && '\0' == *++path)
847: req.page = PAGE_INDEX;
848:
1.6 kristaps 849: /* Strip file suffix. */
850:
851: if (NULL != path && NULL != (p = strrchr(path, '.')))
852: if (NULL != p && NULL == strchr(p, '/'))
853: *p++ = '\0';
854:
855: /* Resolve subpath component. */
1.1 kristaps 856:
857: if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6 kristaps 858: *subpath++ = '\0';
1.1 kristaps 859:
1.6 kristaps 860: /* Map path into one we recognise. */
1.1 kristaps 861:
862: if (NULL != path && '\0' != *path)
863: for (i = 0; i < (int)PAGE__MAX; i++)
864: if (0 == strcmp(pages[i], path)) {
865: req.page = (enum page)i;
866: break;
867: }
868:
1.6 kristaps 869: /* Initialise MANPATH. */
870:
871: memset(&paths, 0, sizeof(struct manpaths));
1.9 kristaps 872: manpath_manconf("etc/catman.conf", &paths);
1.6 kristaps 873:
874: /* Route pages. */
875:
1.1 kristaps 876: switch (req.page) {
877: case (PAGE_INDEX):
1.6 kristaps 878: pg_index(&paths, &req, subpath);
1.1 kristaps 879: break;
880: case (PAGE_SEARCH):
1.6 kristaps 881: pg_search(&paths, &req, subpath);
882: break;
883: case (PAGE_SHOW):
884: pg_show(&paths, &req, subpath);
1.1 kristaps 885: break;
886: default:
1.10 kristaps 887: resp_error404(path);
1.1 kristaps 888: break;
889: }
890:
1.6 kristaps 891: manpath_free(&paths);
1.1 kristaps 892: kval_free(req.fields, req.fieldsz);
1.6 kristaps 893:
1.1 kristaps 894: return(EXIT_SUCCESS);
895: }
CVSweb