]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.30 2010/01/01 17:14:28 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.
28 #define REW_REWIND (0) /* See rew_scope(). */
29 #define REW_NOHALT (1) /* See rew_scope(). */
30 #define REW_HALT (2) /* See rew_scope(). */
32 static int in_line_eoln(MACRO_PROT_ARGS
);
33 static int blk_imp(MACRO_PROT_ARGS
);
34 static int blk_close(MACRO_PROT_ARGS
);
36 static int rew_scope(enum man_type
, struct man
*, int);
37 static int rew_dohalt(int, enum man_type
,
38 const struct man_node
*);
39 static int rew_block(int, enum man_type
,
40 const struct man_node
*);
42 const struct man_macro __man_macros
[MAN_MAX
] = {
43 { in_line_eoln
, 0 }, /* br */
44 { in_line_eoln
, 0 }, /* TH */
45 { blk_imp
, MAN_SCOPED
}, /* SH */
46 { blk_imp
, MAN_SCOPED
}, /* SS */
47 { blk_imp
, MAN_SCOPED
| MAN_FSCOPED
}, /* TP */
48 { blk_imp
, 0 }, /* LP */
49 { blk_imp
, 0 }, /* PP */
50 { blk_imp
, 0 }, /* P */
51 { blk_imp
, 0 }, /* IP */
52 { blk_imp
, 0 }, /* HP */
53 { in_line_eoln
, MAN_SCOPED
}, /* SM */
54 { in_line_eoln
, MAN_SCOPED
}, /* 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
}, /* R */
60 { in_line_eoln
, MAN_SCOPED
}, /* B */
61 { in_line_eoln
, MAN_SCOPED
}, /* I */
62 { in_line_eoln
, 0 }, /* IR */
63 { in_line_eoln
, 0 }, /* RI */
64 { in_line_eoln
, 0 }, /* na */
65 { in_line_eoln
, 0 }, /* i */
66 { in_line_eoln
, 0 }, /* sp */
67 { in_line_eoln
, 0 }, /* nf */
68 { in_line_eoln
, 0 }, /* fi */
69 { in_line_eoln
, 0 }, /* r */
70 { blk_close
, 0 }, /* RE */
71 { blk_imp
, MAN_EXPLICIT
}, /* RS */
72 { in_line_eoln
, 0 }, /* DT */
73 { in_line_eoln
, 0 }, /* UC */
74 { in_line_eoln
, 0 }, /* PD */
77 const struct man_macro
* const man_macros
= __man_macros
;
81 man_unscope(struct man
*m
, const struct man_node
*n
)
85 m
->next
= MAN_NEXT_SIBLING
;
88 while (m
->last
!= n
) {
89 if ( ! man_valid_post(m
))
91 if ( ! man_action_post(m
))
93 m
->last
= m
->last
->parent
;
97 if ( ! man_valid_post(m
))
99 return(man_action_post(m
));
104 rew_block(int ntok
, enum man_type type
, const struct man_node
*n
)
107 if (MAN_BLOCK
== type
&& ntok
== n
->parent
->tok
&&
108 MAN_BODY
== n
->parent
->type
)
110 return(ntok
== n
->tok
? REW_HALT
: REW_NOHALT
);
115 * There are three scope levels: scoped to the root (all), scoped to the
116 * section (all less sections), and scoped to subsections (all less
117 * sections and subsections).
120 rew_dohalt(int tok
, enum man_type type
, const struct man_node
*n
)
124 if (MAN_ROOT
== n
->type
)
127 if (MAN_ROOT
== n
->parent
->type
)
129 if (MAN_VALID
& n
->flags
)
132 /* Rewind to ourselves, first. */
133 if (type
== n
->type
&& tok
== n
->tok
)
140 /* Rewind to a section, if a block. */
141 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
145 /* Rewind to a subsection, if a block. */
146 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
148 /* Rewind to a section, if a block. */
149 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
153 /* Rewind to an offsetter, if a block. */
154 if (REW_NOHALT
!= (c
= rew_block(MAN_RS
, type
, n
)))
156 /* Rewind to a subsection, if a block. */
157 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
159 /* Rewind to a section, if a block. */
160 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
170 * Rewinding entails ascending the parse tree until a coherent point,
171 * for example, the `SH' macro will close out any intervening `SS'
172 * scopes. When a scope is closed, it must be validated and actioned.
175 rew_scope(enum man_type type
, struct man
*m
, int tok
)
181 for (n
= m
->last
; n
; n
= n
->parent
) {
183 * Whether we should stop immediately (REW_HALT), stop
184 * and rewind until this point (REW_REWIND), or keep
185 * rewinding (REW_NOHALT).
187 c
= rew_dohalt(tok
, type
, n
);
194 /* Rewind until the current point. */
197 return(man_unscope(m
, n
));
203 blk_close(MACRO_PROT_ARGS
)
206 const struct man_node
*nn
;
217 for (nn
= m
->last
->parent
; nn
; nn
= nn
->parent
)
222 if ( ! man_pwarn(m
, line
, ppos
, WNOSCOPE
))
225 if ( ! rew_scope(MAN_BODY
, m
, ntok
))
227 if ( ! rew_scope(MAN_BLOCK
, m
, ntok
))
229 m
->next
= MAN_NEXT_SIBLING
;
235 * Parse an implicit-block macro. These contain a MAN_HEAD and a
236 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
237 * scopes, such as `SH' closing out an `SS', are defined in the rew
241 blk_imp(MACRO_PROT_ARGS
)
247 /* Close out prior scopes. */
249 if ( ! rew_scope(MAN_BODY
, m
, tok
))
251 if ( ! rew_scope(MAN_BLOCK
, m
, tok
))
254 /* Allocate new block & head scope. */
256 if ( ! man_block_alloc(m
, line
, ppos
, tok
))
258 if ( ! man_head_alloc(m
, line
, ppos
, tok
))
263 /* Add line arguments. */
267 w
= man_args(m
, line
, pos
, buf
, &p
);
274 if ( ! man_word_alloc(m
, line
, la
, p
))
278 /* Close out head and open body (unless MAN_SCOPE). */
280 if (MAN_SCOPED
& man_macros
[tok
].flags
) {
281 /* If we're forcing scope (`TP'), keep it open. */
282 if (MAN_FSCOPED
& man_macros
[tok
].flags
) {
283 m
->flags
|= MAN_BLINE
;
285 } else if (n
== m
->last
) {
286 m
->flags
|= MAN_BLINE
;
291 if ( ! rew_scope(MAN_HEAD
, m
, tok
))
294 return(man_body_alloc(m
, line
, ppos
, tok
));
299 in_line_eoln(MACRO_PROT_ARGS
)
305 if ( ! man_elem_alloc(m
, line
, ppos
, tok
))
312 w
= man_args(m
, line
, pos
, buf
, &p
);
319 if ( ! man_word_alloc(m
, line
, la
, p
))
323 if (n
== m
->last
&& MAN_SCOPED
& man_macros
[tok
].flags
) {
324 m
->flags
|= MAN_ELINE
;
329 * Note that when TH is pruned, we'll be back at the root, so
330 * make sure that we don't clobber as its sibling.
333 for ( ; m
->last
; m
->last
= m
->last
->parent
) {
336 if (m
->last
->type
== MAN_ROOT
)
338 if ( ! man_valid_post(m
))
340 if ( ! man_action_post(m
))
347 * Same here regarding whether we're back at the root.
350 if (m
->last
->type
!= MAN_ROOT
&& ! man_valid_post(m
))
352 if (m
->last
->type
!= MAN_ROOT
&& ! man_action_post(m
))
354 if (m
->last
->type
!= MAN_ROOT
)
355 m
->next
= MAN_NEXT_SIBLING
;
362 man_macroend(struct man
*m
)
366 n
= MAN_VALID
& m
->last
->flags
?
367 m
->last
->parent
: m
->last
;
369 for ( ; n
; n
= n
->parent
) {
370 if (MAN_BLOCK
!= n
->type
)
372 if ( ! (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
))
374 if ( ! man_nwarn(m
, n
, WEXITSCOPE
))
378 return(man_unscope(m
, m
->first
));