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

Annotation of mandoc/man_validate.c, Revision 1.34

1.34    ! kristaps    1: /*     $Id: man_validate.c,v 1.33 2010/03/29 10:10:35 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:  */
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>
                     29:
                     30: #include "libman.h"
1.15      kristaps   31: #include "libmandoc.h"
1.1       kristaps   32:
1.17      kristaps   33: #define        CHKARGS   struct man *m, const struct man_node *n
1.1       kristaps   34:
1.17      kristaps   35: typedef        int     (*v_check)(CHKARGS);
1.1       kristaps   36:
                     37: struct man_valid {
1.17      kristaps   38:        v_check  *pres;
                     39:        v_check  *posts;
1.1       kristaps   40: };
                     41:
1.17      kristaps   42: static int       check_bline(CHKARGS);
                     43: static int       check_eq0(CHKARGS);
1.25      kristaps   44: static int       check_le1(CHKARGS);
1.17      kristaps   45: static int       check_ge2(CHKARGS);
                     46: static int       check_le5(CHKARGS);
                     47: static int       check_par(CHKARGS);
1.23      kristaps   48: static int       check_part(CHKARGS);
1.31      kristaps   49: static int       check_roff(CHKARGS);
1.17      kristaps   50: static int       check_root(CHKARGS);
                     51: static int       check_sec(CHKARGS);
                     52: static int       check_text(CHKARGS);
1.32      kristaps   53: static int       check_title(CHKARGS);
1.17      kristaps   54:
                     55: static v_check   posts_eq0[] = { check_eq0, NULL };
1.32      kristaps   56: static v_check   posts_th[] = { check_ge2, check_le5, check_title, NULL };
1.17      kristaps   57: static v_check   posts_par[] = { check_par, NULL };
1.23      kristaps   58: static v_check   posts_part[] = { check_part, NULL };
1.17      kristaps   59: static v_check   posts_sec[] = { check_sec, NULL };
1.30      kristaps   60: static v_check   posts_le1[] = { check_le1, NULL };
1.17      kristaps   61: static v_check   pres_bline[] = { check_bline, NULL };
1.33      kristaps   62: static v_check   pres_roff[] = { check_roff, 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.29      kristaps   86:        { NULL, posts_eq0 }, /* na */
1.17      kristaps   87:        { NULL, NULL }, /* i */
1.30      kristaps   88:        { NULL, posts_le1 }, /* sp */
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.30      kristaps   97:        { NULL, posts_eq0 }, /* Sp */
                     98:        { pres_bline, posts_le1 }, /* Vb */
                     99:        { pres_bline, posts_eq0 }, /* Ve */
1.31      kristaps  100:        { pres_roff, NULL }, /* de */
                    101:        { pres_roff, NULL }, /* dei */
                    102:        { pres_roff, NULL }, /* am */
                    103:        { pres_roff, NULL }, /* ami */
                    104:        { pres_roff, NULL }, /* ig */
                    105:        { NULL, NULL }, /* . */
1.1       kristaps  106: };
                    107:
                    108:
                    109: int
1.17      kristaps  110: man_valid_pre(struct man *m, const struct man_node *n)
                    111: {
                    112:        v_check         *cp;
                    113:
                    114:        if (MAN_TEXT == n->type)
                    115:                return(1);
                    116:        if (MAN_ROOT == n->type)
                    117:                return(1);
                    118:
                    119:        if (NULL == (cp = man_valids[n->tok].pres))
                    120:                return(1);
                    121:        for ( ; *cp; cp++)
                    122:                if ( ! (*cp)(m, n))
                    123:                        return(0);
                    124:        return(1);
                    125: }
                    126:
                    127:
                    128: int
1.1       kristaps  129: man_valid_post(struct man *m)
                    130: {
1.17      kristaps  131:        v_check         *cp;
1.1       kristaps  132:
                    133:        if (MAN_VALID & m->last->flags)
                    134:                return(1);
                    135:        m->last->flags |= MAN_VALID;
                    136:
                    137:        switch (m->last->type) {
                    138:        case (MAN_TEXT):
1.11      kristaps  139:                return(check_text(m, m->last));
1.1       kristaps  140:        case (MAN_ROOT):
1.14      kristaps  141:                return(check_root(m, m->last));
1.1       kristaps  142:        default:
                    143:                break;
                    144:        }
                    145:
                    146:        if (NULL == (cp = man_valids[m->last->tok].posts))
                    147:                return(1);
                    148:        for ( ; *cp; cp++)
                    149:                if ( ! (*cp)(m, m->last))
                    150:                        return(0);
                    151:
                    152:        return(1);
                    153: }
                    154:
                    155:
1.11      kristaps  156: static int
1.17      kristaps  157: check_root(CHKARGS)
1.14      kristaps  158: {
1.17      kristaps  159:
                    160:        if (MAN_BLINE & m->flags)
1.22      kristaps  161:                return(man_nwarn(m, n, WEXITSCOPE));
1.17      kristaps  162:        if (MAN_ELINE & m->flags)
1.22      kristaps  163:                return(man_nwarn(m, n, WEXITSCOPE));
                    164:
                    165:        m->flags &= ~MAN_BLINE;
                    166:        m->flags &= ~MAN_ELINE;
1.17      kristaps  167:
1.14      kristaps  168:        if (NULL == m->first->child)
                    169:                return(man_nerr(m, n, WNODATA));
1.34    ! kristaps  170:        if (NULL == m->meta.title) {
        !           171:                if ( ! man_nwarn(m, n, WNOTITLE))
        !           172:                        return(0);
        !           173:                /*
        !           174:                 * If a title hasn't been set, do so now (by
        !           175:                 * implication, date and section also aren't set).
        !           176:                 *
        !           177:                 * FIXME: this should be in man_action.c.
        !           178:                 */
        !           179:                m->meta.title = mandoc_strdup("unknown");
        !           180:                m->meta.date = time(NULL);
        !           181:                m->meta.msec = 1;
        !           182:        }
1.32      kristaps  183:
                    184:        return(1);
                    185: }
                    186:
                    187:
                    188: static int
                    189: check_title(CHKARGS)
                    190: {
                    191:        const char      *p;
                    192:
                    193:        assert(n->child);
                    194:        if ('\0' == *n->child->string)
                    195:                return(man_nerr(m, n, WNOTITLE));
                    196:
                    197:        for (p = n->child->string; '\0' != *p; p++)
                    198:                if (isalpha((u_char)*p) && ! isupper((u_char)*p))
                    199:                        if ( ! man_nwarn(m, n, WTITLECASE))
                    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: {
                    209:        const char      *p;
1.15      kristaps  210:        int              pos, c;
1.11      kristaps  211:
                    212:        assert(n->string);
                    213:
                    214:        for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
1.15      kristaps  215:                if ('\\' == *p) {
                    216:                        c = mandoc_special(p);
                    217:                        if (c) {
                    218:                                p += c - 1;
                    219:                                pos += c - 1;
                    220:                                continue;
                    221:                        }
                    222:                        if ( ! (MAN_IGN_ESCAPE & m->pflags))
                    223:                                return(man_perr(m, n->line, pos, WESCAPE));
                    224:                        if ( ! man_pwarn(m, n->line, pos, WESCAPE))
                    225:                                return(0);
                    226:                        continue;
                    227:                }
                    228:
                    229:                if ('\t' == *p || isprint((u_char)*p))
1.11      kristaps  230:                        continue;
                    231:
                    232:                if (MAN_IGN_CHARS & m->pflags)
1.12      kristaps  233:                        return(man_pwarn(m, n->line, pos, WNPRINT));
                    234:                return(man_perr(m, n->line, pos, WNPRINT));
1.11      kristaps  235:        }
                    236:
                    237:        return(1);
1.1       kristaps  238: }
                    239:
                    240:
                    241: #define        INEQ_DEFINE(x, ineq, name) \
                    242: static int \
1.17      kristaps  243: check_##name(CHKARGS) \
1.1       kristaps  244: { \
1.11      kristaps  245:        if (n->nchild ineq (x)) \
1.1       kristaps  246:                return(1); \
1.4       kristaps  247:        return(man_verr(m, n->line, n->pos, \
1.1       kristaps  248:                        "expected line arguments %s %d, have %d", \
1.11      kristaps  249:                        #ineq, (x), n->nchild)); \
1.1       kristaps  250: }
                    251:
                    252: INEQ_DEFINE(0, ==, eq0)
1.25      kristaps  253: INEQ_DEFINE(1, <=, le1)
1.1       kristaps  254: INEQ_DEFINE(2, >=, ge2)
                    255: INEQ_DEFINE(5, <=, le5)
                    256:
1.16      kristaps  257:
                    258: static int
1.17      kristaps  259: check_sec(CHKARGS)
                    260: {
1.16      kristaps  261:
1.17      kristaps  262:        if (MAN_BODY == n->type && 0 == n->nchild)
                    263:                return(man_nwarn(m, n, WBODYARGS));
                    264:        if (MAN_HEAD == n->type && 0 == n->nchild)
                    265:                return(man_nerr(m, n, WHEADARGS));
1.16      kristaps  266:        return(1);
                    267: }
1.17      kristaps  268:
                    269:
                    270: static int
1.23      kristaps  271: check_part(CHKARGS)
                    272: {
                    273:
                    274:        if (MAN_BODY == n->type && 0 == n->nchild)
                    275:                return(man_nwarn(m, n, WBODYARGS));
                    276:        return(1);
                    277: }
                    278:
                    279:
                    280: static int
1.17      kristaps  281: check_par(CHKARGS)
                    282: {
                    283:
                    284:        if (MAN_BODY == n->type)
                    285:                switch (n->tok) {
                    286:                case (MAN_IP):
                    287:                        /* FALLTHROUGH */
                    288:                case (MAN_HP):
                    289:                        /* FALLTHROUGH */
                    290:                case (MAN_TP):
                    291:                        /* Body-less lists are ok. */
                    292:                        break;
                    293:                default:
                    294:                        if (n->nchild)
                    295:                                break;
                    296:                        return(man_nwarn(m, n, WBODYARGS));
                    297:                }
                    298:        if (MAN_HEAD == n->type)
                    299:                switch (n->tok) {
                    300:                case (MAN_PP):
                    301:                        /* FALLTHROUGH */
                    302:                case (MAN_P):
                    303:                        /* FALLTHROUGH */
                    304:                case (MAN_LP):
                    305:                        if (0 == n->nchild)
                    306:                                break;
                    307:                        return(man_nwarn(m, n, WNHEADARGS));
                    308:                default:
                    309:                        if (n->nchild)
                    310:                                break;
                    311:                        return(man_nwarn(m, n, WHEADARGS));
                    312:                }
                    313:
                    314:        return(1);
                    315: }
                    316:
                    317:
                    318: static int
                    319: check_bline(CHKARGS)
                    320: {
                    321:
1.22      kristaps  322:        assert( ! (MAN_ELINE & m->flags));
1.18      kristaps  323:        if (MAN_BLINE & m->flags)
                    324:                return(man_nerr(m, n, WLNSCOPE));
1.31      kristaps  325:
1.18      kristaps  326:        return(1);
1.17      kristaps  327: }
                    328:
1.31      kristaps  329:
                    330: static int
                    331: check_roff(CHKARGS)
                    332: {
                    333:
                    334:        if (MAN_BLOCK != n->type)
                    335:                return(1);
                    336:
                    337:        for (n = n->parent; n; n = n->parent)
                    338:                if (MAN_de == n->tok || MAN_dei == n->tok ||
                    339:                                MAN_am == n->tok ||
                    340:                                MAN_ami == n->tok ||
                    341:                                MAN_ig == n->tok)
                    342:                        return(man_nerr(m, n, WROFFNEST));
                    343:
                    344:        return(1);
                    345: }

CVSweb