Annotation of mandoc/cgi.c, Revision 1.64
1.64 ! schwarze 1: /* $Id: cgi.c,v 1.63 2014/07/11 22:27:35 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=");
1.63 schwarze 168: html_print(req->q.expr);
1.53 schwarze 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];
1.63 schwarze 287: char *q;
1.1 kristaps 288: int c;
289:
290: hex[2] = '\0';
291:
1.63 schwarze 292: q = p;
293: for ( ; '\0' != *p; p++, q++) {
1.1 kristaps 294: if ('%' == *p) {
295: if ('\0' == (hex[0] = *(p + 1)))
296: return(0);
297: if ('\0' == (hex[1] = *(p + 2)))
298: return(0);
299: if (1 != sscanf(hex, "%x", &c))
300: return(0);
301: if ('\0' == c)
302: return(0);
303:
1.63 schwarze 304: *q = (char)c;
305: p += 2;
1.1 kristaps 306: } else
1.63 schwarze 307: *q = '+' == *p ? ' ' : *p;
1.1 kristaps 308: }
309:
1.63 schwarze 310: *q = '\0';
1.1 kristaps 311: return(1);
312: }
313:
1.6 kristaps 314: static void
315: resp_begin_http(int code, const char *msg)
316: {
317:
318: if (200 != code)
1.62 schwarze 319: printf("Status: %d %s\r\n", code, msg);
1.6 kristaps 320:
1.62 schwarze 321: printf("Content-Type: text/html; charset=utf-8\r\n"
322: "Cache-Control: no-cache\r\n"
323: "Pragma: no-cache\r\n"
324: "\r\n");
1.6 kristaps 325:
326: fflush(stdout);
327: }
328:
329: static void
330: resp_begin_html(int code, const char *msg)
331: {
332:
333: resp_begin_http(code, msg);
334:
1.29 kristaps 335: printf("<!DOCTYPE HTML PUBLIC "
336: " \"-//W3C//DTD HTML 4.01//EN\""
337: " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
338: "<HTML>\n"
339: "<HEAD>\n"
340: "<META HTTP-EQUIV=\"Content-Type\""
341: " CONTENT=\"text/html; charset=utf-8\">\n"
1.32 kristaps 342: "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
343: " TYPE=\"text/css\" media=\"all\">\n"
344: "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
1.29 kristaps 345: " TYPE=\"text/css\" media=\"all\">\n"
346: "<TITLE>System Manpage Reference</TITLE>\n"
347: "</HEAD>\n"
348: "<BODY>\n"
1.58 schwarze 349: "<!-- Begin page content. //-->\n",
350: cssdir, cssdir);
1.6 kristaps 351: }
352:
353: static void
354: resp_end_html(void)
355: {
356:
1.20 kristaps 357: puts("</BODY>\n"
358: "</HTML>");
1.6 kristaps 359: }
360:
361: static void
362: resp_searchform(const struct req *req)
363: {
1.27 kristaps 364: int i;
1.13 kristaps 365:
1.6 kristaps 366: puts("<!-- Begin search form. //-->");
1.32 kristaps 367: printf("<DIV ID=\"mancgi\">\n"
1.52 schwarze 368: "<FORM ACTION=\"%s/search\" METHOD=\"get\">\n"
1.29 kristaps 369: "<FIELDSET>\n"
1.16 kristaps 370: "<LEGEND>Search Parameters</LEGEND>\n"
1.40 kristaps 371: "<INPUT TYPE=\"submit\" "
1.61 schwarze 372: " VALUE=\"Search\"> for manuals matching \n"
1.29 kristaps 373: "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
1.58 schwarze 374: scriptname);
1.24 kristaps 375: html_print(req->q.expr ? req->q.expr : "");
1.14 kristaps 376: printf("\">, section "
1.20 kristaps 377: "<INPUT TYPE=\"text\""
378: " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
1.24 kristaps 379: html_print(req->q.sec ? req->q.sec : "");
1.14 kristaps 380: printf("\">, arch "
1.20 kristaps 381: "<INPUT TYPE=\"text\""
382: " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
1.24 kristaps 383: html_print(req->q.arch ? req->q.arch : "");
1.27 kristaps 384: printf("\">");
385: if (req->psz > 1) {
1.60 schwarze 386: puts(", in <SELECT NAME=\"manpath\">");
1.27 kristaps 387: for (i = 0; i < (int)req->psz; i++) {
1.52 schwarze 388: printf("<OPTION ");
1.58 schwarze 389: if (NULL == req->q.manpath ? 0 == i :
390: 0 == strcmp(req->q.manpath, req->p[i]))
1.52 schwarze 391: printf("SELECTED=\"selected\" ");
392: printf("VALUE=\"");
393: html_print(req->p[i]);
1.27 kristaps 394: printf("\">");
1.52 schwarze 395: html_print(req->p[i]);
1.27 kristaps 396: puts("</OPTION>");
397: }
398: puts("</SELECT>");
399: }
1.60 schwarze 400: puts("—\n"
1.12 kristaps 401: "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
402: "</FIELDSET>\n"
1.32 kristaps 403: "</FORM>\n"
404: "</DIV>");
1.20 kristaps 405: puts("<!-- End search form. //-->");
1.6 kristaps 406: }
407:
408: static void
409: resp_index(const struct req *req)
410: {
411:
412: resp_begin_html(200, NULL);
1.60 schwarze 413: puts("<H1>\n"
414: "Online manuals with "
415: "<A HREF=\"http://mdocml.bsd.lv/\">mandoc</A>\n"
416: "</H1>");
1.6 kristaps 417: resp_searchform(req);
1.64 ! schwarze 418: printf("<P>\n"
! 419: "This web interface is documented in the "
! 420: "<A HREF=\"%s/search?expr=Nm~^man\\.cgi$&sec=8\">"
! 421: "man.cgi</A> manual, and the "
! 422: "<A HREF=\"%s/search?expr=Nm~^apropos$&sec=1\">"
! 423: "apropos</A> manual explains the query syntax.\n"
! 424: "</P>\n",
! 425: scriptname, scriptname);
1.6 kristaps 426: resp_end_html();
427: }
428:
429: static void
1.59 schwarze 430: resp_noresult(const struct req *req, const char *msg)
431: {
432: resp_begin_html(200, NULL);
433: resp_searchform(req);
434: puts("<P>");
435: puts(msg);
436: puts("</P>");
437: resp_end_html();
438: }
439:
440: static void
441: resp_error_badrequest(const char *msg)
1.9 kristaps 442: {
443:
1.59 schwarze 444: resp_begin_html(400, "Bad Request");
445: puts("<H1>Bad Request</H1>\n"
446: "<P>\n");
447: puts(msg);
448: printf("Try again from the\n"
449: "<A HREF=\"%s\">main page</A>.\n"
1.58 schwarze 450: "</P>", scriptname);
1.9 kristaps 451: resp_end_html();
452: }
453:
454: static void
1.59 schwarze 455: resp_error_notfound(const char *page)
1.6 kristaps 456: {
457:
458: resp_begin_html(404, "Not Found");
1.10 kristaps 459: puts("<H1>Page Not Found</H1>\n"
460: "<P>\n"
1.20 kristaps 461: "The page you're looking for, ");
462: printf("<B>");
1.10 kristaps 463: html_print(page);
1.12 kristaps 464: printf("</B>,\n"
1.20 kristaps 465: "could not be found.\n"
466: "Try searching from the\n"
1.58 schwarze 467: "<A HREF=\"%s\">main page</A>.\n"
468: "</P>", scriptname);
1.6 kristaps 469: resp_end_html();
470: }
1.1 kristaps 471:
472: static void
1.59 schwarze 473: resp_error_internal(void)
1.7 kristaps 474: {
475: resp_begin_html(500, "Internal Server Error");
1.58 schwarze 476: puts("<P>Internal Server Error</P>");
1.7 kristaps 477: resp_end_html();
478: }
479:
480: static void
1.52 schwarze 481: resp_search(const struct req *req, struct manpage *r, size_t sz)
1.1 kristaps 482: {
1.52 schwarze 483: size_t i;
1.19 kristaps 484:
1.52 schwarze 485: if (1 == sz) {
1.6 kristaps 486: /*
487: * If we have just one result, then jump there now
488: * without any delay.
489: */
1.62 schwarze 490: printf("Status: 303 See Other\r\n");
1.52 schwarze 491: printf("Location: http://%s%s/show/%s/%s?",
1.58 schwarze 492: httphost, scriptname, req->q.manpath, r[0].file);
1.36 kristaps 493: http_printquery(req);
1.62 schwarze 494: printf("\r\n"
495: "Content-Type: text/html; charset=utf-8\r\n"
496: "\r\n");
1.6 kristaps 497: return;
498: }
499:
1.59 schwarze 500: qsort(r, sz, sizeof(struct manpage), cmp);
501:
1.12 kristaps 502: resp_begin_html(200, NULL);
1.19 kristaps 503: resp_searchform(req);
1.33 kristaps 504: puts("<DIV CLASS=\"results\">");
505: puts("<TABLE>");
1.1 kristaps 506:
1.41 kristaps 507: for (i = 0; i < sz; i++) {
1.20 kristaps 508: printf("<TR>\n"
509: "<TD CLASS=\"title\">\n"
1.52 schwarze 510: "<A HREF=\"%s/show/%s/%s?",
1.58 schwarze 511: scriptname, req->q.manpath, r[i].file);
1.36 kristaps 512: html_printquery(req);
513: printf("\">");
1.52 schwarze 514: html_print(r[i].names);
515: printf("</A>\n"
1.20 kristaps 516: "</TD>\n"
517: "<TD CLASS=\"desc\">");
1.52 schwarze 518: html_print(r[i].output);
1.20 kristaps 519: puts("</TD>\n"
520: "</TR>");
1.1 kristaps 521: }
1.16 kristaps 522:
1.33 kristaps 523: puts("</TABLE>\n"
524: "</DIV>");
1.6 kristaps 525: resp_end_html();
526: }
527:
528: /* ARGSUSED */
529: static void
1.24 kristaps 530: pg_index(const struct req *req, char *path)
1.6 kristaps 531: {
532:
533: resp_index(req);
1.1 kristaps 534: }
535:
536: static void
1.32 kristaps 537: catman(const struct req *req, const char *file)
1.9 kristaps 538: {
1.10 kristaps 539: FILE *f;
540: size_t len;
541: int i;
542: char *p;
543: int italic, bold;
1.9 kristaps 544:
1.10 kristaps 545: if (NULL == (f = fopen(file, "r"))) {
1.59 schwarze 546: resp_error_badrequest(
547: "You specified an invalid manual file.");
1.9 kristaps 548: return;
549: }
550:
1.32 kristaps 551: resp_begin_html(200, NULL);
552: resp_searchform(req);
553: puts("<DIV CLASS=\"catman\">\n"
554: "<PRE>");
1.10 kristaps 555:
556: while (NULL != (p = fgetln(f, &len))) {
557: bold = italic = 0;
558: for (i = 0; i < (int)len - 1; i++) {
559: /*
560: * This means that the catpage is out of state.
561: * Ignore it and keep going (although the
562: * catpage is bogus).
563: */
564:
565: if ('\b' == p[i] || '\n' == p[i])
566: continue;
567:
568: /*
569: * Print a regular character.
570: * Close out any bold/italic scopes.
571: * If we're in back-space mode, make sure we'll
572: * have something to enter when we backspace.
573: */
574:
575: if ('\b' != p[i + 1]) {
576: if (italic)
577: printf("</I>");
578: if (bold)
579: printf("</B>");
580: italic = bold = 0;
581: html_putchar(p[i]);
582: continue;
583: } else if (i + 2 >= (int)len)
584: continue;
585:
586: /* Italic mode. */
587:
588: if ('_' == p[i]) {
589: if (bold)
590: printf("</B>");
591: if ( ! italic)
592: printf("<I>");
593: bold = 0;
594: italic = 1;
595: i += 2;
596: html_putchar(p[i]);
597: continue;
598: }
599:
600: /*
601: * Handle funny behaviour troff-isms.
602: * These grok'd from the original man2html.c.
603: */
604:
605: if (('+' == p[i] && 'o' == p[i + 2]) ||
606: ('o' == p[i] && '+' == p[i + 2]) ||
607: ('|' == p[i] && '=' == p[i + 2]) ||
608: ('=' == p[i] && '|' == p[i + 2]) ||
609: ('*' == p[i] && '=' == p[i + 2]) ||
610: ('=' == p[i] && '*' == p[i + 2]) ||
611: ('*' == p[i] && '|' == p[i + 2]) ||
612: ('|' == p[i] && '*' == p[i + 2])) {
613: if (italic)
614: printf("</I>");
615: if (bold)
616: printf("</B>");
617: italic = bold = 0;
618: putchar('*');
619: i += 2;
620: continue;
621: } else if (('|' == p[i] && '-' == p[i + 2]) ||
622: ('-' == p[i] && '|' == p[i + 1]) ||
623: ('+' == p[i] && '-' == p[i + 1]) ||
624: ('-' == p[i] && '+' == p[i + 1]) ||
625: ('+' == p[i] && '|' == p[i + 1]) ||
626: ('|' == p[i] && '+' == p[i + 1])) {
627: if (italic)
628: printf("</I>");
629: if (bold)
630: printf("</B>");
631: italic = bold = 0;
632: putchar('+');
633: i += 2;
634: continue;
635: }
636:
637: /* Bold mode. */
638:
639: if (italic)
640: printf("</I>");
641: if ( ! bold)
642: printf("<B>");
643: bold = 1;
644: italic = 0;
645: i += 2;
646: html_putchar(p[i]);
647: }
648:
649: /*
650: * Clean up the last character.
651: * We can get to a newline; don't print that.
652: */
1.9 kristaps 653:
1.10 kristaps 654: if (italic)
655: printf("</I>");
656: if (bold)
657: printf("</B>");
1.9 kristaps 658:
1.10 kristaps 659: if (i == (int)len - 1 && '\n' != p[i])
660: html_putchar(p[i]);
1.9 kristaps 661:
1.10 kristaps 662: putchar('\n');
663: }
664:
665: puts("</PRE>\n"
1.32 kristaps 666: "</DIV>\n"
1.10 kristaps 667: "</BODY>\n"
668: "</HTML>");
669:
670: fclose(f);
1.9 kristaps 671: }
672:
673: static void
1.32 kristaps 674: format(const struct req *req, const char *file)
1.7 kristaps 675: {
1.8 kristaps 676: struct mparse *mp;
677: int fd;
678: struct mdoc *mdoc;
679: struct man *man;
680: void *vp;
681: enum mandoclevel rc;
1.45 schwarze 682: char opts[PATH_MAX + 128];
1.7 kristaps 683:
1.8 kristaps 684: if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.59 schwarze 685: resp_error_badrequest(
686: "You specified an invalid manual file.");
1.7 kristaps 687: return;
688: }
689:
1.56 schwarze 690: mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,
1.58 schwarze 691: req->q.manpath);
1.8 kristaps 692: rc = mparse_readfd(mp, fd, file);
693: close(fd);
1.7 kristaps 694:
1.8 kristaps 695: if (rc >= MANDOCLEVEL_FATAL) {
1.59 schwarze 696: fprintf(stderr, "fatal mandoc error: %s/%s\n",
697: req->q.manpath, file);
698: resp_error_internal();
1.7 kristaps 699: return;
700: }
701:
1.52 schwarze 702: snprintf(opts, sizeof(opts),
703: "fragment,man=%s/search?sec=%%S&expr=Nm~^%%N$",
1.58 schwarze 704: scriptname);
1.10 kristaps 705:
1.49 schwarze 706: mparse_result(mp, &mdoc, &man, NULL);
1.32 kristaps 707: if (NULL == man && NULL == mdoc) {
1.59 schwarze 708: fprintf(stderr, "fatal mandoc error: %s/%s\n",
709: req->q.manpath, file);
710: resp_error_internal();
1.32 kristaps 711: mparse_free(mp);
712: return;
713: }
714:
715: resp_begin_html(200, NULL);
716: resp_searchform(req);
717:
1.10 kristaps 718: vp = html_alloc(opts);
1.7 kristaps 719:
1.32 kristaps 720: if (NULL != mdoc)
1.8 kristaps 721: html_mdoc(vp, mdoc);
1.32 kristaps 722: else
1.8 kristaps 723: html_man(vp, man);
1.32 kristaps 724:
725: puts("</BODY>\n"
726: "</HTML>");
1.7 kristaps 727:
1.8 kristaps 728: html_free(vp);
729: mparse_free(mp);
1.7 kristaps 730: }
731:
732: static void
1.24 kristaps 733: pg_show(const struct req *req, char *path)
1.1 kristaps 734: {
1.6 kristaps 735: char *sub;
1.25 kristaps 736:
737: if (NULL == path || NULL == (sub = strchr(path, '/'))) {
1.59 schwarze 738: resp_error_badrequest(
739: "You did not specify a page to show.");
1.25 kristaps 740: return;
741: }
742: *sub++ = '\0';
1.6 kristaps 743:
1.24 kristaps 744: /*
1.58 schwarze 745: * Begin by chdir()ing into the manpath.
1.24 kristaps 746: * This way we can pick up the database files, which are
747: * relative to the manpath root.
748: */
749:
1.52 schwarze 750: if (-1 == chdir(path)) {
1.59 schwarze 751: resp_error_badrequest(
752: "You specified an invalid manpath.");
1.24 kristaps 753: return;
754: }
755:
1.52 schwarze 756: if ('c' == *sub)
757: catman(req, sub);
758: else
759: format(req, sub);
1.6 kristaps 760: }
761:
762: static void
1.24 kristaps 763: pg_search(const struct req *req, char *path)
1.6 kristaps 764: {
1.52 schwarze 765: struct mansearch search;
766: struct manpaths paths;
767: struct manpage *res;
768: char **cp;
769: const char *ep, *start;
770: size_t ressz;
771: int i, sz;
1.6 kristaps 772:
773: /*
1.24 kristaps 774: * Begin by chdir()ing into the root of the manpath.
775: * This way we can pick up the database files, which are
776: * relative to the manpath root.
777: */
778:
1.58 schwarze 779: if (-1 == (chdir(req->q.manpath))) {
1.59 schwarze 780: resp_error_badrequest(
781: "You specified an invalid manpath.");
1.24 kristaps 782: return;
783: }
784:
1.52 schwarze 785: search.arch = req->q.arch;
786: search.sec = req->q.sec;
787: search.deftype = TYPE_Nm | TYPE_Nd;
788: search.flags = 0;
789:
790: paths.sz = 1;
791: paths.paths = mandoc_malloc(sizeof(char *));
792: paths.paths[0] = mandoc_strdup(".");
1.24 kristaps 793:
794: /*
795: * Poor man's tokenisation: just break apart by spaces.
1.6 kristaps 796: * Yes, this is half-ass. But it works for now.
797: */
798:
1.52 schwarze 799: ep = req->q.expr;
1.6 kristaps 800: while (ep && isspace((unsigned char)*ep))
801: ep++;
802:
1.52 schwarze 803: sz = 0;
804: cp = NULL;
1.6 kristaps 805: while (ep && '\0' != *ep) {
1.51 schwarze 806: cp = mandoc_reallocarray(cp, sz + 1, sizeof(char *));
1.6 kristaps 807: start = ep;
808: while ('\0' != *ep && ! isspace((unsigned char)*ep))
809: ep++;
810: cp[sz] = mandoc_malloc((ep - start) + 1);
811: memcpy(cp[sz], start, ep - start);
812: cp[sz++][ep - start] = '\0';
813: while (isspace((unsigned char)*ep))
814: ep++;
815: }
816:
1.59 schwarze 817: if (0 == mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
818: resp_noresult(req, "You entered an invalid query.");
819: else if (0 == ressz)
820: resp_noresult(req, "No results found.");
821: else
1.52 schwarze 822: resp_search(req, res, ressz);
1.6 kristaps 823:
824: for (i = 0; i < sz; i++)
825: free(cp[i]);
1.52 schwarze 826: free(cp);
827:
828: for (i = 0; i < (int)ressz; i++) {
829: free(res[i].file);
830: free(res[i].names);
831: free(res[i].output);
832: }
833: free(res);
1.6 kristaps 834:
1.52 schwarze 835: free(paths.paths[0]);
836: free(paths.paths);
1.1 kristaps 837: }
838:
839: int
840: main(void)
841: {
842: int i;
843: struct req req;
1.58 schwarze 844: char *querystring, *path, *subpath;
1.6 kristaps 845:
1.24 kristaps 846: /* Scan our run-time environment. */
1.6 kristaps 847:
1.58 schwarze 848: if (NULL == (mandir = getenv("MAN_DIR")))
849: mandir = "/man";
1.29 kristaps 850:
1.58 schwarze 851: if (NULL == (scriptname = getenv("SCRIPT_NAME")))
852: scriptname = "";
1.6 kristaps 853:
1.58 schwarze 854: if (NULL == (cssdir = getenv("CSS_DIR")))
855: cssdir = "";
1.7 kristaps 856:
1.58 schwarze 857: if (NULL == (httphost = getenv("HTTP_HOST")))
858: httphost = "localhost";
1.24 kristaps 859:
860: /*
1.58 schwarze 861: * First we change directory into the mandir so that
1.24 kristaps 862: * subsequent scanning for manpath directories is rooted
863: * relative to the same position.
864: */
865:
1.58 schwarze 866: if (-1 == chdir(mandir)) {
867: fprintf(stderr, "MAN_DIR: %s: %s\n",
868: mandir, strerror(errno));
1.59 schwarze 869: resp_error_internal();
1.24 kristaps 870: return(EXIT_FAILURE);
871: }
872:
873: memset(&req, 0, sizeof(struct req));
1.54 schwarze 874: pathgen(&req);
1.1 kristaps 875:
1.24 kristaps 876: /* Next parse out the query string. */
1.1 kristaps 877:
1.58 schwarze 878: if (NULL != (querystring = getenv("QUERY_STRING")))
879: http_parse(&req, querystring);
1.1 kristaps 880:
1.24 kristaps 881: /*
882: * Now juggle paths to extract information.
883: * We want to extract our filetype (the file suffix), the
884: * initial path component, then the trailing component(s).
885: * Start with leading subpath component.
886: */
1.1 kristaps 887:
1.6 kristaps 888: subpath = path = NULL;
1.1 kristaps 889: req.page = PAGE__MAX;
890:
891: if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
892: req.page = PAGE_INDEX;
1.6 kristaps 893:
1.1 kristaps 894: if (NULL != path && '/' == *path && '\0' == *++path)
895: req.page = PAGE_INDEX;
896:
1.6 kristaps 897: /* Resolve subpath component. */
1.1 kristaps 898:
899: if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6 kristaps 900: *subpath++ = '\0';
1.1 kristaps 901:
1.6 kristaps 902: /* Map path into one we recognise. */
1.1 kristaps 903:
904: if (NULL != path && '\0' != *path)
905: for (i = 0; i < (int)PAGE__MAX; i++)
906: if (0 == strcmp(pages[i], path)) {
907: req.page = (enum page)i;
908: break;
909: }
910:
1.6 kristaps 911: /* Route pages. */
912:
1.1 kristaps 913: switch (req.page) {
914: case (PAGE_INDEX):
1.24 kristaps 915: pg_index(&req, subpath);
1.1 kristaps 916: break;
917: case (PAGE_SEARCH):
1.24 kristaps 918: pg_search(&req, subpath);
1.6 kristaps 919: break;
920: case (PAGE_SHOW):
1.24 kristaps 921: pg_show(&req, subpath);
1.1 kristaps 922: break;
923: default:
1.59 schwarze 924: resp_error_notfound(path);
1.1 kristaps 925: break;
926: }
927:
1.52 schwarze 928: for (i = 0; i < (int)req.psz; i++)
929: free(req.p[i]);
1.24 kristaps 930: free(req.p);
1.1 kristaps 931: return(EXIT_SUCCESS);
932: }
1.15 kristaps 933:
934: static int
935: cmp(const void *p1, const void *p2)
936: {
937:
1.52 schwarze 938: return(strcasecmp(((const struct manpage *)p1)->names,
939: ((const struct manpage *)p2)->names));
1.24 kristaps 940: }
941:
942: /*
943: * Scan for indexable paths.
944: */
945: static void
1.54 schwarze 946: pathgen(struct req *req)
1.24 kristaps 947: {
1.54 schwarze 948: FILE *fp;
949: char *dp;
950: size_t dpsz;
951:
952: if (NULL == (fp = fopen("manpath.conf", "r")))
953: return;
1.24 kristaps 954:
1.54 schwarze 955: while (NULL != (dp = fgetln(fp, &dpsz))) {
1.55 schwarze 956: if ('\n' == dp[dpsz - 1])
957: dpsz--;
1.54 schwarze 958: req->p = mandoc_realloc(req->p,
959: (req->psz + 1) * sizeof(char *));
960: req->p[req->psz++] = mandoc_strndup(dp, dpsz);
1.24 kristaps 961: }
962: }
CVSweb