]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
71bc225f39e4f2728ae7116146743bd1c3a01c14
1 /* $Id: man_macro.c,v 1.133 2018/08/26 16:21:24 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 static const struct man_macro man_macros
[MAN_MAX
- MAN_TH
] = {
44 { in_line_eoln
, MAN_XSCOPE
}, /* TH */
45 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* SH */
46 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* SS */
47 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* TP */
48 { blk_imp
, MAN_XSCOPE
| MAN_BSCOPED
}, /* TQ */
49 { blk_imp
, MAN_XSCOPE
}, /* LP */
50 { blk_imp
, MAN_XSCOPE
}, /* PP */
51 { blk_imp
, MAN_XSCOPE
}, /* P */
52 { blk_imp
, MAN_XSCOPE
}, /* IP */
53 { blk_imp
, MAN_XSCOPE
}, /* HP */
54 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* SM */
55 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| 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_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* R */
61 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* B */
62 { in_line_eoln
, MAN_NSCOPED
| MAN_ESCOPED
| MAN_JOIN
}, /* I */
63 { in_line_eoln
, 0 }, /* IR */
64 { in_line_eoln
, 0 }, /* RI */
65 { in_line_eoln
, MAN_NSCOPED
}, /* nf */
66 { in_line_eoln
, MAN_NSCOPED
}, /* fi */
67 { blk_close
, MAN_XSCOPE
}, /* RE */
68 { blk_exp
, MAN_XSCOPE
}, /* RS */
69 { in_line_eoln
, 0 }, /* DT */
70 { in_line_eoln
, 0 }, /* UC */
71 { in_line_eoln
, MAN_NSCOPED
}, /* PD */
72 { in_line_eoln
, 0 }, /* AT */
73 { in_line_eoln
, MAN_NSCOPED
}, /* in */
74 { blk_imp
, MAN_XSCOPE
}, /* SY */
75 { blk_close
, MAN_XSCOPE
}, /* YS */
76 { in_line_eoln
, 0 }, /* OP */
77 { in_line_eoln
, MAN_XSCOPE
}, /* EX */
78 { in_line_eoln
, MAN_XSCOPE
}, /* EE */
79 { blk_exp
, MAN_XSCOPE
}, /* UR */
80 { blk_close
, MAN_XSCOPE
}, /* UE */
81 { blk_exp
, MAN_XSCOPE
}, /* MT */
82 { blk_close
, MAN_XSCOPE
}, /* ME */
86 const struct man_macro
*
87 man_macro(enum roff_tok tok
)
89 assert(tok
>= MAN_TH
&& tok
<= MAN_MAX
);
90 return man_macros
+ (tok
- MAN_TH
);
94 man_unscope(struct roff_man
*man
, const struct roff_node
*to
)
102 /* Reached the end of the document? */
104 if (to
== NULL
&& ! (n
->flags
& NODE_VALID
)) {
105 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
106 man_macro(n
->tok
)->flags
&
107 (MAN_BSCOPED
| MAN_NSCOPED
)) {
108 mandoc_vmsg(MANDOCERR_BLK_LINE
,
109 man
->parse
, n
->line
, n
->pos
,
110 "EOF breaks %s", roff_name
[n
->tok
]);
111 if (man
->flags
& MAN_ELINE
)
112 man
->flags
&= ~MAN_ELINE
;
114 assert(n
->type
== ROFFT_HEAD
);
116 man
->flags
&= ~MAN_BLINE
;
120 roff_node_delete(man
, man
->last
);
123 if (n
->type
== ROFFT_BLOCK
&&
124 man_macro(n
->tok
)->fp
== blk_exp
)
125 mandoc_msg(MANDOCERR_BLK_NOEND
,
126 man
->parse
, n
->line
, n
->pos
,
131 * We might delete the man->last node
132 * in the post-validation phase.
133 * Save a pointer to the parent such that
134 * we know where to continue the iteration.
139 man
->last
->flags
|= NODE_VALID
;
143 * If we ended up at the parent of the node we were
144 * supposed to rewind to, that means the target node
145 * got deleted, so add the next node we parse as a child
146 * of the parent instead of as a sibling of the target.
149 man
->next
= (man
->last
== to
) ?
150 ROFF_NEXT_CHILD
: ROFF_NEXT_SIBLING
;
154 * Rewinding entails ascending the parse tree until a coherent point,
155 * for example, the `SH' macro will close out any intervening `SS'
156 * scopes. When a scope is closed, it must be validated and actioned.
159 rew_scope(struct roff_man
*man
, enum roff_tok tok
)
163 /* Preserve empty paragraphs before RS. */
166 if (tok
== MAN_RS
&& n
->child
== NULL
&&
167 (n
->tok
== MAN_P
|| n
->tok
== MAN_PP
|| n
->tok
== MAN_LP
))
171 if (n
->type
== ROFFT_ROOT
)
173 if (n
->flags
& NODE_VALID
) {
177 if (n
->type
!= ROFFT_BLOCK
) {
178 if (n
->parent
->type
== ROFFT_ROOT
) {
186 if (tok
!= MAN_SH
&& (n
->tok
== MAN_SH
||
187 (tok
!= MAN_SS
&& (n
->tok
== MAN_SS
||
188 man_macro(n
->tok
)->fp
== blk_exp
))))
197 * Close out a generic explicit macro.
200 blk_close(MACRO_PROT_ARGS
)
202 enum roff_tok ctok
, ntok
;
203 const struct roff_node
*nn
;
205 int cline
, cpos
, nrew
, target
;
211 if ( ! man_args(man
, line
, pos
, buf
, &p
))
213 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
214 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
216 target
= strtol(p
, &p
, 10);
218 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
219 line
, p
- buf
, "RE ... %s", p
);
224 mandoc_vmsg(MANDOCERR_RE_NOTOPEN
, man
->parse
,
225 line
, ppos
, "RE %d", target
);
242 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
243 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
&& ! --nrew
)
247 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, man
->parse
,
248 line
, ppos
, roff_name
[tok
]);
249 rew_scope(man
, MAN_PP
);
251 roff_elem_alloc(man
, line
, ppos
, ROFF_br
);
252 man
->last
->flags
|= NODE_LINE
|
253 NODE_VALID
| NODE_ENDED
;
254 man
->next
= ROFF_NEXT_SIBLING
;
259 cline
= man
->last
->line
;
260 cpos
= man
->last
->pos
;
261 ctok
= man
->last
->tok
;
262 man_unscope(man
, nn
);
264 if (tok
== MAN_RE
&& nn
->head
->aux
> 0)
265 roff_setreg(man
->roff
, "an-margin", nn
->head
->aux
, '-');
269 if (buf
[*pos
] != '\0') {
270 roff_word_alloc(man
, line
, ppos
, buf
+ *pos
);
271 man
->last
->flags
|= NODE_DELIMC
;
272 if (mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
273 man
->last
->flags
|= NODE_EOS
;
276 /* Move a trailing paragraph behind the block. */
278 if (ctok
== MAN_LP
|| ctok
== MAN_PP
|| ctok
== MAN_P
) {
280 blk_imp(man
, ctok
, cline
, cpos
, pos
, buf
);
283 /* Synopsis blocks need an explicit end marker for spacing. */
285 if (tok
== MAN_YS
&& man
->last
== nn
) {
286 roff_elem_alloc(man
, line
, ppos
, tok
);
287 man_unscope(man
, man
->last
);
292 blk_exp(MACRO_PROT_ARGS
)
294 struct roff_node
*head
;
300 roff_block_alloc(man
, line
, ppos
, tok
);
301 head
= roff_head_alloc(man
, line
, ppos
, tok
);
304 if (man_args(man
, line
, pos
, buf
, &p
)) {
305 roff_word_alloc(man
, line
, la
, p
);
307 if (roff_getreg(man
->roff
, "an-margin") == 0)
308 roff_setreg(man
->roff
, "an-margin",
310 if ((head
->aux
= strtod(p
, NULL
) * 24.0) > 0)
311 roff_setreg(man
->roff
, "an-margin",
316 if (buf
[*pos
] != '\0')
317 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
, line
,
318 *pos
, "%s ... %s", roff_name
[tok
], buf
+ *pos
);
320 man_unscope(man
, head
);
321 roff_body_alloc(man
, line
, ppos
, tok
);
325 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
326 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
327 * scopes, such as `SH' closing out an `SS', are defined in the rew
331 blk_imp(MACRO_PROT_ARGS
)
338 n
= roff_block_alloc(man
, line
, ppos
, tok
);
339 if (n
->tok
== MAN_SH
|| n
->tok
== MAN_SS
)
340 man
->flags
&= ~MAN_LITERAL
;
341 n
= roff_head_alloc(man
, line
, ppos
, tok
);
343 /* Add line arguments. */
347 if ( ! man_args(man
, line
, pos
, buf
, &p
))
349 roff_word_alloc(man
, line
, la
, p
);
353 * For macros having optional next-line scope,
354 * keep the head open if there were no arguments.
355 * For `TP' and `TQ', always keep the head open.
358 if (man_macro(tok
)->flags
& MAN_BSCOPED
&&
359 (tok
== MAN_TP
|| tok
== MAN_TQ
|| n
== man
->last
)) {
360 man
->flags
|= MAN_BLINE
;
364 /* Close out the head and open the body. */
367 roff_body_alloc(man
, line
, ppos
, tok
);
371 in_line_eoln(MACRO_PROT_ARGS
)
377 roff_elem_alloc(man
, line
, ppos
, tok
);
381 if (buf
[*pos
] != '\0' && (tok
== MAN_fi
|| tok
== MAN_nf
)) {
382 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
383 man
->parse
, line
, *pos
, "%s %s",
384 roff_name
[tok
], buf
+ *pos
);
387 if (buf
[*pos
] != '\0' && man
->last
!= n
&& tok
== MAN_PD
) {
388 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
389 man
->parse
, line
, *pos
, "%s ... %s",
390 roff_name
[tok
], buf
+ *pos
);
394 if ( ! man_args(man
, line
, pos
, buf
, &p
))
396 if (man_macro(tok
)->flags
& MAN_JOIN
&&
397 man
->last
->type
== ROFFT_TEXT
)
398 roff_word_append(man
, p
);
400 roff_word_alloc(man
, line
, la
, p
);
404 * Append NODE_EOS in case the last snipped argument
405 * ends with a dot, e.g. `.IR syslog (3).'
408 if (n
!= man
->last
&&
409 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
410 man
->last
->flags
|= NODE_EOS
;
413 * If no arguments are specified and this is MAN_ESCOPED (i.e.,
414 * next-line scoped), then set our mode to indicate that we're
415 * waiting for terms to load into our context.
418 if (n
== man
->last
&& man_macro(tok
)->flags
& MAN_ESCOPED
) {
419 man
->flags
|= MAN_ELINE
;
423 assert(man
->last
->type
!= ROFFT_ROOT
);
424 man
->next
= ROFF_NEXT_SIBLING
;
426 /* Rewind our element scope. */
428 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
429 man_state(man
, man
->last
);
434 /* Rewind next-line scoped ancestors, if any. */
436 if (man_macro(tok
)->flags
& MAN_ESCOPED
)
437 man_descope(man
, line
, ppos
, NULL
);
441 man_endparse(struct roff_man
*man
)
444 man_unscope(man
, man
->first
);
445 man
->flags
&= ~MAN_LITERAL
;
449 man_args(struct roff_man
*man
, int line
, int *pos
, char *buf
, char **v
)
454 *v
= start
= buf
+ *pos
;
455 assert(' ' != *start
);
460 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);