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