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

1.5     ! schwarze    1: /*     $Id: mdoc.c,v 1.4 2009/06/15 02:19:32 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] = {
                     37:        "\\\"",         "Dd",           "Dt",           "Os",
                     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",
                     66:        "Fr",           "Ud",           "Lb",           "Ap",
                     67:        "Lp",           "Lk",           "Mt",           "Brq",
                     68:        /* LINTED */
                     69:        "Bro",          "Brc",          "\%C",          "Es",
                     70:        /* LINTED */
                     71:        "En",           "Dx",           "\%Q"
                     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: #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
                    101:
                    102: /*
                    103:  * Get the first (root) node of the parse tree.
                    104:  */
                    105: const struct mdoc_node *
                    106: mdoc_node(const struct mdoc *m)
                    107: {
                    108:
                    109:        return(MDOC_HALT & m->flags ? NULL : m->first);
                    110: }
                    111:
                    112:
                    113: const struct mdoc_meta *
                    114: mdoc_meta(const struct mdoc *m)
                    115: {
                    116:
                    117:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
                    118: }
                    119:
                    120:
                    121: static void
                    122: mdoc_free1(struct mdoc *mdoc)
                    123: {
                    124:
                    125:        if (mdoc->first)
                    126:                mdoc_node_freelist(mdoc->first);
                    127:        if (mdoc->meta.title)
                    128:                free(mdoc->meta.title);
                    129:        if (mdoc->meta.os)
                    130:                free(mdoc->meta.os);
                    131:        if (mdoc->meta.name)
                    132:                free(mdoc->meta.name);
                    133:        if (mdoc->meta.arch)
                    134:                free(mdoc->meta.arch);
                    135:        if (mdoc->meta.vol)
                    136:                free(mdoc->meta.vol);
                    137: }
                    138:
                    139:
                    140: static int
                    141: mdoc_alloc1(struct mdoc *mdoc)
                    142: {
                    143:
                    144:        bzero(&mdoc->meta, sizeof(struct mdoc_meta));
                    145:        mdoc->flags = 0;
                    146:        mdoc->lastnamed = mdoc->lastsec = 0;
                    147:        mdoc->last = calloc(1, sizeof(struct mdoc_node));
                    148:        if (NULL == mdoc->last)
                    149:                return(0);
                    150:
                    151:        mdoc->first = mdoc->last;
                    152:        mdoc->last->type = MDOC_ROOT;
                    153:        mdoc->next = MDOC_NEXT_CHILD;
                    154:        return(1);
                    155: }
                    156:
                    157:
                    158: /*
                    159:  * Free up all resources contributed by a parse:  the node tree,
                    160:  * meta-data and so on.  Then reallocate the root node for another
                    161:  * parse.
                    162:  */
                    163: int
                    164: mdoc_reset(struct mdoc *mdoc)
                    165: {
                    166:
                    167:        mdoc_free1(mdoc);
                    168:        return(mdoc_alloc1(mdoc));
                    169: }
                    170:
                    171:
                    172: /*
                    173:  * Completely free up all resources.
                    174:  */
                    175: void
                    176: mdoc_free(struct mdoc *mdoc)
                    177: {
                    178:
                    179:        mdoc_free1(mdoc);
                    180:        if (mdoc->htab)
                    181:                mdoc_hash_free(mdoc->htab);
                    182:        free(mdoc);
                    183: }
                    184:
                    185:
                    186: struct mdoc *
                    187: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
                    188: {
                    189:        struct mdoc     *p;
                    190:
                    191:        if (NULL == (p = calloc(1, sizeof(struct mdoc))))
                    192:                return(NULL);
                    193:        if (cb)
                    194:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
                    195:
                    196:        p->data = data;
                    197:        p->pflags = pflags;
                    198:
                    199:        if (NULL == (p->htab = mdoc_hash_alloc())) {
                    200:                free(p);
                    201:                return(NULL);
                    202:        } else if (mdoc_alloc1(p))
                    203:                return(p);
                    204:
                    205:        free(p);
                    206:        return(NULL);
                    207: }
                    208:
                    209:
                    210: /*
                    211:  * Climb back up the parse tree, validating open scopes.  Mostly calls
                    212:  * through to macro_end in macro.c.
                    213:  */
                    214: int
                    215: mdoc_endparse(struct mdoc *m)
                    216: {
                    217:
                    218:        if (MDOC_HALT & m->flags)
                    219:                return(0);
                    220:        else if (mdoc_macroend(m))
                    221:                return(1);
                    222:        m->flags |= MDOC_HALT;
                    223:        return(0);
                    224: }
                    225:
                    226:
                    227: /*
                    228:  * Main parse routine.  Parses a single line -- really just hands off to
                    229:  * the macro or text parser.
                    230:  */
                    231: int
                    232: mdoc_parseln(struct mdoc *m, int ln, char *buf)
                    233: {
                    234:
                    235:        /* If in error-mode, then we parse no more. */
                    236:
                    237:        if (MDOC_HALT & m->flags)
                    238:                return(0);
                    239:
                    240:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    241:                        parsetext(m, ln, buf));
                    242: }
                    243:
                    244:
                    245: void
                    246: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
                    247: {
                    248:        char              buf[256];
                    249:        va_list           ap;
                    250:
                    251:        if (NULL == mdoc->cb.mdoc_msg)
                    252:                return;
                    253:
                    254:        va_start(ap, fmt);
                    255:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    256:        va_end(ap);
                    257:        (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
                    258: }
                    259:
                    260:
                    261: int
1.4       schwarze  262: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    263:                const char *fmt, ...)
1.1       kristaps  264: {
                    265:        char             buf[256];
                    266:        va_list          ap;
                    267:
                    268:        if (NULL == mdoc->cb.mdoc_err)
                    269:                return(0);
                    270:
                    271:        va_start(ap, fmt);
                    272:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    273:        va_end(ap);
                    274:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
                    275: }
                    276:
                    277:
                    278: int
                    279: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
                    280:                enum mdoc_warn type, const char *fmt, ...)
                    281: {
                    282:        char             buf[256];
                    283:        va_list          ap;
                    284:
                    285:        if (NULL == mdoc->cb.mdoc_warn)
                    286:                return(0);
                    287:
                    288:        va_start(ap, fmt);
                    289:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    290:        va_end(ap);
                    291:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.5     ! schwarze  292: }
        !           293:
        !           294:
        !           295: int
        !           296: mdoc_nwarn(struct mdoc *mdoc, const struct mdoc_node *node, enum mdoc_warn type,
        !           297:                const char *fmt, ...)
        !           298: {
        !           299:        char             buf[256];
        !           300:        va_list          ap;
        !           301:
        !           302:        if (NULL == mdoc->cb.mdoc_warn)
        !           303:                return(0);
        !           304:
        !           305:        va_start(ap, fmt);
        !           306:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           307:        va_end(ap);
        !           308:        return((*mdoc->cb.mdoc_warn)(mdoc->data, node->line, node->pos, type,
        !           309:            buf));
        !           310: }
        !           311:
        !           312: int
        !           313: mdoc_nerr(struct mdoc *mdoc, const struct mdoc_node *node, const char *fmt, ...)
        !           314: {
        !           315:        char             buf[256];
        !           316:        va_list          ap;
        !           317:
        !           318:        if (NULL == mdoc->cb.mdoc_err)
        !           319:                return(0);
        !           320:
        !           321:        va_start(ap, fmt);
        !           322:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           323:        va_end(ap);
        !           324:        return((*mdoc->cb.mdoc_err)(mdoc->data, node->line, node->pos, buf));
        !           325: }
        !           326:
        !           327:
        !           328: int
        !           329: mdoc_warn(struct mdoc *mdoc, enum mdoc_warn type, const char *fmt, ...)
        !           330: {
        !           331:        char             buf[256];
        !           332:        va_list          ap;
        !           333:
        !           334:        if (NULL == mdoc->cb.mdoc_warn)
        !           335:                return(0);
        !           336:
        !           337:        va_start(ap, fmt);
        !           338:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           339:        va_end(ap);
        !           340:        return((*mdoc->cb.mdoc_warn)(mdoc->data, mdoc->last->line,
        !           341:            mdoc->last->pos, type, buf));
        !           342: }
        !           343:
        !           344:
        !           345: int
        !           346: mdoc_err(struct mdoc *mdoc, const char *fmt, ...)
        !           347: {
        !           348:        char             buf[256];
        !           349:        va_list          ap;
        !           350:
        !           351:        if (NULL == mdoc->cb.mdoc_err)
        !           352:                return(0);
        !           353:
        !           354:        va_start(ap, fmt);
        !           355:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           356:        va_end(ap);
        !           357:        return((*mdoc->cb.mdoc_err)(mdoc->data, mdoc->last->line,
        !           358:            mdoc->last->pos, buf));
        !           359: }
        !           360:
        !           361:
        !           362: void
        !           363: mdoc_msg(struct mdoc *mdoc, const char *fmt, ...)
        !           364: {
        !           365:        char              buf[256];
        !           366:        va_list           ap;
        !           367:
        !           368:        if (NULL == mdoc->cb.mdoc_msg)
        !           369:                return;
        !           370:
        !           371:        va_start(ap, fmt);
        !           372:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           373:        va_end(ap);
        !           374:        (*mdoc->cb.mdoc_msg)(mdoc->data, mdoc->last->line, mdoc->last->pos,
        !           375:            buf);
        !           376: }
        !           377:
        !           378:
        !           379: void
        !           380: mdoc_pmsg(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
        !           381: {
        !           382:        char              buf[256];
        !           383:        va_list           ap;
        !           384:
        !           385:        if (NULL == mdoc->cb.mdoc_msg)
        !           386:                return;
        !           387:
        !           388:        va_start(ap, fmt);
        !           389:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           390:        va_end(ap);
        !           391:        (*mdoc->cb.mdoc_msg)(mdoc->data, line, pos, buf);
        !           392: }
        !           393:
        !           394:
        !           395: int
        !           396: mdoc_pwarn(struct mdoc *mdoc, int line, int pos, enum mdoc_warn type,
        !           397:                const char *fmt, ...)
        !           398: {
        !           399:        char             buf[256];
        !           400:        va_list          ap;
        !           401:
        !           402:        if (NULL == mdoc->cb.mdoc_warn)
        !           403:                return(0);
        !           404:
        !           405:        va_start(ap, fmt);
        !           406:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           407:        va_end(ap);
        !           408:        return((*mdoc->cb.mdoc_warn)(mdoc->data, line, pos, type, buf));
        !           409: }
        !           410:
        !           411: int
        !           412: mdoc_perr(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
        !           413: {
        !           414:        char             buf[256];
        !           415:        va_list          ap;
        !           416:
        !           417:        if (NULL == mdoc->cb.mdoc_err)
        !           418:                return(0);
        !           419:
        !           420:        va_start(ap, fmt);
        !           421:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
        !           422:        va_end(ap);
        !           423:        return((*mdoc->cb.mdoc_err)(mdoc->data, line, pos, buf));
1.2       miod      424: }
                    425:
                    426:
                    427: int
1.1       kristaps  428: mdoc_macro(struct mdoc *m, int tok,
                    429:                int ln, int pp, int *pos, char *buf)
                    430: {
                    431:
                    432:        /* FIXME - these should happen during validation. */
                    433:
                    434:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
                    435:                        SEC_PROLOGUE != m->lastnamed)
                    436:                return(perr(m, ln, pp, EPROLBODY));
                    437:
                    438:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
                    439:                        SEC_PROLOGUE == m->lastnamed)
                    440:                return(perr(m, ln, pp, EBODYPROL));
                    441:
                    442:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
                    443:                return(perr(m, ln, pp, ENOCALL));
                    444:
                    445:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
                    446: }
                    447:
                    448:
                    449: static int
                    450: perr(struct mdoc *m, int line, int pos, enum merr type)
                    451: {
                    452:        char            *p;
                    453:
                    454:        p = NULL;
                    455:        switch (type) {
                    456:        case (ENOCALL):
                    457:                p = "not callable";
                    458:                break;
                    459:        case (EPROLBODY):
                    460:                p = "macro disallowed in document body";
                    461:                break;
                    462:        case (EBODYPROL):
                    463:                p = "macro disallowed in document prologue";
                    464:                break;
                    465:        case (EMALLOC):
                    466:                p = "memory exhausted";
                    467:                break;
                    468:        case (ETEXTPROL):
                    469:                p = "text disallowed in document prologue";
                    470:                break;
                    471:        case (ENOBLANK):
                    472:                p = "blank lines disallowed in non-literal contexts";
                    473:                break;
                    474:        case (ESPACE):
                    475:                p = "whitespace disallowed after delimiter";
                    476:                break;
                    477:        }
                    478:        assert(p);
                    479:        return(mdoc_perr(m, line, pos, p));
                    480: }
                    481:
                    482:
                    483: static int
                    484: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    485: {
                    486:
                    487:        assert(mdoc->last);
                    488:        assert(mdoc->first);
                    489:        assert(MDOC_ROOT != p->type);
                    490:
                    491:        switch (mdoc->next) {
                    492:        case (MDOC_NEXT_SIBLING):
                    493:                mdoc->last->next = p;
                    494:                p->prev = mdoc->last;
                    495:                p->parent = mdoc->last->parent;
                    496:                break;
                    497:        case (MDOC_NEXT_CHILD):
                    498:                mdoc->last->child = p;
                    499:                p->parent = mdoc->last;
                    500:                break;
                    501:        default:
                    502:                abort();
                    503:                /* NOTREACHED */
                    504:        }
                    505:
                    506:        if ( ! mdoc_valid_pre(mdoc, p))
                    507:                return(0);
                    508:        if ( ! mdoc_action_pre(mdoc, p))
                    509:                return(0);
                    510:
                    511:        switch (p->type) {
                    512:        case (MDOC_HEAD):
                    513:                assert(MDOC_BLOCK == p->parent->type);
                    514:                p->parent->head = p;
                    515:                break;
                    516:        case (MDOC_TAIL):
                    517:                assert(MDOC_BLOCK == p->parent->type);
                    518:                p->parent->tail = p;
                    519:                break;
                    520:        case (MDOC_BODY):
                    521:                assert(MDOC_BLOCK == p->parent->type);
                    522:                p->parent->body = p;
                    523:                break;
                    524:        default:
                    525:                break;
                    526:        }
                    527:
                    528:        mdoc->last = p;
                    529:
                    530:        switch (p->type) {
                    531:        case (MDOC_TEXT):
                    532:                if ( ! mdoc_valid_post(mdoc))
                    533:                        return(0);
                    534:                if ( ! mdoc_action_post(mdoc))
                    535:                        return(0);
                    536:                break;
                    537:        default:
                    538:                break;
                    539:        }
                    540:
                    541:        return(1);
                    542: }
                    543:
                    544:
                    545: static struct mdoc_node *
                    546: node_alloc(struct mdoc *mdoc, int line,
                    547:                int pos, int tok, enum mdoc_type type)
                    548: {
                    549:        struct mdoc_node *p;
                    550:
                    551:        if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
                    552:                (void)verr(mdoc, EMALLOC);
                    553:                return(NULL);
                    554:        }
                    555:
                    556:        p->sec = mdoc->lastsec;
                    557:        p->line = line;
                    558:        p->pos = pos;
                    559:        p->tok = tok;
                    560:        if (MDOC_TEXT != (p->type = type))
                    561:                assert(p->tok >= 0);
                    562:
                    563:        return(p);
                    564: }
                    565:
                    566:
                    567: int
                    568: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    569: {
                    570:        struct mdoc_node *p;
                    571:
                    572:        p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
                    573:        if (NULL == p)
                    574:                return(0);
                    575:        return(node_append(mdoc, p));
                    576: }
                    577:
                    578:
                    579: int
                    580: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    581: {
                    582:        struct mdoc_node *p;
                    583:
                    584:        assert(mdoc->first);
                    585:        assert(mdoc->last);
                    586:
                    587:        p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
                    588:        if (NULL == p)
                    589:                return(0);
                    590:        return(node_append(mdoc, p));
                    591: }
                    592:
                    593:
                    594: int
                    595: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
                    596: {
                    597:        struct mdoc_node *p;
                    598:
                    599:        p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
                    600:        if (NULL == p)
                    601:                return(0);
                    602:        return(node_append(mdoc, p));
                    603: }
                    604:
                    605:
                    606: int
                    607: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
                    608:                int tok, struct mdoc_arg *args)
                    609: {
                    610:        struct mdoc_node *p;
                    611:
                    612:        p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
                    613:        if (NULL == p)
                    614:                return(0);
1.4       schwarze  615:        p->args = args;
                    616:        if (p->args)
1.1       kristaps  617:                (args->refcnt)++;
                    618:        return(node_append(mdoc, p));
                    619: }
                    620:
                    621:
                    622: int
                    623: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
                    624:                int tok, struct mdoc_arg *args)
                    625: {
                    626:        struct mdoc_node *p;
                    627:
                    628:        p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
                    629:        if (NULL == p)
                    630:                return(0);
1.4       schwarze  631:        p->args = args;
                    632:        if (p->args)
1.1       kristaps  633:                (args->refcnt)++;
                    634:        return(node_append(mdoc, p));
                    635: }
                    636:
                    637:
                    638: int
                    639: mdoc_word_alloc(struct mdoc *mdoc,
                    640:                int line, int pos, const char *word)
                    641: {
                    642:        struct mdoc_node *p;
                    643:
                    644:        p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
                    645:        if (NULL == p)
                    646:                return(0);
                    647:        if (NULL == (p->string = strdup(word))) {
                    648:                (void)verr(mdoc, EMALLOC);
                    649:                return(0);
                    650:        }
                    651:        return(node_append(mdoc, p));
                    652: }
                    653:
                    654:
                    655: void
                    656: mdoc_node_free(struct mdoc_node *p)
                    657: {
                    658:
                    659:        if (p->string)
                    660:                free(p->string);
                    661:        if (p->args)
                    662:                mdoc_argv_free(p->args);
                    663:        free(p);
                    664: }
                    665:
                    666:
                    667: void
                    668: mdoc_node_freelist(struct mdoc_node *p)
                    669: {
                    670:
                    671:        if (p->child)
                    672:                mdoc_node_freelist(p->child);
                    673:        if (p->next)
                    674:                mdoc_node_freelist(p->next);
                    675:
                    676:        mdoc_node_free(p);
                    677: }
                    678:
                    679:
                    680: /*
                    681:  * Parse free-form text, that is, a line that does not begin with the
                    682:  * control character.
                    683:  */
                    684: static int
                    685: parsetext(struct mdoc *m, int line, char *buf)
                    686: {
                    687:
                    688:        if (SEC_PROLOGUE == m->lastnamed)
                    689:                return(perr(m, line, 0, ETEXTPROL));
                    690:
                    691:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
                    692:                return(perr(m, line, 0, ENOBLANK));
                    693:
                    694:        if ( ! mdoc_word_alloc(m, line, 0, buf))
                    695:                return(0);
                    696:
                    697:        m->next = MDOC_NEXT_SIBLING;
                    698:        return(1);
                    699: }
                    700:
                    701:
                    702: static int
                    703: macrowarn(struct mdoc *m, int ln, const char *buf)
                    704: {
                    705:        if ( ! (MDOC_IGN_MACRO & m->pflags))
                    706:                return(mdoc_perr(m, ln, 1,
                    707:                                "unknown macro: %s%s",
                    708:                                buf, strlen(buf) > 3 ? "..." : ""));
                    709:        return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
                    710:                                "unknown macro: %s%s",
                    711:                                buf, strlen(buf) > 3 ? "..." : ""));
                    712: }
                    713:
                    714:
                    715:
                    716: /*
                    717:  * Parse a macro line, that is, a line beginning with the control
                    718:  * character.
                    719:  */
                    720: int
                    721: parsemacro(struct mdoc *m, int ln, char *buf)
                    722: {
                    723:        int               i, c;
                    724:        char              mac[5];
                    725:
                    726:        /* Comments and empties are quickly ignored. */
                    727:
                    728:        if (0 == buf[1])
                    729:                return(1);
                    730:
                    731:        if (' ' == buf[1]) {
                    732:                i = 2;
                    733:                while (buf[i] && ' ' == buf[i])
                    734:                        i++;
                    735:                if (0 == buf[i])
                    736:                        return(1);
                    737:                return(perr(m, ln, 1, ESPACE));
                    738:        }
                    739:
                    740:        if (buf[1] && '\\' == buf[1])
                    741:                if (buf[2] && '\"' == buf[2])
                    742:                        return(1);
                    743:
                    744:        /* Copy the first word into a nil-terminated buffer. */
                    745:
                    746:        for (i = 1; i < 5; i++) {
                    747:                if (0 == (mac[i - 1] = buf[i]))
                    748:                        break;
                    749:                else if (' ' == buf[i])
                    750:                        break;
                    751:        }
                    752:
                    753:        mac[i - 1] = 0;
                    754:
                    755:        if (i == 5 || i <= 2) {
                    756:                if ( ! macrowarn(m, ln, mac))
                    757:                        goto err;
                    758:                return(1);
                    759:        }
                    760:
                    761:        if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
                    762:                if ( ! macrowarn(m, ln, mac))
                    763:                        goto err;
                    764:                return(1);
                    765:        }
                    766:
                    767:        /* The macro is sane.  Jump to the next word. */
                    768:
                    769:        while (buf[i] && ' ' == buf[i])
                    770:                i++;
                    771:
                    772:        /* Begin recursive parse sequence. */
                    773:
                    774:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    775:                goto err;
                    776:
                    777:        return(1);
                    778:
                    779: err:   /* Error out. */
                    780:
                    781:        m->flags |= MDOC_HALT;
                    782:        return(0);
                    783: }