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

Annotation of mandoc/man_validate.c, Revision 1.45

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

CVSweb