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

Annotation of mandoc/cgi.c, Revision 1.65

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

CVSweb