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

Annotation of docbook2mdoc/macro.c, Revision 1.10

1.10    ! schwarze    1: /* $Id: macro.c,v 1.9 2019/04/11 04:23:22 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
                     16:  */
                     17: #include <assert.h>
                     18: #include <ctype.h>
                     19: #include <stdio.h>
1.6       schwarze   20: #include <string.h>
1.1       schwarze   21:
                     22: #include "node.h"
                     23: #include "macro.h"
                     24:
                     25: /*
                     26:  * The implementation of the macro line formatter,
                     27:  * a part of the mdoc(7) formatter.
                     28:  */
                     29:
                     30: void
                     31: macro_open(struct format *f, const char *name)
                     32: {
                     33:        switch (f->linestate) {
1.9       schwarze   34:        case LINE_MACRO:
                     35:                if (f->flags & FMT_NOSPC) {
                     36:                        fputs(" Ns ", stdout);
                     37:                        break;
                     38:                }
                     39:                if (f->flags & (FMT_CHILD | FMT_IMPL)) {
                     40:                        putchar(' ');
                     41:                        break;
                     42:                }
                     43:                /* FALLTHROUGH */
1.1       schwarze   44:        case LINE_TEXT:
                     45:                putchar('\n');
                     46:                /* FALLTHROUGH */
                     47:        case LINE_NEW:
                     48:                putchar('.');
                     49:                f->linestate = LINE_MACRO;
1.9       schwarze   50:                f->flags = 0;
1.1       schwarze   51:                break;
                     52:        }
                     53:        fputs(name, stdout);
1.9       schwarze   54:        f->flags &= FMT_IMPL;
                     55:        f->flags |= FMT_ARG;
1.1       schwarze   56: }
                     57:
                     58: void
                     59: macro_close(struct format *f)
                     60: {
1.9       schwarze   61:        if (f->linestate != LINE_NEW)
                     62:                putchar('\n');
1.1       schwarze   63:        f->linestate = LINE_NEW;
1.9       schwarze   64:        f->flags = 0;
1.1       schwarze   65: }
                     66:
                     67: void
                     68: macro_line(struct format *f, const char *name)
                     69: {
1.7       schwarze   70:        macro_close(f);
1.1       schwarze   71:        macro_open(f, name);
                     72:        macro_close(f);
                     73: }
                     74:
                     75: /*
                     76:  * Print an argument string on a macro line, collapsing whitespace.
                     77:  */
                     78: void
                     79: macro_addarg(struct format *f, const char *arg, int flags)
                     80: {
                     81:        const char      *cp;
                     82:
                     83:        assert(f->linestate == LINE_MACRO);
                     84:
                     85:        /* Quote if requested and necessary. */
                     86:
                     87:        if ((flags & (ARG_SINGLE | ARG_QUOTED)) == ARG_SINGLE) {
                     88:                for (cp = arg; *cp != '\0'; cp++)
                     89:                        if (isspace((unsigned char)*cp))
                     90:                                break;
                     91:                if (*cp != '\0') {
                     92:                        if (flags & ARG_SPACE) {
                     93:                                putchar(' ');
                     94:                                flags &= ~ ARG_SPACE;
                     95:                        }
                     96:                        putchar('"');
                     97:                        flags = ARG_QUOTED;
                     98:                }
                     99:        }
                    100:
                    101:        for (cp = arg; *cp != '\0'; cp++) {
                    102:
                    103:                /* Collapse whitespace. */
                    104:
                    105:                if (isspace((unsigned char)*cp)) {
                    106:                        flags |= ARG_SPACE;
                    107:                        continue;
                    108:                } else if (flags & ARG_SPACE) {
                    109:                        putchar(' ');
                    110:                        flags &= ~ ARG_SPACE;
                    111:                }
                    112:
                    113:                /* Escape us if we look like a macro. */
                    114:
                    115:                if ((flags & ARG_QUOTED) == 0 &&
                    116:                    (cp == arg || isspace((unsigned char)cp[-1])) &&
                    117:                    isupper((unsigned char)cp[0]) &&
                    118:                    islower((unsigned char)cp[1]) &&
                    119:                    (cp[2] == '\0' || cp[2] == ' ' ||
                    120:                     (islower((unsigned char)cp[2]) &&
                    121:                      (cp[3] == '\0' || cp[3] == ' '))))
                    122:                        fputs("\\&", stdout);
                    123:
                    124:                if (*cp == '"')
                    125:                        fputs("\\(dq", stdout);
                    126:                else if (flags & ARG_UPPER)
                    127:                        putchar(toupper((unsigned char)*cp));
                    128:                else
                    129:                        putchar(*cp);
                    130:                if (*cp == '\\')
                    131:                        putchar('e');
                    132:        }
                    133: }
                    134:
                    135: void
                    136: macro_argline(struct format *f, const char *name, const char *arg)
                    137: {
                    138:        macro_open(f, name);
                    139:        macro_addarg(f, arg, ARG_SPACE);
                    140:        macro_close(f);
                    141: }
                    142:
                    143: /*
                    144:  * Recursively append text from the children of a node to a macro line.
                    145:  */
                    146: void
1.8       schwarze  147: macro_addnode(struct format *f, struct pnode *n, int flags)
1.1       schwarze  148: {
1.5       schwarze  149:        struct pnode    *nc;
1.1       schwarze  150:        int              quote_now;
                    151:
                    152:        assert(f->linestate == LINE_MACRO);
                    153:
                    154:        /*
1.2       schwarze  155:         * If this node or its only child is a text node, just add
                    156:         * that text, letting macro_addarg() decide about quoting.
1.1       schwarze  157:         */
                    158:
1.8       schwarze  159:        while ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
1.5       schwarze  160:            TAILQ_NEXT(nc, child) == NULL)
1.8       schwarze  161:                n = nc;
1.5       schwarze  162:
1.8       schwarze  163:        if (n->node == NODE_TEXT || n->node == NODE_ESCAPE) {
                    164:                macro_addarg(f, n->b, flags);
1.1       schwarze  165:                return;
                    166:        }
                    167:
                    168:        /*
                    169:         * If we want the argument quoted and are not already
                    170:         * in a quoted context, quote now.
                    171:         */
                    172:
                    173:        quote_now = 0;
                    174:        if (flags & ARG_SINGLE) {
                    175:                if ((flags & ARG_QUOTED) == 0) {
                    176:                        if (flags & ARG_SPACE) {
                    177:                                putchar(' ');
                    178:                                flags &= ~ARG_SPACE;
                    179:                        }
                    180:                        putchar('"');
                    181:                        flags |= ARG_QUOTED;
                    182:                        quote_now = 1;
                    183:                }
                    184:                flags &= ~ARG_SINGLE;
                    185:        }
                    186:
                    187:        /*
                    188:         * Iterate to child and sibling nodes,
                    189:         * inserting whitespace between nodes.
                    190:         */
                    191:
1.5       schwarze  192:        while (nc != NULL) {
                    193:                macro_addnode(f, nc, flags);
                    194:                nc = TAILQ_NEXT(nc, child);
1.1       schwarze  195:                flags |= ARG_SPACE;
                    196:        }
                    197:        if (quote_now)
                    198:                putchar('"');
                    199: }
                    200:
                    201: void
1.8       schwarze  202: macro_nodeline(struct format *f, const char *name, struct pnode *n, int flags)
1.1       schwarze  203: {
                    204:        macro_open(f, name);
1.8       schwarze  205:        macro_addnode(f, n, ARG_SPACE | flags);
1.1       schwarze  206:        macro_close(f);
1.2       schwarze  207: }
                    208:
                    209:
                    210: /*
                    211:  * Print a word on the current text line if one is open, or on a new text
                    212:  * line otherwise.  The flag ARG_SPACE inserts spaces between words.
                    213:  */
                    214: void
1.10    ! schwarze  215: print_text(struct format *f, const char *word, int flags)
        !           216: {
1.2       schwarze  217:        switch (f->linestate) {
                    218:        case LINE_NEW:
1.10    ! schwarze  219:                if (*word == '.' || *word == '\'')
        !           220:                        fputs("\\&", stdout);
1.2       schwarze  221:                break;
                    222:        case LINE_TEXT:
                    223:                if (flags & ARG_SPACE)
                    224:                        putchar(' ');
                    225:                break;
                    226:        case LINE_MACRO:
                    227:                macro_close(f);
                    228:                break;
                    229:        }
1.10    ! schwarze  230:        while (*word != '\0') {
        !           231:                putchar(*word);
        !           232:                if (*word++ == '\\')
        !           233:                        putchar('e');
        !           234:        }
1.2       schwarze  235:        f->linestate = LINE_TEXT;
1.9       schwarze  236:        f->flags = 0;
1.2       schwarze  237: }
                    238:
                    239: /*
                    240:  * Recursively print the content of a node on a text line.
                    241:  */
                    242: void
                    243: print_textnode(struct format *f, struct pnode *n)
                    244: {
                    245:        struct pnode    *nc;
                    246:
1.3       schwarze  247:        if (n->node == NODE_TEXT || n->node == NODE_ESCAPE)
1.2       schwarze  248:                print_text(f, n->b, ARG_SPACE);
                    249:        else
                    250:                TAILQ_FOREACH(nc, &n->childq, child)
                    251:                        print_textnode(f, nc);
1.1       schwarze  252: }

CVSweb