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

Annotation of mandoc/mdoc.c, Revision 1.113

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

CVSweb