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

Annotation of mandoc/man_validate.c, Revision 1.49

1.49    ! schwarze    1: /*     $Id: man_validate.c,v 1.48 2010/07/31 23:52:58 schwarze 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.1       kristaps   30:
1.41      kristaps   31: #include "mandoc.h"
1.1       kristaps   32: #include "libman.h"
1.15      kristaps   33: #include "libmandoc.h"
1.1       kristaps   34:
1.43      kristaps   35: #define        CHKARGS   struct man *m, struct man_node *n
1.1       kristaps   36:
1.17      kristaps   37: typedef        int     (*v_check)(CHKARGS);
1.1       kristaps   38:
                     39: struct man_valid {
1.17      kristaps   40:        v_check  *pres;
                     41:        v_check  *posts;
1.1       kristaps   42: };
                     43:
1.17      kristaps   44: static int       check_bline(CHKARGS);
                     45: static int       check_eq0(CHKARGS);
1.25      kristaps   46: static int       check_le1(CHKARGS);
1.17      kristaps   47: static int       check_ge2(CHKARGS);
                     48: static int       check_le5(CHKARGS);
                     49: static int       check_par(CHKARGS);
1.23      kristaps   50: static int       check_part(CHKARGS);
1.17      kristaps   51: static int       check_root(CHKARGS);
                     52: static int       check_sec(CHKARGS);
                     53: static int       check_text(CHKARGS);
1.32      kristaps   54: static int       check_title(CHKARGS);
1.17      kristaps   55:
                     56: static v_check   posts_eq0[] = { check_eq0, NULL };
1.32      kristaps   57: static v_check   posts_th[] = { check_ge2, check_le5, check_title, NULL };
1.17      kristaps   58: static v_check   posts_par[] = { check_par, NULL };
1.23      kristaps   59: static v_check   posts_part[] = { check_part, NULL };
1.17      kristaps   60: static v_check   posts_sec[] = { check_sec, NULL };
1.30      kristaps   61: static v_check   posts_le1[] = { check_le1, NULL };
1.17      kristaps   62: static v_check   pres_bline[] = { check_bline, NULL };
1.1       kristaps   63:
                     64: static const struct man_valid man_valids[MAN_MAX] = {
1.29      kristaps   65:        { NULL, posts_eq0 }, /* br */
1.32      kristaps   66:        { pres_bline, posts_th }, /* TH */
1.17      kristaps   67:        { pres_bline, posts_sec }, /* SH */
                     68:        { pres_bline, posts_sec }, /* SS */
                     69:        { pres_bline, posts_par }, /* TP */
                     70:        { pres_bline, posts_par }, /* LP */
                     71:        { pres_bline, posts_par }, /* PP */
                     72:        { pres_bline, posts_par }, /* P */
                     73:        { pres_bline, posts_par }, /* IP */
                     74:        { pres_bline, posts_par }, /* HP */
1.22      kristaps   75:        { NULL, NULL }, /* SM */
                     76:        { NULL, NULL }, /* SB */
1.17      kristaps   77:        { NULL, NULL }, /* BI */
                     78:        { NULL, NULL }, /* IB */
                     79:        { NULL, NULL }, /* BR */
                     80:        { NULL, NULL }, /* RB */
1.22      kristaps   81:        { NULL, NULL }, /* R */
                     82:        { NULL, NULL }, /* B */
                     83:        { NULL, NULL }, /* I */
1.17      kristaps   84:        { NULL, NULL }, /* IR */
                     85:        { NULL, NULL }, /* RI */
1.47      kristaps   86:        { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
1.17      kristaps   87:        { NULL, NULL }, /* i */
1.47      kristaps   88:        { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
1.17      kristaps   89:        { pres_bline, posts_eq0 }, /* nf */
                     90:        { pres_bline, posts_eq0 }, /* fi */
                     91:        { NULL, NULL }, /* r */
1.19      kristaps   92:        { NULL, NULL }, /* RE */
1.23      kristaps   93:        { NULL, posts_part }, /* RS */
1.21      kristaps   94:        { NULL, NULL }, /* DT */
1.24      kristaps   95:        { NULL, NULL }, /* UC */
1.26      kristaps   96:        { NULL, NULL }, /* PD */
1.47      kristaps   97:        { NULL, posts_le1 }, /* Sp */ /* FIXME: should warn only. */
                     98:        { pres_bline, posts_le1 }, /* Vb */ /* FIXME: should warn only. */
1.30      kristaps   99:        { pres_bline, posts_eq0 }, /* Ve */
1.40      joerg     100:        { NULL, NULL }, /* AT */
1.47      kristaps  101:        { NULL, NULL }, /* in */
1.1       kristaps  102: };
                    103:
                    104:
                    105: int
1.43      kristaps  106: man_valid_pre(struct man *m, struct man_node *n)
1.17      kristaps  107: {
                    108:        v_check         *cp;
                    109:
                    110:        if (MAN_TEXT == n->type)
                    111:                return(1);
                    112:        if (MAN_ROOT == n->type)
                    113:                return(1);
                    114:
                    115:        if (NULL == (cp = man_valids[n->tok].pres))
                    116:                return(1);
                    117:        for ( ; *cp; cp++)
                    118:                if ( ! (*cp)(m, n))
                    119:                        return(0);
                    120:        return(1);
                    121: }
                    122:
                    123:
                    124: int
1.1       kristaps  125: man_valid_post(struct man *m)
                    126: {
1.17      kristaps  127:        v_check         *cp;
1.1       kristaps  128:
                    129:        if (MAN_VALID & m->last->flags)
                    130:                return(1);
                    131:        m->last->flags |= MAN_VALID;
                    132:
                    133:        switch (m->last->type) {
                    134:        case (MAN_TEXT):
1.11      kristaps  135:                return(check_text(m, m->last));
1.1       kristaps  136:        case (MAN_ROOT):
1.14      kristaps  137:                return(check_root(m, m->last));
1.1       kristaps  138:        default:
                    139:                break;
                    140:        }
                    141:
                    142:        if (NULL == (cp = man_valids[m->last->tok].posts))
                    143:                return(1);
                    144:        for ( ; *cp; cp++)
                    145:                if ( ! (*cp)(m, m->last))
                    146:                        return(0);
                    147:
                    148:        return(1);
                    149: }
                    150:
                    151:
1.11      kristaps  152: static int
1.17      kristaps  153: check_root(CHKARGS)
1.14      kristaps  154: {
1.17      kristaps  155:
                    156:        if (MAN_BLINE & m->flags)
1.41      kristaps  157:                return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.17      kristaps  158:        if (MAN_ELINE & m->flags)
1.41      kristaps  159:                return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
1.22      kristaps  160:
                    161:        m->flags &= ~MAN_BLINE;
                    162:        m->flags &= ~MAN_ELINE;
1.17      kristaps  163:
1.41      kristaps  164:        if (NULL == m->first->child) {
                    165:                man_nmsg(m, n, MANDOCERR_NODOCBODY);
                    166:                return(0);
                    167:        } else if (NULL == m->meta.title) {
                    168:                if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
1.34      kristaps  169:                        return(0);
                    170:                /*
                    171:                 * If a title hasn't been set, do so now (by
                    172:                 * implication, date and section also aren't set).
                    173:                 *
                    174:                 * FIXME: this should be in man_action.c.
                    175:                 */
                    176:                m->meta.title = mandoc_strdup("unknown");
                    177:                m->meta.date = time(NULL);
1.37      kristaps  178:                m->meta.msec = mandoc_strdup("1");
1.34      kristaps  179:        }
1.32      kristaps  180:
                    181:        return(1);
                    182: }
                    183:
                    184:
                    185: static int
                    186: check_title(CHKARGS)
                    187: {
                    188:        const char      *p;
                    189:
                    190:        assert(n->child);
1.41      kristaps  191:        /* FIXME: is this sufficient? */
                    192:        if ('\0' == *n->child->string) {
                    193:                man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
                    194:                return(0);
                    195:        }
1.32      kristaps  196:
                    197:        for (p = n->child->string; '\0' != *p; p++)
                    198:                if (isalpha((u_char)*p) && ! isupper((u_char)*p))
1.41      kristaps  199:                        if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
1.32      kristaps  200:                                return(0);
1.14      kristaps  201:
                    202:        return(1);
                    203: }
                    204:
                    205:
                    206: static int
1.17      kristaps  207: check_text(CHKARGS)
1.11      kristaps  208: {
1.43      kristaps  209:        char            *p;
1.15      kristaps  210:        int              pos, c;
1.46      kristaps  211:        size_t           sz;
1.11      kristaps  212:
1.46      kristaps  213:        for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
                    214:                sz = strcspn(p, "\t\\");
                    215:                p += (int)sz;
                    216:
                    217:                if ('\0' == *p)
                    218:                        break;
                    219:
                    220:                pos += (int)sz;
1.11      kristaps  221:
1.46      kristaps  222:                if ('\t' == *p) {
                    223:                        if (MAN_LITERAL & m->flags)
                    224:                                continue;
                    225:                        if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
1.15      kristaps  226:                                continue;
1.46      kristaps  227:                        return(0);
1.15      kristaps  228:                }
1.45      kristaps  229:
1.46      kristaps  230:                /* Check the special character. */
1.15      kristaps  231:
1.46      kristaps  232:                c = mandoc_special(p);
                    233:                if (c) {
                    234:                        p += c - 1;
                    235:                        pos += c - 1;
1.49    ! schwarze  236:                } else
        !           237:                        man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
1.11      kristaps  238:        }
                    239:
                    240:        return(1);
1.1       kristaps  241: }
                    242:
                    243:
                    244: #define        INEQ_DEFINE(x, ineq, name) \
                    245: static int \
1.17      kristaps  246: check_##name(CHKARGS) \
1.1       kristaps  247: { \
1.11      kristaps  248:        if (n->nchild ineq (x)) \
1.1       kristaps  249:                return(1); \
1.41      kristaps  250:        man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
                    251:                        "line arguments %s %d (have %d)", \
                    252:                        #ineq, (x), n->nchild); \
                    253:        return(0); \
1.1       kristaps  254: }
                    255:
                    256: INEQ_DEFINE(0, ==, eq0)
1.25      kristaps  257: INEQ_DEFINE(1, <=, le1)
1.1       kristaps  258: INEQ_DEFINE(2, >=, ge2)
                    259: INEQ_DEFINE(5, <=, le5)
                    260:
1.16      kristaps  261:
                    262: static int
1.17      kristaps  263: check_sec(CHKARGS)
                    264: {
1.16      kristaps  265:
1.41      kristaps  266:        if (MAN_HEAD == n->type && 0 == n->nchild) {
                    267:                man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
                    268:                return(0);
                    269:        } else if (MAN_BODY == n->type && 0 == n->nchild)
                    270:                return(man_nmsg(m, n, MANDOCERR_NOBODY));
                    271:
1.16      kristaps  272:        return(1);
                    273: }
1.17      kristaps  274:
                    275:
                    276: static int
1.23      kristaps  277: check_part(CHKARGS)
                    278: {
                    279:
                    280:        if (MAN_BODY == n->type && 0 == n->nchild)
1.41      kristaps  281:                return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.23      kristaps  282:        return(1);
                    283: }
                    284:
                    285:
                    286: static int
1.17      kristaps  287: check_par(CHKARGS)
                    288: {
                    289:
                    290:        if (MAN_BODY == n->type)
                    291:                switch (n->tok) {
                    292:                case (MAN_IP):
                    293:                        /* FALLTHROUGH */
                    294:                case (MAN_HP):
                    295:                        /* FALLTHROUGH */
                    296:                case (MAN_TP):
                    297:                        /* Body-less lists are ok. */
                    298:                        break;
                    299:                default:
                    300:                        if (n->nchild)
                    301:                                break;
1.41      kristaps  302:                        return(man_nmsg(m, n, MANDOCERR_NOBODY));
1.17      kristaps  303:                }
                    304:        if (MAN_HEAD == n->type)
                    305:                switch (n->tok) {
                    306:                case (MAN_PP):
                    307:                        /* FALLTHROUGH */
                    308:                case (MAN_P):
                    309:                        /* FALLTHROUGH */
                    310:                case (MAN_LP):
                    311:                        if (0 == n->nchild)
                    312:                                break;
1.41      kristaps  313:                        return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
1.17      kristaps  314:                default:
                    315:                        if (n->nchild)
                    316:                                break;
1.41      kristaps  317:                        return(man_nmsg(m, n, MANDOCERR_NOARGS));
1.17      kristaps  318:                }
                    319:
                    320:        return(1);
                    321: }
                    322:
                    323:
                    324: static int
                    325: check_bline(CHKARGS)
                    326: {
                    327:
1.22      kristaps  328:        assert( ! (MAN_ELINE & m->flags));
1.41      kristaps  329:        if (MAN_BLINE & m->flags) {
                    330:                man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
                    331:                return(0);
                    332:        }
1.31      kristaps  333:
1.18      kristaps  334:        return(1);
1.17      kristaps  335: }
                    336:

CVSweb