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