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

Annotation of mandoc/man_validate.c, Revision 1.68

1.68    ! kristaps    1: /*     $Id: man_validate.c,v 1.67 2011/03/22 15:30:30 kristaps Exp $ */
1.1       kristaps    2: /*
1.63      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.8       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.8       kristaps   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.
1.1       kristaps   17:  */
1.28      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.1       kristaps   22: #include <sys/types.h>
                     23:
                     24: #include <assert.h>
                     25: #include <ctype.h>
1.16      kristaps   26: #include <errno.h>
                     27: #include <limits.h>
1.1       kristaps   28: #include <stdarg.h>
                     29: #include <stdlib.h>
1.46      kristaps   30: #include <string.h>
1.50      kristaps   31: #include <time.h>
1.1       kristaps   32:
1.66      kristaps   33: #include "man.h"
1.41      kristaps   34: #include "mandoc.h"
1.1       kristaps   35: #include "libman.h"
1.15      kristaps   36: #include "libmandoc.h"
1.1       kristaps   37:
1.43      kristaps   38: #define        CHKARGS   struct man *m, struct man_node *n
1.1       kristaps   39:
1.17      kristaps   40: typedef        int     (*v_check)(CHKARGS);
1.1       kristaps   41:
                     42: struct man_valid {
1.17      kristaps   43:        v_check  *pres;
                     44:        v_check  *posts;
1.1       kristaps   45: };
                     46:
1.17      kristaps   47: static int       check_bline(CHKARGS);
                     48: static int       check_eq0(CHKARGS);
1.55      kristaps   49: static int       check_ft(CHKARGS);
1.25      kristaps   50: static int       check_le1(CHKARGS);
1.17      kristaps   51: static int       check_ge2(CHKARGS);
                     52: static int       check_le5(CHKARGS);
                     53: static int       check_par(CHKARGS);
1.23      kristaps   54: static int       check_part(CHKARGS);
1.17      kristaps   55: static int       check_root(CHKARGS);
                     56: static int       check_sec(CHKARGS);
1.68    ! kristaps   57: static void      check_text(CHKARGS);
1.17      kristaps   58:
1.51      kristaps   59: static int       post_AT(CHKARGS);
                     60: static int       post_fi(CHKARGS);
                     61: static int       post_nf(CHKARGS);
                     62: static int       post_TH(CHKARGS);
                     63: static int       post_UC(CHKARGS);
                     64:
                     65: static v_check   posts_at[] = { post_AT, NULL };
1.17      kristaps   66: static v_check   posts_eq0[] = { check_eq0, NULL };
1.51      kristaps   67: static v_check   posts_fi[] = { check_eq0, post_fi, NULL };
                     68: static v_check   posts_le1[] = { check_le1, NULL };
1.55      kristaps   69: static v_check   posts_ft[] = { check_ft, NULL };
1.51      kristaps   70: static v_check   posts_nf[] = { check_eq0, post_nf, NULL };
1.17      kristaps   71: static v_check   posts_par[] = { check_par, NULL };
1.23      kristaps   72: static v_check   posts_part[] = { check_part, NULL };
1.17      kristaps   73: static v_check   posts_sec[] = { check_sec, NULL };
1.60      schwarze   74: static v_check   posts_th[] = { check_ge2, check_le5, post_TH, NULL };
1.51      kristaps   75: static v_check   posts_uc[] = { post_UC, NULL };
1.17      kristaps   76: static v_check   pres_bline[] = { check_bline, NULL };
1.1       kristaps   77:
1.51      kristaps   78:
1.1       kristaps   79: static const struct man_valid man_valids[MAN_MAX] = {
1.29      kristaps   80:        { NULL, posts_eq0 }, /* br */
1.32      kristaps   81:        { pres_bline, posts_th }, /* TH */
1.17      kristaps   82:        { pres_bline, posts_sec }, /* SH */
                     83:        { pres_bline, posts_sec }, /* SS */
1.59      kristaps   84:        { pres_bline, NULL }, /* TP */
1.17      kristaps   85:        { pres_bline, posts_par }, /* LP */
                     86:        { pres_bline, posts_par }, /* PP */
                     87:        { pres_bline, posts_par }, /* P */
1.59      kristaps   88:        { pres_bline, NULL }, /* IP */
                     89:        { pres_bline, NULL }, /* HP */
1.22      kristaps   90:        { NULL, NULL }, /* SM */
                     91:        { NULL, NULL }, /* SB */
1.17      kristaps   92:        { NULL, NULL }, /* BI */
                     93:        { NULL, NULL }, /* IB */
                     94:        { NULL, NULL }, /* BR */
                     95:        { NULL, NULL }, /* RB */
1.22      kristaps   96:        { NULL, NULL }, /* R */
                     97:        { NULL, NULL }, /* B */
                     98:        { NULL, NULL }, /* I */
1.17      kristaps   99:        { NULL, NULL }, /* IR */
                    100:        { NULL, NULL }, /* RI */
1.47      kristaps  101:        { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
                    102:        { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.51      kristaps  103:        { pres_bline, posts_nf }, /* nf */
                    104:        { pres_bline, posts_fi }, /* fi */
1.19      kristaps  105:        { NULL, NULL }, /* RE */
1.23      kristaps  106:        { NULL, posts_part }, /* RS */
1.21      kristaps  107:        { NULL, NULL }, /* DT */
1.51      kristaps  108:        { NULL, posts_uc }, /* UC */
1.26      kristaps  109:        { NULL, NULL }, /* PD */
1.51      kristaps  110:        { NULL, posts_at }, /* AT */
1.47      kristaps  111:        { NULL, NULL }, /* in */
1.55      kristaps  112:        { NULL, posts_ft }, /* ft */
1.1       kristaps  113: };
                    114:
                    115:
                    116: int
1.43      kristaps  117: man_valid_pre(struct man *m, struct man_node *n)
1.17      kristaps  118: {
                    119:        v_check         *cp;
                    120:
1.57      kristaps  121:        switch (n->type) {
                    122:        case (MAN_TEXT):
                    123:                /* FALLTHROUGH */
                    124:        case (MAN_ROOT):
1.61      kristaps  125:                /* FALLTHROUGH */
                    126:        case (MAN_EQN):
1.57      kristaps  127:                /* FALLTHROUGH */
                    128:        case (MAN_TBL):
1.17      kristaps  129:                return(1);
1.57      kristaps  130:        default:
                    131:                break;
                    132:        }
1.17      kristaps  133:
                    134:        if (NULL == (cp = man_valids[n->tok].pres))
                    135:                return(1);
                    136:        for ( ; *cp; cp++)
                    137:                if ( ! (*cp)(m, n))
                    138:                        return(0);
                    139:        return(1);
                    140: }
                    141:
                    142:
                    143: int
1.1       kristaps  144: man_valid_post(struct man *m)
                    145: {
1.17      kristaps  146:        v_check         *cp;
1.1       kristaps  147:
                    148:        if (MAN_VALID & m->last->flags)
                    149:                return(1);
                    150:        m->last->flags |= MAN_VALID;
                    151:
                    152:        switch (m->last->type) {
                    153:        case (MAN_TEXT):
1.68    ! kristaps  154:                check_text(m, m->last);
        !           155:                return(1);
1.1       kristaps  156:        case (MAN_ROOT):
1.14      kristaps  157:                return(check_root(m, m->last));
1.62      kristaps  158:        case (MAN_EQN):
                    159:                /* FALLTHROUGH */
1.57      kristaps  160:        case (MAN_TBL):
                    161:                return(1);
1.1       kristaps  162:        default:
                    163:                break;
                    164:        }
                    165:
                    166:        if (NULL == (cp = man_valids[m->last->tok].posts))
                    167:                return(1);
                    168:        for ( ; *cp; cp++)
                    169:                if ( ! (*cp)(m, m->last))
                    170:                        return(0);
                    171:
                    172:        return(1);
                    173: }
                    174:
                    175:
1.11      kristaps  176: static int
1.17      kristaps  177: check_root(CHKARGS)
1.14      kristaps  178: {
1.17      kristaps  179:
                    180:        if (MAN_BLINE & m->flags)
1.54      kristaps  181:                man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
                    182:        else if (MAN_ELINE & m->flags)
                    183:                man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
1.22      kristaps  184:
                    185:        m->flags &= ~MAN_BLINE;
                    186:        m->flags &= ~MAN_ELINE;
1.17      kristaps  187:
1.41      kristaps  188:        if (NULL == m->first->child) {
                    189:                man_nmsg(m, n, MANDOCERR_NODOCBODY);
                    190:                return(0);
                    191:        } else if (NULL == m->meta.title) {
1.54      kristaps  192:                man_nmsg(m, n, MANDOCERR_NOTITLE);
                    193:
1.34      kristaps  194:                /*
                    195:                 * If a title hasn't been set, do so now (by
                    196:                 * implication, date and section also aren't set).
                    197:                 */
1.54      kristaps  198:
1.34      kristaps  199:                m->meta.title = mandoc_strdup("unknown");
1.37      kristaps  200:                m->meta.msec = mandoc_strdup("1");
1.65      kristaps  201:                m->meta.date = mandoc_normdate
                    202:                        (m->parse, NULL, n->line, n->pos);
1.34      kristaps  203:        }
1.32      kristaps  204:
                    205:        return(1);
                    206: }
                    207:
1.68    ! kristaps  208: static void
1.17      kristaps  209: check_text(CHKARGS)
1.11      kristaps  210: {
1.68    ! kristaps  211:        char            *p, *pp, *cpp;
        !           212:        int              pos;
1.46      kristaps  213:        size_t           sz;
1.11      kristaps  214:
1.68    ! kristaps  215:        p = n->string;
        !           216:        pos = n->pos + 1;
        !           217:
        !           218:        while ('\0' != *p) {
1.46      kristaps  219:                sz = strcspn(p, "\t\\");
1.68    ! kristaps  220:
1.46      kristaps  221:                p += (int)sz;
1.68    ! kristaps  222:                pos += (int)sz;
1.46      kristaps  223:
1.68    ! kristaps  224:                if ('\t' == *p) {
        !           225:                        if ( ! (MAN_LITERAL & m->flags))
        !           226:                                man_pmsg(m, n->line, pos, MANDOCERR_BADTAB);
        !           227:                        p++;
        !           228:                        pos++;
        !           229:                        continue;
        !           230:                } else if ('\0' == *p)
1.46      kristaps  231:                        break;
                    232:
1.68    ! kristaps  233:                pos++;
        !           234:                pp = ++p;
1.11      kristaps  235:
1.68    ! kristaps  236:                if (ESCAPE_ERROR == mandoc_escape
        !           237:                                ((const char **)&pp, NULL, NULL)) {
        !           238:                        man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
        !           239:                        break;
1.15      kristaps  240:                }
1.45      kristaps  241:
1.68    ! kristaps  242:                cpp = p;
        !           243:                while (NULL != (cpp = memchr(cpp, ASCII_HYPH, pp - cpp)))
        !           244:                        *cpp = '-';
1.15      kristaps  245:
1.68    ! kristaps  246:                pos += pp - p;
        !           247:                p = pp;
1.11      kristaps  248:        }
1.1       kristaps  249: }
                    250:
                    251: #define        INEQ_DEFINE(x, ineq, name) \
                    252: static int \
1.17      kristaps  253: check_##name(CHKARGS) \
1.1       kristaps  254: { \
1.11      kristaps  255:        if (n->nchild ineq (x)) \
1.1       kristaps  256:                return(1); \
1.65      kristaps  257:        mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
1.41      kristaps  258:                        "line arguments %s %d (have %d)", \
                    259:                        #ineq, (x), n->nchild); \
1.60      schwarze  260:        return(1); \
1.1       kristaps  261: }
                    262:
                    263: INEQ_DEFINE(0, ==, eq0)
1.25      kristaps  264: INEQ_DEFINE(1, <=, le1)
1.1       kristaps  265: INEQ_DEFINE(2, >=, ge2)
                    266: INEQ_DEFINE(5, <=, le5)
                    267:
1.55      kristaps  268: static int
                    269: check_ft(CHKARGS)
                    270: {
                    271:        char    *cp;
                    272:        int      ok;
                    273:
                    274:        if (0 == n->nchild)
                    275:                return(1);
                    276:
                    277:        ok = 0;
                    278:        cp = n->child->string;
                    279:        switch (*cp) {
                    280:        case ('1'):
                    281:                /* FALLTHROUGH */
                    282:        case ('2'):
                    283:                /* FALLTHROUGH */
                    284:        case ('3'):
                    285:                /* FALLTHROUGH */
                    286:        case ('4'):
                    287:                /* FALLTHROUGH */
                    288:        case ('I'):
                    289:                /* FALLTHROUGH */
                    290:        case ('P'):
                    291:                /* FALLTHROUGH */
                    292:        case ('R'):
                    293:                if ('\0' == cp[1])
                    294:                        ok = 1;
                    295:                break;
                    296:        case ('B'):
                    297:                if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
                    298:                        ok = 1;
                    299:                break;
                    300:        case ('C'):
                    301:                if ('W' == cp[1] && '\0' == cp[2])
                    302:                        ok = 1;
                    303:                break;
                    304:        default:
                    305:                break;
                    306:        }
                    307:
                    308:        if (0 == ok) {
1.65      kristaps  309:                mandoc_vmsg
                    310:                        (MANDOCERR_BADFONT, m->parse,
                    311:                         n->line, n->pos, "%s", cp);
1.55      kristaps  312:                *cp = '\0';
                    313:        }
                    314:
                    315:        if (1 < n->nchild)
1.65      kristaps  316:                mandoc_vmsg
                    317:                        (MANDOCERR_ARGCOUNT, m->parse, n->line,
                    318:                         n->pos, "want one child (have %d)",
                    319:                         n->nchild);
1.55      kristaps  320:
                    321:        return(1);
                    322: }
1.16      kristaps  323:
                    324: static int
1.17      kristaps  325: check_sec(CHKARGS)
                    326: {
1.16      kristaps  327:
1.41      kristaps  328:        if (MAN_HEAD == n->type && 0 == n->nchild) {
                    329:                man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
                    330:                return(0);
                    331:        } else if (MAN_BODY == n->type && 0 == n->nchild)
1.67      kristaps  332:                mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
                    333:                                n->pos, "want children (have none)");
1.41      kristaps  334:
1.16      kristaps  335:        return(1);
                    336: }
1.17      kristaps  337:
                    338:
                    339: static int
1.23      kristaps  340: check_part(CHKARGS)
                    341: {
                    342:
                    343:        if (MAN_BODY == n->type && 0 == n->nchild)
1.67      kristaps  344:                mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
                    345:                                n->pos, "want children (have none)");
1.54      kristaps  346:
1.23      kristaps  347:        return(1);
                    348: }
                    349:
                    350:
                    351: static int
1.17      kristaps  352: check_par(CHKARGS)
                    353: {
                    354:
1.59      kristaps  355:        switch (n->type) {
                    356:        case (MAN_BLOCK):
                    357:                if (0 == n->body->nchild)
                    358:                        man_node_delete(m, n);
                    359:                break;
                    360:        case (MAN_BODY):
                    361:                if (0 == n->nchild)
                    362:                        man_nmsg(m, n, MANDOCERR_IGNPAR);
                    363:                break;
                    364:        case (MAN_HEAD):
                    365:                if (n->nchild)
                    366:                        man_nmsg(m, n, MANDOCERR_ARGSLOST);
                    367:                break;
                    368:        default:
                    369:                break;
                    370:        }
1.17      kristaps  371:
                    372:        return(1);
                    373: }
                    374:
                    375:
                    376: static int
                    377: check_bline(CHKARGS)
                    378: {
                    379:
1.22      kristaps  380:        assert( ! (MAN_ELINE & m->flags));
1.41      kristaps  381:        if (MAN_BLINE & m->flags) {
                    382:                man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
                    383:                return(0);
                    384:        }
1.31      kristaps  385:
1.18      kristaps  386:        return(1);
1.17      kristaps  387: }
                    388:
1.51      kristaps  389: static int
                    390: post_TH(CHKARGS)
                    391: {
1.60      schwarze  392:        const char      *p;
1.63      schwarze  393:        int              line, pos;
1.51      kristaps  394:
                    395:        if (m->meta.title)
                    396:                free(m->meta.title);
                    397:        if (m->meta.vol)
                    398:                free(m->meta.vol);
                    399:        if (m->meta.source)
                    400:                free(m->meta.source);
                    401:        if (m->meta.msec)
                    402:                free(m->meta.msec);
1.63      schwarze  403:        if (m->meta.date)
                    404:                free(m->meta.date);
1.51      kristaps  405:
1.63      schwarze  406:        line = n->line;
                    407:        pos = n->pos;
                    408:        m->meta.title = m->meta.vol = m->meta.date =
1.51      kristaps  409:                m->meta.msec = m->meta.source = NULL;
                    410:
                    411:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    412:
                    413:        n = n->child;
1.60      schwarze  414:        if (n && n->string) {
                    415:                for (p = n->string; '\0' != *p; p++) {
                    416:                        /* Only warn about this once... */
                    417:                        if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
                    418:                                man_nmsg(m, n, MANDOCERR_UPPERCASE);
                    419:                                break;
                    420:                        }
                    421:                }
                    422:                m->meta.title = mandoc_strdup(n->string);
                    423:        } else
                    424:                m->meta.title = mandoc_strdup("");
1.51      kristaps  425:
                    426:        /* TITLE ->MSEC<- DATE SOURCE VOL */
                    427:
1.60      schwarze  428:        if (n)
                    429:                n = n->next;
                    430:        if (n && n->string)
                    431:                m->meta.msec = mandoc_strdup(n->string);
                    432:        else
                    433:                m->meta.msec = mandoc_strdup("");
1.51      kristaps  434:
                    435:        /* TITLE MSEC ->DATE<- SOURCE VOL */
                    436:
1.60      schwarze  437:        if (n)
                    438:                n = n->next;
1.63      schwarze  439:        if (n)
                    440:                pos = n->pos;
1.65      kristaps  441:        m->meta.date = mandoc_normdate
                    442:                (m->parse, n ? n->string : NULL, line, pos);
1.51      kristaps  443:
                    444:        /* TITLE MSEC DATE ->SOURCE<- VOL */
                    445:
                    446:        if (n && (n = n->next))
                    447:                m->meta.source = mandoc_strdup(n->string);
                    448:
                    449:        /* TITLE MSEC DATE SOURCE ->VOL<- */
                    450:
                    451:        if (n && (n = n->next))
                    452:                m->meta.vol = mandoc_strdup(n->string);
                    453:
                    454:        /*
                    455:         * Remove the `TH' node after we've processed it for our
                    456:         * meta-data.
                    457:         */
                    458:        man_node_delete(m, m->last);
                    459:        return(1);
                    460: }
                    461:
                    462: static int
                    463: post_nf(CHKARGS)
                    464: {
                    465:
                    466:        if (MAN_LITERAL & m->flags)
                    467:                man_nmsg(m, n, MANDOCERR_SCOPEREP);
                    468:
                    469:        m->flags |= MAN_LITERAL;
                    470:        return(1);
                    471: }
                    472:
                    473: static int
                    474: post_fi(CHKARGS)
                    475: {
                    476:
                    477:        if ( ! (MAN_LITERAL & m->flags))
1.58      kristaps  478:                man_nmsg(m, n, MANDOCERR_WNOSCOPE);
1.51      kristaps  479:
                    480:        m->flags &= ~MAN_LITERAL;
                    481:        return(1);
                    482: }
                    483:
                    484: static int
                    485: post_UC(CHKARGS)
                    486: {
                    487:        static const char * const bsd_versions[] = {
                    488:            "3rd Berkeley Distribution",
                    489:            "4th Berkeley Distribution",
                    490:            "4.2 Berkeley Distribution",
                    491:            "4.3 Berkeley Distribution",
                    492:            "4.4 Berkeley Distribution",
                    493:        };
                    494:
                    495:        const char      *p, *s;
                    496:
                    497:        n = n->child;
                    498:        n = m->last->child;
                    499:
                    500:        if (NULL == n || MAN_TEXT != n->type)
                    501:                p = bsd_versions[0];
                    502:        else {
                    503:                s = n->string;
                    504:                if (0 == strcmp(s, "3"))
                    505:                        p = bsd_versions[0];
                    506:                else if (0 == strcmp(s, "4"))
                    507:                        p = bsd_versions[1];
                    508:                else if (0 == strcmp(s, "5"))
                    509:                        p = bsd_versions[2];
                    510:                else if (0 == strcmp(s, "6"))
                    511:                        p = bsd_versions[3];
                    512:                else if (0 == strcmp(s, "7"))
                    513:                        p = bsd_versions[4];
                    514:                else
                    515:                        p = bsd_versions[0];
                    516:        }
                    517:
                    518:        if (m->meta.source)
                    519:                free(m->meta.source);
                    520:
                    521:        m->meta.source = mandoc_strdup(p);
                    522:        return(1);
                    523: }
                    524:
                    525: static int
                    526: post_AT(CHKARGS)
                    527: {
                    528:        static const char * const unix_versions[] = {
                    529:            "7th Edition",
                    530:            "System III",
                    531:            "System V",
                    532:            "System V Release 2",
                    533:        };
                    534:
                    535:        const char      *p, *s;
                    536:        struct man_node *nn;
                    537:
                    538:        n = n->child;
                    539:
                    540:        if (NULL == n || MAN_TEXT != n->type)
                    541:                p = unix_versions[0];
                    542:        else {
                    543:                s = n->string;
                    544:                if (0 == strcmp(s, "3"))
                    545:                        p = unix_versions[0];
                    546:                else if (0 == strcmp(s, "4"))
                    547:                        p = unix_versions[1];
                    548:                else if (0 == strcmp(s, "5")) {
                    549:                        nn = n->next;
                    550:                        if (nn && MAN_TEXT == nn->type && nn->string[0])
                    551:                                p = unix_versions[3];
                    552:                        else
                    553:                                p = unix_versions[2];
                    554:                } else
                    555:                        p = unix_versions[0];
                    556:        }
                    557:
                    558:        if (m->meta.source)
                    559:                free(m->meta.source);
                    560:
                    561:        m->meta.source = mandoc_strdup(p);
                    562:        return(1);
                    563: }

CVSweb