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

Annotation of mandoc/man_validate.c, Revision 1.40

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

CVSweb