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