Annotation of mandoc/cgi.c, Revision 1.40
1.40 ! kristaps 1: /* $Id: cgi.c,v 1.39 2011/12/25 17:49:52 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.24 kristaps 27: #include <dirent.h>
1.1 kristaps 28: #include <fcntl.h>
1.6 kristaps 29: #include <limits.h>
1.1 kristaps 30: #include <regex.h>
31: #include <stdio.h>
32: #include <stdarg.h>
1.5 kristaps 33: #include <stdint.h>
1.1 kristaps 34: #include <stdlib.h>
35: #include <string.h>
1.6 kristaps 36: #include <unistd.h>
1.1 kristaps 37:
1.6 kristaps 38: #include "apropos_db.h"
1.4 schwarze 39: #include "mandoc.h"
1.8 kristaps 40: #include "mdoc.h"
41: #include "man.h"
42: #include "main.h"
1.6 kristaps 43: #include "manpath.h"
1.39 kristaps 44: #include "mandocdb.h"
1.6 kristaps 45:
46: #ifdef __linux__
47: # include <db_185.h>
48: #else
49: # include <db.h>
50: #endif
1.1 kristaps 51:
52: enum page {
53: PAGE_INDEX,
54: PAGE_SEARCH,
1.6 kristaps 55: PAGE_SHOW,
1.1 kristaps 56: PAGE__MAX
57: };
58:
1.24 kristaps 59: struct paths {
60: char *name;
61: char *path;
62: };
63:
1.20 kristaps 64: /*
65: * A query as passed to the search function.
66: */
67: struct query {
68: const char *arch; /* architecture */
69: const char *sec; /* manual section */
70: const char *expr; /* unparsed expression string */
1.26 kristaps 71: int manroot; /* manroot index (or -1)*/
1.20 kristaps 72: int legacy; /* whether legacy mode */
73: };
74:
1.1 kristaps 75: struct req {
1.24 kristaps 76: struct query q;
77: struct paths *p;
78: size_t psz;
1.1 kristaps 79: enum page page;
80: };
81:
1.6 kristaps 82: static int atou(const char *, unsigned *);
1.32 kristaps 83: static void catman(const struct req *, const char *);
1.15 kristaps 84: static int cmp(const void *, const void *);
1.32 kristaps 85: static void format(const struct req *, const char *);
1.6 kristaps 86: static void html_print(const char *);
1.36 kristaps 87: static void html_printquery(const struct req *);
1.10 kristaps 88: static void html_putchar(char);
1.24 kristaps 89: static int http_decode(char *);
1.26 kristaps 90: static void http_parse(struct req *, char *);
1.36 kristaps 91: static void http_print(const char *);
92: static void http_putchar(char);
93: static void http_printquery(const struct req *);
1.24 kristaps 94: static int pathstop(DIR *);
95: static void pathgen(DIR *, char *, struct req *);
96: static void pg_index(const struct req *, char *);
97: static void pg_search(const struct req *, char *);
98: static void pg_show(const struct req *, char *);
1.7 kristaps 99: static void resp_bad(void);
1.6 kristaps 100: static void resp_baddb(void);
1.10 kristaps 101: static void resp_error400(void);
102: static void resp_error404(const char *);
1.6 kristaps 103: static void resp_begin_html(int, const char *);
104: static void resp_begin_http(int, const char *);
105: static void resp_end_html(void);
106: static void resp_index(const struct req *);
107: static void resp_search(struct res *, size_t, void *);
108: static void resp_searchform(const struct req *);
109:
1.29 kristaps 110: static const char *progname; /* cgi script name */
111: static const char *cache; /* cache directory */
112: static const char *css; /* css directory */
113: static const char *host; /* hostname */
1.1 kristaps 114:
115: static const char * const pages[PAGE__MAX] = {
116: "index", /* PAGE_INDEX */
117: "search", /* PAGE_SEARCH */
1.6 kristaps 118: "show", /* PAGE_SHOW */
1.1 kristaps 119: };
120:
1.6 kristaps 121: /*
122: * This is just OpenBSD's strtol(3) suggestion.
123: * I use it instead of strtonum(3) for portability's sake.
124: */
125: static int
126: atou(const char *buf, unsigned *v)
127: {
128: char *ep;
129: long lval;
130:
131: errno = 0;
132: lval = strtol(buf, &ep, 10);
133: if (buf[0] == '\0' || *ep != '\0')
134: return(0);
135: if ((errno == ERANGE && (lval == LONG_MAX ||
136: lval == LONG_MIN)) ||
1.38 kristaps 137: (lval > INT_MAX || lval < 0))
1.6 kristaps 138: return(0);
139:
140: *v = (unsigned int)lval;
141: return(1);
142: }
1.1 kristaps 143:
1.20 kristaps 144: /*
145: * Print a character, escaping HTML along the way.
146: * This will pass non-ASCII straight to output: be warned!
147: */
1.10 kristaps 148: static void
149: html_putchar(char c)
150: {
151:
152: switch (c) {
153: case ('"'):
154: printf(""e;");
155: break;
156: case ('&'):
157: printf("&");
158: break;
159: case ('>'):
160: printf(">");
161: break;
162: case ('<'):
163: printf("<");
164: break;
165: default:
166: putchar((unsigned char)c);
167: break;
168: }
169: }
1.36 kristaps 170: static void
171: http_printquery(const struct req *req)
172: {
173:
174: printf("&expr=");
175: http_print(req->q.expr ? req->q.expr : "");
176: printf("&sec=");
177: http_print(req->q.sec ? req->q.sec : "");
178: printf("&arch=");
179: http_print(req->q.arch ? req->q.arch : "");
180: }
181:
182:
183: static void
184: html_printquery(const struct req *req)
185: {
186:
187: printf("&expr=");
188: html_print(req->q.expr ? req->q.expr : "");
189: printf("&sec=");
190: html_print(req->q.sec ? req->q.sec : "");
191: printf("&arch=");
192: html_print(req->q.arch ? req->q.arch : "");
193: }
194:
195: static void
196: http_print(const char *p)
197: {
198:
199: if (NULL == p)
200: return;
201: while ('\0' != *p)
202: http_putchar(*p++);
203: }
1.10 kristaps 204:
1.6 kristaps 205: /*
1.20 kristaps 206: * Call through to html_putchar().
207: * Accepts NULL strings.
1.6 kristaps 208: */
1.1 kristaps 209: static void
1.6 kristaps 210: html_print(const char *p)
1.1 kristaps 211: {
1.6 kristaps 212:
213: if (NULL == p)
214: return;
1.1 kristaps 215: while ('\0' != *p)
1.10 kristaps 216: html_putchar(*p++);
1.1 kristaps 217: }
218:
219: /*
220: * Parse out key-value pairs from an HTTP request variable.
1.6 kristaps 221: * This can be either a cookie or a POST/GET string, although man.cgi
222: * uses only GET for simplicity.
1.1 kristaps 223: */
224: static void
1.26 kristaps 225: http_parse(struct req *req, char *p)
1.1 kristaps 226: {
1.26 kristaps 227: char *key, *val, *manroot;
228: int i, legacy;
1.1 kristaps 229:
1.26 kristaps 230: memset(&req->q, 0, sizeof(struct query));
1.24 kristaps 231:
232: legacy = -1;
1.26 kristaps 233: manroot = NULL;
1.1 kristaps 234:
1.36 kristaps 235: while ('\0' != *p) {
1.1 kristaps 236: key = p;
237: val = NULL;
238:
1.36 kristaps 239: p += (int)strcspn(p, ";&");
240: if ('\0' != *p)
1.1 kristaps 241: *p++ = '\0';
1.36 kristaps 242: if (NULL != (val = strchr(key, '=')))
243: *val++ = '\0';
1.1 kristaps 244:
1.36 kristaps 245: if ('\0' == *key || NULL == val || '\0' == *val)
1.1 kristaps 246: continue;
247:
248: /* Just abort handling. */
249:
1.24 kristaps 250: if ( ! http_decode(key))
251: break;
1.36 kristaps 252: if (NULL != val && ! http_decode(val))
1.24 kristaps 253: break;
254:
255: if (0 == strcmp(key, "expr"))
1.26 kristaps 256: req->q.expr = val;
1.24 kristaps 257: else if (0 == strcmp(key, "query"))
1.26 kristaps 258: req->q.expr = val;
1.24 kristaps 259: else if (0 == strcmp(key, "sec"))
1.26 kristaps 260: req->q.sec = val;
1.24 kristaps 261: else if (0 == strcmp(key, "sektion"))
1.26 kristaps 262: req->q.sec = val;
1.24 kristaps 263: else if (0 == strcmp(key, "arch"))
1.26 kristaps 264: req->q.arch = val;
265: else if (0 == strcmp(key, "manpath"))
266: manroot = val;
1.24 kristaps 267: else if (0 == strcmp(key, "apropos"))
268: legacy = 0 == strcmp(val, "0");
269: }
1.1 kristaps 270:
1.24 kristaps 271: /* Test for old man.cgi compatibility mode. */
1.1 kristaps 272:
1.40 ! kristaps 273: req->q.legacy = legacy > 0;
1.24 kristaps 274:
275: /*
276: * Section "0" means no section when in legacy mode.
277: * For some man.cgi scripts, "default" arch is none.
278: */
279:
1.26 kristaps 280: if (req->q.legacy && NULL != req->q.sec)
281: if (0 == strcmp(req->q.sec, "0"))
282: req->q.sec = NULL;
283: if (req->q.legacy && NULL != req->q.arch)
284: if (0 == strcmp(req->q.arch, "default"))
285: req->q.arch = NULL;
286:
287: /* Default to first manroot. */
288:
289: if (NULL != manroot) {
290: for (i = 0; i < (int)req->psz; i++)
291: if (0 == strcmp(req->p[i].name, manroot))
292: break;
293: req->q.manroot = i < (int)req->psz ? i : -1;
294: }
1.1 kristaps 295: }
296:
1.36 kristaps 297: static void
298: http_putchar(char c)
299: {
300:
301: if (isalnum((unsigned char)c)) {
302: putchar((unsigned char)c);
303: return;
304: } else if (' ' == c) {
305: putchar('+');
306: return;
307: }
308: printf("%%%.2x", c);
309: }
310:
1.1 kristaps 311: /*
1.6 kristaps 312: * HTTP-decode a string. The standard explanation is that this turns
313: * "%4e+foo" into "n foo" in the regular way. This is done in-place
314: * over the allocated string.
1.1 kristaps 315: */
316: static int
1.24 kristaps 317: http_decode(char *p)
1.1 kristaps 318: {
319: char hex[3];
320: int c;
321:
322: hex[2] = '\0';
323:
324: for ( ; '\0' != *p; p++) {
325: if ('%' == *p) {
326: if ('\0' == (hex[0] = *(p + 1)))
327: return(0);
328: if ('\0' == (hex[1] = *(p + 2)))
329: return(0);
330: if (1 != sscanf(hex, "%x", &c))
331: return(0);
332: if ('\0' == c)
333: return(0);
334:
335: *p = (char)c;
336: memmove(p + 1, p + 3, strlen(p + 3) + 1);
337: } else
338: *p = '+' == *p ? ' ' : *p;
339: }
340:
341: *p = '\0';
342: return(1);
343: }
344:
1.6 kristaps 345: static void
346: resp_begin_http(int code, const char *msg)
347: {
348:
349: if (200 != code)
350: printf("Status: %d %s\n", code, msg);
351:
1.20 kristaps 352: puts("Content-Type: text/html; charset=utf-8\n"
353: "Cache-Control: no-cache\n"
354: "Pragma: no-cache\n"
1.6 kristaps 355: "");
356:
357: fflush(stdout);
358: }
359:
360: static void
361: resp_begin_html(int code, const char *msg)
362: {
363:
364: resp_begin_http(code, msg);
365:
1.29 kristaps 366: printf("<!DOCTYPE HTML PUBLIC "
367: " \"-//W3C//DTD HTML 4.01//EN\""
368: " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
369: "<HTML>\n"
370: "<HEAD>\n"
371: "<META HTTP-EQUIV=\"Content-Type\""
372: " CONTENT=\"text/html; charset=utf-8\">\n"
1.32 kristaps 373: "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
374: " TYPE=\"text/css\" media=\"all\">\n"
375: "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
1.29 kristaps 376: " TYPE=\"text/css\" media=\"all\">\n"
377: "<TITLE>System Manpage Reference</TITLE>\n"
378: "</HEAD>\n"
379: "<BODY>\n"
1.32 kristaps 380: "<!-- Begin page content. //-->\n", css, css);
1.6 kristaps 381: }
382:
383: static void
384: resp_end_html(void)
385: {
386:
1.20 kristaps 387: puts("</BODY>\n"
388: "</HTML>");
1.6 kristaps 389: }
390:
391: static void
392: resp_searchform(const struct req *req)
393: {
1.27 kristaps 394: int i;
1.13 kristaps 395:
1.6 kristaps 396: puts("<!-- Begin search form. //-->");
1.32 kristaps 397: printf("<DIV ID=\"mancgi\">\n"
398: "<FORM ACTION=\"%s/search.html\" METHOD=\"get\">\n"
1.29 kristaps 399: "<FIELDSET>\n"
1.16 kristaps 400: "<LEGEND>Search Parameters</LEGEND>\n"
1.40 ! kristaps 401: "<INPUT TYPE=\"submit\" "
! 402: " VALUE=\"Search\"> for manuals satisfying \n"
1.29 kristaps 403: "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
404: progname);
1.24 kristaps 405: html_print(req->q.expr ? req->q.expr : "");
1.14 kristaps 406: printf("\">, section "
1.20 kristaps 407: "<INPUT TYPE=\"text\""
408: " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
1.24 kristaps 409: html_print(req->q.sec ? req->q.sec : "");
1.14 kristaps 410: printf("\">, arch "
1.20 kristaps 411: "<INPUT TYPE=\"text\""
412: " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
1.24 kristaps 413: html_print(req->q.arch ? req->q.arch : "");
1.27 kristaps 414: printf("\">");
415: if (req->psz > 1) {
416: puts(", <SELECT NAME=\"manpath\">");
417: for (i = 0; i < (int)req->psz; i++) {
418: printf("<OPTION %s VALUE=\"",
419: (i == req->q.manroot) ||
420: (0 == i && -1 == req->q.manroot) ?
421: "SELECTED=\"selected\"" : "");
422: html_print(req->p[i].name);
423: printf("\">");
424: html_print(req->p[i].name);
425: puts("</OPTION>");
426: }
427: puts("</SELECT>");
428: }
429: puts(".\n"
1.12 kristaps 430: "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
431: "</FIELDSET>\n"
1.32 kristaps 432: "</FORM>\n"
433: "</DIV>");
1.20 kristaps 434: puts("<!-- End search form. //-->");
1.6 kristaps 435: }
436:
437: static void
438: resp_index(const struct req *req)
439: {
440:
441: resp_begin_html(200, NULL);
442: resp_searchform(req);
443: resp_end_html();
444: }
445:
446: static void
1.10 kristaps 447: resp_error400(void)
1.9 kristaps 448: {
449:
1.10 kristaps 450: resp_begin_html(400, "Query Malformed");
1.12 kristaps 451: printf("<H1>Malformed Query</H1>\n"
452: "<P>\n"
1.20 kristaps 453: "The query your entered was malformed.\n"
454: "Try again from the\n"
1.22 kristaps 455: "<A HREF=\"%s/index.html\">main page</A>.\n"
1.12 kristaps 456: "</P>", progname);
1.9 kristaps 457: resp_end_html();
458: }
459:
460: static void
1.10 kristaps 461: resp_error404(const char *page)
1.6 kristaps 462: {
463:
464: resp_begin_html(404, "Not Found");
1.10 kristaps 465: puts("<H1>Page Not Found</H1>\n"
466: "<P>\n"
1.20 kristaps 467: "The page you're looking for, ");
468: printf("<B>");
1.10 kristaps 469: html_print(page);
1.12 kristaps 470: printf("</B>,\n"
1.20 kristaps 471: "could not be found.\n"
472: "Try searching from the\n"
1.22 kristaps 473: "<A HREF=\"%s/index.html\">main page</A>.\n"
1.12 kristaps 474: "</P>", progname);
1.6 kristaps 475: resp_end_html();
476: }
1.1 kristaps 477:
478: static void
1.7 kristaps 479: resp_bad(void)
480: {
481: resp_begin_html(500, "Internal Server Error");
482: puts("<P>Generic badness happened.</P>");
483: resp_end_html();
484: }
485:
486: static void
1.6 kristaps 487: resp_baddb(void)
1.1 kristaps 488: {
489:
1.6 kristaps 490: resp_begin_html(500, "Internal Server Error");
491: puts("<P>Your database is broken.</P>");
492: resp_end_html();
1.1 kristaps 493: }
494:
495: static void
1.6 kristaps 496: resp_search(struct res *r, size_t sz, void *arg)
1.1 kristaps 497: {
1.20 kristaps 498: int i;
1.19 kristaps 499: const struct req *req;
500:
1.29 kristaps 501: req = (const struct req *)arg;
1.37 kristaps 502:
503: if (sz > 0)
504: assert(req->q.manroot >= 0);
1.36 kristaps 505:
1.6 kristaps 506: if (1 == sz) {
507: /*
508: * If we have just one result, then jump there now
509: * without any delay.
510: */
511: puts("Status: 303 See Other");
1.36 kristaps 512: printf("Location: http://%s%s/show/%d/%u/%u.html?",
1.29 kristaps 513: host, progname, req->q.manroot,
1.6 kristaps 514: r[0].volume, r[0].rec);
1.36 kristaps 515: http_printquery(req);
516: puts("\n"
517: "Content-Type: text/html; charset=utf-8\n");
1.6 kristaps 518: return;
519: }
520:
1.15 kristaps 521: qsort(r, sz, sizeof(struct res), cmp);
522:
1.12 kristaps 523: resp_begin_html(200, NULL);
1.19 kristaps 524: resp_searchform(req);
1.6 kristaps 525:
1.33 kristaps 526: puts("<DIV CLASS=\"results\">");
527:
1.14 kristaps 528: if (0 == sz) {
1.40 ! kristaps 529: puts("<P>\n"
! 530: "No results found.\n"
! 531: "</P>\n"
! 532: "</DIV>");
1.14 kristaps 533: resp_end_html();
534: return;
535: }
536:
1.33 kristaps 537: puts("<TABLE>");
1.1 kristaps 538:
539: for (i = 0; i < (int)sz; i++) {
1.20 kristaps 540: printf("<TR>\n"
541: "<TD CLASS=\"title\">\n"
1.36 kristaps 542: "<A HREF=\"%s/show/%d/%u/%u.html?",
1.29 kristaps 543: progname, req->q.manroot,
1.26 kristaps 544: r[i].volume, r[i].rec);
1.36 kristaps 545: html_printquery(req);
546: printf("\">");
1.15 kristaps 547: html_print(r[i].title);
1.1 kristaps 548: putchar('(');
1.6 kristaps 549: html_print(r[i].cat);
550: if (r[i].arch && '\0' != *r[i].arch) {
551: putchar('/');
552: html_print(r[i].arch);
553: }
1.20 kristaps 554: printf(")</A>\n"
555: "</TD>\n"
556: "<TD CLASS=\"desc\">");
1.6 kristaps 557: html_print(r[i].desc);
1.20 kristaps 558: puts("</TD>\n"
559: "</TR>");
1.1 kristaps 560: }
1.16 kristaps 561:
1.33 kristaps 562: puts("</TABLE>\n"
563: "</DIV>");
1.6 kristaps 564: resp_end_html();
565: }
566:
567: /* ARGSUSED */
568: static void
1.24 kristaps 569: pg_index(const struct req *req, char *path)
1.6 kristaps 570: {
571:
572: resp_index(req);
1.1 kristaps 573: }
574:
575: static void
1.32 kristaps 576: catman(const struct req *req, const char *file)
1.9 kristaps 577: {
1.10 kristaps 578: FILE *f;
579: size_t len;
580: int i;
581: char *p;
582: int italic, bold;
1.9 kristaps 583:
1.10 kristaps 584: if (NULL == (f = fopen(file, "r"))) {
1.9 kristaps 585: resp_baddb();
586: return;
587: }
588:
1.32 kristaps 589: resp_begin_html(200, NULL);
590: resp_searchform(req);
591: puts("<DIV CLASS=\"catman\">\n"
592: "<PRE>");
1.10 kristaps 593:
594: while (NULL != (p = fgetln(f, &len))) {
595: bold = italic = 0;
596: for (i = 0; i < (int)len - 1; i++) {
597: /*
598: * This means that the catpage is out of state.
599: * Ignore it and keep going (although the
600: * catpage is bogus).
601: */
602:
603: if ('\b' == p[i] || '\n' == p[i])
604: continue;
605:
606: /*
607: * Print a regular character.
608: * Close out any bold/italic scopes.
609: * If we're in back-space mode, make sure we'll
610: * have something to enter when we backspace.
611: */
612:
613: if ('\b' != p[i + 1]) {
614: if (italic)
615: printf("</I>");
616: if (bold)
617: printf("</B>");
618: italic = bold = 0;
619: html_putchar(p[i]);
620: continue;
621: } else if (i + 2 >= (int)len)
622: continue;
623:
624: /* Italic mode. */
625:
626: if ('_' == p[i]) {
627: if (bold)
628: printf("</B>");
629: if ( ! italic)
630: printf("<I>");
631: bold = 0;
632: italic = 1;
633: i += 2;
634: html_putchar(p[i]);
635: continue;
636: }
637:
638: /*
639: * Handle funny behaviour troff-isms.
640: * These grok'd from the original man2html.c.
641: */
642:
643: if (('+' == p[i] && 'o' == p[i + 2]) ||
644: ('o' == p[i] && '+' == p[i + 2]) ||
645: ('|' == p[i] && '=' == p[i + 2]) ||
646: ('=' == p[i] && '|' == p[i + 2]) ||
647: ('*' == p[i] && '=' == p[i + 2]) ||
648: ('=' == p[i] && '*' == p[i + 2]) ||
649: ('*' == p[i] && '|' == p[i + 2]) ||
650: ('|' == p[i] && '*' == p[i + 2])) {
651: if (italic)
652: printf("</I>");
653: if (bold)
654: printf("</B>");
655: italic = bold = 0;
656: putchar('*');
657: i += 2;
658: continue;
659: } else if (('|' == p[i] && '-' == p[i + 2]) ||
660: ('-' == p[i] && '|' == p[i + 1]) ||
661: ('+' == p[i] && '-' == p[i + 1]) ||
662: ('-' == p[i] && '+' == p[i + 1]) ||
663: ('+' == p[i] && '|' == p[i + 1]) ||
664: ('|' == p[i] && '+' == p[i + 1])) {
665: if (italic)
666: printf("</I>");
667: if (bold)
668: printf("</B>");
669: italic = bold = 0;
670: putchar('+');
671: i += 2;
672: continue;
673: }
674:
675: /* Bold mode. */
676:
677: if (italic)
678: printf("</I>");
679: if ( ! bold)
680: printf("<B>");
681: bold = 1;
682: italic = 0;
683: i += 2;
684: html_putchar(p[i]);
685: }
686:
687: /*
688: * Clean up the last character.
689: * We can get to a newline; don't print that.
690: */
1.9 kristaps 691:
1.10 kristaps 692: if (italic)
693: printf("</I>");
694: if (bold)
695: printf("</B>");
1.9 kristaps 696:
1.10 kristaps 697: if (i == (int)len - 1 && '\n' != p[i])
698: html_putchar(p[i]);
1.9 kristaps 699:
1.10 kristaps 700: putchar('\n');
701: }
702:
703: puts("</PRE>\n"
1.32 kristaps 704: "</DIV>\n"
1.10 kristaps 705: "</BODY>\n"
706: "</HTML>");
707:
708: fclose(f);
1.9 kristaps 709: }
710:
711: static void
1.32 kristaps 712: format(const struct req *req, const char *file)
1.7 kristaps 713: {
1.8 kristaps 714: struct mparse *mp;
715: int fd;
716: struct mdoc *mdoc;
717: struct man *man;
718: void *vp;
719: enum mandoclevel rc;
1.10 kristaps 720: char opts[MAXPATHLEN + 128];
1.7 kristaps 721:
1.8 kristaps 722: if (-1 == (fd = open(file, O_RDONLY, 0))) {
723: resp_baddb();
1.7 kristaps 724: return;
725: }
726:
1.8 kristaps 727: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
728: rc = mparse_readfd(mp, fd, file);
729: close(fd);
1.7 kristaps 730:
1.8 kristaps 731: if (rc >= MANDOCLEVEL_FATAL) {
1.7 kristaps 732: resp_baddb();
733: return;
734: }
735:
1.32 kristaps 736: snprintf(opts, sizeof(opts), "fragment,"
1.10 kristaps 737: "man=%s/search.html?sec=%%S&expr=%%N,"
1.11 kristaps 738: /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
1.32 kristaps 739: progname);
1.10 kristaps 740:
1.8 kristaps 741: mparse_result(mp, &mdoc, &man);
1.32 kristaps 742: if (NULL == man && NULL == mdoc) {
743: resp_baddb();
744: mparse_free(mp);
745: return;
746: }
747:
748: resp_begin_html(200, NULL);
749: resp_searchform(req);
750:
1.10 kristaps 751: vp = html_alloc(opts);
1.7 kristaps 752:
1.32 kristaps 753: if (NULL != mdoc)
1.8 kristaps 754: html_mdoc(vp, mdoc);
1.32 kristaps 755: else
1.8 kristaps 756: html_man(vp, man);
1.32 kristaps 757:
758: puts("</BODY>\n"
759: "</HTML>");
1.7 kristaps 760:
1.8 kristaps 761: html_free(vp);
762: mparse_free(mp);
1.7 kristaps 763: }
764:
765: static void
1.24 kristaps 766: pg_show(const struct req *req, char *path)
1.1 kristaps 767: {
1.24 kristaps 768: struct manpaths ps;
1.34 kristaps 769: size_t sz;
1.6 kristaps 770: char *sub;
1.7 kristaps 771: char file[MAXPATHLEN];
1.35 kristaps 772: const char *cp;
773: int rc, catm;
1.25 kristaps 774: unsigned int vol, rec, mr;
1.9 kristaps 775: DB *idx;
1.6 kristaps 776: DBT key, val;
777:
1.24 kristaps 778: idx = NULL;
779:
1.25 kristaps 780: /* Parse out mroot, volume, and record from the path. */
781:
782: if (NULL == path || NULL == (sub = strchr(path, '/'))) {
783: resp_error400();
784: return;
785: }
786: *sub++ = '\0';
787: if ( ! atou(path, &mr)) {
788: resp_error400();
789: return;
790: }
791: path = sub;
792: if (NULL == (sub = strchr(path, '/'))) {
793: resp_error400();
794: return;
795: }
796: *sub++ = '\0';
797: if ( ! atou(path, &vol) || ! atou(sub, &rec)) {
1.10 kristaps 798: resp_error400();
1.6 kristaps 799: return;
1.25 kristaps 800: } else if (mr >= (unsigned int)req->psz) {
1.10 kristaps 801: resp_error400();
1.6 kristaps 802: return;
1.25 kristaps 803: }
1.6 kristaps 804:
1.24 kristaps 805: /*
1.26 kristaps 806: * Begin by chdir()ing into the manroot.
1.24 kristaps 807: * This way we can pick up the database files, which are
808: * relative to the manpath root.
809: */
810:
1.25 kristaps 811: if (-1 == chdir(req->p[(int)mr].path)) {
812: perror(req->p[(int)mr].path);
813: resp_baddb();
1.24 kristaps 814: return;
815: }
816:
817: memset(&ps, 0, sizeof(struct manpaths));
1.30 schwarze 818: manpath_manconf(&ps, "etc/catman.conf");
1.24 kristaps 819:
1.25 kristaps 820: if (vol >= (unsigned int)ps.sz) {
1.10 kristaps 821: resp_error400();
1.24 kristaps 822: goto out;
1.6 kristaps 823: }
824:
1.34 kristaps 825: sz = strlcpy(file, ps.paths[vol], MAXPATHLEN);
826: assert(sz < MAXPATHLEN);
1.39 kristaps 827: strlcat(file, "/", MAXPATHLEN);
828: strlcat(file, MANDOC_IDX, MAXPATHLEN);
1.6 kristaps 829:
830: /* Open the index recno(3) database. */
831:
1.9 kristaps 832: idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
833: if (NULL == idx) {
1.24 kristaps 834: perror(file);
1.6 kristaps 835: resp_baddb();
1.24 kristaps 836: goto out;
1.6 kristaps 837: }
838:
839: key.data = &rec;
840: key.size = 4;
841:
1.9 kristaps 842: if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
1.10 kristaps 843: rc < 0 ? resp_baddb() : resp_error400();
1.9 kristaps 844: goto out;
1.35 kristaps 845: } else if (0 == val.size) {
846: resp_baddb();
847: goto out;
848: }
1.6 kristaps 849:
1.9 kristaps 850: cp = (char *)val.data;
1.35 kristaps 851: catm = 'c' == *cp++;
1.6 kristaps 852:
1.35 kristaps 853: if (NULL == memchr(cp, '\0', val.size - 1))
1.9 kristaps 854: resp_baddb();
855: else {
1.34 kristaps 856: file[(int)sz] = '\0';
857: strlcat(file, "/", MAXPATHLEN);
1.35 kristaps 858: strlcat(file, cp, MAXPATHLEN);
859: if (catm)
1.34 kristaps 860: catman(req, file);
1.9 kristaps 861: else
1.34 kristaps 862: format(req, file);
1.9 kristaps 863: }
864: out:
1.24 kristaps 865: if (idx)
866: (*idx->close)(idx);
867: manpath_free(&ps);
1.6 kristaps 868: }
869:
870: static void
1.24 kristaps 871: pg_search(const struct req *req, char *path)
1.6 kristaps 872: {
873: size_t tt;
1.24 kristaps 874: struct manpaths ps;
1.20 kristaps 875: int i, sz, rc;
1.6 kristaps 876: const char *ep, *start;
877: char **cp;
878: struct opts opt;
879: struct expr *expr;
880:
1.28 kristaps 881: if (req->q.manroot < 0 || 0 == req->psz) {
1.24 kristaps 882: resp_search(NULL, 0, (void *)req);
883: return;
884: }
885:
1.1 kristaps 886: memset(&opt, 0, sizeof(struct opts));
1.6 kristaps 887:
1.24 kristaps 888: ep = req->q.expr;
889: opt.arch = req->q.arch;
890: opt.cat = req->q.sec;
1.20 kristaps 891: rc = -1;
892: sz = 0;
893: cp = NULL;
1.6 kristaps 894:
895: /*
1.24 kristaps 896: * Begin by chdir()ing into the root of the manpath.
897: * This way we can pick up the database files, which are
898: * relative to the manpath root.
899: */
900:
1.26 kristaps 901: assert(req->q.manroot < (int)req->psz);
902: if (-1 == (chdir(req->p[req->q.manroot].path))) {
903: perror(req->p[req->q.manroot].path);
1.24 kristaps 904: resp_search(NULL, 0, (void *)req);
905: return;
906: }
907:
908: memset(&ps, 0, sizeof(struct manpaths));
1.30 schwarze 909: manpath_manconf(&ps, "etc/catman.conf");
1.24 kristaps 910:
911: /*
912: * Poor man's tokenisation: just break apart by spaces.
1.6 kristaps 913: * Yes, this is half-ass. But it works for now.
914: */
915:
916: while (ep && isspace((unsigned char)*ep))
917: ep++;
918:
919: while (ep && '\0' != *ep) {
920: cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
921: start = ep;
922: while ('\0' != *ep && ! isspace((unsigned char)*ep))
923: ep++;
924: cp[sz] = mandoc_malloc((ep - start) + 1);
925: memcpy(cp[sz], start, ep - start);
926: cp[sz++][ep - start] = '\0';
927: while (isspace((unsigned char)*ep))
928: ep++;
929: }
930:
931: /*
932: * Pump down into apropos backend.
933: * The resp_search() function is called with the results.
934: */
935:
1.40 ! kristaps 936: expr = req->q.legacy ?
1.24 kristaps 937: termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt);
1.12 kristaps 938:
939: if (NULL != expr)
1.6 kristaps 940: rc = apropos_search
1.24 kristaps 941: (ps.sz, ps.paths, &opt,
1.6 kristaps 942: expr, tt, (void *)req, resp_search);
943:
944: /* ...unless errors occured. */
945:
946: if (0 == rc)
947: resp_baddb();
948: else if (-1 == rc)
1.10 kristaps 949: resp_search(NULL, 0, (void *)req);
1.6 kristaps 950:
951: for (i = 0; i < sz; i++)
952: free(cp[i]);
953:
954: free(cp);
955: exprfree(expr);
1.24 kristaps 956: manpath_free(&ps);
1.1 kristaps 957: }
958:
959: int
960: main(void)
961: {
962: int i;
1.24 kristaps 963: char buf[MAXPATHLEN];
964: DIR *cwd;
1.1 kristaps 965: struct req req;
1.6 kristaps 966: char *p, *path, *subpath;
967:
1.24 kristaps 968: /* Scan our run-time environment. */
1.6 kristaps 969:
1.29 kristaps 970: if (NULL == (cache = getenv("CACHE_DIR")))
971: cache = "/cache/man.cgi";
972:
973: if (NULL == (progname = getenv("SCRIPT_NAME")))
1.6 kristaps 974: progname = "";
975:
1.29 kristaps 976: if (NULL == (css = getenv("CSS_DIR")))
1.31 kristaps 977: css = "";
1.7 kristaps 978:
1.29 kristaps 979: if (NULL == (host = getenv("HTTP_HOST")))
1.24 kristaps 980: host = "localhost";
981:
982: /*
983: * First we change directory into the cache directory so that
984: * subsequent scanning for manpath directories is rooted
985: * relative to the same position.
986: */
987:
1.8 kristaps 988: if (-1 == chdir(cache)) {
1.24 kristaps 989: perror(cache);
990: resp_bad();
991: return(EXIT_FAILURE);
992: } else if (NULL == (cwd = opendir(cache))) {
993: perror(cache);
1.8 kristaps 994: resp_bad();
995: return(EXIT_FAILURE);
1.24 kristaps 996: }
997:
998: memset(&req, 0, sizeof(struct req));
1.7 kristaps 999:
1.24 kristaps 1000: strlcpy(buf, ".", MAXPATHLEN);
1001: pathgen(cwd, buf, &req);
1002: closedir(cwd);
1.1 kristaps 1003:
1.24 kristaps 1004: /* Next parse out the query string. */
1.1 kristaps 1005:
1006: if (NULL != (p = getenv("QUERY_STRING")))
1.26 kristaps 1007: http_parse(&req, p);
1.1 kristaps 1008:
1.24 kristaps 1009: /*
1010: * Now juggle paths to extract information.
1011: * We want to extract our filetype (the file suffix), the
1012: * initial path component, then the trailing component(s).
1013: * Start with leading subpath component.
1014: */
1.1 kristaps 1015:
1.6 kristaps 1016: subpath = path = NULL;
1.1 kristaps 1017: req.page = PAGE__MAX;
1018:
1019: if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
1020: req.page = PAGE_INDEX;
1.6 kristaps 1021:
1.1 kristaps 1022: if (NULL != path && '/' == *path && '\0' == *++path)
1023: req.page = PAGE_INDEX;
1024:
1.6 kristaps 1025: /* Strip file suffix. */
1026:
1027: if (NULL != path && NULL != (p = strrchr(path, '.')))
1028: if (NULL != p && NULL == strchr(p, '/'))
1029: *p++ = '\0';
1030:
1031: /* Resolve subpath component. */
1.1 kristaps 1032:
1033: if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6 kristaps 1034: *subpath++ = '\0';
1.1 kristaps 1035:
1.6 kristaps 1036: /* Map path into one we recognise. */
1.1 kristaps 1037:
1038: if (NULL != path && '\0' != *path)
1039: for (i = 0; i < (int)PAGE__MAX; i++)
1040: if (0 == strcmp(pages[i], path)) {
1041: req.page = (enum page)i;
1042: break;
1043: }
1044:
1.6 kristaps 1045: /* Route pages. */
1046:
1.1 kristaps 1047: switch (req.page) {
1048: case (PAGE_INDEX):
1.24 kristaps 1049: pg_index(&req, subpath);
1.1 kristaps 1050: break;
1051: case (PAGE_SEARCH):
1.24 kristaps 1052: pg_search(&req, subpath);
1.6 kristaps 1053: break;
1054: case (PAGE_SHOW):
1.24 kristaps 1055: pg_show(&req, subpath);
1.1 kristaps 1056: break;
1057: default:
1.10 kristaps 1058: resp_error404(path);
1.1 kristaps 1059: break;
1060: }
1061:
1.24 kristaps 1062: for (i = 0; i < (int)req.psz; i++) {
1063: free(req.p[i].path);
1064: free(req.p[i].name);
1065: }
1.6 kristaps 1066:
1.24 kristaps 1067: free(req.p);
1.1 kristaps 1068: return(EXIT_SUCCESS);
1069: }
1.15 kristaps 1070:
1071: static int
1072: cmp(const void *p1, const void *p2)
1073: {
1074:
1075: return(strcasecmp(((const struct res *)p1)->title,
1076: ((const struct res *)p2)->title));
1077: }
1078:
1.24 kristaps 1079: /*
1080: * Check to see if an "etc" path consists of a catman.conf file. If it
1081: * does, that means that the path contains a tree created by catman(8)
1082: * and should be used for indexing.
1083: */
1084: static int
1085: pathstop(DIR *dir)
1086: {
1087: struct dirent *d;
1088:
1089: while (NULL != (d = readdir(dir)))
1090: if (DT_REG == d->d_type)
1091: if (0 == strcmp(d->d_name, "catman.conf"))
1092: return(1);
1093:
1094: return(0);
1095: }
1096:
1097: /*
1098: * Scan for indexable paths.
1099: * This adds all paths with "etc/catman.conf" to the buffer.
1100: */
1101: static void
1102: pathgen(DIR *dir, char *path, struct req *req)
1103: {
1104: struct dirent *d;
1105: char *cp;
1106: DIR *cd;
1107: int rc;
1108: size_t sz, ssz;
1109:
1110: sz = strlcat(path, "/", MAXPATHLEN);
1111: if (sz >= MAXPATHLEN) {
1112: fprintf(stderr, "%s: Path too long", path);
1113: return;
1114: }
1115:
1116: /*
1117: * First, scan for the "etc" directory.
1118: * If it's found, then see if it should cause us to stop. This
1119: * happens when a catman.conf is found in the directory.
1120: */
1121:
1122: rc = 0;
1123: while (0 == rc && NULL != (d = readdir(dir))) {
1124: if (DT_DIR != d->d_type || strcmp(d->d_name, "etc"))
1125: continue;
1126:
1127: path[(int)sz] = '\0';
1128: ssz = strlcat(path, d->d_name, MAXPATHLEN);
1129:
1130: if (ssz >= MAXPATHLEN) {
1131: fprintf(stderr, "%s: Path too long", path);
1132: return;
1133: } else if (NULL == (cd = opendir(path))) {
1134: perror(path);
1135: return;
1136: }
1137:
1138: rc = pathstop(cd);
1139: closedir(cd);
1140: }
1141:
1142: if (rc > 0) {
1143: /* This also strips the trailing slash. */
1.27 kristaps 1144: path[(int)--sz] = '\0';
1.24 kristaps 1145: req->p = mandoc_realloc
1146: (req->p,
1147: (req->psz + 1) * sizeof(struct paths));
1.27 kristaps 1148: /*
1149: * Strip out the leading "./" unless we're just a ".",
1150: * in which case use an empty string as our name.
1151: */
1.24 kristaps 1152: req->p[(int)req->psz].path = mandoc_strdup(path);
1153: req->p[(int)req->psz].name =
1.27 kristaps 1154: cp = mandoc_strdup(path + (1 == sz ? 1 : 2));
1.24 kristaps 1155: req->psz++;
1156: /*
1157: * The name is just the path with all the slashes taken
1158: * out of it. Simple but effective.
1159: */
1160: for ( ; '\0' != *cp; cp++)
1161: if ('/' == *cp)
1162: *cp = ' ';
1163: return;
1164: }
1165:
1166: /*
1167: * If no etc/catman.conf was found, recursively enter child
1168: * directory and continue scanning.
1169: */
1170:
1171: rewinddir(dir);
1172: while (NULL != (d = readdir(dir))) {
1173: if (DT_DIR != d->d_type || '.' == d->d_name[0])
1174: continue;
1175:
1176: path[(int)sz] = '\0';
1177: ssz = strlcat(path, d->d_name, MAXPATHLEN);
1178:
1179: if (ssz >= MAXPATHLEN) {
1180: fprintf(stderr, "%s: Path too long", path);
1181: return;
1182: } else if (NULL == (cd = opendir(path))) {
1183: perror(path);
1184: return;
1185: }
1186:
1187: pathgen(cd, path, req);
1188: closedir(cd);
1189: }
1190: }
CVSweb