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

Annotation of mandoc/cgi.c, Revision 1.13

1.13    ! kristaps    1: /*     $Id: cgi.c,v 1.12 2011/12/07 13:00:40 kristaps Exp $ */
1.6       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/param.h>
                     22: #include <sys/wait.h>
                     23:
1.1       kristaps   24: #include <assert.h>
1.6       kristaps   25: #include <ctype.h>
                     26: #include <errno.h>
1.1       kristaps   27: #include <fcntl.h>
1.6       kristaps   28: #include <limits.h>
1.1       kristaps   29: #include <regex.h>
                     30: #include <stdio.h>
                     31: #include <stdarg.h>
1.5       kristaps   32: #include <stdint.h>
1.1       kristaps   33: #include <stdlib.h>
                     34: #include <string.h>
1.6       kristaps   35: #include <unistd.h>
1.1       kristaps   36:
1.6       kristaps   37: #include "apropos_db.h"
1.4       schwarze   38: #include "mandoc.h"
1.8       kristaps   39: #include "mdoc.h"
                     40: #include "man.h"
                     41: #include "main.h"
1.6       kristaps   42: #include "manpath.h"
                     43:
                     44: #ifdef __linux__
                     45: # include <db_185.h>
                     46: #else
                     47: # include <db.h>
                     48: #endif
1.1       kristaps   49:
                     50: enum   page {
                     51:        PAGE_INDEX,
                     52:        PAGE_SEARCH,
1.6       kristaps   53:        PAGE_SHOW,
1.1       kristaps   54:        PAGE__MAX
                     55: };
                     56:
                     57: struct kval {
                     58:        char            *key;
                     59:        char            *val;
                     60: };
                     61:
                     62: struct req {
1.6       kristaps   63:        struct kval     *fields;
1.1       kristaps   64:        size_t           fieldsz;
                     65:        enum page        page;
                     66: };
                     67:
1.6       kristaps   68: static int              atou(const char *, unsigned *);
1.9       kristaps   69: static void             catman(const char *);
1.8       kristaps   70: static void             format(const char *);
1.6       kristaps   71: static void             html_print(const char *);
1.10      kristaps   72: static void             html_putchar(char);
1.1       kristaps   73: static int              kval_decode(char *);
                     74: static void             kval_parse(struct kval **, size_t *, char *);
                     75: static void             kval_free(struct kval *, size_t);
1.6       kristaps   76: static void             pg_index(const struct manpaths *,
                     77:                                const struct req *, char *);
                     78: static void             pg_search(const struct manpaths *,
                     79:                                const struct req *, char *);
                     80: static void             pg_show(const struct manpaths *,
                     81:                                const struct req *, char *);
1.7       kristaps   82: static void             resp_bad(void);
1.6       kristaps   83: static void             resp_baddb(void);
1.10      kristaps   84: static void             resp_error400(void);
                     85: static void             resp_error404(const char *);
1.6       kristaps   86: static void             resp_begin_html(int, const char *);
                     87: static void             resp_begin_http(int, const char *);
                     88: static void             resp_end_html(void);
                     89: static void             resp_index(const struct req *);
                     90: static void             resp_search(struct res *, size_t, void *);
                     91: static void             resp_searchform(const struct req *);
                     92:
                     93: static const char       *progname;
1.7       kristaps   94: static const char       *cache;
1.6       kristaps   95: static const char       *host;
1.1       kristaps   96:
                     97: static const char * const pages[PAGE__MAX] = {
                     98:        "index", /* PAGE_INDEX */
                     99:        "search", /* PAGE_SEARCH */
1.6       kristaps  100:        "show", /* PAGE_SHOW */
1.1       kristaps  101: };
                    102:
1.6       kristaps  103: /*
                    104:  * This is just OpenBSD's strtol(3) suggestion.
                    105:  * I use it instead of strtonum(3) for portability's sake.
                    106:  */
                    107: static int
                    108: atou(const char *buf, unsigned *v)
                    109: {
                    110:        char            *ep;
                    111:        long             lval;
                    112:
                    113:        errno = 0;
                    114:        lval = strtol(buf, &ep, 10);
                    115:        if (buf[0] == '\0' || *ep != '\0')
                    116:                return(0);
                    117:        if ((errno == ERANGE && (lval == LONG_MAX ||
                    118:                                        lval == LONG_MIN)) ||
                    119:                        (lval > UINT_MAX || lval < 0))
                    120:                return(0);
                    121:
                    122:        *v = (unsigned int)lval;
                    123:        return(1);
                    124: }
1.1       kristaps  125:
1.10      kristaps  126: static void
                    127: html_putchar(char c)
                    128: {
                    129:
                    130:        switch (c) {
                    131:        case ('"'):
                    132:                printf("&quote;");
                    133:                break;
                    134:        case ('&'):
                    135:                printf("&amp;");
                    136:                break;
                    137:        case ('>'):
                    138:                printf("&gt;");
                    139:                break;
                    140:        case ('<'):
                    141:                printf("&lt;");
                    142:                break;
                    143:        default:
                    144:                putchar((unsigned char)c);
                    145:                break;
                    146:        }
                    147: }
                    148:
1.6       kristaps  149: /*
                    150:  * Print a word, escaping HTML along the way.
                    151:  * This will pass non-ASCII straight to output: be warned!
                    152:  */
1.1       kristaps  153: static void
1.6       kristaps  154: html_print(const char *p)
1.1       kristaps  155: {
1.6       kristaps  156:
                    157:        if (NULL == p)
                    158:                return;
1.1       kristaps  159:        while ('\0' != *p)
1.10      kristaps  160:                html_putchar(*p++);
1.1       kristaps  161: }
                    162:
                    163: static void
                    164: kval_free(struct kval *p, size_t sz)
                    165: {
                    166:        int              i;
                    167:
                    168:        for (i = 0; i < (int)sz; i++) {
                    169:                free(p[i].key);
                    170:                free(p[i].val);
                    171:        }
                    172:        free(p);
                    173: }
                    174:
                    175: /*
                    176:  * Parse out key-value pairs from an HTTP request variable.
1.6       kristaps  177:  * This can be either a cookie or a POST/GET string, although man.cgi
                    178:  * uses only GET for simplicity.
1.1       kristaps  179:  */
                    180: static void
                    181: kval_parse(struct kval **kv, size_t *kvsz, char *p)
                    182: {
                    183:        char            *key, *val;
                    184:        size_t           sz, cur;
                    185:
                    186:        cur = 0;
                    187:
                    188:        while (p && '\0' != *p) {
                    189:                while (' ' == *p)
                    190:                        p++;
                    191:
                    192:                key = p;
                    193:                val = NULL;
                    194:
                    195:                if (NULL != (p = strchr(p, '='))) {
                    196:                        *p++ = '\0';
                    197:                        val = p;
                    198:
                    199:                        sz = strcspn(p, ";&");
                    200:                        /* LINTED */
                    201:                        p += sz;
                    202:
                    203:                        if ('\0' != *p)
                    204:                                *p++ = '\0';
                    205:                } else {
                    206:                        p = key;
                    207:                        sz = strcspn(p, ";&");
                    208:                        /* LINTED */
                    209:                        p += sz;
                    210:
                    211:                        if ('\0' != *p)
                    212:                                p++;
                    213:                        continue;
                    214:                }
                    215:
                    216:                if ('\0' == *key || '\0' == *val)
                    217:                        continue;
                    218:
                    219:                /* Just abort handling. */
                    220:
                    221:                if ( ! kval_decode(key))
                    222:                        return;
                    223:                if ( ! kval_decode(val))
                    224:                        return;
                    225:
                    226:                if (*kvsz + 1 >= cur) {
                    227:                        cur++;
                    228:                        *kv = mandoc_realloc
                    229:                                (*kv, cur * sizeof(struct kval));
                    230:                }
                    231:
                    232:                (*kv)[(int)*kvsz].key = mandoc_strdup(key);
                    233:                (*kv)[(int)*kvsz].val = mandoc_strdup(val);
                    234:                (*kvsz)++;
                    235:        }
                    236: }
                    237:
                    238: /*
1.6       kristaps  239:  * HTTP-decode a string.  The standard explanation is that this turns
                    240:  * "%4e+foo" into "n foo" in the regular way.  This is done in-place
                    241:  * over the allocated string.
1.1       kristaps  242:  */
                    243: static int
                    244: kval_decode(char *p)
                    245: {
                    246:        char             hex[3];
                    247:        int              c;
                    248:
                    249:        hex[2] = '\0';
                    250:
                    251:        for ( ; '\0' != *p; p++) {
                    252:                if ('%' == *p) {
                    253:                        if ('\0' == (hex[0] = *(p + 1)))
                    254:                                return(0);
                    255:                        if ('\0' == (hex[1] = *(p + 2)))
                    256:                                return(0);
                    257:                        if (1 != sscanf(hex, "%x", &c))
                    258:                                return(0);
                    259:                        if ('\0' == c)
                    260:                                return(0);
                    261:
                    262:                        *p = (char)c;
                    263:                        memmove(p + 1, p + 3, strlen(p + 3) + 1);
                    264:                } else
                    265:                        *p = '+' == *p ? ' ' : *p;
                    266:        }
                    267:
                    268:        *p = '\0';
                    269:        return(1);
                    270: }
                    271:
1.6       kristaps  272: static void
                    273: resp_begin_http(int code, const char *msg)
                    274: {
                    275:
                    276:        if (200 != code)
                    277:                printf("Status: %d %s\n", code, msg);
                    278:
                    279:        puts("Content-Type: text/html; charset=utf-8"           "\n"
                    280:             "Cache-Control: no-cache"                          "\n"
                    281:             "Pragma: no-cache"                                 "\n"
                    282:             "");
                    283:
                    284:        fflush(stdout);
                    285: }
                    286:
                    287: static void
                    288: resp_begin_html(int code, const char *msg)
                    289: {
                    290:
                    291:        resp_begin_http(code, msg);
                    292:
                    293:        puts("<!DOCTYPE HTML PUBLIC "                           "\n"
                    294:             " \"-//W3C//DTD HTML 4.01//EN\""                   "\n"
                    295:             " \"http://www.w3.org/TR/html4/strict.dtd\">"      "\n"
                    296:             "<HTML>"                                           "\n"
                    297:             " <HEAD>"                                          "\n"
1.10      kristaps  298:             "   <META HTTP-EQUIV=\"Content-Type\" "            "\n"
                    299:             "         CONTENT=\"text/html; charset=utf-8\">"   "\n"
1.12      kristaps  300:             "  <LINK REL=\"stylesheet\" HREF=\"/man.cgi.css\"" "\n"
                    301:             "        TYPE=\"text/css\" media=\"all\">"         "\n"
1.6       kristaps  302:             "  <TITLE>System Manpage Reference</TITLE>"        "\n"
                    303:             " </HEAD>"                                         "\n"
                    304:             " <BODY>"                                          "\n"
                    305:             "<!-- Begin page content. //-->");
                    306: }
                    307:
                    308: static void
                    309: resp_end_html(void)
                    310: {
                    311:
                    312:        puts(" </BODY>\n</HTML>");
                    313: }
                    314:
                    315: static void
                    316: resp_searchform(const struct req *req)
                    317: {
                    318:        int              i;
                    319:        const char      *expr, *sec, *arch;
                    320:
                    321:        expr = sec = arch = "";
                    322:
                    323:        for (i = 0; i < (int)req->fieldsz; i++)
                    324:                if (0 == strcmp(req->fields[i].key, "expr"))
                    325:                        expr = req->fields[i].val;
1.13    ! kristaps  326:                else if (0 == strcmp(req->fields[i].key, "query"))
        !           327:                        expr = req->fields[i].val;
1.6       kristaps  328:                else if (0 == strcmp(req->fields[i].key, "sec"))
                    329:                        sec = req->fields[i].val;
1.13    ! kristaps  330:                else if (0 == strcmp(req->fields[i].key, "sektion"))
        !           331:                        sec = req->fields[i].val;
1.6       kristaps  332:                else if (0 == strcmp(req->fields[i].key, "arch"))
                    333:                        arch = req->fields[i].val;
                    334:
1.13    ! kristaps  335:        if (NULL != sec && 0 == strcmp(sec, "0"))
        !           336:                sec = NULL;
        !           337:
1.6       kristaps  338:        puts("<!-- Begin search form. //-->");
                    339:        printf("<FORM ACTION=\"");
                    340:        html_print(progname);
1.9       kristaps  341:        printf("/search.html\" METHOD=\"get\">\n");
1.12      kristaps  342:        puts("<FIELDSET>\n"
                    343:             "<INPUT TYPE=\"submit\" NAME=\"op\" "
                    344:              "VALUE=\"Whatis\"> or \n"
                    345:             "<INPUT TYPE=\"submit\" NAME=\"op\" "
                    346:              "VALUE=\"apropos\"> for manuals satisfying \n"
                    347:             "<INPUT TYPE=\"text\" SIZE=\"40\" "
                    348:              "NAME=\"expr\" VALUE=\"");
1.6       kristaps  349:        html_print(expr);
1.12      kristaps  350:        puts("\">, section "
                    351:             "<INPUT TYPE=\"text\" "
                    352:              "SIZE=\"4\" NAME=\"sec\" VALUE=\"");
1.6       kristaps  353:        html_print(sec);
1.12      kristaps  354:        puts("\">, arch "
                    355:             "<INPUT TYPE=\"text\" "
                    356:              "SIZE=\"8\" NAME=\"arch\" VALUE=\"");
1.6       kristaps  357:        html_print(arch);
1.12      kristaps  358:        puts("\">.\n"
                    359:             "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
                    360:             "</FIELDSET>\n"
                    361:             "</FORM>\n"
                    362:             "<!-- End search form. //-->");
1.6       kristaps  363: }
                    364:
                    365: static void
                    366: resp_index(const struct req *req)
                    367: {
                    368:
                    369:        resp_begin_html(200, NULL);
                    370:        resp_searchform(req);
                    371:        resp_end_html();
                    372: }
                    373:
                    374: static void
1.10      kristaps  375: resp_error400(void)
1.9       kristaps  376: {
                    377:
1.10      kristaps  378:        resp_begin_html(400, "Query Malformed");
1.12      kristaps  379:        printf("<H1>Malformed Query</H1>\n"
                    380:               "<P>\n"
                    381:               "  The query your entered was malformed.\n"
                    382:               "  Try again from the\n"
                    383:               "  <A HREF=\"%s/index.html\">main page</A>\n"
                    384:               "</P>", progname);
1.9       kristaps  385:        resp_end_html();
                    386: }
                    387:
                    388: static void
1.10      kristaps  389: resp_error404(const char *page)
1.6       kristaps  390: {
                    391:
                    392:        resp_begin_html(404, "Not Found");
1.10      kristaps  393:        puts("<H1>Page Not Found</H1>\n"
                    394:             "<P>\n"
                    395:             "  The page you're looking for, ");
                    396:        printf("  <B>");
                    397:        html_print(page);
1.12      kristaps  398:        printf("</B>,\n"
                    399:               "  could not be found.\n"
                    400:               "  Try searching from the\n"
                    401:               "  <A HREF=\"%s/index.html\">main page</A>\n"
                    402:               "</P>", progname);
1.6       kristaps  403:        resp_end_html();
                    404: }
1.1       kristaps  405:
                    406: static void
1.7       kristaps  407: resp_bad(void)
                    408: {
                    409:        resp_begin_html(500, "Internal Server Error");
                    410:        puts("<P>Generic badness happened.</P>");
                    411:        resp_end_html();
                    412: }
                    413:
                    414: static void
1.6       kristaps  415: resp_baddb(void)
1.1       kristaps  416: {
                    417:
1.6       kristaps  418:        resp_begin_html(500, "Internal Server Error");
                    419:        puts("<P>Your database is broken.</P>");
                    420:        resp_end_html();
1.1       kristaps  421: }
                    422:
                    423: static void
1.6       kristaps  424: resp_search(struct res *r, size_t sz, void *arg)
1.1       kristaps  425: {
                    426:        int              i;
                    427:
1.6       kristaps  428:        if (1 == sz) {
                    429:                /*
                    430:                 * If we have just one result, then jump there now
                    431:                 * without any delay.
                    432:                 */
                    433:                puts("Status: 303 See Other");
                    434:                printf("Location: http://%s%s/show/%u/%u.html\n",
                    435:                                host, progname,
                    436:                                r[0].volume, r[0].rec);
                    437:                puts("Content-Type: text/html; charset=utf-8\n");
                    438:                return;
                    439:        }
                    440:
1.12      kristaps  441:        resp_begin_html(200, NULL);
1.6       kristaps  442:        resp_searchform((const struct req *)arg);
                    443:
                    444:        if (0 == sz)
                    445:                puts("<P>No results found.</P>");
1.1       kristaps  446:
                    447:        for (i = 0; i < (int)sz; i++) {
1.6       kristaps  448:                printf("<P><A HREF=\"");
                    449:                html_print(progname);
                    450:                printf("/show/%u/%u.html\">", r[i].volume, r[i].rec);
                    451:                html_print(r[i].title);
1.1       kristaps  452:                putchar('(');
1.6       kristaps  453:                html_print(r[i].cat);
                    454:                if (r[i].arch && '\0' != *r[i].arch) {
                    455:                        putchar('/');
                    456:                        html_print(r[i].arch);
                    457:                }
                    458:                printf(")</A> ");
                    459:                html_print(r[i].desc);
                    460:                puts("</P>");
1.1       kristaps  461:        }
1.6       kristaps  462:
                    463:        resp_end_html();
                    464: }
                    465:
                    466: /* ARGSUSED */
                    467: static void
                    468: pg_index(const struct manpaths *ps, const struct req *req, char *path)
                    469: {
                    470:
                    471:        resp_index(req);
1.1       kristaps  472: }
                    473:
                    474: static void
1.9       kristaps  475: catman(const char *file)
                    476: {
1.10      kristaps  477:        FILE            *f;
                    478:        size_t           len;
                    479:        int              i;
                    480:        char            *p;
                    481:        int              italic, bold;
1.9       kristaps  482:
1.10      kristaps  483:        if (NULL == (f = fopen(file, "r"))) {
1.9       kristaps  484:                resp_baddb();
                    485:                return;
                    486:        }
                    487:
1.12      kristaps  488:        resp_begin_http(200, NULL);
                    489:        puts("<!DOCTYPE HTML PUBLIC "                           "\n"
                    490:             " \"-//W3C//DTD HTML 4.01//EN\""                   "\n"
                    491:             " \"http://www.w3.org/TR/html4/strict.dtd\">"      "\n"
                    492:             "<HTML>"                                           "\n"
                    493:             " <HEAD>"                                          "\n"
                    494:             "  <META HTTP-EQUIV=\"Content-Type\" "             "\n"
                    495:             "        CONTENT=\"text/html; charset=utf-8\">"    "\n"
                    496:             "  <LINK REL=\"stylesheet\" HREF=\"/catman.css\""  "\n"
                    497:             "        TYPE=\"text/css\" media=\"all\">"         "\n"
                    498:             "  <TITLE>System Manpage Reference</TITLE>"        "\n"
                    499:             " </HEAD>"                                         "\n"
                    500:             " <BODY>"                                          "\n"
                    501:             "<!-- Begin page content. //-->");
1.10      kristaps  502:
                    503:        puts("<PRE>");
                    504:        while (NULL != (p = fgetln(f, &len))) {
                    505:                bold = italic = 0;
                    506:                for (i = 0; i < (int)len - 1; i++) {
                    507:                        /*
                    508:                         * This means that the catpage is out of state.
                    509:                         * Ignore it and keep going (although the
                    510:                         * catpage is bogus).
                    511:                         */
                    512:
                    513:                        if ('\b' == p[i] || '\n' == p[i])
                    514:                                continue;
                    515:
                    516:                        /*
                    517:                         * Print a regular character.
                    518:                         * Close out any bold/italic scopes.
                    519:                         * If we're in back-space mode, make sure we'll
                    520:                         * have something to enter when we backspace.
                    521:                         */
                    522:
                    523:                        if ('\b' != p[i + 1]) {
                    524:                                if (italic)
                    525:                                        printf("</I>");
                    526:                                if (bold)
                    527:                                        printf("</B>");
                    528:                                italic = bold = 0;
                    529:                                html_putchar(p[i]);
                    530:                                continue;
                    531:                        } else if (i + 2 >= (int)len)
                    532:                                continue;
                    533:
                    534:                        /* Italic mode. */
                    535:
                    536:                        if ('_' == p[i]) {
                    537:                                if (bold)
                    538:                                        printf("</B>");
                    539:                                if ( ! italic)
                    540:                                        printf("<I>");
                    541:                                bold = 0;
                    542:                                italic = 1;
                    543:                                i += 2;
                    544:                                html_putchar(p[i]);
                    545:                                continue;
                    546:                        }
                    547:
                    548:                        /*
                    549:                         * Handle funny behaviour troff-isms.
                    550:                         * These grok'd from the original man2html.c.
                    551:                         */
                    552:
                    553:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    554:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    555:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    556:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    557:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    558:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    559:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    560:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    561:                                if (italic)
                    562:                                        printf("</I>");
                    563:                                if (bold)
                    564:                                        printf("</B>");
                    565:                                italic = bold = 0;
                    566:                                putchar('*');
                    567:                                i += 2;
                    568:                                continue;
                    569:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    570:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    571:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    572:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    573:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    574:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    575:                                if (italic)
                    576:                                        printf("</I>");
                    577:                                if (bold)
                    578:                                        printf("</B>");
                    579:                                italic = bold = 0;
                    580:                                putchar('+');
                    581:                                i += 2;
                    582:                                continue;
                    583:                        }
                    584:
                    585:                        /* Bold mode. */
                    586:
                    587:                        if (italic)
                    588:                                printf("</I>");
                    589:                        if ( ! bold)
                    590:                                printf("<B>");
                    591:                        bold = 1;
                    592:                        italic = 0;
                    593:                        i += 2;
                    594:                        html_putchar(p[i]);
                    595:                }
                    596:
                    597:                /*
                    598:                 * Clean up the last character.
                    599:                 * We can get to a newline; don't print that.
                    600:                 */
1.9       kristaps  601:
1.10      kristaps  602:                if (italic)
                    603:                        printf("</I>");
                    604:                if (bold)
                    605:                        printf("</B>");
1.9       kristaps  606:
1.10      kristaps  607:                if (i == (int)len - 1 && '\n' != p[i])
                    608:                        html_putchar(p[i]);
1.9       kristaps  609:
1.10      kristaps  610:                putchar('\n');
                    611:        }
                    612:
                    613:        puts("</PRE>\n"
                    614:             "</BODY>\n"
                    615:             "</HTML>");
                    616:
                    617:        fclose(f);
1.9       kristaps  618: }
                    619:
                    620: static void
1.8       kristaps  621: format(const char *file)
1.7       kristaps  622: {
1.8       kristaps  623:        struct mparse   *mp;
                    624:        int              fd;
                    625:        struct mdoc     *mdoc;
                    626:        struct man      *man;
                    627:        void            *vp;
                    628:        enum mandoclevel rc;
1.10      kristaps  629:        char             opts[MAXPATHLEN + 128];
1.7       kristaps  630:
1.8       kristaps  631:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    632:                resp_baddb();
1.7       kristaps  633:                return;
                    634:        }
                    635:
1.8       kristaps  636:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
                    637:        rc = mparse_readfd(mp, fd, file);
                    638:        close(fd);
1.7       kristaps  639:
1.8       kristaps  640:        if (rc >= MANDOCLEVEL_FATAL) {
1.7       kristaps  641:                resp_baddb();
                    642:                return;
                    643:        }
                    644:
1.11      kristaps  645:        snprintf(opts, sizeof(opts), "style=/man.css,"
1.10      kristaps  646:                        "man=%s/search.html?sec=%%S&expr=%%N,"
1.11      kristaps  647:                        /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
1.10      kristaps  648:                        progname);
                    649:
1.8       kristaps  650:        mparse_result(mp, &mdoc, &man);
1.10      kristaps  651:        vp = html_alloc(opts);
1.7       kristaps  652:
1.8       kristaps  653:        if (NULL != mdoc) {
                    654:                resp_begin_http(200, NULL);
                    655:                html_mdoc(vp, mdoc);
                    656:        } else if (NULL != man) {
                    657:                resp_begin_http(200, NULL);
                    658:                html_man(vp, man);
                    659:        } else
                    660:                resp_baddb();
1.7       kristaps  661:
1.8       kristaps  662:        html_free(vp);
                    663:        mparse_free(mp);
1.7       kristaps  664: }
                    665:
                    666: static void
1.6       kristaps  667: pg_show(const struct manpaths *ps, const struct req *req, char *path)
1.1       kristaps  668: {
1.6       kristaps  669:        char            *sub;
1.7       kristaps  670:        char             file[MAXPATHLEN];
1.9       kristaps  671:        const char      *fn, *cp;
1.6       kristaps  672:        int              rc;
                    673:        unsigned int     vol, rec;
1.9       kristaps  674:        DB              *idx;
1.6       kristaps  675:        DBT              key, val;
                    676:
                    677:        if (NULL == path) {
1.10      kristaps  678:                resp_error400();
1.6       kristaps  679:                return;
                    680:        } else if (NULL == (sub = strrchr(path, '/'))) {
1.10      kristaps  681:                resp_error400();
1.6       kristaps  682:                return;
                    683:        } else
                    684:                *sub++ = '\0';
                    685:
                    686:        if ( ! (atou(path, &vol) && atou(sub, &rec))) {
1.10      kristaps  687:                resp_error400();
1.6       kristaps  688:                return;
                    689:        } else if (vol >= (unsigned int)ps->sz) {
1.10      kristaps  690:                resp_error400();
1.6       kristaps  691:                return;
                    692:        }
                    693:
                    694:        strlcpy(file, ps->paths[vol], MAXPATHLEN);
                    695:        strlcat(file, "/mandoc.index", MAXPATHLEN);
                    696:
                    697:        /* Open the index recno(3) database. */
                    698:
1.9       kristaps  699:        idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
                    700:        if (NULL == idx) {
1.6       kristaps  701:                resp_baddb();
                    702:                return;
                    703:        }
                    704:
                    705:        key.data = &rec;
                    706:        key.size = 4;
                    707:
1.9       kristaps  708:        if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
1.10      kristaps  709:                rc < 0 ? resp_baddb() : resp_error400();
1.9       kristaps  710:                goto out;
1.6       kristaps  711:        }
                    712:
1.9       kristaps  713:        cp = (char *)val.data;
1.6       kristaps  714:
1.9       kristaps  715:        if (NULL == (fn = memchr(cp, '\0', val.size)))
                    716:                resp_baddb();
                    717:        else if (++fn - cp >= (int)val.size)
                    718:                resp_baddb();
                    719:        else if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
                    720:                resp_baddb();
                    721:        else {
                    722:                strlcpy(file, ps->paths[vol], MAXPATHLEN);
                    723:                strlcat(file, "/", MAXPATHLEN);
                    724:                strlcat(file, fn, MAXPATHLEN);
                    725:                if (0 == strcmp(cp, "cat"))
                    726:                        catman(file);
                    727:                else
                    728:                        format(file);
                    729:        }
                    730: out:
                    731:        (*idx->close)(idx);
1.6       kristaps  732: }
                    733:
                    734: static void
                    735: pg_search(const struct manpaths *ps, const struct req *req, char *path)
                    736: {
                    737:        size_t            tt;
1.12      kristaps  738:        int               i, sz, rc, whatis;
1.6       kristaps  739:        const char       *ep, *start;
                    740:        char            **cp;
                    741:        struct opts       opt;
                    742:        struct expr      *expr;
                    743:
                    744:        expr = NULL;
                    745:        cp = NULL;
                    746:        ep = NULL;
                    747:        sz = 0;
1.12      kristaps  748:        whatis = 0;
1.1       kristaps  749:
                    750:        memset(&opt, 0, sizeof(struct opts));
1.6       kristaps  751:
                    752:        for (sz = i = 0; i < (int)req->fieldsz; i++)
                    753:                if (0 == strcmp(req->fields[i].key, "expr"))
                    754:                        ep = req->fields[i].val;
1.13    ! kristaps  755:                else if (0 == strcmp(req->fields[i].key, "query"))
        !           756:                        ep = req->fields[i].val;
1.6       kristaps  757:                else if (0 == strcmp(req->fields[i].key, "sec"))
                    758:                        opt.cat = req->fields[i].val;
1.13    ! kristaps  759:                else if (0 == strcmp(req->fields[i].key, "sektion"))
        !           760:                        opt.cat = req->fields[i].val;
1.6       kristaps  761:                else if (0 == strcmp(req->fields[i].key, "arch"))
                    762:                        opt.arch = req->fields[i].val;
1.13    ! kristaps  763:                else if (0 == strcmp(req->fields[i].key, "apropos"))
        !           764:                        whatis = 0 == strcmp
        !           765:                                (req->fields[i].val, "0");
1.12      kristaps  766:                else if (0 == strcmp(req->fields[i].key, "op"))
                    767:                        whatis = 0 == strcasecmp
                    768:                                (req->fields[i].val, "whatis");
1.13    ! kristaps  769:
        !           770:        if (NULL != opt.cat && 0 == strcmp(opt.cat, "0"))
        !           771:                opt.cat = NULL;
1.6       kristaps  772:
                    773:        /*
                    774:         * Poor man's tokenisation.
                    775:         * Just break apart by spaces.
                    776:         * Yes, this is half-ass.  But it works for now.
                    777:         */
                    778:
                    779:        while (ep && isspace((unsigned char)*ep))
                    780:                ep++;
                    781:
                    782:        while (ep && '\0' != *ep) {
                    783:                cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
                    784:                start = ep;
                    785:                while ('\0' != *ep && ! isspace((unsigned char)*ep))
                    786:                        ep++;
                    787:                cp[sz] = mandoc_malloc((ep - start) + 1);
                    788:                memcpy(cp[sz], start, ep - start);
                    789:                cp[sz++][ep - start] = '\0';
                    790:                while (isspace((unsigned char)*ep))
                    791:                        ep++;
                    792:        }
                    793:
                    794:        rc = -1;
                    795:
                    796:        /*
                    797:         * Pump down into apropos backend.
                    798:         * The resp_search() function is called with the results.
                    799:         */
                    800:
1.12      kristaps  801:        expr = whatis ? termcomp(sz, cp, &tt) :
                    802:                        exprcomp(sz, cp, &tt);
                    803:
                    804:        if (NULL != expr)
1.6       kristaps  805:                rc = apropos_search
                    806:                        (ps->sz, ps->paths, &opt,
                    807:                         expr, tt, (void *)req, resp_search);
                    808:
                    809:        /* ...unless errors occured. */
                    810:
                    811:        if (0 == rc)
                    812:                resp_baddb();
                    813:        else if (-1 == rc)
1.10      kristaps  814:                resp_search(NULL, 0, (void *)req);
1.6       kristaps  815:
                    816:        for (i = 0; i < sz; i++)
                    817:                free(cp[i]);
                    818:
                    819:        free(cp);
                    820:        exprfree(expr);
1.1       kristaps  821: }
                    822:
                    823: int
                    824: main(void)
                    825: {
                    826:        int              i;
                    827:        struct req       req;
1.6       kristaps  828:        char            *p, *path, *subpath;
                    829:        struct manpaths  paths;
                    830:
                    831:        /* HTTP init: read and parse the query string. */
                    832:
                    833:        progname = getenv("SCRIPT_NAME");
                    834:        if (NULL == progname)
                    835:                progname = "";
                    836:
1.7       kristaps  837:        cache = getenv("CACHE_DIR");
                    838:        if (NULL == cache)
                    839:                cache = "/cache/man.cgi";
                    840:
1.8       kristaps  841:        if (-1 == chdir(cache)) {
                    842:                resp_bad();
                    843:                return(EXIT_FAILURE);
1.7       kristaps  844:        }
                    845:
1.6       kristaps  846:        host = getenv("HTTP_HOST");
                    847:        if (NULL == host)
                    848:                host = "localhost";
1.1       kristaps  849:
                    850:        memset(&req, 0, sizeof(struct req));
                    851:
                    852:        if (NULL != (p = getenv("QUERY_STRING")))
                    853:                kval_parse(&req.fields, &req.fieldsz, p);
                    854:
1.6       kristaps  855:        /* Resolve leading subpath component. */
1.1       kristaps  856:
1.6       kristaps  857:        subpath = path = NULL;
1.1       kristaps  858:        req.page = PAGE__MAX;
                    859:
                    860:        if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
                    861:                req.page = PAGE_INDEX;
1.6       kristaps  862:
1.1       kristaps  863:        if (NULL != path && '/' == *path && '\0' == *++path)
                    864:                req.page = PAGE_INDEX;
                    865:
1.6       kristaps  866:        /* Strip file suffix. */
                    867:
                    868:        if (NULL != path && NULL != (p = strrchr(path, '.')))
                    869:                if (NULL != p && NULL == strchr(p, '/'))
                    870:                        *p++ = '\0';
                    871:
                    872:        /* Resolve subpath component. */
1.1       kristaps  873:
                    874:        if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6       kristaps  875:                *subpath++ = '\0';
1.1       kristaps  876:
1.6       kristaps  877:        /* Map path into one we recognise. */
1.1       kristaps  878:
                    879:        if (NULL != path && '\0' != *path)
                    880:                for (i = 0; i < (int)PAGE__MAX; i++)
                    881:                        if (0 == strcmp(pages[i], path)) {
                    882:                                req.page = (enum page)i;
                    883:                                break;
                    884:                        }
                    885:
1.6       kristaps  886:        /* Initialise MANPATH. */
                    887:
                    888:        memset(&paths, 0, sizeof(struct manpaths));
1.9       kristaps  889:        manpath_manconf("etc/catman.conf", &paths);
1.6       kristaps  890:
                    891:        /* Route pages. */
                    892:
1.1       kristaps  893:        switch (req.page) {
                    894:        case (PAGE_INDEX):
1.6       kristaps  895:                pg_index(&paths, &req, subpath);
1.1       kristaps  896:                break;
                    897:        case (PAGE_SEARCH):
1.6       kristaps  898:                pg_search(&paths, &req, subpath);
                    899:                break;
                    900:        case (PAGE_SHOW):
                    901:                pg_show(&paths, &req, subpath);
1.1       kristaps  902:                break;
                    903:        default:
1.10      kristaps  904:                resp_error404(path);
1.1       kristaps  905:                break;
                    906:        }
                    907:
1.6       kristaps  908:        manpath_free(&paths);
1.1       kristaps  909:        kval_free(req.fields, req.fieldsz);
1.6       kristaps  910:
1.1       kristaps  911:        return(EXIT_SUCCESS);
                    912: }

CVSweb