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

Annotation of mandoc/mdoc.c, Revision 1.123

1.123   ! kristaps    1: /*     $Id: mdoc.c,v 1.122 2010/04/08 07:06:15 kristaps Exp $ */
1.1       kristaps    2: /*
1.76      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.75      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.75      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.114     kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.106     kristaps   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <ctype.h>
                     25: #include <stdarg.h>
1.73      kristaps   26: #include <stdio.h>
1.1       kristaps   27: #include <stdlib.h>
                     28: #include <string.h>
1.120     kristaps   29: #include <time.h>
1.1       kristaps   30:
1.70      kristaps   31: #include "libmdoc.h"
1.113     kristaps   32: #include "libmandoc.h"
1.1       kristaps   33:
1.89      kristaps   34: const  char *const __mdoc_merrnames[MERRMAX] = {
                     35:        "trailing whitespace", /* ETAILWS */
                     36:        "unexpected quoted parameter", /* EQUOTPARM */
                     37:        "unterminated quoted parameter", /* EQUOTTERM */
                     38:        "argument parameter suggested", /* EARGVAL */
                     39:        "macro disallowed in prologue", /* EBODYPROL */
                     40:        "macro disallowed in body", /* EPROLBODY */
                     41:        "text disallowed in prologue", /* ETEXTPROL */
                     42:        "blank line disallowed", /* ENOBLANK */
                     43:        "text parameter too long", /* ETOOLONG */
                     44:        "invalid escape sequence", /* EESCAPE */
                     45:        "invalid character", /* EPRINT */
                     46:        "document has no body", /* ENODAT */
                     47:        "document has no prologue", /* ENOPROLOGUE */
                     48:        "expected line arguments", /* ELINE */
                     49:        "invalid AT&T argument", /* EATT */
                     50:        "default name not yet set", /* ENAME */
                     51:        "missing list type", /* ELISTTYPE */
                     52:        "missing display type", /* EDISPTYPE */
                     53:        "too many display types", /* EMULTIDISP */
                     54:        "too many list types", /* EMULTILIST */
                     55:        "NAME section must be first", /* ESECNAME */
                     56:        "badly-formed NAME section", /* ENAMESECINC */
                     57:        "argument repeated", /* EARGREP */
                     58:        "expected boolean parameter", /* EBOOL */
                     59:        "inconsistent column syntax", /* ECOLMIS */
                     60:        "nested display invalid", /* ENESTDISP */
                     61:        "width argument missing", /* EMISSWIDTH */
                     62:        "invalid section for this manual section", /* EWRONGMSEC */
                     63:        "section out of conventional order", /* ESECOOO */
                     64:        "section repeated", /* ESECREP */
                     65:        "invalid standard argument", /* EBADSTAND */
                     66:        "multi-line arguments discouraged", /* ENOMULTILINE */
                     67:        "multi-line arguments suggested", /* EMULTILINE */
                     68:        "line arguments discouraged", /* ENOLINE */
                     69:        "prologue macro out of conventional order", /* EPROLOOO */
                     70:        "prologue macro repeated", /* EPROLREP */
                     71:        "invalid manual section", /* EBADMSEC */
                     72:        "invalid section", /* EBADSEC */
                     73:        "invalid font mode", /* EFONT */
                     74:        "invalid date syntax", /* EBADDATE */
                     75:        "invalid number format", /* ENUMFMT */
                     76:        "superfluous width argument", /* ENOWIDTH */
                     77:        "system: utsname error", /* EUTSNAME */
                     78:        "obsolete macro", /* EOBS */
                     79:        "end-of-line scope violation", /* EIMPBRK */
                     80:        "empty macro ignored", /* EIGNE */
                     81:        "unclosed explicit scope", /* EOPEN */
                     82:        "unterminated quoted phrase", /* EQUOTPHR */
                     83:        "closure macro without prior context", /* ENOCTX */
1.107     kristaps   84:        "no description found for library", /* ELIB */
                     85:        "bad child for parent context", /* EBADCHILD */
1.109     kristaps   86:        "list arguments preceding type", /* ENOTYPE */
1.123   ! kristaps   87:        "deprecated comment style", /* EBADCOMMENT */
1.89      kristaps   88: };
                     89:
1.1       kristaps   90: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.82      kristaps   91:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   92:        "Sh",           "Ss",           "Pp",           "D1",
                     93:        "Dl",           "Bd",           "Ed",           "Bl",
                     94:        "El",           "It",           "Ad",           "An",
                     95:        "Ar",           "Cd",           "Cm",           "Dv",
                     96:        "Er",           "Ev",           "Ex",           "Fa",
                     97:        "Fd",           "Fl",           "Fn",           "Ft",
                     98:        "Ic",           "In",           "Li",           "Nd",
                     99:        "Nm",           "Op",           "Ot",           "Pa",
                    100:        "Rv",           "St",           "Va",           "Vt",
                    101:        /* LINTED */
1.114     kristaps  102:        "Xr",           "%A",           "%B",           "%D",
1.1       kristaps  103:        /* LINTED */
1.114     kristaps  104:        "%I",           "%J",           "%N",           "%O",
1.1       kristaps  105:        /* LINTED */
1.114     kristaps  106:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps  107:        "Ac",           "Ao",           "Aq",           "At",
                    108:        "Bc",           "Bf",           "Bo",           "Bq",
                    109:        "Bsx",          "Bx",           "Db",           "Dc",
                    110:        "Do",           "Dq",           "Ec",           "Ef",
                    111:        "Em",           "Eo",           "Fx",           "Ms",
                    112:        "No",           "Ns",           "Nx",           "Ox",
                    113:        "Pc",           "Pf",           "Po",           "Pq",
                    114:        "Qc",           "Ql",           "Qo",           "Qq",
                    115:        "Re",           "Rs",           "Sc",           "So",
                    116:        "Sq",           "Sm",           "Sx",           "Sy",
                    117:        "Tn",           "Ux",           "Xc",           "Xo",
                    118:        "Fo",           "Fc",           "Oo",           "Oc",
                    119:        "Bk",           "Ek",           "Bt",           "Hf",
1.82      kristaps  120:        "Fr",           "Ud",           "Lb",           "Lp",
                    121:        "Lk",           "Mt",           "Brq",          "Bro",
1.64      kristaps  122:        /* LINTED */
1.114     kristaps  123:        "Brc",          "%C",           "Es",           "En",
1.69      kristaps  124:        /* LINTED */
1.114     kristaps  125:        "Dx",           "%Q",           "br",           "sp",
1.110     kristaps  126:        /* LINTED */
1.114     kristaps  127:        "%U"
1.1       kristaps  128:        };
                    129:
                    130: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                    131:        "split",                "nosplit",              "ragged",
                    132:        "unfilled",             "literal",              "file",
                    133:        "offset",               "bullet",               "dash",
                    134:        "hyphen",               "item",                 "enum",
                    135:        "tag",                  "diag",                 "hang",
                    136:        "ohang",                "inset",                "column",
                    137:        "width",                "compact",              "std",
1.52      kristaps  138:        "filled",               "words",                "emphasis",
1.108     kristaps  139:        "symbolic",             "nested",               "centered"
1.1       kristaps  140:        };
                    141:
                    142: const  char * const *mdoc_macronames = __mdoc_macronames;
                    143: const  char * const *mdoc_argnames = __mdoc_argnames;
                    144:
1.121     kristaps  145: static void              mdoc_node_free(struct mdoc_node *);
                    146: static void              mdoc_node_unlink(struct mdoc *,
                    147:                                struct mdoc_node *);
1.73      kristaps  148: static void              mdoc_free1(struct mdoc *);
1.113     kristaps  149: static void              mdoc_alloc1(struct mdoc *);
1.73      kristaps  150: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117     kristaps  151:                                enum mdoct, enum mdoc_type);
1.73      kristaps  152: static int               node_append(struct mdoc *,
1.71      kristaps  153:                                struct mdoc_node *);
1.123   ! kristaps  154: static int               mdoc_ptext(struct mdoc *, int, char *);
        !           155: static int               mdoc_pmacro(struct mdoc *, int, char *);
1.71      kristaps  156: static int               macrowarn(struct mdoc *, int, const char *);
1.91      kristaps  157: static int               pstring(struct mdoc *, int, int,
                    158:                                const char *, size_t);
1.88      kristaps  159:
1.1       kristaps  160: const struct mdoc_node *
1.71      kristaps  161: mdoc_node(const struct mdoc *m)
1.1       kristaps  162: {
                    163:
1.71      kristaps  164:        return(MDOC_HALT & m->flags ? NULL : m->first);
1.1       kristaps  165: }
                    166:
                    167:
1.37      kristaps  168: const struct mdoc_meta *
1.71      kristaps  169: mdoc_meta(const struct mdoc *m)
1.37      kristaps  170: {
                    171:
1.71      kristaps  172:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37      kristaps  173: }
                    174:
                    175:
1.85      kristaps  176: /*
                    177:  * Frees volatile resources (parse tree, meta-data, fields).
                    178:  */
1.73      kristaps  179: static void
                    180: mdoc_free1(struct mdoc *mdoc)
1.67      kristaps  181: {
                    182:
                    183:        if (mdoc->first)
1.121     kristaps  184:                mdoc_node_delete(mdoc, mdoc->first);
1.67      kristaps  185:        if (mdoc->meta.title)
                    186:                free(mdoc->meta.title);
                    187:        if (mdoc->meta.os)
                    188:                free(mdoc->meta.os);
                    189:        if (mdoc->meta.name)
                    190:                free(mdoc->meta.name);
                    191:        if (mdoc->meta.arch)
                    192:                free(mdoc->meta.arch);
                    193:        if (mdoc->meta.vol)
                    194:                free(mdoc->meta.vol);
1.73      kristaps  195: }
                    196:
                    197:
1.85      kristaps  198: /*
                    199:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    200:  */
1.113     kristaps  201: static void
1.73      kristaps  202: mdoc_alloc1(struct mdoc *mdoc)
                    203: {
1.67      kristaps  204:
1.112     kristaps  205:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67      kristaps  206:        mdoc->flags = 0;
1.85      kristaps  207:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113     kristaps  208:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70      kristaps  209:        mdoc->first = mdoc->last;
1.67      kristaps  210:        mdoc->last->type = MDOC_ROOT;
                    211:        mdoc->next = MDOC_NEXT_CHILD;
1.73      kristaps  212: }
                    213:
                    214:
                    215: /*
1.85      kristaps  216:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    217:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    218:  * and the parser is ready for re-invocation on a new tree; however,
                    219:  * cross-parse non-volatile data is kept intact.
1.73      kristaps  220:  */
1.113     kristaps  221: void
1.73      kristaps  222: mdoc_reset(struct mdoc *mdoc)
                    223: {
                    224:
                    225:        mdoc_free1(mdoc);
1.113     kristaps  226:        mdoc_alloc1(mdoc);
1.67      kristaps  227: }
                    228:
                    229:
1.68      kristaps  230: /*
1.85      kristaps  231:  * Completely free up all volatile and non-volatile parse resources.
                    232:  * After invocation, the pointer is no longer usable.
1.68      kristaps  233:  */
1.67      kristaps  234: void
1.38      kristaps  235: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  236: {
                    237:
1.73      kristaps  238:        mdoc_free1(mdoc);
1.1       kristaps  239:        free(mdoc);
                    240: }
                    241:
                    242:
1.85      kristaps  243: /*
                    244:  * Allocate volatile and non-volatile parse resources.
                    245:  */
1.1       kristaps  246: struct mdoc *
1.55      kristaps  247: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  248: {
                    249:        struct mdoc     *p;
                    250:
1.113     kristaps  251:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    252:
1.74      kristaps  253:        if (cb)
1.113     kristaps  254:                memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.105     kristaps  255:
1.1       kristaps  256:        p->data = data;
1.73      kristaps  257:        p->pflags = pflags;
                    258:
1.113     kristaps  259:        mdoc_hash_init();
                    260:        mdoc_alloc1(p);
                    261:        return(p);
1.1       kristaps  262: }
                    263:
                    264:
1.68      kristaps  265: /*
                    266:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.85      kristaps  267:  * through to macro_end() in macro.c.
1.68      kristaps  268:  */
1.1       kristaps  269: int
1.72      kristaps  270: mdoc_endparse(struct mdoc *m)
1.20      kristaps  271: {
                    272:
1.72      kristaps  273:        if (MDOC_HALT & m->flags)
1.20      kristaps  274:                return(0);
1.72      kristaps  275:        else if (mdoc_macroend(m))
1.20      kristaps  276:                return(1);
1.72      kristaps  277:        m->flags |= MDOC_HALT;
                    278:        return(0);
1.20      kristaps  279: }
                    280:
                    281:
1.50      kristaps  282: /*
1.53      kristaps  283:  * Main parse routine.  Parses a single line -- really just hands off to
1.123   ! kristaps  284:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps  285:  */
1.20      kristaps  286: int
1.53      kristaps  287: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  288: {
                    289:
1.53      kristaps  290:        if (MDOC_HALT & m->flags)
1.20      kristaps  291:                return(0);
1.50      kristaps  292:
1.123   ! kristaps  293:        return('.' == *buf ? mdoc_pmacro(m, ln, buf) :
        !           294:                        mdoc_ptext(m, ln, buf));
1.1       kristaps  295: }
                    296:
                    297:
                    298: int
1.31      kristaps  299: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    300:                const char *fmt, ...)
1.1       kristaps  301: {
1.31      kristaps  302:        char             buf[256];
                    303:        va_list          ap;
1.1       kristaps  304:
                    305:        if (NULL == mdoc->cb.mdoc_err)
                    306:                return(0);
1.31      kristaps  307:
                    308:        va_start(ap, fmt);
                    309:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    310:        va_end(ap);
1.88      kristaps  311:
1.31      kristaps  312:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  313: }
                    314:
                    315:
                    316: int
1.87      kristaps  317: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  318: {
1.31      kristaps  319:        char             buf[256];
                    320:        va_list          ap;
1.1       kristaps  321:
                    322:        if (NULL == mdoc->cb.mdoc_warn)
                    323:                return(0);
1.31      kristaps  324:
                    325:        va_start(ap, fmt);
                    326:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    327:        va_end(ap);
1.88      kristaps  328:
1.87      kristaps  329:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1       kristaps  330: }
                    331:
                    332:
                    333: int
1.88      kristaps  334: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78      kristaps  335: {
1.89      kristaps  336:        const char      *p;
1.88      kristaps  337:
1.89      kristaps  338:        p = __mdoc_merrnames[(int)type];
1.73      kristaps  339:        assert(p);
1.88      kristaps  340:
                    341:        if (iserr)
                    342:                return(mdoc_verr(m, line, pos, p));
                    343:
                    344:        return(mdoc_vwarn(m, line, pos, p));
                    345: }
                    346:
                    347:
                    348: int
1.117     kristaps  349: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88      kristaps  350:                int ln, int pp, int *pos, char *buf)
                    351: {
1.122     kristaps  352:        assert(tok < MDOC_MAX);
                    353:
                    354:        /* If we're in the body, deny prologue calls. */
1.117     kristaps  355:
1.88      kristaps  356:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122     kristaps  357:                        MDOC_PBODY & m->flags)
                    358:                return(mdoc_perr(m, ln, pp, EPROLBODY));
                    359:
                    360:        /* If we're in the prologue, deny "body" macros.  */
                    361:
                    362:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    363:                        ! (MDOC_PBODY & m->flags)) {
1.120     kristaps  364:                if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
                    365:                        return(0);
                    366:                if (NULL == m->meta.title)
                    367:                        m->meta.title = mandoc_strdup("unknown");
                    368:                if (NULL == m->meta.vol)
                    369:                        m->meta.vol = mandoc_strdup("local");
                    370:                if (NULL == m->meta.os)
                    371:                        m->meta.os = mandoc_strdup("local");
                    372:                if (0 == m->meta.date)
                    373:                        m->meta.date = time(NULL);
                    374:                m->flags |= MDOC_PBODY;
                    375:        }
1.88      kristaps  376:
                    377:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73      kristaps  378: }
                    379:
                    380:
                    381: static int
                    382: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  383: {
                    384:
1.25      kristaps  385:        assert(mdoc->last);
                    386:        assert(mdoc->first);
                    387:        assert(MDOC_ROOT != p->type);
1.1       kristaps  388:
1.13      kristaps  389:        switch (mdoc->next) {
                    390:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  391:                mdoc->last->next = p;
                    392:                p->prev = mdoc->last;
1.13      kristaps  393:                p->parent = mdoc->last->parent;
1.1       kristaps  394:                break;
1.13      kristaps  395:        case (MDOC_NEXT_CHILD):
                    396:                mdoc->last->child = p;
1.1       kristaps  397:                p->parent = mdoc->last;
                    398:                break;
                    399:        default:
1.13      kristaps  400:                abort();
                    401:                /* NOTREACHED */
1.1       kristaps  402:        }
                    403:
1.86      kristaps  404:        p->parent->nchild++;
                    405:
1.23      kristaps  406:        if ( ! mdoc_valid_pre(mdoc, p))
                    407:                return(0);
1.68      kristaps  408:        if ( ! mdoc_action_pre(mdoc, p))
                    409:                return(0);
1.27      kristaps  410:
                    411:        switch (p->type) {
                    412:        case (MDOC_HEAD):
                    413:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  414:                p->parent->head = p;
1.27      kristaps  415:                break;
                    416:        case (MDOC_TAIL):
                    417:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  418:                p->parent->tail = p;
1.27      kristaps  419:                break;
                    420:        case (MDOC_BODY):
                    421:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  422:                p->parent->body = p;
1.27      kristaps  423:                break;
                    424:        default:
                    425:                break;
                    426:        }
                    427:
1.1       kristaps  428:        mdoc->last = p;
1.71      kristaps  429:
                    430:        switch (p->type) {
                    431:        case (MDOC_TEXT):
                    432:                if ( ! mdoc_valid_post(mdoc))
                    433:                        return(0);
                    434:                if ( ! mdoc_action_post(mdoc))
                    435:                        return(0);
                    436:                break;
                    437:        default:
                    438:                break;
                    439:        }
                    440:
1.23      kristaps  441:        return(1);
1.1       kristaps  442: }
                    443:
                    444:
1.45      kristaps  445: static struct mdoc_node *
1.117     kristaps  446: node_alloc(struct mdoc *m, int line, int pos,
                    447:                enum mdoct tok, enum mdoc_type type)
1.45      kristaps  448: {
1.46      kristaps  449:        struct mdoc_node *p;
                    450:
1.113     kristaps  451:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91      kristaps  452:        p->sec = m->lastsec;
1.73      kristaps  453:        p->line = line;
                    454:        p->pos = pos;
                    455:        p->tok = tok;
1.118     kristaps  456:        p->type = type;
1.45      kristaps  457:
1.46      kristaps  458:        return(p);
1.45      kristaps  459: }
                    460:
                    461:
1.23      kristaps  462: int
1.117     kristaps  463: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17      kristaps  464: {
                    465:        struct mdoc_node *p;
                    466:
1.91      kristaps  467:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102     kristaps  468:        if ( ! node_append(m, p))
                    469:                return(0);
                    470:        m->next = MDOC_NEXT_CHILD;
                    471:        return(1);
1.17      kristaps  472: }
                    473:
                    474:
1.23      kristaps  475: int
1.117     kristaps  476: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  477: {
                    478:        struct mdoc_node *p;
                    479:
1.91      kristaps  480:        assert(m->first);
                    481:        assert(m->last);
1.1       kristaps  482:
1.91      kristaps  483:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102     kristaps  484:        if ( ! node_append(m, p))
                    485:                return(0);
                    486:        m->next = MDOC_NEXT_CHILD;
                    487:        return(1);
1.1       kristaps  488: }
                    489:
                    490:
1.23      kristaps  491: int
1.117     kristaps  492: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  493: {
                    494:        struct mdoc_node *p;
                    495:
1.91      kristaps  496:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102     kristaps  497:        if ( ! node_append(m, p))
                    498:                return(0);
                    499:        m->next = MDOC_NEXT_CHILD;
                    500:        return(1);
1.1       kristaps  501: }
                    502:
                    503:
1.23      kristaps  504: int
1.91      kristaps  505: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  506:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  507: {
                    508:        struct mdoc_node *p;
                    509:
1.91      kristaps  510:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77      kristaps  511:        p->args = args;
                    512:        if (p->args)
1.53      kristaps  513:                (args->refcnt)++;
1.102     kristaps  514:        if ( ! node_append(m, p))
                    515:                return(0);
                    516:        m->next = MDOC_NEXT_CHILD;
                    517:        return(1);
1.1       kristaps  518: }
                    519:
                    520:
1.23      kristaps  521: int
1.91      kristaps  522: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117     kristaps  523:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  524: {
                    525:        struct mdoc_node *p;
                    526:
1.91      kristaps  527:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77      kristaps  528:        p->args = args;
                    529:        if (p->args)
1.53      kristaps  530:                (args->refcnt)++;
1.102     kristaps  531:        if ( ! node_append(m, p))
                    532:                return(0);
                    533:        m->next = MDOC_NEXT_CHILD;
                    534:        return(1);
1.1       kristaps  535: }
                    536:
                    537:
1.91      kristaps  538: static int
                    539: pstring(struct mdoc *m, int line, int pos, const char *p, size_t len)
1.1       kristaps  540: {
1.91      kristaps  541:        struct mdoc_node *n;
                    542:        size_t            sv;
1.1       kristaps  543:
1.91      kristaps  544:        n = node_alloc(m, line, pos, -1, MDOC_TEXT);
1.113     kristaps  545:        n->string = mandoc_malloc(len + 1);
1.91      kristaps  546:        sv = strlcpy(n->string, p, len + 1);
                    547:
                    548:        /* Prohibit truncation. */
                    549:        assert(sv < len + 1);
                    550:
1.101     kristaps  551:        if ( ! node_append(m, n))
                    552:                return(0);
                    553:        m->next = MDOC_NEXT_SIBLING;
                    554:        return(1);
1.91      kristaps  555: }
                    556:
                    557:
                    558: int
                    559: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
                    560: {
                    561:
                    562:        return(pstring(m, line, pos, p, strlen(p)));
1.1       kristaps  563: }
                    564:
                    565:
1.53      kristaps  566: void
                    567: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  568: {
                    569:
1.53      kristaps  570:        if (p->string)
                    571:                free(p->string);
                    572:        if (p->args)
                    573:                mdoc_argv_free(p->args);
1.1       kristaps  574:        free(p);
                    575: }
                    576:
                    577:
1.121     kristaps  578: static void
                    579: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    580: {
                    581:
                    582:        /* Adjust siblings. */
                    583:
                    584:        if (n->prev)
                    585:                n->prev->next = n->next;
                    586:        if (n->next)
                    587:                n->next->prev = n->prev;
                    588:
                    589:        /* Adjust parent. */
                    590:
                    591:        if (n->parent) {
                    592:                n->parent->nchild--;
                    593:                if (n->parent->child == n)
                    594:                        n->parent->child = n->prev ? n->prev : n->next;
                    595:        }
                    596:
                    597:        /* Adjust parse point, if applicable. */
                    598:
                    599:        if (m && m->last == n) {
                    600:                if (n->prev) {
                    601:                        m->last = n->prev;
                    602:                        m->next = MDOC_NEXT_SIBLING;
                    603:                } else {
                    604:                        m->last = n->parent;
                    605:                        m->next = MDOC_NEXT_CHILD;
                    606:                }
                    607:        }
                    608:
                    609:        if (m && m->first == n)
                    610:                m->first = NULL;
                    611: }
                    612:
                    613:
1.53      kristaps  614: void
1.121     kristaps  615: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  616: {
                    617:
1.121     kristaps  618:        while (p->child) {
                    619:                assert(p->nchild);
                    620:                mdoc_node_delete(m, p->child);
                    621:        }
                    622:        assert(0 == p->nchild);
1.1       kristaps  623:
1.121     kristaps  624:        mdoc_node_unlink(m, p);
1.53      kristaps  625:        mdoc_node_free(p);
1.1       kristaps  626: }
                    627:
                    628:
1.53      kristaps  629: /*
                    630:  * Parse free-form text, that is, a line that does not begin with the
                    631:  * control character.
                    632:  */
                    633: static int
1.123   ! kristaps  634: mdoc_ptext(struct mdoc *m, int line, char *buf)
1.1       kristaps  635: {
1.91      kristaps  636:        int              i, j;
1.116     kristaps  637:        char             sv;
1.1       kristaps  638:
1.123   ! kristaps  639:        /* Ignore bogus comments. */
        !           640:
        !           641:        if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
        !           642:                return(mdoc_pwarn(m, line, 0, EBADCOMMENT));
        !           643:
1.85      kristaps  644:        if (SEC_NONE == m->lastnamed)
1.88      kristaps  645:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.91      kristaps  646:
                    647:        /*
                    648:         * If in literal mode, then pass the buffer directly to the
                    649:         * back-end, as it should be preserved as a single term.
                    650:         */
                    651:
1.101     kristaps  652:        if (MDOC_LITERAL & m->flags)
                    653:                return(mdoc_word_alloc(m, line, 0, buf));
1.91      kristaps  654:
                    655:        /* Disallow blank/white-space lines in non-literal mode. */
1.1       kristaps  656:
1.91      kristaps  657:        for (i = 0; ' ' == buf[i]; i++)
                    658:                /* Skip leading whitespace. */ ;
1.115     kristaps  659:
1.119     kristaps  660:        if ('\0' == buf[i]) {
                    661:                if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
                    662:                        return(0);
                    663:                /*
                    664:                 * Assume that a `Pp' should be inserted in the case of
                    665:                 * a blank line.  Technically, blank lines aren't
                    666:                 * allowed, but enough manuals assume this behaviour
                    667:                 * that we want to work around it.
                    668:                 */
                    669:                if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
                    670:                        return(0);
                    671:        }
1.68      kristaps  672:
1.91      kristaps  673:        /*
                    674:         * Break apart a free-form line into tokens.  Spaces are
                    675:         * stripped out of the input.
                    676:         */
                    677:
                    678:        for (j = i; buf[i]; i++) {
                    679:                if (' ' != buf[i])
                    680:                        continue;
                    681:
                    682:                /* Escaped whitespace. */
                    683:                if (i && ' ' == buf[i] && '\\' == buf[i - 1])
                    684:                        continue;
                    685:
1.116     kristaps  686:                sv = buf[i];
1.115     kristaps  687:                buf[i++] = '\0';
                    688:
1.91      kristaps  689:                if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
                    690:                        return(0);
                    691:
1.116     kristaps  692:                /* Trailing whitespace?  Check at overwritten byte. */
                    693:
                    694:                if (' ' == sv && '\0' == buf[i])
                    695:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    696:                                return(0);
                    697:
1.91      kristaps  698:                for ( ; ' ' == buf[i]; i++)
                    699:                        /* Skip trailing whitespace. */ ;
                    700:
                    701:                j = i;
1.116     kristaps  702:
                    703:                /* Trailing whitespace? */
                    704:
                    705:                if (' ' == buf[i - 1] && '\0' == buf[i])
                    706:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    707:                                return(0);
                    708:
1.115     kristaps  709:                if ('\0' == buf[i])
1.91      kristaps  710:                        break;
                    711:        }
                    712:
                    713:        if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.53      kristaps  714:                return(0);
1.1       kristaps  715:
1.68      kristaps  716:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  717:        return(1);
1.1       kristaps  718: }
                    719:
                    720:
1.91      kristaps  721:
1.58      kristaps  722: static int
                    723: macrowarn(struct mdoc *m, int ln, const char *buf)
                    724: {
                    725:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.96      kristaps  726:                return(mdoc_verr(m, ln, 0,
1.73      kristaps  727:                                "unknown macro: %s%s",
1.59      kristaps  728:                                buf, strlen(buf) > 3 ? "..." : ""));
1.96      kristaps  729:        return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  730:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  731: }
                    732:
                    733:
1.53      kristaps  734: /*
                    735:  * Parse a macro line, that is, a line beginning with the control
                    736:  * character.
                    737:  */
                    738: int
1.123   ! kristaps  739: mdoc_pmacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  740: {
1.103     kristaps  741:        int               i, j, c;
1.53      kristaps  742:        char              mac[5];
1.1       kristaps  743:
1.81      kristaps  744:        /* Empty lines are ignored. */
1.63      kristaps  745:
1.115     kristaps  746:        if ('\0' == buf[1])
1.63      kristaps  747:                return(1);
                    748:
1.100     kristaps  749:        i = 1;
                    750:
                    751:        /* Accept whitespace after the initial control char. */
                    752:
                    753:        if (' ' == buf[i]) {
                    754:                i++;
1.65      kristaps  755:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  756:                        i++;
1.115     kristaps  757:                if ('\0' == buf[i])
1.63      kristaps  758:                        return(1);
                    759:        }
1.1       kristaps  760:
1.53      kristaps  761:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  762:
1.100     kristaps  763:        for (j = 0; j < 4; j++, i++) {
1.115     kristaps  764:                if ('\0' == (mac[j] = buf[i]))
1.53      kristaps  765:                        break;
1.65      kristaps  766:                else if (' ' == buf[i])
1.53      kristaps  767:                        break;
1.104     kristaps  768:
                    769:                /* Check for invalid characters. */
                    770:
                    771:                if (isgraph((u_char)buf[i]))
                    772:                        continue;
                    773:                return(mdoc_perr(m, ln, i, EPRINT));
1.53      kristaps  774:        }
1.1       kristaps  775:
1.100     kristaps  776:        mac[j] = 0;
1.1       kristaps  777:
1.100     kristaps  778:        if (j == 4 || j < 2) {
1.58      kristaps  779:                if ( ! macrowarn(m, ln, mac))
                    780:                        goto err;
                    781:                return(1);
1.53      kristaps  782:        }
                    783:
1.105     kristaps  784:        if (MDOC_MAX == (c = mdoc_hash_find(mac))) {
1.58      kristaps  785:                if ( ! macrowarn(m, ln, mac))
                    786:                        goto err;
                    787:                return(1);
1.53      kristaps  788:        }
1.1       kristaps  789:
1.53      kristaps  790:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  791:
1.65      kristaps  792:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  793:                i++;
1.1       kristaps  794:
1.115     kristaps  795:        /* Trailing whitespace? */
                    796:
                    797:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    798:                if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
                    799:                        goto err;
                    800:
1.98      kristaps  801:        /*
                    802:         * Begin recursive parse sequence.  Since we're at the start of
                    803:         * the line, we don't need to do callable/parseable checks.
                    804:         */
1.103     kristaps  805:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
1.53      kristaps  806:                goto err;
1.1       kristaps  807:
1.53      kristaps  808:        return(1);
1.1       kristaps  809:
1.53      kristaps  810: err:   /* Error out. */
1.1       kristaps  811:
1.53      kristaps  812:        m->flags |= MDOC_HALT;
                    813:        return(0);
1.1       kristaps  814: }
1.100     kristaps  815:
                    816:

CVSweb