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

Annotation of mandoc/mdoc.c, Revision 1.223

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

CVSweb