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

1.55    ! schwarze    1: /*     $Id: mdoc.c,v 1.54 2010/05/23 22:45:00 schwarze Exp $ */
1.1       kristaps    2: /*
1.3       schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.3       schwarze    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.3       schwarze    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
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.48      schwarze   68:        "%U"
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)
                    275:                        m->meta.title = mandoc_strdup("unknown");
                    276:                if (NULL == m->meta.vol)
                    277:                        m->meta.vol = mandoc_strdup("local");
                    278:                if (NULL == m->meta.os)
                    279:                        m->meta.os = mandoc_strdup("local");
                    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.52      schwarze  541:        char            *c, *ws, *end;
1.44      schwarze  542:
                    543:        /* Ignore bogus comments. */
                    544:
1.53      schwarze  545:        if ('\\' == buf[offs] &&
                    546:                        '.' == buf[offs + 1] &&
                    547:                        '"' == buf[offs + 2])
1.54      schwarze  548:                return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.1       kristaps  549:
1.46      schwarze  550:        /* No text before an initial macro. */
                    551:
1.9       schwarze  552:        if (SEC_NONE == m->lastnamed)
1.54      schwarze  553:                return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.46      schwarze  554:
1.52      schwarze  555:        /*
                    556:         * Search for the beginning of unescaped trailing whitespace (ws)
                    557:         * and for the first character not to be output (end).
                    558:         */
                    559:        ws = NULL;
1.53      schwarze  560:        for (c = end = buf + offs; *c; c++) {
1.52      schwarze  561:                switch (*c) {
1.55    ! schwarze  562:                case '-':
        !           563:                        if (mandoc_hyph(buf + offs, c))
        !           564:                                *c = ASCII_HYPH;
        !           565:                        break;
1.52      schwarze  566:                case ' ':
                    567:                        if (NULL == ws)
                    568:                                ws = c;
                    569:                        continue;
                    570:                case '\t':
                    571:                        /*
                    572:                         * Always warn about trailing tabs,
                    573:                         * even outside literal context,
                    574:                         * where they should be put on the next line.
                    575:                         */
                    576:                        if (NULL == ws)
                    577:                                ws = c;
                    578:                        /*
                    579:                         * Strip trailing tabs in literal context only;
                    580:                         * outside, they affect the next line.
                    581:                         */
                    582:                        if (MDOC_LITERAL & m->flags)
                    583:                                continue;
                    584:                        break;
                    585:                case '\\':
                    586:                        /* Skip the escaped character, too, if any. */
                    587:                        if (c[1])
                    588:                                c++;
                    589:                        /* FALLTHROUGH */
                    590:                default:
                    591:                        ws = NULL;
                    592:                        break;
                    593:                }
                    594:                end = c + 1;
                    595:        }
                    596:        *end = '\0';
1.19      schwarze  597:
1.52      schwarze  598:        if (ws)
1.54      schwarze  599:                if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
1.52      schwarze  600:                        return(0);
1.34      schwarze  601:
1.53      schwarze  602:        if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.54      schwarze  603:                if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.38      schwarze  604:                        return(0);
1.46      schwarze  605:
1.40      schwarze  606:                /*
1.46      schwarze  607:                 * Insert a `Pp' in the case of a blank line.  Technically,
                    608:                 * blank lines aren't allowed, but enough manuals assume this
                    609:                 * behaviour that we want to work around it.
1.40      schwarze  610:                 */
1.53      schwarze  611:                if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
1.42      schwarze  612:                        return(0);
1.46      schwarze  613:
1.42      schwarze  614:                m->next = MDOC_NEXT_SIBLING;
                    615:                return(1);
1.38      schwarze  616:        }
1.1       kristaps  617:
1.53      schwarze  618:        if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
1.1       kristaps  619:                return(0);
                    620:
1.52      schwarze  621:        if (MDOC_LITERAL & m->flags)
                    622:                return(1);
                    623:
1.35      schwarze  624:        /*
1.48      schwarze  625:         * End-of-sentence check.  If the last character is an unescaped
                    626:         * EOS character, then flag the node as being the end of a
                    627:         * sentence.  The front-end will know how to interpret this.
1.35      schwarze  628:         */
                    629:
1.52      schwarze  630:        assert(buf < end);
1.48      schwarze  631:
1.53      schwarze  632:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.48      schwarze  633:                m->last->flags |= MDOC_EOS;
                    634:
1.1       kristaps  635:        return(1);
                    636: }
1.19      schwarze  637:
1.1       kristaps  638:
                    639: static int
1.53      schwarze  640: macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
1.1       kristaps  641: {
1.54      schwarze  642:        int              rc;
                    643:
                    644:        rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
                    645:                        "unknown macro: %s%s",
                    646:                        buf, strlen(buf) > 3 ? "..." : "");
                    647:
                    648:        /* FIXME: logic should be in driver. */
                    649:        return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
1.1       kristaps  650: }
                    651:
                    652:
                    653: /*
                    654:  * Parse a macro line, that is, a line beginning with the control
                    655:  * character.
                    656:  */
                    657: int
1.53      schwarze  658: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1       kristaps  659: {
1.46      schwarze  660:        enum mdoct      tok;
1.47      schwarze  661:        int             i, j, sv;
1.46      schwarze  662:        char            mac[5];
1.1       kristaps  663:
1.7       schwarze  664:        /* Empty lines are ignored. */
1.1       kristaps  665:
1.53      schwarze  666:        offs++;
                    667:
                    668:        if ('\0' == buf[offs])
1.1       kristaps  669:                return(1);
                    670:
1.53      schwarze  671:        i = offs;
1.24      schwarze  672:
                    673:        /* Accept whitespace after the initial control char. */
                    674:
                    675:        if (' ' == buf[i]) {
                    676:                i++;
1.1       kristaps  677:                while (buf[i] && ' ' == buf[i])
                    678:                        i++;
1.34      schwarze  679:                if ('\0' == buf[i])
1.1       kristaps  680:                        return(1);
                    681:        }
                    682:
1.47      schwarze  683:        sv = i;
                    684:
1.1       kristaps  685:        /* Copy the first word into a nil-terminated buffer. */
                    686:
1.24      schwarze  687:        for (j = 0; j < 4; j++, i++) {
1.34      schwarze  688:                if ('\0' == (mac[j] = buf[i]))
1.1       kristaps  689:                        break;
                    690:                else if (' ' == buf[i])
                    691:                        break;
1.26      schwarze  692:
                    693:                /* Check for invalid characters. */
                    694:
                    695:                if (isgraph((u_char)buf[i]))
                    696:                        continue;
1.54      schwarze  697:                if ( ! mdoc_pmsg(m, ln, i, MANDOCERR_BADCHAR))
                    698:                        return(0);
                    699:                i--;
1.1       kristaps  700:        }
                    701:
1.53      schwarze  702:        mac[j] = '\0';
1.1       kristaps  703:
1.24      schwarze  704:        if (j == 4 || j < 2) {
1.53      schwarze  705:                if ( ! macrowarn(m, ln, mac, sv))
1.1       kristaps  706:                        goto err;
                    707:                return(1);
                    708:        }
                    709:
1.46      schwarze  710:        if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.53      schwarze  711:                if ( ! macrowarn(m, ln, mac, sv))
1.1       kristaps  712:                        goto err;
                    713:                return(1);
                    714:        }
                    715:
                    716:        /* The macro is sane.  Jump to the next word. */
                    717:
                    718:        while (buf[i] && ' ' == buf[i])
                    719:                i++;
1.34      schwarze  720:
1.46      schwarze  721:        /*
                    722:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    723:         * into the parser as "text", so we only warn about spaces here.
                    724:         */
1.34      schwarze  725:
                    726:        if ('\0' == buf[i] && ' ' == buf[i - 1])
1.54      schwarze  727:                if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.34      schwarze  728:                        goto err;
1.1       kristaps  729:
1.24      schwarze  730:        /*
                    731:         * Begin recursive parse sequence.  Since we're at the start of
                    732:         * the line, we don't need to do callable/parseable checks.
                    733:         */
1.47      schwarze  734:        if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.1       kristaps  735:                goto err;
                    736:
                    737:        return(1);
                    738:
                    739: err:   /* Error out. */
                    740:
                    741:        m->flags |= MDOC_HALT;
                    742:        return(0);
                    743: }
1.24      schwarze  744:
                    745: