]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.104 2015/04/03 23:19:15 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"
34 static void blk_close(MACRO_PROT_ARGS
);
35 static void blk_exp(MACRO_PROT_ARGS
);
36 static void blk_imp(MACRO_PROT_ARGS
);
37 static void in_line_eoln(MACRO_PROT_ARGS
);
38 static int man_args(struct man
*, int,
39 int *, char *, char **);
40 static void rew_scope(struct man
*, int);
42 const struct man_macro __man_macros
[MAN_MAX
] = {
43 { in_line_eoln
, MAN_NSCOPED
}, /* br */
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
}, /* sp */
65 { in_line_eoln
, MAN_BSCOPE
}, /* nf */
66 { in_line_eoln
, MAN_BSCOPE
}, /* fi */
67 { blk_close
, MAN_BSCOPE
}, /* RE */
68 { blk_exp
, MAN_BSCOPE
}, /* 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
, 0 }, /* in */
74 { in_line_eoln
, 0 }, /* ft */
75 { in_line_eoln
, 0 }, /* OP */
76 { in_line_eoln
, MAN_BSCOPE
}, /* EX */
77 { in_line_eoln
, MAN_BSCOPE
}, /* EE */
78 { blk_exp
, MAN_BSCOPE
}, /* UR */
79 { blk_close
, MAN_BSCOPE
}, /* UE */
80 { in_line_eoln
, 0 }, /* ll */
83 const struct man_macro
* const man_macros
= __man_macros
;
87 man_unscope(struct man
*man
, const struct roff_node
*to
)
95 /* Reached the end of the document? */
97 if (to
== NULL
&& ! (n
->flags
& MAN_VALID
)) {
98 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
99 man_macros
[n
->tok
].flags
& MAN_SCOPED
) {
100 mandoc_vmsg(MANDOCERR_BLK_LINE
,
101 man
->parse
, n
->line
, n
->pos
,
103 man_macronames
[n
->tok
]);
104 if (man
->flags
& MAN_ELINE
)
105 man
->flags
&= ~MAN_ELINE
;
107 assert(n
->type
== ROFFT_HEAD
);
109 man
->flags
&= ~MAN_BLINE
;
113 man_node_delete(man
, man
->last
);
116 if (n
->type
== ROFFT_BLOCK
&&
117 man_macros
[n
->tok
].fp
== blk_exp
)
118 mandoc_msg(MANDOCERR_BLK_NOEND
,
119 man
->parse
, n
->line
, n
->pos
,
120 man_macronames
[n
->tok
]);
124 * We might delete the man->last node
125 * in the post-validation phase.
126 * Save a pointer to the parent such that
127 * we know where to continue the iteration.
136 * If we ended up at the parent of the node we were
137 * supposed to rewind to, that means the target node
138 * got deleted, so add the next node we parse as a child
139 * of the parent instead of as a sibling of the target.
142 man
->next
= (man
->last
== to
) ?
143 MAN_NEXT_CHILD
: MAN_NEXT_SIBLING
;
147 * Rewinding entails ascending the parse tree until a coherent point,
148 * for example, the `SH' macro will close out any intervening `SS'
149 * scopes. When a scope is closed, it must be validated and actioned.
152 rew_scope(struct man
*man
, int tok
)
156 /* Preserve empty paragraphs before RS. */
159 if (tok
== MAN_RS
&& n
->nchild
== 0 &&
160 (n
->tok
== MAN_P
|| n
->tok
== MAN_PP
|| n
->tok
== MAN_LP
))
164 if (n
->type
== ROFFT_ROOT
)
166 if (n
->flags
& MAN_VALID
) {
170 if (n
->type
!= ROFFT_BLOCK
) {
171 if (n
->parent
->type
== ROFFT_ROOT
) {
179 if (tok
!= MAN_SH
&& (n
->tok
== MAN_SH
||
180 (tok
!= MAN_SS
&& (n
->tok
== MAN_SS
||
181 man_macros
[n
->tok
].fp
== blk_exp
))))
190 * Close out a generic explicit macro.
193 blk_close(MACRO_PROT_ARGS
)
196 const struct roff_node
*nn
;
204 if ( ! man_args(man
, line
, pos
, buf
, &p
))
206 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
207 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
209 target
= strtol(p
, &p
, 10);
211 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
212 line
, p
- buf
, "RE ... %s", p
);
217 mandoc_vmsg(MANDOCERR_RE_NOTOPEN
, man
->parse
,
218 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 man_block_alloc(man
, line
, ppos
, tok
);
262 man_head_alloc(man
, line
, ppos
, tok
);
266 if (man_args(man
, line
, pos
, buf
, &p
))
267 man_word_alloc(man
, line
, la
, p
);
269 if (buf
[*pos
] != '\0')
270 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
271 man
->parse
, line
, *pos
, "%s ... %s",
272 man_macronames
[tok
], buf
+ *pos
);
274 man_unscope(man
, head
);
275 man_body_alloc(man
, line
, ppos
, tok
);
279 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
280 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
281 * scopes, such as `SH' closing out an `SS', are defined in the rew
285 blk_imp(MACRO_PROT_ARGS
)
292 man_block_alloc(man
, line
, ppos
, tok
);
293 man_head_alloc(man
, line
, ppos
, tok
);
296 /* Add line arguments. */
300 if ( ! man_args(man
, line
, pos
, buf
, &p
))
302 man_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 man_body_alloc(man
, line
, ppos
, tok
);
324 in_line_eoln(MACRO_PROT_ARGS
)
330 man_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 man_word_append(man
, p
);
355 man_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
= MAN_NEXT_SIBLING
;
383 * Rewind our element scope. Note that when TH is pruned, we'll
384 * be back at the root, so make sure that we don't clobber as
388 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
391 if (man
->last
->type
== ROFFT_ROOT
)
399 * Same here regarding whether we're back at the root.
402 if (man
->last
->type
!= ROFFT_ROOT
)
408 man_macroend(struct man
*man
)
411 man_unscope(man
, man
->first
);
415 man_args(struct man
*man
, int line
, int *pos
, char *buf
, char **v
)
420 *v
= start
= buf
+ *pos
;
421 assert(' ' != *start
);
426 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);