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

Annotation of mandoc/mdoc.c, Revision 1.205

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

CVSweb