]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.101 2015/04/02 21:36:50 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"
40 static void blk_close(MACRO_PROT_ARGS
);
41 static void blk_exp(MACRO_PROT_ARGS
);
42 static void blk_imp(MACRO_PROT_ARGS
);
43 static void in_line_eoln(MACRO_PROT_ARGS
);
44 static int man_args(struct man
*, int,
45 int *, char *, char **);
47 static void rew_scope(enum roff_type
,
48 struct man
*, enum mant
);
49 static enum rew
rew_dohalt(enum mant
, enum roff_type
,
50 const struct man_node
*);
51 static enum rew
rew_block(enum mant
, enum roff_type
,
52 const struct man_node
*);
54 const struct man_macro __man_macros
[MAN_MAX
] = {
55 { in_line_eoln
, MAN_NSCOPED
}, /* br */
56 { in_line_eoln
, MAN_BSCOPE
}, /* TH */
57 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SH */
58 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SS */
59 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* TP */
60 { blk_imp
, MAN_BSCOPE
}, /* LP */
61 { blk_imp
, MAN_BSCOPE
}, /* PP */
62 { blk_imp
, MAN_BSCOPE
}, /* P */
63 { blk_imp
, MAN_BSCOPE
}, /* IP */
64 { blk_imp
, MAN_BSCOPE
}, /* HP */
65 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* SM */
66 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* SB */
67 { in_line_eoln
, 0 }, /* BI */
68 { in_line_eoln
, 0 }, /* IB */
69 { in_line_eoln
, 0 }, /* BR */
70 { in_line_eoln
, 0 }, /* RB */
71 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* R */
72 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* B */
73 { in_line_eoln
, MAN_SCOPED
| MAN_JOIN
}, /* I */
74 { in_line_eoln
, 0 }, /* IR */
75 { in_line_eoln
, 0 }, /* RI */
76 { in_line_eoln
, MAN_NSCOPED
}, /* sp */
77 { in_line_eoln
, MAN_BSCOPE
}, /* nf */
78 { in_line_eoln
, MAN_BSCOPE
}, /* fi */
79 { blk_close
, MAN_BSCOPE
}, /* RE */
80 { blk_exp
, MAN_BSCOPE
}, /* RS */
81 { in_line_eoln
, 0 }, /* DT */
82 { in_line_eoln
, 0 }, /* UC */
83 { in_line_eoln
, MAN_NSCOPED
}, /* PD */
84 { in_line_eoln
, 0 }, /* AT */
85 { in_line_eoln
, 0 }, /* in */
86 { in_line_eoln
, 0 }, /* ft */
87 { in_line_eoln
, 0 }, /* OP */
88 { in_line_eoln
, MAN_BSCOPE
}, /* EX */
89 { in_line_eoln
, MAN_BSCOPE
}, /* EE */
90 { blk_exp
, MAN_BSCOPE
}, /* UR */
91 { blk_close
, MAN_BSCOPE
}, /* UE */
92 { in_line_eoln
, 0 }, /* ll */
95 const struct man_macro
* const man_macros
= __man_macros
;
99 man_unscope(struct man
*man
, const struct man_node
*to
)
107 /* Reached the end of the document? */
109 if (to
== NULL
&& ! (n
->flags
& MAN_VALID
)) {
110 if (man
->flags
& (MAN_BLINE
| MAN_ELINE
) &&
111 man_macros
[n
->tok
].flags
& MAN_SCOPED
) {
112 mandoc_vmsg(MANDOCERR_BLK_LINE
,
113 man
->parse
, n
->line
, n
->pos
,
115 man_macronames
[n
->tok
]);
116 if (man
->flags
& MAN_ELINE
)
117 man
->flags
&= ~MAN_ELINE
;
119 assert(n
->type
== ROFFT_HEAD
);
121 man
->flags
&= ~MAN_BLINE
;
125 man_node_delete(man
, man
->last
);
128 if (n
->type
== ROFFT_BLOCK
&&
129 man_macros
[n
->tok
].fp
== blk_exp
)
130 mandoc_msg(MANDOCERR_BLK_NOEND
,
131 man
->parse
, n
->line
, n
->pos
,
132 man_macronames
[n
->tok
]);
136 * We might delete the man->last node
137 * in the post-validation phase.
138 * Save a pointer to the parent such that
139 * we know where to continue the iteration.
148 * If we ended up at the parent of the node we were
149 * supposed to rewind to, that means the target node
150 * got deleted, so add the next node we parse as a child
151 * of the parent instead of as a sibling of the target.
154 man
->next
= (man
->last
== to
) ?
155 MAN_NEXT_CHILD
: MAN_NEXT_SIBLING
;
159 rew_block(enum mant ntok
, enum roff_type type
, const struct man_node
*n
)
162 if (type
== ROFFT_BLOCK
&& n
->parent
->tok
== ntok
&&
163 n
->parent
->type
== ROFFT_BODY
)
165 return(ntok
== n
->tok
? REW_HALT
: REW_NOHALT
);
169 * There are three scope levels: scoped to the root (all), scoped to the
170 * section (all less sections), and scoped to subsections (all less
171 * sections and subsections).
174 rew_dohalt(enum mant tok
, enum roff_type type
, const struct man_node
*n
)
178 /* We cannot progress beyond the root ever. */
179 if (n
->type
== ROFFT_ROOT
)
184 /* Normal nodes shouldn't go to the level of the root. */
185 if (n
->parent
->type
== ROFFT_ROOT
)
188 /* Already-validated nodes should be closed out. */
189 if (MAN_VALID
& n
->flags
)
192 /* First: rewind to ourselves. */
193 if (type
== n
->type
&& tok
== n
->tok
) {
194 if (man_macros
[n
->tok
].fp
== blk_exp
)
201 * Next follow the implicit scope-smashings as defined by man.7:
202 * section, sub-section, etc.
209 /* Rewind to a section, if a block. */
210 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
214 /* Preserve empty paragraphs before RS. */
215 if (0 == n
->nchild
&& (MAN_P
== n
->tok
||
216 MAN_PP
== n
->tok
|| MAN_LP
== n
->tok
))
218 /* Rewind to a subsection, if a block. */
219 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
221 /* Rewind to a section, if a block. */
222 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
226 /* Rewind to an offsetter, if a block. */
227 if (REW_NOHALT
!= (c
= rew_block(MAN_RS
, type
, n
)))
229 /* Rewind to a subsection, if a block. */
230 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
232 /* Rewind to a section, if a block. */
233 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
242 * Rewinding entails ascending the parse tree until a coherent point,
243 * for example, the `SH' macro will close out any intervening `SS'
244 * scopes. When a scope is closed, it must be validated and actioned.
247 rew_scope(enum roff_type type
, struct man
*man
, enum mant tok
)
252 for (n
= man
->last
; n
; n
= n
->parent
) {
254 * Whether we should stop immediately (REW_HALT), stop
255 * and rewind until this point (REW_REWIND), or keep
256 * rewinding (REW_NOHALT).
258 c
= rew_dohalt(tok
, type
, n
);
266 * Rewind until the current point. Warn if we're a roff
267 * instruction that's mowing over explicit scopes.
275 * Close out a generic explicit macro.
278 blk_close(MACRO_PROT_ARGS
)
281 const struct man_node
*nn
;
289 if ( ! man_args(man
, line
, pos
, buf
, &p
))
291 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
292 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
)
294 target
= strtol(p
, &p
, 10);
296 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, man
->parse
,
297 line
, p
- buf
, "RE ... %s", p
);
302 mandoc_vmsg(MANDOCERR_RE_NOTOPEN
, man
->parse
,
303 line
, ppos
, "RE %d", target
);
315 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
316 if (nn
->tok
== ntok
&& nn
->type
== ROFFT_BLOCK
&& ! --nrew
)
320 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, man
->parse
,
321 line
, ppos
, man_macronames
[tok
]);
322 rew_scope(ROFFT_BLOCK
, man
, MAN_PP
);
324 line
= man
->last
->line
;
325 ppos
= man
->last
->pos
;
326 ntok
= man
->last
->tok
;
327 man_unscope(man
, nn
);
329 /* Move a trailing paragraph behind the block. */
331 if (ntok
== MAN_LP
|| ntok
== MAN_PP
|| ntok
== MAN_P
) {
333 blk_imp(man
, ntok
, line
, ppos
, pos
, buf
);
339 blk_exp(MACRO_PROT_ARGS
)
341 struct man_node
*head
;
345 rew_scope(ROFFT_BLOCK
, man
, tok
);
346 man_block_alloc(man
, line
, ppos
, tok
);
347 man_head_alloc(man
, line
, ppos
, tok
);
351 if (man_args(man
, line
, pos
, buf
, &p
))
352 man_word_alloc(man
, line
, la
, p
);
354 if (buf
[*pos
] != '\0')
355 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
356 man
->parse
, line
, *pos
, "%s ... %s",
357 man_macronames
[tok
], buf
+ *pos
);
359 man_unscope(man
, head
);
360 man_body_alloc(man
, line
, ppos
, tok
);
364 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
365 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
366 * scopes, such as `SH' closing out an `SS', are defined in the rew
370 blk_imp(MACRO_PROT_ARGS
)
376 rew_scope(ROFFT_BODY
, man
, tok
);
377 rew_scope(ROFFT_BLOCK
, man
, tok
);
378 man_block_alloc(man
, line
, ppos
, tok
);
379 man_head_alloc(man
, line
, ppos
, tok
);
382 /* Add line arguments. */
386 if ( ! man_args(man
, line
, pos
, buf
, &p
))
388 man_word_alloc(man
, line
, la
, p
);
392 * For macros having optional next-line scope,
393 * keep the head open if there were no arguments.
394 * For `TP', always keep the head open.
397 if (man_macros
[tok
].flags
& MAN_SCOPED
&&
398 (tok
== MAN_TP
|| n
== man
->last
)) {
399 man
->flags
|= MAN_BLINE
;
403 /* Close out the head and open the body. */
405 rew_scope(ROFFT_HEAD
, man
, tok
);
406 man_body_alloc(man
, line
, ppos
, tok
);
410 in_line_eoln(MACRO_PROT_ARGS
)
416 man_elem_alloc(man
, line
, ppos
, tok
);
420 if (buf
[*pos
] != '\0' && (tok
== MAN_br
||
421 tok
== MAN_fi
|| tok
== MAN_nf
)) {
422 mandoc_vmsg(MANDOCERR_ARG_SKIP
,
423 man
->parse
, line
, *pos
, "%s %s",
424 man_macronames
[tok
], buf
+ *pos
);
427 if (buf
[*pos
] != '\0' && man
->last
!= n
&&
428 (tok
== MAN_PD
|| tok
== MAN_ft
|| tok
== MAN_sp
)) {
429 mandoc_vmsg(MANDOCERR_ARG_EXCESS
,
430 man
->parse
, line
, *pos
, "%s ... %s",
431 man_macronames
[tok
], buf
+ *pos
);
435 if ( ! man_args(man
, line
, pos
, buf
, &p
))
437 if (man_macros
[tok
].flags
& MAN_JOIN
&&
438 man
->last
->type
== ROFFT_TEXT
)
439 man_word_append(man
, p
);
441 man_word_alloc(man
, line
, la
, p
);
445 * Append MAN_EOS in case the last snipped argument
446 * ends with a dot, e.g. `.IR syslog (3).'
449 if (n
!= man
->last
&&
450 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
451 man
->last
->flags
|= MAN_EOS
;
454 * If no arguments are specified and this is MAN_SCOPED (i.e.,
455 * next-line scoped), then set our mode to indicate that we're
456 * waiting for terms to load into our context.
459 if (n
== man
->last
&& man_macros
[tok
].flags
& MAN_SCOPED
) {
460 assert( ! (man_macros
[tok
].flags
& MAN_NSCOPED
));
461 man
->flags
|= MAN_ELINE
;
465 assert(man
->last
->type
!= ROFFT_ROOT
);
466 man
->next
= MAN_NEXT_SIBLING
;
469 * Rewind our element scope. Note that when TH is pruned, we'll
470 * be back at the root, so make sure that we don't clobber as
474 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
477 if (man
->last
->type
== ROFFT_ROOT
)
485 * Same here regarding whether we're back at the root.
488 if (man
->last
->type
!= ROFFT_ROOT
)
494 man_macroend(struct man
*man
)
497 man_unscope(man
, man
->first
);
501 man_args(struct man
*man
, int line
, int *pos
, char *buf
, char **v
)
506 *v
= start
= buf
+ *pos
;
507 assert(' ' != *start
);
512 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);