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

Annotation of mandoc/man_validate.c, Revision 1.74

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

CVSweb