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

Annotation of mandoc/mdoc.c, Revision 1.248

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

CVSweb