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

1.56    ! schwarze    1: /*     $Id: mdoc.c,v 1.55 2010/05/26 02:39:58 schwarze Exp $ */
1.1       kristaps    2: /*
1.56    ! schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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:  */
1.28      schwarze   17: #include <sys/types.h>
                     18:
1.1       kristaps   19: #include <assert.h>
                     20: #include <ctype.h>
                     21: #include <stdarg.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
1.39      schwarze   25: #include <time.h>
1.1       kristaps   26:
1.54      schwarze   27: #include "mandoc.h"
1.1       kristaps   28: #include "libmdoc.h"
1.32      schwarze   29: #include "libmandoc.h"
1.1       kristaps   30:
                     31: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.7       schwarze   32:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   33:        "Sh",           "Ss",           "Pp",           "D1",
                     34:        "Dl",           "Bd",           "Ed",           "Bl",
                     35:        "El",           "It",           "Ad",           "An",
                     36:        "Ar",           "Cd",           "Cm",           "Dv",
                     37:        "Er",           "Ev",           "Ex",           "Fa",
                     38:        "Fd",           "Fl",           "Fn",           "Ft",
                     39:        "Ic",           "In",           "Li",           "Nd",
                     40:        "Nm",           "Op",           "Ot",           "Pa",
                     41:        "Rv",           "St",           "Va",           "Vt",
                     42:        /* LINTED */
1.33      schwarze   43:        "Xr",           "%A",           "%B",           "%D",
1.1       kristaps   44:        /* LINTED */
1.33      schwarze   45:        "%I",           "%J",           "%N",           "%O",
1.1       kristaps   46:        /* LINTED */
1.33      schwarze   47:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps   48:        "Ac",           "Ao",           "Aq",           "At",
                     49:        "Bc",           "Bf",           "Bo",           "Bq",
                     50:        "Bsx",          "Bx",           "Db",           "Dc",
                     51:        "Do",           "Dq",           "Ec",           "Ef",
                     52:        "Em",           "Eo",           "Fx",           "Ms",
                     53:        "No",           "Ns",           "Nx",           "Ox",
                     54:        "Pc",           "Pf",           "Po",           "Pq",
                     55:        "Qc",           "Ql",           "Qo",           "Qq",
                     56:        "Re",           "Rs",           "Sc",           "So",
                     57:        "Sq",           "Sm",           "Sx",           "Sy",
                     58:        "Tn",           "Ux",           "Xc",           "Xo",
                     59:        "Fo",           "Fc",           "Oo",           "Oc",
                     60:        "Bk",           "Ek",           "Bt",           "Hf",
1.7       schwarze   61:        "Fr",           "Ud",           "Lb",           "Lp",
                     62:        "Lk",           "Mt",           "Brq",          "Bro",
1.1       kristaps   63:        /* LINTED */
1.33      schwarze   64:        "Brc",          "%C",           "Es",           "En",
1.1       kristaps   65:        /* LINTED */
1.33      schwarze   66:        "Dx",           "%Q",           "br",           "sp",
1.31      schwarze   67:        /* LINTED */
1.56    ! schwarze   68:        "%U",           "Ta"
1.1       kristaps   69:        };
                     70:
                     71: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                     72:        "split",                "nosplit",              "ragged",
                     73:        "unfilled",             "literal",              "file",
                     74:        "offset",               "bullet",               "dash",
                     75:        "hyphen",               "item",                 "enum",
                     76:        "tag",                  "diag",                 "hang",
                     77:        "ohang",                "inset",                "column",
                     78:        "width",                "compact",              "std",
                     79:        "filled",               "words",                "emphasis",
1.30      schwarze   80:        "symbolic",             "nested",               "centered"
1.1       kristaps   81:        };
                     82:
                     83: const  char * const *mdoc_macronames = __mdoc_macronames;
                     84: const  char * const *mdoc_argnames = __mdoc_argnames;
                     85:
1.40      schwarze   86: static void              mdoc_node_free(struct mdoc_node *);
                     87: static void              mdoc_node_unlink(struct mdoc *,
                     88:                                struct mdoc_node *);
1.1       kristaps   89: static void              mdoc_free1(struct mdoc *);
1.32      schwarze   90: static void              mdoc_alloc1(struct mdoc *);
1.1       kristaps   91: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.37      schwarze   92:                                enum mdoct, enum mdoc_type);
1.1       kristaps   93: static int               node_append(struct mdoc *,
                     94:                                struct mdoc_node *);
1.53      schwarze   95: static int               mdoc_ptext(struct mdoc *, int, char *, int);
                     96: static int               mdoc_pmacro(struct mdoc *, int, char *, int);
                     97: static int               macrowarn(struct mdoc *, int,
                     98:                                const char *, int);
1.46      schwarze   99:
1.1       kristaps  100:
                    101: const struct mdoc_node *
                    102: mdoc_node(const struct mdoc *m)
                    103: {
                    104:
                    105:        return(MDOC_HALT & m->flags ? NULL : m->first);
                    106: }
                    107:
                    108:
                    109: const struct mdoc_meta *
                    110: mdoc_meta(const struct mdoc *m)
                    111: {
                    112:
                    113:        return(MDOC_HALT & m->flags ? NULL : &m->meta);
                    114: }
                    115:
                    116:
1.8       schwarze  117: /*
                    118:  * Frees volatile resources (parse tree, meta-data, fields).
                    119:  */
1.1       kristaps  120: static void
                    121: mdoc_free1(struct mdoc *mdoc)
                    122: {
                    123:
                    124:        if (mdoc->first)
1.40      schwarze  125:                mdoc_node_delete(mdoc, mdoc->first);
1.1       kristaps  126:        if (mdoc->meta.title)
                    127:                free(mdoc->meta.title);
                    128:        if (mdoc->meta.os)
                    129:                free(mdoc->meta.os);
                    130:        if (mdoc->meta.name)
                    131:                free(mdoc->meta.name);
                    132:        if (mdoc->meta.arch)
                    133:                free(mdoc->meta.arch);
                    134:        if (mdoc->meta.vol)
                    135:                free(mdoc->meta.vol);
1.50      schwarze  136:        if (mdoc->meta.msec)
                    137:                free(mdoc->meta.msec);
1.1       kristaps  138: }
                    139:
                    140:
1.8       schwarze  141: /*
                    142:  * Allocate all volatile resources (parse tree, meta-data, fields).
                    143:  */
1.32      schwarze  144: static void
1.1       kristaps  145: mdoc_alloc1(struct mdoc *mdoc)
                    146: {
                    147:
1.32      schwarze  148:        memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.1       kristaps  149:        mdoc->flags = 0;
1.9       schwarze  150:        mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.32      schwarze  151:        mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.1       kristaps  152:        mdoc->first = mdoc->last;
                    153:        mdoc->last->type = MDOC_ROOT;
                    154:        mdoc->next = MDOC_NEXT_CHILD;
                    155: }
                    156:
                    157:
                    158: /*
1.8       schwarze  159:  * Free up volatile resources (see mdoc_free1()) then re-initialises the
                    160:  * data with mdoc_alloc1().  After invocation, parse data has been reset
                    161:  * and the parser is ready for re-invocation on a new tree; however,
                    162:  * cross-parse non-volatile data is kept intact.
1.1       kristaps  163:  */
1.32      schwarze  164: void
1.1       kristaps  165: mdoc_reset(struct mdoc *mdoc)
                    166: {
                    167:
                    168:        mdoc_free1(mdoc);
1.32      schwarze  169:        mdoc_alloc1(mdoc);
1.1       kristaps  170: }
                    171:
                    172:
                    173: /*
1.8       schwarze  174:  * Completely free up all volatile and non-volatile parse resources.
                    175:  * After invocation, the pointer is no longer usable.
1.1       kristaps  176:  */
                    177: void
                    178: mdoc_free(struct mdoc *mdoc)
                    179: {
                    180:
                    181:        mdoc_free1(mdoc);
                    182:        free(mdoc);
                    183: }
                    184:
                    185:
1.8       schwarze  186: /*
                    187:  * Allocate volatile and non-volatile parse resources.
                    188:  */
1.1       kristaps  189: struct mdoc *
1.54      schwarze  190: mdoc_alloc(void *data, int pflags, mandocmsg msg)
1.1       kristaps  191: {
                    192:        struct mdoc     *p;
                    193:
1.32      schwarze  194:        p = mandoc_calloc(1, sizeof(struct mdoc));
                    195:
1.54      schwarze  196:        p->msg = msg;
1.1       kristaps  197:        p->data = data;
                    198:        p->pflags = pflags;
                    199:
1.32      schwarze  200:        mdoc_hash_init();
                    201:        mdoc_alloc1(p);
                    202:        return(p);
1.1       kristaps  203: }
                    204:
                    205:
                    206: /*
                    207:  * Climb back up the parse tree, validating open scopes.  Mostly calls
1.8       schwarze  208:  * through to macro_end() in macro.c.
1.1       kristaps  209:  */
                    210: int
                    211: mdoc_endparse(struct mdoc *m)
                    212: {
                    213:
                    214:        if (MDOC_HALT & m->flags)
                    215:                return(0);
                    216:        else if (mdoc_macroend(m))
                    217:                return(1);
                    218:        m->flags |= MDOC_HALT;
                    219:        return(0);
                    220: }
                    221:
                    222:
                    223: /*
                    224:  * Main parse routine.  Parses a single line -- really just hands off to
1.45      schwarze  225:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.1       kristaps  226:  */
                    227: int
1.53      schwarze  228: mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  229: {
                    230:
                    231:        if (MDOC_HALT & m->flags)
                    232:                return(0);
                    233:
1.47      schwarze  234:        m->flags |= MDOC_NEWLINE;
1.53      schwarze  235:        return(('.' == buf[offs] || '\'' == buf[offs]) ?
                    236:                        mdoc_pmacro(m, ln, buf, offs) :
                    237:                        mdoc_ptext(m, ln, buf, offs));
1.1       kristaps  238: }
                    239:
                    240:
                    241: int
1.54      schwarze  242: mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
                    243:                int ln, int pos, const char *fmt, ...)
1.1       kristaps  244: {
                    245:        char             buf[256];
                    246:        va_list          ap;
                    247:
                    248:        va_start(ap, fmt);
1.54      schwarze  249:        vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1       kristaps  250:        va_end(ap);
1.17      schwarze  251:
1.54      schwarze  252:        return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
1.5       schwarze  253: }
                    254:
                    255:
                    256: int
1.37      schwarze  257: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.1       kristaps  258:                int ln, int pp, int *pos, char *buf)
                    259: {
1.46      schwarze  260:        assert(tok < MDOC_MAX);
                    261:
                    262:        /* If we're in the body, deny prologue calls. */
1.37      schwarze  263:
1.1       kristaps  264:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9       schwarze  265:                        MDOC_PBODY & m->flags)
1.54      schwarze  266:                return(mdoc_pmsg(m, ln, pp, MANDOCERR_BADBODY));
1.46      schwarze  267:
                    268:        /* If we're in the prologue, deny "body" macros.  */
                    269:
1.1       kristaps  270:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39      schwarze  271:                        ! (MDOC_PBODY & m->flags)) {
1.54      schwarze  272:                if ( ! mdoc_pmsg(m, ln, pp, MANDOCERR_BADPROLOG))
1.39      schwarze  273:                        return(0);
                    274:                if (NULL == m->meta.title)
1.56    ! schwarze  275:                        m->meta.title = mandoc_strdup("UNKNOWN");
1.39      schwarze  276:                if (NULL == m->meta.vol)
1.56    ! schwarze  277:                        m->meta.vol = mandoc_strdup("LOCAL");
1.39      schwarze  278:                if (NULL == m->meta.os)
1.56    ! schwarze  279:                        m->meta.os = mandoc_strdup("LOCAL");
1.39      schwarze  280:                if (0 == m->meta.date)
                    281:                        m->meta.date = time(NULL);
                    282:                m->flags |= MDOC_PBODY;
                    283:        }
1.1       kristaps  284:
                    285:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
                    286: }
                    287:
                    288:
                    289: static int
                    290: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    291: {
                    292:
                    293:        assert(mdoc->last);
                    294:        assert(mdoc->first);
                    295:        assert(MDOC_ROOT != p->type);
                    296:
                    297:        switch (mdoc->next) {
                    298:        case (MDOC_NEXT_SIBLING):
                    299:                mdoc->last->next = p;
                    300:                p->prev = mdoc->last;
                    301:                p->parent = mdoc->last->parent;
                    302:                break;
                    303:        case (MDOC_NEXT_CHILD):
                    304:                mdoc->last->child = p;
                    305:                p->parent = mdoc->last;
                    306:                break;
                    307:        default:
                    308:                abort();
                    309:                /* NOTREACHED */
                    310:        }
                    311:
1.10      schwarze  312:        p->parent->nchild++;
                    313:
1.1       kristaps  314:        if ( ! mdoc_valid_pre(mdoc, p))
                    315:                return(0);
                    316:        if ( ! mdoc_action_pre(mdoc, p))
                    317:                return(0);
                    318:
                    319:        switch (p->type) {
                    320:        case (MDOC_HEAD):
                    321:                assert(MDOC_BLOCK == p->parent->type);
                    322:                p->parent->head = p;
                    323:                break;
                    324:        case (MDOC_TAIL):
                    325:                assert(MDOC_BLOCK == p->parent->type);
                    326:                p->parent->tail = p;
                    327:                break;
                    328:        case (MDOC_BODY):
                    329:                assert(MDOC_BLOCK == p->parent->type);
                    330:                p->parent->body = p;
                    331:                break;
                    332:        default:
                    333:                break;
                    334:        }
                    335:
                    336:        mdoc->last = p;
                    337:
                    338:        switch (p->type) {
                    339:        case (MDOC_TEXT):
                    340:                if ( ! mdoc_valid_post(mdoc))
                    341:                        return(0);
                    342:                if ( ! mdoc_action_post(mdoc))
                    343:                        return(0);
                    344:                break;
                    345:        default:
                    346:                break;
                    347:        }
                    348:
                    349:        return(1);
                    350: }
                    351:
                    352:
                    353: static struct mdoc_node *
1.37      schwarze  354: node_alloc(struct mdoc *m, int line, int pos,
                    355:                enum mdoct tok, enum mdoc_type type)
1.1       kristaps  356: {
                    357:        struct mdoc_node *p;
                    358:
1.32      schwarze  359:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.19      schwarze  360:        p->sec = m->lastsec;
1.1       kristaps  361:        p->line = line;
                    362:        p->pos = pos;
                    363:        p->tok = tok;
1.37      schwarze  364:        p->type = type;
1.47      schwarze  365:        if (MDOC_NEWLINE & m->flags)
                    366:                p->flags |= MDOC_LINE;
                    367:        m->flags &= ~MDOC_NEWLINE;
1.1       kristaps  368:        return(p);
                    369: }
                    370:
                    371:
                    372: int
1.37      schwarze  373: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  374: {
                    375:        struct mdoc_node *p;
                    376:
1.19      schwarze  377:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.25      schwarze  378:        if ( ! node_append(m, p))
                    379:                return(0);
                    380:        m->next = MDOC_NEXT_CHILD;
                    381:        return(1);
1.1       kristaps  382: }
                    383:
                    384:
                    385: int
1.37      schwarze  386: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  387: {
                    388:        struct mdoc_node *p;
                    389:
1.19      schwarze  390:        assert(m->first);
                    391:        assert(m->last);
1.1       kristaps  392:
1.19      schwarze  393:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.25      schwarze  394:        if ( ! node_append(m, p))
                    395:                return(0);
                    396:        m->next = MDOC_NEXT_CHILD;
                    397:        return(1);
1.1       kristaps  398: }
                    399:
                    400:
                    401: int
1.37      schwarze  402: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  403: {
                    404:        struct mdoc_node *p;
                    405:
1.19      schwarze  406:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.25      schwarze  407:        if ( ! node_append(m, p))
                    408:                return(0);
                    409:        m->next = MDOC_NEXT_CHILD;
                    410:        return(1);
1.1       kristaps  411: }
                    412:
                    413:
                    414: int
1.19      schwarze  415: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  416:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  417: {
                    418:        struct mdoc_node *p;
                    419:
1.19      schwarze  420:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.4       schwarze  421:        p->args = args;
                    422:        if (p->args)
1.1       kristaps  423:                (args->refcnt)++;
1.25      schwarze  424:        if ( ! node_append(m, p))
                    425:                return(0);
                    426:        m->next = MDOC_NEXT_CHILD;
                    427:        return(1);
1.1       kristaps  428: }
                    429:
                    430:
                    431: int
1.19      schwarze  432: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  433:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  434: {
                    435:        struct mdoc_node *p;
                    436:
1.19      schwarze  437:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.4       schwarze  438:        p->args = args;
                    439:        if (p->args)
1.1       kristaps  440:                (args->refcnt)++;
1.25      schwarze  441:        if ( ! node_append(m, p))
                    442:                return(0);
                    443:        m->next = MDOC_NEXT_CHILD;
                    444:        return(1);
1.1       kristaps  445: }
                    446:
                    447:
1.46      schwarze  448: int
                    449: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1       kristaps  450: {
1.19      schwarze  451:        struct mdoc_node *n;
1.46      schwarze  452:        size_t            sv, len;
                    453:
                    454:        len = strlen(p);
1.1       kristaps  455:
1.46      schwarze  456:        n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.32      schwarze  457:        n->string = mandoc_malloc(len + 1);
1.19      schwarze  458:        sv = strlcpy(n->string, p, len + 1);
                    459:
                    460:        /* Prohibit truncation. */
                    461:        assert(sv < len + 1);
                    462:
1.25      schwarze  463:        if ( ! node_append(m, n))
                    464:                return(0);
1.46      schwarze  465:
1.25      schwarze  466:        m->next = MDOC_NEXT_SIBLING;
                    467:        return(1);
1.19      schwarze  468: }
                    469:
                    470:
1.1       kristaps  471: void
                    472: mdoc_node_free(struct mdoc_node *p)
                    473: {
                    474:
                    475:        if (p->string)
                    476:                free(p->string);
                    477:        if (p->args)
                    478:                mdoc_argv_free(p->args);
                    479:        free(p);
                    480: }
                    481:
                    482:
1.40      schwarze  483: static void
                    484: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    485: {
                    486:
                    487:        /* Adjust siblings. */
                    488:
                    489:        if (n->prev)
                    490:                n->prev->next = n->next;
                    491:        if (n->next)
                    492:                n->next->prev = n->prev;
                    493:
                    494:        /* Adjust parent. */
                    495:
                    496:        if (n->parent) {
                    497:                n->parent->nchild--;
                    498:                if (n->parent->child == n)
                    499:                        n->parent->child = n->prev ? n->prev : n->next;
                    500:        }
                    501:
                    502:        /* Adjust parse point, if applicable. */
                    503:
                    504:        if (m && m->last == n) {
                    505:                if (n->prev) {
                    506:                        m->last = n->prev;
                    507:                        m->next = MDOC_NEXT_SIBLING;
                    508:                } else {
                    509:                        m->last = n->parent;
                    510:                        m->next = MDOC_NEXT_CHILD;
                    511:                }
                    512:        }
                    513:
                    514:        if (m && m->first == n)
                    515:                m->first = NULL;
                    516: }
                    517:
                    518:
1.1       kristaps  519: void
1.40      schwarze  520: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  521: {
                    522:
1.40      schwarze  523:        while (p->child) {
                    524:                assert(p->nchild);
                    525:                mdoc_node_delete(m, p->child);
                    526:        }
                    527:        assert(0 == p->nchild);
1.1       kristaps  528:
1.40      schwarze  529:        mdoc_node_unlink(m, p);
1.1       kristaps  530:        mdoc_node_free(p);
                    531: }
                    532:
                    533:
                    534: /*
                    535:  * Parse free-form text, that is, a line that does not begin with the
                    536:  * control character.
                    537:  */
                    538: static int
1.53      schwarze  539: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1       kristaps  540: {
1.56    ! schwarze  541:        char             *c, *ws, *end;
        !           542:        struct mdoc_node *n;
1.44      schwarze  543:
                    544:        /* Ignore bogus comments. */
                    545:
1.53      schwarze  546:        if ('\\' == buf[offs] &&
                    547:                        '.' == buf[offs + 1] &&
                    548:                        '"' == buf[offs + 2])
1.54      schwarze  549:                return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.1       kristaps  550:
1.46      schwarze  551:        /* No text before an initial macro. */
                    552:
1.9       schwarze  553:        if (SEC_NONE == m->lastnamed)
1.54      schwarze  554:                return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.46      schwarze  555:
1.56    ! schwarze  556:        assert(m->last);
        !           557:        n = m->last;
        !           558:
        !           559:        /*
        !           560:         * Divert directly to list processing if we're encountering a
        !           561:         * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
        !           562:         * (a MDOC_BODY means it's already open, in which case we should
        !           563:         * process within its context in the normal way).
        !           564:         */
        !           565:
        !           566:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
        !           567:                        LIST_column == n->data.list) {
        !           568:                /* `Bl' is open without any children. */
        !           569:                m->flags |= MDOC_FREECOL;
        !           570:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
        !           571:        }
        !           572:
        !           573:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
        !           574:                        NULL != n->parent &&
        !           575:                        MDOC_Bl == n->parent->tok &&
        !           576:                        LIST_column == n->parent->data.list) {
        !           577:                /* `Bl' has block-level `It' children. */
        !           578:                m->flags |= MDOC_FREECOL;
        !           579:                return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
        !           580:        }
        !           581:
1.52      schwarze  582:        /*
                    583:         * Search for the beginning of unescaped trailing whitespace (ws)
                    584:         * and for the first character not to be output (end).
                    585:         */
1.56    ! schwarze  586:
        !           587:        /* FIXME: replace with strcspn(). */
1.52      schwarze  588:        ws = NULL;
1.53      schwarze  589:        for (c = end = buf + offs; *c; c++) {
1.52      schwarze  590:                switch (*c) {
1.55      schwarze  591:                case '-':
                    592:                        if (mandoc_hyph(buf + offs, c))
                    593:                                *c = ASCII_HYPH;
1.56    ! schwarze  594:                        ws = NULL;
1.55      schwarze  595:                        break;
1.52      schwarze  596:                case ' ':
                    597:                        if (NULL == ws)
                    598:                                ws = c;
                    599:                        continue;
                    600:                case '\t':
                    601:                        /*
                    602:                         * Always warn about trailing tabs,
                    603:                         * even outside literal context,
                    604:                         * where they should be put on the next line.
                    605:                         */
                    606:                        if (NULL == ws)
                    607:                                ws = c;
                    608:                        /*
                    609:                         * Strip trailing tabs in literal context only;
                    610:                         * outside, they affect the next line.
                    611:                         */
                    612:                        if (MDOC_LITERAL & m->flags)
                    613:                                continue;
                    614:                        break;
                    615:                case '\\':
                    616:                        /* Skip the escaped character, too, if any. */
                    617:                        if (c[1])
                    618:                                c++;
                    619:                        /* FALLTHROUGH */
                    620:                default:
                    621:                        ws = NULL;
                    622:                        break;
                    623:                }
                    624:                end = c + 1;
                    625:        }
                    626:        *end = '\0';
1.19      schwarze  627:
1.52      schwarze  628:        if (ws)
1.54      schwarze  629:                if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
1.52      schwarze  630:                        return(0);
1.34      schwarze  631:
1.53      schwarze  632:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.54      schwarze  633:                if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.38      schwarze  634:                        return(0);
1.46      schwarze  635:
1.40      schwarze  636:                /*
1.46      schwarze  637:                 * Insert a `Pp' in the case of a blank line.  Technically,
                    638:                 * blank lines aren't allowed, but enough manuals assume this
                    639:                 * behaviour that we want to work around it.
1.40      schwarze  640:                 */
1.53      schwarze  641:                if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
1.42      schwarze  642:                        return(0);
1.46      schwarze  643:
1.42      schwarze  644:                m->next = MDOC_NEXT_SIBLING;
                    645:                return(1);
1.38      schwarze  646:        }
1.1       kristaps  647:
1.53      schwarze  648:        if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
1.1       kristaps  649:                return(0);
                    650:
1.52      schwarze  651:        if (MDOC_LITERAL & m->flags)
                    652:                return(1);
                    653:
1.35      schwarze  654:        /*
1.48      schwarze  655:         * End-of-sentence check.  If the last character is an unescaped
                    656:         * EOS character, then flag the node as being the end of a
                    657:         * sentence.  The front-end will know how to interpret this.
1.35      schwarze  658:         */
                    659:
1.52      schwarze  660:        assert(buf < end);
1.48      schwarze  661:
1.53      schwarze  662:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.48      schwarze  663:                m->last->flags |= MDOC_EOS;
                    664:
1.1       kristaps  665:        return(1);
                    666: }
1.19      schwarze  667:
1.1       kristaps  668:
                    669: static int
1.53      schwarze  670: macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
1.1       kristaps  671: {
1.54      schwarze  672:        int              rc;
                    673:
                    674:        rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
                    675:                        "unknown macro: %s%s",
                    676:                        buf, strlen(buf) > 3 ? "..." : "");
                    677:
                    678:        /* FIXME: logic should be in driver. */
1.56    ! schwarze  679:        /* FIXME: broken, will error out and not omit a message. */
1.54      schwarze  680:        return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
1.1       kristaps  681: }
                    682:
                    683:
                    684: /*
                    685:  * Parse a macro line, that is, a line beginning with the control
                    686:  * character.
                    687:  */
                    688: int
1.53      schwarze  689: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  690: {
1.56    ! schwarze  691:        enum mdoct        tok;
        !           692:        int               i, j, sv;
        !           693:        char              mac[5];
        !           694:        struct mdoc_node *n;
1.1       kristaps  695:
1.7       schwarze  696:        /* Empty lines are ignored. */
1.1       kristaps  697:
1.53      schwarze  698:        offs++;
                    699:
                    700:        if ('\0' == buf[offs])
1.1       kristaps  701:                return(1);
                    702:
1.53      schwarze  703:        i = offs;
1.24      schwarze  704:
                    705:        /* Accept whitespace after the initial control char. */
                    706:
                    707:        if (' ' == buf[i]) {
                    708:                i++;
1.1       kristaps  709:                while (buf[i] && ' ' == buf[i])
                    710:                        i++;
1.34      schwarze  711:                if ('\0' == buf[i])
1.1       kristaps  712:                        return(1);
                    713:        }
                    714:
1.47      schwarze  715:        sv = i;
                    716:
1.1       kristaps  717:        /* Copy the first word into a nil-terminated buffer. */
                    718:
1.24      schwarze  719:        for (j = 0; j < 4; j++, i++) {
1.34      schwarze  720:                if ('\0' == (mac[j] = buf[i]))
1.1       kristaps  721:                        break;
                    722:                else if (' ' == buf[i])
                    723:                        break;
1.26      schwarze  724:
                    725:                /* Check for invalid characters. */
                    726:
                    727:                if (isgraph((u_char)buf[i]))
                    728:                        continue;
1.54      schwarze  729:                if ( ! mdoc_pmsg(m, ln, i, MANDOCERR_BADCHAR))
                    730:                        return(0);
                    731:                i--;
1.1       kristaps  732:        }
                    733:
1.53      schwarze  734:        mac[j] = '\0';
1.1       kristaps  735:
1.24      schwarze  736:        if (j == 4 || j < 2) {
1.53      schwarze  737:                if ( ! macrowarn(m, ln, mac, sv))
1.1       kristaps  738:                        goto err;
                    739:                return(1);
                    740:        }
                    741:
1.46      schwarze  742:        if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.53      schwarze  743:                if ( ! macrowarn(m, ln, mac, sv))
1.1       kristaps  744:                        goto err;
                    745:                return(1);
                    746:        }
                    747:
                    748:        /* The macro is sane.  Jump to the next word. */
                    749:
                    750:        while (buf[i] && ' ' == buf[i])
                    751:                i++;
1.34      schwarze  752:
1.46      schwarze  753:        /*
                    754:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    755:         * into the parser as "text", so we only warn about spaces here.
                    756:         */
1.34      schwarze  757:
                    758:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.54      schwarze  759:                if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.34      schwarze  760:                        goto err;
1.1       kristaps  761:
1.56    ! schwarze  762:        /*
        !           763:         * If an initial macro or a list invocation, divert directly
        !           764:         * into macro processing.
        !           765:         */
        !           766:
        !           767:        if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
        !           768:                if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
        !           769:                        goto err;
        !           770:                return(1);
        !           771:        }
        !           772:
        !           773:        n = m->last;
        !           774:        assert(m->last);
        !           775:
        !           776:        /*
        !           777:         * If the first macro of a `Bl -column', open an `It' block
        !           778:         * context around the parsed macro.
        !           779:         */
        !           780:
        !           781:        if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
        !           782:                        LIST_column == n->data.list) {
        !           783:                m->flags |= MDOC_FREECOL;
        !           784:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
        !           785:                        goto err;
        !           786:                return(1);
        !           787:        }
        !           788:
        !           789:        /*
        !           790:         * If we're following a block-level `It' within a `Bl -column'
        !           791:         * context (perhaps opened in the above block or in ptext()),
        !           792:         * then open an `It' block context around the parsed macro.
1.24      schwarze  793:         */
1.56    ! schwarze  794:
        !           795:        if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
        !           796:                        NULL != n->parent &&
        !           797:                        MDOC_Bl == n->parent->tok &&
        !           798:                        LIST_column == n->parent->data.list) {
        !           799:                m->flags |= MDOC_FREECOL;
        !           800:                if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
        !           801:                        goto err;
        !           802:                return(1);
        !           803:        }
        !           804:
        !           805:        /* Normal processing of a macro. */
        !           806:
1.47      schwarze  807:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.1       kristaps  808:                goto err;
                    809:
                    810:        return(1);
                    811:
                    812: err:   /* Error out. */
                    813:
                    814:        m->flags |= MDOC_HALT;
                    815:        return(0);
                    816: }
1.24      schwarze  817:
                    818: