]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.145 2020/09/09 17:01:10 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012-2015, 2017-2020 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>
32 #include "libmandoc.h"
36 static void blk_close(MACRO_PROT_ARGS
);
37 static void blk_exp(MACRO_PROT_ARGS
);
38 static void blk_imp(MACRO_PROT_ARGS
);
39 static void in_line_eoln(MACRO_PROT_ARGS
);
40 static int man_args(struct roff_man
*, int,
41 int *, char *, char **);
42 static void rew_scope(struct roff_man
*, enum roff_tok
);
44 static const struct man_macro man_macros
[MAN_MAX
- MAN_TH
] = {
45 { in_line_eoln
, MAN_XSCOPE
}, /* TH */
46 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* SH */
47 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* SS */
48 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* TP */
49 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* TQ */
50 { blk_imp
, MAN_XSCOPE
}, /* LP */
51 { blk_imp
, MAN_XSCOPE
}, /* PP */
52 { blk_imp
, MAN_XSCOPE
}, /* P */
53 { blk_imp
, MAN_XSCOPE
}, /* IP */
54 { blk_imp
, MAN_XSCOPE
}, /* HP */
55 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* SM */
56 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* SB */
57 { in_line_eoln
, 0 }, /* BI */
58 { in_line_eoln
, 0 }, /* IB */
59 { in_line_eoln
, 0 }, /* BR */
60 { in_line_eoln
, 0 }, /* RB */
61 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* R */
62 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* B */
63 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* I */
64 { in_line_eoln
, 0 }, /* IR */
65 { in_line_eoln
, 0 }, /* RI */
66 { blk_close
, MAN_XSCOPE
}, /* RE */
67 { blk_exp
, MAN_XSCOPE
}, /* 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
, MAN_NSCOPED
}, /* in */
73 { blk_imp
, MAN_XSCOPE
}, /* SY */
74 { blk_close
, MAN_XSCOPE
}, /* YS */
75 { in_line_eoln
, 0 }, /* OP */
76 { in_line_eoln
, MAN_XSCOPE
}, /* EX */
77 { in_line_eoln
, MAN_XSCOPE
}, /* EE */
78 { blk_exp
, MAN_XSCOPE
}, /* UR */
79 { blk_close
, MAN_XSCOPE
}, /* UE */
80 { blk_exp
, MAN_XSCOPE
}, /* MT */
81 { blk_close
, MAN_XSCOPE
}, /* ME */
85 const struct man_macro
*
86 man_macro(enum roff_tok tok
)
88 assert(tok
>= MAN_TH
&& tok
<= MAN_MAX
);
89 return man_macros
+ (tok
- MAN_TH
);
93 man_unscope(struct roff_man
*man
, const struct roff_node
*to
)
101 /* Reached the end of the document? */
103 if (to
== NULL
&& ! (n
->flags
& NODE_VALID
)) {
104 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
105 man_macro(n
->tok
)->flags
&
106 (MAN_BSCOPED
| MAN_NSCOPED
)) {
107 mandoc_msg(MANDOCERR_BLK_LINE
,
109 "EOF breaks %s", roff_name
[n
->tok
]);
110 if (man
->flags
& MAN_ELINE
) {
111 if ((man_macro(n
->parent
->tok
)->flags
&
113 man
->flags
&= ~MAN_ELINE
;
115 assert(n
->type
== ROFFT_HEAD
);
117 man
->flags
&= ~MAN_BLINE
;
121 roff_node_delete(man
, man
->last
);
124 if (n
->type
== ROFFT_BLOCK
&&
125 man_macro(n
->tok
)->fp
== blk_exp
)
126 mandoc_msg(MANDOCERR_BLK_NOEND
,
127 n
->line
, n
->pos
, "%s",
132 * We might delete the man->last node
133 * in the post-validation phase.
134 * Save a pointer to the parent such that
135 * we know where to continue the iteration.
140 man
->last
->flags
|= NODE_VALID
;
144 * If we ended up at the parent of the node we were
145 * supposed to rewind to, that means the target node
146 * got deleted, so add the next node we parse as a child
147 * of the parent instead of as a sibling of the target.
150 man
->next
= (man
->last
== to
) ?
151 ROFF_NEXT_CHILD
: ROFF_NEXT_SIBLING
;
155 * Rewinding entails ascending the parse tree until a coherent point,
156 * for example, the `SH' macro will close out any intervening `SS'
157 * scopes. When a scope is closed, it must be validated and actioned.
160 rew_scope(struct roff_man
*man
, enum roff_tok tok
)
164 /* Preserve empty paragraphs before RS. */
167 if (tok
== MAN_RS
&& n
->child
== NULL
&&
168 (n
->tok
== MAN_P
|| n
->tok
== MAN_PP
|| n
->tok
== MAN_LP
))
172 if (n
->type
== ROFFT_ROOT
)
174 if (n
->flags
& NODE_VALID
) {
178 if (n
->type
!= ROFFT_BLOCK
) {
179 if (n
->parent
->type
== ROFFT_ROOT
) {
187 if (tok
!= MAN_SH
&& (n
->tok
== MAN_SH
||
188 (tok
!= MAN_SS
&& (n
->tok
== MAN_SS
||
189 man_macro(n
->tok
)->fp
== blk_exp
))))
198 * Close out a generic explicit macro.
201 blk_close(MACRO_PROT_ARGS
)
203 enum roff_tok ctok
, ntok
;
204 const struct roff_node
*nn
;
206 int cline
, cpos
, la
, nrew
, target
;
213 if ( ! man_args(man
, line
, pos
, buf
, &p
))
215 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
216 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
218 target
= strtol(p
, &ep
, 10);
220 mandoc_msg(MANDOCERR_ARG_EXCESS
, line
,
221 la
+ (buf
[la
] == '"') + (int)(ep
- p
),
228 mandoc_msg(MANDOCERR_RE_NOTOPEN
,
229 line
, ppos
, "RE %d", target
);
246 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
247 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
&& ! --nrew
)
251 mandoc_msg(MANDOCERR_BLK_NOTOPEN
,
252 line
, ppos
, "%s", roff_name
[tok
]);
253 rew_scope(man
, MAN_PP
);
255 roff_elem_alloc(man
, line
, ppos
, ROFF_br
);
256 man
->last
->flags
|= NODE_LINE
|
257 NODE_VALID
| NODE_ENDED
;
258 man
->next
= ROFF_NEXT_SIBLING
;
263 cline
= man
->last
->line
;
264 cpos
= man
->last
->pos
;
265 ctok
= man
->last
->tok
;
266 man_unscope(man
, nn
);
268 if (tok
== MAN_RE
&& nn
->head
->aux
> 0)
269 roff_setreg(man
->roff
, "an-margin", nn
->head
->aux
, '-');
273 if (buf
[*pos
] != '\0') {
274 roff_word_alloc(man
, line
, ppos
, buf
+ *pos
);
275 man
->last
->flags
|= NODE_DELIMC
;
276 if (mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
277 man
->last
->flags
|= NODE_EOS
;
280 /* Move a trailing paragraph behind the block. */
282 if (ctok
== MAN_LP
|| ctok
== MAN_PP
|| ctok
== MAN_P
) {
284 blk_imp(man
, ctok
, cline
, cpos
, pos
, buf
);
287 /* Synopsis blocks need an explicit end marker for spacing. */
289 if (tok
== MAN_YS
&& man
->last
== nn
) {
290 roff_elem_alloc(man
, line
, ppos
, tok
);
291 man_unscope(man
, man
->last
);
296 blk_exp(MACRO_PROT_ARGS
)
298 struct roff_node
*head
;
304 man
->flags
|= ROFF_NONOFILL
;
306 roff_block_alloc(man
, line
, ppos
, tok
);
307 head
= roff_head_alloc(man
, line
, ppos
, tok
);
310 if (man_args(man
, line
, pos
, buf
, &p
)) {
311 roff_word_alloc(man
, line
, la
, p
);
313 if (roff_getreg(man
->roff
, "an-margin") == 0)
314 roff_setreg(man
->roff
, "an-margin",
316 if ((head
->aux
= strtod(p
, NULL
) * 24.0) > 0)
317 roff_setreg(man
->roff
, "an-margin",
323 if (buf
[*pos
] != '\0')
324 mandoc_msg(MANDOCERR_ARG_EXCESS
, line
, *pos
,
325 "%s ... %s", roff_name
[tok
], buf
+ *pos
);
327 man_unscope(man
, head
);
328 roff_body_alloc(man
, line
, ppos
, tok
);
329 man
->flags
&= ~ROFF_NONOFILL
;
333 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
334 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
335 * scopes, such as `SH' closing out an `SS', are defined in the rew
339 blk_imp(MACRO_PROT_ARGS
)
346 man
->flags
|= ROFF_NONOFILL
;
347 if (tok
== MAN_SH
|| tok
== MAN_SS
)
348 man
->flags
&= ~ROFF_NOFILL
;
349 roff_block_alloc(man
, line
, ppos
, tok
);
350 n
= roff_head_alloc(man
, line
, ppos
, tok
);
352 /* Add line arguments. */
356 if ( ! man_args(man
, line
, pos
, buf
, &p
))
358 roff_word_alloc(man
, line
, la
, p
);
363 * For macros having optional next-line scope,
364 * keep the head open if there were no arguments.
365 * For `TP' and `TQ', always keep the head open.
368 if (man_macro(tok
)->flags
& MAN_BSCOPED
&&
369 (tok
== MAN_TP
|| tok
== MAN_TQ
|| n
== man
->last
)) {
370 man
->flags
|= MAN_BLINE
;
374 /* Close out the head and open the body. */
377 roff_body_alloc(man
, line
, ppos
, tok
);
378 man
->flags
&= ~ROFF_NONOFILL
;
382 in_line_eoln(MACRO_PROT_ARGS
)
388 roff_elem_alloc(man
, line
, ppos
, tok
);
392 man
->flags
|= ROFF_NOFILL
;
393 else if (tok
== MAN_EE
)
394 man
->flags
&= ~ROFF_NOFILL
;
397 if (buf
[*pos
] != '\0' && man
->last
!= n
&& tok
== MAN_PD
) {
398 mandoc_msg(MANDOCERR_ARG_EXCESS
, line
, *pos
,
399 "%s ... %s", roff_name
[tok
], buf
+ *pos
);
403 if ( ! man_args(man
, line
, pos
, buf
, &p
))
405 if (man_macro(tok
)->flags
& MAN_JOIN
&&
406 man
->last
->type
== ROFFT_TEXT
)
407 roff_word_append(man
, p
);
409 roff_word_alloc(man
, line
, la
, p
);
414 * Append NODE_EOS in case the last snipped argument
415 * ends with a dot, e.g. `.IR syslog (3).'
418 if (n
!= man
->last
&&
419 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
420 man
->last
->flags
|= NODE_EOS
;
423 * If no arguments are specified and this is MAN_ESCOPED (i.e.,
424 * next-line scoped), then set our mode to indicate that we're
425 * waiting for terms to load into our context.
428 if (n
== man
->last
&& man_macro(tok
)->flags
& MAN_ESCOPED
) {
429 man
->flags
|= MAN_ELINE
;
433 assert(man
->last
->type
!= ROFFT_ROOT
);
434 man
->next
= ROFF_NEXT_SIBLING
;
436 /* Rewind our element scope. */
438 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
439 man
->last
->flags
|= NODE_VALID
;
444 /* Rewind next-line scoped ancestors, if any. */
446 if (man_macro(tok
)->flags
& MAN_ESCOPED
)
447 man_descope(man
, line
, ppos
, NULL
);
451 man_endparse(struct roff_man
*man
)
453 man_unscope(man
, man
->meta
.first
);
457 man_args(struct roff_man
*man
, int line
, int *pos
, char *buf
, char **v
)
462 *v
= start
= buf
+ *pos
;
463 assert(' ' != *start
);
468 *v
= roff_getarg(man
->roff
, v
, line
, pos
);