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

Annotation of mandoc/mdoc.c, Revision 1.251

1.251   ! schwarze    1: /*     $Id: mdoc.c,v 1.250 2015/04/19 14:57:38 schwarze Exp $ */
1.1       kristaps    2: /*
1.182     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.237     schwarze    4:  * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.75      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.241     schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.75      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.241     schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.75      kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.114     kristaps   18: #include "config.h"
                     19:
1.106     kristaps   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.211     schwarze   23: #include <ctype.h>
1.1       kristaps   24: #include <stdarg.h>
1.73      kristaps   25: #include <stdio.h>
1.1       kristaps   26: #include <stdlib.h>
                     27: #include <string.h>
1.120     kristaps   28: #include <time.h>
1.1       kristaps   29:
1.239     schwarze   30: #include "mandoc_aux.h"
                     31: #include "mandoc.h"
                     32: #include "roff.h"
1.187     kristaps   33: #include "mdoc.h"
1.239     schwarze   34: #include "libmandoc.h"
1.247     schwarze   35: #include "roff_int.h"
1.70      kristaps   36: #include "libmdoc.h"
1.1       kristaps   37:
1.216     schwarze   38: const  char *const __mdoc_macronames[MDOC_MAX + 1] = {
1.248     schwarze   39:        "text",
1.82      kristaps   40:        "Ap",           "Dd",           "Dt",           "Os",
1.1       kristaps   41:        "Sh",           "Ss",           "Pp",           "D1",
                     42:        "Dl",           "Bd",           "Ed",           "Bl",
                     43:        "El",           "It",           "Ad",           "An",
                     44:        "Ar",           "Cd",           "Cm",           "Dv",
                     45:        "Er",           "Ev",           "Ex",           "Fa",
                     46:        "Fd",           "Fl",           "Fn",           "Ft",
                     47:        "Ic",           "In",           "Li",           "Nd",
                     48:        "Nm",           "Op",           "Ot",           "Pa",
                     49:        "Rv",           "St",           "Va",           "Vt",
1.114     kristaps   50:        "Xr",           "%A",           "%B",           "%D",
                     51:        "%I",           "%J",           "%N",           "%O",
                     52:        "%P",           "%R",           "%T",           "%V",
1.1       kristaps   53:        "Ac",           "Ao",           "Aq",           "At",
                     54:        "Bc",           "Bf",           "Bo",           "Bq",
                     55:        "Bsx",          "Bx",           "Db",           "Dc",
                     56:        "Do",           "Dq",           "Ec",           "Ef",
                     57:        "Em",           "Eo",           "Fx",           "Ms",
                     58:        "No",           "Ns",           "Nx",           "Ox",
                     59:        "Pc",           "Pf",           "Po",           "Pq",
                     60:        "Qc",           "Ql",           "Qo",           "Qq",
                     61:        "Re",           "Rs",           "Sc",           "So",
                     62:        "Sq",           "Sm",           "Sx",           "Sy",
                     63:        "Tn",           "Ux",           "Xc",           "Xo",
                     64:        "Fo",           "Fc",           "Oo",           "Oc",
                     65:        "Bk",           "Ek",           "Bt",           "Hf",
1.82      kristaps   66:        "Fr",           "Ud",           "Lb",           "Lp",
                     67:        "Lk",           "Mt",           "Brq",          "Bro",
1.114     kristaps   68:        "Brc",          "%C",           "Es",           "En",
                     69:        "Dx",           "%Q",           "br",           "sp",
1.248     schwarze   70:        "%U",           "Ta",           "ll",
                     71: };
1.1       kristaps   72:
1.213     schwarze   73: const  char *const __mdoc_argnames[MDOC_ARG_MAX] = {
1.1       kristaps   74:        "split",                "nosplit",              "ragged",
1.213     schwarze   75:        "unfilled",             "literal",              "file",
                     76:        "offset",               "bullet",               "dash",
                     77:        "hyphen",               "item",                 "enum",
                     78:        "tag",                  "diag",                 "hang",
                     79:        "ohang",                "inset",                "column",
                     80:        "width",                "compact",              "std",
1.52      kristaps   81:        "filled",               "words",                "emphasis",
1.108     kristaps   82:        "symbolic",             "nested",               "centered"
1.1       kristaps   83:        };
                     84:
1.248     schwarze   85: const  char * const *mdoc_macronames = __mdoc_macronames + 1;
1.1       kristaps   86: const  char * const *mdoc_argnames = __mdoc_argnames;
                     87:
1.242     schwarze   88: static int               mdoc_ptext(struct roff_man *, int, char *, int);
                     89: static int               mdoc_pmacro(struct roff_man *, int, char *, int);
1.88      kristaps   90:
1.181     kristaps   91:
1.50      kristaps   92: /*
1.53      kristaps   93:  * Main parse routine.  Parses a single line -- really just hands off to
1.123     kristaps   94:  * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50      kristaps   95:  */
1.20      kristaps   96: int
1.242     schwarze   97: mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1       kristaps   98: {
                     99:
1.239     schwarze  100:        if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
1.228     schwarze  101:                mdoc->flags |= MDOC_NEWLINE;
1.153     schwarze  102:
                    103:        /*
                    104:         * Let the roff nS register switch SYNOPSIS mode early,
                    105:         * such that the parser knows at all times
                    106:         * whether this mode is on or off.
                    107:         * Note that this mode is also switched by the Sh macro.
                    108:         */
1.204     schwarze  109:        if (roff_getreg(mdoc->roff, "nS"))
                    110:                mdoc->flags |= MDOC_SYNOPSIS;
                    111:        else
                    112:                mdoc->flags &= ~MDOC_SYNOPSIS;
1.153     schwarze  113:
1.203     schwarze  114:        return(roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213     schwarze  115:            mdoc_pmacro(mdoc, ln, buf, offs) :
                    116:            mdoc_ptext(mdoc, ln, buf, offs));
1.1       kristaps  117: }
                    118:
1.232     schwarze  119: void
1.148     kristaps  120: mdoc_macro(MACRO_PROT_ARGS)
1.88      kristaps  121: {
1.248     schwarze  122:        assert(tok > TOKEN_NONE && tok < MDOC_MAX);
1.122     kristaps  123:
1.223     schwarze  124:        if (mdoc->flags & MDOC_PBODY) {
                    125:                if (tok == MDOC_Dt) {
                    126:                        mandoc_vmsg(MANDOCERR_DT_LATE,
                    127:                            mdoc->parse, line, ppos,
                    128:                            "Dt %s", buf + *pos);
1.232     schwarze  129:                        return;
1.223     schwarze  130:                }
                    131:        } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
                    132:                if (mdoc->meta.title == NULL) {
                    133:                        mandoc_vmsg(MANDOCERR_DT_NOTITLE,
                    134:                            mdoc->parse, line, ppos, "%s %s",
                    135:                            mdoc_macronames[tok], buf + *pos);
                    136:                        mdoc->meta.title = mandoc_strdup("UNTITLED");
                    137:                }
1.203     schwarze  138:                if (NULL == mdoc->meta.vol)
                    139:                        mdoc->meta.vol = mandoc_strdup("LOCAL");
                    140:                mdoc->flags |= MDOC_PBODY;
1.120     kristaps  141:        }
1.232     schwarze  142:        (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
1.73      kristaps  143: }
                    144:
1.231     schwarze  145: void
1.242     schwarze  146: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.17      kristaps  147: {
1.240     schwarze  148:        struct roff_node *p;
1.17      kristaps  149:
1.247     schwarze  150:        p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
                    151:        roff_node_append(mdoc, p);
1.242     schwarze  152:        mdoc->next = ROFF_NEXT_CHILD;
1.152     schwarze  153: }
                    154:
1.240     schwarze  155: struct roff_node *
1.242     schwarze  156: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
1.240     schwarze  157:                struct roff_node *body, enum mdoc_endbody end)
1.152     schwarze  158: {
1.240     schwarze  159:        struct roff_node *p;
1.152     schwarze  160:
1.237     schwarze  161:        body->flags |= MDOC_ENDED;
                    162:        body->parent->flags |= MDOC_ENDED;
1.247     schwarze  163:        p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
1.237     schwarze  164:        p->body = body;
1.202     schwarze  165:        p->norm = body->norm;
1.152     schwarze  166:        p->end = end;
1.247     schwarze  167:        roff_node_append(mdoc, p);
1.242     schwarze  168:        mdoc->next = ROFF_NEXT_SIBLING;
1.235     schwarze  169:        return(p);
1.1       kristaps  170: }
                    171:
1.240     schwarze  172: struct roff_node *
1.242     schwarze  173: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.240     schwarze  174:        int tok, struct mdoc_arg *args)
1.1       kristaps  175: {
1.240     schwarze  176:        struct roff_node *p;
1.1       kristaps  177:
1.247     schwarze  178:        p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
1.77      kristaps  179:        p->args = args;
                    180:        if (p->args)
1.53      kristaps  181:                (args->refcnt)++;
1.172     kristaps  182:
                    183:        switch (tok) {
1.213     schwarze  184:        case MDOC_Bd:
1.172     kristaps  185:                /* FALLTHROUGH */
1.213     schwarze  186:        case MDOC_Bf:
1.172     kristaps  187:                /* FALLTHROUGH */
1.213     schwarze  188:        case MDOC_Bl:
1.217     schwarze  189:                /* FALLTHROUGH */
                    190:        case MDOC_En:
1.173     kristaps  191:                /* FALLTHROUGH */
1.213     schwarze  192:        case MDOC_Rs:
1.172     kristaps  193:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    194:                break;
                    195:        default:
                    196:                break;
                    197:        }
1.247     schwarze  198:        roff_node_append(mdoc, p);
1.242     schwarze  199:        mdoc->next = ROFF_NEXT_CHILD;
1.231     schwarze  200:        return(p);
1.1       kristaps  201: }
                    202:
1.231     schwarze  203: void
1.242     schwarze  204: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.240     schwarze  205:        int tok, struct mdoc_arg *args)
1.1       kristaps  206: {
1.240     schwarze  207:        struct roff_node *p;
1.1       kristaps  208:
1.247     schwarze  209:        p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
1.77      kristaps  210:        p->args = args;
                    211:        if (p->args)
1.53      kristaps  212:                (args->refcnt)++;
1.172     kristaps  213:
                    214:        switch (tok) {
1.213     schwarze  215:        case MDOC_An:
1.172     kristaps  216:                p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
                    217:                break;
                    218:        default:
                    219:                break;
                    220:        }
1.247     schwarze  221:        roff_node_append(mdoc, p);
1.242     schwarze  222:        mdoc->next = ROFF_NEXT_CHILD;
1.175     kristaps  223: }
1.1       kristaps  224:
1.231     schwarze  225: void
1.242     schwarze  226: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201     schwarze  227: {
                    228:
1.247     schwarze  229:        roff_node_unlink(mdoc, p);
                    230:        roff_node_append(mdoc, p);
1.1       kristaps  231: }
                    232:
1.53      kristaps  233: /*
                    234:  * Parse free-form text, that is, a line that does not begin with the
                    235:  * control character.
                    236:  */
                    237: static int
1.242     schwarze  238: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1       kristaps  239: {
1.240     schwarze  240:        struct roff_node *n;
1.142     kristaps  241:        char             *c, *ws, *end;
                    242:
1.203     schwarze  243:        assert(mdoc->last);
                    244:        n = mdoc->last;
1.142     kristaps  245:
                    246:        /*
1.144     kristaps  247:         * Divert directly to list processing if we're encountering a
1.239     schwarze  248:         * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
                    249:         * (a ROFFT_BODY means it's already open, in which case we should
1.144     kristaps  250:         * process within its context in the normal way).
1.142     kristaps  251:         */
                    252:
1.239     schwarze  253:        if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238     schwarze  254:            n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.144     kristaps  255:                /* `Bl' is open without any children. */
1.203     schwarze  256:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  257:                mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
                    258:                return(1);
1.142     kristaps  259:        }
                    260:
1.239     schwarze  261:        if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213     schwarze  262:            NULL != n->parent &&
                    263:            MDOC_Bl == n->parent->tok &&
                    264:            LIST_column == n->parent->norm->Bl.type) {
1.144     kristaps  265:                /* `Bl' has block-level `It' children. */
1.203     schwarze  266:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  267:                mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
                    268:                return(1);
1.142     kristaps  269:        }
1.124     kristaps  270:
1.137     schwarze  271:        /*
                    272:         * Search for the beginning of unescaped trailing whitespace (ws)
                    273:         * and for the first character not to be output (end).
                    274:         */
1.139     kristaps  275:
                    276:        /* FIXME: replace with strcspn(). */
1.137     schwarze  277:        ws = NULL;
                    278:        for (c = end = buf + offs; *c; c++) {
                    279:                switch (*c) {
                    280:                case ' ':
                    281:                        if (NULL == ws)
                    282:                                ws = c;
                    283:                        continue;
                    284:                case '\t':
                    285:                        /*
                    286:                         * Always warn about trailing tabs,
                    287:                         * even outside literal context,
                    288:                         * where they should be put on the next line.
                    289:                         */
                    290:                        if (NULL == ws)
                    291:                                ws = c;
                    292:                        /*
                    293:                         * Strip trailing tabs in literal context only;
                    294:                         * outside, they affect the next line.
                    295:                         */
1.203     schwarze  296:                        if (MDOC_LITERAL & mdoc->flags)
1.137     schwarze  297:                                continue;
                    298:                        break;
                    299:                case '\\':
                    300:                        /* Skip the escaped character, too, if any. */
                    301:                        if (c[1])
                    302:                                c++;
                    303:                        /* FALLTHROUGH */
                    304:                default:
                    305:                        ws = NULL;
                    306:                        break;
                    307:                }
                    308:                end = c + 1;
                    309:        }
                    310:        *end = '\0';
1.91      kristaps  311:
1.137     schwarze  312:        if (ws)
1.218     schwarze  313:                mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                    314:                    line, (int)(ws-buf), NULL);
1.115     kristaps  315:
1.231     schwarze  316:        if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.218     schwarze  317:                mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
                    318:                    line, (int)(c - buf), NULL);
1.124     kristaps  319:
1.119     kristaps  320:                /*
1.165     schwarze  321:                 * Insert a `sp' in the case of a blank line.  Technically,
1.124     kristaps  322:                 * blank lines aren't allowed, but enough manuals assume this
                    323:                 * behaviour that we want to work around it.
1.119     kristaps  324:                 */
1.250     schwarze  325:                roff_elem_alloc(mdoc, line, offs, MDOC_sp);
1.242     schwarze  326:                mdoc->next = ROFF_NEXT_SIBLING;
1.230     schwarze  327:                mdoc_valid_post(mdoc);
                    328:                return(1);
1.119     kristaps  329:        }
1.68      kristaps  330:
1.249     schwarze  331:        roff_word_alloc(mdoc, line, offs, buf+offs);
1.91      kristaps  332:
1.231     schwarze  333:        if (mdoc->flags & MDOC_LITERAL)
1.137     schwarze  334:                return(1);
1.128     kristaps  335:
                    336:        /*
                    337:         * End-of-sentence check.  If the last character is an unescaped
                    338:         * EOS character, then flag the node as being the end of a
                    339:         * sentence.  The front-end will know how to interpret this.
                    340:         */
1.132     kristaps  341:
1.137     schwarze  342:        assert(buf < end);
                    343:
1.207     schwarze  344:        if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203     schwarze  345:                mdoc->last->flags |= MDOC_EOS;
1.128     kristaps  346:        return(1);
1.1       kristaps  347: }
                    348:
1.53      kristaps  349: /*
                    350:  * Parse a macro line, that is, a line beginning with the control
                    351:  * character.
                    352:  */
1.155     kristaps  353: static int
1.242     schwarze  354: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1       kristaps  355: {
1.240     schwarze  356:        struct roff_node *n;
1.229     schwarze  357:        const char       *cp;
1.240     schwarze  358:        int               tok;
1.188     kristaps  359:        int               i, sv;
1.144     kristaps  360:        char              mac[5];
1.63      kristaps  361:
1.188     kristaps  362:        sv = offs;
1.130     kristaps  363:
1.213     schwarze  364:        /*
1.162     schwarze  365:         * Copy the first word into a nil-terminated buffer.
1.229     schwarze  366:         * Stop when a space, tab, escape, or eoln is encountered.
1.160     kristaps  367:         */
1.1       kristaps  368:
1.188     kristaps  369:        i = 0;
1.229     schwarze  370:        while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.188     kristaps  371:                mac[i++] = buf[offs++];
                    372:
                    373:        mac[i] = '\0';
                    374:
1.248     schwarze  375:        tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
1.1       kristaps  376:
1.248     schwarze  377:        if (tok == TOKEN_NONE) {
1.222     schwarze  378:                mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
                    379:                    ln, sv, buf + sv - 1);
1.58      kristaps  380:                return(1);
1.53      kristaps  381:        }
1.1       kristaps  382:
1.229     schwarze  383:        /* Skip a leading escape sequence or tab. */
1.160     kristaps  384:
1.229     schwarze  385:        switch (buf[offs]) {
                    386:        case '\\':
                    387:                cp = buf + offs + 1;
                    388:                mandoc_escape(&cp, NULL, NULL);
                    389:                offs = cp - buf;
                    390:                break;
                    391:        case '\t':
1.188     kristaps  392:                offs++;
1.229     schwarze  393:                break;
                    394:        default:
                    395:                break;
                    396:        }
1.160     kristaps  397:
                    398:        /* Jump to the next non-whitespace word. */
1.1       kristaps  399:
1.188     kristaps  400:        while (buf[offs] && ' ' == buf[offs])
                    401:                offs++;
1.1       kristaps  402:
1.213     schwarze  403:        /*
1.125     kristaps  404:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    405:         * into the parser as "text", so we only warn about spaces here.
                    406:         */
1.115     kristaps  407:
1.188     kristaps  408:        if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218     schwarze  409:                mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
                    410:                    ln, offs - 1, NULL);
1.115     kristaps  411:
1.144     kristaps  412:        /*
                    413:         * If an initial macro or a list invocation, divert directly
                    414:         * into macro processing.
                    415:         */
                    416:
1.232     schwarze  417:        if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
                    418:                mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
                    419:                return(1);
                    420:        }
1.144     kristaps  421:
1.203     schwarze  422:        n = mdoc->last;
                    423:        assert(mdoc->last);
1.144     kristaps  424:
                    425:        /*
                    426:         * If the first macro of a `Bl -column', open an `It' block
                    427:         * context around the parsed macro.
                    428:         */
                    429:
1.239     schwarze  430:        if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238     schwarze  431:            n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.203     schwarze  432:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  433:                mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
                    434:                return(1);
1.144     kristaps  435:        }
                    436:
                    437:        /*
                    438:         * If we're following a block-level `It' within a `Bl -column'
                    439:         * context (perhaps opened in the above block or in ptext()),
                    440:         * then open an `It' block context around the parsed macro.
1.98      kristaps  441:         */
1.144     kristaps  442:
1.239     schwarze  443:        if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213     schwarze  444:            NULL != n->parent &&
                    445:            MDOC_Bl == n->parent->tok &&
                    446:            LIST_column == n->parent->norm->Bl.type) {
1.203     schwarze  447:                mdoc->flags |= MDOC_FREECOL;
1.232     schwarze  448:                mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
                    449:                return(1);
1.144     kristaps  450:        }
                    451:
                    452:        /* Normal processing of a macro. */
                    453:
1.232     schwarze  454:        mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.208     schwarze  455:
                    456:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    457:
                    458:        if (mdoc->quick && MDOC_Sh == tok &&
                    459:            SEC_NAME != mdoc->last->sec)
                    460:                return(2);
1.1       kristaps  461:
1.53      kristaps  462:        return(1);
1.1       kristaps  463: }
1.100     kristaps  464:
1.186     kristaps  465: enum mdelim
                    466: mdoc_isdelim(const char *p)
                    467: {
                    468:
                    469:        if ('\0' == p[0])
                    470:                return(DELIM_NONE);
                    471:
                    472:        if ('\0' == p[1])
                    473:                switch (p[0]) {
1.213     schwarze  474:                case '(':
1.186     kristaps  475:                        /* FALLTHROUGH */
1.213     schwarze  476:                case '[':
1.186     kristaps  477:                        return(DELIM_OPEN);
1.213     schwarze  478:                case '|':
1.186     kristaps  479:                        return(DELIM_MIDDLE);
1.213     schwarze  480:                case '.':
1.186     kristaps  481:                        /* FALLTHROUGH */
1.213     schwarze  482:                case ',':
1.186     kristaps  483:                        /* FALLTHROUGH */
1.213     schwarze  484:                case ';':
1.186     kristaps  485:                        /* FALLTHROUGH */
1.213     schwarze  486:                case ':':
1.186     kristaps  487:                        /* FALLTHROUGH */
1.213     schwarze  488:                case '?':
1.186     kristaps  489:                        /* FALLTHROUGH */
1.213     schwarze  490:                case '!':
1.186     kristaps  491:                        /* FALLTHROUGH */
1.213     schwarze  492:                case ')':
1.186     kristaps  493:                        /* FALLTHROUGH */
1.213     schwarze  494:                case ']':
1.186     kristaps  495:                        return(DELIM_CLOSE);
                    496:                default:
                    497:                        return(DELIM_NONE);
                    498:                }
                    499:
                    500:        if ('\\' != p[0])
                    501:                return(DELIM_NONE);
1.100     kristaps  502:
1.186     kristaps  503:        if (0 == strcmp(p + 1, "."))
                    504:                return(DELIM_CLOSE);
1.200     schwarze  505:        if (0 == strcmp(p + 1, "fR|\\fP"))
1.186     kristaps  506:                return(DELIM_MIDDLE);
                    507:
                    508:        return(DELIM_NONE);
1.211     schwarze  509: }
                    510:
                    511: void
1.240     schwarze  512: mdoc_deroff(char **dest, const struct roff_node *n)
1.211     schwarze  513: {
                    514:        char    *cp;
                    515:        size_t   sz;
                    516:
1.239     schwarze  517:        if (n->type != ROFFT_TEXT) {
1.211     schwarze  518:                for (n = n->child; n; n = n->next)
                    519:                        mdoc_deroff(dest, n);
                    520:                return;
                    521:        }
                    522:
                    523:        /* Skip leading whitespace. */
                    524:
                    525:        for (cp = n->string; '\0' != *cp; cp++)
                    526:                if (0 == isspace((unsigned char)*cp))
                    527:                        break;
                    528:
                    529:        /* Skip trailing whitespace. */
                    530:
                    531:        for (sz = strlen(cp); sz; sz--)
                    532:                if (0 == isspace((unsigned char)cp[sz-1]))
                    533:                        break;
                    534:
                    535:        /* Skip empty strings. */
                    536:
                    537:        if (0 == sz)
                    538:                return;
                    539:
                    540:        if (NULL == *dest) {
                    541:                *dest = mandoc_strndup(cp, sz);
                    542:                return;
                    543:        }
                    544:
                    545:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                    546:        free(*dest);
                    547:        *dest = cp;
1.186     kristaps  548: }

CVSweb