]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.37 2010/03/25 07:39:25 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 static int blk_close(MACRO_PROT_ARGS
);
35 static int blk_dotted(MACRO_PROT_ARGS
);
36 static int blk_exp(MACRO_PROT_ARGS
);
37 static int blk_imp(MACRO_PROT_ARGS
);
38 static int in_line_eoln(MACRO_PROT_ARGS
);
40 static int rew_scope(enum man_type
,
41 struct man
*, enum mant
);
42 static enum rew
rew_dohalt(enum mant
, enum man_type
,
43 const struct man_node
*);
44 static enum rew
rew_block(enum mant
, enum man_type
,
45 const struct man_node
*);
47 const struct man_macro __man_macros
[MAN_MAX
] = {
48 { in_line_eoln
, MAN_NSCOPED
}, /* br */
49 { in_line_eoln
, 0 }, /* TH */
50 { blk_imp
, MAN_SCOPED
}, /* SH */
51 { blk_imp
, MAN_SCOPED
}, /* SS */
52 { blk_imp
, MAN_SCOPED
| MAN_FSCOPED
}, /* TP */
53 { blk_imp
, 0 }, /* LP */
54 { blk_imp
, 0 }, /* PP */
55 { blk_imp
, 0 }, /* P */
56 { blk_imp
, 0 }, /* IP */
57 { blk_imp
, 0 }, /* HP */
58 { in_line_eoln
, MAN_SCOPED
}, /* SM */
59 { in_line_eoln
, MAN_SCOPED
}, /* SB */
60 { in_line_eoln
, 0 }, /* BI */
61 { in_line_eoln
, 0 }, /* IB */
62 { in_line_eoln
, 0 }, /* BR */
63 { in_line_eoln
, 0 }, /* RB */
64 { in_line_eoln
, MAN_SCOPED
}, /* R */
65 { in_line_eoln
, MAN_SCOPED
}, /* B */
66 { in_line_eoln
, MAN_SCOPED
}, /* I */
67 { in_line_eoln
, 0 }, /* IR */
68 { in_line_eoln
, 0 }, /* RI */
69 { in_line_eoln
, MAN_NSCOPED
}, /* na */
70 { in_line_eoln
, 0 }, /* i */
71 { in_line_eoln
, MAN_NSCOPED
}, /* sp */
72 { in_line_eoln
, 0 }, /* nf */
73 { in_line_eoln
, 0 }, /* fi */
74 { in_line_eoln
, 0 }, /* r */
75 { blk_close
, 0 }, /* RE */
76 { blk_exp
, MAN_EXPLICIT
}, /* RS */
77 { in_line_eoln
, 0 }, /* DT */
78 { in_line_eoln
, 0 }, /* UC */
79 { in_line_eoln
, 0 }, /* PD */
80 { in_line_eoln
, MAN_NSCOPED
}, /* Sp */
81 { in_line_eoln
, 0 }, /* Vb */
82 { in_line_eoln
, 0 }, /* Ve */
83 { blk_exp
, MAN_EXPLICIT
| MAN_NOCLOSE
}, /* de */
84 { blk_exp
, MAN_EXPLICIT
| MAN_NOCLOSE
}, /* dei */
85 { blk_exp
, MAN_EXPLICIT
| MAN_NOCLOSE
}, /* am */
86 { blk_exp
, MAN_EXPLICIT
| MAN_NOCLOSE
}, /* ami */
87 { blk_exp
, MAN_EXPLICIT
| MAN_NOCLOSE
}, /* ig */
88 { blk_dotted
, 0 }, /* . */
91 const struct man_macro
* const man_macros
= __man_macros
;
95 man_unscope(struct man
*m
, const struct man_node
*n
)
101 while (m
->last
!= n
) {
102 if ( ! man_valid_post(m
))
104 if ( ! man_action_post(m
))
106 m
->last
= m
->last
->parent
;
110 if ( ! man_valid_post(m
))
112 if ( ! man_action_post(m
))
115 m
->next
= MAN_ROOT
== m
->last
->type
?
116 MAN_NEXT_CHILD
: MAN_NEXT_SIBLING
;
123 rew_block(enum mant ntok
, enum man_type type
, const struct man_node
*n
)
126 if (MAN_BLOCK
== type
&& ntok
== n
->parent
->tok
&&
127 MAN_BODY
== n
->parent
->type
)
129 return(ntok
== n
->tok
? REW_HALT
: REW_NOHALT
);
134 * There are three scope levels: scoped to the root (all), scoped to the
135 * section (all less sections), and scoped to subsections (all less
136 * sections and subsections).
139 rew_dohalt(enum mant tok
, enum man_type type
, const struct man_node
*n
)
143 if (MAN_ROOT
== n
->type
)
146 if (MAN_ROOT
== n
->parent
->type
)
148 if (MAN_VALID
& n
->flags
)
151 /* Rewind to ourselves, first. */
152 if (type
== n
->type
&& tok
== n
->tok
)
159 /* Rewind to a section, if a block. */
160 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
164 /* Rewind to a subsection, if a block. */
165 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
167 /* Rewind to a section, if a block. */
168 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
172 /* Rewind to an offsetter, if a block. */
173 if (REW_NOHALT
!= (c
= rew_block(MAN_RS
, type
, n
)))
175 /* Rewind to a subsection, if a block. */
176 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
178 /* Rewind to a section, if a block. */
179 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
189 * Rewinding entails ascending the parse tree until a coherent point,
190 * for example, the `SH' macro will close out any intervening `SS'
191 * scopes. When a scope is closed, it must be validated and actioned.
194 rew_scope(enum man_type type
, struct man
*m
, enum mant tok
)
200 for (n
= m
->last
; n
; n
= n
->parent
) {
202 * Whether we should stop immediately (REW_HALT), stop
203 * and rewind until this point (REW_REWIND), or keep
204 * rewinding (REW_NOHALT).
206 c
= rew_dohalt(tok
, type
, n
);
213 /* Rewind until the current point. */
216 return(man_unscope(m
, n
));
221 * Closure for dotted macros (de, dei, am, ami, ign). This must handle
222 * any of these as the parent node, so it needs special handling.
223 * Beyond this, it's the same as blk_close().
227 blk_dotted(MACRO_PROT_ARGS
)
232 for (nn
= m
->last
->parent
; nn
; nn
= nn
->parent
)
233 if (nn
->tok
== MAN_de
|| nn
->tok
== MAN_dei
||
235 nn
->tok
== MAN_ami
||
242 if ( ! man_pwarn(m
, line
, ppos
, WNOSCOPE
))
247 if ( ! rew_scope(MAN_BODY
, m
, ntok
))
249 if ( ! rew_scope(MAN_BLOCK
, m
, ntok
))
257 * Close out a generic explicit macro.
261 blk_close(MACRO_PROT_ARGS
)
264 const struct man_node
*nn
;
275 for (nn
= m
->last
->parent
; nn
; nn
= nn
->parent
)
280 if ( ! man_pwarn(m
, line
, ppos
, WNOSCOPE
))
283 if ( ! rew_scope(MAN_BODY
, m
, ntok
))
285 if ( ! rew_scope(MAN_BLOCK
, m
, ntok
))
293 blk_exp(MACRO_PROT_ARGS
)
299 * Close out prior scopes. "Regular" explicit macros cannot be
300 * nested, but we allow roff macros to be placed just about
304 if ( ! (MAN_NOCLOSE
& man_macros
[tok
].flags
)) {
305 if ( ! rew_scope(MAN_BODY
, m
, tok
))
307 if ( ! rew_scope(MAN_BLOCK
, m
, tok
))
311 if ( ! man_block_alloc(m
, line
, ppos
, tok
))
313 if ( ! man_head_alloc(m
, line
, ppos
, tok
))
318 w
= man_args(m
, line
, pos
, buf
, &p
);
325 if ( ! man_word_alloc(m
, line
, la
, p
))
330 assert(tok
!= MAN_MAX
);
332 if ( ! rew_scope(MAN_HEAD
, m
, tok
))
334 return(man_body_alloc(m
, line
, ppos
, tok
));
340 * Parse an implicit-block macro. These contain a MAN_HEAD and a
341 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
342 * scopes, such as `SH' closing out an `SS', are defined in the rew
346 blk_imp(MACRO_PROT_ARGS
)
352 /* Close out prior scopes. */
354 if ( ! rew_scope(MAN_BODY
, m
, tok
))
356 if ( ! rew_scope(MAN_BLOCK
, m
, tok
))
359 /* Allocate new block & head scope. */
361 if ( ! man_block_alloc(m
, line
, ppos
, tok
))
363 if ( ! man_head_alloc(m
, line
, ppos
, tok
))
368 /* Add line arguments. */
372 w
= man_args(m
, line
, pos
, buf
, &p
);
379 if ( ! man_word_alloc(m
, line
, la
, p
))
383 /* Close out head and open body (unless MAN_SCOPE). */
385 if (MAN_SCOPED
& man_macros
[tok
].flags
) {
386 /* If we're forcing scope (`TP'), keep it open. */
387 if (MAN_FSCOPED
& man_macros
[tok
].flags
) {
388 m
->flags
|= MAN_BLINE
;
390 } else if (n
== m
->last
) {
391 m
->flags
|= MAN_BLINE
;
396 if ( ! rew_scope(MAN_HEAD
, m
, tok
))
398 return(man_body_alloc(m
, line
, ppos
, tok
));
403 in_line_eoln(MACRO_PROT_ARGS
)
409 if ( ! man_elem_alloc(m
, line
, ppos
, tok
))
416 w
= man_args(m
, line
, pos
, buf
, &p
);
422 if ( ! man_word_alloc(m
, line
, la
, p
))
427 * If no arguments are specified and this is MAN_SCOPED (i.e.,
428 * next-line scoped), then set our mode to indicate that we're
429 * waiting for terms to load into our context.
432 if (n
== m
->last
&& MAN_SCOPED
& man_macros
[tok
].flags
) {
433 assert( ! (MAN_NSCOPED
& man_macros
[tok
].flags
));
434 m
->flags
|= MAN_ELINE
;
438 /* Set ignorable context, if applicable. */
440 if (MAN_NSCOPED
& man_macros
[tok
].flags
) {
441 assert( ! (MAN_SCOPED
& man_macros
[tok
].flags
));
442 m
->flags
|= MAN_ILINE
;
446 * Rewind our element scope. Note that when TH is pruned, we'll
447 * be back at the root, so make sure that we don't clobber as
451 for ( ; m
->last
; m
->last
= m
->last
->parent
) {
454 if (m
->last
->type
== MAN_ROOT
)
456 if ( ! man_valid_post(m
))
458 if ( ! man_action_post(m
))
465 * Same here regarding whether we're back at the root.
468 if (m
->last
->type
!= MAN_ROOT
&& ! man_valid_post(m
))
470 if (m
->last
->type
!= MAN_ROOT
&& ! man_action_post(m
))
473 m
->next
= MAN_ROOT
== m
->last
->type
?
474 MAN_NEXT_CHILD
: MAN_NEXT_SIBLING
;
481 man_macroend(struct man
*m
)
485 n
= MAN_VALID
& m
->last
->flags
?
486 m
->last
->parent
: m
->last
;
488 for ( ; n
; n
= n
->parent
) {
489 if (MAN_BLOCK
!= n
->type
)
491 if ( ! (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
))
493 if ( ! man_nwarn(m
, n
, WEXITSCOPE
))
497 return(man_unscope(m
, m
->first
));