]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.124 2018/08/14 01:27:48 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012-2015, 2017, 2018 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
, MAN_NSCOPED
}, /* 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 */
78 { blk_exp
, MAN_BSCOPE
}, /* MT */
79 { blk_close
, MAN_BSCOPE
}, /* ME */
81 const struct man_macro
*const man_macros
= __man_macros
- MAN_TH
;
85 man_unscope(struct roff_man
*man
, const struct roff_node
*to
)
93 /* Reached the end of the document? */
95 if (to
== NULL
&& ! (n
->flags
& NODE_VALID
)) {
96 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
97 man_macros
[n
->tok
].flags
& MAN_SCOPED
) {
98 mandoc_vmsg(MANDOCERR_BLK_LINE
,
99 man
->parse
, n
->line
, n
->pos
,
100 "EOF breaks %s", roff_name
[n
->tok
]);
101 if (man
->flags
& MAN_ELINE
)
102 man
->flags
&= ~MAN_ELINE
;
104 assert(n
->type
== ROFFT_HEAD
);
106 man
->flags
&= ~MAN_BLINE
;
110 roff_node_delete(man
, man
->last
);
113 if (n
->type
== ROFFT_BLOCK
&&
114 man_macros
[n
->tok
].fp
== blk_exp
)
115 mandoc_msg(MANDOCERR_BLK_NOEND
,
116 man
->parse
, n
->line
, n
->pos
,
121 * We might delete the man->last node
122 * in the post-validation phase.
123 * Save a pointer to the parent such that
124 * we know where to continue the iteration.
129 man
->last
->flags
|= NODE_VALID
;
133 * If we ended up at the parent of the node we were
134 * supposed to rewind to, that means the target node
135 * got deleted, so add the next node we parse as a child
136 * of the parent instead of as a sibling of the target.
139 man
->next
= (man
->last
== to
) ?
140 ROFF_NEXT_CHILD
: ROFF_NEXT_SIBLING
;
144 * Rewinding entails ascending the parse tree until a coherent point,
145 * for example, the `SH' macro will close out any intervening `SS'
146 * scopes. When a scope is closed, it must be validated and actioned.
149 rew_scope(struct roff_man
*man
, enum roff_tok tok
)
153 /* Preserve empty paragraphs before RS. */
156 if (tok
== MAN_RS
&& n
->child
== NULL
&&
157 (n
->tok
== MAN_P
|| n
->tok
== MAN_PP
|| n
->tok
== MAN_LP
))
161 if (n
->type
== ROFFT_ROOT
)
163 if (n
->flags
& NODE_VALID
) {
167 if (n
->type
!= ROFFT_BLOCK
) {
168 if (n
->parent
->type
== ROFFT_ROOT
) {
176 if (tok
!= MAN_SH
&& (n
->tok
== MAN_SH
||
177 (tok
!= MAN_SS
&& (n
->tok
== MAN_SS
||
178 man_macros
[n
->tok
].fp
== blk_exp
))))
187 * Close out a generic explicit macro.
190 blk_close(MACRO_PROT_ARGS
)
192 enum roff_tok ctok
, ntok
;
193 const struct roff_node
*nn
;
195 int cline
, cpos
, nrew
, target
;
201 if ( ! man_args(man
, line
, pos
, buf
, &p
))
203 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
204 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
206 target
= strtol(p
, &p
, 10);
208 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
209 line
, p
- buf
, "RE ... %s", p
);
214 mandoc_vmsg(MANDOCERR_RE_NOTOPEN
, man
->parse
,
215 line
, ppos
, "RE %d", target
);
229 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
230 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
&& ! --nrew
)
234 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, man
->parse
,
235 line
, ppos
, roff_name
[tok
]);
236 rew_scope(man
, MAN_PP
);
240 cline
= man
->last
->line
;
241 cpos
= man
->last
->pos
;
242 ctok
= man
->last
->tok
;
243 man_unscope(man
, nn
);
245 if (tok
== MAN_RE
&& nn
->head
->aux
> 0)
246 roff_setreg(man
->roff
, "an-margin", nn
->head
->aux
, '-');
250 if (buf
[*pos
] != '\0') {
251 roff_word_alloc(man
, line
, ppos
, buf
+ *pos
);
252 man
->last
->flags
|= NODE_DELIMC
;
255 /* Move a trailing paragraph behind the block. */
257 if (ctok
== MAN_LP
|| ctok
== MAN_PP
|| ctok
== MAN_P
) {
259 blk_imp(man
, ctok
, line
, ppos
, pos
, buf
);
264 blk_exp(MACRO_PROT_ARGS
)
266 struct roff_node
*head
;
271 roff_block_alloc(man
, line
, ppos
, tok
);
272 head
= roff_head_alloc(man
, line
, ppos
, tok
);
275 if (man_args(man
, line
, pos
, buf
, &p
)) {
276 roff_word_alloc(man
, line
, la
, p
);
278 if (roff_getreg(man
->roff
, "an-margin") == 0)
279 roff_setreg(man
->roff
, "an-margin",
281 if ((head
->aux
= strtod(p
, NULL
) * 24.0) > 0)
282 roff_setreg(man
->roff
, "an-margin",
287 if (buf
[*pos
] != '\0')
288 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
, line
,
289 *pos
, "%s ... %s", roff_name
[tok
], buf
+ *pos
);
291 man_unscope(man
, head
);
292 roff_body_alloc(man
, line
, ppos
, tok
);
296 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
297 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
298 * scopes, such as `SH' closing out an `SS', are defined in the rew
302 blk_imp(MACRO_PROT_ARGS
)
309 n
= roff_block_alloc(man
, line
, ppos
, tok
);
310 if (n
->tok
== MAN_SH
|| n
->tok
== MAN_SS
)
311 man
->flags
&= ~MAN_LITERAL
;
312 n
= roff_head_alloc(man
, line
, ppos
, tok
);
314 /* Add line arguments. */
318 if ( ! man_args(man
, line
, pos
, buf
, &p
))
320 roff_word_alloc(man
, line
, la
, p
);
324 * For macros having optional next-line scope,
325 * keep the head open if there were no arguments.
326 * For `TP', always keep the head open.
329 if (man_macros
[tok
].flags
& MAN_SCOPED
&&
330 (tok
== MAN_TP
|| n
== man
->last
)) {
331 man
->flags
|= MAN_BLINE
;
335 /* Close out the head and open the body. */
338 roff_body_alloc(man
, line
, ppos
, tok
);
342 in_line_eoln(MACRO_PROT_ARGS
)
348 roff_elem_alloc(man
, line
, ppos
, tok
);
352 if (buf
[*pos
] != '\0' && (tok
== MAN_fi
|| tok
== MAN_nf
)) {
353 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
354 man
->parse
, line
, *pos
, "%s %s",
355 roff_name
[tok
], buf
+ *pos
);
358 if (buf
[*pos
] != '\0' && man
->last
!= n
&& tok
== MAN_PD
) {
359 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
360 man
->parse
, line
, *pos
, "%s ... %s",
361 roff_name
[tok
], buf
+ *pos
);
365 if ( ! man_args(man
, line
, pos
, buf
, &p
))
367 if (man_macros
[tok
].flags
& MAN_JOIN
&&
368 man
->last
->type
== ROFFT_TEXT
)
369 roff_word_append(man
, p
);
371 roff_word_alloc(man
, line
, la
, p
);
375 * Append NODE_EOS in case the last snipped argument
376 * ends with a dot, e.g. `.IR syslog (3).'
379 if (n
!= man
->last
&&
380 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
381 man
->last
->flags
|= NODE_EOS
;
384 * If no arguments are specified and this is MAN_SCOPED (i.e.,
385 * next-line scoped), then set our mode to indicate that we're
386 * waiting for terms to load into our context.
389 if (n
== man
->last
&& man_macros
[tok
].flags
& MAN_SCOPED
) {
390 assert( ! (man_macros
[tok
].flags
& MAN_NSCOPED
));
391 man
->flags
|= MAN_ELINE
;
395 assert(man
->last
->type
!= ROFFT_ROOT
);
396 man
->next
= ROFF_NEXT_SIBLING
;
398 /* Rewind our element scope. */
400 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
401 man_state(man
, man
->last
);
408 man_endparse(struct roff_man
*man
)
411 man_unscope(man
, man
->first
);
412 man
->flags
&= ~MAN_LITERAL
;
416 man_args(struct roff_man
*man
, int line
, int *pos
, char *buf
, char **v
)
421 *v
= start
= buf
+ *pos
;
422 assert(' ' != *start
);
427 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);