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

Annotation of mandoc/man_validate.c, Revision 1.39

1.39    ! kristaps    1: /*     $Id: man_validate.c,v 1.38 2010/05/15 20:51:40 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.1       kristaps   98: };
                     99:
                    100:
                    101: int
1.17      kristaps  102: man_valid_pre(struct man *m, const struct man_node *n)
                    103: {
                    104:        v_check         *cp;
                    105:
                    106:        if (MAN_TEXT == n->type)
                    107:                return(1);
                    108:        if (MAN_ROOT == n->type)
                    109:                return(1);
                    110:
                    111:        if (NULL == (cp = man_valids[n->tok].pres))
                    112:                return(1);
                    113:        for ( ; *cp; cp++)
                    114:                if ( ! (*cp)(m, n))
                    115:                        return(0);
                    116:        return(1);
                    117: }
                    118:
                    119:
                    120: int
1.1       kristaps  121: man_valid_post(struct man *m)
                    122: {
1.17      kristaps  123:        v_check         *cp;
1.1       kristaps  124:
                    125:        if (MAN_VALID & m->last->flags)
                    126:                return(1);
                    127:        m->last->flags |= MAN_VALID;
                    128:
                    129:        switch (m->last->type) {
                    130:        case (MAN_TEXT):
1.11      kristaps  131:                return(check_text(m, m->last));
1.1       kristaps  132:        case (MAN_ROOT):
1.14      kristaps  133:                return(check_root(m, m->last));
1.1       kristaps  134:        default:
                    135:                break;
                    136:        }
                    137:
                    138:        if (NULL == (cp = man_valids[m->last->tok].posts))
                    139:                return(1);
                    140:        for ( ; *cp; cp++)
                    141:                if ( ! (*cp)(m, m->last))
                    142:                        return(0);
                    143:
                    144:        return(1);
                    145: }
                    146:
                    147:
1.11      kristaps  148: static int
1.17      kristaps  149: check_root(CHKARGS)
1.14      kristaps  150: {
1.17      kristaps  151:
                    152:        if (MAN_BLINE & m->flags)
1.22      kristaps  153:                return(man_nwarn(m, n, WEXITSCOPE));
1.17      kristaps  154:        if (MAN_ELINE & m->flags)
1.22      kristaps  155:                return(man_nwarn(m, n, WEXITSCOPE));
                    156:
                    157:        m->flags &= ~MAN_BLINE;
                    158:        m->flags &= ~MAN_ELINE;
1.17      kristaps  159:
1.14      kristaps  160:        if (NULL == m->first->child)
                    161:                return(man_nerr(m, n, WNODATA));
1.34      kristaps  162:        if (NULL == m->meta.title) {
                    163:                if ( ! man_nwarn(m, n, WNOTITLE))
                    164:                        return(0);
                    165:                /*
                    166:                 * If a title hasn't been set, do so now (by
                    167:                 * implication, date and section also aren't set).
                    168:                 *
                    169:                 * FIXME: this should be in man_action.c.
                    170:                 */
                    171:                m->meta.title = mandoc_strdup("unknown");
                    172:                m->meta.date = time(NULL);
1.37      kristaps  173:                m->meta.msec = mandoc_strdup("1");
1.34      kristaps  174:        }
1.32      kristaps  175:
                    176:        return(1);
                    177: }
                    178:
                    179:
                    180: static int
                    181: check_title(CHKARGS)
                    182: {
                    183:        const char      *p;
                    184:
                    185:        assert(n->child);
                    186:        if ('\0' == *n->child->string)
                    187:                return(man_nerr(m, n, WNOTITLE));
                    188:
                    189:        for (p = n->child->string; '\0' != *p; p++)
                    190:                if (isalpha((u_char)*p) && ! isupper((u_char)*p))
                    191:                        if ( ! man_nwarn(m, n, WTITLECASE))
                    192:                                return(0);
1.14      kristaps  193:
                    194:        return(1);
                    195: }
                    196:
                    197:
                    198: static int
1.17      kristaps  199: check_text(CHKARGS)
1.11      kristaps  200: {
                    201:        const char      *p;
1.15      kristaps  202:        int              pos, c;
1.11      kristaps  203:
                    204:        assert(n->string);
                    205:
                    206:        for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
1.15      kristaps  207:                if ('\\' == *p) {
                    208:                        c = mandoc_special(p);
                    209:                        if (c) {
                    210:                                p += c - 1;
                    211:                                pos += c - 1;
                    212:                                continue;
                    213:                        }
                    214:                        if ( ! (MAN_IGN_ESCAPE & m->pflags))
                    215:                                return(man_perr(m, n->line, pos, WESCAPE));
                    216:                        if ( ! man_pwarn(m, n->line, pos, WESCAPE))
                    217:                                return(0);
                    218:                        continue;
                    219:                }
                    220:
                    221:                if ('\t' == *p || isprint((u_char)*p))
1.11      kristaps  222:                        continue;
                    223:
1.35      kristaps  224:                return(man_pwarn(m, n->line, pos, WNPRINT));
1.11      kristaps  225:        }
                    226:
                    227:        return(1);
1.1       kristaps  228: }
                    229:
                    230:
                    231: #define        INEQ_DEFINE(x, ineq, name) \
                    232: static int \
1.17      kristaps  233: check_##name(CHKARGS) \
1.1       kristaps  234: { \
1.11      kristaps  235:        if (n->nchild ineq (x)) \
1.1       kristaps  236:                return(1); \
1.4       kristaps  237:        return(man_verr(m, n->line, n->pos, \
1.1       kristaps  238:                        "expected line arguments %s %d, have %d", \
1.11      kristaps  239:                        #ineq, (x), n->nchild)); \
1.1       kristaps  240: }
                    241:
                    242: INEQ_DEFINE(0, ==, eq0)
1.25      kristaps  243: INEQ_DEFINE(1, <=, le1)
1.1       kristaps  244: INEQ_DEFINE(2, >=, ge2)
                    245: INEQ_DEFINE(5, <=, le5)
                    246:
1.16      kristaps  247:
                    248: static int
1.17      kristaps  249: check_sec(CHKARGS)
                    250: {
1.16      kristaps  251:
1.17      kristaps  252:        if (MAN_BODY == n->type && 0 == n->nchild)
                    253:                return(man_nwarn(m, n, WBODYARGS));
                    254:        if (MAN_HEAD == n->type && 0 == n->nchild)
                    255:                return(man_nerr(m, n, WHEADARGS));
1.16      kristaps  256:        return(1);
                    257: }
1.17      kristaps  258:
                    259:
                    260: static int
1.23      kristaps  261: check_part(CHKARGS)
                    262: {
                    263:
                    264:        if (MAN_BODY == n->type && 0 == n->nchild)
                    265:                return(man_nwarn(m, n, WBODYARGS));
                    266:        return(1);
                    267: }
                    268:
                    269:
                    270: static int
1.17      kristaps  271: check_par(CHKARGS)
                    272: {
                    273:
                    274:        if (MAN_BODY == n->type)
                    275:                switch (n->tok) {
                    276:                case (MAN_IP):
                    277:                        /* FALLTHROUGH */
                    278:                case (MAN_HP):
                    279:                        /* FALLTHROUGH */
                    280:                case (MAN_TP):
                    281:                        /* Body-less lists are ok. */
                    282:                        break;
                    283:                default:
                    284:                        if (n->nchild)
                    285:                                break;
                    286:                        return(man_nwarn(m, n, WBODYARGS));
                    287:                }
                    288:        if (MAN_HEAD == n->type)
                    289:                switch (n->tok) {
                    290:                case (MAN_PP):
                    291:                        /* FALLTHROUGH */
                    292:                case (MAN_P):
                    293:                        /* FALLTHROUGH */
                    294:                case (MAN_LP):
                    295:                        if (0 == n->nchild)
                    296:                                break;
                    297:                        return(man_nwarn(m, n, WNHEADARGS));
                    298:                default:
                    299:                        if (n->nchild)
                    300:                                break;
                    301:                        return(man_nwarn(m, n, WHEADARGS));
                    302:                }
                    303:
                    304:        return(1);
                    305: }
                    306:
                    307:
                    308: static int
                    309: check_bline(CHKARGS)
                    310: {
                    311:
1.22      kristaps  312:        assert( ! (MAN_ELINE & m->flags));
1.18      kristaps  313:        if (MAN_BLINE & m->flags)
                    314:                return(man_nerr(m, n, WLNSCOPE));
1.31      kristaps  315:
1.18      kristaps  316:        return(1);
1.17      kristaps  317: }
                    318:

CVSweb