Annotation of docbook2mdoc/macro.c, Revision 1.12
1.12 ! schwarze 1: /* $Id: macro.c,v 1.11 2019/04/12 09:01:48 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:
1.12 ! schwarze 115: if ((flags & (ARG_QUOTED | ARG_UPPER)) == 0 &&
1.1 schwarze 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] == ' ' ||
1.12 ! schwarze 120: ((cp[3] == '\0' || cp[3] == ' ') &&
! 121: (strncmp(cp, "Brq", 3) == 0 ||
! 122: strncmp(cp, "Bro", 3) == 0 ||
! 123: strncmp(cp, "Brc", 3) == 0 ||
! 124: strncmp(cp, "Bsx", 3) == 0))))
1.1 schwarze 125: fputs("\\&", stdout);
126:
127: if (*cp == '"')
128: fputs("\\(dq", stdout);
129: else if (flags & ARG_UPPER)
130: putchar(toupper((unsigned char)*cp));
131: else
132: putchar(*cp);
133: if (*cp == '\\')
134: putchar('e');
135: }
136: }
137:
138: void
139: macro_argline(struct format *f, const char *name, const char *arg)
140: {
141: macro_open(f, name);
142: macro_addarg(f, arg, ARG_SPACE);
143: macro_close(f);
144: }
145:
146: /*
147: * Recursively append text from the children of a node to a macro line.
148: */
149: void
1.8 schwarze 150: macro_addnode(struct format *f, struct pnode *n, int flags)
1.1 schwarze 151: {
1.5 schwarze 152: struct pnode *nc;
1.1 schwarze 153: int quote_now;
154:
155: assert(f->linestate == LINE_MACRO);
156:
157: /*
1.2 schwarze 158: * If this node or its only child is a text node, just add
159: * that text, letting macro_addarg() decide about quoting.
1.1 schwarze 160: */
161:
1.8 schwarze 162: while ((nc = TAILQ_FIRST(&n->childq)) != NULL &&
1.5 schwarze 163: TAILQ_NEXT(nc, child) == NULL)
1.8 schwarze 164: n = nc;
1.5 schwarze 165:
1.8 schwarze 166: if (n->node == NODE_TEXT || n->node == NODE_ESCAPE) {
167: macro_addarg(f, n->b, flags);
1.1 schwarze 168: return;
169: }
170:
171: /*
172: * If we want the argument quoted and are not already
173: * in a quoted context, quote now.
174: */
175:
176: quote_now = 0;
177: if (flags & ARG_SINGLE) {
178: if ((flags & ARG_QUOTED) == 0) {
179: if (flags & ARG_SPACE) {
180: putchar(' ');
181: flags &= ~ARG_SPACE;
182: }
183: putchar('"');
184: flags |= ARG_QUOTED;
185: quote_now = 1;
186: }
187: flags &= ~ARG_SINGLE;
188: }
189:
190: /*
191: * Iterate to child and sibling nodes,
192: * inserting whitespace between nodes.
193: */
194:
1.5 schwarze 195: while (nc != NULL) {
196: macro_addnode(f, nc, flags);
197: nc = TAILQ_NEXT(nc, child);
1.1 schwarze 198: flags |= ARG_SPACE;
199: }
200: if (quote_now)
201: putchar('"');
202: }
203:
204: void
1.8 schwarze 205: macro_nodeline(struct format *f, const char *name, struct pnode *n, int flags)
1.1 schwarze 206: {
207: macro_open(f, name);
1.8 schwarze 208: macro_addnode(f, n, ARG_SPACE | flags);
1.1 schwarze 209: macro_close(f);
1.2 schwarze 210: }
211:
212:
213: /*
214: * Print a word on the current text line if one is open, or on a new text
215: * line otherwise. The flag ARG_SPACE inserts spaces between words.
216: */
217: void
1.10 schwarze 218: print_text(struct format *f, const char *word, int flags)
219: {
1.2 schwarze 220: switch (f->linestate) {
221: case LINE_NEW:
222: break;
223: case LINE_TEXT:
224: if (flags & ARG_SPACE)
225: putchar(' ');
226: break;
227: case LINE_MACRO:
228: macro_close(f);
229: break;
230: }
1.11 schwarze 231: if (f->linestate == LINE_NEW && (*word == '.' || *word == '\''))
232: fputs("\\&", stdout);
1.10 schwarze 233: while (*word != '\0') {
234: putchar(*word);
235: if (*word++ == '\\')
236: putchar('e');
237: }
1.2 schwarze 238: f->linestate = LINE_TEXT;
1.9 schwarze 239: f->flags = 0;
1.2 schwarze 240: }
241:
242: /*
243: * Recursively print the content of a node on a text line.
244: */
245: void
246: print_textnode(struct format *f, struct pnode *n)
247: {
248: struct pnode *nc;
249:
1.3 schwarze 250: if (n->node == NODE_TEXT || n->node == NODE_ESCAPE)
1.2 schwarze 251: print_text(f, n->b, ARG_SPACE);
252: else
253: TAILQ_FOREACH(nc, &n->childq, child)
254: print_textnode(f, nc);
1.1 schwarze 255: }
CVSweb