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

Annotation of mandoc/mdoc.c, Revision 1.211

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

CVSweb