1 |
|
|
/* $OpenBSD: man_macro.c,v 1.85 2017/06/25 07:23:53 bentley Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
4 |
|
|
* Copyright (c) 2012-2015, 2017 Ingo Schwarze <schwarze@openbsd.org> |
5 |
|
|
* Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de> |
6 |
|
|
* |
7 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
8 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
9 |
|
|
* copyright notice and this permission notice appear in all copies. |
10 |
|
|
* |
11 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
12 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
14 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
16 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
17 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 |
|
|
*/ |
19 |
|
|
#include <sys/types.h> |
20 |
|
|
|
21 |
|
|
#include <assert.h> |
22 |
|
|
#include <ctype.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
#include <string.h> |
25 |
|
|
|
26 |
|
|
#include "mandoc.h" |
27 |
|
|
#include "roff.h" |
28 |
|
|
#include "man.h" |
29 |
|
|
#include "libmandoc.h" |
30 |
|
|
#include "roff_int.h" |
31 |
|
|
#include "libman.h" |
32 |
|
|
|
33 |
|
|
static void blk_close(MACRO_PROT_ARGS); |
34 |
|
|
static void blk_exp(MACRO_PROT_ARGS); |
35 |
|
|
static void blk_imp(MACRO_PROT_ARGS); |
36 |
|
|
static void in_line_eoln(MACRO_PROT_ARGS); |
37 |
|
|
static int man_args(struct roff_man *, int, |
38 |
|
|
int *, char *, char **); |
39 |
|
|
static void rew_scope(struct roff_man *, enum roff_tok); |
40 |
|
|
|
41 |
|
|
const struct man_macro __man_macros[MAN_MAX - MAN_TH] = { |
42 |
|
|
{ in_line_eoln, MAN_BSCOPE }, /* TH */ |
43 |
|
|
{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */ |
44 |
|
|
{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */ |
45 |
|
|
{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* TP */ |
46 |
|
|
{ blk_imp, MAN_BSCOPE }, /* LP */ |
47 |
|
|
{ blk_imp, MAN_BSCOPE }, /* PP */ |
48 |
|
|
{ blk_imp, MAN_BSCOPE }, /* P */ |
49 |
|
|
{ blk_imp, MAN_BSCOPE }, /* IP */ |
50 |
|
|
{ blk_imp, MAN_BSCOPE }, /* HP */ |
51 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */ |
52 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */ |
53 |
|
|
{ in_line_eoln, 0 }, /* BI */ |
54 |
|
|
{ in_line_eoln, 0 }, /* IB */ |
55 |
|
|
{ in_line_eoln, 0 }, /* BR */ |
56 |
|
|
{ in_line_eoln, 0 }, /* RB */ |
57 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */ |
58 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */ |
59 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */ |
60 |
|
|
{ in_line_eoln, 0 }, /* IR */ |
61 |
|
|
{ in_line_eoln, 0 }, /* RI */ |
62 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* nf */ |
63 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* fi */ |
64 |
|
|
{ blk_close, MAN_BSCOPE }, /* RE */ |
65 |
|
|
{ blk_exp, MAN_BSCOPE }, /* RS */ |
66 |
|
|
{ in_line_eoln, 0 }, /* DT */ |
67 |
|
|
{ in_line_eoln, 0 }, /* UC */ |
68 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* PD */ |
69 |
|
|
{ in_line_eoln, 0 }, /* AT */ |
70 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* in */ |
71 |
|
|
{ in_line_eoln, 0 }, /* OP */ |
72 |
|
|
{ in_line_eoln, MAN_BSCOPE }, /* EX */ |
73 |
|
|
{ in_line_eoln, MAN_BSCOPE }, /* EE */ |
74 |
|
|
{ blk_exp, MAN_BSCOPE }, /* UR */ |
75 |
|
|
{ blk_close, MAN_BSCOPE }, /* UE */ |
76 |
|
|
{ blk_exp, MAN_BSCOPE }, /* MT */ |
77 |
|
|
{ blk_close, MAN_BSCOPE }, /* ME */ |
78 |
|
|
}; |
79 |
|
|
const struct man_macro *const man_macros = __man_macros - MAN_TH; |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
void |
83 |
|
|
man_unscope(struct roff_man *man, const struct roff_node *to) |
84 |
|
|
{ |
85 |
|
|
struct roff_node *n; |
86 |
|
|
|
87 |
|
1030220 |
to = to->parent; |
88 |
|
515110 |
n = man->last; |
89 |
✓✓ |
3413126 |
while (n != to) { |
90 |
|
|
|
91 |
|
|
/* Reached the end of the document? */ |
92 |
|
|
|
93 |
✓✓✓✓
|
1226227 |
if (to == NULL && ! (n->flags & NODE_VALID)) { |
94 |
✓✓✓✗
|
27926 |
if (man->flags & (MAN_BLINE | MAN_ELINE) && |
95 |
|
144 |
man_macros[n->tok].flags & MAN_SCOPED) { |
96 |
|
144 |
mandoc_vmsg(MANDOCERR_BLK_LINE, |
97 |
|
144 |
man->parse, n->line, n->pos, |
98 |
|
144 |
"EOF breaks %s", roff_name[n->tok]); |
99 |
✓✓ |
144 |
if (man->flags & MAN_ELINE) |
100 |
|
54 |
man->flags &= ~MAN_ELINE; |
101 |
|
|
else { |
102 |
✗✓ |
90 |
assert(n->type == ROFFT_HEAD); |
103 |
|
90 |
n = n->parent; |
104 |
|
|
man->flags &= ~MAN_BLINE; |
105 |
|
|
} |
106 |
|
144 |
man->last = n; |
107 |
|
144 |
n = n->parent; |
108 |
|
144 |
roff_node_delete(man, man->last); |
109 |
|
144 |
continue; |
110 |
|
|
} |
111 |
✓✓✓✓
|
37865 |
if (n->type == ROFFT_BLOCK && |
112 |
|
10227 |
man_macros[n->tok].fp == blk_exp) |
113 |
|
54 |
mandoc_msg(MANDOCERR_BLK_NOEND, |
114 |
|
54 |
man->parse, n->line, n->pos, |
115 |
|
54 |
roff_name[n->tok]); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
/* |
119 |
|
|
* We might delete the man->last node |
120 |
|
|
* in the post-validation phase. |
121 |
|
|
* Save a pointer to the parent such that |
122 |
|
|
* we know where to continue the iteration. |
123 |
|
|
*/ |
124 |
|
|
|
125 |
|
1191381 |
man->last = n; |
126 |
|
1191381 |
n = n->parent; |
127 |
|
1191381 |
man->last->flags |= NODE_VALID; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* |
131 |
|
|
* If we ended up at the parent of the node we were |
132 |
|
|
* supposed to rewind to, that means the target node |
133 |
|
|
* got deleted, so add the next node we parse as a child |
134 |
|
|
* of the parent instead of as a sibling of the target. |
135 |
|
|
*/ |
136 |
|
|
|
137 |
|
515110 |
man->next = (man->last == to) ? |
138 |
|
|
ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING; |
139 |
|
515110 |
} |
140 |
|
|
|
141 |
|
|
/* |
142 |
|
|
* Rewinding entails ascending the parse tree until a coherent point, |
143 |
|
|
* for example, the `SH' macro will close out any intervening `SS' |
144 |
|
|
* scopes. When a scope is closed, it must be validated and actioned. |
145 |
|
|
*/ |
146 |
|
|
static void |
147 |
|
|
rew_scope(struct roff_man *man, enum roff_tok tok) |
148 |
|
|
{ |
149 |
|
|
struct roff_node *n; |
150 |
|
|
|
151 |
|
|
/* Preserve empty paragraphs before RS. */ |
152 |
|
|
|
153 |
|
524450 |
n = man->last; |
154 |
✓✓✓✓ ✗✓ |
278359 |
if (tok == MAN_RS && n->child == NULL && |
155 |
✓✓✓✓
|
17256 |
(n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP)) |
156 |
|
120 |
return; |
157 |
|
|
|
158 |
|
235790 |
for (;;) { |
159 |
✓✓ |
1426365 |
if (n->type == ROFFT_ROOT) |
160 |
|
31567 |
return; |
161 |
✓✓ |
1394798 |
if (n->flags & NODE_VALID) { |
162 |
|
461771 |
n = n->parent; |
163 |
|
461771 |
continue; |
164 |
|
|
} |
165 |
✓✓ |
933027 |
if (n->type != ROFFT_BLOCK) { |
166 |
✓✓ |
466700 |
if (n->parent->type == ROFFT_ROOT) { |
167 |
|
1 |
man_unscope(man, n); |
168 |
|
1 |
return; |
169 |
|
|
} else { |
170 |
|
|
n = n->parent; |
171 |
|
466699 |
continue; |
172 |
|
|
} |
173 |
|
|
} |
174 |
✓✓✓✓ ✓✓ |
1088456 |
if (tok != MAN_SH && (n->tok == MAN_SH || |
175 |
✓✓✓✓
|
640485 |
(tok != MAN_SS && (n->tok == MAN_SS || |
176 |
|
192284 |
man_macros[n->tok].fp == blk_exp)))) |
177 |
|
230537 |
return; |
178 |
|
235790 |
man_unscope(man, n); |
179 |
|
235790 |
n = man->last; |
180 |
|
|
} |
181 |
|
262225 |
} |
182 |
|
|
|
183 |
|
|
|
184 |
|
|
/* |
185 |
|
|
* Close out a generic explicit macro. |
186 |
|
|
*/ |
187 |
|
|
void |
188 |
|
|
blk_close(MACRO_PROT_ARGS) |
189 |
|
|
{ |
190 |
|
|
enum roff_tok ntok; |
191 |
|
|
const struct roff_node *nn; |
192 |
|
21460 |
char *p; |
193 |
|
|
int nrew, target; |
194 |
|
|
|
195 |
|
|
nrew = 1; |
196 |
✓✓✓✗
|
10730 |
switch (tok) { |
197 |
|
|
case MAN_RE: |
198 |
|
|
ntok = MAN_RS; |
199 |
✓✓ |
10478 |
if ( ! man_args(man, line, pos, buf, &p)) |
200 |
|
|
break; |
201 |
✓✓ |
1224 |
for (nn = man->last->parent; nn; nn = nn->parent) |
202 |
✓✓✓✓
|
774 |
if (nn->tok == ntok && nn->type == ROFFT_BLOCK) |
203 |
|
126 |
nrew++; |
204 |
|
90 |
target = strtol(p, &p, 10); |
205 |
✓✗ |
90 |
if (*p != '\0') |
206 |
|
180 |
mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, |
207 |
|
90 |
line, p - buf, "RE ... %s", p); |
208 |
✓✓ |
90 |
if (target == 0) |
209 |
|
18 |
target = 1; |
210 |
|
90 |
nrew -= target; |
211 |
✓✓ |
90 |
if (nrew < 1) { |
212 |
|
18 |
mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse, |
213 |
|
|
line, ppos, "RE %d", target); |
214 |
|
18 |
return; |
215 |
|
|
} |
216 |
|
|
break; |
217 |
|
|
case MAN_UE: |
218 |
|
|
ntok = MAN_UR; |
219 |
|
144 |
break; |
220 |
|
|
case MAN_ME: |
221 |
|
|
ntok = MAN_MT; |
222 |
|
108 |
break; |
223 |
|
|
default: |
224 |
|
|
abort(); |
225 |
|
|
} |
226 |
|
|
|
227 |
✓✓ |
51378 |
for (nn = man->last->parent; nn; nn = nn->parent) |
228 |
✓✓✓✓ ✓✓ |
53553 |
if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew) |
229 |
|
|
break; |
230 |
|
|
|
231 |
✓✓ |
10712 |
if (nn == NULL) { |
232 |
|
336 |
mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse, |
233 |
|
168 |
line, ppos, roff_name[tok]); |
234 |
|
168 |
rew_scope(man, MAN_PP); |
235 |
|
168 |
} else { |
236 |
|
10544 |
line = man->last->line; |
237 |
|
10544 |
ppos = man->last->pos; |
238 |
|
10544 |
ntok = man->last->tok; |
239 |
|
10544 |
man_unscope(man, nn); |
240 |
|
|
|
241 |
✓✓✓✓
|
20908 |
if (tok == MAN_RE && nn->head->aux > 0) |
242 |
|
9147 |
roff_setreg(man->roff, "an-margin", |
243 |
|
|
nn->head->aux, '-'); |
244 |
|
|
|
245 |
|
|
/* Move a trailing paragraph behind the block. */ |
246 |
|
|
|
247 |
✓✓ |
10544 |
if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) { |
248 |
|
45 |
*pos = strlen(buf); |
249 |
|
45 |
blk_imp(man, ntok, line, ppos, pos, buf); |
250 |
|
45 |
} |
251 |
|
|
} |
252 |
|
21442 |
} |
253 |
|
|
|
254 |
|
|
void |
255 |
|
|
blk_exp(MACRO_PROT_ARGS) |
256 |
|
|
{ |
257 |
|
|
struct roff_node *head; |
258 |
|
21412 |
char *p; |
259 |
|
|
int la; |
260 |
|
|
|
261 |
|
10706 |
rew_scope(man, tok); |
262 |
|
10706 |
roff_block_alloc(man, line, ppos, tok); |
263 |
|
10706 |
head = roff_head_alloc(man, line, ppos, tok); |
264 |
|
|
|
265 |
|
10706 |
la = *pos; |
266 |
✓✓ |
10706 |
if (man_args(man, line, pos, buf, &p)) { |
267 |
|
10087 |
roff_word_alloc(man, line, la, p); |
268 |
✓✓ |
10087 |
if (tok == MAN_RS) { |
269 |
✓✓ |
9871 |
if (roff_getreg(man->roff, "an-margin") == 0) |
270 |
|
841 |
roff_setreg(man->roff, "an-margin", |
271 |
|
|
7 * 24, '='); |
272 |
✓✓ |
9871 |
if ((head->aux = strtod(p, NULL) * 24.0) > 0) |
273 |
|
9237 |
roff_setreg(man->roff, "an-margin", |
274 |
|
|
head->aux, '+'); |
275 |
|
|
} |
276 |
|
|
} |
277 |
|
|
|
278 |
✓✓ |
10706 |
if (buf[*pos] != '\0') |
279 |
|
180 |
mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, line, |
280 |
|
90 |
*pos, "%s ... %s", roff_name[tok], buf + *pos); |
281 |
|
|
|
282 |
|
10706 |
man_unscope(man, head); |
283 |
|
10706 |
roff_body_alloc(man, line, ppos, tok); |
284 |
|
10706 |
} |
285 |
|
|
|
286 |
|
|
/* |
287 |
|
|
* Parse an implicit-block macro. These contain a ROFFT_HEAD and a |
288 |
|
|
* ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other |
289 |
|
|
* scopes, such as `SH' closing out an `SS', are defined in the rew |
290 |
|
|
* routines. |
291 |
|
|
*/ |
292 |
|
|
void |
293 |
|
|
blk_imp(MACRO_PROT_ARGS) |
294 |
|
|
{ |
295 |
|
|
int la; |
296 |
|
502702 |
char *p; |
297 |
|
|
struct roff_node *n; |
298 |
|
|
|
299 |
|
251351 |
rew_scope(man, tok); |
300 |
|
251351 |
n = roff_block_alloc(man, line, ppos, tok); |
301 |
✓✓✓✓
|
471341 |
if (n->tok == MAN_SH || n->tok == MAN_SS) |
302 |
|
50033 |
man->flags &= ~MAN_LITERAL; |
303 |
|
251351 |
n = roff_head_alloc(man, line, ppos, tok); |
304 |
|
|
|
305 |
|
|
/* Add line arguments. */ |
306 |
|
|
|
307 |
|
522168 |
for (;;) { |
308 |
|
522168 |
la = *pos; |
309 |
✓✓ |
522168 |
if ( ! man_args(man, line, pos, buf, &p)) |
310 |
|
|
break; |
311 |
|
270817 |
roff_word_alloc(man, line, la, p); |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
/* |
315 |
|
|
* For macros having optional next-line scope, |
316 |
|
|
* keep the head open if there were no arguments. |
317 |
|
|
* For `TP', always keep the head open. |
318 |
|
|
*/ |
319 |
|
|
|
320 |
✓✓✓✓
|
301384 |
if (man_macros[tok].flags & MAN_SCOPED && |
321 |
✓✓ |
108609 |
(tok == MAN_TP || n == man->last)) { |
322 |
|
9200 |
man->flags |= MAN_BLINE; |
323 |
|
9200 |
return; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
/* Close out the head and open the body. */ |
327 |
|
|
|
328 |
|
242151 |
man_unscope(man, n); |
329 |
|
242151 |
roff_body_alloc(man, line, ppos, tok); |
330 |
|
493502 |
} |
331 |
|
|
|
332 |
|
|
void |
333 |
|
|
in_line_eoln(MACRO_PROT_ARGS) |
334 |
|
|
{ |
335 |
|
|
int la; |
336 |
|
219550 |
char *p; |
337 |
|
|
struct roff_node *n; |
338 |
|
|
|
339 |
|
109775 |
roff_elem_alloc(man, line, ppos, tok); |
340 |
|
109775 |
n = man->last; |
341 |
|
|
|
342 |
|
109775 |
for (;;) { |
343 |
✓✓✓✓
|
238007 |
if (buf[*pos] != '\0' && (tok == MAN_fi || tok == MAN_nf)) { |
344 |
|
36 |
mandoc_vmsg(MANDOCERR_ARG_SKIP, |
345 |
|
36 |
man->parse, line, *pos, "%s %s", |
346 |
|
36 |
roff_name[tok], buf + *pos); |
347 |
|
36 |
break; |
348 |
|
|
} |
349 |
✓✓✓✓
|
237935 |
if (buf[*pos] != '\0' && man->last != n && tok == MAN_PD) { |
350 |
|
18 |
mandoc_vmsg(MANDOCERR_ARG_EXCESS, |
351 |
|
18 |
man->parse, line, *pos, "%s ... %s", |
352 |
|
18 |
roff_name[tok], buf + *pos); |
353 |
|
18 |
break; |
354 |
|
|
} |
355 |
|
173810 |
la = *pos; |
356 |
✓✓ |
173810 |
if ( ! man_args(man, line, pos, buf, &p)) |
357 |
|
|
break; |
358 |
✓✓✓✓
|
76768 |
if (man_macros[tok].flags & MAN_JOIN && |
359 |
|
12679 |
man->last->type == ROFFT_TEXT) |
360 |
|
3553 |
roff_word_append(man, p); |
361 |
|
|
else |
362 |
|
60536 |
roff_word_alloc(man, line, la, p); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
/* |
366 |
|
|
* Append NODE_EOS in case the last snipped argument |
367 |
|
|
* ends with a dot, e.g. `.IR syslog (3).' |
368 |
|
|
*/ |
369 |
|
|
|
370 |
✓✓✓✓
|
139959 |
if (n != man->last && |
371 |
|
30184 |
mandoc_eos(man->last->string, strlen(man->last->string))) |
372 |
|
811 |
man->last->flags |= NODE_EOS; |
373 |
|
|
|
374 |
|
|
/* |
375 |
|
|
* If no arguments are specified and this is MAN_SCOPED (i.e., |
376 |
|
|
* next-line scoped), then set our mode to indicate that we're |
377 |
|
|
* waiting for terms to load into our context. |
378 |
|
|
*/ |
379 |
|
|
|
380 |
✓✓✓✓
|
189366 |
if (n == man->last && man_macros[tok].flags & MAN_SCOPED) { |
381 |
✗✓ |
222 |
assert( ! (man_macros[tok].flags & MAN_NSCOPED)); |
382 |
|
222 |
man->flags |= MAN_ELINE; |
383 |
|
222 |
return; |
384 |
|
|
} |
385 |
|
|
|
386 |
✗✓ |
109553 |
assert(man->last->type != ROFFT_ROOT); |
387 |
|
109553 |
man->next = ROFF_NEXT_SIBLING; |
388 |
|
|
|
389 |
|
|
/* Rewind our element scope. */ |
390 |
|
|
|
391 |
✓✗ |
279474 |
for ( ; man->last; man->last = man->last->parent) { |
392 |
|
139737 |
man_state(man, man->last); |
393 |
✓✓ |
139737 |
if (man->last == n) |
394 |
|
|
break; |
395 |
|
|
} |
396 |
|
219328 |
} |
397 |
|
|
|
398 |
|
|
void |
399 |
|
|
man_endparse(struct roff_man *man) |
400 |
|
|
{ |
401 |
|
|
|
402 |
|
14346 |
man_unscope(man, man->first); |
403 |
|
7173 |
man->flags &= ~MAN_LITERAL; |
404 |
|
7173 |
} |
405 |
|
|
|
406 |
|
|
static int |
407 |
|
|
man_args(struct roff_man *man, int line, int *pos, char *buf, char **v) |
408 |
|
|
{ |
409 |
|
|
char *start; |
410 |
|
|
|
411 |
✗✓ |
1434324 |
assert(*pos); |
412 |
|
717162 |
*v = start = buf + *pos; |
413 |
✗✓ |
717162 |
assert(' ' != *start); |
414 |
|
|
|
415 |
✓✓ |
717162 |
if ('\0' == *start) |
416 |
|
372079 |
return 0; |
417 |
|
|
|
418 |
|
345083 |
*v = mandoc_getarg(man->parse, v, line, pos); |
419 |
|
345083 |
return 1; |
420 |
|
717162 |
} |