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

Annotation of mandoc/cgi.c, Revision 1.29

1.29    ! kristaps    1: /*     $Id: cgi.c,v 1.28 2011/12/10 23:09:25 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.24      kristaps   27: #include <dirent.h>
1.1       kristaps   28: #include <fcntl.h>
1.6       kristaps   29: #include <limits.h>
1.1       kristaps   30: #include <regex.h>
                     31: #include <stdio.h>
                     32: #include <stdarg.h>
1.5       kristaps   33: #include <stdint.h>
1.1       kristaps   34: #include <stdlib.h>
                     35: #include <string.h>
1.6       kristaps   36: #include <unistd.h>
1.1       kristaps   37:
1.6       kristaps   38: #include "apropos_db.h"
1.4       schwarze   39: #include "mandoc.h"
1.8       kristaps   40: #include "mdoc.h"
                     41: #include "man.h"
                     42: #include "main.h"
1.6       kristaps   43: #include "manpath.h"
                     44:
                     45: #ifdef __linux__
                     46: # include <db_185.h>
                     47: #else
                     48: # include <db.h>
                     49: #endif
1.1       kristaps   50:
                     51: enum   page {
                     52:        PAGE_INDEX,
                     53:        PAGE_SEARCH,
1.6       kristaps   54:        PAGE_SHOW,
1.1       kristaps   55:        PAGE__MAX
                     56: };
                     57:
1.24      kristaps   58: struct paths {
                     59:        char            *name;
                     60:        char            *path;
                     61: };
                     62:
1.20      kristaps   63: /*
                     64:  * A query as passed to the search function.
                     65:  */
                     66: struct query {
                     67:        const char      *arch; /* architecture */
                     68:        const char      *sec; /* manual section */
                     69:        const char      *expr; /* unparsed expression string */
1.26      kristaps   70:        int              manroot; /* manroot index (or -1)*/
1.20      kristaps   71:        int              whatis; /* whether whatis mode */
                     72:        int              legacy; /* whether legacy mode */
                     73: };
                     74:
1.1       kristaps   75: struct req {
1.24      kristaps   76:        struct query     q;
                     77:        struct paths    *p;
                     78:        size_t           psz;
1.1       kristaps   79:        enum page        page;
                     80: };
                     81:
1.6       kristaps   82: static int              atou(const char *, unsigned *);
1.9       kristaps   83: static void             catman(const char *);
1.15      kristaps   84: static int              cmp(const void *, const void *);
1.8       kristaps   85: static void             format(const char *);
1.6       kristaps   86: static void             html_print(const char *);
1.10      kristaps   87: static void             html_putchar(char);
1.24      kristaps   88: static int              http_decode(char *);
1.26      kristaps   89: static void             http_parse(struct req *, char *);
1.24      kristaps   90: static int              pathstop(DIR *);
                     91: static void             pathgen(DIR *, char *, struct req *);
                     92: static void             pg_index(const struct req *, char *);
                     93: static void             pg_search(const struct req *, char *);
                     94: static void             pg_show(const struct req *, char *);
1.7       kristaps   95: static void             resp_bad(void);
1.6       kristaps   96: static void             resp_baddb(void);
1.10      kristaps   97: static void             resp_error400(void);
                     98: static void             resp_error404(const char *);
1.6       kristaps   99: static void             resp_begin_html(int, const char *);
                    100: static void             resp_begin_http(int, const char *);
                    101: static void             resp_end_html(void);
                    102: static void             resp_index(const struct req *);
                    103: static void             resp_search(struct res *, size_t, void *);
                    104: static void             resp_searchform(const struct req *);
                    105:
1.29    ! kristaps  106: static const char       *progname; /* cgi script name */
        !           107: static const char       *cache; /* cache directory */
        !           108: static const char       *css; /* css directory */
        !           109: static const char       *host; /* hostname */
1.1       kristaps  110:
                    111: static const char * const pages[PAGE__MAX] = {
                    112:        "index", /* PAGE_INDEX */
                    113:        "search", /* PAGE_SEARCH */
1.6       kristaps  114:        "show", /* PAGE_SHOW */
1.1       kristaps  115: };
                    116:
1.6       kristaps  117: /*
                    118:  * This is just OpenBSD's strtol(3) suggestion.
                    119:  * I use it instead of strtonum(3) for portability's sake.
                    120:  */
                    121: static int
                    122: atou(const char *buf, unsigned *v)
                    123: {
                    124:        char            *ep;
                    125:        long             lval;
                    126:
                    127:        errno = 0;
                    128:        lval = strtol(buf, &ep, 10);
                    129:        if (buf[0] == '\0' || *ep != '\0')
                    130:                return(0);
                    131:        if ((errno == ERANGE && (lval == LONG_MAX ||
                    132:                                        lval == LONG_MIN)) ||
                    133:                        (lval > UINT_MAX || lval < 0))
                    134:                return(0);
                    135:
                    136:        *v = (unsigned int)lval;
                    137:        return(1);
                    138: }
1.1       kristaps  139:
1.20      kristaps  140: /*
                    141:  * Print a character, escaping HTML along the way.
                    142:  * This will pass non-ASCII straight to output: be warned!
                    143:  */
1.10      kristaps  144: static void
                    145: html_putchar(char c)
                    146: {
                    147:
                    148:        switch (c) {
                    149:        case ('"'):
                    150:                printf("&quote;");
                    151:                break;
                    152:        case ('&'):
                    153:                printf("&amp;");
                    154:                break;
                    155:        case ('>'):
                    156:                printf("&gt;");
                    157:                break;
                    158:        case ('<'):
                    159:                printf("&lt;");
                    160:                break;
                    161:        default:
                    162:                putchar((unsigned char)c);
                    163:                break;
                    164:        }
                    165: }
                    166:
1.6       kristaps  167: /*
1.20      kristaps  168:  * Call through to html_putchar().
                    169:  * Accepts NULL strings.
1.6       kristaps  170:  */
1.1       kristaps  171: static void
1.6       kristaps  172: html_print(const char *p)
1.1       kristaps  173: {
1.6       kristaps  174:
                    175:        if (NULL == p)
                    176:                return;
1.1       kristaps  177:        while ('\0' != *p)
1.10      kristaps  178:                html_putchar(*p++);
1.1       kristaps  179: }
                    180:
                    181: /*
                    182:  * Parse out key-value pairs from an HTTP request variable.
1.6       kristaps  183:  * This can be either a cookie or a POST/GET string, although man.cgi
                    184:  * uses only GET for simplicity.
1.1       kristaps  185:  */
                    186: static void
1.26      kristaps  187: http_parse(struct req *req, char *p)
1.1       kristaps  188: {
1.26      kristaps  189:        char            *key, *val, *manroot;
1.24      kristaps  190:        size_t           sz;
1.26      kristaps  191:        int              i, legacy;
1.1       kristaps  192:
1.26      kristaps  193:        memset(&req->q, 0, sizeof(struct query));
1.24      kristaps  194:
1.26      kristaps  195:        req->q.whatis = 1;
1.24      kristaps  196:        legacy = -1;
1.26      kristaps  197:        manroot = NULL;
1.1       kristaps  198:
                    199:        while (p && '\0' != *p) {
                    200:                while (' ' == *p)
                    201:                        p++;
                    202:
                    203:                key = p;
                    204:                val = NULL;
                    205:
                    206:                if (NULL != (p = strchr(p, '='))) {
                    207:                        *p++ = '\0';
                    208:                        val = p;
                    209:
                    210:                        sz = strcspn(p, ";&");
                    211:                        /* LINTED */
                    212:                        p += sz;
                    213:
                    214:                        if ('\0' != *p)
                    215:                                *p++ = '\0';
                    216:                } else {
                    217:                        p = key;
                    218:                        sz = strcspn(p, ";&");
                    219:                        /* LINTED */
                    220:                        p += sz;
                    221:
                    222:                        if ('\0' != *p)
                    223:                                p++;
                    224:                        continue;
                    225:                }
                    226:
                    227:                if ('\0' == *key || '\0' == *val)
                    228:                        continue;
                    229:
                    230:                /* Just abort handling. */
                    231:
1.24      kristaps  232:                if ( ! http_decode(key))
                    233:                        break;
                    234:                if ( ! http_decode(val))
                    235:                        break;
                    236:
                    237:                if (0 == strcmp(key, "expr"))
1.26      kristaps  238:                        req->q.expr = val;
1.24      kristaps  239:                else if (0 == strcmp(key, "query"))
1.26      kristaps  240:                        req->q.expr = val;
1.24      kristaps  241:                else if (0 == strcmp(key, "sec"))
1.26      kristaps  242:                        req->q.sec = val;
1.24      kristaps  243:                else if (0 == strcmp(key, "sektion"))
1.26      kristaps  244:                        req->q.sec = val;
1.24      kristaps  245:                else if (0 == strcmp(key, "arch"))
1.26      kristaps  246:                        req->q.arch = val;
                    247:                else if (0 == strcmp(key, "manpath"))
                    248:                        manroot = val;
1.24      kristaps  249:                else if (0 == strcmp(key, "apropos"))
                    250:                        legacy = 0 == strcmp(val, "0");
                    251:                else if (0 == strcmp(key, "op"))
1.26      kristaps  252:                        req->q.whatis = 0 == strcasecmp(val, "whatis");
1.24      kristaps  253:        }
1.1       kristaps  254:
1.24      kristaps  255:        /* Test for old man.cgi compatibility mode. */
1.1       kristaps  256:
1.24      kristaps  257:        if (legacy == 0) {
1.26      kristaps  258:                req->q.whatis = 0;
                    259:                req->q.legacy = 1;
1.24      kristaps  260:        } else if (legacy > 0) {
1.26      kristaps  261:                req->q.legacy = 1;
                    262:                req->q.whatis = 1;
1.1       kristaps  263:        }
1.24      kristaps  264:
                    265:        /*
                    266:         * Section "0" means no section when in legacy mode.
                    267:         * For some man.cgi scripts, "default" arch is none.
                    268:         */
                    269:
1.26      kristaps  270:        if (req->q.legacy && NULL != req->q.sec)
                    271:                if (0 == strcmp(req->q.sec, "0"))
                    272:                        req->q.sec = NULL;
                    273:        if (req->q.legacy && NULL != req->q.arch)
                    274:                if (0 == strcmp(req->q.arch, "default"))
                    275:                        req->q.arch = NULL;
                    276:
                    277:        /* Default to first manroot. */
                    278:
                    279:        if (NULL != manroot) {
                    280:                for (i = 0; i < (int)req->psz; i++)
                    281:                        if (0 == strcmp(req->p[i].name, manroot))
                    282:                                break;
                    283:                req->q.manroot = i < (int)req->psz ? i : -1;
                    284:        }
1.1       kristaps  285: }
                    286:
                    287: /*
1.6       kristaps  288:  * HTTP-decode a string.  The standard explanation is that this turns
                    289:  * "%4e+foo" into "n foo" in the regular way.  This is done in-place
                    290:  * over the allocated string.
1.1       kristaps  291:  */
                    292: static int
1.24      kristaps  293: http_decode(char *p)
1.1       kristaps  294: {
                    295:        char             hex[3];
                    296:        int              c;
                    297:
                    298:        hex[2] = '\0';
                    299:
                    300:        for ( ; '\0' != *p; p++) {
                    301:                if ('%' == *p) {
                    302:                        if ('\0' == (hex[0] = *(p + 1)))
                    303:                                return(0);
                    304:                        if ('\0' == (hex[1] = *(p + 2)))
                    305:                                return(0);
                    306:                        if (1 != sscanf(hex, "%x", &c))
                    307:                                return(0);
                    308:                        if ('\0' == c)
                    309:                                return(0);
                    310:
                    311:                        *p = (char)c;
                    312:                        memmove(p + 1, p + 3, strlen(p + 3) + 1);
                    313:                } else
                    314:                        *p = '+' == *p ? ' ' : *p;
                    315:        }
                    316:
                    317:        *p = '\0';
                    318:        return(1);
                    319: }
                    320:
1.6       kristaps  321: static void
                    322: resp_begin_http(int code, const char *msg)
                    323: {
                    324:
                    325:        if (200 != code)
                    326:                printf("Status: %d %s\n", code, msg);
                    327:
1.20      kristaps  328:        puts("Content-Type: text/html; charset=utf-8\n"
                    329:             "Cache-Control: no-cache\n"
                    330:             "Pragma: no-cache\n"
1.6       kristaps  331:             "");
                    332:
                    333:        fflush(stdout);
                    334: }
                    335:
                    336: static void
                    337: resp_begin_html(int code, const char *msg)
                    338: {
                    339:
                    340:        resp_begin_http(code, msg);
                    341:
1.29    ! kristaps  342:        printf("<!DOCTYPE HTML PUBLIC "
        !           343:               " \"-//W3C//DTD HTML 4.01//EN\""
        !           344:               " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
        !           345:               "<HTML>\n"
        !           346:               "<HEAD>\n"
        !           347:               "<META HTTP-EQUIV=\"Content-Type\""
        !           348:               " CONTENT=\"text/html; charset=utf-8\">\n"
        !           349:               "<LINK REL=\"stylesheet\" HREF=\"%s/man.cgi.css\""
        !           350:               " TYPE=\"text/css\" media=\"all\">\n"
        !           351:               "<TITLE>System Manpage Reference</TITLE>\n"
        !           352:               "</HEAD>\n"
        !           353:               "<BODY>\n"
        !           354:               "<!-- Begin page content. //-->\n", css);
1.6       kristaps  355: }
                    356:
                    357: static void
                    358: resp_end_html(void)
                    359: {
                    360:
1.20      kristaps  361:        puts("</BODY>\n"
                    362:             "</HTML>");
1.6       kristaps  363: }
                    364:
                    365: static void
                    366: resp_searchform(const struct req *req)
                    367: {
1.27      kristaps  368:        int              i;
1.13      kristaps  369:
1.6       kristaps  370:        puts("<!-- Begin search form. //-->");
1.29    ! kristaps  371:        printf("<FORM ACTION=\"%s/search.html\" METHOD=\"get\">\n"
        !           372:               "<FIELDSET>\n"
1.16      kristaps  373:               "<LEGEND>Search Parameters</LEGEND>\n"
1.20      kristaps  374:               "<INPUT TYPE=\"submit\" NAME=\"op\""
                    375:               " VALUE=\"Whatis\"> or \n"
                    376:               "<INPUT TYPE=\"submit\" NAME=\"op\""
                    377:               " VALUE=\"apropos\"> for manuals satisfying \n"
1.29    ! kristaps  378:               "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
        !           379:               progname);
1.24      kristaps  380:        html_print(req->q.expr ? req->q.expr : "");
1.14      kristaps  381:        printf("\">, section "
1.20      kristaps  382:               "<INPUT TYPE=\"text\""
                    383:               " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
1.24      kristaps  384:        html_print(req->q.sec ? req->q.sec : "");
1.14      kristaps  385:        printf("\">, arch "
1.20      kristaps  386:               "<INPUT TYPE=\"text\""
                    387:               " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
1.24      kristaps  388:        html_print(req->q.arch ? req->q.arch : "");
1.27      kristaps  389:        printf("\">");
                    390:        if (req->psz > 1) {
                    391:                puts(", <SELECT NAME=\"manpath\">");
                    392:                for (i = 0; i < (int)req->psz; i++) {
                    393:                        printf("<OPTION %s VALUE=\"",
                    394:                                (i == req->q.manroot) ||
                    395:                                (0 == i && -1 == req->q.manroot) ?
                    396:                                "SELECTED=\"selected\"" : "");
                    397:                        html_print(req->p[i].name);
                    398:                        printf("\">");
                    399:                        html_print(req->p[i].name);
                    400:                        puts("</OPTION>");
                    401:                }
                    402:                puts("</SELECT>");
                    403:        }
                    404:        puts(".\n"
1.12      kristaps  405:             "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
                    406:             "</FIELDSET>\n"
1.20      kristaps  407:             "</FORM>");
                    408:        puts("<!-- End search form. //-->");
1.6       kristaps  409: }
                    410:
                    411: static void
                    412: resp_index(const struct req *req)
                    413: {
                    414:
                    415:        resp_begin_html(200, NULL);
                    416:        resp_searchform(req);
                    417:        resp_end_html();
                    418: }
                    419:
                    420: static void
1.10      kristaps  421: resp_error400(void)
1.9       kristaps  422: {
                    423:
1.10      kristaps  424:        resp_begin_html(400, "Query Malformed");
1.12      kristaps  425:        printf("<H1>Malformed Query</H1>\n"
                    426:               "<P>\n"
1.20      kristaps  427:               "The query your entered was malformed.\n"
                    428:               "Try again from the\n"
1.22      kristaps  429:               "<A HREF=\"%s/index.html\">main page</A>.\n"
1.12      kristaps  430:               "</P>", progname);
1.9       kristaps  431:        resp_end_html();
                    432: }
                    433:
                    434: static void
1.10      kristaps  435: resp_error404(const char *page)
1.6       kristaps  436: {
                    437:
                    438:        resp_begin_html(404, "Not Found");
1.10      kristaps  439:        puts("<H1>Page Not Found</H1>\n"
                    440:             "<P>\n"
1.20      kristaps  441:             "The page you're looking for, ");
                    442:        printf("<B>");
1.10      kristaps  443:        html_print(page);
1.12      kristaps  444:        printf("</B>,\n"
1.20      kristaps  445:               "could not be found.\n"
                    446:               "Try searching from the\n"
1.22      kristaps  447:               "<A HREF=\"%s/index.html\">main page</A>.\n"
1.12      kristaps  448:               "</P>", progname);
1.6       kristaps  449:        resp_end_html();
                    450: }
1.1       kristaps  451:
                    452: static void
1.7       kristaps  453: resp_bad(void)
                    454: {
                    455:        resp_begin_html(500, "Internal Server Error");
                    456:        puts("<P>Generic badness happened.</P>");
                    457:        resp_end_html();
                    458: }
                    459:
                    460: static void
1.6       kristaps  461: resp_baddb(void)
1.1       kristaps  462: {
                    463:
1.6       kristaps  464:        resp_begin_html(500, "Internal Server Error");
                    465:        puts("<P>Your database is broken.</P>");
                    466:        resp_end_html();
1.1       kristaps  467: }
                    468:
                    469: static void
1.6       kristaps  470: resp_search(struct res *r, size_t sz, void *arg)
1.1       kristaps  471: {
1.20      kristaps  472:        int               i;
1.19      kristaps  473:        const struct req *req;
                    474:
1.29    ! kristaps  475:        req = (const struct req *)arg;
        !           476:        assert(req->q.manroot >= 0);
        !           477:
1.6       kristaps  478:        if (1 == sz) {
                    479:                /*
                    480:                 * If we have just one result, then jump there now
                    481:                 * without any delay.
                    482:                 */
                    483:                puts("Status: 303 See Other");
1.29    ! kristaps  484:                printf("Location: http://%s%s/show/%d/%u/%u.html\n",
        !           485:                                host, progname, req->q.manroot,
1.6       kristaps  486:                                r[0].volume, r[0].rec);
                    487:                puts("Content-Type: text/html; charset=utf-8\n");
                    488:                return;
                    489:        }
                    490:
1.15      kristaps  491:        qsort(r, sz, sizeof(struct res), cmp);
                    492:
1.12      kristaps  493:        resp_begin_html(200, NULL);
1.19      kristaps  494:        resp_searchform(req);
1.6       kristaps  495:
1.14      kristaps  496:        if (0 == sz) {
1.21      kristaps  497:                printf("<P>\n"
1.24      kristaps  498:                       "No %s results found.\n",
                    499:                       req->q.whatis ? "whatis" : "apropos");
                    500:                if (req->q.whatis) {
1.29    ! kristaps  501:                        printf("(Try <A HREF=\"%s/search.html?"
        !           502:                               "op=apropos&amp;expr=", progname);
1.24      kristaps  503:                        html_print(req->q.expr ? req->q.expr : "");
1.19      kristaps  504:                        printf("&amp;sec=");
1.24      kristaps  505:                        html_print(req->q.sec ? req->q.sec : "");
1.19      kristaps  506:                        printf("&amp;arch=");
1.24      kristaps  507:                        html_print(req->q.arch ? req->q.arch : "");
1.19      kristaps  508:                        puts("\">apropos</A>?)");
                    509:                }
                    510:                puts("</P>");
1.14      kristaps  511:                resp_end_html();
                    512:                return;
                    513:        }
                    514:
                    515:        puts("<P></P>\n"
                    516:             "<TABLE>");
1.1       kristaps  517:
                    518:        for (i = 0; i < (int)sz; i++) {
1.20      kristaps  519:                printf("<TR>\n"
                    520:                       "<TD CLASS=\"title\">\n"
1.29    ! kristaps  521:                       "<A HREF=\"%s/show/%d/%u/%u.html\">",
        !           522:                                progname, req->q.manroot,
1.26      kristaps  523:                                r[i].volume, r[i].rec);
1.15      kristaps  524:                html_print(r[i].title);
1.1       kristaps  525:                putchar('(');
1.6       kristaps  526:                html_print(r[i].cat);
                    527:                if (r[i].arch && '\0' != *r[i].arch) {
                    528:                        putchar('/');
                    529:                        html_print(r[i].arch);
                    530:                }
1.20      kristaps  531:                printf(")</A>\n"
                    532:                       "</TD>\n"
                    533:                       "<TD CLASS=\"desc\">");
1.6       kristaps  534:                html_print(r[i].desc);
1.20      kristaps  535:                puts("</TD>\n"
                    536:                     "</TR>");
1.1       kristaps  537:        }
1.16      kristaps  538:
                    539:        puts("</TABLE>");
1.6       kristaps  540:        resp_end_html();
                    541: }
                    542:
                    543: /* ARGSUSED */
                    544: static void
1.24      kristaps  545: pg_index(const struct req *req, char *path)
1.6       kristaps  546: {
                    547:
                    548:        resp_index(req);
1.1       kristaps  549: }
                    550:
                    551: static void
1.9       kristaps  552: catman(const char *file)
                    553: {
1.10      kristaps  554:        FILE            *f;
                    555:        size_t           len;
                    556:        int              i;
                    557:        char            *p;
                    558:        int              italic, bold;
1.9       kristaps  559:
1.10      kristaps  560:        if (NULL == (f = fopen(file, "r"))) {
1.9       kristaps  561:                resp_baddb();
                    562:                return;
                    563:        }
                    564:
1.12      kristaps  565:        resp_begin_http(200, NULL);
1.29    ! kristaps  566:        printf("<!DOCTYPE HTML PUBLIC "
        !           567:               " \"-//W3C//DTD HTML 4.01//EN\""
        !           568:               " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
        !           569:               "<HTML>\n"
        !           570:               "<HEAD>\n"
        !           571:               "<META HTTP-EQUIV=\"Content-Type\""
        !           572:               " CONTENT=\"text/html; charset=utf-8\">\n"
        !           573:               "<LINK REL=\"stylesheet\" HREF=\"%s/catman.css\""
        !           574:               " TYPE=\"text/css\" media=\"all\">\n"
        !           575:               "<TITLE>System Manpage Reference</TITLE>\n"
        !           576:               "</HEAD>\n"
        !           577:               "<BODY>\n"
        !           578:               "<!-- Begin page content. //-->\n"
        !           579:               "<PRE>\n", css);
1.10      kristaps  580:
                    581:        while (NULL != (p = fgetln(f, &len))) {
                    582:                bold = italic = 0;
                    583:                for (i = 0; i < (int)len - 1; i++) {
                    584:                        /*
                    585:                         * This means that the catpage is out of state.
                    586:                         * Ignore it and keep going (although the
                    587:                         * catpage is bogus).
                    588:                         */
                    589:
                    590:                        if ('\b' == p[i] || '\n' == p[i])
                    591:                                continue;
                    592:
                    593:                        /*
                    594:                         * Print a regular character.
                    595:                         * Close out any bold/italic scopes.
                    596:                         * If we're in back-space mode, make sure we'll
                    597:                         * have something to enter when we backspace.
                    598:                         */
                    599:
                    600:                        if ('\b' != p[i + 1]) {
                    601:                                if (italic)
                    602:                                        printf("</I>");
                    603:                                if (bold)
                    604:                                        printf("</B>");
                    605:                                italic = bold = 0;
                    606:                                html_putchar(p[i]);
                    607:                                continue;
                    608:                        } else if (i + 2 >= (int)len)
                    609:                                continue;
                    610:
                    611:                        /* Italic mode. */
                    612:
                    613:                        if ('_' == p[i]) {
                    614:                                if (bold)
                    615:                                        printf("</B>");
                    616:                                if ( ! italic)
                    617:                                        printf("<I>");
                    618:                                bold = 0;
                    619:                                italic = 1;
                    620:                                i += 2;
                    621:                                html_putchar(p[i]);
                    622:                                continue;
                    623:                        }
                    624:
                    625:                        /*
                    626:                         * Handle funny behaviour troff-isms.
                    627:                         * These grok'd from the original man2html.c.
                    628:                         */
                    629:
                    630:                        if (('+' == p[i] && 'o' == p[i + 2]) ||
                    631:                                        ('o' == p[i] && '+' == p[i + 2]) ||
                    632:                                        ('|' == p[i] && '=' == p[i + 2]) ||
                    633:                                        ('=' == p[i] && '|' == p[i + 2]) ||
                    634:                                        ('*' == p[i] && '=' == p[i + 2]) ||
                    635:                                        ('=' == p[i] && '*' == p[i + 2]) ||
                    636:                                        ('*' == p[i] && '|' == p[i + 2]) ||
                    637:                                        ('|' == p[i] && '*' == p[i + 2]))  {
                    638:                                if (italic)
                    639:                                        printf("</I>");
                    640:                                if (bold)
                    641:                                        printf("</B>");
                    642:                                italic = bold = 0;
                    643:                                putchar('*');
                    644:                                i += 2;
                    645:                                continue;
                    646:                        } else if (('|' == p[i] && '-' == p[i + 2]) ||
                    647:                                        ('-' == p[i] && '|' == p[i + 1]) ||
                    648:                                        ('+' == p[i] && '-' == p[i + 1]) ||
                    649:                                        ('-' == p[i] && '+' == p[i + 1]) ||
                    650:                                        ('+' == p[i] && '|' == p[i + 1]) ||
                    651:                                        ('|' == p[i] && '+' == p[i + 1]))  {
                    652:                                if (italic)
                    653:                                        printf("</I>");
                    654:                                if (bold)
                    655:                                        printf("</B>");
                    656:                                italic = bold = 0;
                    657:                                putchar('+');
                    658:                                i += 2;
                    659:                                continue;
                    660:                        }
                    661:
                    662:                        /* Bold mode. */
                    663:
                    664:                        if (italic)
                    665:                                printf("</I>");
                    666:                        if ( ! bold)
                    667:                                printf("<B>");
                    668:                        bold = 1;
                    669:                        italic = 0;
                    670:                        i += 2;
                    671:                        html_putchar(p[i]);
                    672:                }
                    673:
                    674:                /*
                    675:                 * Clean up the last character.
                    676:                 * We can get to a newline; don't print that.
                    677:                 */
1.9       kristaps  678:
1.10      kristaps  679:                if (italic)
                    680:                        printf("</I>");
                    681:                if (bold)
                    682:                        printf("</B>");
1.9       kristaps  683:
1.10      kristaps  684:                if (i == (int)len - 1 && '\n' != p[i])
                    685:                        html_putchar(p[i]);
1.9       kristaps  686:
1.10      kristaps  687:                putchar('\n');
                    688:        }
                    689:
                    690:        puts("</PRE>\n"
                    691:             "</BODY>\n"
                    692:             "</HTML>");
                    693:
                    694:        fclose(f);
1.9       kristaps  695: }
                    696:
                    697: static void
1.8       kristaps  698: format(const char *file)
1.7       kristaps  699: {
1.8       kristaps  700:        struct mparse   *mp;
                    701:        int              fd;
                    702:        struct mdoc     *mdoc;
                    703:        struct man      *man;
                    704:        void            *vp;
                    705:        enum mandoclevel rc;
1.10      kristaps  706:        char             opts[MAXPATHLEN + 128];
1.7       kristaps  707:
1.8       kristaps  708:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    709:                resp_baddb();
1.7       kristaps  710:                return;
                    711:        }
                    712:
1.8       kristaps  713:        mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
                    714:        rc = mparse_readfd(mp, fd, file);
                    715:        close(fd);
1.7       kristaps  716:
1.8       kristaps  717:        if (rc >= MANDOCLEVEL_FATAL) {
1.7       kristaps  718:                resp_baddb();
                    719:                return;
                    720:        }
                    721:
1.29    ! kristaps  722:        snprintf(opts, sizeof(opts), "style=%s/man.css,"
1.10      kristaps  723:                        "man=%s/search.html?sec=%%S&expr=%%N,"
1.11      kristaps  724:                        /*"includes=/cgi-bin/man.cgi/usr/include/%%I"*/,
1.29    ! kristaps  725:                        css, progname);
1.10      kristaps  726:
1.8       kristaps  727:        mparse_result(mp, &mdoc, &man);
1.10      kristaps  728:        vp = html_alloc(opts);
1.7       kristaps  729:
1.8       kristaps  730:        if (NULL != mdoc) {
                    731:                resp_begin_http(200, NULL);
                    732:                html_mdoc(vp, mdoc);
                    733:        } else if (NULL != man) {
                    734:                resp_begin_http(200, NULL);
                    735:                html_man(vp, man);
                    736:        } else
                    737:                resp_baddb();
1.7       kristaps  738:
1.8       kristaps  739:        html_free(vp);
                    740:        mparse_free(mp);
1.7       kristaps  741: }
                    742:
                    743: static void
1.24      kristaps  744: pg_show(const struct req *req, char *path)
1.1       kristaps  745: {
1.24      kristaps  746:        struct manpaths  ps;
1.6       kristaps  747:        char            *sub;
1.7       kristaps  748:        char             file[MAXPATHLEN];
1.9       kristaps  749:        const char      *fn, *cp;
1.6       kristaps  750:        int              rc;
1.25      kristaps  751:        unsigned int     vol, rec, mr;
1.9       kristaps  752:        DB              *idx;
1.6       kristaps  753:        DBT              key, val;
                    754:
1.24      kristaps  755:        idx = NULL;
                    756:
1.25      kristaps  757:        /* Parse out mroot, volume, and record from the path. */
                    758:
                    759:        if (NULL == path || NULL == (sub = strchr(path, '/'))) {
                    760:                resp_error400();
                    761:                return;
                    762:        }
                    763:        *sub++ = '\0';
                    764:        if ( ! atou(path, &mr)) {
                    765:                resp_error400();
                    766:                return;
                    767:        }
                    768:        path = sub;
                    769:        if (NULL == (sub = strchr(path, '/'))) {
                    770:                resp_error400();
                    771:                return;
                    772:        }
                    773:        *sub++ = '\0';
                    774:        if ( ! atou(path, &vol) || ! atou(sub, &rec)) {
1.10      kristaps  775:                resp_error400();
1.6       kristaps  776:                return;
1.25      kristaps  777:        } else if (mr >= (unsigned int)req->psz) {
1.10      kristaps  778:                resp_error400();
1.6       kristaps  779:                return;
1.25      kristaps  780:        }
1.6       kristaps  781:
1.24      kristaps  782:        /*
1.26      kristaps  783:         * Begin by chdir()ing into the manroot.
1.24      kristaps  784:         * This way we can pick up the database files, which are
                    785:         * relative to the manpath root.
                    786:         */
                    787:
1.25      kristaps  788:        if (-1 == chdir(req->p[(int)mr].path)) {
                    789:                perror(req->p[(int)mr].path);
                    790:                resp_baddb();
1.24      kristaps  791:                return;
                    792:        }
                    793:
                    794:        memset(&ps, 0, sizeof(struct manpaths));
                    795:        manpath_manconf("etc/catman.conf", &ps);
                    796:
1.25      kristaps  797:        if (vol >= (unsigned int)ps.sz) {
1.10      kristaps  798:                resp_error400();
1.24      kristaps  799:                goto out;
1.6       kristaps  800:        }
                    801:
1.24      kristaps  802:        strlcpy(file, ps.paths[vol], MAXPATHLEN);
1.6       kristaps  803:        strlcat(file, "/mandoc.index", MAXPATHLEN);
                    804:
                    805:        /* Open the index recno(3) database. */
                    806:
1.9       kristaps  807:        idx = dbopen(file, O_RDONLY, 0, DB_RECNO, NULL);
                    808:        if (NULL == idx) {
1.24      kristaps  809:                perror(file);
1.6       kristaps  810:                resp_baddb();
1.24      kristaps  811:                goto out;
1.6       kristaps  812:        }
                    813:
                    814:        key.data = &rec;
                    815:        key.size = 4;
                    816:
1.9       kristaps  817:        if (0 != (rc = (*idx->get)(idx, &key, &val, 0))) {
1.10      kristaps  818:                rc < 0 ? resp_baddb() : resp_error400();
1.9       kristaps  819:                goto out;
1.6       kristaps  820:        }
                    821:
1.9       kristaps  822:        cp = (char *)val.data;
1.6       kristaps  823:
1.9       kristaps  824:        if (NULL == (fn = memchr(cp, '\0', val.size)))
                    825:                resp_baddb();
                    826:        else if (++fn - cp >= (int)val.size)
                    827:                resp_baddb();
                    828:        else if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
                    829:                resp_baddb();
                    830:        else {
                    831:                if (0 == strcmp(cp, "cat"))
1.24      kristaps  832:                        catman(fn + 1);
1.9       kristaps  833:                else
1.24      kristaps  834:                        format(fn + 1);
1.9       kristaps  835:        }
                    836: out:
1.24      kristaps  837:        if (idx)
                    838:                (*idx->close)(idx);
                    839:        manpath_free(&ps);
1.6       kristaps  840: }
                    841:
                    842: static void
1.24      kristaps  843: pg_search(const struct req *req, char *path)
1.6       kristaps  844: {
                    845:        size_t            tt;
1.24      kristaps  846:        struct manpaths   ps;
1.20      kristaps  847:        int               i, sz, rc;
1.6       kristaps  848:        const char       *ep, *start;
                    849:        char            **cp;
                    850:        struct opts       opt;
                    851:        struct expr      *expr;
                    852:
1.28      kristaps  853:        if (req->q.manroot < 0 || 0 == req->psz) {
1.24      kristaps  854:                resp_search(NULL, 0, (void *)req);
                    855:                return;
                    856:        }
                    857:
1.1       kristaps  858:        memset(&opt, 0, sizeof(struct opts));
1.6       kristaps  859:
1.24      kristaps  860:        ep       = req->q.expr;
                    861:        opt.arch = req->q.arch;
                    862:        opt.cat  = req->q.sec;
1.20      kristaps  863:        rc       = -1;
                    864:        sz       = 0;
                    865:        cp       = NULL;
1.6       kristaps  866:
                    867:        /*
1.24      kristaps  868:         * Begin by chdir()ing into the root of the manpath.
                    869:         * This way we can pick up the database files, which are
                    870:         * relative to the manpath root.
                    871:         */
                    872:
1.26      kristaps  873:        assert(req->q.manroot < (int)req->psz);
                    874:        if (-1 == (chdir(req->p[req->q.manroot].path))) {
                    875:                perror(req->p[req->q.manroot].path);
1.24      kristaps  876:                resp_search(NULL, 0, (void *)req);
                    877:                return;
                    878:        }
                    879:
                    880:        memset(&ps, 0, sizeof(struct manpaths));
                    881:        manpath_manconf("etc/catman.conf", &ps);
                    882:
                    883:        /*
                    884:         * Poor man's tokenisation: just break apart by spaces.
1.6       kristaps  885:         * Yes, this is half-ass.  But it works for now.
                    886:         */
                    887:
                    888:        while (ep && isspace((unsigned char)*ep))
                    889:                ep++;
                    890:
                    891:        while (ep && '\0' != *ep) {
                    892:                cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *));
                    893:                start = ep;
                    894:                while ('\0' != *ep && ! isspace((unsigned char)*ep))
                    895:                        ep++;
                    896:                cp[sz] = mandoc_malloc((ep - start) + 1);
                    897:                memcpy(cp[sz], start, ep - start);
                    898:                cp[sz++][ep - start] = '\0';
                    899:                while (isspace((unsigned char)*ep))
                    900:                        ep++;
                    901:        }
                    902:
                    903:        /*
                    904:         * Pump down into apropos backend.
                    905:         * The resp_search() function is called with the results.
                    906:         */
                    907:
1.24      kristaps  908:        expr = req->q.whatis ?
                    909:                termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt);
1.12      kristaps  910:
                    911:        if (NULL != expr)
1.6       kristaps  912:                rc = apropos_search
1.24      kristaps  913:                        (ps.sz, ps.paths, &opt,
1.6       kristaps  914:                         expr, tt, (void *)req, resp_search);
                    915:
                    916:        /* ...unless errors occured. */
                    917:
                    918:        if (0 == rc)
                    919:                resp_baddb();
                    920:        else if (-1 == rc)
1.10      kristaps  921:                resp_search(NULL, 0, (void *)req);
1.6       kristaps  922:
                    923:        for (i = 0; i < sz; i++)
                    924:                free(cp[i]);
                    925:
                    926:        free(cp);
                    927:        exprfree(expr);
1.24      kristaps  928:        manpath_free(&ps);
1.1       kristaps  929: }
                    930:
                    931: int
                    932: main(void)
                    933: {
                    934:        int              i;
1.24      kristaps  935:        char             buf[MAXPATHLEN];
                    936:        DIR             *cwd;
1.1       kristaps  937:        struct req       req;
1.6       kristaps  938:        char            *p, *path, *subpath;
                    939:
1.24      kristaps  940:        /* Scan our run-time environment. */
1.6       kristaps  941:
1.29    ! kristaps  942:        if (NULL == (cache = getenv("CACHE_DIR")))
        !           943:                cache = "/cache/man.cgi";
        !           944:
        !           945:        if (NULL == (progname = getenv("SCRIPT_NAME")))
1.6       kristaps  946:                progname = "";
                    947:
1.29    ! kristaps  948:        if (NULL == (css = getenv("CSS_DIR")))
        !           949:                css = "/";
1.7       kristaps  950:
1.29    ! kristaps  951:        if (NULL == (host = getenv("HTTP_HOST")))
1.24      kristaps  952:                host = "localhost";
                    953:
                    954:        /*
                    955:         * First we change directory into the cache directory so that
                    956:         * subsequent scanning for manpath directories is rooted
                    957:         * relative to the same position.
                    958:         */
                    959:
1.8       kristaps  960:        if (-1 == chdir(cache)) {
1.24      kristaps  961:                perror(cache);
                    962:                resp_bad();
                    963:                return(EXIT_FAILURE);
                    964:        } else if (NULL == (cwd = opendir(cache))) {
                    965:                perror(cache);
1.8       kristaps  966:                resp_bad();
                    967:                return(EXIT_FAILURE);
1.24      kristaps  968:        }
                    969:
                    970:        memset(&req, 0, sizeof(struct req));
1.7       kristaps  971:
1.24      kristaps  972:        strlcpy(buf, ".", MAXPATHLEN);
                    973:        pathgen(cwd, buf, &req);
                    974:        closedir(cwd);
1.1       kristaps  975:
1.24      kristaps  976:        /* Next parse out the query string. */
1.1       kristaps  977:
                    978:        if (NULL != (p = getenv("QUERY_STRING")))
1.26      kristaps  979:                http_parse(&req, p);
1.1       kristaps  980:
1.24      kristaps  981:        /*
                    982:         * Now juggle paths to extract information.
                    983:         * We want to extract our filetype (the file suffix), the
                    984:         * initial path component, then the trailing component(s).
                    985:         * Start with leading subpath component.
                    986:         */
1.1       kristaps  987:
1.6       kristaps  988:        subpath = path = NULL;
1.1       kristaps  989:        req.page = PAGE__MAX;
                    990:
                    991:        if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
                    992:                req.page = PAGE_INDEX;
1.6       kristaps  993:
1.1       kristaps  994:        if (NULL != path && '/' == *path && '\0' == *++path)
                    995:                req.page = PAGE_INDEX;
                    996:
1.6       kristaps  997:        /* Strip file suffix. */
                    998:
                    999:        if (NULL != path && NULL != (p = strrchr(path, '.')))
                   1000:                if (NULL != p && NULL == strchr(p, '/'))
                   1001:                        *p++ = '\0';
                   1002:
                   1003:        /* Resolve subpath component. */
1.1       kristaps 1004:
                   1005:        if (NULL != path && NULL != (subpath = strchr(path, '/')))
1.6       kristaps 1006:                *subpath++ = '\0';
1.1       kristaps 1007:
1.6       kristaps 1008:        /* Map path into one we recognise. */
1.1       kristaps 1009:
                   1010:        if (NULL != path && '\0' != *path)
                   1011:                for (i = 0; i < (int)PAGE__MAX; i++)
                   1012:                        if (0 == strcmp(pages[i], path)) {
                   1013:                                req.page = (enum page)i;
                   1014:                                break;
                   1015:                        }
                   1016:
1.6       kristaps 1017:        /* Route pages. */
                   1018:
1.1       kristaps 1019:        switch (req.page) {
                   1020:        case (PAGE_INDEX):
1.24      kristaps 1021:                pg_index(&req, subpath);
1.1       kristaps 1022:                break;
                   1023:        case (PAGE_SEARCH):
1.24      kristaps 1024:                pg_search(&req, subpath);
1.6       kristaps 1025:                break;
                   1026:        case (PAGE_SHOW):
1.24      kristaps 1027:                pg_show(&req, subpath);
1.1       kristaps 1028:                break;
                   1029:        default:
1.10      kristaps 1030:                resp_error404(path);
1.1       kristaps 1031:                break;
                   1032:        }
                   1033:
1.24      kristaps 1034:        for (i = 0; i < (int)req.psz; i++) {
                   1035:                free(req.p[i].path);
                   1036:                free(req.p[i].name);
                   1037:        }
1.6       kristaps 1038:
1.24      kristaps 1039:        free(req.p);
1.1       kristaps 1040:        return(EXIT_SUCCESS);
                   1041: }
1.15      kristaps 1042:
                   1043: static int
                   1044: cmp(const void *p1, const void *p2)
                   1045: {
                   1046:
                   1047:        return(strcasecmp(((const struct res *)p1)->title,
                   1048:                                ((const struct res *)p2)->title));
                   1049: }
                   1050:
1.24      kristaps 1051: /*
                   1052:  * Check to see if an "etc" path consists of a catman.conf file.  If it
                   1053:  * does, that means that the path contains a tree created by catman(8)
                   1054:  * and should be used for indexing.
                   1055:  */
                   1056: static int
                   1057: pathstop(DIR *dir)
                   1058: {
                   1059:        struct dirent   *d;
                   1060:
                   1061:        while (NULL != (d = readdir(dir)))
                   1062:                if (DT_REG == d->d_type)
                   1063:                        if (0 == strcmp(d->d_name, "catman.conf"))
                   1064:                                return(1);
                   1065:
                   1066:        return(0);
                   1067: }
                   1068:
                   1069: /*
                   1070:  * Scan for indexable paths.
                   1071:  * This adds all paths with "etc/catman.conf" to the buffer.
                   1072:  */
                   1073: static void
                   1074: pathgen(DIR *dir, char *path, struct req *req)
                   1075: {
                   1076:        struct dirent   *d;
                   1077:        char            *cp;
                   1078:        DIR             *cd;
                   1079:        int              rc;
                   1080:        size_t           sz, ssz;
                   1081:
                   1082:        sz = strlcat(path, "/", MAXPATHLEN);
                   1083:        if (sz >= MAXPATHLEN) {
                   1084:                fprintf(stderr, "%s: Path too long", path);
                   1085:                return;
                   1086:        }
                   1087:
                   1088:        /*
                   1089:         * First, scan for the "etc" directory.
                   1090:         * If it's found, then see if it should cause us to stop.  This
                   1091:         * happens when a catman.conf is found in the directory.
                   1092:         */
                   1093:
                   1094:        rc = 0;
                   1095:        while (0 == rc && NULL != (d = readdir(dir))) {
                   1096:                if (DT_DIR != d->d_type || strcmp(d->d_name, "etc"))
                   1097:                        continue;
                   1098:
                   1099:                path[(int)sz] = '\0';
                   1100:                ssz = strlcat(path, d->d_name, MAXPATHLEN);
                   1101:
                   1102:                if (ssz >= MAXPATHLEN) {
                   1103:                        fprintf(stderr, "%s: Path too long", path);
                   1104:                        return;
                   1105:                } else if (NULL == (cd = opendir(path))) {
                   1106:                        perror(path);
                   1107:                        return;
                   1108:                }
                   1109:
                   1110:                rc = pathstop(cd);
                   1111:                closedir(cd);
                   1112:        }
                   1113:
                   1114:        if (rc > 0) {
                   1115:                /* This also strips the trailing slash. */
1.27      kristaps 1116:                path[(int)--sz] = '\0';
1.24      kristaps 1117:                req->p = mandoc_realloc
                   1118:                        (req->p,
                   1119:                         (req->psz + 1) * sizeof(struct paths));
1.27      kristaps 1120:                /*
                   1121:                 * Strip out the leading "./" unless we're just a ".",
                   1122:                 * in which case use an empty string as our name.
                   1123:                 */
1.24      kristaps 1124:                req->p[(int)req->psz].path = mandoc_strdup(path);
                   1125:                req->p[(int)req->psz].name =
1.27      kristaps 1126:                        cp = mandoc_strdup(path + (1 == sz ? 1 : 2));
1.24      kristaps 1127:                req->psz++;
                   1128:                /*
                   1129:                 * The name is just the path with all the slashes taken
                   1130:                 * out of it.  Simple but effective.
                   1131:                 */
                   1132:                for ( ; '\0' != *cp; cp++)
                   1133:                        if ('/' == *cp)
                   1134:                                *cp = ' ';
                   1135:                return;
                   1136:        }
                   1137:
                   1138:        /*
                   1139:         * If no etc/catman.conf was found, recursively enter child
                   1140:         * directory and continue scanning.
                   1141:         */
                   1142:
                   1143:        rewinddir(dir);
                   1144:        while (NULL != (d = readdir(dir))) {
                   1145:                if (DT_DIR != d->d_type || '.' == d->d_name[0])
                   1146:                        continue;
                   1147:
                   1148:                path[(int)sz] = '\0';
                   1149:                ssz = strlcat(path, d->d_name, MAXPATHLEN);
                   1150:
                   1151:                if (ssz >= MAXPATHLEN) {
                   1152:                        fprintf(stderr, "%s: Path too long", path);
                   1153:                        return;
                   1154:                } else if (NULL == (cd = opendir(path))) {
                   1155:                        perror(path);
                   1156:                        return;
                   1157:                }
                   1158:
                   1159:                pathgen(cd, path, req);
                   1160:                closedir(cd);
                   1161:        }
                   1162: }

CVSweb