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

1.41    ! schwarze    1: /*     $Id: mdoc.c,v 1.40 2010/04/07 23:15:05 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.1       kristaps   83: };
                     84:
                     85: const  char *const __mdoc_macronames[MDOC_MAX] = {
1.7       schwarze   86:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   87:        "Sh",           "Ss",           "Pp",           "D1",
                     88:        "Dl",           "Bd",           "Ed",           "Bl",
                     89:        "El",           "It",           "Ad",           "An",
                     90:        "Ar",           "Cd",           "Cm",           "Dv",
                     91:        "Er",           "Ev",           "Ex",           "Fa",
                     92:        "Fd",           "Fl",           "Fn",           "Ft",
                     93:        "Ic",           "In",           "Li",           "Nd",
                     94:        "Nm",           "Op",           "Ot",           "Pa",
                     95:        "Rv",           "St",           "Va",           "Vt",
                     96:        /* LINTED */
1.33      schwarze   97:        "Xr",           "%A",           "%B",           "%D",
1.1       kristaps   98:        /* LINTED */
1.33      schwarze   99:        "%I",           "%J",           "%N",           "%O",
1.1       kristaps  100:        /* LINTED */
1.33      schwarze  101:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps  102:        "Ac",           "Ao",           "Aq",           "At",
                    103:        "Bc",           "Bf",           "Bo",           "Bq",
                    104:        "Bsx",          "Bx",           "Db",           "Dc",
                    105:        "Do",           "Dq",           "Ec",           "Ef",
                    106:        "Em",           "Eo",           "Fx",           "Ms",
                    107:        "No",           "Ns",           "Nx",           "Ox",
                    108:        "Pc",           "Pf",           "Po",           "Pq",
                    109:        "Qc",           "Ql",           "Qo",           "Qq",
                    110:        "Re",           "Rs",           "Sc",           "So",
                    111:        "Sq",           "Sm",           "Sx",           "Sy",
                    112:        "Tn",           "Ux",           "Xc",           "Xo",
                    113:        "Fo",           "Fc",           "Oo",           "Oc",
                    114:        "Bk",           "Ek",           "Bt",           "Hf",
1.7       schwarze  115:        "Fr",           "Ud",           "Lb",           "Lp",
                    116:        "Lk",           "Mt",           "Brq",          "Bro",
1.1       kristaps  117:        /* LINTED */
1.33      schwarze  118:        "Brc",          "%C",           "Es",           "En",
1.1       kristaps  119:        /* LINTED */
1.33      schwarze  120:        "Dx",           "%Q",           "br",           "sp",
1.31      schwarze  121:        /* LINTED */
1.35      schwarze  122:        "%U",           "eos"
1.1       kristaps  123:        };
                    124:
                    125: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
                    126:        "split",                "nosplit",              "ragged",
                    127:        "unfilled",             "literal",              "file",
                    128:        "offset",               "bullet",               "dash",
                    129:        "hyphen",               "item",                 "enum",
                    130:        "tag",                  "diag",                 "hang",
                    131:        "ohang",                "inset",                "column",
                    132:        "width",                "compact",              "std",
                    133:        "filled",               "words",                "emphasis",
1.30      schwarze  134:        "symbolic",             "nested",               "centered"
1.1       kristaps  135:        };
                    136:
                    137: const  char * const *mdoc_macronames = __mdoc_macronames;
                    138: const  char * const *mdoc_argnames = __mdoc_argnames;
                    139:
1.40      schwarze  140: static void              mdoc_node_free(struct mdoc_node *);
                    141: static void              mdoc_node_unlink(struct mdoc *,
                    142:                                struct mdoc_node *);
1.1       kristaps  143: static void              mdoc_free1(struct mdoc *);
1.32      schwarze  144: static void              mdoc_alloc1(struct mdoc *);
1.1       kristaps  145: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.37      schwarze  146:                                enum mdoct, enum mdoc_type);
1.1       kristaps  147: static int               node_append(struct mdoc *,
                    148:                                struct mdoc_node *);
                    149: static int               parsetext(struct mdoc *, int, char *);
                    150: static int               parsemacro(struct mdoc *, int, char *);
                    151: static int               macrowarn(struct mdoc *, int, const char *);
1.19      schwarze  152: static int               pstring(struct mdoc *, int, int,
                    153:                                const char *, size_t);
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.8       schwarze  279:  * the macro (parsemacro()) or text parser (parsetext()).
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:
                    288:        return('.' == *buf ? parsemacro(m, ln, buf) :
                    289:                        parsetext(m, ln, buf));
                    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.37      schwarze  347:
                    348:        assert(tok < MDOC_MAX);
1.24      schwarze  349:        /*
                    350:         * If we're in the prologue, deny "body" macros.  Similarly, if
                    351:         * we're in the body, deny prologue calls.
                    352:         */
1.1       kristaps  353:        if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9       schwarze  354:                        MDOC_PBODY & m->flags)
1.16      schwarze  355:                return(mdoc_perr(m, ln, pp, EPROLBODY));
1.1       kristaps  356:        if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39      schwarze  357:                        ! (MDOC_PBODY & m->flags)) {
                    358:                if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
                    359:                        return(0);
                    360:                if (NULL == m->meta.title)
                    361:                        m->meta.title = mandoc_strdup("unknown");
                    362:                if (NULL == m->meta.vol)
                    363:                        m->meta.vol = mandoc_strdup("local");
                    364:                if (NULL == m->meta.os)
                    365:                        m->meta.os = mandoc_strdup("local");
                    366:                if (0 == m->meta.date)
                    367:                        m->meta.date = time(NULL);
                    368:                m->flags |= MDOC_PBODY;
                    369:        }
1.1       kristaps  370:
                    371:        return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
                    372: }
                    373:
                    374:
                    375: static int
                    376: node_append(struct mdoc *mdoc, struct mdoc_node *p)
                    377: {
                    378:
                    379:        assert(mdoc->last);
                    380:        assert(mdoc->first);
                    381:        assert(MDOC_ROOT != p->type);
                    382:
                    383:        switch (mdoc->next) {
                    384:        case (MDOC_NEXT_SIBLING):
                    385:                mdoc->last->next = p;
                    386:                p->prev = mdoc->last;
                    387:                p->parent = mdoc->last->parent;
                    388:                break;
                    389:        case (MDOC_NEXT_CHILD):
                    390:                mdoc->last->child = p;
                    391:                p->parent = mdoc->last;
                    392:                break;
                    393:        default:
                    394:                abort();
                    395:                /* NOTREACHED */
                    396:        }
                    397:
1.10      schwarze  398:        p->parent->nchild++;
                    399:
1.1       kristaps  400:        if ( ! mdoc_valid_pre(mdoc, p))
                    401:                return(0);
                    402:        if ( ! mdoc_action_pre(mdoc, p))
                    403:                return(0);
                    404:
                    405:        switch (p->type) {
                    406:        case (MDOC_HEAD):
                    407:                assert(MDOC_BLOCK == p->parent->type);
                    408:                p->parent->head = p;
                    409:                break;
                    410:        case (MDOC_TAIL):
                    411:                assert(MDOC_BLOCK == p->parent->type);
                    412:                p->parent->tail = p;
                    413:                break;
                    414:        case (MDOC_BODY):
                    415:                assert(MDOC_BLOCK == p->parent->type);
                    416:                p->parent->body = p;
                    417:                break;
                    418:        default:
                    419:                break;
                    420:        }
                    421:
                    422:        mdoc->last = p;
                    423:
                    424:        switch (p->type) {
                    425:        case (MDOC_TEXT):
                    426:                if ( ! mdoc_valid_post(mdoc))
                    427:                        return(0);
                    428:                if ( ! mdoc_action_post(mdoc))
                    429:                        return(0);
                    430:                break;
                    431:        default:
                    432:                break;
                    433:        }
                    434:
                    435:        return(1);
                    436: }
                    437:
                    438:
                    439: static struct mdoc_node *
1.37      schwarze  440: node_alloc(struct mdoc *m, int line, int pos,
                    441:                enum mdoct tok, enum mdoc_type type)
1.1       kristaps  442: {
                    443:        struct mdoc_node *p;
                    444:
1.32      schwarze  445:        p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.19      schwarze  446:        p->sec = m->lastsec;
1.1       kristaps  447:        p->line = line;
                    448:        p->pos = pos;
                    449:        p->tok = tok;
1.37      schwarze  450:        p->type = type;
1.1       kristaps  451:
                    452:        return(p);
                    453: }
                    454:
                    455:
                    456: int
1.37      schwarze  457: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  458: {
                    459:        struct mdoc_node *p;
                    460:
1.19      schwarze  461:        p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.25      schwarze  462:        if ( ! node_append(m, p))
                    463:                return(0);
                    464:        m->next = MDOC_NEXT_CHILD;
                    465:        return(1);
1.1       kristaps  466: }
                    467:
                    468:
                    469: int
1.37      schwarze  470: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  471: {
                    472:        struct mdoc_node *p;
                    473:
1.19      schwarze  474:        assert(m->first);
                    475:        assert(m->last);
1.1       kristaps  476:
1.19      schwarze  477:        p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.25      schwarze  478:        if ( ! node_append(m, p))
                    479:                return(0);
                    480:        m->next = MDOC_NEXT_CHILD;
                    481:        return(1);
1.1       kristaps  482: }
                    483:
                    484:
                    485: int
1.37      schwarze  486: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1       kristaps  487: {
                    488:        struct mdoc_node *p;
                    489:
1.19      schwarze  490:        p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.25      schwarze  491:        if ( ! node_append(m, p))
                    492:                return(0);
                    493:        m->next = MDOC_NEXT_CHILD;
                    494:        return(1);
1.1       kristaps  495: }
                    496:
                    497:
                    498: int
1.19      schwarze  499: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  500:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  501: {
                    502:        struct mdoc_node *p;
                    503:
1.19      schwarze  504:        p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.4       schwarze  505:        p->args = args;
                    506:        if (p->args)
1.1       kristaps  507:                (args->refcnt)++;
1.25      schwarze  508:        if ( ! node_append(m, p))
                    509:                return(0);
                    510:        m->next = MDOC_NEXT_CHILD;
                    511:        return(1);
1.1       kristaps  512: }
                    513:
                    514:
                    515: int
1.19      schwarze  516: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.37      schwarze  517:                enum mdoct tok, struct mdoc_arg *args)
1.1       kristaps  518: {
                    519:        struct mdoc_node *p;
                    520:
1.19      schwarze  521:        p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.4       schwarze  522:        p->args = args;
                    523:        if (p->args)
1.1       kristaps  524:                (args->refcnt)++;
1.25      schwarze  525:        if ( ! node_append(m, p))
                    526:                return(0);
                    527:        m->next = MDOC_NEXT_CHILD;
                    528:        return(1);
1.1       kristaps  529: }
                    530:
                    531:
1.19      schwarze  532: static int
                    533: pstring(struct mdoc *m, int line, int pos, const char *p, size_t len)
1.1       kristaps  534: {
1.19      schwarze  535:        struct mdoc_node *n;
                    536:        size_t            sv;
1.1       kristaps  537:
1.19      schwarze  538:        n = node_alloc(m, line, pos, -1, MDOC_TEXT);
1.32      schwarze  539:        n->string = mandoc_malloc(len + 1);
1.19      schwarze  540:        sv = strlcpy(n->string, p, len + 1);
                    541:
                    542:        /* Prohibit truncation. */
                    543:        assert(sv < len + 1);
                    544:
1.25      schwarze  545:        if ( ! node_append(m, n))
                    546:                return(0);
                    547:        m->next = MDOC_NEXT_SIBLING;
                    548:        return(1);
1.19      schwarze  549: }
                    550:
                    551:
                    552: int
                    553: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
                    554: {
                    555:
                    556:        return(pstring(m, line, pos, p, strlen(p)));
1.1       kristaps  557: }
                    558:
                    559:
                    560: void
                    561: mdoc_node_free(struct mdoc_node *p)
                    562: {
                    563:
                    564:        if (p->string)
                    565:                free(p->string);
                    566:        if (p->args)
                    567:                mdoc_argv_free(p->args);
                    568:        free(p);
                    569: }
                    570:
                    571:
1.40      schwarze  572: static void
                    573: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
                    574: {
                    575:
                    576:        /* Adjust siblings. */
                    577:
                    578:        if (n->prev)
                    579:                n->prev->next = n->next;
                    580:        if (n->next)
                    581:                n->next->prev = n->prev;
                    582:
                    583:        /* Adjust parent. */
                    584:
                    585:        if (n->parent) {
                    586:                n->parent->nchild--;
                    587:                if (n->parent->child == n)
                    588:                        n->parent->child = n->prev ? n->prev : n->next;
                    589:        }
                    590:
                    591:        /* Adjust parse point, if applicable. */
                    592:
                    593:        if (m && m->last == n) {
                    594:                if (n->prev) {
                    595:                        m->last = n->prev;
                    596:                        m->next = MDOC_NEXT_SIBLING;
                    597:                } else {
                    598:                        m->last = n->parent;
                    599:                        m->next = MDOC_NEXT_CHILD;
                    600:                }
                    601:        }
                    602:
                    603:        if (m && m->first == n)
                    604:                m->first = NULL;
                    605: }
                    606:
                    607:
1.1       kristaps  608: void
1.40      schwarze  609: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1       kristaps  610: {
                    611:
1.40      schwarze  612:        while (p->child) {
                    613:                assert(p->nchild);
                    614:                mdoc_node_delete(m, p->child);
                    615:        }
                    616:        assert(0 == p->nchild);
1.1       kristaps  617:
1.40      schwarze  618:        mdoc_node_unlink(m, p);
1.1       kristaps  619:        mdoc_node_free(p);
                    620: }
                    621:
                    622:
                    623: /*
                    624:  * Parse free-form text, that is, a line that does not begin with the
                    625:  * control character.
                    626:  */
                    627: static int
                    628: parsetext(struct mdoc *m, int line, char *buf)
                    629: {
1.19      schwarze  630:        int              i, j;
1.34      schwarze  631:        char             sv;
1.1       kristaps  632:
1.9       schwarze  633:        if (SEC_NONE == m->lastnamed)
1.16      schwarze  634:                return(mdoc_perr(m, line, 0, ETEXTPROL));
1.19      schwarze  635:
                    636:        /*
                    637:         * If in literal mode, then pass the buffer directly to the
                    638:         * back-end, as it should be preserved as a single term.
                    639:         */
1.1       kristaps  640:
1.25      schwarze  641:        if (MDOC_LITERAL & m->flags)
                    642:                return(mdoc_word_alloc(m, line, 0, buf));
1.19      schwarze  643:
                    644:        /* Disallow blank/white-space lines in non-literal mode. */
                    645:
                    646:        for (i = 0; ' ' == buf[i]; i++)
                    647:                /* Skip leading whitespace. */ ;
1.34      schwarze  648:
1.38      schwarze  649:        if ('\0' == buf[i]) {
                    650:                if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
                    651:                        return(0);
1.40      schwarze  652:                /*
                    653:                 * Assume that a `Pp' should be inserted in the case of
                    654:                 * a blank line.  Technically, blank lines aren't
                    655:                 * allowed, but enough manuals assume this behaviour
                    656:                 * that we want to work around it.
                    657:                 */
1.41    ! schwarze  658:                return(mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL));
1.38      schwarze  659:        }
1.1       kristaps  660:
1.19      schwarze  661:        /*
                    662:         * Break apart a free-form line into tokens.  Spaces are
                    663:         * stripped out of the input.
                    664:         */
                    665:
                    666:        for (j = i; buf[i]; i++) {
                    667:                if (' ' != buf[i])
                    668:                        continue;
                    669:
                    670:                /* Escaped whitespace. */
                    671:                if (i && ' ' == buf[i] && '\\' == buf[i - 1])
                    672:                        continue;
                    673:
1.34      schwarze  674:                sv = buf[i];
                    675:                buf[i++] = '\0';
                    676:
1.19      schwarze  677:                if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
                    678:                        return(0);
                    679:
1.34      schwarze  680:                /* Trailing whitespace?  Check at overwritten byte. */
                    681:
                    682:                if (' ' == sv && '\0' == buf[i])
                    683:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    684:                                return(0);
                    685:
1.19      schwarze  686:                for ( ; ' ' == buf[i]; i++)
                    687:                        /* Skip trailing whitespace. */ ;
                    688:
                    689:                j = i;
1.34      schwarze  690:
                    691:                /* Trailing whitespace? */
                    692:
                    693:                if (' ' == buf[i - 1] && '\0' == buf[i])
                    694:                        if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
                    695:                                return(0);
                    696:
                    697:                if ('\0' == buf[i])
1.19      schwarze  698:                        break;
                    699:        }
                    700:
                    701:        if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.1       kristaps  702:                return(0);
                    703:
1.35      schwarze  704:        /*
                    705:         * Mark the end of a sentence.  Only works when you respect
                    706:         * Jason's rule: "new sentence, new line".
                    707:         */
                    708:        if ('.' == buf[i-1] || '!' == buf[i-1] || '?' == buf[i-1]) {
                    709:                m->next = MDOC_NEXT_SIBLING;
                    710:                if ( ! mdoc_elem_alloc(m, line, i, MDOC_eos, NULL))
                    711:                        return(0);
                    712:        }
                    713:
1.1       kristaps  714:        m->next = MDOC_NEXT_SIBLING;
                    715:        return(1);
                    716: }
1.19      schwarze  717:
1.1       kristaps  718:
                    719:
                    720: static int
                    721: macrowarn(struct mdoc *m, int ln, const char *buf)
                    722: {
                    723:        if ( ! (MDOC_IGN_MACRO & m->pflags))
1.23      schwarze  724:                return(mdoc_verr(m, ln, 0,
1.1       kristaps  725:                                "unknown macro: %s%s",
                    726:                                buf, strlen(buf) > 3 ? "..." : ""));
1.23      schwarze  727:        return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.1       kristaps  728:                                buf, strlen(buf) > 3 ? "..." : ""));
                    729: }
                    730:
                    731:
                    732: /*
                    733:  * Parse a macro line, that is, a line beginning with the control
                    734:  * character.
                    735:  */
                    736: int
                    737: parsemacro(struct mdoc *m, int ln, char *buf)
                    738: {
1.25      schwarze  739:        int               i, j, c;
1.1       kristaps  740:        char              mac[5];
1.35      schwarze  741:        struct mdoc_node *n;
                    742:        char             *t;
1.1       kristaps  743:
1.7       schwarze  744:        /* Empty lines are ignored. */
1.1       kristaps  745:
1.34      schwarze  746:        if ('\0' == buf[1])
1.1       kristaps  747:                return(1);
                    748:
1.24      schwarze  749:        i = 1;
                    750:
                    751:        /* Accept whitespace after the initial control char. */
                    752:
                    753:        if (' ' == buf[i]) {
                    754:                i++;
1.1       kristaps  755:                while (buf[i] && ' ' == buf[i])
                    756:                        i++;
1.34      schwarze  757:                if ('\0' == buf[i])
1.1       kristaps  758:                        return(1);
                    759:        }
                    760:
                    761:        /* Copy the first word into a nil-terminated buffer. */
                    762:
1.24      schwarze  763:        for (j = 0; j < 4; j++, i++) {
1.34      schwarze  764:                if ('\0' == (mac[j] = buf[i]))
1.1       kristaps  765:                        break;
                    766:                else if (' ' == buf[i])
                    767:                        break;
1.26      schwarze  768:
                    769:                /* Check for invalid characters. */
                    770:
                    771:                if (isgraph((u_char)buf[i]))
                    772:                        continue;
                    773:                return(mdoc_perr(m, ln, i, EPRINT));
1.1       kristaps  774:        }
                    775:
1.24      schwarze  776:        mac[j] = 0;
1.1       kristaps  777:
1.24      schwarze  778:        if (j == 4 || j < 2) {
1.1       kristaps  779:                if ( ! macrowarn(m, ln, mac))
                    780:                        goto err;
                    781:                return(1);
                    782:        }
                    783:
1.27      schwarze  784:        if (MDOC_MAX == (c = mdoc_hash_find(mac))) {
1.1       kristaps  785:                if ( ! macrowarn(m, ln, mac))
                    786:                        goto err;
                    787:                return(1);
                    788:        }
                    789:
                    790:        /* The macro is sane.  Jump to the next word. */
                    791:
                    792:        while (buf[i] && ' ' == buf[i])
                    793:                i++;
1.34      schwarze  794:
                    795:        /* Trailing whitespace? */
                    796:
                    797:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    798:                if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
                    799:                        goto err;
1.1       kristaps  800:
1.24      schwarze  801:        /*
                    802:         * Begin recursive parse sequence.  Since we're at the start of
                    803:         * the line, we don't need to do callable/parseable checks.
                    804:         */
1.25      schwarze  805:        if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
1.1       kristaps  806:                goto err;
1.35      schwarze  807:
                    808:        /*
                    809:         * Mark the end of a sentence, but be careful not to insert
                    810:         * markers into reference blocks.
                    811:         */
                    812:        n = m->last;
                    813:        if (n->child)
                    814:                n = n->child;
                    815:        while (n->next)
                    816:                n = n->next;
                    817:        if (MDOC_TEXT == n->type && m->last->parent->tok != MDOC_Rs) {
                    818:                t = n->string;
1.36      schwarze  819:                while (t[0] && t[1])
1.35      schwarze  820:                        t++;
                    821:                if ('.' == *t || '!' == *t || '?' == *t) {
                    822:                        if ( ! mdoc_elem_alloc(m, ln, i, MDOC_eos, NULL))
                    823:                                return(0);
                    824:                        m->next = MDOC_NEXT_SIBLING;
                    825:                }
                    826:        }
1.1       kristaps  827:
                    828:        return(1);
                    829:
                    830: err:   /* Error out. */
                    831:
                    832:        m->flags |= MDOC_HALT;
                    833:        return(0);
                    834: }
1.24      schwarze  835:
                    836: