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

Annotation of mandoc/man_validate.c, Revision 1.53

1.53    ! kristaps    1: /*     $Id: man_validate.c,v 1.52 2010/12/05 16:14:16 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.41      kristaps  166:                return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.17      kristaps  167:        if (MAN_ELINE & m->flags)
1.41      kristaps  168:                return(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) {
                    177:                if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
1.34      kristaps  178:                        return(0);
                    179:                /*
                    180:                 * If a title hasn't been set, do so now (by
                    181:                 * implication, date and section also aren't set).
                    182:                 *
                    183:                 * FIXME: this should be in man_action.c.
                    184:                 */
                    185:                m->meta.title = mandoc_strdup("unknown");
                    186:                m->meta.date = time(NULL);
1.37      kristaps  187:                m->meta.msec = mandoc_strdup("1");
1.34      kristaps  188:        }
1.32      kristaps  189:
                    190:        return(1);
                    191: }
                    192:
                    193:
                    194: static int
                    195: check_title(CHKARGS)
                    196: {
                    197:        const char      *p;
                    198:
                    199:        assert(n->child);
1.41      kristaps  200:        /* FIXME: is this sufficient? */
                    201:        if ('\0' == *n->child->string) {
                    202:                man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
                    203:                return(0);
                    204:        }
1.32      kristaps  205:
                    206:        for (p = n->child->string; '\0' != *p; p++)
                    207:                if (isalpha((u_char)*p) && ! isupper((u_char)*p))
1.41      kristaps  208:                        if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
1.32      kristaps  209:                                return(0);
1.14      kristaps  210:
                    211:        return(1);
                    212: }
                    213:
                    214:
                    215: static int
1.17      kristaps  216: check_text(CHKARGS)
1.11      kristaps  217: {
1.43      kristaps  218:        char            *p;
1.15      kristaps  219:        int              pos, c;
1.46      kristaps  220:        size_t           sz;
1.11      kristaps  221:
1.46      kristaps  222:        for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
                    223:                sz = strcspn(p, "\t\\");
                    224:                p += (int)sz;
                    225:
                    226:                if ('\0' == *p)
                    227:                        break;
                    228:
                    229:                pos += (int)sz;
1.11      kristaps  230:
1.46      kristaps  231:                if ('\t' == *p) {
                    232:                        if (MAN_LITERAL & m->flags)
                    233:                                continue;
                    234:                        if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15      kristaps  235:                                continue;
1.46      kristaps  236:                        return(0);
1.15      kristaps  237:                }
1.45      kristaps  238:
1.46      kristaps  239:                /* Check the special character. */
1.15      kristaps  240:
1.46      kristaps  241:                c = mandoc_special(p);
                    242:                if (c) {
                    243:                        p += c - 1;
                    244:                        pos += c - 1;
1.49      schwarze  245:                } else
                    246:                        man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11      kristaps  247:        }
                    248:
                    249:        return(1);
1.1       kristaps  250: }
                    251:
                    252:
                    253: #define        INEQ_DEFINE(x, ineq, name) \
                    254: static int \
1.17      kristaps  255: check_##name(CHKARGS) \
1.1       kristaps  256: { \
1.11      kristaps  257:        if (n->nchild ineq (x)) \
1.1       kristaps  258:                return(1); \
1.41      kristaps  259:        man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
                    260:                        "line arguments %s %d (have %d)", \
                    261:                        #ineq, (x), n->nchild); \
                    262:        return(0); \
1.1       kristaps  263: }
                    264:
                    265: INEQ_DEFINE(0, ==, eq0)
1.25      kristaps  266: INEQ_DEFINE(1, <=, le1)
1.1       kristaps  267: INEQ_DEFINE(2, >=, ge2)
                    268: INEQ_DEFINE(5, <=, le5)
                    269:
1.16      kristaps  270:
                    271: static int
1.17      kristaps  272: check_sec(CHKARGS)
                    273: {
1.16      kristaps  274:
1.41      kristaps  275:        if (MAN_HEAD == n->type && 0 == n->nchild) {
                    276:                man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
                    277:                return(0);
                    278:        } else if (MAN_BODY == n->type && 0 == n->nchild)
                    279:                return(man_nmsg(m, n, MANDOCERR_NOBODY));
                    280:
1.16      kristaps  281:        return(1);
                    282: }
1.17      kristaps  283:
                    284:
                    285: static int
1.23      kristaps  286: check_part(CHKARGS)
                    287: {
                    288:
                    289:        if (MAN_BODY == n->type && 0 == n->nchild)
1.41      kristaps  290:                return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.23      kristaps  291:        return(1);
                    292: }
                    293:
                    294:
                    295: static int
1.17      kristaps  296: check_par(CHKARGS)
                    297: {
                    298:
                    299:        if (MAN_BODY == n->type)
                    300:                switch (n->tok) {
                    301:                case (MAN_IP):
                    302:                        /* FALLTHROUGH */
                    303:                case (MAN_HP):
                    304:                        /* FALLTHROUGH */
                    305:                case (MAN_TP):
                    306:                        /* Body-less lists are ok. */
                    307:                        break;
                    308:                default:
                    309:                        if (n->nchild)
                    310:                                break;
1.41      kristaps  311:                        return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.17      kristaps  312:                }
                    313:        if (MAN_HEAD == n->type)
                    314:                switch (n->tok) {
                    315:                case (MAN_PP):
                    316:                        /* FALLTHROUGH */
                    317:                case (MAN_P):
                    318:                        /* FALLTHROUGH */
                    319:                case (MAN_LP):
                    320:                        if (0 == n->nchild)
                    321:                                break;
1.41      kristaps  322:                        return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
1.17      kristaps  323:                default:
1.53    ! kristaps  324:                        break;
1.17      kristaps  325:                }
                    326:
                    327:        return(1);
                    328: }
                    329:
                    330:
                    331: static int
                    332: check_bline(CHKARGS)
                    333: {
                    334:
1.22      kristaps  335:        assert( ! (MAN_ELINE & m->flags));
1.41      kristaps  336:        if (MAN_BLINE & m->flags) {
                    337:                man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
                    338:                return(0);
                    339:        }
1.31      kristaps  340:
1.18      kristaps  341:        return(1);
1.17      kristaps  342: }
                    343:
1.51      kristaps  344: static int
                    345: post_TH(CHKARGS)
                    346: {
                    347:
                    348:        if (m->meta.title)
                    349:                free(m->meta.title);
                    350:        if (m->meta.vol)
                    351:                free(m->meta.vol);
                    352:        if (m->meta.source)
                    353:                free(m->meta.source);
                    354:        if (m->meta.msec)
                    355:                free(m->meta.msec);
                    356:        if (m->meta.rawdate)
                    357:                free(m->meta.rawdate);
                    358:
                    359:        m->meta.title = m->meta.vol = m->meta.rawdate =
                    360:                m->meta.msec = m->meta.source = NULL;
                    361:        m->meta.date = 0;
                    362:
                    363:        /* ->TITLE<- MSEC DATE SOURCE VOL */
                    364:
                    365:        n = n->child;
                    366:        assert(n);
                    367:        m->meta.title = mandoc_strdup(n->string);
                    368:
                    369:        /* TITLE ->MSEC<- DATE SOURCE VOL */
                    370:
                    371:        n = n->next;
                    372:        assert(n);
                    373:        m->meta.msec = mandoc_strdup(n->string);
                    374:
                    375:        /* TITLE MSEC ->DATE<- SOURCE VOL */
                    376:
                    377:        /*
                    378:         * Try to parse the date.  If this works, stash the epoch (this
                    379:         * is optimal because we can reformat it in the canonical form).
                    380:         * If it doesn't parse, isn't specified at all, or is an empty
                    381:         * string, then use the current date.
                    382:         */
                    383:
                    384:        n = n->next;
                    385:        if (n && n->string && *n->string) {
                    386:                m->meta.date = mandoc_a2time
                    387:                        (MTIME_ISO_8601, n->string);
                    388:                if (0 == m->meta.date) {
                    389:                        man_nmsg(m, n, MANDOCERR_BADDATE);
                    390:                        m->meta.rawdate = mandoc_strdup(n->string);
                    391:                }
                    392:        } else
                    393:                m->meta.date = time(NULL);
                    394:
                    395:        /* TITLE MSEC DATE ->SOURCE<- VOL */
                    396:
                    397:        if (n && (n = n->next))
                    398:                m->meta.source = mandoc_strdup(n->string);
                    399:
                    400:        /* TITLE MSEC DATE SOURCE ->VOL<- */
                    401:
                    402:        if (n && (n = n->next))
                    403:                m->meta.vol = mandoc_strdup(n->string);
                    404:
                    405:        /*
                    406:         * Remove the `TH' node after we've processed it for our
                    407:         * meta-data.
                    408:         */
                    409:        man_node_delete(m, m->last);
                    410:        return(1);
                    411: }
                    412:
                    413: static int
                    414: post_nf(CHKARGS)
                    415: {
                    416:
                    417:        if (MAN_LITERAL & m->flags)
                    418:                man_nmsg(m, n, MANDOCERR_SCOPEREP);
                    419:
                    420:        m->flags |= MAN_LITERAL;
                    421:        return(1);
                    422: }
                    423:
                    424: static int
                    425: post_fi(CHKARGS)
                    426: {
                    427:
                    428:        if ( ! (MAN_LITERAL & m->flags))
                    429:                man_nmsg(m, n, MANDOCERR_NOSCOPE);
                    430:
                    431:        m->flags &= ~MAN_LITERAL;
                    432:        return(1);
                    433: }
                    434:
                    435: static int
                    436: post_UC(CHKARGS)
                    437: {
                    438:        static const char * const bsd_versions[] = {
                    439:            "3rd Berkeley Distribution",
                    440:            "4th Berkeley Distribution",
                    441:            "4.2 Berkeley Distribution",
                    442:            "4.3 Berkeley Distribution",
                    443:            "4.4 Berkeley Distribution",
                    444:        };
                    445:
                    446:        const char      *p, *s;
                    447:
                    448:        n = n->child;
                    449:        n = m->last->child;
                    450:
                    451:        if (NULL == n || MAN_TEXT != n->type)
                    452:                p = bsd_versions[0];
                    453:        else {
                    454:                s = n->string;
                    455:                if (0 == strcmp(s, "3"))
                    456:                        p = bsd_versions[0];
                    457:                else if (0 == strcmp(s, "4"))
                    458:                        p = bsd_versions[1];
                    459:                else if (0 == strcmp(s, "5"))
                    460:                        p = bsd_versions[2];
                    461:                else if (0 == strcmp(s, "6"))
                    462:                        p = bsd_versions[3];
                    463:                else if (0 == strcmp(s, "7"))
                    464:                        p = bsd_versions[4];
                    465:                else
                    466:                        p = bsd_versions[0];
                    467:        }
                    468:
                    469:        if (m->meta.source)
                    470:                free(m->meta.source);
                    471:
                    472:        m->meta.source = mandoc_strdup(p);
                    473:        return(1);
                    474: }
                    475:
                    476: static int
                    477: post_AT(CHKARGS)
                    478: {
                    479:        static const char * const unix_versions[] = {
                    480:            "7th Edition",
                    481:            "System III",
                    482:            "System V",
                    483:            "System V Release 2",
                    484:        };
                    485:
                    486:        const char      *p, *s;
                    487:        struct man_node *nn;
                    488:
                    489:        n = n->child;
                    490:
                    491:        if (NULL == n || MAN_TEXT != n->type)
                    492:                p = unix_versions[0];
                    493:        else {
                    494:                s = n->string;
                    495:                if (0 == strcmp(s, "3"))
                    496:                        p = unix_versions[0];
                    497:                else if (0 == strcmp(s, "4"))
                    498:                        p = unix_versions[1];
                    499:                else if (0 == strcmp(s, "5")) {
                    500:                        nn = n->next;
                    501:                        if (nn && MAN_TEXT == nn->type && nn->string[0])
                    502:                                p = unix_versions[3];
                    503:                        else
                    504:                                p = unix_versions[2];
                    505:                } else
                    506:                        p = unix_versions[0];
                    507:        }
                    508:
                    509:        if (m->meta.source)
                    510:                free(m->meta.source);
                    511:
                    512:        m->meta.source = mandoc_strdup(p);
                    513:        return(1);
                    514: }

CVSweb