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

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