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

Annotation of mandoc/mdoc.c, Revision 1.209

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

CVSweb