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

Annotation of mandoc/mdoc.c, Revision 1.247

1.247   ! schwarze    1: /*     $Id: mdoc.c,v 1.246 2015/04/18 17:53:21 schwarze Exp $ */
1.1       kristaps    2: /*
1.182     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.237     schwarze    4:  * Copyright (c) 2010, 2012-2015 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.216     schwarze   38: const  char *const __mdoc_macronames[MDOC_MAX + 1] = {
1.82      kristaps   39:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   40:        "Sh",           "Ss",           "Pp",           "D1",
                     41:        "Dl",           "Bd",           "Ed",           "Bl",
                     42:        "El",           "It",           "Ad",           "An",
                     43:        "Ar",           "Cd",           "Cm",           "Dv",
                     44:        "Er",           "Ev",           "Ex",           "Fa",
                     45:        "Fd",           "Fl",           "Fn",           "Ft",
                     46:        "Ic",           "In",           "Li",           "Nd",
                     47:        "Nm",           "Op",           "Ot",           "Pa",
                     48:        "Rv",           "St",           "Va",           "Vt",
1.114     kristaps   49:        "Xr",           "%A",           "%B",           "%D",
                     50:        "%I",           "%J",           "%N",           "%O",
                     51:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps   52:        "Ac",           "Ao",           "Aq",           "At",
                     53:        "Bc",           "Bf",           "Bo",           "Bq",
                     54:        "Bsx",          "Bx",           "Db",           "Dc",
                     55:        "Do",           "Dq",           "Ec",           "Ef",
                     56:        "Em",           "Eo",           "Fx",           "Ms",
                     57:        "No",           "Ns",           "Nx",           "Ox",
                     58:        "Pc",           "Pf",           "Po",           "Pq",
                     59:        "Qc",           "Ql",           "Qo",           "Qq",
                     60:        "Re",           "Rs",           "Sc",           "So",
                     61:        "Sq",           "Sm",           "Sx",           "Sy",
                     62:        "Tn",           "Ux",           "Xc",           "Xo",
                     63:        "Fo",           "Fc",           "Oo",           "Oc",
                     64:        "Bk",           "Ek",           "Bt",           "Hf",
1.82      kristaps   65:        "Fr",           "Ud",           "Lb",           "Lp",
                     66:        "Lk",           "Mt",           "Brq",          "Bro",
1.114     kristaps   67:        "Brc",          "%C",           "Es",           "En",
                     68:        "Dx",           "%Q",           "br",           "sp",
1.216     schwarze   69:        "%U",           "Ta",           "ll",           "text",
1.1       kristaps   70:        };
                     71:
1.213     schwarze   72: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
1.1       kristaps   73:        "split",                "nosplit",              "ragged",
1.213     schwarze   74:        "unfilled",             "literal",              "file",
                     75:        "offset",               "bullet",               "dash",
                     76:        "hyphen",               "item",                 "enum",
                     77:        "tag",                  "diag",                 "hang",
                     78:        "ohang",                "inset",                "column",
                     79:        "width",                "compact",              "std",
1.52      kristaps   80:        "filled",               "words",                "emphasis",
1.108     kristaps   81:        "symbolic",             "nested",               "centered"
1.1       kristaps   82:        };
                     83:
                     84: const  char * const *mdoc_macronames = __mdoc_macronames;
                     85: const  char * const *mdoc_argnames = __mdoc_argnames;
                     86:
1.242     schwarze   87: static int               mdoc_ptext(struct roff_man *, int, char *, int);
                     88: static int               mdoc_pmacro(struct roff_man *, int, char *, int);
1.88      kristaps   89:
1.1       kristaps   90:
1.234     schwarze   91: void
1.242     schwarze   92: mdoc_endparse(struct roff_man *mdoc)
1.20      kristaps   93: {
                     94:
1.232     schwarze   95:        mdoc_macroend(mdoc);
1.181     kristaps   96: }
                     97:
1.233     schwarze   98: void
1.242     schwarze   99: mdoc_addeqn(struct roff_man *mdoc, const struct eqn *ep)
1.181     kristaps  100: {
1.240     schwarze  101:        struct roff_node *n;
1.181     kristaps  102:
1.247   ! schwarze  103:        n = roff_node_alloc(mdoc, ep->ln, ep->pos, ROFFT_EQN, MDOC_MAX);
1.181     kristaps  104:        n->eqn = ep;
1.227     schwarze  105:        if (ep->ln > mdoc->last->line)
                    106:                n->flags |= MDOC_LINE;
1.247   ! schwarze  107:        roff_node_append(mdoc, n);
1.242     schwarze  108:        mdoc->next = ROFF_NEXT_SIBLING;
1.20      kristaps  109: }
                    110:
1.233     schwarze  111: void
1.242     schwarze  112: mdoc_addspan(struct roff_man *mdoc, const struct tbl_span *sp)
1.175     kristaps  113: {
1.240     schwarze  114:        struct roff_node *n;
1.175     kristaps  115:
1.247   ! schwarze  116:        n = roff_node_alloc(mdoc, sp->line, 0, ROFFT_TBL, MDOC_MAX);
1.180     kristaps  117:        n->span = sp;
1.247   ! schwarze  118:        roff_node_append(mdoc, n);
        !           119:        mdoc_valid_post(mdoc);
1.242     schwarze  120:        mdoc->next = ROFF_NEXT_SIBLING;
1.175     kristaps  121: }
                    122:
1.50      kristaps  123: /*
1.53      kristaps  124:  * Main parse routine.  Parses a single line -- really just hands off to
1.123     kristaps  125:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps  126:  */
1.20      kristaps  127: int
1.242     schwarze  128: mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1       kristaps  129: {
                    130:
1.239     schwarze  131:        if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
1.228     schwarze  132:                mdoc->flags |= MDOC_NEWLINE;
1.153     schwarze  133:
                    134:        /*
                    135:         * Let the roff nS register switch SYNOPSIS mode early,
                    136:         * such that the parser knows at all times
                    137:         * whether this mode is on or off.
                    138:         * Note that this mode is also switched by the Sh macro.
                    139:         */
1.204     schwarze  140:        if (roff_getreg(mdoc->roff, "nS"))
                    141:                mdoc->flags |= MDOC_SYNOPSIS;
                    142:        else
                    143:                mdoc->flags &= ~MDOC_SYNOPSIS;
1.153     schwarze  144:
1.203     schwarze  145:        return(roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213     schwarze  146:            mdoc_pmacro(mdoc, ln, buf, offs) :
                    147:            mdoc_ptext(mdoc, ln, buf, offs));
1.1       kristaps  148: }
                    149:
1.232     schwarze  150: void
1.148     kristaps  151: mdoc_macro(MACRO_PROT_ARGS)
1.88      kristaps  152: {
1.122     kristaps  153:        assert(tok < MDOC_MAX);
                    154:
1.223     schwarze  155:        if (mdoc->flags & MDOC_PBODY) {
                    156:                if (tok == MDOC_Dt) {
                    157:                        mandoc_vmsg(MANDOCERR_DT_LATE,
                    158:                            mdoc->parse, line, ppos,
                    159:                            "Dt %s", buf + *pos);
1.232     schwarze  160:                        return;
1.223     schwarze  161:                }
                    162:        } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
                    163:                if (mdoc->meta.title == NULL) {
                    164:                        mandoc_vmsg(MANDOCERR_DT_NOTITLE,
                    165:                            mdoc->parse, line, ppos, "%s %s",
                    166:                            mdoc_macronames[tok], buf + *pos);
                    167:                        mdoc->meta.title = mandoc_strdup("UNTITLED");
                    168:                }
1.203     schwarze  169:                if (NULL == mdoc->meta.vol)
                    170:                        mdoc->meta.vol = mandoc_strdup("LOCAL");
                    171:                mdoc->flags |= MDOC_PBODY;
1.120     kristaps  172:        }
1.232     schwarze  173:        (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
1.73      kristaps  174: }
                    175:
1.231     schwarze  176: void
1.242     schwarze  177: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.17      kristaps  178: {
1.240     schwarze  179:        struct roff_node *p;
1.17      kristaps  180:
1.247   ! schwarze  181:        p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
        !           182:        roff_node_append(mdoc, p);
1.242     schwarze  183:        mdoc->next = ROFF_NEXT_CHILD;
1.152     schwarze  184: }
                    185:
1.240     schwarze  186: struct roff_node *
1.242     schwarze  187: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
1.240     schwarze  188:                struct roff_node *body, enum mdoc_endbody end)
1.152     schwarze  189: {
1.240     schwarze  190:        struct roff_node *p;
1.152     schwarze  191:
1.237     schwarze  192:        body->flags |= MDOC_ENDED;
                    193:        body->parent->flags |= MDOC_ENDED;
1.247   ! schwarze  194:        p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
1.237     schwarze  195:        p->body = body;
1.202     schwarze  196:        p->norm = body->norm;
1.152     schwarze  197:        p->end = end;
1.247   ! schwarze  198:        roff_node_append(mdoc, p);
1.242     schwarze  199:        mdoc->next = ROFF_NEXT_SIBLING;
1.235     schwarze  200:        return(p);
1.1       kristaps  201: }
                    202:
1.240     schwarze  203: struct roff_node *
1.242     schwarze  204: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.240     schwarze  205:        int tok, struct mdoc_arg *args)
1.1       kristaps  206: {
1.240     schwarze  207:        struct roff_node *p;
1.1       kristaps  208:
1.247   ! schwarze  209:        p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
1.77      kristaps  210:        p->args = args;
                    211:        if (p->args)
1.53      kristaps  212:                (args->refcnt)++;
1.172     kristaps  213:
                    214:        switch (tok) {
1.213     schwarze  215:        case MDOC_Bd:
1.172     kristaps  216:                /* FALLTHROUGH */
1.213     schwarze  217:        case MDOC_Bf:
1.172     kristaps  218:                /* FALLTHROUGH */
1.213     schwarze  219:        case MDOC_Bl:
1.217     schwarze  220:                /* FALLTHROUGH */
                    221:        case MDOC_En:
1.173     kristaps  222:                /* FALLTHROUGH */
1.213     schwarze  223:        case MDOC_Rs:
1.172     kristaps  224:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    225:                break;
                    226:        default:
                    227:                break;
                    228:        }
1.247   ! schwarze  229:        roff_node_append(mdoc, p);
1.242     schwarze  230:        mdoc->next = ROFF_NEXT_CHILD;
1.231     schwarze  231:        return(p);
1.1       kristaps  232: }
                    233:
1.231     schwarze  234: void
1.242     schwarze  235: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.240     schwarze  236:        int tok, struct mdoc_arg *args)
1.1       kristaps  237: {
1.240     schwarze  238:        struct roff_node *p;
1.1       kristaps  239:
1.247   ! schwarze  240:        p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
1.77      kristaps  241:        p->args = args;
                    242:        if (p->args)
1.53      kristaps  243:                (args->refcnt)++;
1.172     kristaps  244:
                    245:        switch (tok) {
1.213     schwarze  246:        case MDOC_An:
1.172     kristaps  247:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    248:                break;
                    249:        default:
                    250:                break;
                    251:        }
1.247   ! schwarze  252:        roff_node_append(mdoc, p);
1.242     schwarze  253:        mdoc->next = ROFF_NEXT_CHILD;
1.175     kristaps  254: }
1.1       kristaps  255:
1.231     schwarze  256: void
1.242     schwarze  257: mdoc_word_alloc(struct roff_man *mdoc, int line, int pos, const char *p)
1.1       kristaps  258: {
1.240     schwarze  259:        struct roff_node *n;
1.1       kristaps  260:
1.247   ! schwarze  261:        n = roff_node_alloc(mdoc, line, pos, ROFFT_TEXT, MDOC_MAX);
1.203     schwarze  262:        n->string = roff_strdup(mdoc->roff, p);
1.247   ! schwarze  263:        roff_node_append(mdoc, n);
        !           264:        mdoc_valid_post(mdoc);
1.242     schwarze  265:        mdoc->next = ROFF_NEXT_SIBLING;
1.91      kristaps  266: }
                    267:
1.205     schwarze  268: void
1.242     schwarze  269: mdoc_word_append(struct roff_man *mdoc, const char *p)
1.205     schwarze  270: {
1.240     schwarze  271:        struct roff_node        *n;
1.205     schwarze  272:        char                    *addstr, *newstr;
                    273:
                    274:        n = mdoc->last;
                    275:        addstr = roff_strdup(mdoc->roff, p);
1.210     schwarze  276:        mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1.205     schwarze  277:        free(addstr);
                    278:        free(n->string);
                    279:        n->string = newstr;
1.242     schwarze  280:        mdoc->next = ROFF_NEXT_SIBLING;
1.205     schwarze  281: }
1.91      kristaps  282:
1.231     schwarze  283: void
1.242     schwarze  284: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201     schwarze  285: {
                    286:
1.247   ! schwarze  287:        roff_node_unlink(mdoc, p);
        !           288:        roff_node_append(mdoc, p);
1.1       kristaps  289: }
                    290:
1.53      kristaps  291: /*
                    292:  * Parse free-form text, that is, a line that does not begin with the
                    293:  * control character.
                    294:  */
                    295: static int
1.242     schwarze  296: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1       kristaps  297: {
1.240     schwarze  298:        struct roff_node *n;
1.142     kristaps  299:        char             *c, *ws, *end;
                    300:
1.203     schwarze  301:        assert(mdoc->last);
                    302:        n = mdoc->last;
1.142     kristaps  303:
                    304:        /*
1.144     kristaps  305:         * Divert directly to list processing if we're encountering a
1.239     schwarze  306:         * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
                    307:         * (a ROFFT_BODY means it's already open, in which case we should
1.144     kristaps  308:         * process within its context in the normal way).
1.142     kristaps  309:         */
                    310:
1.239     schwarze  311:        if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238     schwarze  312:            n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.144     kristaps  313:                /* `Bl' is open without any children. */
1.203     schwarze  314:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  315:                mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
                    316:                return(1);
1.142     kristaps  317:        }
                    318:
1.239     schwarze  319:        if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213     schwarze  320:            NULL != n->parent &&
                    321:            MDOC_Bl == n->parent->tok &&
                    322:            LIST_column == n->parent->norm->Bl.type) {
1.144     kristaps  323:                /* `Bl' has block-level `It' children. */
1.203     schwarze  324:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  325:                mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
                    326:                return(1);
1.142     kristaps  327:        }
1.124     kristaps  328:
1.137     schwarze  329:        /*
                    330:         * Search for the beginning of unescaped trailing whitespace (ws)
                    331:         * and for the first character not to be output (end).
                    332:         */
1.139     kristaps  333:
                    334:        /* FIXME: replace with strcspn(). */
1.137     schwarze  335:        ws = NULL;
                    336:        for (c = end = buf + offs; *c; c++) {
                    337:                switch (*c) {
                    338:                case ' ':
                    339:                        if (NULL == ws)
                    340:                                ws = c;
                    341:                        continue;
                    342:                case '\t':
                    343:                        /*
                    344:                         * Always warn about trailing tabs,
                    345:                         * even outside literal context,
                    346:                         * where they should be put on the next line.
                    347:                         */
                    348:                        if (NULL == ws)
                    349:                                ws = c;
                    350:                        /*
                    351:                         * Strip trailing tabs in literal context only;
                    352:                         * outside, they affect the next line.
                    353:                         */
1.203     schwarze  354:                        if (MDOC_LITERAL & mdoc->flags)
1.137     schwarze  355:                                continue;
                    356:                        break;
                    357:                case '\\':
                    358:                        /* Skip the escaped character, too, if any. */
                    359:                        if (c[1])
                    360:                                c++;
                    361:                        /* FALLTHROUGH */
                    362:                default:
                    363:                        ws = NULL;
                    364:                        break;
                    365:                }
                    366:                end = c + 1;
                    367:        }
                    368:        *end = '\0';
1.91      kristaps  369:
1.137     schwarze  370:        if (ws)
1.218     schwarze  371:                mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                    372:                    line, (int)(ws-buf), NULL);
1.115     kristaps  373:
1.231     schwarze  374:        if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.218     schwarze  375:                mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
                    376:                    line, (int)(c - buf), NULL);
1.124     kristaps  377:
1.119     kristaps  378:                /*
1.165     schwarze  379:                 * Insert a `sp' in the case of a blank line.  Technically,
1.124     kristaps  380:                 * blank lines aren't allowed, but enough manuals assume this
                    381:                 * behaviour that we want to work around it.
1.119     kristaps  382:                 */
1.231     schwarze  383:                mdoc_elem_alloc(mdoc, line, offs, MDOC_sp, NULL);
1.242     schwarze  384:                mdoc->next = ROFF_NEXT_SIBLING;
1.230     schwarze  385:                mdoc_valid_post(mdoc);
                    386:                return(1);
1.119     kristaps  387:        }
1.68      kristaps  388:
1.231     schwarze  389:        mdoc_word_alloc(mdoc, line, offs, buf+offs);
1.91      kristaps  390:
1.231     schwarze  391:        if (mdoc->flags & MDOC_LITERAL)
1.137     schwarze  392:                return(1);
1.128     kristaps  393:
                    394:        /*
                    395:         * End-of-sentence check.  If the last character is an unescaped
                    396:         * EOS character, then flag the node as being the end of a
                    397:         * sentence.  The front-end will know how to interpret this.
                    398:         */
1.132     kristaps  399:
1.137     schwarze  400:        assert(buf < end);
                    401:
1.207     schwarze  402:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203     schwarze  403:                mdoc->last->flags |= MDOC_EOS;
1.128     kristaps  404:        return(1);
1.1       kristaps  405: }
                    406:
1.53      kristaps  407: /*
                    408:  * Parse a macro line, that is, a line beginning with the control
                    409:  * character.
                    410:  */
1.155     kristaps  411: static int
1.242     schwarze  412: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1       kristaps  413: {
1.240     schwarze  414:        struct roff_node *n;
1.229     schwarze  415:        const char       *cp;
1.240     schwarze  416:        int               tok;
1.188     kristaps  417:        int               i, sv;
1.144     kristaps  418:        char              mac[5];
1.63      kristaps  419:
1.188     kristaps  420:        sv = offs;
1.130     kristaps  421:
1.213     schwarze  422:        /*
1.162     schwarze  423:         * Copy the first word into a nil-terminated buffer.
1.229     schwarze  424:         * Stop when a space, tab, escape, or eoln is encountered.
1.160     kristaps  425:         */
1.1       kristaps  426:
1.188     kristaps  427:        i = 0;
1.229     schwarze  428:        while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.188     kristaps  429:                mac[i++] = buf[offs++];
                    430:
                    431:        mac[i] = '\0';
                    432:
1.214     schwarze  433:        tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
1.1       kristaps  434:
1.229     schwarze  435:        if (tok == MDOC_MAX) {
1.222     schwarze  436:                mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
                    437:                    ln, sv, buf + sv - 1);
1.58      kristaps  438:                return(1);
1.53      kristaps  439:        }
1.1       kristaps  440:
1.229     schwarze  441:        /* Skip a leading escape sequence or tab. */
1.160     kristaps  442:
1.229     schwarze  443:        switch (buf[offs]) {
                    444:        case '\\':
                    445:                cp = buf + offs + 1;
                    446:                mandoc_escape(&cp, NULL, NULL);
                    447:                offs = cp - buf;
                    448:                break;
                    449:        case '\t':
1.188     kristaps  450:                offs++;
1.229     schwarze  451:                break;
                    452:        default:
                    453:                break;
                    454:        }
1.160     kristaps  455:
                    456:        /* Jump to the next non-whitespace word. */
1.1       kristaps  457:
1.188     kristaps  458:        while (buf[offs] && ' ' == buf[offs])
                    459:                offs++;
1.1       kristaps  460:
1.213     schwarze  461:        /*
1.125     kristaps  462:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    463:         * into the parser as "text", so we only warn about spaces here.
                    464:         */
1.115     kristaps  465:
1.188     kristaps  466:        if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218     schwarze  467:                mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                    468:                    ln, offs - 1, NULL);
1.115     kristaps  469:
1.144     kristaps  470:        /*
                    471:         * If an initial macro or a list invocation, divert directly
                    472:         * into macro processing.
                    473:         */
                    474:
1.232     schwarze  475:        if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
                    476:                mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
                    477:                return(1);
                    478:        }
1.144     kristaps  479:
1.203     schwarze  480:        n = mdoc->last;
                    481:        assert(mdoc->last);
1.144     kristaps  482:
                    483:        /*
                    484:         * If the first macro of a `Bl -column', open an `It' block
                    485:         * context around the parsed macro.
                    486:         */
                    487:
1.239     schwarze  488:        if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238     schwarze  489:            n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.203     schwarze  490:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  491:                mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
                    492:                return(1);
1.144     kristaps  493:        }
                    494:
                    495:        /*
                    496:         * If we're following a block-level `It' within a `Bl -column'
                    497:         * context (perhaps opened in the above block or in ptext()),
                    498:         * then open an `It' block context around the parsed macro.
1.98      kristaps  499:         */
1.144     kristaps  500:
1.239     schwarze  501:        if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213     schwarze  502:            NULL != n->parent &&
                    503:            MDOC_Bl == n->parent->tok &&
                    504:            LIST_column == n->parent->norm->Bl.type) {
1.203     schwarze  505:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  506:                mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
                    507:                return(1);
1.144     kristaps  508:        }
                    509:
                    510:        /* Normal processing of a macro. */
                    511:
1.232     schwarze  512:        mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.208     schwarze  513:
                    514:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    515:
                    516:        if (mdoc->quick && MDOC_Sh == tok &&
                    517:            SEC_NAME != mdoc->last->sec)
                    518:                return(2);
1.1       kristaps  519:
1.53      kristaps  520:        return(1);
1.1       kristaps  521: }
1.100     kristaps  522:
1.186     kristaps  523: enum mdelim
                    524: mdoc_isdelim(const char *p)
                    525: {
                    526:
                    527:        if ('\0' == p[0])
                    528:                return(DELIM_NONE);
                    529:
                    530:        if ('\0' == p[1])
                    531:                switch (p[0]) {
1.213     schwarze  532:                case '(':
1.186     kristaps  533:                        /* FALLTHROUGH */
1.213     schwarze  534:                case '[':
1.186     kristaps  535:                        return(DELIM_OPEN);
1.213     schwarze  536:                case '|':
1.186     kristaps  537:                        return(DELIM_MIDDLE);
1.213     schwarze  538:                case '.':
1.186     kristaps  539:                        /* FALLTHROUGH */
1.213     schwarze  540:                case ',':
1.186     kristaps  541:                        /* FALLTHROUGH */
1.213     schwarze  542:                case ';':
1.186     kristaps  543:                        /* FALLTHROUGH */
1.213     schwarze  544:                case ':':
1.186     kristaps  545:                        /* FALLTHROUGH */
1.213     schwarze  546:                case '?':
1.186     kristaps  547:                        /* FALLTHROUGH */
1.213     schwarze  548:                case '!':
1.186     kristaps  549:                        /* FALLTHROUGH */
1.213     schwarze  550:                case ')':
1.186     kristaps  551:                        /* FALLTHROUGH */
1.213     schwarze  552:                case ']':
1.186     kristaps  553:                        return(DELIM_CLOSE);
                    554:                default:
                    555:                        return(DELIM_NONE);
                    556:                }
                    557:
                    558:        if ('\\' != p[0])
                    559:                return(DELIM_NONE);
1.100     kristaps  560:
1.186     kristaps  561:        if (0 == strcmp(p + 1, "."))
                    562:                return(DELIM_CLOSE);
1.200     schwarze  563:        if (0 == strcmp(p + 1, "fR|\\fP"))
1.186     kristaps  564:                return(DELIM_MIDDLE);
                    565:
                    566:        return(DELIM_NONE);
1.211     schwarze  567: }
                    568:
                    569: void
1.240     schwarze  570: mdoc_deroff(char **dest, const struct roff_node *n)
1.211     schwarze  571: {
                    572:        char    *cp;
                    573:        size_t   sz;
                    574:
1.239     schwarze  575:        if (n->type != ROFFT_TEXT) {
1.211     schwarze  576:                for (n = n->child; n; n = n->next)
                    577:                        mdoc_deroff(dest, n);
                    578:                return;
                    579:        }
                    580:
                    581:        /* Skip leading whitespace. */
                    582:
                    583:        for (cp = n->string; '\0' != *cp; cp++)
                    584:                if (0 == isspace((unsigned char)*cp))
                    585:                        break;
                    586:
                    587:        /* Skip trailing whitespace. */
                    588:
                    589:        for (sz = strlen(cp); sz; sz--)
                    590:                if (0 == isspace((unsigned char)cp[sz-1]))
                    591:                        break;
                    592:
                    593:        /* Skip empty strings. */
                    594:
                    595:        if (0 == sz)
                    596:                return;
                    597:
                    598:        if (NULL == *dest) {
                    599:                *dest = mandoc_strndup(cp, sz);
                    600:                return;
                    601:        }
                    602:
                    603:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                    604:        free(*dest);
                    605:        *dest = cp;
1.186     kristaps  606: }

CVSweb