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

Annotation of mandoc/mdoc.c, Revision 1.114

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

CVSweb