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

Annotation of mandoc/mdoc.c, Revision 1.69

1.69    ! kristaps    1: /* $Id: mdoc.c,v 1.68 2009/03/20 15:14:01 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.69    ! kristaps   78:        /* LINTED */
        !            79:        "En",           "Dx",           "\%Q"
1.1       kristaps   80:        };
                     81:
                     82: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                     83:        "split",                "nosplit",              "ragged",
                     84:        "unfilled",             "literal",              "file",
                     85:        "offset",               "bullet",               "dash",
                     86:        "hyphen",               "item",                 "enum",
                     87:        "tag",                  "diag",                 "hang",
                     88:        "ohang",                "inset",                "column",
                     89:        "width",                "compact",              "std",
1.52      kristaps   90:        "filled",               "words",                "emphasis",
1.64      kristaps   91:        "symbolic",             "nested"
1.1       kristaps   92:        };
                     93:
                     94: const  char * const *mdoc_macronames = __mdoc_macronames;
                     95: const  char * const *mdoc_argnames = __mdoc_argnames;
                     96:
1.45      kristaps   97:
1.68      kristaps   98: /*
                     99:  * Get the first (root) node of the parse tree.
                    100:  */
1.1       kristaps  101: const struct mdoc_node *
1.47      kristaps  102: mdoc_node(const struct mdoc *mdoc)
1.1       kristaps  103: {
                    104:
1.68      kristaps  105:        if (MDOC_HALT & mdoc->flags)
                    106:                return(NULL);
                    107:        if (mdoc->first)
                    108:                assert(MDOC_ROOT == mdoc->first->type);
1.1       kristaps  109:        return(mdoc->first);
                    110: }
                    111:
                    112:
1.37      kristaps  113: const struct mdoc_meta *
1.47      kristaps  114: mdoc_meta(const struct mdoc *mdoc)
1.37      kristaps  115: {
                    116:
1.68      kristaps  117:        if (MDOC_HALT & mdoc->flags)
                    118:                return(NULL);
1.37      kristaps  119:        return(&mdoc->meta);
                    120: }
                    121:
                    122:
1.68      kristaps  123: /*
                    124:  * Free up all resources contributed by a parse:  the node tree, meta-data and
                    125:  * so on.  Then reallocate the root node for another parse.
                    126:  */
1.1       kristaps  127: void
1.67      kristaps  128: mdoc_reset(struct mdoc *mdoc)
                    129: {
                    130:
                    131:        if (mdoc->first)
                    132:                mdoc_node_freelist(mdoc->first);
                    133:        if (mdoc->meta.title)
                    134:                free(mdoc->meta.title);
                    135:        if (mdoc->meta.os)
                    136:                free(mdoc->meta.os);
                    137:        if (mdoc->meta.name)
                    138:                free(mdoc->meta.name);
                    139:        if (mdoc->meta.arch)
                    140:                free(mdoc->meta.arch);
                    141:        if (mdoc->meta.vol)
                    142:                free(mdoc->meta.vol);
                    143:
                    144:        bzero(&mdoc->meta, sizeof(struct mdoc_meta));
                    145:        mdoc->flags = 0;
                    146:        mdoc->lastnamed = mdoc->lastsec = 0;
                    147:
                    148:        mdoc->first = mdoc->last =
                    149:                xcalloc(1, sizeof(struct mdoc_node));
                    150:        mdoc->last->type = MDOC_ROOT;
                    151:        mdoc->next = MDOC_NEXT_CHILD;
                    152: }
                    153:
                    154:
1.68      kristaps  155: /*
                    156:  * Completely free up all resources.
                    157:  */
1.67      kristaps  158: void
1.38      kristaps  159: mdoc_free(struct mdoc *mdoc)
1.34      kristaps  160: {
                    161:
1.38      kristaps  162:        if (mdoc->first)
                    163:                mdoc_node_freelist(mdoc->first);
1.34      kristaps  164:        if (mdoc->meta.title)
                    165:                free(mdoc->meta.title);
                    166:        if (mdoc->meta.os)
                    167:                free(mdoc->meta.os);
                    168:        if (mdoc->meta.name)
                    169:                free(mdoc->meta.name);
1.52      kristaps  170:        if (mdoc->meta.arch)
                    171:                free(mdoc->meta.arch);
                    172:        if (mdoc->meta.vol)
                    173:                free(mdoc->meta.vol);
1.34      kristaps  174:
1.68      kristaps  175:        if (mdoc->htab)
                    176:                mdoc_tokhash_free(mdoc->htab);
                    177:
1.1       kristaps  178:        free(mdoc);
                    179: }
                    180:
                    181:
                    182: struct mdoc *
1.55      kristaps  183: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1       kristaps  184: {
                    185:        struct mdoc     *p;
                    186:
                    187:        p = xcalloc(1, sizeof(struct mdoc));
                    188:
                    189:        p->data = data;
1.33      kristaps  190:        if (cb)
                    191:                (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1       kristaps  192:
1.67      kristaps  193:        p->last = p->first =
                    194:                xcalloc(1, sizeof(struct mdoc_node));
1.25      kristaps  195:        p->last->type = MDOC_ROOT;
1.55      kristaps  196:        p->pflags = pflags;
1.25      kristaps  197:        p->next = MDOC_NEXT_CHILD;
1.4       kristaps  198:        p->htab = mdoc_tokhash_alloc();
1.1       kristaps  199:        return(p);
                    200: }
                    201:
                    202:
1.68      kristaps  203: /*
                    204:  * Climb back up the parse tree, validating open scopes.  Mostly calls
                    205:  * through to macro_end in macro.c.
                    206:  */
1.1       kristaps  207: int
1.20      kristaps  208: mdoc_endparse(struct mdoc *mdoc)
                    209: {
                    210:
                    211:        if (MDOC_HALT & mdoc->flags)
                    212:                return(0);
                    213:        if (NULL == mdoc->first)
                    214:                return(1);
                    215:
                    216:        assert(mdoc->last);
                    217:        if ( ! macro_end(mdoc)) {
                    218:                mdoc->flags |= MDOC_HALT;
                    219:                return(0);
                    220:        }
                    221:        return(1);
                    222: }
                    223:
                    224:
1.50      kristaps  225: /*
1.53      kristaps  226:  * Main parse routine.  Parses a single line -- really just hands off to
                    227:  * the macro or text parser.
1.50      kristaps  228:  */
1.20      kristaps  229: int
1.53      kristaps  230: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1       kristaps  231: {
                    232:
1.53      kristaps  233:        /* If in error-mode, then we parse no more. */
1.50      kristaps  234:
1.53      kristaps  235:        if (MDOC_HALT & m->flags)
1.20      kristaps  236:                return(0);
1.50      kristaps  237:
1.53      kristaps  238:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    239:                        parsetext(m, ln, buf));
1.1       kristaps  240: }
                    241:
                    242:
                    243: void
1.31      kristaps  244: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1       kristaps  245: {
1.31      kristaps  246:        char              buf[256];
1.23      kristaps  247:        va_list           ap;
1.1       kristaps  248:
                    249:        if (NULL == mdoc->cb.mdoc_msg)
                    250:                return;
                    251:
                    252:        va_start(ap, fmt);
1.31      kristaps  253:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  254:        va_end(ap);
1.31      kristaps  255:        (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1       kristaps  256: }
                    257:
                    258:
                    259: int
1.31      kristaps  260: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
                    261:                const char *fmt, ...)
1.1       kristaps  262: {
1.31      kristaps  263:        char             buf[256];
                    264:        va_list          ap;
1.1       kristaps  265:
                    266:        if (NULL == mdoc->cb.mdoc_err)
                    267:                return(0);
1.31      kristaps  268:
                    269:        va_start(ap, fmt);
                    270:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    271:        va_end(ap);
                    272:        return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1       kristaps  273: }
                    274:
                    275:
                    276: int
1.31      kristaps  277: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
                    278:                enum mdoc_warn type, const char *fmt, ...)
1.1       kristaps  279: {
1.31      kristaps  280:        char             buf[256];
                    281:        va_list          ap;
1.1       kristaps  282:
                    283:        if (NULL == mdoc->cb.mdoc_warn)
                    284:                return(0);
1.31      kristaps  285:
                    286:        va_start(ap, fmt);
                    287:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    288:        va_end(ap);
                    289:        return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1       kristaps  290: }
                    291:
                    292:
                    293: int
1.53      kristaps  294: mdoc_macro(struct mdoc *m, int tok,
                    295:                int ln, int pp, int *pos, char *buf)
1.1       kristaps  296: {
                    297:
1.53      kristaps  298:        /* FIXME - these should happen during validation. */
1.31      kristaps  299:
1.44      kristaps  300:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53      kristaps  301:                        SEC_PROLOGUE != m->lastnamed)
                    302:                return(mdoc_perr(m, ln, pp,
                    303:                                "disallowed in document body"));
                    304:
1.44      kristaps  305:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.53      kristaps  306:                        SEC_PROLOGUE == m->lastnamed)
                    307:                return(mdoc_perr(m, ln, pp,
                    308:                                "disallowed in prologue"));
                    309:
                    310:        if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.64      kristaps  311:                return(mdoc_perr(m, ln, pp, "%s not callable",
                    312:                                        mdoc_macronames[tok]));
1.53      kristaps  313:
                    314:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1       kristaps  315: }
                    316:
                    317:
1.23      kristaps  318: static int
                    319: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1       kristaps  320: {
                    321:
1.25      kristaps  322:        assert(mdoc->last);
                    323:        assert(mdoc->first);
                    324:        assert(MDOC_ROOT != p->type);
1.1       kristaps  325:
1.13      kristaps  326:        switch (mdoc->next) {
                    327:        case (MDOC_NEXT_SIBLING):
1.6       kristaps  328:                mdoc->last->next = p;
                    329:                p->prev = mdoc->last;
1.13      kristaps  330:                p->parent = mdoc->last->parent;
1.1       kristaps  331:                break;
1.13      kristaps  332:        case (MDOC_NEXT_CHILD):
                    333:                mdoc->last->child = p;
1.1       kristaps  334:                p->parent = mdoc->last;
                    335:                break;
                    336:        default:
1.13      kristaps  337:                abort();
                    338:                /* NOTREACHED */
1.1       kristaps  339:        }
                    340:
1.23      kristaps  341:        if ( ! mdoc_valid_pre(mdoc, p))
                    342:                return(0);
1.68      kristaps  343:        if ( ! mdoc_action_pre(mdoc, p))
                    344:                return(0);
1.27      kristaps  345:
                    346:        switch (p->type) {
                    347:        case (MDOC_HEAD):
                    348:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  349:                p->parent->head = p;
1.27      kristaps  350:                break;
                    351:        case (MDOC_TAIL):
                    352:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  353:                p->parent->tail = p;
1.27      kristaps  354:                break;
                    355:        case (MDOC_BODY):
                    356:                assert(MDOC_BLOCK == p->parent->type);
1.53      kristaps  357:                p->parent->body = p;
1.27      kristaps  358:                break;
                    359:        default:
                    360:                break;
                    361:        }
                    362:
1.1       kristaps  363:        mdoc->last = p;
1.23      kristaps  364:        return(1);
1.1       kristaps  365: }
                    366:
                    367:
1.45      kristaps  368: static struct mdoc_node *
1.46      kristaps  369: mdoc_node_alloc(const struct mdoc *mdoc)
1.45      kristaps  370: {
1.46      kristaps  371:        struct mdoc_node *p;
                    372:
                    373:        p = xcalloc(1, sizeof(struct mdoc_node));
                    374:        p->sec = mdoc->lastsec;
1.45      kristaps  375:
1.46      kristaps  376:        return(p);
1.45      kristaps  377: }
                    378:
                    379:
1.23      kristaps  380: int
1.22      kristaps  381: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17      kristaps  382: {
                    383:        struct mdoc_node *p;
                    384:
                    385:        assert(mdoc->first);
                    386:        assert(mdoc->last);
                    387:
1.46      kristaps  388:        p = mdoc_node_alloc(mdoc);
1.17      kristaps  389:
1.22      kristaps  390:        p->line = line;
                    391:        p->pos = pos;
1.17      kristaps  392:        p->type = MDOC_TAIL;
1.27      kristaps  393:        p->tok = tok;
1.17      kristaps  394:
1.23      kristaps  395:        return(mdoc_node_append(mdoc, p));
1.17      kristaps  396: }
                    397:
                    398:
1.23      kristaps  399: int
1.22      kristaps  400: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  401: {
                    402:        struct mdoc_node *p;
                    403:
                    404:        assert(mdoc->first);
                    405:        assert(mdoc->last);
                    406:
1.46      kristaps  407:        p = mdoc_node_alloc(mdoc);
1.13      kristaps  408:
1.22      kristaps  409:        p->line = line;
                    410:        p->pos = pos;
1.1       kristaps  411:        p->type = MDOC_HEAD;
1.27      kristaps  412:        p->tok = tok;
1.1       kristaps  413:
1.23      kristaps  414:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  415: }
                    416:
                    417:
1.23      kristaps  418: int
1.22      kristaps  419: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1       kristaps  420: {
                    421:        struct mdoc_node *p;
                    422:
                    423:        assert(mdoc->first);
                    424:        assert(mdoc->last);
                    425:
1.46      kristaps  426:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  427:
1.22      kristaps  428:        p->line = line;
                    429:        p->pos = pos;
1.1       kristaps  430:        p->type = MDOC_BODY;
1.27      kristaps  431:        p->tok = tok;
1.1       kristaps  432:
1.23      kristaps  433:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  434: }
                    435:
                    436:
1.23      kristaps  437: int
1.25      kristaps  438: mdoc_root_alloc(struct mdoc *mdoc)
                    439: {
                    440:        struct mdoc_node *p;
                    441:
1.46      kristaps  442:        p = mdoc_node_alloc(mdoc);
1.25      kristaps  443:
                    444:        p->type = MDOC_ROOT;
                    445:
                    446:        return(mdoc_node_append(mdoc, p));
                    447: }
                    448:
                    449:
                    450: int
1.22      kristaps  451: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  452:                int tok, struct mdoc_arg *args)
1.1       kristaps  453: {
                    454:        struct mdoc_node *p;
                    455:
1.46      kristaps  456:        p = mdoc_node_alloc(mdoc);
1.1       kristaps  457:
1.22      kristaps  458:        p->pos = pos;
                    459:        p->line = line;
1.1       kristaps  460:        p->type = MDOC_BLOCK;
1.27      kristaps  461:        p->tok = tok;
1.53      kristaps  462:        p->args = args;
                    463:
                    464:        if (args)
                    465:                (args->refcnt)++;
1.1       kristaps  466:
1.23      kristaps  467:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  468: }
                    469:
                    470:
1.23      kristaps  471: int
1.22      kristaps  472: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53      kristaps  473:                int tok, struct mdoc_arg *args)
1.1       kristaps  474: {
                    475:        struct mdoc_node *p;
                    476:
1.46      kristaps  477:        p = mdoc_node_alloc(mdoc);
1.22      kristaps  478:
                    479:        p->line = line;
                    480:        p->pos = pos;
1.1       kristaps  481:        p->type = MDOC_ELEM;
1.27      kristaps  482:        p->tok = tok;
1.53      kristaps  483:        p->args = args;
                    484:
                    485:        if (args)
                    486:                (args->refcnt)++;
1.1       kristaps  487:
1.23      kristaps  488:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  489: }
                    490:
                    491:
1.23      kristaps  492: int
1.22      kristaps  493: mdoc_word_alloc(struct mdoc *mdoc,
                    494:                int line, int pos, const char *word)
1.1       kristaps  495: {
                    496:        struct mdoc_node *p;
                    497:
1.46      kristaps  498:        p = mdoc_node_alloc(mdoc);
1.45      kristaps  499:
1.22      kristaps  500:        p->line = line;
                    501:        p->pos = pos;
1.1       kristaps  502:        p->type = MDOC_TEXT;
1.53      kristaps  503:        p->string = xstrdup(word);
1.1       kristaps  504:
1.23      kristaps  505:        return(mdoc_node_append(mdoc, p));
1.1       kristaps  506: }
                    507:
                    508:
1.53      kristaps  509: void
                    510: mdoc_node_free(struct mdoc_node *p)
1.1       kristaps  511: {
                    512:
1.53      kristaps  513:        if (p->string)
                    514:                free(p->string);
                    515:        if (p->args)
                    516:                mdoc_argv_free(p->args);
1.1       kristaps  517:        free(p);
                    518: }
                    519:
                    520:
1.53      kristaps  521: void
                    522: mdoc_node_freelist(struct mdoc_node *p)
1.1       kristaps  523: {
                    524:
1.53      kristaps  525:        if (p->child)
                    526:                mdoc_node_freelist(p->child);
                    527:        if (p->next)
                    528:                mdoc_node_freelist(p->next);
1.1       kristaps  529:
1.53      kristaps  530:        mdoc_node_free(p);
1.1       kristaps  531: }
                    532:
                    533:
1.53      kristaps  534: /*
                    535:  * Parse free-form text, that is, a line that does not begin with the
                    536:  * control character.
                    537:  */
                    538: static int
1.68      kristaps  539: parsetext(struct mdoc *m, int line, char *buf)
1.1       kristaps  540: {
                    541:
1.68      kristaps  542:        if (SEC_PROLOGUE == m->lastnamed)
                    543:                return(mdoc_perr(m, line, 0,
1.53      kristaps  544:                        "text disallowed in prologue"));
1.1       kristaps  545:
1.68      kristaps  546:        if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
                    547:                return(mdoc_perr(m, line, 0,
                    548:                        "blank lines only in literal context"));
                    549:
                    550:        if ( ! mdoc_word_alloc(m, line, 0, buf))
1.53      kristaps  551:                return(0);
1.1       kristaps  552:
1.68      kristaps  553:        m->next = MDOC_NEXT_SIBLING;
1.53      kristaps  554:        return(1);
1.1       kristaps  555: }
                    556:
                    557:
1.58      kristaps  558: static int
                    559: macrowarn(struct mdoc *m, int ln, const char *buf)
                    560: {
                    561:        if ( ! (MDOC_IGN_MACRO & m->pflags))
                    562:                return(mdoc_perr(m, ln, 1, "unknown macro: %s%s",
1.59      kristaps  563:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  564:        return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
                    565:                                "unknown macro: %s%s",
1.59      kristaps  566:                                buf, strlen(buf) > 3 ? "..." : ""));
1.58      kristaps  567: }
                    568:
                    569:
                    570:
1.53      kristaps  571: /*
                    572:  * Parse a macro line, that is, a line beginning with the control
                    573:  * character.
                    574:  */
                    575: int
                    576: parsemacro(struct mdoc *m, int ln, char *buf)
1.1       kristaps  577: {
1.53      kristaps  578:        int               i, c;
                    579:        char              mac[5];
1.1       kristaps  580:
1.63      kristaps  581:        /* Comments and empties are quickly ignored. */
                    582:
                    583:        if (0 == buf[1])
                    584:                return(1);
                    585:
1.65      kristaps  586:        if (' ' == buf[1]) {
1.63      kristaps  587:                i = 2;
1.65      kristaps  588:                while (buf[i] && ' ' == buf[i])
1.63      kristaps  589:                        i++;
                    590:                if (0 == buf[i])
                    591:                        return(1);
                    592:                return(mdoc_perr(m, ln, 1, "invalid syntax"));
                    593:        }
1.1       kristaps  594:
1.53      kristaps  595:        if (buf[1] && '\\' == buf[1])
                    596:                if (buf[2] && '\"' == buf[2])
                    597:                        return(1);
1.1       kristaps  598:
1.53      kristaps  599:        /* Copy the first word into a nil-terminated buffer. */
1.1       kristaps  600:
1.53      kristaps  601:        for (i = 1; i < 5; i++) {
                    602:                if (0 == (mac[i - 1] = buf[i]))
                    603:                        break;
1.65      kristaps  604:                else if (' ' == buf[i])
1.53      kristaps  605:                        break;
                    606:        }
1.1       kristaps  607:
1.53      kristaps  608:        mac[i - 1] = 0;
1.1       kristaps  609:
1.53      kristaps  610:        if (i == 5 || i <= 2) {
1.58      kristaps  611:                if ( ! macrowarn(m, ln, mac))
                    612:                        goto err;
                    613:                return(1);
1.53      kristaps  614:        }
                    615:
                    616:        if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) {
1.58      kristaps  617:                if ( ! macrowarn(m, ln, mac))
                    618:                        goto err;
                    619:                return(1);
1.53      kristaps  620:        }
1.1       kristaps  621:
1.53      kristaps  622:        /* The macro is sane.  Jump to the next word. */
1.1       kristaps  623:
1.65      kristaps  624:        while (buf[i] && ' ' == buf[i])
1.53      kristaps  625:                i++;
1.1       kristaps  626:
1.53      kristaps  627:        /* Begin recursive parse sequence. */
1.1       kristaps  628:
1.53      kristaps  629:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
                    630:                goto err;
1.1       kristaps  631:
1.68      kristaps  632:        /*
                    633:         * If we're in literal mode, then add a newline to the end of
                    634:         * macro lines.  Our frontends will interpret this correctly
                    635:         * (it's documented in mdoc.3).
                    636:         */
                    637:
1.53      kristaps  638:        return(1);
1.1       kristaps  639:
1.53      kristaps  640: err:   /* Error out. */
1.1       kristaps  641:
1.53      kristaps  642:        m->flags |= MDOC_HALT;
                    643:        return(0);
1.1       kristaps  644: }

CVSweb