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

Annotation of mandoc/mdoc.c, Revision 1.217

1.217   ! schwarze    1: /*     $Id: mdoc.c,v 1.216 2014/07/01 22:37:15 schwarze Exp $ */
1.1       kristaps    2: /*
1.182     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.208     schwarze    4:  * Copyright (c) 2010, 2012, 2013, 2014 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.75      kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     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: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.106     kristaps   22: #include <sys/types.h>
                     23:
1.1       kristaps   24: #include <assert.h>
1.211     schwarze   25: #include <ctype.h>
1.1       kristaps   26: #include <stdarg.h>
1.73      kristaps   27: #include <stdio.h>
1.1       kristaps   28: #include <stdlib.h>
                     29: #include <string.h>
1.120     kristaps   30: #include <time.h>
1.1       kristaps   31:
1.187     kristaps   32: #include "mdoc.h"
1.136     kristaps   33: #include "mandoc.h"
1.209     schwarze   34: #include "mandoc_aux.h"
1.70      kristaps   35: #include "libmdoc.h"
1.113     kristaps   36: #include "libmandoc.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.121     kristaps   87: static void              mdoc_node_free(struct mdoc_node *);
1.213     schwarze   88: static void              mdoc_node_unlink(struct mdoc *,
1.121     kristaps   89:                                struct mdoc_node *);
1.73      kristaps   90: static void              mdoc_free1(struct mdoc *);
1.113     kristaps   91: static void              mdoc_alloc1(struct mdoc *);
1.213     schwarze   92: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117     kristaps   93:                                enum mdoct, enum mdoc_type);
1.213     schwarze   94: static int               node_append(struct mdoc *,
1.71      kristaps   95:                                struct mdoc_node *);
1.193     kristaps   96: #if 0
1.191     kristaps   97: static int               mdoc_preptext(struct mdoc *, int, char *, int);
1.193     kristaps   98: #endif
1.149     kristaps   99: static int               mdoc_ptext(struct mdoc *, int, char *, int);
                    100: static int               mdoc_pmacro(struct mdoc *, int, char *, int);
1.88      kristaps  101:
1.213     schwarze  102:
1.1       kristaps  103: const struct mdoc_node *
1.203     schwarze  104: mdoc_node(const struct mdoc *mdoc)
1.1       kristaps  105: {
                    106:
1.203     schwarze  107:        assert( ! (MDOC_HALT & mdoc->flags));
                    108:        return(mdoc->first);
1.1       kristaps  109: }
                    110:
1.37      kristaps  111: const struct mdoc_meta *
1.203     schwarze  112: mdoc_meta(const struct mdoc *mdoc)
1.37      kristaps  113: {
                    114:
1.203     schwarze  115:        assert( ! (MDOC_HALT & mdoc->flags));
                    116:        return(&mdoc->meta);
1.37      kristaps  117: }
                    118:
1.85      kristaps  119: /*
                    120:  * Frees volatile resources (parse tree, meta-data, fields).
                    121:  */
1.73      kristaps  122: static void
                    123: mdoc_free1(struct mdoc *mdoc)
1.67      kristaps  124: {
                    125:
                    126:        if (mdoc->first)
1.121     kristaps  127:                mdoc_node_delete(mdoc, mdoc->first);
1.67      kristaps  128:        if (mdoc->meta.title)
                    129:                free(mdoc->meta.title);
                    130:        if (mdoc->meta.os)
                    131:                free(mdoc->meta.os);
                    132:        if (mdoc->meta.name)
                    133:                free(mdoc->meta.name);
                    134:        if (mdoc->meta.arch)
                    135:                free(mdoc->meta.arch);
                    136:        if (mdoc->meta.vol)
                    137:                free(mdoc->meta.vol);
1.133     kristaps  138:        if (mdoc->meta.msec)
                    139:                free(mdoc->meta.msec);
1.183     kristaps  140:        if (mdoc->meta.date)
                    141:                free(mdoc->meta.date);
1.73      kristaps  142: }
                    143:
1.85      kristaps  144: /*
                    145:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    146:  */
1.113     kristaps  147: static void
1.73      kristaps  148: mdoc_alloc1(struct mdoc *mdoc)
                    149: {
1.67      kristaps  150:
1.112     kristaps  151:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67      kristaps  152:        mdoc->flags = 0;
1.85      kristaps  153:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113     kristaps  154:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70      kristaps  155:        mdoc->first = mdoc->last;
1.67      kristaps  156:        mdoc->last->type = MDOC_ROOT;
1.196     schwarze  157:        mdoc->last->tok = MDOC_MAX;
1.67      kristaps  158:        mdoc->next = MDOC_NEXT_CHILD;
1.73      kristaps  159: }
                    160:
                    161: /*
1.85      kristaps  162:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    163:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    164:  * and the parser is ready for re-invocation on a new tree; however,
                    165:  * cross-parse non-volatile data is kept intact.
1.73      kristaps  166:  */
1.113     kristaps  167: void
1.73      kristaps  168: mdoc_reset(struct mdoc *mdoc)
                    169: {
                    170:
                    171:        mdoc_free1(mdoc);
1.113     kristaps  172:        mdoc_alloc1(mdoc);
1.67      kristaps  173: }
                    174:
1.68      kristaps  175: /*
1.85      kristaps  176:  * Completely free up all volatile and non-volatile parse resources.
                    177:  * After invocation, the pointer is no longer usable.
1.68      kristaps  178:  */
1.67      kristaps  179: void
1.38      kristaps  180: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  181: {
                    182:
1.73      kristaps  183:        mdoc_free1(mdoc);
1.1       kristaps  184:        free(mdoc);
                    185: }
                    186:
1.85      kristaps  187: /*
1.213     schwarze  188:  * Allocate volatile and non-volatile parse resources.
1.85      kristaps  189:  */
1.1       kristaps  190: struct mdoc *
1.208     schwarze  191: mdoc_alloc(struct roff *roff, struct mparse *parse,
                    192:        char *defos, int quick)
1.1       kristaps  193: {
                    194:        struct mdoc     *p;
                    195:
1.113     kristaps  196:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    197:
1.185     kristaps  198:        p->parse = parse;
1.197     schwarze  199:        p->defos = defos;
1.208     schwarze  200:        p->quick = quick;
1.189     kristaps  201:        p->roff = roff;
1.73      kristaps  202:
1.113     kristaps  203:        mdoc_hash_init();
                    204:        mdoc_alloc1(p);
                    205:        return(p);
1.1       kristaps  206: }
                    207:
1.68      kristaps  208: /*
                    209:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.85      kristaps  210:  * through to macro_end() in macro.c.
1.68      kristaps  211:  */
1.1       kristaps  212: int
1.203     schwarze  213: mdoc_endparse(struct mdoc *mdoc)
1.20      kristaps  214: {
                    215:
1.203     schwarze  216:        assert( ! (MDOC_HALT & mdoc->flags));
                    217:        if (mdoc_macroend(mdoc))
1.20      kristaps  218:                return(1);
1.203     schwarze  219:        mdoc->flags |= MDOC_HALT;
1.72      kristaps  220:        return(0);
1.181     kristaps  221: }
                    222:
                    223: int
1.203     schwarze  224: mdoc_addeqn(struct mdoc *mdoc, const struct eqn *ep)
1.181     kristaps  225: {
                    226:        struct mdoc_node *n;
                    227:
1.203     schwarze  228:        assert( ! (MDOC_HALT & mdoc->flags));
1.181     kristaps  229:
                    230:        /* No text before an initial macro. */
                    231:
1.203     schwarze  232:        if (SEC_NONE == mdoc->lastnamed) {
                    233:                mdoc_pmsg(mdoc, ep->ln, ep->pos, MANDOCERR_NOTEXT);
1.181     kristaps  234:                return(1);
                    235:        }
                    236:
1.203     schwarze  237:        n = node_alloc(mdoc, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN);
1.181     kristaps  238:        n->eqn = ep;
                    239:
1.203     schwarze  240:        if ( ! node_append(mdoc, n))
1.181     kristaps  241:                return(0);
                    242:
1.203     schwarze  243:        mdoc->next = MDOC_NEXT_SIBLING;
1.181     kristaps  244:        return(1);
1.20      kristaps  245: }
                    246:
1.175     kristaps  247: int
1.203     schwarze  248: mdoc_addspan(struct mdoc *mdoc, const struct tbl_span *sp)
1.175     kristaps  249: {
1.180     kristaps  250:        struct mdoc_node *n;
1.175     kristaps  251:
1.203     schwarze  252:        assert( ! (MDOC_HALT & mdoc->flags));
1.175     kristaps  253:
                    254:        /* No text before an initial macro. */
                    255:
1.203     schwarze  256:        if (SEC_NONE == mdoc->lastnamed) {
                    257:                mdoc_pmsg(mdoc, sp->line, 0, MANDOCERR_NOTEXT);
1.175     kristaps  258:                return(1);
                    259:        }
                    260:
1.203     schwarze  261:        n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, MDOC_TBL);
1.180     kristaps  262:        n->span = sp;
                    263:
1.203     schwarze  264:        if ( ! node_append(mdoc, n))
1.180     kristaps  265:                return(0);
                    266:
1.203     schwarze  267:        mdoc->next = MDOC_NEXT_SIBLING;
1.180     kristaps  268:        return(1);
1.175     kristaps  269: }
                    270:
1.50      kristaps  271: /*
1.53      kristaps  272:  * Main parse routine.  Parses a single line -- really just hands off to
1.123     kristaps  273:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps  274:  */
1.20      kristaps  275: int
1.203     schwarze  276: mdoc_parseln(struct mdoc *mdoc, int ln, char *buf, int offs)
1.1       kristaps  277: {
                    278:
1.203     schwarze  279:        assert( ! (MDOC_HALT & mdoc->flags));
1.50      kristaps  280:
1.203     schwarze  281:        mdoc->flags |= MDOC_NEWLINE;
1.153     schwarze  282:
                    283:        /*
                    284:         * Let the roff nS register switch SYNOPSIS mode early,
                    285:         * such that the parser knows at all times
                    286:         * whether this mode is on or off.
                    287:         * Note that this mode is also switched by the Sh macro.
                    288:         */
1.204     schwarze  289:        if (roff_getreg(mdoc->roff, "nS"))
                    290:                mdoc->flags |= MDOC_SYNOPSIS;
                    291:        else
                    292:                mdoc->flags &= ~MDOC_SYNOPSIS;
1.153     schwarze  293:
1.203     schwarze  294:        return(roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213     schwarze  295:            mdoc_pmacro(mdoc, ln, buf, offs) :
                    296:            mdoc_ptext(mdoc, ln, buf, offs));
1.1       kristaps  297: }
                    298:
1.88      kristaps  299: int
1.148     kristaps  300: mdoc_macro(MACRO_PROT_ARGS)
1.88      kristaps  301: {
1.122     kristaps  302:        assert(tok < MDOC_MAX);
                    303:
                    304:        /* If we're in the body, deny prologue calls. */
1.117     kristaps  305:
1.213     schwarze  306:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
                    307:            MDOC_PBODY & mdoc->flags) {
1.215     schwarze  308:                mandoc_vmsg(MANDOCERR_PROLOG_ONLY, mdoc->parse,
                    309:                    line, ppos, "%s", mdoc_macronames[tok]);
1.174     kristaps  310:                return(1);
                    311:        }
1.122     kristaps  312:
                    313:        /* If we're in the prologue, deny "body" macros.  */
                    314:
1.213     schwarze  315:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    316:             ! (MDOC_PBODY & mdoc->flags)) {
1.215     schwarze  317:                mandoc_vmsg(MANDOCERR_PROLOG_BAD, mdoc->parse,
                    318:                    line, ppos, "%s", mdoc_macronames[tok]);
1.203     schwarze  319:                if (NULL == mdoc->meta.msec)
                    320:                        mdoc->meta.msec = mandoc_strdup("1");
                    321:                if (NULL == mdoc->meta.title)
                    322:                        mdoc->meta.title = mandoc_strdup("UNKNOWN");
                    323:                if (NULL == mdoc->meta.vol)
                    324:                        mdoc->meta.vol = mandoc_strdup("LOCAL");
                    325:                if (NULL == mdoc->meta.os)
                    326:                        mdoc->meta.os = mandoc_strdup("LOCAL");
                    327:                if (NULL == mdoc->meta.date)
                    328:                        mdoc->meta.date = mandoc_normdate
                    329:                                (mdoc->parse, NULL, line, ppos);
                    330:                mdoc->flags |= MDOC_PBODY;
1.120     kristaps  331:        }
1.88      kristaps  332:
1.203     schwarze  333:        return((*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf));
1.73      kristaps  334: }
                    335:
                    336:
                    337: static int
                    338: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  339: {
                    340:
1.25      kristaps  341:        assert(mdoc->last);
                    342:        assert(mdoc->first);
                    343:        assert(MDOC_ROOT != p->type);
1.1       kristaps  344:
1.13      kristaps  345:        switch (mdoc->next) {
1.213     schwarze  346:        case MDOC_NEXT_SIBLING:
1.6       kristaps  347:                mdoc->last->next = p;
                    348:                p->prev = mdoc->last;
1.13      kristaps  349:                p->parent = mdoc->last->parent;
1.1       kristaps  350:                break;
1.213     schwarze  351:        case MDOC_NEXT_CHILD:
1.13      kristaps  352:                mdoc->last->child = p;
1.1       kristaps  353:                p->parent = mdoc->last;
                    354:                break;
                    355:        default:
1.13      kristaps  356:                abort();
                    357:                /* NOTREACHED */
1.1       kristaps  358:        }
                    359:
1.86      kristaps  360:        p->parent->nchild++;
                    361:
1.172     kristaps  362:        /*
                    363:         * Copy over the normalised-data pointer of our parent.  Not
                    364:         * everybody has one, but copying a null pointer is fine.
                    365:         */
                    366:
                    367:        switch (p->type) {
1.213     schwarze  368:        case MDOC_BODY:
1.202     schwarze  369:                if (ENDBODY_NOT != p->end)
                    370:                        break;
1.172     kristaps  371:                /* FALLTHROUGH */
1.213     schwarze  372:        case MDOC_TAIL:
1.172     kristaps  373:                /* FALLTHROUGH */
1.213     schwarze  374:        case MDOC_HEAD:
1.172     kristaps  375:                p->norm = p->parent->norm;
                    376:                break;
                    377:        default:
                    378:                break;
                    379:        }
                    380:
1.23      kristaps  381:        if ( ! mdoc_valid_pre(mdoc, p))
                    382:                return(0);
1.27      kristaps  383:
                    384:        switch (p->type) {
1.213     schwarze  385:        case MDOC_HEAD:
1.27      kristaps  386:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  387:                p->parent->head = p;
1.27      kristaps  388:                break;
1.213     schwarze  389:        case MDOC_TAIL:
1.27      kristaps  390:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  391:                p->parent->tail = p;
1.27      kristaps  392:                break;
1.213     schwarze  393:        case MDOC_BODY:
1.152     schwarze  394:                if (p->end)
                    395:                        break;
1.27      kristaps  396:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  397:                p->parent->body = p;
1.27      kristaps  398:                break;
                    399:        default:
                    400:                break;
                    401:        }
                    402:
1.1       kristaps  403:        mdoc->last = p;
1.71      kristaps  404:
                    405:        switch (p->type) {
1.213     schwarze  406:        case MDOC_TBL:
1.176     kristaps  407:                /* FALLTHROUGH */
1.213     schwarze  408:        case MDOC_TEXT:
1.71      kristaps  409:                if ( ! mdoc_valid_post(mdoc))
                    410:                        return(0);
                    411:                break;
                    412:        default:
                    413:                break;
                    414:        }
                    415:
1.23      kristaps  416:        return(1);
1.1       kristaps  417: }
                    418:
1.45      kristaps  419: static struct mdoc_node *
1.213     schwarze  420: node_alloc(struct mdoc *mdoc, int line, int pos,
1.117     kristaps  421:                enum mdoct tok, enum mdoc_type type)
1.45      kristaps  422: {
1.46      kristaps  423:        struct mdoc_node *p;
                    424:
1.113     kristaps  425:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.203     schwarze  426:        p->sec = mdoc->lastsec;
1.73      kristaps  427:        p->line = line;
                    428:        p->pos = pos;
1.206     schwarze  429:        p->lastline = line;
1.73      kristaps  430:        p->tok = tok;
1.118     kristaps  431:        p->type = type;
1.150     kristaps  432:
                    433:        /* Flag analysis. */
                    434:
1.203     schwarze  435:        if (MDOC_SYNOPSIS & mdoc->flags)
1.153     schwarze  436:                p->flags |= MDOC_SYNPRETTY;
                    437:        else
                    438:                p->flags &= ~MDOC_SYNPRETTY;
1.203     schwarze  439:        if (MDOC_NEWLINE & mdoc->flags)
1.130     kristaps  440:                p->flags |= MDOC_LINE;
1.203     schwarze  441:        mdoc->flags &= ~MDOC_NEWLINE;
1.150     kristaps  442:
1.46      kristaps  443:        return(p);
1.45      kristaps  444: }
                    445:
1.23      kristaps  446: int
1.203     schwarze  447: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
1.17      kristaps  448: {
                    449:        struct mdoc_node *p;
                    450:
1.203     schwarze  451:        p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
                    452:        if ( ! node_append(mdoc, p))
1.102     kristaps  453:                return(0);
1.203     schwarze  454:        mdoc->next = MDOC_NEXT_CHILD;
1.102     kristaps  455:        return(1);
1.17      kristaps  456: }
                    457:
1.23      kristaps  458: int
1.203     schwarze  459: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
1.1       kristaps  460: {
                    461:        struct mdoc_node *p;
                    462:
1.203     schwarze  463:        assert(mdoc->first);
                    464:        assert(mdoc->last);
1.1       kristaps  465:
1.203     schwarze  466:        p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
                    467:        if ( ! node_append(mdoc, p))
1.102     kristaps  468:                return(0);
1.203     schwarze  469:        mdoc->next = MDOC_NEXT_CHILD;
1.102     kristaps  470:        return(1);
1.1       kristaps  471: }
                    472:
1.23      kristaps  473: int
1.203     schwarze  474: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
1.1       kristaps  475: {
                    476:        struct mdoc_node *p;
                    477:
1.203     schwarze  478:        p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
                    479:        if ( ! node_append(mdoc, p))
1.102     kristaps  480:                return(0);
1.203     schwarze  481:        mdoc->next = MDOC_NEXT_CHILD;
1.152     schwarze  482:        return(1);
                    483: }
                    484:
                    485: int
1.203     schwarze  486: mdoc_endbody_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok,
1.152     schwarze  487:                struct mdoc_node *body, enum mdoc_endbody end)
                    488: {
                    489:        struct mdoc_node *p;
                    490:
1.203     schwarze  491:        p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
1.152     schwarze  492:        p->pending = body;
1.202     schwarze  493:        p->norm = body->norm;
1.152     schwarze  494:        p->end = end;
1.203     schwarze  495:        if ( ! node_append(mdoc, p))
1.152     schwarze  496:                return(0);
1.203     schwarze  497:        mdoc->next = MDOC_NEXT_SIBLING;
1.102     kristaps  498:        return(1);
1.1       kristaps  499: }
                    500:
1.23      kristaps  501: int
1.213     schwarze  502: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.117     kristaps  503:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  504: {
                    505:        struct mdoc_node *p;
                    506:
1.203     schwarze  507:        p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
1.77      kristaps  508:        p->args = args;
                    509:        if (p->args)
1.53      kristaps  510:                (args->refcnt)++;
1.172     kristaps  511:
                    512:        switch (tok) {
1.213     schwarze  513:        case MDOC_Bd:
1.172     kristaps  514:                /* FALLTHROUGH */
1.213     schwarze  515:        case MDOC_Bf:
1.172     kristaps  516:                /* FALLTHROUGH */
1.213     schwarze  517:        case MDOC_Bl:
1.217   ! schwarze  518:                /* FALLTHROUGH */
        !           519:        case MDOC_En:
1.173     kristaps  520:                /* FALLTHROUGH */
1.213     schwarze  521:        case MDOC_Rs:
1.172     kristaps  522:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    523:                break;
                    524:        default:
                    525:                break;
                    526:        }
                    527:
1.203     schwarze  528:        if ( ! node_append(mdoc, p))
1.102     kristaps  529:                return(0);
1.203     schwarze  530:        mdoc->next = MDOC_NEXT_CHILD;
1.102     kristaps  531:        return(1);
1.1       kristaps  532: }
                    533:
1.23      kristaps  534: int
1.213     schwarze  535: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.117     kristaps  536:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  537: {
                    538:        struct mdoc_node *p;
                    539:
1.203     schwarze  540:        p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
1.77      kristaps  541:        p->args = args;
                    542:        if (p->args)
1.53      kristaps  543:                (args->refcnt)++;
1.172     kristaps  544:
                    545:        switch (tok) {
1.213     schwarze  546:        case MDOC_An:
1.172     kristaps  547:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    548:                break;
                    549:        default:
                    550:                break;
                    551:        }
                    552:
1.203     schwarze  553:        if ( ! node_append(mdoc, p))
1.102     kristaps  554:                return(0);
1.203     schwarze  555:        mdoc->next = MDOC_NEXT_CHILD;
1.175     kristaps  556:        return(1);
                    557: }
1.1       kristaps  558:
1.124     kristaps  559: int
1.203     schwarze  560: mdoc_word_alloc(struct mdoc *mdoc, int line, int pos, const char *p)
1.1       kristaps  561: {
1.91      kristaps  562:        struct mdoc_node *n;
1.1       kristaps  563:
1.203     schwarze  564:        n = node_alloc(mdoc, line, pos, MDOC_MAX, MDOC_TEXT);
                    565:        n->string = roff_strdup(mdoc->roff, p);
1.91      kristaps  566:
1.203     schwarze  567:        if ( ! node_append(mdoc, n))
1.101     kristaps  568:                return(0);
1.124     kristaps  569:
1.203     schwarze  570:        mdoc->next = MDOC_NEXT_SIBLING;
1.101     kristaps  571:        return(1);
1.91      kristaps  572: }
                    573:
1.205     schwarze  574: void
                    575: mdoc_word_append(struct mdoc *mdoc, const char *p)
                    576: {
                    577:        struct mdoc_node        *n;
                    578:        char                    *addstr, *newstr;
                    579:
                    580:        n = mdoc->last;
                    581:        addstr = roff_strdup(mdoc->roff, p);
1.210     schwarze  582:        mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1.205     schwarze  583:        free(addstr);
                    584:        free(n->string);
                    585:        n->string = newstr;
                    586:        mdoc->next = MDOC_NEXT_SIBLING;
                    587: }
1.91      kristaps  588:
1.155     kristaps  589: static void
1.53      kristaps  590: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  591: {
                    592:
1.172     kristaps  593:        if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
1.171     kristaps  594:                free(p->norm);
1.53      kristaps  595:        if (p->string)
                    596:                free(p->string);
                    597:        if (p->args)
                    598:                mdoc_argv_free(p->args);
1.1       kristaps  599:        free(p);
                    600: }
                    601:
1.121     kristaps  602: static void
1.203     schwarze  603: mdoc_node_unlink(struct mdoc *mdoc, struct mdoc_node *n)
1.121     kristaps  604: {
                    605:
                    606:        /* Adjust siblings. */
                    607:
                    608:        if (n->prev)
                    609:                n->prev->next = n->next;
                    610:        if (n->next)
                    611:                n->next->prev = n->prev;
                    612:
                    613:        /* Adjust parent. */
                    614:
                    615:        if (n->parent) {
                    616:                n->parent->nchild--;
                    617:                if (n->parent->child == n)
                    618:                        n->parent->child = n->prev ? n->prev : n->next;
1.169     kristaps  619:                if (n->parent->last == n)
                    620:                        n->parent->last = n->prev ? n->prev : NULL;
1.121     kristaps  621:        }
                    622:
                    623:        /* Adjust parse point, if applicable. */
                    624:
1.203     schwarze  625:        if (mdoc && mdoc->last == n) {
1.121     kristaps  626:                if (n->prev) {
1.203     schwarze  627:                        mdoc->last = n->prev;
                    628:                        mdoc->next = MDOC_NEXT_SIBLING;
1.121     kristaps  629:                } else {
1.203     schwarze  630:                        mdoc->last = n->parent;
                    631:                        mdoc->next = MDOC_NEXT_CHILD;
1.121     kristaps  632:                }
                    633:        }
                    634:
1.203     schwarze  635:        if (mdoc && mdoc->first == n)
                    636:                mdoc->first = NULL;
1.121     kristaps  637: }
                    638:
1.53      kristaps  639: void
1.203     schwarze  640: mdoc_node_delete(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  641: {
                    642:
1.121     kristaps  643:        while (p->child) {
                    644:                assert(p->nchild);
1.203     schwarze  645:                mdoc_node_delete(mdoc, p->child);
1.121     kristaps  646:        }
                    647:        assert(0 == p->nchild);
1.1       kristaps  648:
1.203     schwarze  649:        mdoc_node_unlink(mdoc, p);
1.53      kristaps  650:        mdoc_node_free(p);
1.201     schwarze  651: }
                    652:
                    653: int
1.203     schwarze  654: mdoc_node_relink(struct mdoc *mdoc, struct mdoc_node *p)
1.201     schwarze  655: {
                    656:
1.203     schwarze  657:        mdoc_node_unlink(mdoc, p);
                    658:        return(node_append(mdoc, p));
1.1       kristaps  659: }
                    660:
1.193     kristaps  661: #if 0
1.191     kristaps  662: /*
                    663:  * Pre-treat a text line.
                    664:  * Text lines can consist of equations, which must be handled apart from
                    665:  * the regular text.
                    666:  * Thus, use this function to step through a line checking if it has any
                    667:  * equations embedded in it.
                    668:  * This must handle multiple equations AND equations that do not end at
                    669:  * the end-of-line, i.e., will re-enter in the next roff parse.
                    670:  */
                    671: static int
1.203     schwarze  672: mdoc_preptext(struct mdoc *mdoc, int line, char *buf, int offs)
1.191     kristaps  673: {
                    674:        char            *start, *end;
                    675:        char             delim;
                    676:
                    677:        while ('\0' != buf[offs]) {
                    678:                /* Mark starting position if eqn is set. */
                    679:                start = NULL;
1.203     schwarze  680:                if ('\0' != (delim = roff_eqndelim(mdoc->roff)))
1.191     kristaps  681:                        if (NULL != (start = strchr(buf + offs, delim)))
                    682:                                *start++ = '\0';
                    683:
                    684:                /* Parse text as normal. */
1.203     schwarze  685:                if ( ! mdoc_ptext(mdoc, line, buf, offs))
1.191     kristaps  686:                        return(0);
                    687:
                    688:                /* Continue only if an equation exists. */
                    689:                if (NULL == start)
                    690:                        break;
                    691:
                    692:                /* Read past the end of the equation. */
                    693:                offs += start - (buf + offs);
                    694:                assert(start == &buf[offs]);
                    695:                if (NULL != (end = strchr(buf + offs, delim))) {
                    696:                        *end++ = '\0';
                    697:                        while (' ' == *end)
                    698:                                end++;
                    699:                }
                    700:
                    701:                /* Parse the equation itself. */
1.203     schwarze  702:                roff_openeqn(mdoc->roff, NULL, line, offs, buf);
1.191     kristaps  703:
                    704:                /* Process a finished equation? */
1.203     schwarze  705:                if (roff_closeeqn(mdoc->roff))
                    706:                        if ( ! mdoc_addeqn(mdoc, roff_eqn(mdoc->roff)))
1.191     kristaps  707:                                return(0);
                    708:                offs += (end - (buf + offs));
1.213     schwarze  709:        }
1.191     kristaps  710:
                    711:        return(1);
                    712: }
1.193     kristaps  713: #endif
1.1       kristaps  714:
1.53      kristaps  715: /*
                    716:  * Parse free-form text, that is, a line that does not begin with the
                    717:  * control character.
                    718:  */
                    719: static int
1.203     schwarze  720: mdoc_ptext(struct mdoc *mdoc, int line, char *buf, int offs)
1.1       kristaps  721: {
1.142     kristaps  722:        char             *c, *ws, *end;
                    723:        struct mdoc_node *n;
1.1       kristaps  724:
1.124     kristaps  725:        /* No text before an initial macro. */
                    726:
1.203     schwarze  727:        if (SEC_NONE == mdoc->lastnamed) {
                    728:                mdoc_pmsg(mdoc, line, offs, MANDOCERR_NOTEXT);
1.174     kristaps  729:                return(1);
                    730:        }
1.142     kristaps  731:
1.203     schwarze  732:        assert(mdoc->last);
                    733:        n = mdoc->last;
1.142     kristaps  734:
                    735:        /*
1.144     kristaps  736:         * Divert directly to list processing if we're encountering a
1.142     kristaps  737:         * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
1.144     kristaps  738:         * (a MDOC_BODY means it's already open, in which case we should
                    739:         * process within its context in the normal way).
1.142     kristaps  740:         */
                    741:
1.143     kristaps  742:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.213     schwarze  743:            LIST_column == n->norm->Bl.type) {
1.144     kristaps  744:                /* `Bl' is open without any children. */
1.203     schwarze  745:                mdoc->flags |= MDOC_FREECOL;
                    746:                return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
1.142     kristaps  747:        }
                    748:
                    749:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
1.213     schwarze  750:            NULL != n->parent &&
                    751:            MDOC_Bl == n->parent->tok &&
                    752:            LIST_column == n->parent->norm->Bl.type) {
1.144     kristaps  753:                /* `Bl' has block-level `It' children. */
1.203     schwarze  754:                mdoc->flags |= MDOC_FREECOL;
                    755:                return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
1.142     kristaps  756:        }
1.124     kristaps  757:
1.137     schwarze  758:        /*
                    759:         * Search for the beginning of unescaped trailing whitespace (ws)
                    760:         * and for the first character not to be output (end).
                    761:         */
1.139     kristaps  762:
                    763:        /* FIXME: replace with strcspn(). */
1.137     schwarze  764:        ws = NULL;
                    765:        for (c = end = buf + offs; *c; c++) {
                    766:                switch (*c) {
                    767:                case ' ':
                    768:                        if (NULL == ws)
                    769:                                ws = c;
                    770:                        continue;
                    771:                case '\t':
                    772:                        /*
                    773:                         * Always warn about trailing tabs,
                    774:                         * even outside literal context,
                    775:                         * where they should be put on the next line.
                    776:                         */
                    777:                        if (NULL == ws)
                    778:                                ws = c;
                    779:                        /*
                    780:                         * Strip trailing tabs in literal context only;
                    781:                         * outside, they affect the next line.
                    782:                         */
1.203     schwarze  783:                        if (MDOC_LITERAL & mdoc->flags)
1.137     schwarze  784:                                continue;
                    785:                        break;
                    786:                case '\\':
                    787:                        /* Skip the escaped character, too, if any. */
                    788:                        if (c[1])
                    789:                                c++;
                    790:                        /* FALLTHROUGH */
                    791:                default:
                    792:                        ws = NULL;
                    793:                        break;
                    794:                }
                    795:                end = c + 1;
                    796:        }
                    797:        *end = '\0';
1.91      kristaps  798:
1.137     schwarze  799:        if (ws)
1.203     schwarze  800:                mdoc_pmsg(mdoc, line, (int)(ws-buf), MANDOCERR_EOLNSPACE);
1.115     kristaps  801:
1.203     schwarze  802:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & mdoc->flags)) {
                    803:                mdoc_pmsg(mdoc, line, (int)(c-buf), MANDOCERR_NOBLANKLN);
1.124     kristaps  804:
1.119     kristaps  805:                /*
1.165     schwarze  806:                 * Insert a `sp' in the case of a blank line.  Technically,
1.124     kristaps  807:                 * blank lines aren't allowed, but enough manuals assume this
                    808:                 * behaviour that we want to work around it.
1.119     kristaps  809:                 */
1.203     schwarze  810:                if ( ! mdoc_elem_alloc(mdoc, line, offs, MDOC_sp, NULL))
1.119     kristaps  811:                        return(0);
1.124     kristaps  812:
1.203     schwarze  813:                mdoc->next = MDOC_NEXT_SIBLING;
1.199     schwarze  814:
1.203     schwarze  815:                return(mdoc_valid_post(mdoc));
1.119     kristaps  816:        }
1.68      kristaps  817:
1.203     schwarze  818:        if ( ! mdoc_word_alloc(mdoc, line, offs, buf+offs))
1.137     schwarze  819:                return(0);
1.91      kristaps  820:
1.203     schwarze  821:        if (MDOC_LITERAL & mdoc->flags)
1.137     schwarze  822:                return(1);
1.128     kristaps  823:
                    824:        /*
                    825:         * End-of-sentence check.  If the last character is an unescaped
                    826:         * EOS character, then flag the node as being the end of a
                    827:         * sentence.  The front-end will know how to interpret this.
                    828:         */
1.132     kristaps  829:
1.137     schwarze  830:        assert(buf < end);
                    831:
1.207     schwarze  832:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203     schwarze  833:                mdoc->last->flags |= MDOC_EOS;
1.128     kristaps  834:
                    835:        return(1);
1.1       kristaps  836: }
                    837:
1.53      kristaps  838: /*
                    839:  * Parse a macro line, that is, a line beginning with the control
                    840:  * character.
                    841:  */
1.155     kristaps  842: static int
1.203     schwarze  843: mdoc_pmacro(struct mdoc *mdoc, int ln, char *buf, int offs)
1.1       kristaps  844: {
1.144     kristaps  845:        enum mdoct        tok;
1.188     kristaps  846:        int               i, sv;
1.144     kristaps  847:        char              mac[5];
                    848:        struct mdoc_node *n;
1.1       kristaps  849:
1.188     kristaps  850:        /* Empty post-control lines are ignored. */
1.63      kristaps  851:
1.188     kristaps  852:        if ('"' == buf[offs]) {
1.203     schwarze  853:                mdoc_pmsg(mdoc, ln, offs, MANDOCERR_BADCOMMENT);
1.188     kristaps  854:                return(1);
                    855:        } else if ('\0' == buf[offs])
1.63      kristaps  856:                return(1);
                    857:
1.188     kristaps  858:        sv = offs;
1.130     kristaps  859:
1.213     schwarze  860:        /*
1.162     schwarze  861:         * Copy the first word into a nil-terminated buffer.
1.165     schwarze  862:         * Stop copying when a tab, space, or eoln is encountered.
1.160     kristaps  863:         */
1.1       kristaps  864:
1.188     kristaps  865:        i = 0;
1.213     schwarze  866:        while (i < 4 && '\0' != buf[offs] && ' ' != buf[offs] &&
                    867:            '\t' != buf[offs])
1.188     kristaps  868:                mac[i++] = buf[offs++];
                    869:
                    870:        mac[i] = '\0';
                    871:
1.214     schwarze  872:        tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
1.1       kristaps  873:
1.163     schwarze  874:        if (MDOC_MAX == tok) {
1.213     schwarze  875:                mandoc_vmsg(MANDOCERR_MACRO, mdoc->parse,
                    876:                    ln, sv, "%s", buf + sv - 1);
1.58      kristaps  877:                return(1);
1.53      kristaps  878:        }
1.1       kristaps  879:
1.160     kristaps  880:        /* Disregard the first trailing tab, if applicable. */
                    881:
1.188     kristaps  882:        if ('\t' == buf[offs])
                    883:                offs++;
1.160     kristaps  884:
                    885:        /* Jump to the next non-whitespace word. */
1.1       kristaps  886:
1.188     kristaps  887:        while (buf[offs] && ' ' == buf[offs])
                    888:                offs++;
1.1       kristaps  889:
1.213     schwarze  890:        /*
1.125     kristaps  891:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    892:         * into the parser as "text", so we only warn about spaces here.
                    893:         */
1.115     kristaps  894:
1.188     kristaps  895:        if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.203     schwarze  896:                mdoc_pmsg(mdoc, ln, offs - 1, MANDOCERR_EOLNSPACE);
1.115     kristaps  897:
1.144     kristaps  898:        /*
                    899:         * If an initial macro or a list invocation, divert directly
                    900:         * into macro processing.
                    901:         */
                    902:
1.203     schwarze  903:        if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
1.213     schwarze  904:                if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
1.144     kristaps  905:                        goto err;
                    906:                return(1);
                    907:        }
                    908:
1.203     schwarze  909:        n = mdoc->last;
                    910:        assert(mdoc->last);
1.144     kristaps  911:
                    912:        /*
                    913:         * If the first macro of a `Bl -column', open an `It' block
                    914:         * context around the parsed macro.
                    915:         */
                    916:
                    917:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.213     schwarze  918:            LIST_column == n->norm->Bl.type) {
1.203     schwarze  919:                mdoc->flags |= MDOC_FREECOL;
                    920:                if ( ! mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf))
1.144     kristaps  921:                        goto err;
                    922:                return(1);
                    923:        }
                    924:
                    925:        /*
                    926:         * If we're following a block-level `It' within a `Bl -column'
                    927:         * context (perhaps opened in the above block or in ptext()),
                    928:         * then open an `It' block context around the parsed macro.
1.98      kristaps  929:         */
1.144     kristaps  930:
                    931:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
1.213     schwarze  932:            NULL != n->parent &&
                    933:            MDOC_Bl == n->parent->tok &&
                    934:            LIST_column == n->parent->norm->Bl.type) {
1.203     schwarze  935:                mdoc->flags |= MDOC_FREECOL;
1.213     schwarze  936:                if ( ! mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf))
1.144     kristaps  937:                        goto err;
                    938:                return(1);
                    939:        }
                    940:
                    941:        /* Normal processing of a macro. */
                    942:
1.213     schwarze  943:        if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
1.53      kristaps  944:                goto err;
1.208     schwarze  945:
                    946:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    947:
                    948:        if (mdoc->quick && MDOC_Sh == tok &&
                    949:            SEC_NAME != mdoc->last->sec)
                    950:                return(2);
1.1       kristaps  951:
1.53      kristaps  952:        return(1);
1.1       kristaps  953:
1.53      kristaps  954: err:   /* Error out. */
1.1       kristaps  955:
1.203     schwarze  956:        mdoc->flags |= MDOC_HALT;
1.53      kristaps  957:        return(0);
1.1       kristaps  958: }
1.100     kristaps  959:
1.186     kristaps  960: enum mdelim
                    961: mdoc_isdelim(const char *p)
                    962: {
                    963:
                    964:        if ('\0' == p[0])
                    965:                return(DELIM_NONE);
                    966:
                    967:        if ('\0' == p[1])
                    968:                switch (p[0]) {
1.213     schwarze  969:                case '(':
1.186     kristaps  970:                        /* FALLTHROUGH */
1.213     schwarze  971:                case '[':
1.186     kristaps  972:                        return(DELIM_OPEN);
1.213     schwarze  973:                case '|':
1.186     kristaps  974:                        return(DELIM_MIDDLE);
1.213     schwarze  975:                case '.':
1.186     kristaps  976:                        /* FALLTHROUGH */
1.213     schwarze  977:                case ',':
1.186     kristaps  978:                        /* FALLTHROUGH */
1.213     schwarze  979:                case ';':
1.186     kristaps  980:                        /* FALLTHROUGH */
1.213     schwarze  981:                case ':':
1.186     kristaps  982:                        /* FALLTHROUGH */
1.213     schwarze  983:                case '?':
1.186     kristaps  984:                        /* FALLTHROUGH */
1.213     schwarze  985:                case '!':
1.186     kristaps  986:                        /* FALLTHROUGH */
1.213     schwarze  987:                case ')':
1.186     kristaps  988:                        /* FALLTHROUGH */
1.213     schwarze  989:                case ']':
1.186     kristaps  990:                        return(DELIM_CLOSE);
                    991:                default:
                    992:                        return(DELIM_NONE);
                    993:                }
                    994:
                    995:        if ('\\' != p[0])
                    996:                return(DELIM_NONE);
1.100     kristaps  997:
1.186     kristaps  998:        if (0 == strcmp(p + 1, "."))
                    999:                return(DELIM_CLOSE);
1.200     schwarze 1000:        if (0 == strcmp(p + 1, "fR|\\fP"))
1.186     kristaps 1001:                return(DELIM_MIDDLE);
                   1002:
                   1003:        return(DELIM_NONE);
1.211     schwarze 1004: }
                   1005:
                   1006: void
                   1007: mdoc_deroff(char **dest, const struct mdoc_node *n)
                   1008: {
                   1009:        char    *cp;
                   1010:        size_t   sz;
                   1011:
                   1012:        if (MDOC_TEXT != n->type) {
                   1013:                for (n = n->child; n; n = n->next)
                   1014:                        mdoc_deroff(dest, n);
                   1015:                return;
                   1016:        }
                   1017:
                   1018:        /* Skip leading whitespace. */
                   1019:
                   1020:        for (cp = n->string; '\0' != *cp; cp++)
                   1021:                if (0 == isspace((unsigned char)*cp))
                   1022:                        break;
                   1023:
                   1024:        /* Skip trailing whitespace. */
                   1025:
                   1026:        for (sz = strlen(cp); sz; sz--)
                   1027:                if (0 == isspace((unsigned char)cp[sz-1]))
                   1028:                        break;
                   1029:
                   1030:        /* Skip empty strings. */
                   1031:
                   1032:        if (0 == sz)
                   1033:                return;
                   1034:
                   1035:        if (NULL == *dest) {
                   1036:                *dest = mandoc_strndup(cp, sz);
                   1037:                return;
                   1038:        }
                   1039:
                   1040:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                   1041:        free(*dest);
                   1042:        *dest = cp;
1.186     kristaps 1043: }

CVSweb