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