1 /* $Id: man_macro.c,v 1.114 2016/01/08 17:48:09 schwarze Exp $ */
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>
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.
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.
21 #include <sys/types.h>
31 #include "libmandoc.h"
35 static void blk_close(MACRO_PROT_ARGS
);
36 static void blk_exp(MACRO_PROT_ARGS
);
37 static void blk_imp(MACRO_PROT_ARGS
);
38 static void in_line_eoln(MACRO_PROT_ARGS
);
39 static int man_args(struct roff_man
*, int,
40 int *, char *, char **);
41 static void rew_scope(struct roff_man
*, int);
43 const struct man_macro __man_macros
[MAN_MAX
] = {
44 { in_line_eoln
, MAN_NSCOPED
}, /* br */
45 { in_line_eoln
, MAN_BSCOPE
}, /* TH */
46 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SH */
47 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SS */
48 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* TP */
49 { blk_imp
, MAN_BSCOPE
}, /* LP */
50 { blk_imp
, MAN_BSCOPE
}, /* PP */
51 { blk_imp
, MAN_BSCOPE
}, /* P */
52 { blk_imp
, MAN_BSCOPE
}, /* IP */
53 { blk_imp
, MAN_BSCOPE
}, /* HP */
54 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* SM */
55 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* SB */
56 { in_line_eoln
, 0 }, /* BI */
57 { in_line_eoln
, 0 }, /* IB */
58 { in_line_eoln
, 0 }, /* BR */
59 { in_line_eoln
, 0 }, /* RB */
60 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* R */
61 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* B */
62 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* I */
63 { in_line_eoln
, 0 }, /* IR */
64 { in_line_eoln
, 0 }, /* RI */
65 { in_line_eoln
, MAN_NSCOPED
}, /* sp */
66 { in_line_eoln
, MAN_NSCOPED
}, /* nf */
67 { in_line_eoln
, MAN_NSCOPED
}, /* fi */
68 { blk_close
, MAN_BSCOPE
}, /* RE */
69 { blk_exp
, MAN_BSCOPE
}, /* RS */
70 { in_line_eoln
, 0 }, /* DT */
71 { in_line_eoln
, 0 }, /* UC */
72 { in_line_eoln
, MAN_NSCOPED
}, /* PD */
73 { in_line_eoln
, 0 }, /* AT */
74 { in_line_eoln
, 0 }, /* in */
75 { in_line_eoln
, 0 }, /* ft */
76 { in_line_eoln
, 0 }, /* OP */
77 { in_line_eoln
, MAN_BSCOPE
}, /* EX */
78 { in_line_eoln
, MAN_BSCOPE
}, /* EE */
79 { blk_exp
, MAN_BSCOPE
}, /* UR */
80 { blk_close
, MAN_BSCOPE
}, /* UE */
81 { in_line_eoln
, 0 }, /* ll */
84 const struct man_macro
* const man_macros
= __man_macros
;
88 man_unscope(struct roff_man
*man
, const struct roff_node
*to
)
96 /* Reached the end of the document? */
98 if (to
== NULL
&& ! (n
->flags
& MAN_VALID
)) {
99 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
100 man_macros
[n
->tok
].flags
& MAN_SCOPED
) {
101 mandoc_vmsg(MANDOCERR_BLK_LINE
,
102 man
->parse
, n
->line
, n
->pos
,
104 man_macronames
[n
->tok
]);
105 if (man
->flags
& MAN_ELINE
)
106 man
->flags
&= ~MAN_ELINE
;
108 assert(n
->type
== ROFFT_HEAD
);
110 man
->flags
&= ~MAN_BLINE
;
114 roff_node_delete(man
, man
->last
);
117 if (n
->type
== ROFFT_BLOCK
&&
118 man_macros
[n
->tok
].fp
== blk_exp
)
119 mandoc_msg(MANDOCERR_BLK_NOEND
,
120 man
->parse
, n
->line
, n
->pos
,
121 man_macronames
[n
->tok
]);
125 * We might delete the man->last node
126 * in the post-validation phase.
127 * Save a pointer to the parent such that
128 * we know where to continue the iteration.
133 man
->last
->flags
|= MAN_VALID
;
137 * If we ended up at the parent of the node we were
138 * supposed to rewind to, that means the target node
139 * got deleted, so add the next node we parse as a child
140 * of the parent instead of as a sibling of the target.
143 man
->next
= (man
->last
== to
) ?
144 ROFF_NEXT_CHILD
: ROFF_NEXT_SIBLING
;
148 * Rewinding entails ascending the parse tree until a coherent point,
149 * for example, the `SH' macro will close out any intervening `SS'
150 * scopes. When a scope is closed, it must be validated and actioned.
153 rew_scope(struct roff_man
*man
, int tok
)
157 /* Preserve empty paragraphs before RS. */
160 if (tok
== MAN_RS
&& n
->child
== NULL
&&
161 (n
->tok
== MAN_P
|| n
->tok
== MAN_PP
|| n
->tok
== MAN_LP
))
165 if (n
->type
== ROFFT_ROOT
)
167 if (n
->flags
& MAN_VALID
) {
171 if (n
->type
!= ROFFT_BLOCK
) {
172 if (n
->parent
->type
== ROFFT_ROOT
) {
180 if (tok
!= MAN_SH
&& (n
->tok
== MAN_SH
||
181 (tok
!= MAN_SS
&& (n
->tok
== MAN_SS
||
182 man_macros
[n
->tok
].fp
== blk_exp
))))
191 * Close out a generic explicit macro.
194 blk_close(MACRO_PROT_ARGS
)
197 const struct roff_node
*nn
;
205 if ( ! man_args(man
, line
, pos
, buf
, &p
))
207 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
208 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
210 target
= strtol(p
, &p
, 10);
212 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
213 line
, p
- buf
, "RE ... %s", p
);
218 mandoc_vmsg(MANDOCERR_RE_NOTOPEN
, man
->parse
,
219 line
, ppos
, "RE %d", target
);
230 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
231 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
&& ! --nrew
)
235 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, man
->parse
,
236 line
, ppos
, man_macronames
[tok
]);
237 rew_scope(man
, MAN_PP
);
239 line
= man
->last
->line
;
240 ppos
= man
->last
->pos
;
241 ntok
= man
->last
->tok
;
242 man_unscope(man
, nn
);
244 /* Move a trailing paragraph behind the block. */
246 if (ntok
== MAN_LP
|| ntok
== MAN_PP
|| ntok
== MAN_P
) {
248 blk_imp(man
, ntok
, line
, ppos
, pos
, buf
);
254 blk_exp(MACRO_PROT_ARGS
)
256 struct roff_node
*head
;
261 roff_block_alloc(man
, line
, ppos
, tok
);
262 head
= roff_head_alloc(man
, line
, ppos
, tok
);
265 if (man_args(man
, line
, pos
, buf
, &p
))
266 roff_word_alloc(man
, line
, la
, p
);
268 if (buf
[*pos
] != '\0')
269 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
270 man
->parse
, line
, *pos
, "%s ... %s",
271 man_macronames
[tok
], buf
+ *pos
);
273 man_unscope(man
, head
);
274 roff_body_alloc(man
, line
, ppos
, tok
);
278 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
279 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
280 * scopes, such as `SH' closing out an `SS', are defined in the rew
284 blk_imp(MACRO_PROT_ARGS
)
291 n
= roff_block_alloc(man
, line
, ppos
, tok
);
292 if (n
->tok
== MAN_SH
|| n
->tok
== MAN_SS
)
293 man
->flags
&= ~MAN_LITERAL
;
294 n
= roff_head_alloc(man
, line
, ppos
, tok
);
296 /* Add line arguments. */
300 if ( ! man_args(man
, line
, pos
, buf
, &p
))
302 roff_word_alloc(man
, line
, la
, p
);
306 * For macros having optional next-line scope,
307 * keep the head open if there were no arguments.
308 * For `TP', always keep the head open.
311 if (man_macros
[tok
].flags
& MAN_SCOPED
&&
312 (tok
== MAN_TP
|| n
== man
->last
)) {
313 man
->flags
|= MAN_BLINE
;
317 /* Close out the head and open the body. */
320 roff_body_alloc(man
, line
, ppos
, tok
);
324 in_line_eoln(MACRO_PROT_ARGS
)
330 roff_elem_alloc(man
, line
, ppos
, tok
);
334 if (buf
[*pos
] != '\0' && (tok
== MAN_br
||
335 tok
== MAN_fi
|| tok
== MAN_nf
)) {
336 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
337 man
->parse
, line
, *pos
, "%s %s",
338 man_macronames
[tok
], buf
+ *pos
);
341 if (buf
[*pos
] != '\0' && man
->last
!= n
&&
342 (tok
== MAN_PD
|| tok
== MAN_ft
|| tok
== MAN_sp
)) {
343 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
344 man
->parse
, line
, *pos
, "%s ... %s",
345 man_macronames
[tok
], buf
+ *pos
);
349 if ( ! man_args(man
, line
, pos
, buf
, &p
))
351 if (man_macros
[tok
].flags
& MAN_JOIN
&&
352 man
->last
->type
== ROFFT_TEXT
)
353 roff_word_append(man
, p
);
355 roff_word_alloc(man
, line
, la
, p
);
359 * Append MAN_EOS in case the last snipped argument
360 * ends with a dot, e.g. `.IR syslog (3).'
363 if (n
!= man
->last
&&
364 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
365 man
->last
->flags
|= MAN_EOS
;
368 * If no arguments are specified and this is MAN_SCOPED (i.e.,
369 * next-line scoped), then set our mode to indicate that we're
370 * waiting for terms to load into our context.
373 if (n
== man
->last
&& man_macros
[tok
].flags
& MAN_SCOPED
) {
374 assert( ! (man_macros
[tok
].flags
& MAN_NSCOPED
));
375 man
->flags
|= MAN_ELINE
;
379 assert(man
->last
->type
!= ROFFT_ROOT
);
380 man
->next
= ROFF_NEXT_SIBLING
;
382 /* Rewind our element scope. */
384 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
385 man_state(man
, man
->last
);
392 man_endparse(struct roff_man
*man
)
395 man_unscope(man
, man
->first
);
396 man
->flags
&= ~MAN_LITERAL
;
400 man_args(struct roff_man
*man
, int line
, int *pos
, char *buf
, char **v
)
405 *v
= start
= buf
+ *pos
;
406 assert(' ' != *start
);
411 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);