[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/cgi.c, Revision 1.16

1.16    ! schwarze    1: /*     $Id: cgi.c,v 1.15 2014/07/18 19:02:07 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.6       schwarze   70: static void             pg_show(const 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("&quote;");
                    124:                break;
                    125:        case ('&'):
                    126:                printf("&amp;");
                    127:                break;
                    128:        case ('>'):
                    129:                printf("&gt;");
                    130:                break;
                    131:        case ('<'):
                    132:                printf("&lt;");
                    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("&amp;manpath=");
                    170:                html_print(req->q.manpath);
                    171:        }
                    172:        if (NULL != req->q.sec) {
                    173:                printf("&amp;sec=");
                    174:                html_print(req->q.sec);
                    175:        }
                    176:        if (NULL != req->q.arch) {
                    177:                printf("&amp;arch=");
                    178:                html_print(req->q.arch);
                    179:        }
                    180:        if (NULL != req->q.expr) {
1.5       schwarze  181:                printf("&amp;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("&amp;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
        !           466: validate_filename(const char *file)
        !           467: {
        !           468:
        !           469:        if ('.' == file[0] && '/' == file[1])
        !           470:                file += 2;
        !           471:
        !           472:        return ( ! (strstr(file, "../") || strstr(file, "/..") ||
        !           473:            (strncmp(file, "man", 3) && strncmp(file, "cat", 3))));
        !           474: }
        !           475:
1.1       schwarze  476: static void
1.12      schwarze  477: pg_index(const struct req *req)
1.1       schwarze  478: {
                    479:
                    480:        resp_begin_html(200, NULL);
                    481:        resp_searchform(req);
1.4       schwarze  482:        printf("<P>\n"
                    483:               "This web interface is documented in the "
1.9       schwarze  484:               "<A HREF=\"%s/mandoc/man8/man.cgi.8\">man.cgi</A> "
                    485:               "manual, and the "
                    486:               "<A HREF=\"%s/mandoc/man1/apropos.1\">apropos</A> "
                    487:               "manual explains the query syntax.\n"
1.4       schwarze  488:               "</P>\n",
                    489:               scriptname, scriptname);
1.1       schwarze  490:        resp_end_html();
                    491: }
                    492:
                    493: static void
1.12      schwarze  494: pg_noresult(const struct req *req, const char *msg)
1.1       schwarze  495: {
                    496:        resp_begin_html(200, NULL);
                    497:        resp_searchform(req);
                    498:        puts("<P>");
                    499:        puts(msg);
                    500:        puts("</P>");
                    501:        resp_end_html();
                    502: }
                    503:
                    504: static void
1.12      schwarze  505: pg_error_badrequest(const char *msg)
1.1       schwarze  506: {
                    507:
                    508:        resp_begin_html(400, "Bad Request");
                    509:        puts("<H1>Bad Request</H1>\n"
                    510:             "<P>\n");
                    511:        puts(msg);
                    512:        printf("Try again from the\n"
                    513:               "<A HREF=\"%s\">main page</A>.\n"
                    514:               "</P>", scriptname);
                    515:        resp_end_html();
                    516: }
                    517:
                    518: static void
1.12      schwarze  519: pg_error_internal(void)
1.1       schwarze  520: {
                    521:        resp_begin_html(500, "Internal Server Error");
                    522:        puts("<P>Internal Server Error</P>");
                    523:        resp_end_html();
                    524: }
                    525:
                    526: static void
1.12      schwarze  527: pg_searchres(const struct req *req, struct manpage *r, size_t sz)
1.1       schwarze  528: {
1.10      schwarze  529:        size_t           i, iuse, isec;
                    530:        int              prio, priouse;
                    531:        char             sec;
1.1       schwarze  532:
1.16    ! schwarze  533:        for (i = 0; i < sz; i++) {
        !           534:                if (validate_filename(r[i].file))
        !           535:                        continue;
        !           536:                fprintf(stderr, "invalid filename %s in %s database\n",
        !           537:                    r[i].file, req->q.manpath);
        !           538:                pg_error_internal();
        !           539:                return;
        !           540:        }
        !           541:
1.1       schwarze  542:        if (1 == sz) {
                    543:                /*
                    544:                 * If we have just one result, then jump there now
                    545:                 * without any delay.
                    546:                 */
1.2       tedu      547:                printf("Status: 303 See Other\r\n");
1.15      schwarze  548:                printf("Location: %s/%s/%s?",
                    549:                    scriptname, req->q.manpath, r[0].file);
1.1       schwarze  550:                http_printquery(req);
1.2       tedu      551:                printf("\r\n"
                    552:                     "Content-Type: text/html; charset=utf-8\r\n"
                    553:                     "\r\n");
1.1       schwarze  554:                return;
                    555:        }
                    556:
                    557:        qsort(r, sz, sizeof(struct manpage), cmp);
                    558:
                    559:        resp_begin_html(200, NULL);
                    560:        resp_searchform(req);
                    561:        puts("<DIV CLASS=\"results\">");
                    562:        puts("<TABLE>");
                    563:
                    564:        for (i = 0; i < sz; i++) {
                    565:                printf("<TR>\n"
                    566:                       "<TD CLASS=\"title\">\n"
1.6       schwarze  567:                       "<A HREF=\"%s/%s/%s?",
1.1       schwarze  568:                    scriptname, req->q.manpath, r[i].file);
                    569:                html_printquery(req);
                    570:                printf("\">");
                    571:                html_print(r[i].names);
                    572:                printf("</A>\n"
                    573:                       "</TD>\n"
                    574:                       "<TD CLASS=\"desc\">");
                    575:                html_print(r[i].output);
                    576:                puts("</TD>\n"
                    577:                     "</TR>");
                    578:        }
                    579:
                    580:        puts("</TABLE>\n"
                    581:             "</DIV>");
1.10      schwarze  582:
                    583:        /*
                    584:         * In man(1) mode, show one of the pages
                    585:         * even if more than one is found.
                    586:         */
                    587:
                    588:        if (req->q.equal) {
                    589:                puts("<HR>");
                    590:                iuse = 0;
                    591:                priouse = 10;
                    592:                for (i = 0; i < sz; i++) {
                    593:                        isec = strcspn(r[i].file, "123456789");
                    594:                        sec = r[i].file[isec];
                    595:                        if ('\0' == sec)
                    596:                                continue;
                    597:                        prio = sec_prios[sec - '1'];
                    598:                        if (prio >= priouse)
                    599:                                continue;
                    600:                        priouse = prio;
                    601:                        iuse = i;
                    602:                }
                    603:                resp_show(req, r[iuse].file);
                    604:        }
                    605:
1.1       schwarze  606:        resp_end_html();
                    607: }
                    608:
                    609: static void
                    610: catman(const struct req *req, const char *file)
                    611: {
                    612:        FILE            *f;
                    613:        size_t           len;
                    614:        int              i;
                    615:        char            *p;
                    616:        int              italic, bold;
                    617:
                    618:        if (NULL == (f = fopen(file, "r"))) {
1.10      schwarze  619:                puts("<P>You specified an invalid manual file.</P>");
1.1       schwarze  620:                return;
                    621:        }
                    622:
                    623:        puts("<DIV CLASS=\"catman\">\n"
                    624:             "<PRE>");
                    625:
                    626:        while (NULL != (p = fgetln(f, &len))) {
                    627:                bold = italic = 0;
                    628:                for (i = 0; i < (int)len - 1; i++) {
                    629:                        /*
                    630:                         * This means that the catpage is out of state.
                    631:                         * Ignore it and keep going (although the
                    632:                         * catpage is bogus).
                    633:                         */
                    634:
                    635:                        if ('\b' == p[i] || '\n' == p[i])
                    636:                                continue;
                    637:
                    638:                        /*
                    639:                         * Print a regular character.
                    640:                         * Close out any bold/italic scopes.
                    641:                         * If we're in back-space mode, make sure we'll
                    642:                         * have something to enter when we backspace.
                    643:                         */
                    644:
                    645:                        if ('\b' != p[i + 1]) {
                    646:                                if (italic)
                    647:                                        printf("</I>");
                    648:                                if (bold)
                    649:                                        printf("</B>");
                    650:                                italic = bold = 0;
                    651:                                html_putchar(p[i]);
                    652:                                continue;
                    653:                        } else if (i + 2 >= (int)len)
                    654:                                continue;
                    655:
                    656:                        /* Italic mode. */
                    657:
                    658:                        if ('_' == p[i]) {
                    659:                                if (bold)
                    660:                                        printf("</B>");
                    661:                                if ( ! italic)
                    662:                                        printf("<I>");
                    663:                                bold = 0;
                    664:                                italic = 1;
                    665:                                i += 2;
                    666:                                html_putchar(p[i]);
                    667:                                continue;
                    668:                        }
                    669:
                    670:                        /*
                    671:                         * Handle funny behaviour troff-isms.
                    672:                         * These grok'd from the original man2html.c.
                    673:                         */
                    674:
                    675:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    676:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    677:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    678:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    679:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    680:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    681:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    682:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    683:                                if (italic)
                    684:                                        printf("</I>");
                    685:                                if (bold)
                    686:                                        printf("</B>");
                    687:                                italic = bold = 0;
                    688:                                putchar('*');
                    689:                                i += 2;
                    690:                                continue;
                    691:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    692:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    693:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    694:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    695:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    696:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    697:                                if (italic)
                    698:                                        printf("</I>");
                    699:                                if (bold)
                    700:                                        printf("</B>");
                    701:                                italic = bold = 0;
                    702:                                putchar('+');
                    703:                                i += 2;
                    704:                                continue;
                    705:                        }
                    706:
                    707:                        /* Bold mode. */
                    708:
                    709:                        if (italic)
                    710:                                printf("</I>");
                    711:                        if ( ! bold)
                    712:                                printf("<B>");
                    713:                        bold = 1;
                    714:                        italic = 0;
                    715:                        i += 2;
                    716:                        html_putchar(p[i]);
                    717:                }
                    718:
                    719:                /*
                    720:                 * Clean up the last character.
                    721:                 * We can get to a newline; don't print that.
                    722:                 */
                    723:
                    724:                if (italic)
                    725:                        printf("</I>");
                    726:                if (bold)
                    727:                        printf("</B>");
                    728:
                    729:                if (i == (int)len - 1 && '\n' != p[i])
                    730:                        html_putchar(p[i]);
                    731:
                    732:                putchar('\n');
                    733:        }
                    734:
                    735:        puts("</PRE>\n"
1.10      schwarze  736:             "</DIV>");
1.1       schwarze  737:
                    738:        fclose(f);
                    739: }
                    740:
                    741: static void
                    742: format(const struct req *req, const char *file)
                    743: {
                    744:        struct mparse   *mp;
                    745:        int              fd;
                    746:        struct mdoc     *mdoc;
                    747:        struct man      *man;
                    748:        void            *vp;
                    749:        enum mandoclevel rc;
                    750:        char             opts[PATH_MAX + 128];
                    751:
                    752:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
1.10      schwarze  753:                puts("<P>You specified an invalid manual file.</P>");
1.1       schwarze  754:                return;
                    755:        }
                    756:
                    757:        mp = mparse_alloc(MPARSE_SO, MANDOCLEVEL_FATAL, NULL,
                    758:            req->q.manpath);
                    759:        rc = mparse_readfd(mp, fd, file);
                    760:        close(fd);
                    761:
                    762:        if (rc >= MANDOCLEVEL_FATAL) {
                    763:                fprintf(stderr, "fatal mandoc error: %s/%s\n",
                    764:                    req->q.manpath, file);
1.12      schwarze  765:                pg_error_internal();
1.1       schwarze  766:                return;
                    767:        }
                    768:
                    769:        snprintf(opts, sizeof(opts),
1.6       schwarze  770:            "fragment,man=%s?query=%%N&amp;sec=%%S",
1.1       schwarze  771:            scriptname);
                    772:
                    773:        mparse_result(mp, &mdoc, &man, NULL);
                    774:        if (NULL == man && NULL == mdoc) {
                    775:                fprintf(stderr, "fatal mandoc error: %s/%s\n",
                    776:                    req->q.manpath, file);
1.12      schwarze  777:                pg_error_internal();
1.1       schwarze  778:                mparse_free(mp);
                    779:                return;
                    780:        }
                    781:
                    782:        vp = html_alloc(opts);
                    783:
                    784:        if (NULL != mdoc)
                    785:                html_mdoc(vp, mdoc);
                    786:        else
                    787:                html_man(vp, man);
                    788:
                    789:        html_free(vp);
                    790:        mparse_free(mp);
                    791: }
                    792:
                    793: static void
1.10      schwarze  794: resp_show(const struct req *req, const char *file)
                    795: {
1.16    ! schwarze  796:
        !           797:        if ('.' == file[0] && '/' == file[1])
1.11      schwarze  798:                file += 2;
1.10      schwarze  799:
                    800:        if ('c' == *file)
                    801:                catman(req, file);
                    802:        else
                    803:                format(req, file);
                    804: }
                    805:
                    806: static void
1.6       schwarze  807: pg_show(const struct req *req, const char *path)
1.1       schwarze  808: {
                    809:        char            *sub;
                    810:
                    811:        if (NULL == path || NULL == (sub = strchr(path, '/'))) {
1.12      schwarze  812:                pg_error_badrequest(
1.1       schwarze  813:                    "You did not specify a page to show.");
                    814:                return;
                    815:        }
                    816:        *sub++ = '\0';
                    817:
                    818:        /*
                    819:         * Begin by chdir()ing into the manpath.
                    820:         * This way we can pick up the database files, which are
                    821:         * relative to the manpath root.
                    822:         */
                    823:
                    824:        if (-1 == chdir(path)) {
1.12      schwarze  825:                pg_error_badrequest(
1.1       schwarze  826:                    "You specified an invalid manpath.");
1.16    ! schwarze  827:                return;
        !           828:        }
        !           829:
        !           830:        if ( ! validate_filename(sub)) {
        !           831:                pg_error_badrequest(
        !           832:                    "You specified an invalid manual file.");
1.1       schwarze  833:                return;
                    834:        }
                    835:
1.10      schwarze  836:        resp_begin_html(200, NULL);
                    837:        resp_searchform(req);
                    838:        resp_show(req, sub);
                    839:        resp_end_html();
1.1       schwarze  840: }
                    841:
                    842: static void
1.6       schwarze  843: pg_search(const struct req *req)
1.1       schwarze  844: {
                    845:        struct mansearch          search;
                    846:        struct manpaths           paths;
                    847:        struct manpage           *res;
                    848:        char                    **cp;
                    849:        const char               *ep, *start;
                    850:        size_t                    ressz;
                    851:        int                       i, sz;
                    852:
                    853:        /*
                    854:         * Begin by chdir()ing into the root of the manpath.
                    855:         * This way we can pick up the database files, which are
                    856:         * relative to the manpath root.
                    857:         */
                    858:
                    859:        if (-1 == (chdir(req->q.manpath))) {
1.12      schwarze  860:                pg_error_badrequest(
1.1       schwarze  861:                    "You specified an invalid manpath.");
                    862:                return;
                    863:        }
                    864:
                    865:        search.arch = req->q.arch;
                    866:        search.sec = req->q.sec;
1.5       schwarze  867:        search.deftype = req->q.equal ? TYPE_Nm : (TYPE_Nm | TYPE_Nd);
                    868:        search.flags = req->q.equal ? MANSEARCH_MAN : 0;
1.1       schwarze  869:
                    870:        paths.sz = 1;
                    871:        paths.paths = mandoc_malloc(sizeof(char *));
                    872:        paths.paths[0] = mandoc_strdup(".");
                    873:
                    874:        /*
                    875:         * Poor man's tokenisation: just break apart by spaces.
                    876:         * Yes, this is half-ass.  But it works for now.
                    877:         */
                    878:
                    879:        ep = req->q.expr;
                    880:        while (ep && isspace((unsigned char)*ep))
                    881:                ep++;
                    882:
                    883:        sz = 0;
                    884:        cp = NULL;
                    885:        while (ep && '\0' != *ep) {
                    886:                cp = mandoc_reallocarray(cp, sz + 1, sizeof(char *));
                    887:                start = ep;
                    888:                while ('\0' != *ep && ! isspace((unsigned char)*ep))
                    889:                        ep++;
                    890:                cp[sz] = mandoc_malloc((ep - start) + 1);
                    891:                memcpy(cp[sz], start, ep - start);
                    892:                cp[sz++][ep - start] = '\0';
                    893:                while (isspace((unsigned char)*ep))
                    894:                        ep++;
                    895:        }
                    896:
                    897:        if (0 == mansearch(&search, &paths, sz, cp, "Nd", &res, &ressz))
1.12      schwarze  898:                pg_noresult(req, "You entered an invalid query.");
1.1       schwarze  899:        else if (0 == ressz)
1.12      schwarze  900:                pg_noresult(req, "No results found.");
1.1       schwarze  901:        else
1.12      schwarze  902:                pg_searchres(req, res, ressz);
1.1       schwarze  903:
                    904:        for (i = 0; i < sz; i++)
                    905:                free(cp[i]);
                    906:        free(cp);
                    907:
                    908:        for (i = 0; i < (int)ressz; i++) {
                    909:                free(res[i].file);
                    910:                free(res[i].names);
                    911:                free(res[i].output);
                    912:        }
                    913:        free(res);
                    914:
                    915:        free(paths.paths[0]);
                    916:        free(paths.paths);
                    917: }
                    918:
                    919: int
                    920: main(void)
                    921: {
1.6       schwarze  922:        struct req       req;
                    923:        const char      *path;
                    924:        char            *querystring;
1.1       schwarze  925:        int              i;
                    926:
                    927:        /* Scan our run-time environment. */
                    928:
                    929:        if (NULL == (scriptname = getenv("SCRIPT_NAME")))
                    930:                scriptname = "";
                    931:
                    932:        /*
1.7       schwarze  933:         * First we change directory into the MAN_DIR so that
1.1       schwarze  934:         * subsequent scanning for manpath directories is rooted
                    935:         * relative to the same position.
                    936:         */
                    937:
1.7       schwarze  938:        if (-1 == chdir(MAN_DIR)) {
1.1       schwarze  939:                fprintf(stderr, "MAN_DIR: %s: %s\n",
1.7       schwarze  940:                    MAN_DIR, strerror(errno));
1.12      schwarze  941:                pg_error_internal();
1.1       schwarze  942:                return(EXIT_FAILURE);
                    943:        }
                    944:
                    945:        memset(&req, 0, sizeof(struct req));
                    946:        pathgen(&req);
                    947:
                    948:        /* Next parse out the query string. */
                    949:
                    950:        if (NULL != (querystring = getenv("QUERY_STRING")))
                    951:                http_parse(&req, querystring);
                    952:
1.6       schwarze  953:        /* Dispatch to the three different pages. */
1.1       schwarze  954:
1.6       schwarze  955:        path = getenv("PATH_INFO");
                    956:        if (NULL == path)
                    957:                path = "";
                    958:        else if ('/' == *path)
                    959:                path++;
                    960:
                    961:        if ('\0' != *path)
                    962:                pg_show(&req, path);
                    963:        else if (NULL != req.q.expr)
                    964:                pg_search(&req);
                    965:        else
1.12      schwarze  966:                pg_index(&req);
1.1       schwarze  967:
                    968:        for (i = 0; i < (int)req.psz; i++)
                    969:                free(req.p[i]);
                    970:        free(req.p);
                    971:        return(EXIT_SUCCESS);
                    972: }
                    973:
                    974: static int
                    975: cmp(const void *p1, const void *p2)
                    976: {
                    977:
                    978:        return(strcasecmp(((const struct manpage *)p1)->names,
                    979:            ((const struct manpage *)p2)->names));
                    980: }
                    981:
                    982: /*
                    983:  * Scan for indexable paths.
                    984:  */
                    985: static void
                    986: pathgen(struct req *req)
                    987: {
                    988:        FILE    *fp;
                    989:        char    *dp;
                    990:        size_t   dpsz;
                    991:
1.14      schwarze  992:        if (NULL == (fp = fopen("manpath.conf", "r"))) {
                    993:                fprintf(stderr, "%s/manpath.conf: %s\n",
                    994:                        MAN_DIR, strerror(errno));
                    995:                pg_error_internal();
                    996:                exit(EXIT_FAILURE);
                    997:        }
1.1       schwarze  998:
                    999:        while (NULL != (dp = fgetln(fp, &dpsz))) {
                   1000:                if ('\n' == dp[dpsz - 1])
                   1001:                        dpsz--;
                   1002:                req->p = mandoc_realloc(req->p,
                   1003:                    (req->psz + 1) * sizeof(char *));
                   1004:                req->p[req->psz++] = mandoc_strndup(dp, dpsz);
1.14      schwarze 1005:        }
                   1006:
                   1007:        if ( req->p == NULL ) {
                   1008:                fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR);
                   1009:                pg_error_internal();
                   1010:                exit(EXIT_FAILURE);
1.1       schwarze 1011:        }
                   1012: }