1 |
|
|
/* $OpenBSD: man_macro.c,v 1.76 2016/01/08 17:48:04 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
4 |
|
|
* Copyright (c) 2012, 2013, 2014, 2015 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 *, int); |
40 |
|
|
|
41 |
|
|
const struct man_macro __man_macros[MAN_MAX] = { |
42 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* br */ |
43 |
|
|
{ in_line_eoln, MAN_BSCOPE }, /* TH */ |
44 |
|
|
{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */ |
45 |
|
|
{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */ |
46 |
|
|
{ blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* TP */ |
47 |
|
|
{ blk_imp, MAN_BSCOPE }, /* LP */ |
48 |
|
|
{ blk_imp, MAN_BSCOPE }, /* PP */ |
49 |
|
|
{ blk_imp, MAN_BSCOPE }, /* P */ |
50 |
|
|
{ blk_imp, MAN_BSCOPE }, /* IP */ |
51 |
|
|
{ blk_imp, MAN_BSCOPE }, /* HP */ |
52 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */ |
53 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */ |
54 |
|
|
{ in_line_eoln, 0 }, /* BI */ |
55 |
|
|
{ in_line_eoln, 0 }, /* IB */ |
56 |
|
|
{ in_line_eoln, 0 }, /* BR */ |
57 |
|
|
{ in_line_eoln, 0 }, /* RB */ |
58 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */ |
59 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */ |
60 |
|
|
{ in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */ |
61 |
|
|
{ in_line_eoln, 0 }, /* IR */ |
62 |
|
|
{ in_line_eoln, 0 }, /* RI */ |
63 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* sp */ |
64 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* nf */ |
65 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* fi */ |
66 |
|
|
{ blk_close, MAN_BSCOPE }, /* RE */ |
67 |
|
|
{ blk_exp, MAN_BSCOPE }, /* RS */ |
68 |
|
|
{ in_line_eoln, 0 }, /* DT */ |
69 |
|
|
{ in_line_eoln, 0 }, /* UC */ |
70 |
|
|
{ in_line_eoln, MAN_NSCOPED }, /* PD */ |
71 |
|
|
{ in_line_eoln, 0 }, /* AT */ |
72 |
|
|
{ in_line_eoln, 0 }, /* in */ |
73 |
|
|
{ in_line_eoln, 0 }, /* ft */ |
74 |
|
|
{ in_line_eoln, 0 }, /* OP */ |
75 |
|
|
{ in_line_eoln, MAN_BSCOPE }, /* EX */ |
76 |
|
|
{ in_line_eoln, MAN_BSCOPE }, /* EE */ |
77 |
|
|
{ blk_exp, MAN_BSCOPE }, /* UR */ |
78 |
|
|
{ blk_close, MAN_BSCOPE }, /* UE */ |
79 |
|
|
{ in_line_eoln, 0 }, /* ll */ |
80 |
|
|
}; |
81 |
|
|
|
82 |
|
|
const struct man_macro * const man_macros = __man_macros; |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
void |
86 |
|
|
man_unscope(struct roff_man *man, const struct roff_node *to) |
87 |
|
|
{ |
88 |
|
|
struct roff_node *n; |
89 |
|
|
|
90 |
|
|
to = to->parent; |
91 |
|
|
n = man->last; |
92 |
|
|
while (n != to) { |
93 |
|
|
|
94 |
|
|
/* Reached the end of the document? */ |
95 |
|
|
|
96 |
|
|
if (to == NULL && ! (n->flags & MAN_VALID)) { |
97 |
|
|
if (man->flags & (MAN_BLINE | MAN_ELINE) && |
98 |
|
|
man_macros[n->tok].flags & MAN_SCOPED) { |
99 |
|
|
mandoc_vmsg(MANDOCERR_BLK_LINE, |
100 |
|
|
man->parse, n->line, n->pos, |
101 |
|
|
"EOF breaks %s", |
102 |
|
|
man_macronames[n->tok]); |
103 |
|
|
if (man->flags & MAN_ELINE) |
104 |
|
|
man->flags &= ~MAN_ELINE; |
105 |
|
|
else { |
106 |
|
|
assert(n->type == ROFFT_HEAD); |
107 |
|
|
n = n->parent; |
108 |
|
|
man->flags &= ~MAN_BLINE; |
109 |
|
|
} |
110 |
|
|
man->last = n; |
111 |
|
|
n = n->parent; |
112 |
|
|
roff_node_delete(man, man->last); |
113 |
|
|
continue; |
114 |
|
|
} |
115 |
|
|
if (n->type == ROFFT_BLOCK && |
116 |
|
|
man_macros[n->tok].fp == blk_exp) |
117 |
|
|
mandoc_msg(MANDOCERR_BLK_NOEND, |
118 |
|
|
man->parse, n->line, n->pos, |
119 |
|
|
man_macronames[n->tok]); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/* |
123 |
|
|
* We might delete the man->last node |
124 |
|
|
* in the post-validation phase. |
125 |
|
|
* Save a pointer to the parent such that |
126 |
|
|
* we know where to continue the iteration. |
127 |
|
|
*/ |
128 |
|
|
|
129 |
|
|
man->last = n; |
130 |
|
|
n = n->parent; |
131 |
|
|
man->last->flags |= MAN_VALID; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
/* |
135 |
|
|
* If we ended up at the parent of the node we were |
136 |
|
|
* supposed to rewind to, that means the target node |
137 |
|
|
* got deleted, so add the next node we parse as a child |
138 |
|
|
* of the parent instead of as a sibling of the target. |
139 |
|
|
*/ |
140 |
|
|
|
141 |
|
|
man->next = (man->last == to) ? |
142 |
|
|
ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
/* |
146 |
|
|
* Rewinding entails ascending the parse tree until a coherent point, |
147 |
|
|
* for example, the `SH' macro will close out any intervening `SS' |
148 |
|
|
* scopes. When a scope is closed, it must be validated and actioned. |
149 |
|
|
*/ |
150 |
|
|
static void |
151 |
|
|
rew_scope(struct roff_man *man, int tok) |
152 |
|
|
{ |
153 |
|
|
struct roff_node *n; |
154 |
|
|
|
155 |
|
|
/* Preserve empty paragraphs before RS. */ |
156 |
|
|
|
157 |
|
|
n = man->last; |
158 |
|
|
if (tok == MAN_RS && n->child == NULL && |
159 |
|
|
(n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP)) |
160 |
|
|
return; |
161 |
|
|
|
162 |
|
|
for (;;) { |
163 |
|
|
if (n->type == ROFFT_ROOT) |
164 |
|
|
return; |
165 |
|
|
if (n->flags & MAN_VALID) { |
166 |
|
|
n = n->parent; |
167 |
|
|
continue; |
168 |
|
|
} |
169 |
|
|
if (n->type != ROFFT_BLOCK) { |
170 |
|
|
if (n->parent->type == ROFFT_ROOT) { |
171 |
|
|
man_unscope(man, n); |
172 |
|
|
return; |
173 |
|
|
} else { |
174 |
|
|
n = n->parent; |
175 |
|
|
continue; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
if (tok != MAN_SH && (n->tok == MAN_SH || |
179 |
|
|
(tok != MAN_SS && (n->tok == MAN_SS || |
180 |
|
|
man_macros[n->tok].fp == blk_exp)))) |
181 |
|
|
return; |
182 |
|
|
man_unscope(man, n); |
183 |
|
|
n = man->last; |
184 |
|
|
} |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
|
188 |
|
|
/* |
189 |
|
|
* Close out a generic explicit macro. |
190 |
|
|
*/ |
191 |
|
|
void |
192 |
|
|
blk_close(MACRO_PROT_ARGS) |
193 |
|
|
{ |
194 |
|
|
int ntok; |
195 |
|
|
const struct roff_node *nn; |
196 |
|
|
char *p; |
197 |
|
|
int nrew, target; |
198 |
|
|
|
199 |
|
|
nrew = 1; |
200 |
|
|
switch (tok) { |
201 |
|
|
case MAN_RE: |
202 |
|
|
ntok = MAN_RS; |
203 |
|
|
if ( ! man_args(man, line, pos, buf, &p)) |
204 |
|
|
break; |
205 |
|
|
for (nn = man->last->parent; nn; nn = nn->parent) |
206 |
|
|
if (nn->tok == ntok && nn->type == ROFFT_BLOCK) |
207 |
|
|
nrew++; |
208 |
|
|
target = strtol(p, &p, 10); |
209 |
|
|
if (*p != '\0') |
210 |
|
|
mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, |
211 |
|
|
line, p - buf, "RE ... %s", p); |
212 |
|
|
if (target == 0) |
213 |
|
|
target = 1; |
214 |
|
|
nrew -= target; |
215 |
|
|
if (nrew < 1) { |
216 |
|
|
mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse, |
217 |
|
|
line, ppos, "RE %d", target); |
218 |
|
|
return; |
219 |
|
|
} |
220 |
|
|
break; |
221 |
|
|
case MAN_UE: |
222 |
|
|
ntok = MAN_UR; |
223 |
|
|
break; |
224 |
|
|
default: |
225 |
|
|
abort(); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
for (nn = man->last->parent; nn; nn = nn->parent) |
229 |
|
|
if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew) |
230 |
|
|
break; |
231 |
|
|
|
232 |
|
|
if (nn == NULL) { |
233 |
|
|
mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse, |
234 |
|
|
line, ppos, man_macronames[tok]); |
235 |
|
|
rew_scope(man, MAN_PP); |
236 |
|
|
} else { |
237 |
|
|
line = man->last->line; |
238 |
|
|
ppos = man->last->pos; |
239 |
|
|
ntok = man->last->tok; |
240 |
|
|
man_unscope(man, nn); |
241 |
|
|
|
242 |
|
|
/* Move a trailing paragraph behind the block. */ |
243 |
|
|
|
244 |
|
|
if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) { |
245 |
|
|
*pos = strlen(buf); |
246 |
|
|
blk_imp(man, ntok, line, ppos, pos, buf); |
247 |
|
|
} |
248 |
|
|
} |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
void |
252 |
|
|
blk_exp(MACRO_PROT_ARGS) |
253 |
|
|
{ |
254 |
|
|
struct roff_node *head; |
255 |
|
|
char *p; |
256 |
|
|
int la; |
257 |
|
|
|
258 |
|
|
rew_scope(man, tok); |
259 |
|
|
roff_block_alloc(man, line, ppos, tok); |
260 |
|
|
head = roff_head_alloc(man, line, ppos, tok); |
261 |
|
|
|
262 |
|
|
la = *pos; |
263 |
|
|
if (man_args(man, line, pos, buf, &p)) |
264 |
|
|
roff_word_alloc(man, line, la, p); |
265 |
|
|
|
266 |
|
|
if (buf[*pos] != '\0') |
267 |
|
|
mandoc_vmsg(MANDOCERR_ARG_EXCESS, |
268 |
|
|
man->parse, line, *pos, "%s ... %s", |
269 |
|
|
man_macronames[tok], buf + *pos); |
270 |
|
|
|
271 |
|
|
man_unscope(man, head); |
272 |
|
|
roff_body_alloc(man, line, ppos, tok); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
/* |
276 |
|
|
* Parse an implicit-block macro. These contain a ROFFT_HEAD and a |
277 |
|
|
* ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other |
278 |
|
|
* scopes, such as `SH' closing out an `SS', are defined in the rew |
279 |
|
|
* routines. |
280 |
|
|
*/ |
281 |
|
|
void |
282 |
|
|
blk_imp(MACRO_PROT_ARGS) |
283 |
|
|
{ |
284 |
|
|
int la; |
285 |
|
|
char *p; |
286 |
|
|
struct roff_node *n; |
287 |
|
|
|
288 |
|
|
rew_scope(man, tok); |
289 |
|
|
n = roff_block_alloc(man, line, ppos, tok); |
290 |
|
|
if (n->tok == MAN_SH || n->tok == MAN_SS) |
291 |
|
|
man->flags &= ~MAN_LITERAL; |
292 |
|
|
n = roff_head_alloc(man, line, ppos, tok); |
293 |
|
|
|
294 |
|
|
/* Add line arguments. */ |
295 |
|
|
|
296 |
|
|
for (;;) { |
297 |
|
|
la = *pos; |
298 |
|
|
if ( ! man_args(man, line, pos, buf, &p)) |
299 |
|
|
break; |
300 |
|
|
roff_word_alloc(man, line, la, p); |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
/* |
304 |
|
|
* For macros having optional next-line scope, |
305 |
|
|
* keep the head open if there were no arguments. |
306 |
|
|
* For `TP', always keep the head open. |
307 |
|
|
*/ |
308 |
|
|
|
309 |
|
|
if (man_macros[tok].flags & MAN_SCOPED && |
310 |
|
|
(tok == MAN_TP || n == man->last)) { |
311 |
|
|
man->flags |= MAN_BLINE; |
312 |
|
|
return; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
/* Close out the head and open the body. */ |
316 |
|
|
|
317 |
|
|
man_unscope(man, n); |
318 |
|
|
roff_body_alloc(man, line, ppos, tok); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
void |
322 |
|
|
in_line_eoln(MACRO_PROT_ARGS) |
323 |
|
|
{ |
324 |
|
|
int la; |
325 |
|
|
char *p; |
326 |
|
|
struct roff_node *n; |
327 |
|
|
|
328 |
|
|
roff_elem_alloc(man, line, ppos, tok); |
329 |
|
|
n = man->last; |
330 |
|
|
|
331 |
|
|
for (;;) { |
332 |
|
|
if (buf[*pos] != '\0' && (tok == MAN_br || |
333 |
|
|
tok == MAN_fi || tok == MAN_nf)) { |
334 |
|
|
mandoc_vmsg(MANDOCERR_ARG_SKIP, |
335 |
|
|
man->parse, line, *pos, "%s %s", |
336 |
|
|
man_macronames[tok], buf + *pos); |
337 |
|
|
break; |
338 |
|
|
} |
339 |
|
|
if (buf[*pos] != '\0' && man->last != n && |
340 |
|
|
(tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) { |
341 |
|
|
mandoc_vmsg(MANDOCERR_ARG_EXCESS, |
342 |
|
|
man->parse, line, *pos, "%s ... %s", |
343 |
|
|
man_macronames[tok], buf + *pos); |
344 |
|
|
break; |
345 |
|
|
} |
346 |
|
|
la = *pos; |
347 |
|
|
if ( ! man_args(man, line, pos, buf, &p)) |
348 |
|
|
break; |
349 |
|
|
if (man_macros[tok].flags & MAN_JOIN && |
350 |
|
|
man->last->type == ROFFT_TEXT) |
351 |
|
|
roff_word_append(man, p); |
352 |
|
|
else |
353 |
|
|
roff_word_alloc(man, line, la, p); |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
/* |
357 |
|
|
* Append MAN_EOS in case the last snipped argument |
358 |
|
|
* ends with a dot, e.g. `.IR syslog (3).' |
359 |
|
|
*/ |
360 |
|
|
|
361 |
|
|
if (n != man->last && |
362 |
|
|
mandoc_eos(man->last->string, strlen(man->last->string))) |
363 |
|
|
man->last->flags |= MAN_EOS; |
364 |
|
|
|
365 |
|
|
/* |
366 |
|
|
* If no arguments are specified and this is MAN_SCOPED (i.e., |
367 |
|
|
* next-line scoped), then set our mode to indicate that we're |
368 |
|
|
* waiting for terms to load into our context. |
369 |
|
|
*/ |
370 |
|
|
|
371 |
|
|
if (n == man->last && man_macros[tok].flags & MAN_SCOPED) { |
372 |
|
|
assert( ! (man_macros[tok].flags & MAN_NSCOPED)); |
373 |
|
|
man->flags |= MAN_ELINE; |
374 |
|
|
return; |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
assert(man->last->type != ROFFT_ROOT); |
378 |
|
|
man->next = ROFF_NEXT_SIBLING; |
379 |
|
|
|
380 |
|
|
/* Rewind our element scope. */ |
381 |
|
|
|
382 |
|
|
for ( ; man->last; man->last = man->last->parent) { |
383 |
|
|
man_state(man, man->last); |
384 |
|
|
if (man->last == n) |
385 |
|
|
break; |
386 |
|
|
} |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
void |
390 |
|
|
man_endparse(struct roff_man *man) |
391 |
|
|
{ |
392 |
|
|
|
393 |
|
|
man_unscope(man, man->first); |
394 |
|
|
man->flags &= ~MAN_LITERAL; |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
static int |
398 |
|
|
man_args(struct roff_man *man, int line, int *pos, char *buf, char **v) |
399 |
|
|
{ |
400 |
|
|
char *start; |
401 |
|
|
|
402 |
|
|
assert(*pos); |
403 |
|
|
*v = start = buf + *pos; |
404 |
|
|
assert(' ' != *start); |
405 |
|
|
|
406 |
|
|
if ('\0' == *start) |
407 |
|
|
return 0; |
408 |
|
|
|
409 |
|
|
*v = mandoc_getarg(man->parse, v, line, pos); |
410 |
|
|
return 1; |
411 |
|
|
} |