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

Annotation of mandoc/mdoc.c, Revision 1.109

1.109   ! kristaps    1: /*     $Id: mdoc.c,v 1.108 2009/10/15 01:33: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.1       kristaps   27:
1.89      kristaps   28: const  char *const __mdoc_merrnames[MERRMAX] = {
                     29:        "trailing whitespace", /* ETAILWS */
                     30:        "unexpected quoted parameter", /* EQUOTPARM */
                     31:        "unterminated quoted parameter", /* EQUOTTERM */
                     32:        "system: malloc error", /* EMALLOC */
                     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.93      kristaps  119:        "Dx",           "\%Q",          "br",           "sp"
1.1       kristaps  120:        };
                    121:
                    122: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                    123:        "split",                "nosplit",              "ragged",
                    124:        "unfilled",             "literal",              "file",
                    125:        "offset",               "bullet",               "dash",
                    126:        "hyphen",               "item",                 "enum",
                    127:        "tag",                  "diag",                 "hang",
                    128:        "ohang",                "inset",                "column",
                    129:        "width",                "compact",              "std",
1.52      kristaps  130:        "filled",               "words",                "emphasis",
1.108     kristaps  131:        "symbolic",             "nested",               "centered"
1.1       kristaps  132:        };
                    133:
                    134: const  char * const *mdoc_macronames = __mdoc_macronames;
                    135: const  char * const *mdoc_argnames = __mdoc_argnames;
                    136:
1.73      kristaps  137: static void              mdoc_free1(struct mdoc *);
                    138: static int               mdoc_alloc1(struct mdoc *);
                    139: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
                    140:                                int, enum mdoc_type);
                    141: static int               node_append(struct mdoc *,
1.71      kristaps  142:                                struct mdoc_node *);
                    143: static int               parsetext(struct mdoc *, int, char *);
                    144: static int               parsemacro(struct mdoc *, int, char *);
                    145: static int               macrowarn(struct mdoc *, int, const char *);
1.91      kristaps  146: static int               pstring(struct mdoc *, int, int,
                    147:                                const char *, size_t);
1.88      kristaps  148:
1.92      kristaps  149: #ifdef __linux__
                    150: extern size_t            strlcpy(char *, const char *, size_t);
                    151: #endif
                    152:
1.71      kristaps  153:
1.1       kristaps  154: const struct mdoc_node *
1.71      kristaps  155: mdoc_node(const struct mdoc *m)
1.1       kristaps  156: {
                    157:
1.71      kristaps  158:        return(MDOC_HALT & m->flags ? NULL : m->first);
1.1       kristaps  159: }
                    160:
                    161:
1.37      kristaps  162: const struct mdoc_meta *
1.71      kristaps  163: mdoc_meta(const struct mdoc *m)
1.37      kristaps  164: {
                    165:
1.71      kristaps  166:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37      kristaps  167: }
                    168:
                    169:
1.85      kristaps  170: /*
                    171:  * Frees volatile resources (parse tree, meta-data, fields).
                    172:  */
1.73      kristaps  173: static void
                    174: mdoc_free1(struct mdoc *mdoc)
1.67      kristaps  175: {
                    176:
                    177:        if (mdoc->first)
                    178:                mdoc_node_freelist(mdoc->first);
                    179:        if (mdoc->meta.title)
                    180:                free(mdoc->meta.title);
                    181:        if (mdoc->meta.os)
                    182:                free(mdoc->meta.os);
                    183:        if (mdoc->meta.name)
                    184:                free(mdoc->meta.name);
                    185:        if (mdoc->meta.arch)
                    186:                free(mdoc->meta.arch);
                    187:        if (mdoc->meta.vol)
                    188:                free(mdoc->meta.vol);
1.73      kristaps  189: }
                    190:
                    191:
1.85      kristaps  192: /*
                    193:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    194:  */
1.73      kristaps  195: static int
                    196: mdoc_alloc1(struct mdoc *mdoc)
                    197: {
1.67      kristaps  198:
                    199:        bzero(&mdoc->meta, sizeof(struct mdoc_meta));
                    200:        mdoc->flags = 0;
1.85      kristaps  201:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.70      kristaps  202:        mdoc->last = calloc(1, sizeof(struct mdoc_node));
                    203:        if (NULL == mdoc->last)
1.73      kristaps  204:                return(0);
                    205:
1.70      kristaps  206:        mdoc->first = mdoc->last;
1.67      kristaps  207:        mdoc->last->type = MDOC_ROOT;
                    208:        mdoc->next = MDOC_NEXT_CHILD;
1.73      kristaps  209:        return(1);
                    210: }
                    211:
                    212:
                    213: /*
1.85      kristaps  214:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    215:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    216:  * and the parser is ready for re-invocation on a new tree; however,
                    217:  * cross-parse non-volatile data is kept intact.
1.73      kristaps  218:  */
                    219: int
                    220: mdoc_reset(struct mdoc *mdoc)
                    221: {
                    222:
                    223:        mdoc_free1(mdoc);
                    224:        return(mdoc_alloc1(mdoc));
1.67      kristaps  225: }
                    226:
                    227:
1.68      kristaps  228: /*
1.85      kristaps  229:  * Completely free up all volatile and non-volatile parse resources.
                    230:  * After invocation, the pointer is no longer usable.
1.68      kristaps  231:  */
1.67      kristaps  232: void
1.38      kristaps  233: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  234: {
                    235:
1.73      kristaps  236:        mdoc_free1(mdoc);
1.1       kristaps  237:        free(mdoc);
                    238: }
                    239:
                    240:
1.85      kristaps  241: /*
                    242:  * Allocate volatile and non-volatile parse resources.
                    243:  */
1.1       kristaps  244: struct mdoc *
1.55      kristaps  245: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  246: {
                    247:        struct mdoc     *p;
                    248:
1.70      kristaps  249:        if (NULL == (p = calloc(1, sizeof(struct mdoc))))
1.73      kristaps  250:                return(NULL);
1.74      kristaps  251:        if (cb)
                    252:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1       kristaps  253:
1.105     kristaps  254:        mdoc_hash_init();
                    255:
1.1       kristaps  256:        p->data = data;
1.73      kristaps  257:        p->pflags = pflags;
                    258:
1.105     kristaps  259:        if (mdoc_alloc1(p))
1.74      kristaps  260:                return(p);
1.1       kristaps  261:
1.73      kristaps  262:        free(p);
                    263:        return(NULL);
1.1       kristaps  264: }
                    265:
                    266:
1.68      kristaps  267: /*
                    268:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.85      kristaps  269:  * through to macro_end() in macro.c.
1.68      kristaps  270:  */
1.1       kristaps  271: int
1.72      kristaps  272: mdoc_endparse(struct mdoc *m)
1.20      kristaps  273: {
                    274:
1.72      kristaps  275:        if (MDOC_HALT & m->flags)
1.20      kristaps  276:                return(0);
1.72      kristaps  277:        else if (mdoc_macroend(m))
1.20      kristaps  278:                return(1);
1.72      kristaps  279:        m->flags |= MDOC_HALT;
                    280:        return(0);
1.20      kristaps  281: }
                    282:
                    283:
1.50      kristaps  284: /*
1.53      kristaps  285:  * Main parse routine.  Parses a single line -- really just hands off to
1.85      kristaps  286:  * the macro (parsemacro()) or text parser (parsetext()).
1.50      kristaps  287:  */
1.20      kristaps  288: int
1.53      kristaps  289: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  290: {
                    291:
1.53      kristaps  292:        if (MDOC_HALT & m->flags)
1.20      kristaps  293:                return(0);
1.50      kristaps  294:
1.53      kristaps  295:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    296:                        parsetext(m, ln, buf));
1.1       kristaps  297: }
                    298:
                    299:
                    300: int
1.31      kristaps  301: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    302:                const char *fmt, ...)
1.1       kristaps  303: {
1.31      kristaps  304:        char             buf[256];
                    305:        va_list          ap;
1.1       kristaps  306:
                    307:        if (NULL == mdoc->cb.mdoc_err)
                    308:                return(0);
1.31      kristaps  309:
                    310:        va_start(ap, fmt);
                    311:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    312:        va_end(ap);
1.88      kristaps  313:
1.31      kristaps  314:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  315: }
                    316:
                    317:
                    318: int
1.87      kristaps  319: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  320: {
1.31      kristaps  321:        char             buf[256];
                    322:        va_list          ap;
1.1       kristaps  323:
                    324:        if (NULL == mdoc->cb.mdoc_warn)
                    325:                return(0);
1.31      kristaps  326:
                    327:        va_start(ap, fmt);
                    328:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    329:        va_end(ap);
1.88      kristaps  330:
1.87      kristaps  331:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1       kristaps  332: }
                    333:
                    334:
                    335: int
1.88      kristaps  336: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78      kristaps  337: {
1.89      kristaps  338:        const char      *p;
1.88      kristaps  339:
1.89      kristaps  340:        p = __mdoc_merrnames[(int)type];
1.73      kristaps  341:        assert(p);
1.88      kristaps  342:
                    343:        if (iserr)
                    344:                return(mdoc_verr(m, line, pos, p));
                    345:
                    346:        return(mdoc_vwarn(m, line, pos, p));
                    347: }
                    348:
                    349:
                    350: int
                    351: mdoc_macro(struct mdoc *m, int tok,
                    352:                int ln, int pp, int *pos, char *buf)
                    353: {
1.97      kristaps  354:        /*
                    355:         * If we're in the prologue, deny "body" macros.  Similarly, if
                    356:         * we're in the body, deny prologue calls.
                    357:         */
1.88      kristaps  358:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
                    359:                        MDOC_PBODY & m->flags)
                    360:                return(mdoc_perr(m, ln, pp, EPROLBODY));
                    361:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    362:                        ! (MDOC_PBODY & m->flags))
                    363:                return(mdoc_perr(m, ln, pp, EBODYPROL));
                    364:
                    365:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73      kristaps  366: }
                    367:
                    368:
                    369: static int
                    370: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  371: {
                    372:
1.25      kristaps  373:        assert(mdoc->last);
                    374:        assert(mdoc->first);
                    375:        assert(MDOC_ROOT != p->type);
1.1       kristaps  376:
1.13      kristaps  377:        switch (mdoc->next) {
                    378:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  379:                mdoc->last->next = p;
                    380:                p->prev = mdoc->last;
1.13      kristaps  381:                p->parent = mdoc->last->parent;
1.1       kristaps  382:                break;
1.13      kristaps  383:        case (MDOC_NEXT_CHILD):
                    384:                mdoc->last->child = p;
1.1       kristaps  385:                p->parent = mdoc->last;
                    386:                break;
                    387:        default:
1.13      kristaps  388:                abort();
                    389:                /* NOTREACHED */
1.1       kristaps  390:        }
                    391:
1.86      kristaps  392:        p->parent->nchild++;
                    393:
1.23      kristaps  394:        if ( ! mdoc_valid_pre(mdoc, p))
                    395:                return(0);
1.68      kristaps  396:        if ( ! mdoc_action_pre(mdoc, p))
                    397:                return(0);
1.27      kristaps  398:
                    399:        switch (p->type) {
                    400:        case (MDOC_HEAD):
                    401:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  402:                p->parent->head = p;
1.27      kristaps  403:                break;
                    404:        case (MDOC_TAIL):
                    405:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  406:                p->parent->tail = p;
1.27      kristaps  407:                break;
                    408:        case (MDOC_BODY):
                    409:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  410:                p->parent->body = p;
1.27      kristaps  411:                break;
                    412:        default:
                    413:                break;
                    414:        }
                    415:
1.1       kristaps  416:        mdoc->last = p;
1.71      kristaps  417:
                    418:        switch (p->type) {
                    419:        case (MDOC_TEXT):
                    420:                if ( ! mdoc_valid_post(mdoc))
                    421:                        return(0);
                    422:                if ( ! mdoc_action_post(mdoc))
                    423:                        return(0);
                    424:                break;
                    425:        default:
                    426:                break;
                    427:        }
                    428:
1.23      kristaps  429:        return(1);
1.1       kristaps  430: }
                    431:
                    432:
1.45      kristaps  433: static struct mdoc_node *
1.91      kristaps  434: node_alloc(struct mdoc *m, int line,
1.73      kristaps  435:                int pos, int tok, enum mdoc_type type)
1.45      kristaps  436: {
1.46      kristaps  437:        struct mdoc_node *p;
                    438:
1.73      kristaps  439:        if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
1.91      kristaps  440:                (void)mdoc_nerr(m, m->last, EMALLOC);
1.73      kristaps  441:                return(NULL);
                    442:        }
                    443:
1.91      kristaps  444:        p->sec = m->lastsec;
1.73      kristaps  445:        p->line = line;
                    446:        p->pos = pos;
                    447:        p->tok = tok;
                    448:        if (MDOC_TEXT != (p->type = type))
                    449:                assert(p->tok >= 0);
1.45      kristaps  450:
1.46      kristaps  451:        return(p);
1.45      kristaps  452: }
                    453:
                    454:
1.23      kristaps  455: int
1.91      kristaps  456: mdoc_tail_alloc(struct mdoc *m, int line, int pos, int tok)
1.17      kristaps  457: {
                    458:        struct mdoc_node *p;
                    459:
1.91      kristaps  460:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.73      kristaps  461:        if (NULL == p)
                    462:                return(0);
1.102     kristaps  463:        if ( ! node_append(m, p))
                    464:                return(0);
                    465:        m->next = MDOC_NEXT_CHILD;
                    466:        return(1);
1.17      kristaps  467: }
                    468:
                    469:
1.23      kristaps  470: int
1.91      kristaps  471: mdoc_head_alloc(struct mdoc *m, int line, int pos, int tok)
1.1       kristaps  472: {
                    473:        struct mdoc_node *p;
                    474:
1.91      kristaps  475:        assert(m->first);
                    476:        assert(m->last);
1.1       kristaps  477:
1.91      kristaps  478:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.73      kristaps  479:        if (NULL == p)
                    480:                return(0);
1.102     kristaps  481:        if ( ! node_append(m, p))
                    482:                return(0);
                    483:        m->next = MDOC_NEXT_CHILD;
                    484:        return(1);
1.1       kristaps  485: }
                    486:
                    487:
1.23      kristaps  488: int
1.91      kristaps  489: mdoc_body_alloc(struct mdoc *m, int line, int pos, int tok)
1.1       kristaps  490: {
                    491:        struct mdoc_node *p;
                    492:
1.91      kristaps  493:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.73      kristaps  494:        if (NULL == p)
                    495:                return(0);
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_block_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_BLOCK);
1.73      kristaps  510:        if (NULL == p)
                    511:                return(0);
1.77      kristaps  512:        p->args = args;
                    513:        if (p->args)
1.53      kristaps  514:                (args->refcnt)++;
1.102     kristaps  515:        if ( ! node_append(m, p))
                    516:                return(0);
                    517:        m->next = MDOC_NEXT_CHILD;
                    518:        return(1);
1.1       kristaps  519: }
                    520:
                    521:
1.23      kristaps  522: int
1.91      kristaps  523: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.53      kristaps  524:                int tok, struct mdoc_arg *args)
1.1       kristaps  525: {
                    526:        struct mdoc_node *p;
                    527:
1.91      kristaps  528:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.73      kristaps  529:        if (NULL == p)
                    530:                return(0);
1.77      kristaps  531:        p->args = args;
                    532:        if (p->args)
1.53      kristaps  533:                (args->refcnt)++;
1.102     kristaps  534:        if ( ! node_append(m, p))
                    535:                return(0);
                    536:        m->next = MDOC_NEXT_CHILD;
                    537:        return(1);
1.1       kristaps  538: }
                    539:
                    540:
1.91      kristaps  541: static int
                    542: pstring(struct mdoc *m, int line, int pos, const char *p, size_t len)
1.1       kristaps  543: {
1.91      kristaps  544:        struct mdoc_node *n;
                    545:        size_t            sv;
1.1       kristaps  546:
1.91      kristaps  547:        n = node_alloc(m, line, pos, -1, MDOC_TEXT);
                    548:        if (NULL == n)
                    549:                return(mdoc_nerr(m, m->last, EMALLOC));
                    550:
                    551:        n->string = malloc(len + 1);
                    552:        if (NULL == n->string) {
                    553:                free(n);
                    554:                return(mdoc_nerr(m, m->last, EMALLOC));
1.73      kristaps  555:        }
1.88      kristaps  556:
1.91      kristaps  557:        sv = strlcpy(n->string, p, len + 1);
                    558:
                    559:        /* Prohibit truncation. */
                    560:        assert(sv < len + 1);
                    561:
1.101     kristaps  562:        if ( ! node_append(m, n))
                    563:                return(0);
                    564:        m->next = MDOC_NEXT_SIBLING;
                    565:        return(1);
1.91      kristaps  566: }
                    567:
                    568:
                    569: int
                    570: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
                    571: {
                    572:
                    573:        return(pstring(m, line, pos, p, strlen(p)));
1.1       kristaps  574: }
                    575:
                    576:
1.53      kristaps  577: void
                    578: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  579: {
                    580:
1.86      kristaps  581:        if (p->parent)
                    582:                p->parent->nchild--;
1.53      kristaps  583:        if (p->string)
                    584:                free(p->string);
                    585:        if (p->args)
                    586:                mdoc_argv_free(p->args);
1.1       kristaps  587:        free(p);
                    588: }
                    589:
                    590:
1.53      kristaps  591: void
                    592: mdoc_node_freelist(struct mdoc_node *p)
1.1       kristaps  593: {
                    594:
1.53      kristaps  595:        if (p->child)
                    596:                mdoc_node_freelist(p->child);
                    597:        if (p->next)
                    598:                mdoc_node_freelist(p->next);
1.1       kristaps  599:
1.86      kristaps  600:        assert(0 == p->nchild);
1.53      kristaps  601:        mdoc_node_free(p);
1.1       kristaps  602: }
                    603:
                    604:
1.53      kristaps  605: /*
                    606:  * Parse free-form text, that is, a line that does not begin with the
                    607:  * control character.
                    608:  */
                    609: static int
1.68      kristaps  610: parsetext(struct mdoc *m, int line, char *buf)
1.1       kristaps  611: {
1.91      kristaps  612:        int              i, j;
1.1       kristaps  613:
1.85      kristaps  614:        if (SEC_NONE == m->lastnamed)
1.88      kristaps  615:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.91      kristaps  616:
                    617:        /*
                    618:         * If in literal mode, then pass the buffer directly to the
                    619:         * back-end, as it should be preserved as a single term.
                    620:         */
                    621:
1.101     kristaps  622:        if (MDOC_LITERAL & m->flags)
                    623:                return(mdoc_word_alloc(m, line, 0, buf));
1.91      kristaps  624:
                    625:        /* Disallow blank/white-space lines in non-literal mode. */
1.1       kristaps  626:
1.91      kristaps  627:        for (i = 0; ' ' == buf[i]; i++)
                    628:                /* Skip leading whitespace. */ ;
                    629:        if (0 == buf[i])
1.88      kristaps  630:                return(mdoc_perr(m, line, 0, ENOBLANK));
1.68      kristaps  631:
1.91      kristaps  632:        /*
                    633:         * Break apart a free-form line into tokens.  Spaces are
                    634:         * stripped out of the input.
                    635:         */
                    636:
                    637:        for (j = i; buf[i]; i++) {
                    638:                if (' ' != buf[i])
                    639:                        continue;
                    640:
                    641:                /* Escaped whitespace. */
                    642:                if (i && ' ' == buf[i] && '\\' == buf[i - 1])
                    643:                        continue;
                    644:
                    645:                buf[i++] = 0;
                    646:                if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
                    647:                        return(0);
                    648:
                    649:                for ( ; ' ' == buf[i]; i++)
                    650:                        /* Skip trailing whitespace. */ ;
                    651:
                    652:                j = i;
                    653:                if (0 == buf[i])
                    654:                        break;
                    655:        }
                    656:
                    657:        if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.53      kristaps  658:                return(0);
1.1       kristaps  659:
1.68      kristaps  660:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  661:        return(1);
1.1       kristaps  662: }
                    663:
                    664:
1.91      kristaps  665:
                    666:
1.58      kristaps  667: static int
                    668: macrowarn(struct mdoc *m, int ln, const char *buf)
                    669: {
                    670:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.96      kristaps  671:                return(mdoc_verr(m, ln, 0,
1.73      kristaps  672:                                "unknown macro: %s%s",
1.59      kristaps  673:                                buf, strlen(buf) > 3 ? "..." : ""));
1.96      kristaps  674:        return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59      kristaps  675:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  676: }
                    677:
                    678:
1.53      kristaps  679: /*
                    680:  * Parse a macro line, that is, a line beginning with the control
                    681:  * character.
                    682:  */
                    683: int
                    684: parsemacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  685: {
1.103     kristaps  686:        int               i, j, c;
1.53      kristaps  687:        char              mac[5];
1.1       kristaps  688:
1.81      kristaps  689:        /* Empty lines are ignored. */
1.63      kristaps  690:
                    691:        if (0 == buf[1])
                    692:                return(1);
                    693:
1.100     kristaps  694:        i = 1;
                    695:
                    696:        /* Accept whitespace after the initial control char. */
                    697:
                    698:        if (' ' == buf[i]) {
                    699:                i++;
1.65      kristaps  700:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  701:                        i++;
                    702:                if (0 == buf[i])
                    703:                        return(1);
                    704:        }
1.1       kristaps  705:
1.53      kristaps  706:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  707:
1.100     kristaps  708:        for (j = 0; j < 4; j++, i++) {
                    709:                if (0 == (mac[j] = buf[i]))
1.53      kristaps  710:                        break;
1.65      kristaps  711:                else if (' ' == buf[i])
1.53      kristaps  712:                        break;
1.104     kristaps  713:
                    714:                /* Check for invalid characters. */
                    715:
                    716:                if (isgraph((u_char)buf[i]))
                    717:                        continue;
                    718:                return(mdoc_perr(m, ln, i, EPRINT));
1.53      kristaps  719:        }
1.1       kristaps  720:
1.100     kristaps  721:        mac[j] = 0;
1.1       kristaps  722:
1.100     kristaps  723:        if (j == 4 || j < 2) {
1.58      kristaps  724:                if ( ! macrowarn(m, ln, mac))
                    725:                        goto err;
                    726:                return(1);
1.53      kristaps  727:        }
                    728:
1.105     kristaps  729:        if (MDOC_MAX == (c = mdoc_hash_find(mac))) {
1.58      kristaps  730:                if ( ! macrowarn(m, ln, mac))
                    731:                        goto err;
                    732:                return(1);
1.53      kristaps  733:        }
1.1       kristaps  734:
1.53      kristaps  735:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  736:
1.65      kristaps  737:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  738:                i++;
1.1       kristaps  739:
1.98      kristaps  740:        /*
                    741:         * Begin recursive parse sequence.  Since we're at the start of
                    742:         * the line, we don't need to do callable/parseable checks.
                    743:         */
1.103     kristaps  744:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
1.53      kristaps  745:                goto err;
1.1       kristaps  746:
1.53      kristaps  747:        return(1);
1.1       kristaps  748:
1.53      kristaps  749: err:   /* Error out. */
1.1       kristaps  750:
1.53      kristaps  751:        m->flags |= MDOC_HALT;
                    752:        return(0);
1.1       kristaps  753: }
1.100     kristaps  754:
                    755:

CVSweb