[BACK]Return to mdoc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.119

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