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

Annotation of mandoc/mdoc.c, Revision 1.269

1.269   ! schwarze    1: /*     $Id: mdoc.c,v 1.268 2017/08/11 16:56:21 schwarze Exp $ */
1.1       kristaps    2: /*
1.182     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.269   ! schwarze    4:  * Copyright (c) 2010, 2012-2018 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.75      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.241     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.75      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.241     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.75      kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.114     kristaps   18: #include "config.h"
                     19:
1.106     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.211     schwarze   23: #include <ctype.h>
1.1       kristaps   24: #include <stdarg.h>
1.73      kristaps   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
                     27: #include <string.h>
1.120     kristaps   28: #include <time.h>
1.1       kristaps   29:
1.239     schwarze   30: #include "mandoc_aux.h"
                     31: #include "mandoc.h"
                     32: #include "roff.h"
1.187     kristaps   33: #include "mdoc.h"
1.239     schwarze   34: #include "libmandoc.h"
1.247     schwarze   35: #include "roff_int.h"
1.70      kristaps   36: #include "libmdoc.h"
1.1       kristaps   37:
1.213     schwarze   38: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
1.1       kristaps   39:        "split",                "nosplit",              "ragged",
1.213     schwarze   40:        "unfilled",             "literal",              "file",
                     41:        "offset",               "bullet",               "dash",
                     42:        "hyphen",               "item",                 "enum",
                     43:        "tag",                  "diag",                 "hang",
                     44:        "ohang",                "inset",                "column",
                     45:        "width",                "compact",              "std",
1.52      kristaps   46:        "filled",               "words",                "emphasis",
1.108     kristaps   47:        "symbolic",             "nested",               "centered"
1.262     schwarze   48: };
1.1       kristaps   49: const  char * const *mdoc_argnames = __mdoc_argnames;
                     50:
1.242     schwarze   51: static int               mdoc_ptext(struct roff_man *, int, char *, int);
                     52: static int               mdoc_pmacro(struct roff_man *, int, char *, int);
1.88      kristaps   53:
1.181     kristaps   54:
1.50      kristaps   55: /*
1.53      kristaps   56:  * Main parse routine.  Parses a single line -- really just hands off to
1.123     kristaps   57:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps   58:  */
1.20      kristaps   59: int
1.242     schwarze   60: mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1       kristaps   61: {
                     62:
1.239     schwarze   63:        if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
1.228     schwarze   64:                mdoc->flags |= MDOC_NEWLINE;
1.153     schwarze   65:
                     66:        /*
                     67:         * Let the roff nS register switch SYNOPSIS mode early,
                     68:         * such that the parser knows at all times
                     69:         * whether this mode is on or off.
                     70:         * Note that this mode is also switched by the Sh macro.
                     71:         */
1.204     schwarze   72:        if (roff_getreg(mdoc->roff, "nS"))
                     73:                mdoc->flags |= MDOC_SYNOPSIS;
                     74:        else
                     75:                mdoc->flags &= ~MDOC_SYNOPSIS;
1.153     schwarze   76:
1.253     schwarze   77:        return roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213     schwarze   78:            mdoc_pmacro(mdoc, ln, buf, offs) :
1.253     schwarze   79:            mdoc_ptext(mdoc, ln, buf, offs);
1.1       kristaps   80: }
                     81:
1.232     schwarze   82: void
1.262     schwarze   83: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, enum roff_tok tok)
1.17      kristaps   84: {
1.240     schwarze   85:        struct roff_node *p;
1.17      kristaps   86:
1.247     schwarze   87:        p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
                     88:        roff_node_append(mdoc, p);
1.242     schwarze   89:        mdoc->next = ROFF_NEXT_CHILD;
1.152     schwarze   90: }
                     91:
1.240     schwarze   92: struct roff_node *
1.262     schwarze   93: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos,
                     94:     enum roff_tok tok, struct roff_node *body)
1.152     schwarze   95: {
1.240     schwarze   96:        struct roff_node *p;
1.152     schwarze   97:
1.258     schwarze   98:        body->flags |= NODE_ENDED;
                     99:        body->parent->flags |= NODE_ENDED;
1.247     schwarze  100:        p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
1.237     schwarze  101:        p->body = body;
1.202     schwarze  102:        p->norm = body->norm;
1.260     schwarze  103:        p->end = ENDBODY_SPACE;
1.247     schwarze  104:        roff_node_append(mdoc, p);
1.242     schwarze  105:        mdoc->next = ROFF_NEXT_SIBLING;
1.253     schwarze  106:        return p;
1.1       kristaps  107: }
                    108:
1.240     schwarze  109: struct roff_node *
1.242     schwarze  110: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.262     schwarze  111:     enum roff_tok tok, struct mdoc_arg *args)
1.1       kristaps  112: {
1.240     schwarze  113:        struct roff_node *p;
1.1       kristaps  114:
1.247     schwarze  115:        p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
1.77      kristaps  116:        p->args = args;
                    117:        if (p->args)
1.53      kristaps  118:                (args->refcnt)++;
1.172     kristaps  119:
                    120:        switch (tok) {
1.213     schwarze  121:        case MDOC_Bd:
                    122:        case MDOC_Bf:
                    123:        case MDOC_Bl:
1.217     schwarze  124:        case MDOC_En:
1.213     schwarze  125:        case MDOC_Rs:
1.172     kristaps  126:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    127:                break;
                    128:        default:
                    129:                break;
                    130:        }
1.247     schwarze  131:        roff_node_append(mdoc, p);
1.242     schwarze  132:        mdoc->next = ROFF_NEXT_CHILD;
1.253     schwarze  133:        return p;
1.1       kristaps  134: }
                    135:
1.231     schwarze  136: void
1.242     schwarze  137: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.262     schwarze  138:      enum roff_tok tok, struct mdoc_arg *args)
1.1       kristaps  139: {
1.240     schwarze  140:        struct roff_node *p;
1.1       kristaps  141:
1.247     schwarze  142:        p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
1.77      kristaps  143:        p->args = args;
                    144:        if (p->args)
1.53      kristaps  145:                (args->refcnt)++;
1.172     kristaps  146:
                    147:        switch (tok) {
1.213     schwarze  148:        case MDOC_An:
1.172     kristaps  149:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    150:                break;
                    151:        default:
                    152:                break;
                    153:        }
1.247     schwarze  154:        roff_node_append(mdoc, p);
1.242     schwarze  155:        mdoc->next = ROFF_NEXT_CHILD;
1.175     kristaps  156: }
1.1       kristaps  157:
1.231     schwarze  158: void
1.242     schwarze  159: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201     schwarze  160: {
                    161:
1.247     schwarze  162:        roff_node_unlink(mdoc, p);
1.256     schwarze  163:        p->prev = p->next = NULL;
1.247     schwarze  164:        roff_node_append(mdoc, p);
1.1       kristaps  165: }
                    166:
1.53      kristaps  167: /*
                    168:  * Parse free-form text, that is, a line that does not begin with the
                    169:  * control character.
                    170:  */
                    171: static int
1.242     schwarze  172: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1       kristaps  173: {
1.240     schwarze  174:        struct roff_node *n;
1.267     schwarze  175:        const char       *cp, *sp;
1.142     kristaps  176:        char             *c, *ws, *end;
                    177:
1.203     schwarze  178:        n = mdoc->last;
1.142     kristaps  179:
                    180:        /*
1.257     schwarze  181:         * If a column list contains plain text, assume an implicit item
                    182:         * macro.  This can happen one or more times at the beginning
                    183:         * of such a list, intermixed with non-It mdoc macros and with
                    184:         * nodes generated on the roff level, for example by tbl.
1.142     kristaps  185:         */
                    186:
1.257     schwarze  187:        if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
                    188:             n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
                    189:            (n->parent != NULL && n->parent->tok == MDOC_Bl &&
                    190:             n->parent->norm->Bl.type == LIST_column)) {
1.203     schwarze  191:                mdoc->flags |= MDOC_FREECOL;
1.269   ! schwarze  192:                (*mdoc_macro(MDOC_It)->fp)(mdoc, MDOC_It,
        !           193:                    line, offs, &offs, buf);
1.253     schwarze  194:                return 1;
1.142     kristaps  195:        }
1.124     kristaps  196:
1.137     schwarze  197:        /*
                    198:         * Search for the beginning of unescaped trailing whitespace (ws)
                    199:         * and for the first character not to be output (end).
                    200:         */
1.139     kristaps  201:
                    202:        /* FIXME: replace with strcspn(). */
1.137     schwarze  203:        ws = NULL;
                    204:        for (c = end = buf + offs; *c; c++) {
                    205:                switch (*c) {
                    206:                case ' ':
                    207:                        if (NULL == ws)
                    208:                                ws = c;
                    209:                        continue;
                    210:                case '\t':
                    211:                        /*
                    212:                         * Always warn about trailing tabs,
                    213:                         * even outside literal context,
                    214:                         * where they should be put on the next line.
                    215:                         */
                    216:                        if (NULL == ws)
                    217:                                ws = c;
                    218:                        /*
                    219:                         * Strip trailing tabs in literal context only;
                    220:                         * outside, they affect the next line.
                    221:                         */
1.203     schwarze  222:                        if (MDOC_LITERAL & mdoc->flags)
1.137     schwarze  223:                                continue;
                    224:                        break;
                    225:                case '\\':
                    226:                        /* Skip the escaped character, too, if any. */
                    227:                        if (c[1])
                    228:                                c++;
                    229:                        /* FALLTHROUGH */
                    230:                default:
                    231:                        ws = NULL;
                    232:                        break;
                    233:                }
                    234:                end = c + 1;
                    235:        }
                    236:        *end = '\0';
1.91      kristaps  237:
1.137     schwarze  238:        if (ws)
1.218     schwarze  239:                mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                    240:                    line, (int)(ws-buf), NULL);
1.115     kristaps  241:
1.267     schwarze  242:        /*
                    243:         * Blank lines are allowed in no-fill mode
                    244:         * and cancel preceding \c,
                    245:         * but add a single vertical space elsewhere.
                    246:         */
                    247:
1.231     schwarze  248:        if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.267     schwarze  249:                switch (mdoc->last->type) {
                    250:                case ROFFT_TEXT:
                    251:                        sp = mdoc->last->string;
                    252:                        cp = end = strchr(sp, '\0') - 2;
                    253:                        if (cp < sp || cp[0] != '\\' || cp[1] != 'c')
                    254:                                break;
                    255:                        while (cp > sp && cp[-1] == '\\')
                    256:                                cp--;
                    257:                        if ((end - cp) % 2)
                    258:                                break;
                    259:                        *end = '\0';
                    260:                        return 1;
                    261:                default:
                    262:                        break;
                    263:                }
1.218     schwarze  264:                mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
                    265:                    line, (int)(c - buf), NULL);
1.264     schwarze  266:                roff_elem_alloc(mdoc, line, offs, ROFF_sp);
1.258     schwarze  267:                mdoc->last->flags |= NODE_VALID | NODE_ENDED;
1.242     schwarze  268:                mdoc->next = ROFF_NEXT_SIBLING;
1.253     schwarze  269:                return 1;
1.119     kristaps  270:        }
1.68      kristaps  271:
1.249     schwarze  272:        roff_word_alloc(mdoc, line, offs, buf+offs);
1.91      kristaps  273:
1.231     schwarze  274:        if (mdoc->flags & MDOC_LITERAL)
1.253     schwarze  275:                return 1;
1.128     kristaps  276:
                    277:        /*
                    278:         * End-of-sentence check.  If the last character is an unescaped
                    279:         * EOS character, then flag the node as being the end of a
                    280:         * sentence.  The front-end will know how to interpret this.
                    281:         */
1.132     kristaps  282:
1.137     schwarze  283:        assert(buf < end);
                    284:
1.207     schwarze  285:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.258     schwarze  286:                mdoc->last->flags |= NODE_EOS;
1.259     schwarze  287:
                    288:        for (c = buf + offs; c != NULL; c = strchr(c + 1, '.')) {
                    289:                if (c - buf < offs + 2)
                    290:                        continue;
1.265     schwarze  291:                if (end - c < 3)
1.259     schwarze  292:                        break;
1.266     schwarze  293:                if (c[1] != ' ' ||
1.268     schwarze  294:                    isalnum((unsigned char)c[-2]) == 0 ||
                    295:                    isalnum((unsigned char)c[-1]) == 0 ||
1.266     schwarze  296:                    (c[-2] == 'n' && c[-1] == 'c') ||
                    297:                    (c[-2] == 'v' && c[-1] == 's'))
                    298:                        continue;
                    299:                c += 2;
                    300:                if (*c == ' ')
                    301:                        c++;
                    302:                if (*c == ' ')
                    303:                        c++;
                    304:                if (isupper((unsigned char)(*c)))
1.259     schwarze  305:                        mandoc_msg(MANDOCERR_EOS, mdoc->parse,
                    306:                            line, (int)(c - buf), NULL);
                    307:        }
                    308:
1.253     schwarze  309:        return 1;
1.1       kristaps  310: }
                    311:
1.53      kristaps  312: /*
                    313:  * Parse a macro line, that is, a line beginning with the control
                    314:  * character.
                    315:  */
1.155     kristaps  316: static int
1.242     schwarze  317: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1       kristaps  318: {
1.240     schwarze  319:        struct roff_node *n;
1.229     schwarze  320:        const char       *cp;
1.263     schwarze  321:        size_t            sz;
1.262     schwarze  322:        enum roff_tok     tok;
1.263     schwarze  323:        int               sv;
                    324:
                    325:        /* Determine the line macro. */
1.63      kristaps  326:
1.188     kristaps  327:        sv = offs;
1.263     schwarze  328:        tok = TOKEN_NONE;
                    329:        for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
                    330:                offs++;
                    331:        if (sz == 2 || sz == 3)
                    332:                tok = roffhash_find(mdoc->mdocmac, buf + sv, sz);
1.248     schwarze  333:        if (tok == TOKEN_NONE) {
1.222     schwarze  334:                mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
                    335:                    ln, sv, buf + sv - 1);
1.253     schwarze  336:                return 1;
1.53      kristaps  337:        }
1.1       kristaps  338:
1.229     schwarze  339:        /* Skip a leading escape sequence or tab. */
1.160     kristaps  340:
1.229     schwarze  341:        switch (buf[offs]) {
                    342:        case '\\':
                    343:                cp = buf + offs + 1;
                    344:                mandoc_escape(&cp, NULL, NULL);
                    345:                offs = cp - buf;
                    346:                break;
                    347:        case '\t':
1.188     kristaps  348:                offs++;
1.229     schwarze  349:                break;
                    350:        default:
                    351:                break;
                    352:        }
1.160     kristaps  353:
                    354:        /* Jump to the next non-whitespace word. */
1.1       kristaps  355:
1.261     schwarze  356:        while (buf[offs] == ' ')
1.188     kristaps  357:                offs++;
1.1       kristaps  358:
1.213     schwarze  359:        /*
1.125     kristaps  360:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    361:         * into the parser as "text", so we only warn about spaces here.
                    362:         */
1.115     kristaps  363:
1.188     kristaps  364:        if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218     schwarze  365:                mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                    366:                    ln, offs - 1, NULL);
1.115     kristaps  367:
1.144     kristaps  368:        /*
                    369:         * If an initial macro or a list invocation, divert directly
                    370:         * into macro processing.
                    371:         */
                    372:
1.257     schwarze  373:        n = mdoc->last;
                    374:        if (n == NULL || tok == MDOC_It || tok == MDOC_El) {
1.269   ! schwarze  375:                (*mdoc_macro(tok)->fp)(mdoc, tok, ln, sv, &offs, buf);
1.253     schwarze  376:                return 1;
1.232     schwarze  377:        }
1.144     kristaps  378:
                    379:        /*
1.257     schwarze  380:         * If a column list contains a non-It macro, assume an implicit
                    381:         * item macro.  This can happen one or more times at the
                    382:         * beginning of such a list, intermixed with text lines and
                    383:         * with nodes generated on the roff level, for example by tbl.
1.98      kristaps  384:         */
1.144     kristaps  385:
1.257     schwarze  386:        if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
                    387:             n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
                    388:            (n->parent != NULL && n->parent->tok == MDOC_Bl &&
                    389:             n->parent->norm->Bl.type == LIST_column)) {
1.203     schwarze  390:                mdoc->flags |= MDOC_FREECOL;
1.269   ! schwarze  391:                (*mdoc_macro(MDOC_It)->fp)(mdoc, MDOC_It, ln, sv, &sv, buf);
1.253     schwarze  392:                return 1;
1.144     kristaps  393:        }
                    394:
                    395:        /* Normal processing of a macro. */
                    396:
1.269   ! schwarze  397:        (*mdoc_macro(tok)->fp)(mdoc, tok, ln, sv, &offs, buf);
1.208     schwarze  398:
                    399:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    400:
                    401:        if (mdoc->quick && MDOC_Sh == tok &&
                    402:            SEC_NAME != mdoc->last->sec)
1.253     schwarze  403:                return 2;
1.1       kristaps  404:
1.253     schwarze  405:        return 1;
1.1       kristaps  406: }
1.100     kristaps  407:
1.186     kristaps  408: enum mdelim
                    409: mdoc_isdelim(const char *p)
                    410: {
                    411:
                    412:        if ('\0' == p[0])
1.253     schwarze  413:                return DELIM_NONE;
1.186     kristaps  414:
                    415:        if ('\0' == p[1])
                    416:                switch (p[0]) {
1.213     schwarze  417:                case '(':
                    418:                case '[':
1.253     schwarze  419:                        return DELIM_OPEN;
1.213     schwarze  420:                case '|':
1.253     schwarze  421:                        return DELIM_MIDDLE;
1.213     schwarze  422:                case '.':
                    423:                case ',':
                    424:                case ';':
                    425:                case ':':
                    426:                case '?':
                    427:                case '!':
                    428:                case ')':
                    429:                case ']':
1.253     schwarze  430:                        return DELIM_CLOSE;
1.186     kristaps  431:                default:
1.253     schwarze  432:                        return DELIM_NONE;
1.186     kristaps  433:                }
                    434:
                    435:        if ('\\' != p[0])
1.253     schwarze  436:                return DELIM_NONE;
1.100     kristaps  437:
1.186     kristaps  438:        if (0 == strcmp(p + 1, "."))
1.253     schwarze  439:                return DELIM_CLOSE;
1.200     schwarze  440:        if (0 == strcmp(p + 1, "fR|\\fP"))
1.253     schwarze  441:                return DELIM_MIDDLE;
1.186     kristaps  442:
1.253     schwarze  443:        return DELIM_NONE;
1.255     schwarze  444: }
                    445:
                    446: void
                    447: mdoc_validate(struct roff_man *mdoc)
                    448: {
                    449:
                    450:        mdoc->last = mdoc->first;
                    451:        mdoc_node_validate(mdoc);
                    452:        mdoc_state_reset(mdoc);
1.186     kristaps  453: }

CVSweb