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

Annotation of mandoc/cgi.c, Revision 1.61

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

CVSweb