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

Annotation of mandoc/macro.c, Revision 1.4

1.4     ! kristaps    1: /* $Id: macro.c,v 1.3 2008/12/15 02:23:12 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.2       kristaps   19: #include <assert.h>
                     20: #include <ctype.h>
1.1       kristaps   21: #include <stdlib.h>
1.2       kristaps   22: #include <stdio.h>
                     23:
                     24: #include "private.h"
                     25:
1.3       kristaps   26: #define        _CC(p)  ((const char **)p)
1.2       kristaps   27:
1.3       kristaps   28: static int       append_text(struct mdoc *, int,
                     29:                        int, int, char *[]);
                     30: static int       append_scoped(struct mdoc *, int,
                     31:                        int, int, char *[]);
                     32: static int       args_next(struct mdoc *, int,
                     33:                        int *, char *, char **);
1.1       kristaps   34:
                     35:
                     36: static int
1.2       kristaps   37: args_next(struct mdoc *mdoc, int tok,
                     38:                int *pos, char *buf, char **v)
1.1       kristaps   39: {
                     40:
                     41:        if (0 == buf[*pos])
                     42:                return(0);
                     43:
1.2       kristaps   44:        assert( ! isspace(buf[*pos]));
                     45:
1.1       kristaps   46:        if ('\"' == buf[*pos]) {
1.2       kristaps   47:                (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_QUOTE);
1.1       kristaps   48:                return(-1);
                     49:        }
                     50:
                     51:        *v = &buf[*pos];
                     52:
1.2       kristaps   53:        /* Scan ahead to end of token. */
                     54:
1.1       kristaps   55:        while (buf[*pos] && ! isspace(buf[*pos]))
                     56:                (*pos)++;
                     57:
1.2       kristaps   58:        if (buf[*pos] && buf[*pos + 1] && '\\' == buf[*pos]) {
                     59:                (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_WS);
1.1       kristaps   60:                return(-1);
                     61:        }
                     62:
1.2       kristaps   63:        if (0 == buf[*pos])
                     64:                return(1);
                     65:
                     66:        /* Scan ahead over trailing whitespace. */
                     67:
                     68:        buf[(*pos)++] = 0;
                     69:        while (buf[*pos] && isspace(buf[*pos]))
                     70:                (*pos)++;
                     71:
                     72:        if (0 == buf[*pos])
                     73:                if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
                     74:                        return(-1);
                     75:
1.1       kristaps   76:        return(1);
                     77: }
                     78:
1.2       kristaps   79:
                     80: static int
1.3       kristaps   81: append_scoped(struct mdoc *mdoc, int tok,
                     82:                int pos, int sz, char *args[])
1.2       kristaps   83: {
                     84:
1.4     ! kristaps   85:        switch (tok) {
        !            86:         /* ======= ADD MORE MACRO CHECKS BELOW. ======= */
        !            87:        case (MDOC_Sh):
        !            88:                break;
        !            89:        case (MDOC_Ss):
        !            90:                break;
        !            91:         /* ======= ADD MORE MACRO CHECKS ABOVE. ======= */
        !            92:        default:
        !            93:                abort();
        !            94:                /* NOTREACHED */
        !            95:        }
        !            96:
1.3       kristaps   97:        assert(sz >= 0);
1.2       kristaps   98:        args[sz] = NULL;
                     99:        mdoc_block_alloc(mdoc, pos, tok, 0, NULL);
1.3       kristaps  100:        mdoc_head_alloc(mdoc, pos, tok, (size_t)sz, _CC(args));
1.2       kristaps  101:        mdoc_body_alloc(mdoc, pos, tok);
                    102:        return(1);
                    103: }
                    104:
                    105:
1.1       kristaps  106: static int
1.3       kristaps  107: append_text(struct mdoc *mdoc, int tok,
                    108:                int pos, int sz, char *args[])
1.1       kristaps  109: {
                    110:
1.3       kristaps  111:        assert(sz >= 0);
1.2       kristaps  112:        args[sz] = NULL;
                    113:
                    114:        switch (tok) {
1.4     ! kristaps  115:         /* ======= ADD MORE MACRO CHECKS BELOW. ======= */
1.2       kristaps  116:        case (MDOC_Ft):
                    117:                /* FALLTHROUGH */
                    118:        case (MDOC_Li):
                    119:                /* FALLTHROUGH */
                    120:        case (MDOC_Ms):
                    121:                /* FALLTHROUGH */
                    122:        case (MDOC_Pa):
                    123:                /* FALLTHROUGH */
                    124:        case (MDOC_Tn):
1.4     ! kristaps  125:                if (0 < sz)
        !           126:                        break;
        !           127:                if ( ! mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1))
1.1       kristaps  128:                        return(0);
1.4     ! kristaps  129:                break;
1.2       kristaps  130:        case (MDOC_Ar):
                    131:                /* FALLTHROUGH */
                    132:        case (MDOC_Cm):
                    133:                /* FALLTHROUGH */
                    134:        case (MDOC_Fl):
1.4     ! kristaps  135:                break;
1.2       kristaps  136:        case (MDOC_Ad):
                    137:                /* FALLTHROUGH */
                    138:        case (MDOC_Em):
                    139:                /* FALLTHROUGH */
                    140:        case (MDOC_Er):
                    141:                /* FALLTHROUGH */
                    142:        case (MDOC_Ev):
                    143:                /* FALLTHROUGH */
                    144:        case (MDOC_Fa):
                    145:                /* FALLTHROUGH */
                    146:        case (MDOC_Dv):
                    147:                /* FALLTHROUGH */
                    148:        case (MDOC_Ic):
                    149:                /* FALLTHROUGH */
                    150:        case (MDOC_Va):
                    151:                /* FALLTHROUGH */
                    152:        case (MDOC_Vt):
1.4     ! kristaps  153:                if (0 < sz)
        !           154:                        break;
        !           155:                return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
        !           156:         /* ======= ADD MORE MACRO CHECKS ABOVE. ======= */
1.2       kristaps  157:        default:
1.4     ! kristaps  158:                abort();
        !           159:                /* NOTREACHED */
1.2       kristaps  160:        }
                    161:
1.4     ! kristaps  162:        mdoc_elem_alloc(mdoc, pos, tok, 0,
        !           163:                        NULL, (size_t)sz, _CC(args));
        !           164:        return(1);
1.2       kristaps  165: }
                    166:
1.1       kristaps  167:
1.2       kristaps  168: int
                    169: macro_text(struct mdoc *mdoc, int tok, int ppos, int *pos, char *buf)
                    170: {
1.3       kristaps  171:        int               lastarg, c, lasttok, lastpunct, j;
1.2       kristaps  172:        char             *args[MDOC_LINEARG_MAX], *p;
1.1       kristaps  173:
1.2       kristaps  174:        lasttok = ppos;
                    175:        lastpunct = 0;
                    176:        j = 0;
                    177:
                    178: again:
                    179:
                    180:        lastarg = *pos;
                    181:        c = args_next(mdoc, tok, pos, buf, &args[j]);
                    182:
                    183:        if (-1 == c)
                    184:                return(0);
                    185:        if (0 == c && ! lastpunct)
                    186:                return(append_text(mdoc, tok, lasttok, j, args));
                    187:        else if (0 == c)
                    188:                return(1);
                    189:
                    190:        /* Command found. */
                    191:
                    192:        if (MDOC_MAX != (c = mdoc_find(mdoc, args[j]))) {
                    193:                if ( ! lastpunct)
                    194:                        if ( ! append_text(mdoc, tok, lasttok, j, args))
1.1       kristaps  195:                                return(0);
1.2       kristaps  196:                return(mdoc_macro(mdoc, c, lastarg, pos, buf));
                    197:        }
                    198:
                    199:        /* Word found. */
                    200:
1.4     ! kristaps  201:        if ( ! mdoc_isdelim(args[j])) {
1.2       kristaps  202:                j++;
                    203:                goto again;
                    204:        }
                    205:
                    206:        /* Punctuation found.  */
                    207:
                    208:        p = args[j]; /* Save argument (NULL-ified in append). */
                    209:
                    210:        if ( ! lastpunct)
                    211:                if ( ! append_text(mdoc, tok, lasttok, j, args))
                    212:                        return(0);
                    213:
                    214:        args[j] = p;
                    215:
                    216:        mdoc_word_alloc(mdoc, lastarg, args[j]);
                    217:        lastpunct = 1;
                    218:        j = 0;
                    219:
                    220:        goto again;
                    221:
                    222:        /* NOTREACHED */
                    223: }
1.1       kristaps  224:
                    225:
1.2       kristaps  226: int
                    227: macro_scoped_implicit(struct mdoc *mdoc,
                    228:                int tok, int ppos, int *pos, char *buf)
                    229: {
1.3       kristaps  230:        int               t, c, lastarg, j;
1.2       kristaps  231:        char             *args[MDOC_LINEARG_MAX];
                    232:        struct mdoc_node *n;
                    233:
                    234:        /*
                    235:         * Look for an implicit parent.
                    236:         */
1.1       kristaps  237:
1.2       kristaps  238:        assert( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
1.1       kristaps  239:
1.3       kristaps  240:        /* LINTED */
1.2       kristaps  241:        for (n = mdoc->last; n; n = n->parent) {
                    242:                if (MDOC_BLOCK != n->type)
                    243:                        continue;
                    244:                if (tok == (t = n->data.block.tok))
1.1       kristaps  245:                        break;
1.2       kristaps  246:                if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags))
                    247:                        continue;
                    248:                return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK));
                    249:        }
                    250:
                    251:        if (n) {
                    252:                mdoc->last = n;
                    253:                mdoc_msg(mdoc, ppos, "scope: rewound `%s'",
                    254:                                mdoc_macronames[tok]);
                    255:        } else
                    256:                mdoc_msg(mdoc, ppos, "scope: new `%s'",
                    257:                                mdoc_macronames[tok]);
                    258:
                    259:        j = 0;
                    260:
                    261: again:
                    262:
                    263:        lastarg = *pos;
                    264:        c = args_next(mdoc, tok, pos, buf, &args[j]);
                    265:
                    266:        if (-1 == c)
                    267:                return(0);
                    268:        if (0 == c)
                    269:                return(append_scoped(mdoc, tok, ppos, j, args));
1.1       kristaps  270:
1.2       kristaps  271:        /* Command found. */
1.1       kristaps  272:
1.2       kristaps  273:        if (MDOC_MAX != (c = mdoc_find(mdoc, args[j])))
1.3       kristaps  274:                if ( ! mdoc_warn(mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE))
1.1       kristaps  275:                        return(0);
                    276:
1.2       kristaps  277:        /* Word found. */
                    278:
                    279:        j++;
                    280:        goto again;
1.1       kristaps  281:
1.2       kristaps  282:        /* NOTREACHED */
1.1       kristaps  283: }

CVSweb