[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/cgi.c, Revision 1.71

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

CVSweb