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

Annotation of mandoc/cgi.c, Revision 1.54

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

CVSweb