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

Annotation of mandoc/man_validate.c, Revision 1.54

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

CVSweb