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

Annotation of mandoc/mdoc.c, Revision 1.101

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

CVSweb