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

Annotation of mandoc/cgi.c, Revision 1.52

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

CVSweb