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

Annotation of mandoc/mdoc.c, Revision 1.160

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

CVSweb