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

Annotation of mandoc/mdoc.c, Revision 1.68

1.68    ! kristaps    1: /* $Id: mdoc.c,v 1.67 2009/03/19 11:49:00 kristaps Exp $ */
1.1       kristaps    2: /*
1.66      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19: #include <assert.h>
                     20: #include <ctype.h>
                     21: #include <err.h>
                     22: #include <stdarg.h>
                     23: #include <stdlib.h>
                     24: #include <stdio.h>
                     25: #include <string.h>
                     26:
                     27: #include "private.h"
                     28:
1.41      kristaps   29: /*
                     30:  * Main caller in the libmdoc library.  This begins the parsing routine,
                     31:  * handles allocation of data, and so forth.  Most of the "work" is done
1.68    ! kristaps   32:  * in macro.c, validate.c and action.c.
1.41      kristaps   33:  */
                     34:
1.48      kristaps   35: static struct mdoc_node *mdoc_node_alloc(const struct mdoc *);
                     36: static int               mdoc_node_append(struct mdoc *,
                     37:                                struct mdoc_node *);
1.53      kristaps   38:
                     39: static int               parsetext(struct mdoc *, int, char *);
                     40: static int               parsemacro(struct mdoc *, int, char *);
1.58      kristaps   41: static int               macrowarn(struct mdoc *, int, const char *);
1.48      kristaps   42:
                     43:
1.1       kristaps   44: const  char *const __mdoc_macronames[MDOC_MAX] = {
                     45:        "\\\"",         "Dd",           "Dt",           "Os",
                     46:        "Sh",           "Ss",           "Pp",           "D1",
                     47:        "Dl",           "Bd",           "Ed",           "Bl",
                     48:        "El",           "It",           "Ad",           "An",
                     49:        "Ar",           "Cd",           "Cm",           "Dv",
                     50:        "Er",           "Ev",           "Ex",           "Fa",
                     51:        "Fd",           "Fl",           "Fn",           "Ft",
                     52:        "Ic",           "In",           "Li",           "Nd",
                     53:        "Nm",           "Op",           "Ot",           "Pa",
                     54:        "Rv",           "St",           "Va",           "Vt",
                     55:        /* LINTED */
                     56:        "Xr",           "\%A",          "\%B",          "\%D",
                     57:        /* LINTED */
                     58:        "\%I",          "\%J",          "\%N",          "\%O",
                     59:        /* LINTED */
                     60:        "\%P",          "\%R",          "\%T",          "\%V",
                     61:        "Ac",           "Ao",           "Aq",           "At",
                     62:        "Bc",           "Bf",           "Bo",           "Bq",
                     63:        "Bsx",          "Bx",           "Db",           "Dc",
                     64:        "Do",           "Dq",           "Ec",           "Ef",
                     65:        "Em",           "Eo",           "Fx",           "Ms",
                     66:        "No",           "Ns",           "Nx",           "Ox",
                     67:        "Pc",           "Pf",           "Po",           "Pq",
                     68:        "Qc",           "Ql",           "Qo",           "Qq",
                     69:        "Re",           "Rs",           "Sc",           "So",
                     70:        "Sq",           "Sm",           "Sx",           "Sy",
                     71:        "Tn",           "Ux",           "Xc",           "Xo",
                     72:        "Fo",           "Fc",           "Oo",           "Oc",
                     73:        "Bk",           "Ek",           "Bt",           "Hf",
1.57      kristaps   74:        "Fr",           "Ud",           "Lb",           "Ap",
1.61      kristaps   75:        "Lp",           "Lk",           "Mt",           "Brq",
1.64      kristaps   76:        /* LINTED */
1.65      kristaps   77:        "Bro",          "Brc",          "\%C",          "Es",
1.66      kristaps   78:        "En",           "Dx"
1.1       kristaps   79:        };
                     80:
                     81: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                     82:        "split",                "nosplit",              "ragged",
                     83:        "unfilled",             "literal",              "file",
                     84:        "offset",               "bullet",               "dash",
                     85:        "hyphen",               "item",                 "enum",
                     86:        "tag",                  "diag",                 "hang",
                     87:        "ohang",                "inset",                "column",
                     88:        "width",                "compact",              "std",
1.52      kristaps   89:        "filled",               "words",                "emphasis",
1.64      kristaps   90:        "symbolic",             "nested"
1.1       kristaps   91:        };
                     92:
                     93: const  char * const *mdoc_macronames = __mdoc_macronames;
                     94: const  char * const *mdoc_argnames = __mdoc_argnames;
                     95:
1.45      kristaps   96:
1.68    ! kristaps   97: /*
        !            98:  * Get the first (root) node of the parse tree.
        !            99:  */
1.1       kristaps  100: const struct mdoc_node *
1.47      kristaps  101: mdoc_node(const struct mdoc *mdoc)
1.1       kristaps  102: {
                    103:
1.68    ! kristaps  104:        if (MDOC_HALT & mdoc->flags)
        !           105:                return(NULL);
        !           106:        if (mdoc->first)
        !           107:                assert(MDOC_ROOT == mdoc->first->type);
1.1       kristaps  108:        return(mdoc->first);
                    109: }
                    110:
                    111:
1.37      kristaps  112: const struct mdoc_meta *
1.47      kristaps  113: mdoc_meta(const struct mdoc *mdoc)
1.37      kristaps  114: {
                    115:
1.68    ! kristaps  116:        if (MDOC_HALT & mdoc->flags)
        !           117:                return(NULL);
1.37      kristaps  118:        return(&mdoc->meta);
                    119: }
                    120:
                    121:
1.68    ! kristaps  122: /*
        !           123:  * Free up all resources contributed by a parse:  the node tree, meta-data and
        !           124:  * so on.  Then reallocate the root node for another parse.
        !           125:  */
1.1       kristaps  126: void
1.67      kristaps  127: mdoc_reset(struct mdoc *mdoc)
                    128: {
                    129:
                    130:        if (mdoc->first)
                    131:                mdoc_node_freelist(mdoc->first);
                    132:        if (mdoc->meta.title)
                    133:                free(mdoc->meta.title);
                    134:        if (mdoc->meta.os)
                    135:                free(mdoc->meta.os);
                    136:        if (mdoc->meta.name)
                    137:                free(mdoc->meta.name);
                    138:        if (mdoc->meta.arch)
                    139:                free(mdoc->meta.arch);
                    140:        if (mdoc->meta.vol)
                    141:                free(mdoc->meta.vol);
                    142:
                    143:        bzero(&mdoc->meta, sizeof(struct mdoc_meta));
                    144:        mdoc->flags = 0;
                    145:        mdoc->lastnamed = mdoc->lastsec = 0;
                    146:
                    147:        mdoc->first = mdoc->last =
                    148:                xcalloc(1, sizeof(struct mdoc_node));
                    149:        mdoc->last->type = MDOC_ROOT;
                    150:        mdoc->next = MDOC_NEXT_CHILD;
                    151: }
                    152:
                    153:
1.68    ! kristaps  154: /*
        !           155:  * Completely free up all resources.
        !           156:  */
1.67      kristaps  157: void
1.38      kristaps  158: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  159: {
                    160:
1.38      kristaps  161:        if (mdoc->first)
                    162:                mdoc_node_freelist(mdoc->first);
1.34      kristaps  163:        if (mdoc->meta.title)
                    164:                free(mdoc->meta.title);
                    165:        if (mdoc->meta.os)
                    166:                free(mdoc->meta.os);
                    167:        if (mdoc->meta.name)
                    168:                free(mdoc->meta.name);
1.52      kristaps  169:        if (mdoc->meta.arch)
                    170:                free(mdoc->meta.arch);
                    171:        if (mdoc->meta.vol)
                    172:                free(mdoc->meta.vol);
1.34      kristaps  173:
1.68    ! kristaps  174:        if (mdoc->htab)
        !           175:                mdoc_tokhash_free(mdoc->htab);
        !           176:
1.1       kristaps  177:        free(mdoc);
                    178: }
                    179:
                    180:
                    181: struct mdoc *
1.55      kristaps  182: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  183: {
                    184:        struct mdoc     *p;
                    185:
                    186:        p = xcalloc(1, sizeof(struct mdoc));
                    187:
                    188:        p->data = data;
1.33      kristaps  189:        if (cb)
                    190:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1       kristaps  191:
1.67      kristaps  192:        p->last = p->first =
                    193:                xcalloc(1, sizeof(struct mdoc_node));
1.25      kristaps  194:        p->last->type = MDOC_ROOT;
1.55      kristaps  195:        p->pflags = pflags;
1.25      kristaps  196:        p->next = MDOC_NEXT_CHILD;
1.4       kristaps  197:        p->htab = mdoc_tokhash_alloc();
1.1       kristaps  198:        return(p);
                    199: }
                    200:
                    201:
1.68    ! kristaps  202: /*
        !           203:  * Climb back up the parse tree, validating open scopes.  Mostly calls
        !           204:  * through to macro_end in macro.c.
        !           205:  */
1.1       kristaps  206: int
1.20      kristaps  207: mdoc_endparse(struct mdoc *mdoc)
                    208: {
                    209:
                    210:        if (MDOC_HALT & mdoc->flags)
                    211:                return(0);
                    212:        if (NULL == mdoc->first)
                    213:                return(1);
                    214:
                    215:        assert(mdoc->last);
                    216:        if ( ! macro_end(mdoc)) {
                    217:                mdoc->flags |= MDOC_HALT;
                    218:                return(0);
                    219:        }
                    220:        return(1);
                    221: }
                    222:
                    223:
1.50      kristaps  224: /*
1.53      kristaps  225:  * Main parse routine.  Parses a single line -- really just hands off to
                    226:  * the macro or text parser.
1.50      kristaps  227:  */
1.20      kristaps  228: int
1.53      kristaps  229: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  230: {
                    231:
1.53      kristaps  232:        /* If in error-mode, then we parse no more. */
1.50      kristaps  233:
1.53      kristaps  234:        if (MDOC_HALT & m->flags)
1.20      kristaps  235:                return(0);
1.50      kristaps  236:
1.53      kristaps  237:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    238:                        parsetext(m, ln, buf));
1.1       kristaps  239: }
                    240:
                    241:
                    242: void
1.31      kristaps  243: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  244: {
1.31      kristaps  245:        char              buf[256];
1.23      kristaps  246:        va_list           ap;
1.1       kristaps  247:
                    248:        if (NULL == mdoc->cb.mdoc_msg)
                    249:                return;
                    250:
                    251:        va_start(ap, fmt);
1.31      kristaps  252:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  253:        va_end(ap);
1.31      kristaps  254:        (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1       kristaps  255: }
                    256:
                    257:
                    258: int
1.31      kristaps  259: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    260:                const char *fmt, ...)
1.1       kristaps  261: {
1.31      kristaps  262:        char             buf[256];
                    263:        va_list          ap;
1.1       kristaps  264:
                    265:        if (NULL == mdoc->cb.mdoc_err)
                    266:                return(0);
1.31      kristaps  267:
                    268:        va_start(ap, fmt);
                    269:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    270:        va_end(ap);
                    271:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  272: }
                    273:
                    274:
                    275: int
1.31      kristaps  276: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
                    277:                enum mdoc_warn type, const char *fmt, ...)
1.1       kristaps  278: {
1.31      kristaps  279:        char             buf[256];
                    280:        va_list          ap;
1.1       kristaps  281:
                    282:        if (NULL == mdoc->cb.mdoc_warn)
                    283:                return(0);
1.31      kristaps  284:
                    285:        va_start(ap, fmt);
                    286:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    287:        va_end(ap);
                    288:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1       kristaps  289: }
                    290:
                    291:
                    292: int
1.53      kristaps  293: mdoc_macro(struct mdoc *m, int tok,
                    294:                int ln, int pp, int *pos, char *buf)
1.1       kristaps  295: {
                    296:
1.53      kristaps  297:        /* FIXME - these should happen during validation. */
1.31      kristaps  298:
1.44      kristaps  299:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53      kristaps  300:                        SEC_PROLOGUE != m->lastnamed)
                    301:                return(mdoc_perr(m, ln, pp,
                    302:                                "disallowed in document body"));
                    303:
1.44      kristaps  304:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.53      kristaps  305:                        SEC_PROLOGUE == m->lastnamed)
                    306:                return(mdoc_perr(m, ln, pp,
                    307:                                "disallowed in prologue"));
                    308:
                    309:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.64      kristaps  310:                return(mdoc_perr(m, ln, pp, "%s not callable",
                    311:                                        mdoc_macronames[tok]));
1.53      kristaps  312:
                    313:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1       kristaps  314: }
                    315:
                    316:
1.23      kristaps  317: static int
                    318: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  319: {
                    320:
1.25      kristaps  321:        assert(mdoc->last);
                    322:        assert(mdoc->first);
                    323:        assert(MDOC_ROOT != p->type);
1.1       kristaps  324:
1.13      kristaps  325:        switch (mdoc->next) {
                    326:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  327:                mdoc->last->next = p;
                    328:                p->prev = mdoc->last;
1.13      kristaps  329:                p->parent = mdoc->last->parent;
1.1       kristaps  330:                break;
1.13      kristaps  331:        case (MDOC_NEXT_CHILD):
                    332:                mdoc->last->child = p;
1.1       kristaps  333:                p->parent = mdoc->last;
                    334:                break;
                    335:        default:
1.13      kristaps  336:                abort();
                    337:                /* NOTREACHED */
1.1       kristaps  338:        }
                    339:
1.23      kristaps  340:        if ( ! mdoc_valid_pre(mdoc, p))
                    341:                return(0);
1.68    ! kristaps  342:        if ( ! mdoc_action_pre(mdoc, p))
        !           343:                return(0);
1.27      kristaps  344:
                    345:        switch (p->type) {
                    346:        case (MDOC_HEAD):
                    347:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  348:                p->parent->head = p;
1.27      kristaps  349:                break;
                    350:        case (MDOC_TAIL):
                    351:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  352:                p->parent->tail = p;
1.27      kristaps  353:                break;
                    354:        case (MDOC_BODY):
                    355:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  356:                p->parent->body = p;
1.27      kristaps  357:                break;
                    358:        default:
                    359:                break;
                    360:        }
                    361:
1.1       kristaps  362:        mdoc->last = p;
1.23      kristaps  363:        return(1);
1.1       kristaps  364: }
                    365:
                    366:
1.45      kristaps  367: static struct mdoc_node *
1.46      kristaps  368: mdoc_node_alloc(const struct mdoc *mdoc)
1.45      kristaps  369: {
1.46      kristaps  370:        struct mdoc_node *p;
                    371:
                    372:        p = xcalloc(1, sizeof(struct mdoc_node));
                    373:        p->sec = mdoc->lastsec;
1.45      kristaps  374:
1.46      kristaps  375:        return(p);
1.45      kristaps  376: }
                    377:
                    378:
1.23      kristaps  379: int
1.22      kristaps  380: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17      kristaps  381: {
                    382:        struct mdoc_node *p;
                    383:
                    384:        assert(mdoc->first);
                    385:        assert(mdoc->last);
                    386:
1.46      kristaps  387:        p = mdoc_node_alloc(mdoc);
1.17      kristaps  388:
1.22      kristaps  389:        p->line = line;
                    390:        p->pos = pos;
1.17      kristaps  391:        p->type = MDOC_TAIL;
1.27      kristaps  392:        p->tok = tok;
1.17      kristaps  393:
1.23      kristaps  394:        return(mdoc_node_append(mdoc, p));
1.17      kristaps  395: }
                    396:
                    397:
1.23      kristaps  398: int
1.22      kristaps  399: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  400: {
                    401:        struct mdoc_node *p;
                    402:
                    403:        assert(mdoc->first);
                    404:        assert(mdoc->last);
                    405:
1.46      kristaps  406:        p = mdoc_node_alloc(mdoc);
1.13      kristaps  407:
1.22      kristaps  408:        p->line = line;
                    409:        p->pos = pos;
1.1       kristaps  410:        p->type = MDOC_HEAD;
1.27      kristaps  411:        p->tok = tok;
1.1       kristaps  412:
1.23      kristaps  413:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  414: }
                    415:
                    416:
1.23      kristaps  417: int
1.22      kristaps  418: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  419: {
                    420:        struct mdoc_node *p;
                    421:
                    422:        assert(mdoc->first);
                    423:        assert(mdoc->last);
                    424:
1.46      kristaps  425:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  426:
1.22      kristaps  427:        p->line = line;
                    428:        p->pos = pos;
1.1       kristaps  429:        p->type = MDOC_BODY;
1.27      kristaps  430:        p->tok = tok;
1.1       kristaps  431:
1.23      kristaps  432:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  433: }
                    434:
                    435:
1.23      kristaps  436: int
1.25      kristaps  437: mdoc_root_alloc(struct mdoc *mdoc)
                    438: {
                    439:        struct mdoc_node *p;
                    440:
1.46      kristaps  441:        p = mdoc_node_alloc(mdoc);
1.25      kristaps  442:
                    443:        p->type = MDOC_ROOT;
                    444:
                    445:        return(mdoc_node_append(mdoc, p));
                    446: }
                    447:
                    448:
                    449: int
1.22      kristaps  450: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  451:                int tok, struct mdoc_arg *args)
1.1       kristaps  452: {
                    453:        struct mdoc_node *p;
                    454:
1.46      kristaps  455:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  456:
1.22      kristaps  457:        p->pos = pos;
                    458:        p->line = line;
1.1       kristaps  459:        p->type = MDOC_BLOCK;
1.27      kristaps  460:        p->tok = tok;
1.53      kristaps  461:        p->args = args;
                    462:
                    463:        if (args)
                    464:                (args->refcnt)++;
1.1       kristaps  465:
1.23      kristaps  466:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  467: }
                    468:
                    469:
1.23      kristaps  470: int
1.22      kristaps  471: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  472:                int tok, struct mdoc_arg *args)
1.1       kristaps  473: {
                    474:        struct mdoc_node *p;
                    475:
1.46      kristaps  476:        p = mdoc_node_alloc(mdoc);
1.22      kristaps  477:
                    478:        p->line = line;
                    479:        p->pos = pos;
1.1       kristaps  480:        p->type = MDOC_ELEM;
1.27      kristaps  481:        p->tok = tok;
1.53      kristaps  482:        p->args = args;
                    483:
                    484:        if (args)
                    485:                (args->refcnt)++;
1.1       kristaps  486:
1.23      kristaps  487:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  488: }
                    489:
                    490:
1.23      kristaps  491: int
1.22      kristaps  492: mdoc_word_alloc(struct mdoc *mdoc,
                    493:                int line, int pos, const char *word)
1.1       kristaps  494: {
                    495:        struct mdoc_node *p;
                    496:
1.46      kristaps  497:        p = mdoc_node_alloc(mdoc);
1.45      kristaps  498:
1.22      kristaps  499:        p->line = line;
                    500:        p->pos = pos;
1.1       kristaps  501:        p->type = MDOC_TEXT;
1.53      kristaps  502:        p->string = xstrdup(word);
1.1       kristaps  503:
1.23      kristaps  504:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  505: }
                    506:
                    507:
1.53      kristaps  508: void
                    509: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  510: {
                    511:
1.53      kristaps  512:        if (p->string)
                    513:                free(p->string);
                    514:        if (p->args)
                    515:                mdoc_argv_free(p->args);
1.1       kristaps  516:        free(p);
                    517: }
                    518:
                    519:
1.53      kristaps  520: void
                    521: mdoc_node_freelist(struct mdoc_node *p)
1.1       kristaps  522: {
                    523:
1.53      kristaps  524:        if (p->child)
                    525:                mdoc_node_freelist(p->child);
                    526:        if (p->next)
                    527:                mdoc_node_freelist(p->next);
1.1       kristaps  528:
1.53      kristaps  529:        mdoc_node_free(p);
1.1       kristaps  530: }
                    531:
                    532:
1.53      kristaps  533: /*
                    534:  * Parse free-form text, that is, a line that does not begin with the
                    535:  * control character.
                    536:  */
                    537: static int
1.68    ! kristaps  538: parsetext(struct mdoc *m, int line, char *buf)
1.1       kristaps  539: {
                    540:
1.68    ! kristaps  541:        if (SEC_PROLOGUE == m->lastnamed)
        !           542:                return(mdoc_perr(m, line, 0,
1.53      kristaps  543:                        "text disallowed in prologue"));
1.1       kristaps  544:
1.68    ! kristaps  545:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
        !           546:                return(mdoc_perr(m, line, 0,
        !           547:                        "blank lines only in literal context"));
        !           548:
        !           549:        if ( ! mdoc_word_alloc(m, line, 0, buf))
1.53      kristaps  550:                return(0);
1.1       kristaps  551:
1.68    ! kristaps  552:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  553:        return(1);
1.1       kristaps  554: }
                    555:
                    556:
1.58      kristaps  557: static int
                    558: macrowarn(struct mdoc *m, int ln, const char *buf)
                    559: {
                    560:        if ( ! (MDOC_IGN_MACRO & m->pflags))
                    561:                return(mdoc_perr(m, ln, 1, "unknown macro: %s%s",
1.59      kristaps  562:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  563:        return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
                    564:                                "unknown macro: %s%s",
1.59      kristaps  565:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  566: }
                    567:
                    568:
                    569:
1.53      kristaps  570: /*
                    571:  * Parse a macro line, that is, a line beginning with the control
                    572:  * character.
                    573:  */
                    574: int
                    575: parsemacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  576: {
1.53      kristaps  577:        int               i, c;
                    578:        char              mac[5];
1.1       kristaps  579:
1.63      kristaps  580:        /* Comments and empties are quickly ignored. */
                    581:
                    582:        if (0 == buf[1])
                    583:                return(1);
                    584:
1.65      kristaps  585:        if (' ' == buf[1]) {
1.63      kristaps  586:                i = 2;
1.65      kristaps  587:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  588:                        i++;
                    589:                if (0 == buf[i])
                    590:                        return(1);
                    591:                return(mdoc_perr(m, ln, 1, "invalid syntax"));
                    592:        }
1.1       kristaps  593:
1.53      kristaps  594:        if (buf[1] && '\\' == buf[1])
                    595:                if (buf[2] && '\"' == buf[2])
                    596:                        return(1);
1.1       kristaps  597:
1.53      kristaps  598:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  599:
1.53      kristaps  600:        for (i = 1; i < 5; i++) {
                    601:                if (0 == (mac[i - 1] = buf[i]))
                    602:                        break;
1.65      kristaps  603:                else if (' ' == buf[i])
1.53      kristaps  604:                        break;
                    605:        }
1.1       kristaps  606:
1.53      kristaps  607:        mac[i - 1] = 0;
1.1       kristaps  608:
1.53      kristaps  609:        if (i == 5 || i <= 2) {
1.58      kristaps  610:                if ( ! macrowarn(m, ln, mac))
                    611:                        goto err;
                    612:                return(1);
1.53      kristaps  613:        }
                    614:
                    615:        if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) {
1.58      kristaps  616:                if ( ! macrowarn(m, ln, mac))
                    617:                        goto err;
                    618:                return(1);
1.53      kristaps  619:        }
1.1       kristaps  620:
1.53      kristaps  621:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  622:
1.65      kristaps  623:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  624:                i++;
1.1       kristaps  625:
1.53      kristaps  626:        /* Begin recursive parse sequence. */
1.1       kristaps  627:
1.53      kristaps  628:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    629:                goto err;
1.1       kristaps  630:
1.68    ! kristaps  631:        /*
        !           632:         * If we're in literal mode, then add a newline to the end of
        !           633:         * macro lines.  Our frontends will interpret this correctly
        !           634:         * (it's documented in mdoc.3).
        !           635:         */
        !           636:
1.53      kristaps  637:        return(1);
1.1       kristaps  638:
1.53      kristaps  639: err:   /* Error out. */
1.1       kristaps  640:
1.53      kristaps  641:        m->flags |= MDOC_HALT;
                    642:        return(0);
1.1       kristaps  643: }

CVSweb