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

Annotation of mandoc/mdoc.c, Revision 1.93

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

CVSweb