]> git.cameronkatri.com Git - mandoc.git/blob - roff_term.c
Provide a real feature test for __attribute__().
[mandoc.git] / roff_term.c
1 /* $Id: roff_term.c,v 1.20 2020/06/22 19:20:40 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010,2014,2015,2017-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 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.
16 */
17 #include "config.h"
18
19 #include <sys/types.h>
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "mandoc.h"
26 #include "roff.h"
27 #include "out.h"
28 #include "term.h"
29
30 #define ROFF_TERM_ARGS struct termp *p, const struct roff_node *n
31
32 typedef void (*roff_term_pre_fp)(ROFF_TERM_ARGS);
33
34 static void roff_term_pre_br(ROFF_TERM_ARGS);
35 static void roff_term_pre_ce(ROFF_TERM_ARGS);
36 static void roff_term_pre_ft(ROFF_TERM_ARGS);
37 static void roff_term_pre_ll(ROFF_TERM_ARGS);
38 static void roff_term_pre_mc(ROFF_TERM_ARGS);
39 static void roff_term_pre_po(ROFF_TERM_ARGS);
40 static void roff_term_pre_sp(ROFF_TERM_ARGS);
41 static void roff_term_pre_ta(ROFF_TERM_ARGS);
42 static void roff_term_pre_ti(ROFF_TERM_ARGS);
43
44 static const roff_term_pre_fp roff_term_pre_acts[ROFF_MAX] = {
45 roff_term_pre_br, /* br */
46 roff_term_pre_ce, /* ce */
47 roff_term_pre_br, /* fi */
48 roff_term_pre_ft, /* ft */
49 roff_term_pre_ll, /* ll */
50 roff_term_pre_mc, /* mc */
51 roff_term_pre_br, /* nf */
52 roff_term_pre_po, /* po */
53 roff_term_pre_ce, /* rj */
54 roff_term_pre_sp, /* sp */
55 roff_term_pre_ta, /* ta */
56 roff_term_pre_ti, /* ti */
57 };
58
59
60 void
61 roff_term_pre(struct termp *p, const struct roff_node *n)
62 {
63 assert(n->tok < ROFF_MAX);
64 (*roff_term_pre_acts[n->tok])(p, n);
65 }
66
67 static void
68 roff_term_pre_br(ROFF_TERM_ARGS)
69 {
70 term_newln(p);
71 if (p->flags & TERMP_BRIND) {
72 p->tcol->offset = p->tcol->rmargin;
73 p->tcol->rmargin = p->maxrmargin;
74 p->trailspace = 0;
75 p->flags &= ~(TERMP_NOBREAK | TERMP_BRIND);
76 p->flags |= TERMP_NOSPACE;
77 }
78 }
79
80 static void
81 roff_term_pre_ce(ROFF_TERM_ARGS)
82 {
83 const struct roff_node *nc1, *nc2;
84
85 roff_term_pre_br(p, n);
86 p->flags |= n->tok == ROFF_ce ? TERMP_CENTER : TERMP_RIGHT;
87 nc1 = n->child->next;
88 while (nc1 != NULL) {
89 nc2 = nc1;
90 do {
91 nc2 = nc2->next;
92 } while (nc2 != NULL && (nc2->type != ROFFT_TEXT ||
93 (nc2->flags & NODE_LINE) == 0));
94 while (nc1 != nc2) {
95 if (nc1->type == ROFFT_TEXT)
96 term_word(p, nc1->string);
97 else
98 roff_term_pre(p, nc1);
99 nc1 = nc1->next;
100 }
101 p->flags |= TERMP_NOSPACE;
102 term_flushln(p);
103 }
104 p->flags &= ~(TERMP_CENTER | TERMP_RIGHT);
105 }
106
107 static void
108 roff_term_pre_ft(ROFF_TERM_ARGS)
109 {
110 const char *cp;
111
112 cp = n->child->string;
113 switch (mandoc_font(cp, (int)strlen(cp))) {
114 case ESCAPE_FONTBOLD:
115 term_fontrepl(p, TERMFONT_BOLD);
116 break;
117 case ESCAPE_FONTITALIC:
118 term_fontrepl(p, TERMFONT_UNDER);
119 break;
120 case ESCAPE_FONTBI:
121 term_fontrepl(p, TERMFONT_BI);
122 break;
123 case ESCAPE_FONTPREV:
124 term_fontlast(p);
125 break;
126 case ESCAPE_FONTROMAN:
127 case ESCAPE_FONTCW:
128 term_fontrepl(p, TERMFONT_NONE);
129 break;
130 default:
131 break;
132 }
133 }
134
135 static void
136 roff_term_pre_ll(ROFF_TERM_ARGS)
137 {
138 term_setwidth(p, n->child != NULL ? n->child->string : NULL);
139 }
140
141 static void
142 roff_term_pre_mc(ROFF_TERM_ARGS)
143 {
144 if (p->col) {
145 p->flags |= TERMP_NOBREAK;
146 term_flushln(p);
147 p->flags &= ~(TERMP_NOBREAK | TERMP_NOSPACE);
148 }
149 if (n->child != NULL) {
150 p->mc = n->child->string;
151 p->flags |= TERMP_NEWMC;
152 } else
153 p->flags |= TERMP_ENDMC;
154 }
155
156 static void
157 roff_term_pre_po(ROFF_TERM_ARGS)
158 {
159 struct roffsu su;
160 static int po, polast;
161 int ponew;
162
163 if (n->child != NULL &&
164 a2roffsu(n->child->string, &su, SCALE_EM) != NULL) {
165 ponew = term_hen(p, &su);
166 if (*n->child->string == '+' ||
167 *n->child->string == '-')
168 ponew += po;
169 } else
170 ponew = polast;
171 polast = po;
172 po = ponew;
173
174 ponew = po - polast + (int)p->tcol->offset;
175 p->tcol->offset = ponew > 0 ? ponew : 0;
176 }
177
178 static void
179 roff_term_pre_sp(ROFF_TERM_ARGS)
180 {
181 struct roffsu su;
182 int len;
183
184 if (n->child != NULL) {
185 if (a2roffsu(n->child->string, &su, SCALE_VS) == NULL)
186 su.scale = 1.0;
187 len = term_vspan(p, &su);
188 } else
189 len = 1;
190
191 if (len < 0)
192 p->skipvsp -= len;
193 else
194 while (len--)
195 term_vspace(p);
196
197 roff_term_pre_br(p, n);
198 }
199
200 static void
201 roff_term_pre_ta(ROFF_TERM_ARGS)
202 {
203 term_tab_set(p, NULL);
204 for (n = n->child; n != NULL; n = n->next)
205 term_tab_set(p, n->string);
206 }
207
208 static void
209 roff_term_pre_ti(ROFF_TERM_ARGS)
210 {
211 struct roffsu su;
212 const char *cp;
213 int len, sign;
214
215 roff_term_pre_br(p, n);
216
217 if (n->child == NULL)
218 return;
219 cp = n->child->string;
220 if (*cp == '+') {
221 sign = 1;
222 cp++;
223 } else if (*cp == '-') {
224 sign = -1;
225 cp++;
226 } else
227 sign = 0;
228
229 if (a2roffsu(cp, &su, SCALE_EM) == NULL)
230 return;
231 len = term_hen(p, &su);
232
233 if (sign == 0) {
234 p->ti = len - p->tcol->offset;
235 p->tcol->offset = len;
236 } else if (sign == 1) {
237 p->ti = len;
238 p->tcol->offset += len;
239 } else if ((size_t)len < p->tcol->offset) {
240 p->ti = -len;
241 p->tcol->offset -= len;
242 } else {
243 p->ti = -p->tcol->offset;
244 p->tcol->offset = 0;
245 }
246 }