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

Annotation of mandoc/man_validate.c, Revision 1.80

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

CVSweb