]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.120 2017/05/05 15:17:32 schwarze Exp $ */
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>
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
*, enum roff_tok
);
43 const struct man_macro __man_macros
[MAN_MAX
- MAN_TH
] = {
44 { in_line_eoln
, MAN_BSCOPE
}, /* TH */
45 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SH */
46 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SS */
47 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* TP */
48 { blk_imp
, MAN_BSCOPE
}, /* LP */
49 { blk_imp
, MAN_BSCOPE
}, /* PP */
50 { blk_imp
, MAN_BSCOPE
}, /* P */
51 { blk_imp
, MAN_BSCOPE
}, /* IP */
52 { blk_imp
, MAN_BSCOPE
}, /* HP */
53 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* SM */
54 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* SB */
55 { in_line_eoln
, 0 }, /* BI */
56 { in_line_eoln
, 0 }, /* IB */
57 { in_line_eoln
, 0 }, /* BR */
58 { in_line_eoln
, 0 }, /* RB */
59 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* R */
60 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* B */
61 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* I */
62 { in_line_eoln
, 0 }, /* IR */
63 { in_line_eoln
, 0 }, /* RI */
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 }, /* OP */
74 { in_line_eoln
, MAN_BSCOPE
}, /* EX */
75 { in_line_eoln
, MAN_BSCOPE
}, /* EE */
76 { blk_exp
, MAN_BSCOPE
}, /* UR */
77 { blk_close
, MAN_BSCOPE
}, /* UE */
79 const struct man_macro
*const man_macros
= __man_macros
- MAN_TH
;
83 man_unscope(struct roff_man
*man
, const struct roff_node
*to
)
91 /* Reached the end of the document? */
93 if (to
== NULL
&& ! (n
->flags
& NODE_VALID
)) {
94 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
95 man_macros
[n
->tok
].flags
& MAN_SCOPED
) {
96 mandoc_vmsg(MANDOCERR_BLK_LINE
,
97 man
->parse
, n
->line
, n
->pos
,
98 "EOF breaks %s", roff_name
[n
->tok
]);
99 if (man
->flags
& MAN_ELINE
)
100 man
->flags
&= ~MAN_ELINE
;
102 assert(n
->type
== ROFFT_HEAD
);
104 man
->flags
&= ~MAN_BLINE
;
108 roff_node_delete(man
, man
->last
);
111 if (n
->type
== ROFFT_BLOCK
&&
112 man_macros
[n
->tok
].fp
== blk_exp
)
113 mandoc_msg(MANDOCERR_BLK_NOEND
,
114 man
->parse
, n
->line
, n
->pos
,
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.
127 man
->last
->flags
|= NODE_VALID
;
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.
137 man
->next
= (man
->last
== to
) ?
138 ROFF_NEXT_CHILD
: ROFF_NEXT_SIBLING
;
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.
147 rew_scope(struct roff_man
*man
, enum roff_tok tok
)
151 /* Preserve empty paragraphs before RS. */
154 if (tok
== MAN_RS
&& n
->child
== NULL
&&
155 (n
->tok
== MAN_P
|| n
->tok
== MAN_PP
|| n
->tok
== MAN_LP
))
159 if (n
->type
== ROFFT_ROOT
)
161 if (n
->flags
& NODE_VALID
) {
165 if (n
->type
!= ROFFT_BLOCK
) {
166 if (n
->parent
->type
== ROFFT_ROOT
) {
174 if (tok
!= MAN_SH
&& (n
->tok
== MAN_SH
||
175 (tok
!= MAN_SS
&& (n
->tok
== MAN_SS
||
176 man_macros
[n
->tok
].fp
== blk_exp
))))
185 * Close out a generic explicit macro.
188 blk_close(MACRO_PROT_ARGS
)
191 const struct roff_node
*nn
;
199 if ( ! man_args(man
, line
, pos
, buf
, &p
))
201 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
202 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
204 target
= strtol(p
, &p
, 10);
206 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
207 line
, p
- buf
, "RE ... %s", p
);
212 mandoc_vmsg(MANDOCERR_RE_NOTOPEN
, man
->parse
,
213 line
, ppos
, "RE %d", target
);
224 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
225 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
&& ! --nrew
)
229 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, man
->parse
,
230 line
, ppos
, roff_name
[tok
]);
231 rew_scope(man
, MAN_PP
);
233 line
= man
->last
->line
;
234 ppos
= man
->last
->pos
;
235 ntok
= man
->last
->tok
;
236 man_unscope(man
, nn
);
238 /* Move a trailing paragraph behind the block. */
240 if (ntok
== MAN_LP
|| ntok
== MAN_PP
|| ntok
== MAN_P
) {
242 blk_imp(man
, ntok
, line
, ppos
, pos
, buf
);
248 blk_exp(MACRO_PROT_ARGS
)
250 struct roff_node
*head
;
255 roff_block_alloc(man
, line
, ppos
, tok
);
256 head
= roff_head_alloc(man
, line
, ppos
, tok
);
259 if (man_args(man
, line
, pos
, buf
, &p
))
260 roff_word_alloc(man
, line
, la
, p
);
262 if (buf
[*pos
] != '\0')
263 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
, line
,
264 *pos
, "%s ... %s", roff_name
[tok
], buf
+ *pos
);
266 man_unscope(man
, head
);
267 roff_body_alloc(man
, line
, ppos
, tok
);
271 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
272 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
273 * scopes, such as `SH' closing out an `SS', are defined in the rew
277 blk_imp(MACRO_PROT_ARGS
)
284 n
= roff_block_alloc(man
, line
, ppos
, tok
);
285 if (n
->tok
== MAN_SH
|| n
->tok
== MAN_SS
)
286 man
->flags
&= ~MAN_LITERAL
;
287 n
= roff_head_alloc(man
, line
, ppos
, tok
);
289 /* Add line arguments. */
293 if ( ! man_args(man
, line
, pos
, buf
, &p
))
295 roff_word_alloc(man
, line
, la
, p
);
299 * For macros having optional next-line scope,
300 * keep the head open if there were no arguments.
301 * For `TP', always keep the head open.
304 if (man_macros
[tok
].flags
& MAN_SCOPED
&&
305 (tok
== MAN_TP
|| n
== man
->last
)) {
306 man
->flags
|= MAN_BLINE
;
310 /* Close out the head and open the body. */
313 roff_body_alloc(man
, line
, ppos
, tok
);
317 in_line_eoln(MACRO_PROT_ARGS
)
323 roff_elem_alloc(man
, line
, ppos
, tok
);
327 if (buf
[*pos
] != '\0' && (tok
== MAN_fi
|| tok
== MAN_nf
)) {
328 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
329 man
->parse
, line
, *pos
, "%s %s",
330 roff_name
[tok
], buf
+ *pos
);
333 if (buf
[*pos
] != '\0' && man
->last
!= n
&& tok
== MAN_PD
) {
334 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
335 man
->parse
, line
, *pos
, "%s ... %s",
336 roff_name
[tok
], buf
+ *pos
);
340 if ( ! man_args(man
, line
, pos
, buf
, &p
))
342 if (man_macros
[tok
].flags
& MAN_JOIN
&&
343 man
->last
->type
== ROFFT_TEXT
)
344 roff_word_append(man
, p
);
346 roff_word_alloc(man
, line
, la
, p
);
350 * Append NODE_EOS in case the last snipped argument
351 * ends with a dot, e.g. `.IR syslog (3).'
354 if (n
!= man
->last
&&
355 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
356 man
->last
->flags
|= NODE_EOS
;
359 * If no arguments are specified and this is MAN_SCOPED (i.e.,
360 * next-line scoped), then set our mode to indicate that we're
361 * waiting for terms to load into our context.
364 if (n
== man
->last
&& man_macros
[tok
].flags
& MAN_SCOPED
) {
365 assert( ! (man_macros
[tok
].flags
& MAN_NSCOPED
));
366 man
->flags
|= MAN_ELINE
;
370 assert(man
->last
->type
!= ROFFT_ROOT
);
371 man
->next
= ROFF_NEXT_SIBLING
;
373 /* Rewind our element scope. */
375 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
376 man_state(man
, man
->last
);
383 man_endparse(struct roff_man
*man
)
386 man_unscope(man
, man
->first
);
387 man
->flags
&= ~MAN_LITERAL
;
391 man_args(struct roff_man
*man
, int line
, int *pos
, char *buf
, char **v
)
396 *v
= start
= buf
+ *pos
;
397 assert(' ' != *start
);
402 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);