[BACK]Return to mdoc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.10

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