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

Annotation of mandoc/mdoc.c, Revision 1.226

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

CVSweb