[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.8

1.8     ! schwarze    1: /*     $Id: mdoc.c,v 1.7 2009/06/18 23:34:53 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;
                    147:        mdoc->lastnamed = mdoc->lastsec = 0;
                    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:        /* FIXME - these should happen during validation. */
                    375:
                    376:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
                    377:                        SEC_PROLOGUE != m->lastnamed)
                    378:                return(perr(m, ln, pp, EPROLBODY));
                    379:
                    380:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    381:                        SEC_PROLOGUE == m->lastnamed)
                    382:                return(perr(m, ln, pp, EBODYPROL));
                    383:
                    384:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
                    385:                return(perr(m, ln, pp, ENOCALL));
                    386:
                    387:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
                    388: }
                    389:
                    390:
                    391: static int
                    392: perr(struct mdoc *m, int line, int pos, enum merr type)
                    393: {
                    394:        char            *p;
                    395:
                    396:        p = NULL;
                    397:        switch (type) {
                    398:        case (ENOCALL):
                    399:                p = "not callable";
                    400:                break;
                    401:        case (EPROLBODY):
                    402:                p = "macro disallowed in document body";
                    403:                break;
                    404:        case (EBODYPROL):
                    405:                p = "macro disallowed in document prologue";
                    406:                break;
                    407:        case (EMALLOC):
                    408:                p = "memory exhausted";
                    409:                break;
                    410:        case (ETEXTPROL):
                    411:                p = "text disallowed in document prologue";
                    412:                break;
                    413:        case (ENOBLANK):
                    414:                p = "blank lines disallowed in non-literal contexts";
                    415:                break;
                    416:        case (ESPACE):
                    417:                p = "whitespace disallowed after delimiter";
                    418:                break;
                    419:        }
                    420:        assert(p);
                    421:        return(mdoc_perr(m, line, pos, p));
                    422: }
                    423:
                    424:
                    425: static int
                    426: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    427: {
                    428:
                    429:        assert(mdoc->last);
                    430:        assert(mdoc->first);
                    431:        assert(MDOC_ROOT != p->type);
                    432:
                    433:        switch (mdoc->next) {
                    434:        case (MDOC_NEXT_SIBLING):
                    435:                mdoc->last->next = p;
                    436:                p->prev = mdoc->last;
                    437:                p->parent = mdoc->last->parent;
                    438:                break;
                    439:        case (MDOC_NEXT_CHILD):
                    440:                mdoc->last->child = p;
                    441:                p->parent = mdoc->last;
                    442:                break;
                    443:        default:
                    444:                abort();
                    445:                /* NOTREACHED */
                    446:        }
                    447:
                    448:        if ( ! mdoc_valid_pre(mdoc, p))
                    449:                return(0);
                    450:        if ( ! mdoc_action_pre(mdoc, p))
                    451:                return(0);
                    452:
                    453:        switch (p->type) {
                    454:        case (MDOC_HEAD):
                    455:                assert(MDOC_BLOCK == p->parent->type);
                    456:                p->parent->head = p;
                    457:                break;
                    458:        case (MDOC_TAIL):
                    459:                assert(MDOC_BLOCK == p->parent->type);
                    460:                p->parent->tail = p;
                    461:                break;
                    462:        case (MDOC_BODY):
                    463:                assert(MDOC_BLOCK == p->parent->type);
                    464:                p->parent->body = p;
                    465:                break;
                    466:        default:
                    467:                break;
                    468:        }
                    469:
                    470:        mdoc->last = p;
                    471:
                    472:        switch (p->type) {
                    473:        case (MDOC_TEXT):
                    474:                if ( ! mdoc_valid_post(mdoc))
                    475:                        return(0);
                    476:                if ( ! mdoc_action_post(mdoc))
                    477:                        return(0);
                    478:                break;
                    479:        default:
                    480:                break;
                    481:        }
                    482:
                    483:        return(1);
                    484: }
                    485:
                    486:
                    487: static struct mdoc_node *
                    488: node_alloc(struct mdoc *mdoc, int line,
                    489:                int pos, int tok, enum mdoc_type type)
                    490: {
                    491:        struct mdoc_node *p;
                    492:
                    493:        if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
1.8     ! schwarze  494:                (void)perr(mdoc, (mdoc)->last->line,
        !           495:                                (mdoc)->last->pos, EMALLOC);
1.1       kristaps  496:                return(NULL);
                    497:        }
                    498:
                    499:        p->sec = mdoc->lastsec;
                    500:        p->line = line;
                    501:        p->pos = pos;
                    502:        p->tok = tok;
                    503:        if (MDOC_TEXT != (p->type = type))
                    504:                assert(p->tok >= 0);
                    505:
                    506:        return(p);
                    507: }
                    508:
                    509:
                    510: int
                    511: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    512: {
                    513:        struct mdoc_node *p;
                    514:
                    515:        p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
                    516:        if (NULL == p)
                    517:                return(0);
                    518:        return(node_append(mdoc, p));
                    519: }
                    520:
                    521:
                    522: int
                    523: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    524: {
                    525:        struct mdoc_node *p;
                    526:
                    527:        assert(mdoc->first);
                    528:        assert(mdoc->last);
                    529:
                    530:        p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
                    531:        if (NULL == p)
                    532:                return(0);
                    533:        return(node_append(mdoc, p));
                    534: }
                    535:
                    536:
                    537: int
                    538: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    539: {
                    540:        struct mdoc_node *p;
                    541:
                    542:        p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
                    543:        if (NULL == p)
                    544:                return(0);
                    545:        return(node_append(mdoc, p));
                    546: }
                    547:
                    548:
                    549: int
                    550: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
                    551:                int tok, struct mdoc_arg *args)
                    552: {
                    553:        struct mdoc_node *p;
                    554:
                    555:        p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
                    556:        if (NULL == p)
                    557:                return(0);
1.4       schwarze  558:        p->args = args;
                    559:        if (p->args)
1.1       kristaps  560:                (args->refcnt)++;
                    561:        return(node_append(mdoc, p));
                    562: }
                    563:
                    564:
                    565: int
                    566: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
                    567:                int tok, struct mdoc_arg *args)
                    568: {
                    569:        struct mdoc_node *p;
                    570:
                    571:        p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
                    572:        if (NULL == p)
                    573:                return(0);
1.4       schwarze  574:        p->args = args;
                    575:        if (p->args)
1.1       kristaps  576:                (args->refcnt)++;
                    577:        return(node_append(mdoc, p));
                    578: }
                    579:
                    580:
                    581: int
                    582: mdoc_word_alloc(struct mdoc *mdoc,
                    583:                int line, int pos, const char *word)
                    584: {
                    585:        struct mdoc_node *p;
                    586:
                    587:        p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
                    588:        if (NULL == p)
                    589:                return(0);
                    590:        if (NULL == (p->string = strdup(word))) {
1.8     ! schwarze  591:                (void)perr(mdoc, (mdoc)->last->line,
        !           592:                                (mdoc)->last->pos, EMALLOC);
1.1       kristaps  593:                return(0);
                    594:        }
                    595:        return(node_append(mdoc, p));
                    596: }
                    597:
                    598:
                    599: void
                    600: mdoc_node_free(struct mdoc_node *p)
                    601: {
                    602:
                    603:        if (p->string)
                    604:                free(p->string);
                    605:        if (p->args)
                    606:                mdoc_argv_free(p->args);
                    607:        free(p);
                    608: }
                    609:
                    610:
                    611: void
                    612: mdoc_node_freelist(struct mdoc_node *p)
                    613: {
                    614:
                    615:        if (p->child)
                    616:                mdoc_node_freelist(p->child);
                    617:        if (p->next)
                    618:                mdoc_node_freelist(p->next);
                    619:
                    620:        mdoc_node_free(p);
                    621: }
                    622:
                    623:
                    624: /*
                    625:  * Parse free-form text, that is, a line that does not begin with the
                    626:  * control character.
                    627:  */
                    628: static int
                    629: parsetext(struct mdoc *m, int line, char *buf)
                    630: {
                    631:
                    632:        if (SEC_PROLOGUE == m->lastnamed)
                    633:                return(perr(m, line, 0, ETEXTPROL));
                    634:
                    635:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
                    636:                return(perr(m, line, 0, ENOBLANK));
                    637:
                    638:        if ( ! mdoc_word_alloc(m, line, 0, buf))
                    639:                return(0);
                    640:
                    641:        m->next = MDOC_NEXT_SIBLING;
                    642:        return(1);
                    643: }
                    644:
                    645:
                    646: static int
                    647: macrowarn(struct mdoc *m, int ln, const char *buf)
                    648: {
                    649:        if ( ! (MDOC_IGN_MACRO & m->pflags))
                    650:                return(mdoc_perr(m, ln, 1,
                    651:                                "unknown macro: %s%s",
                    652:                                buf, strlen(buf) > 3 ? "..." : ""));
                    653:        return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
                    654:                                "unknown macro: %s%s",
                    655:                                buf, strlen(buf) > 3 ? "..." : ""));
                    656: }
                    657:
                    658:
                    659: /*
                    660:  * Parse a macro line, that is, a line beginning with the control
                    661:  * character.
                    662:  */
                    663: int
                    664: parsemacro(struct mdoc *m, int ln, char *buf)
                    665: {
                    666:        int               i, c;
                    667:        char              mac[5];
                    668:
1.7       schwarze  669:        /* Empty lines are ignored. */
1.1       kristaps  670:
                    671:        if (0 == buf[1])
                    672:                return(1);
                    673:
                    674:        if (' ' == buf[1]) {
                    675:                i = 2;
                    676:                while (buf[i] && ' ' == buf[i])
                    677:                        i++;
                    678:                if (0 == buf[i])
                    679:                        return(1);
                    680:                return(perr(m, ln, 1, ESPACE));
                    681:        }
                    682:
                    683:        /* Copy the first word into a nil-terminated buffer. */
                    684:
                    685:        for (i = 1; i < 5; i++) {
                    686:                if (0 == (mac[i - 1] = buf[i]))
                    687:                        break;
                    688:                else if (' ' == buf[i])
                    689:                        break;
                    690:        }
                    691:
                    692:        mac[i - 1] = 0;
                    693:
                    694:        if (i == 5 || i <= 2) {
                    695:                if ( ! macrowarn(m, ln, mac))
                    696:                        goto err;
                    697:                return(1);
                    698:        }
                    699:
                    700:        if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
                    701:                if ( ! macrowarn(m, ln, mac))
                    702:                        goto err;
                    703:                return(1);
                    704:        }
                    705:
                    706:        /* The macro is sane.  Jump to the next word. */
                    707:
                    708:        while (buf[i] && ' ' == buf[i])
                    709:                i++;
                    710:
                    711:        /* Begin recursive parse sequence. */
                    712:
                    713:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    714:                goto err;
                    715:
                    716:        return(1);
                    717:
                    718: err:   /* Error out. */
                    719:
                    720:        m->flags |= MDOC_HALT;
                    721:        return(0);
                    722: }