Annotation of mandoc/cgi.c, Revision 1.59
1.59 ! schwarze 1: /* $Id: cgi.c,v 1.58 2014/07/09 14:18:59 schwarze Exp $ */
1.6 kristaps 2: /*
1.42 kristaps 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.52 schwarze 4: * Copyright (c) 2014 Ingo Schwarze <schwarze@usta.de>
1.6 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
22: #include <ctype.h>
1.58 schwarze 23: #include <errno.h>
1.1 kristaps 24: #include <fcntl.h>
1.6 kristaps 25: #include <limits.h>
1.1 kristaps 26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
1.6 kristaps 29: #include <unistd.h>
1.1 kristaps 30:
1.4 schwarze 31: #include "mandoc.h"
1.50 schwarze 32: #include "mandoc_aux.h"
1.8 kristaps 33: #include "main.h"
1.6 kristaps 34: #include "manpath.h"
1.52 schwarze 35: #include "mansearch.h"
1.1 kristaps 36:
37: enum page {
38: PAGE_INDEX,
39: PAGE_SEARCH,
1.6 kristaps 40: PAGE_SHOW,
1.1 kristaps 41: PAGE__MAX
42: };
43:
1.20 kristaps 44: /*
45: * A query as passed to the search function.
46: */
47: struct query {
1.58 schwarze 48: const char *manpath; /* desired manual directory */
1.20 kristaps 49: const char *arch; /* architecture */
50: const char *sec; /* manual section */
51: const char *expr; /* unparsed expression string */
52: int legacy; /* whether legacy mode */
53: };
54:
1.1 kristaps 55: struct req {
1.58 schwarze 56: struct query q;
57: char **p; /* array of available manpaths */
58: size_t psz; /* number of available manpaths */
59: enum page page;
1.1 kristaps 60: };
61:
1.32 kristaps 62: static void catman(const struct req *, const char *);
1.15 kristaps 63: static int cmp(const void *, const void *);
1.32 kristaps 64: static void format(const struct req *, const char *);
1.6 kristaps 65: static void html_print(const char *);
1.36 kristaps 66: static void html_printquery(const struct req *);
1.10 kristaps 67: static void html_putchar(char);
1.24 kristaps 68: static int http_decode(char *);
1.26 kristaps 69: static void http_parse(struct req *, char *);
1.36 kristaps 70: static void http_print(const char *);
71: static void http_putchar(char);
72: static void http_printquery(const struct req *);
1.54 schwarze 73: static void pathgen(struct req *);
1.24 kristaps 74: static void pg_index(const struct req *, char *);
75: static void pg_search(const struct req *, char *);
76: static void pg_show(const struct req *, char *);
1.6 kristaps 77: static void resp_begin_html(int, const char *);
78: static void resp_begin_http(int, const char *);
79: static void resp_end_html(void);
1.59 ! schwarze 80: static void resp_error_badrequest(const char *);
! 81: static void resp_error_internal(void);
! 82: static void resp_error_notfound(const char *);
1.6 kristaps 83: static void resp_index(const struct req *);
1.59 ! schwarze 84: static void resp_noresult(const struct req *,
! 85: const char *);
1.52 schwarze 86: static void resp_search(const struct req *,
87: struct manpage *, size_t);
1.6 kristaps 88: static void resp_searchform(const struct req *);
89:
1.58 schwarze 90: static const char *scriptname; /* CGI script name */
91: static const char *mandir; /* contains all manpath directories */
92: static const char *cssdir; /* css directory */
93: static const char *httphost; /* hostname used in the URIs */
1.1 kristaps 94:
95: static const char * const pages[PAGE__MAX] = {
96: "index", /* PAGE_INDEX */
97: "search", /* PAGE_SEARCH */
1.6 kristaps 98: "show", /* PAGE_SHOW */
1.1 kristaps 99: };
100:
1.6 kristaps 101: /*
1.20 kristaps 102: * Print a character, escaping HTML along the way.
103: * This will pass non-ASCII straight to output: be warned!
104: */
1.10 kristaps 105: static void
106: html_putchar(char c)
107: {
108:
109: switch (c) {
110: case ('"'):
111: printf(""e;");
112: break;
113: case ('&'):
114: printf("&");
115: break;
116: case ('>'):
117: printf(">");
118: break;
119: case ('<'):
120: printf("<");
121: break;
122: default:
123: putchar((unsigned char)c);
124: break;
125: }
126: }
1.57 schwarze 127:
1.36 kristaps 128: static void
129: http_printquery(const struct req *req)
130: {
131:
1.58 schwarze 132: if (NULL != req->q.manpath) {
1.53 schwarze 133: printf("&manpath=");
1.58 schwarze 134: http_print(req->q.manpath);
1.53 schwarze 135: }
136: if (NULL != req->q.sec) {
137: printf("&sec=");
138: http_print(req->q.sec);
139: }
140: if (NULL != req->q.arch) {
141: printf("&arch=");
142: http_print(req->q.arch);
143: }
144: if (NULL != req->q.expr) {
145: printf("&expr=");
146: http_print(req->q.expr ? req->q.expr : "");
147: }
1.36 kristaps 148: }
149:
150: static void
151: html_printquery(const struct req *req)
152: {
153:
1.58 schwarze 154: if (NULL != req->q.manpath) {
1.53 schwarze 155: printf("&manpath=");
1.58 schwarze 156: html_print(req->q.manpath);
1.53 schwarze 157: }
158: if (NULL != req->q.sec) {
159: printf("&sec=");
160: html_print(req->q.sec);
161: }
162: if (NULL != req->q.arch) {
163: printf("&arch=");
164: html_print(req->q.arch);
165: }
166: if (NULL != req->q.expr) {
167: printf("&expr=");
168: html_print(req->q.expr ? req->q.expr : "");
169: }
1.36 kristaps 170: }
171:
172: static void
173: http_print(const char *p)
174: {
175:
176: if (NULL == p)
177: return;
178: while ('\0' != *p)
179: http_putchar(*p++);
180: }
1.10 kristaps 181:
1.6 kristaps 182: /*
1.20 kristaps 183: * Call through to html_putchar().
184: * Accepts NULL strings.
1.6 kristaps 185: */
1.1 kristaps 186: static void
1.6 kristaps 187: html_print(const char *p)
1.1 kristaps 188: {
1.6 kristaps 189:
190: if (NULL == p)
191: return;
1.1 kristaps 192: while ('\0' != *p)
1.10 kristaps 193: html_putchar(*p++);
1.1 kristaps 194: }
195:
196: /*
197: * Parse out key-value pairs from an HTTP request variable.
1.6 kristaps 198: * This can be either a cookie or a POST/GET string, although man.cgi
199: * uses only GET for simplicity.
1.1 kristaps 200: */
201: static void
1.26 kristaps 202: http_parse(struct req *req, char *p)
1.1 kristaps 203: {
1.52 schwarze 204: char *key, *val;
205: int legacy;
1.1 kristaps 206:
1.26 kristaps 207: memset(&req->q, 0, sizeof(struct query));
1.58 schwarze 208: req->q.manpath = req->p[0];
1.24 kristaps 209:
210: legacy = -1;
1.36 kristaps 211: while ('\0' != *p) {
1.1 kristaps 212: key = p;
213: val = NULL;
214:
1.36 kristaps 215: p += (int)strcspn(p, ";&");
216: if ('\0' != *p)
1.1 kristaps 217: *p++ = '\0';
1.36 kristaps 218: if (NULL != (val = strchr(key, '=')))
219: *val++ = '\0';
1.1 kristaps 220:
1.36 kristaps 221: if ('\0' == *key || NULL == val || '\0' == *val)
1.1 kristaps 222: continue;
223:
224: /* Just abort handling. */
225:
1.24 kristaps 226: if ( ! http_decode(key))
227: break;
1.36 kristaps 228: if (NULL != val && ! http_decode(val))
1.24 kristaps 229: break;
230:
231: if (0 == strcmp(key, "expr"))
1.26 kristaps 232: req->q.expr = val;
1.24 kristaps 233: else if (0 == strcmp(key, "query"))
1.26 kristaps 234: req->q.expr = val;
1.24 kristaps 235: else if (0 == strcmp(key, "sec"))
1.26 kristaps 236: req->q.sec = val;
1.24 kristaps 237: else if (0 == strcmp(key, "sektion"))
1.26 kristaps 238: req->q.sec = val;
1.24 kristaps 239: else if (0 == strcmp(key, "arch"))
1.26 kristaps 240: req->q.arch = val;
241: else if (0 == strcmp(key, "manpath"))
1.58 schwarze 242: req->q.manpath = val;
1.24 kristaps 243: else if (0 == strcmp(key, "apropos"))
244: legacy = 0 == strcmp(val, "0");
245: }
1.1 kristaps 246:
1.24 kristaps 247: /* Test for old man.cgi compatibility mode. */
1.1 kristaps 248:
1.40 kristaps 249: req->q.legacy = legacy > 0;
1.24 kristaps 250:
251: /*
252: * Section "0" means no section when in legacy mode.
253: * For some man.cgi scripts, "default" arch is none.
254: */
255:
1.26 kristaps 256: if (req->q.legacy && NULL != req->q.sec)
257: if (0 == strcmp(req->q.sec, "0"))
258: req->q.sec = NULL;
259: if (req->q.legacy && NULL != req->q.arch)
260: if (0 == strcmp(req->q.arch, "default"))
261: req->q.arch = NULL;
1.1 kristaps 262: }
263:
1.36 kristaps 264: static void
265: http_putchar(char c)
266: {
267:
268: if (isalnum((unsigned char)c)) {
269: putchar((unsigned char)c);
270: return;
271: } else if (' ' == c) {
272: putchar('+');
273: return;
274: }
275: printf("%%%.2x", c);
276: }
277:
1.1 kristaps 278: /*
1.6 kristaps 279: * HTTP-decode a string. The standard explanation is that this turns
280: * "%4e+foo" into "n foo" in the regular way. This is done in-place
281: * over the allocated string.
1.1 kristaps 282: */
283: static int
1.24 kristaps 284: http_decode(char *p)
1.1 kristaps 285: {
286: char hex[3];
287: int c;
288:
289: hex[2] = '\0';
290:
291: for ( ; '\0' != *p; p++) {
292: if ('%' == *p) {
293: if ('\0' == (hex[0] = *(p + 1)))
294: return(0);
295: if ('\0' == (hex[1] = *(p + 2)))
296: return(0);
297: if (1 != sscanf(hex, "%x", &c))
298: return(0);
299: if ('\0' == c)
300: return(0);
301:
302: *p = (char)c;
303: memmove(p + 1, p + 3, strlen(p + 3) + 1);
304: } else
305: *p = '+' == *p ? ' ' : *p;
306: }
307:
308: *p = '\0';
309: return(1);
310: }
311:
1.6 kristaps 312: static void
313: resp_begin_http(int code, const char *msg)
314: {
315:
316: if (200 != code)
317: printf("Status: %d %s\n", code, msg);
318:
1.20 kristaps 319: puts("Content-Type: text/html; charset=utf-8\n"
320: "Cache-Control: no-cache\n"
321: "Pragma: no-cache\n"
1.6 kristaps 322: "");
323:
324: fflush(stdout);
325: }
326:
327: static void
328: resp_begin_html(int code, const char *msg)
329: {
330:
331: resp_begin_http(code, msg);
332:
1.29 kristaps 333: printf("<!DOCTYPE HTML PUBLIC "
334: " \"-//W3C//DTD HTML 4.01//EN\""
335: " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
336: "<HTML>\n"
337: "<HEAD>\n"
338: "<META HTTP-EQUIV=\"Content-Type\""
339: " CONTENT=\"text/html; charset=utf-8\">\n"
1.32 kristaps 340: "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
341: " TYPE=\"text/css\" media=\"all\">\n"
342: "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
1.29 kristaps 343: " TYPE=\"text/css\" media=\"all\">\n"
344: "<TITLE>System Manpage Reference</TITLE>\n"
345: "</HEAD>\n"
346: "<BODY>\n"
1.58 schwarze 347: "<!-- Begin page content. //-->\n",
348: cssdir, cssdir);
1.6 kristaps 349: }
350:
351: static void
352: resp_end_html(void)
353: {
354:
1.20 kristaps 355: puts("</BODY>\n"
356: "</HTML>");
1.6 kristaps 357: }
358:
359: static void
360: resp_searchform(const struct req *req)
361: {
1.27 kristaps 362: int i;
1.13 kristaps 363:
1.6 kristaps 364: puts("<!-- Begin search form. //-->");
1.32 kristaps 365: printf("<DIV ID=\"mancgi\">\n"
1.52 schwarze 366: "<FORM ACTION=\"%s/search\" METHOD=\"get\">\n"
1.29 kristaps 367: "<FIELDSET>\n"
1.16 kristaps 368: "<LEGEND>Search Parameters</LEGEND>\n"
1.40 kristaps 369: "<INPUT TYPE=\"submit\" "
370: " VALUE=\"Search\"> for manuals satisfying \n"
1.29 kristaps 371: "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
1.58 schwarze 372: scriptname);
1.24 kristaps 373: html_print(req->q.expr ? req->q.expr : "");
1.14 kristaps 374: printf("\">, section "
1.20 kristaps 375: "<INPUT TYPE=\"text\""
376: " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
1.24 kristaps 377: html_print(req->q.sec ? req->q.sec : "");
1.14 kristaps 378: printf("\">, arch "
1.20 kristaps 379: "<INPUT TYPE=\"text\""
380: " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
1.24 kristaps 381: html_print(req->q.arch ? req->q.arch : "");
1.27 kristaps 382: printf("\">");
383: if (req->psz > 1) {
384: puts(", <SELECT NAME=\"manpath\">");
385: for (i = 0; i < (int)req->psz; i++) {
1.52 schwarze 386: printf("<OPTION ");
1.58 schwarze 387: if (NULL == req->q.manpath ? 0 == i :
388: 0 == strcmp(req->q.manpath, req->p[i]))
1.52 schwarze 389: printf("SELECTED=\"selected\" ");
390: printf("VALUE=\"");
391: html_print(req->p[i]);
1.27 kristaps 392: printf("\">");
1.52 schwarze 393: html_print(req->p[i]);
1.27 kristaps 394: puts("</OPTION>");
395: }
396: puts("</SELECT>");
397: }
398: puts(".\n"
1.12 kristaps 399: "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
400: "</FIELDSET>\n"
1.32 kristaps 401: "</FORM>\n"
402: "</DIV>");
1.20 kristaps 403: puts("<!-- End search form. //-->");
1.6 kristaps 404: }
405:
406: static void
407: resp_index(const struct req *req)
408: {
409:
410: resp_begin_html(200, NULL);
411: resp_searchform(req);
412: resp_end_html();
413: }
414:
415: static void
1.59 ! schwarze 416: resp_noresult(const struct req *req, const char *msg)
! 417: {
! 418: resp_begin_html(200, NULL);
! 419: resp_searchform(req);
! 420: puts("<P>");
! 421: puts(msg);
! 422: puts("</P>");
! 423: resp_end_html();
! 424: }
! 425:
! 426: static void
! 427: resp_error_badrequest(const char *msg)
1.9 kristaps 428: {
429:
1.59 ! schwarze 430: resp_begin_html(400, "Bad Request");
! 431: puts("<H1>Bad Request</H1>\n"
! 432: "<P>\n");
! 433: puts(msg);
! 434: printf("Try again from the\n"
! 435: "<A HREF=\"%s\">main page</A>.\n"
1.58 schwarze 436: "</P>", scriptname);
1.9 kristaps 437: resp_end_html();
438: }
439:
440: static void
1.59 ! schwarze 441: resp_error_notfound(const char *page)
1.6 kristaps 442: {
443:
444: resp_begin_html(404, "Not Found");
1.10 kristaps 445: puts("<H1>Page Not Found</H1>\n"
446: "<P>\n"
1.20 kristaps 447: "The page you're looking for, ");
448: printf("<B>");
1.10 kristaps 449: html_print(page);
1.12 kristaps 450: printf("</B>,\n"
1.20 kristaps 451: "could not be found.\n"
452: "Try searching from the\n"
1.58 schwarze 453: "<A HREF=\"%s\">main page</A>.\n"
454: "</P>", scriptname);
1.6 kristaps 455: resp_end_html();
456: }
1.1 kristaps 457:
458: static void
1.59 ! schwarze 459: resp_error_internal(void)
1.7 kristaps 460: {
461: resp_begin_html(500, "Internal Server Error");
1.58 schwarze 462: puts("<P>Internal Server Error</P>");
1.7 kristaps 463: resp_end_html();
464: }
465:
466: static void
1.52 schwarze 467: resp_search(const struct req *req, struct manpage *r, size_t sz)
1.1 kristaps 468: {
1.52 schwarze 469: size_t i;
1.19 kristaps 470:
1.52 schwarze 471: if (1 == sz) {
1.6 kristaps 472: /*
473: * If we have just one result, then jump there now
474: * without any delay.
475: */
476: puts("Status: 303 See Other");
1.52 schwarze 477: printf("Location: http://%s%s/show/%s/%s?",
1.58 schwarze 478: httphost, scriptname, req->q.manpath, r[0].file);
1.36 kristaps 479: http_printquery(req);
480: puts("\n"
481: "Content-Type: text/html; charset=utf-8\n");
1.6 kristaps 482: return;
483: }
484:
1.59 ! schwarze 485: qsort(r, sz, sizeof(struct manpage), cmp);
! 486:
1.12 kristaps 487: resp_begin_html(200, NULL);
1.19 kristaps 488: resp_searchform(req);
1.33 kristaps 489: puts("<DIV CLASS=\"results\">");
490: puts("<TABLE>");
1.1 kristaps 491:
1.41 kristaps 492: for (i = 0; i < sz; i++) {
1.20 kristaps 493: printf("<TR>\n"
494: "<TD CLASS=\"title\">\n"
1.52 schwarze 495: "<A HREF=\"%s/show/%s/%s?",
1.58 schwarze 496: scriptname, req->q.manpath, r[i].file);
1.36 kristaps 497: html_printquery(req);
498: printf("\">");
1.52 schwarze 499: html_print(r[i].names);
500: printf("</A>\n"
1.20 kristaps 501: "</TD>\n"
502: "<TD CLASS=\"desc\">");
1.52 schwarze 503: html_print(r[i].output);
1.20 kristaps 504: puts("</TD>\n"
505: "</TR>");
1.1 kristaps 506: }
1.16 kristaps 507:
1.33 kristaps 508: puts("</TABLE>\n"
509: "</DIV>");
1.6 kristaps 510: resp_end_html();
511: }
512:
513: /* ARGSUSED */
514: static void
1.24 kristaps 515: pg_index(const struct req *req, char *path)
1.6 kristaps 516: {
517:
518: resp_index(req);
1.1 kristaps 519: }
520:
521: static void
1.32 kristaps 522: catman(const struct req *req, const char *file)
1.9 kristaps 523: {
1.10 kristaps 524: FILE *f;
525: size_t len;
526: int i;
527: char *p;
528: int italic, bold;
1.9 kristaps 529:
1.10 kristaps 530: if (NULL == (f = fopen(file, "r"))) {
1.59 ! schwarze 531: resp_error_badrequest(
! 532: "You specified an invalid manual file.");
1.9 kristaps 533: return;
534: }
535:
1.32 kristaps 536: resp_begin_html(200, NULL);
537: resp_searchform(req);
538: puts("<DIV CLASS=\"catman\">\n"
539: "<PRE>");
1.10 kristaps 540:
541: while (NULL != (p = fgetln(f, &len))) {
542: bold = italic = 0;
543: for (i = 0; i < (int)len - 1; i++) {
544: /*
545: * This means that the catpage is out of state.
546: * Ignore it and keep going (although the
547: * catpage is bogus).
548: */
549:
550: if ('\b' == p[i] || '\n' == p[i])
551: continue;
552:
553: /*
554: * Print a regular character.
555: * Close out any bold/italic scopes.
556: * If we're in back-space mode, make sure we'll
557: * have something to enter when we backspace.
558: */
559:
560: if ('\b' != p[i + 1]) {
561: if (italic)
562: printf("</I>");
563: if (bold)
564: printf("</B>");
565: italic = bold = 0;
566: html_putchar(p[i]);
567: continue;
568: } else if (i + 2 >= (int)len)
569: continue;
570:
571: /* Italic mode. */
572:
573: if ('_' == p[i]) {
574: if (bold)
575: printf("</B>");
576: if ( ! italic)
577: printf("<I>");
578: bold = 0;
579: italic = 1;
580: i += 2;
581: html_putchar(p[i]);
582: continue;
583: }
584:
585: /*
586: * Handle funny behaviour troff-isms.
587: * These grok'd from the original man2html.c.
588: */
589:
590: if (('+' == p[i] && 'o' == p[i + 2]) ||
591: ('o' == p[i] && '+' == p[i + 2]) ||
592: ('|' == p[i] && '=' == p[i + 2]) ||
593: ('=' == p[i] && '|' == p[i + 2]) ||
594: ('*' == p[i] && '=' == p[i + 2]) ||
595: ('=' == p[i] && '*' == p[i + 2]) ||
596: ('*' == p[i] && '|' == p[i + 2]) ||
597: ('|' == p[i] && '*' == p[i + 2])) {
598: if (italic)
599: printf("</I>");
600: if (bold)
601: printf("</B>");
602: italic = bold = 0;
603: putchar('*');
604: i += 2;
605: continue;
606: } else if (('|' == p[i] && '-' == p[i + 2]) ||
607: ('-' == p[i] && '|' == p[i + 1]) ||
608: ('+' == p[i] && '-' == p[i + 1]) ||
609: ('-' == p[i] && '+' == p[i + 1]) ||
610: ('+' == p[i] && '|' == p[i + 1]) ||
611: ('|' == p[i] && '+' == p[i + 1])) {
612: if (italic)
613: printf("</I>");
614: if (bold)
615: printf("</B>");
616: italic = bold = 0;
617: putchar('+');
618: i += 2;
619: continue;
620: }
621:
622: /* Bold mode. */
623:
624: if (italic)
625: printf("</I>");
626: if ( ! bold)
627: printf("<B>");
628: bold = 1;
629: italic = 0;
630: i += 2;
631: html_putchar(p[i]);
632: }
633:
634: /*
635: * Clean up the last character.
636: * We can get to a newline; don't print that.
637: */
1.9 kristaps 638:
1.10 kristaps 639: if (italic)
640: printf("</I>");
641: if (bold)
642: printf("</B>");
1.9 kristaps 643:
1.10 kristaps 644: if (i == (int)len - 1 && '\n' != p[i])
645: html_putchar(p[i]);
1.9 kristaps 646:
1.10 kristaps 647: putchar('\n');
648: }
649:
650: puts("</PRE>\n"
1.32 kristaps 651: "</DIV>\n"
1.10 kristaps 652: "</BODY>\n"
653: "</HTML>");
654:
655: fclose(f);
1.9 kristaps 656: }
657:
658: static void
1.32 kristaps 659: format(const struct req *req, const char *file)
1.7 kristaps 660: {
1.8 kristaps 661: struct mparse *mp;
662: int fd;
663: struct mdoc *mdoc;
664: struct man *man;
665: void *vp;
666: enum mandoclevel rc;
1.45 schwarze 667: char opts[PATH_MAX + 128];
1.7 kristaps 668:
1.8 kristaps 669: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.59 ! schwarze 670: resp_error_badrequest(
! 671: "You specified an invalid manual file.");
1.7 kristaps 672: return;
673: }
674:
1.56 schwarze 675: mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,
1.58 schwarze 676: req->q.manpath);
1.8 kristaps 677: rc = mparse_readfd(mp, fd, file);
678: close(fd);
1.7 kristaps 679:
1.8 kristaps 680: if (rc >= MANDOCLEVEL_FATAL) {
1.59 ! schwarze 681: fprintf(stderr, "fatal mandoc error: %s/%s\n",
! 682: req->q.manpath, file);
! 683: resp_error_internal();
1.7 kristaps 684: return;
685: }
686:
1.52 schwarze 687: snprintf(opts, sizeof(opts),
688: "fragment,man=%s/search?sec=%%S&expr=Nm~^%%N$",
1.58 schwarze 689: scriptname);
1.10 kristaps 690:
1.49 schwarze 691: mparse_result(mp, &mdoc, &man, NULL);
1.32 kristaps 692: if (NULL == man && NULL == mdoc) {
1.59 ! schwarze 693: fprintf(stderr, "fatal mandoc error: %s/%s\n",
! 694: req->q.manpath, file);
! 695: resp_error_internal();
1.32 kristaps 696: mparse_free(mp);
697: return;
698: }
699:
700: resp_begin_html(200, NULL);
701: resp_searchform(req);
702:
1.10 kristaps 703: vp = html_alloc(opts);
1.7 kristaps 704:
1.32 kristaps 705: if (NULL != mdoc)
1.8 kristaps 706: html_mdoc(vp, mdoc);
1.32 kristaps 707: else
1.8 kristaps 708: html_man(vp, man);
1.32 kristaps 709:
710: puts("</BODY>\n"
711: "</HTML>");
1.7 kristaps 712:
1.8 kristaps 713: html_free(vp);
714: mparse_free(mp);
1.7 kristaps 715: }
716:
717: static void
1.24 kristaps 718: pg_show(const struct req *req, char *path)
1.1 kristaps 719: {
1.6 kristaps 720: char *sub;
1.25 kristaps 721:
722: if (NULL == path || NULL == (sub = strchr(path, '/'))) {
1.59 ! schwarze 723: resp_error_badrequest(
! 724: "You did not specify a page to show.");
1.25 kristaps 725: return;
726: }
727: *sub++ = '\0';
1.6 kristaps 728:
1.24 kristaps 729: /*
1.58 schwarze 730: * Begin by chdir()ing into the manpath.
1.24 kristaps 731: * This way we can pick up the database files, which are
732: * relative to the manpath root.
733: */
734:
1.52 schwarze 735: if (-1 == chdir(path)) {
1.59 ! schwarze 736: resp_error_badrequest(
! 737: "You specified an invalid manpath.");
1.24 kristaps 738: return;
739: }
740:
1.52 schwarze 741: if ('c' == *sub)
742: catman(req, sub);
743: else
744: format(req, sub);
1.6 kristaps 745: }
746:
747: static void
1.24 kristaps 748: pg_search(const struct req *req, char *path)
1.6 kristaps 749: {
1.52 schwarze 750: struct mansearch search;
751: struct manpaths paths;
752: struct manpage *res;
753: char **cp;
754: const char *ep, *start;
755: size_t ressz;
756: int i, sz;
1.6 kristaps 757:
758: /*
1.24 kristaps 759: * Begin by chdir()ing into the root of the manpath.
760: * This way we can pick up the database files, which are
761: * relative to the manpath root.
762: */
763:
1.58 schwarze 764: if (-1 == (chdir(req->q.manpath))) {
1.59 ! schwarze 765: resp_error_badrequest(
! 766: "You specified an invalid manpath.");
1.24 kristaps 767: return;
768: }
769:
1.52 schwarze 770: search.arch = req->q.arch;
771: search.sec = req->q.sec;
772: search.deftype = TYPE_Nm | TYPE_Nd;
773: search.flags = 0;
774:
775: paths.sz = 1;
776: paths.paths = mandoc_malloc(sizeof(char *));
777: paths.paths[0] = mandoc_strdup(".");
1.24 kristaps 778:
779: /*
780: * Poor man's tokenisation: just break apart by spaces.
1.6 kristaps 781: * Yes, this is half-ass. But it works for now.
782: */
783:
1.52 schwarze 784: ep = req->q.expr;
1.6 kristaps 785: while (ep && isspace((unsigned char)*ep))
786: ep++;
787:
1.52 schwarze 788: sz = 0;
789: cp = NULL;
1.6 kristaps 790: while (ep && '\0' != *ep) {
1.51 schwarze 791: cp = mandoc_reallocarray(cp, sz + 1, sizeof(char *));
1.6 kristaps 792: start = ep;
793: while ('\0' != *ep && ! isspace((unsigned char)*ep))
794: ep++;
795: cp[sz] = mandoc_malloc((ep - start) + 1);
796: memcpy(cp[sz], start, ep - start);
797: cp[sz++][ep - start] = '\0';
798: while (isspace((unsigned char)*ep))
799: ep++;
800: }
801:
1.59 ! schwarze 802: if (0 == mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
! 803: resp_noresult(req, "You entered an invalid query.");
! 804: else if (0 == ressz)
! 805: resp_noresult(req, "No results found.");
! 806: else
1.52 schwarze 807: resp_search(req, res, ressz);
1.6 kristaps 808:
809: for (i = 0; i < sz; i++)
810: free(cp[i]);
1.52 schwarze 811: free(cp);
812:
813: for (i = 0; i < (int)ressz; i++) {
814: free(res[i].file);
815: free(res[i].names);
816: free(res[i].output);
817: }
818: free(res);
1.6 kristaps 819:
1.52 schwarze 820: free(paths.paths[0]);
821: free(paths.paths);
1.1 kristaps 822: }
823:
824: int
825: main(void)
826: {
827: int i;
828: struct req req;
1.58 schwarze 829: char *querystring, *path, *subpath;
1.6 kristaps 830:
1.24 kristaps 831: /* Scan our run-time environment. */
1.6 kristaps 832:
1.58 schwarze 833: if (NULL == (mandir = getenv("MAN_DIR")))
834: mandir = "/man";
1.29 kristaps 835:
1.58 schwarze 836: if (NULL == (scriptname = getenv("SCRIPT_NAME")))
837: scriptname = "";
1.6 kristaps 838:
1.58 schwarze 839: if (NULL == (cssdir = getenv("CSS_DIR")))
840: cssdir = "";
1.7 kristaps 841:
1.58 schwarze 842: if (NULL == (httphost = getenv("HTTP_HOST")))
843: httphost = "localhost";
1.24 kristaps 844:
845: /*
1.58 schwarze 846: * First we change directory into the mandir so that
1.24 kristaps 847: * subsequent scanning for manpath directories is rooted
848: * relative to the same position.
849: */
850:
1.58 schwarze 851: if (-1 == chdir(mandir)) {
852: fprintf(stderr, "MAN_DIR: %s: %s\n",
853: mandir, strerror(errno));
1.59 ! schwarze 854: resp_error_internal();
1.24 kristaps 855: return(EXIT_FAILURE);
856: }
857:
858: memset(&req, 0, sizeof(struct req));
1.54 schwarze 859: pathgen(&req);
1.1 kristaps 860:
1.24 kristaps 861: /* Next parse out the query string. */
1.1 kristaps 862:
1.58 schwarze 863: if (NULL != (querystring = getenv("QUERY_STRING")))
864: http_parse(&req, querystring);
1.1 kristaps 865:
1.24 kristaps 866: /*
867: * Now juggle paths to extract information.
868: * We want to extract our filetype (the file suffix), the
869: * initial path component, then the trailing component(s).
870: * Start with leading subpath component.
871: */
1.1 kristaps 872:
1.6 kristaps 873: subpath = path = NULL;
1.1 kristaps 874: req.page = PAGE__MAX;
875:
876: if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
877: req.page = PAGE_INDEX;
1.6 kristaps 878:
1.1 kristaps 879: if (NULL != path && '/' == *path && '\0' == *++path)
880: req.page = PAGE_INDEX;
881:
1.6 kristaps 882: /* Resolve subpath component. */
1.1 kristaps 883:
884: if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6 kristaps 885: *subpath++ = '\0';
1.1 kristaps 886:
1.6 kristaps 887: /* Map path into one we recognise. */
1.1 kristaps 888:
889: if (NULL != path && '\0' != *path)
890: for (i = 0; i < (int)PAGE__MAX; i++)
891: if (0 == strcmp(pages[i], path)) {
892: req.page = (enum page)i;
893: break;
894: }
895:
1.6 kristaps 896: /* Route pages. */
897:
1.1 kristaps 898: switch (req.page) {
899: case (PAGE_INDEX):
1.24 kristaps 900: pg_index(&req, subpath);
1.1 kristaps 901: break;
902: case (PAGE_SEARCH):
1.24 kristaps 903: pg_search(&req, subpath);
1.6 kristaps 904: break;
905: case (PAGE_SHOW):
1.24 kristaps 906: pg_show(&req, subpath);
1.1 kristaps 907: break;
908: default:
1.59 ! schwarze 909: resp_error_notfound(path);
1.1 kristaps 910: break;
911: }
912:
1.52 schwarze 913: for (i = 0; i < (int)req.psz; i++)
914: free(req.p[i]);
1.24 kristaps 915: free(req.p);
1.1 kristaps 916: return(EXIT_SUCCESS);
917: }
1.15 kristaps 918:
919: static int
920: cmp(const void *p1, const void *p2)
921: {
922:
1.52 schwarze 923: return(strcasecmp(((const struct manpage *)p1)->names,
924: ((const struct manpage *)p2)->names));
1.24 kristaps 925: }
926:
927: /*
928: * Scan for indexable paths.
929: */
930: static void
1.54 schwarze 931: pathgen(struct req *req)
1.24 kristaps 932: {
1.54 schwarze 933: FILE *fp;
934: char *dp;
935: size_t dpsz;
936:
937: if (NULL == (fp = fopen("manpath.conf", "r")))
938: return;
1.24 kristaps 939:
1.54 schwarze 940: while (NULL != (dp = fgetln(fp, &dpsz))) {
1.55 schwarze 941: if ('\n' == dp[dpsz - 1])
942: dpsz--;
1.54 schwarze 943: req->p = mandoc_realloc(req->p,
944: (req->psz + 1) * sizeof(char *));
945: req->p[req->psz++] = mandoc_strndup(dp, dpsz);
1.24 kristaps 946: }
947: }
CVSweb