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