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

Annotation of mandoc/man_validate.c, Revision 1.24

1.24    ! kristaps    1: /*     $Id: man_validate.c,v 1.23 2009/08/21 13:16:16 kristaps Exp $ */
1.1       kristaps    2: /*
1.9       kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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:  */
                     17: #include <sys/types.h>
                     18:
                     19: #include <assert.h>
                     20: #include <ctype.h>
1.16      kristaps   21: #include <errno.h>
                     22: #include <limits.h>
1.1       kristaps   23: #include <stdarg.h>
                     24: #include <stdlib.h>
                     25:
                     26: #include "libman.h"
1.15      kristaps   27: #include "libmandoc.h"
1.1       kristaps   28:
1.17      kristaps   29: #define        CHKARGS   struct man *m, const struct man_node *n
1.1       kristaps   30:
1.17      kristaps   31: typedef        int     (*v_check)(CHKARGS);
1.1       kristaps   32:
                     33: struct man_valid {
1.17      kristaps   34:        v_check  *pres;
                     35:        v_check  *posts;
1.1       kristaps   36: };
                     37:
1.17      kristaps   38: static int       check_bline(CHKARGS);
                     39: static int       check_eq0(CHKARGS);
                     40: static int       check_eq1(CHKARGS);
                     41: static int       check_ge2(CHKARGS);
                     42: static int       check_le5(CHKARGS);
                     43: static int       check_par(CHKARGS);
1.23      kristaps   44: static int       check_part(CHKARGS);
1.17      kristaps   45: static int       check_root(CHKARGS);
                     46: static int       check_sec(CHKARGS);
                     47: static int       check_sp(CHKARGS);
                     48: static int       check_text(CHKARGS);
                     49:
                     50: static v_check   posts_eq0[] = { check_eq0, NULL };
                     51: static v_check   posts_ge2_le5[] = { check_ge2, check_le5, NULL };
                     52: static v_check   posts_par[] = { check_par, NULL };
1.23      kristaps   53: static v_check   posts_part[] = { check_part, NULL };
1.17      kristaps   54: static v_check   posts_sec[] = { check_sec, NULL };
                     55: static v_check   posts_sp[] = { check_sp, NULL };
                     56: static v_check   pres_bline[] = { check_bline, NULL };
1.1       kristaps   57:
                     58: static const struct man_valid man_valids[MAN_MAX] = {
1.17      kristaps   59:        { pres_bline, posts_eq0 }, /* br */
                     60:        { pres_bline, posts_ge2_le5 }, /* TH */
                     61:        { pres_bline, posts_sec }, /* SH */
                     62:        { pres_bline, posts_sec }, /* SS */
                     63:        { pres_bline, posts_par }, /* TP */
                     64:        { pres_bline, posts_par }, /* LP */
                     65:        { pres_bline, posts_par }, /* PP */
                     66:        { pres_bline, posts_par }, /* P */
                     67:        { pres_bline, posts_par }, /* IP */
                     68:        { pres_bline, posts_par }, /* HP */
1.22      kristaps   69:        { NULL, NULL }, /* SM */
                     70:        { NULL, NULL }, /* SB */
1.17      kristaps   71:        { NULL, NULL }, /* BI */
                     72:        { NULL, NULL }, /* IB */
                     73:        { NULL, NULL }, /* BR */
                     74:        { NULL, NULL }, /* RB */
1.22      kristaps   75:        { NULL, NULL }, /* R */
                     76:        { NULL, NULL }, /* B */
                     77:        { NULL, NULL }, /* I */
1.17      kristaps   78:        { NULL, NULL }, /* IR */
                     79:        { NULL, NULL }, /* RI */
                     80:        { pres_bline, posts_eq0 }, /* na */
                     81:        { NULL, NULL }, /* i */
                     82:        { pres_bline, posts_sp }, /* sp */
                     83:        { pres_bline, posts_eq0 }, /* nf */
                     84:        { pres_bline, posts_eq0 }, /* fi */
                     85:        { NULL, NULL }, /* r */
1.19      kristaps   86:        { NULL, NULL }, /* RE */
1.23      kristaps   87:        { NULL, posts_part }, /* RS */
1.21      kristaps   88:        { NULL, NULL }, /* DT */
1.24    ! kristaps   89:        { NULL, NULL }, /* UC */
1.1       kristaps   90: };
                     91:
                     92:
                     93: int
1.17      kristaps   94: man_valid_pre(struct man *m, const struct man_node *n)
                     95: {
                     96:        v_check         *cp;
                     97:
                     98:        if (MAN_TEXT == n->type)
                     99:                return(1);
                    100:        if (MAN_ROOT == n->type)
                    101:                return(1);
                    102:
                    103:        if (NULL == (cp = man_valids[n->tok].pres))
                    104:                return(1);
                    105:        for ( ; *cp; cp++)
                    106:                if ( ! (*cp)(m, n))
                    107:                        return(0);
                    108:        return(1);
                    109: }
                    110:
                    111:
                    112: int
1.1       kristaps  113: man_valid_post(struct man *m)
                    114: {
1.17      kristaps  115:        v_check         *cp;
1.1       kristaps  116:
                    117:        if (MAN_VALID & m->last->flags)
                    118:                return(1);
                    119:        m->last->flags |= MAN_VALID;
                    120:
                    121:        switch (m->last->type) {
                    122:        case (MAN_TEXT):
1.11      kristaps  123:                return(check_text(m, m->last));
1.1       kristaps  124:        case (MAN_ROOT):
1.14      kristaps  125:                return(check_root(m, m->last));
1.1       kristaps  126:        default:
                    127:                break;
                    128:        }
                    129:
                    130:        if (NULL == (cp = man_valids[m->last->tok].posts))
                    131:                return(1);
                    132:        for ( ; *cp; cp++)
                    133:                if ( ! (*cp)(m, m->last))
                    134:                        return(0);
                    135:
                    136:        return(1);
                    137: }
                    138:
                    139:
1.11      kristaps  140: static int
1.17      kristaps  141: check_root(CHKARGS)
1.14      kristaps  142: {
1.17      kristaps  143:
                    144:        if (MAN_BLINE & m->flags)
1.22      kristaps  145:                return(man_nwarn(m, n, WEXITSCOPE));
1.17      kristaps  146:        if (MAN_ELINE & m->flags)
1.22      kristaps  147:                return(man_nwarn(m, n, WEXITSCOPE));
                    148:
                    149:        m->flags &= ~MAN_BLINE;
                    150:        m->flags &= ~MAN_ELINE;
1.17      kristaps  151:
1.14      kristaps  152:        if (NULL == m->first->child)
                    153:                return(man_nerr(m, n, WNODATA));
                    154:        if (NULL == m->meta.title)
                    155:                return(man_nerr(m, n, WNOTITLE));
                    156:
                    157:        return(1);
                    158: }
                    159:
                    160:
                    161: static int
1.17      kristaps  162: check_text(CHKARGS)
1.11      kristaps  163: {
                    164:        const char      *p;
1.15      kristaps  165:        int              pos, c;
1.11      kristaps  166:
                    167:        assert(n->string);
                    168:
                    169:        for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
1.15      kristaps  170:                if ('\\' == *p) {
                    171:                        c = mandoc_special(p);
                    172:                        if (c) {
                    173:                                p += c - 1;
                    174:                                pos += c - 1;
                    175:                                continue;
                    176:                        }
                    177:                        if ( ! (MAN_IGN_ESCAPE & m->pflags))
                    178:                                return(man_perr(m, n->line, pos, WESCAPE));
                    179:                        if ( ! man_pwarn(m, n->line, pos, WESCAPE))
                    180:                                return(0);
                    181:                        continue;
                    182:                }
                    183:
                    184:                if ('\t' == *p || isprint((u_char)*p))
1.11      kristaps  185:                        continue;
                    186:
                    187:                if (MAN_IGN_CHARS & m->pflags)
1.12      kristaps  188:                        return(man_pwarn(m, n->line, pos, WNPRINT));
                    189:                return(man_perr(m, n->line, pos, WNPRINT));
1.11      kristaps  190:        }
                    191:
                    192:        return(1);
1.1       kristaps  193: }
                    194:
                    195:
                    196: #define        INEQ_DEFINE(x, ineq, name) \
                    197: static int \
1.17      kristaps  198: check_##name(CHKARGS) \
1.1       kristaps  199: { \
1.11      kristaps  200:        if (n->nchild ineq (x)) \
1.1       kristaps  201:                return(1); \
1.4       kristaps  202:        return(man_verr(m, n->line, n->pos, \
1.1       kristaps  203:                        "expected line arguments %s %d, have %d", \
1.11      kristaps  204:                        #ineq, (x), n->nchild)); \
1.1       kristaps  205: }
                    206:
                    207: INEQ_DEFINE(0, ==, eq0)
1.16      kristaps  208: INEQ_DEFINE(1, ==, eq1)
1.1       kristaps  209: INEQ_DEFINE(2, >=, ge2)
                    210: INEQ_DEFINE(5, <=, le5)
                    211:
1.16      kristaps  212:
                    213: static int
1.17      kristaps  214: check_sp(CHKARGS)
1.16      kristaps  215: {
                    216:        long             lval;
                    217:        char            *ep, *buf;
                    218:
1.17      kristaps  219:        if (NULL == n->child)
1.16      kristaps  220:                return(1);
                    221:        else if ( ! check_eq1(m, n))
                    222:                return(0);
                    223:
1.17      kristaps  224:        assert(MAN_TEXT == n->child->type);
                    225:        buf = n->child->string;
1.16      kristaps  226:        assert(buf);
                    227:
                    228:        /* From OpenBSD's strtol(3). */
1.17      kristaps  229:
1.16      kristaps  230:        errno = 0;
                    231:        lval = strtol(buf, &ep, 10);
                    232:        if (buf[0] == '\0' || *ep != '\0')
1.17      kristaps  233:                return(man_nerr(m, n->child, WNUMFMT));
1.16      kristaps  234:
                    235:        if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
                    236:                        (lval > INT_MAX || lval < 0))
1.17      kristaps  237:                return(man_nerr(m, n->child, WNUMFMT));
                    238:
                    239:        return(1);
                    240: }
                    241:
                    242:
                    243: static int
                    244: check_sec(CHKARGS)
                    245: {
1.16      kristaps  246:
1.17      kristaps  247:        if (MAN_BODY == n->type && 0 == n->nchild)
                    248:                return(man_nwarn(m, n, WBODYARGS));
                    249:        if (MAN_HEAD == n->type && 0 == n->nchild)
                    250:                return(man_nerr(m, n, WHEADARGS));
1.16      kristaps  251:        return(1);
                    252: }
1.17      kristaps  253:
                    254:
                    255: static int
1.23      kristaps  256: check_part(CHKARGS)
                    257: {
                    258:
                    259:        if (MAN_BODY == n->type && 0 == n->nchild)
                    260:                return(man_nwarn(m, n, WBODYARGS));
                    261:        return(1);
                    262: }
                    263:
                    264:
                    265: static int
1.17      kristaps  266: check_par(CHKARGS)
                    267: {
                    268:
                    269:        if (MAN_BODY == n->type)
                    270:                switch (n->tok) {
                    271:                case (MAN_IP):
                    272:                        /* FALLTHROUGH */
                    273:                case (MAN_HP):
                    274:                        /* FALLTHROUGH */
                    275:                case (MAN_TP):
                    276:                        /* Body-less lists are ok. */
                    277:                        break;
                    278:                default:
                    279:                        if (n->nchild)
                    280:                                break;
                    281:                        return(man_nwarn(m, n, WBODYARGS));
                    282:                }
                    283:        if (MAN_HEAD == n->type)
                    284:                switch (n->tok) {
                    285:                case (MAN_PP):
                    286:                        /* FALLTHROUGH */
                    287:                case (MAN_P):
                    288:                        /* FALLTHROUGH */
                    289:                case (MAN_LP):
                    290:                        if (0 == n->nchild)
                    291:                                break;
                    292:                        return(man_nwarn(m, n, WNHEADARGS));
                    293:                default:
                    294:                        if (n->nchild)
                    295:                                break;
                    296:                        return(man_nwarn(m, n, WHEADARGS));
                    297:                }
                    298:
                    299:        return(1);
                    300: }
                    301:
                    302:
                    303: static int
                    304: check_bline(CHKARGS)
                    305: {
                    306:
1.22      kristaps  307:        assert( ! (MAN_ELINE & m->flags));
1.18      kristaps  308:        if (MAN_BLINE & m->flags)
                    309:                return(man_nerr(m, n, WLNSCOPE));
                    310:        return(1);
1.17      kristaps  311: }
                    312:

CVSweb