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

Annotation of mandoc/mdoc.c, Revision 1.243

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

CVSweb