]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
d89298e2af5e53f39fb7a2c29502151e4d1fe7b3
1 /* $Id: man_macro.c,v 1.78 2013/12/22 13:25:17 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "libmandoc.h"
38 static int blk_close(MACRO_PROT_ARGS
);
39 static int blk_exp(MACRO_PROT_ARGS
);
40 static int blk_imp(MACRO_PROT_ARGS
);
41 static int in_line_eoln(MACRO_PROT_ARGS
);
42 static int man_args(struct man
*, int,
43 int *, char *, char **);
45 static int rew_scope(enum man_type
,
46 struct man
*, enum mant
);
47 static enum rew
rew_dohalt(enum mant
, enum man_type
,
48 const struct man_node
*);
49 static enum rew
rew_block(enum mant
, enum man_type
,
50 const struct man_node
*);
51 static void rew_warn(struct man
*,
52 struct man_node
*, enum mandocerr
);
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
| MAN_FSCOPED
}, /* 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
}, /* SM */
66 { in_line_eoln
, MAN_SCOPED
}, /* 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
}, /* R */
72 { in_line_eoln
, MAN_SCOPED
}, /* B */
73 { in_line_eoln
, MAN_SCOPED
}, /* I */
74 { in_line_eoln
, 0 }, /* IR */
75 { in_line_eoln
, 0 }, /* RI */
76 { in_line_eoln
, MAN_NSCOPED
}, /* na */
77 { in_line_eoln
, MAN_NSCOPED
}, /* sp */
78 { in_line_eoln
, MAN_BSCOPE
}, /* nf */
79 { in_line_eoln
, MAN_BSCOPE
}, /* fi */
80 { blk_close
, 0 }, /* RE */
81 { blk_exp
, MAN_BSCOPE
| MAN_EXPLICIT
}, /* RS */
82 { in_line_eoln
, 0 }, /* DT */
83 { in_line_eoln
, 0 }, /* UC */
84 { in_line_eoln
, 0 }, /* PD */
85 { in_line_eoln
, 0 }, /* AT */
86 { in_line_eoln
, 0 }, /* in */
87 { in_line_eoln
, 0 }, /* ft */
88 { in_line_eoln
, 0 }, /* OP */
89 { in_line_eoln
, MAN_BSCOPE
}, /* EX */
90 { in_line_eoln
, MAN_BSCOPE
}, /* EE */
91 { blk_exp
, MAN_BSCOPE
| MAN_EXPLICIT
}, /* UR */
92 { blk_close
, 0 }, /* UE */
95 const struct man_macro
* const man_macros
= __man_macros
;
99 * Warn when "n" is an explicit non-roff macro.
102 rew_warn(struct man
*man
, struct man_node
*n
, enum mandocerr er
)
105 if (er
== MANDOCERR_MAX
|| MAN_BLOCK
!= n
->type
)
107 if (MAN_VALID
& n
->flags
)
109 if ( ! (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
))
112 assert(er
< MANDOCERR_FATAL
);
113 man_nmsg(man
, n
, er
);
118 * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
119 * will be used if an explicit block scope is being closed out.
122 man_unscope(struct man
*man
, const struct man_node
*to
,
129 man
->next
= MAN_NEXT_SIBLING
;
132 while (man
->last
!= to
) {
134 * Save the parent here, because we may delete the
135 * man->last node in the post-validation phase and reset
136 * it to man->last->parent, causing a step in the closing
139 n
= man
->last
->parent
;
140 rew_warn(man
, man
->last
, er
);
141 if ( ! man_valid_post(man
))
147 rew_warn(man
, man
->last
, er
);
148 if ( ! man_valid_post(man
))
156 rew_block(enum mant ntok
, enum man_type type
, const struct man_node
*n
)
159 if (MAN_BLOCK
== type
&& ntok
== n
->parent
->tok
&&
160 MAN_BODY
== n
->parent
->type
)
162 return(ntok
== n
->tok
? REW_HALT
: REW_NOHALT
);
167 * There are three scope levels: scoped to the root (all), scoped to the
168 * section (all less sections), and scoped to subsections (all less
169 * sections and subsections).
172 rew_dohalt(enum mant tok
, enum man_type type
, const struct man_node
*n
)
176 /* We cannot progress beyond the root ever. */
177 if (MAN_ROOT
== n
->type
)
182 /* Normal nodes shouldn't go to the level of the root. */
183 if (MAN_ROOT
== n
->parent
->type
)
186 /* Already-validated nodes should be closed out. */
187 if (MAN_VALID
& n
->flags
)
190 /* First: rewind to ourselves. */
191 if (type
== n
->type
&& tok
== n
->tok
) {
192 if (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
)
199 * Next follow the implicit scope-smashings as defined by man.7:
200 * section, sub-section, etc.
207 /* Rewind to a section, if a block. */
208 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
212 /* Preserve empty paragraphs before RS. */
213 if (0 == n
->nchild
&& (MAN_P
== n
->tok
||
214 MAN_PP
== n
->tok
|| MAN_LP
== n
->tok
))
216 /* Rewind to a subsection, if a block. */
217 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
219 /* Rewind to a section, if a block. */
220 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
224 /* Rewind to an offsetter, if a block. */
225 if (REW_NOHALT
!= (c
= rew_block(MAN_RS
, type
, n
)))
227 /* Rewind to a subsection, if a block. */
228 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
230 /* Rewind to a section, if a block. */
231 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
241 * Rewinding entails ascending the parse tree until a coherent point,
242 * for example, the `SH' macro will close out any intervening `SS'
243 * scopes. When a scope is closed, it must be validated and actioned.
246 rew_scope(enum man_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.
271 return(man_unscope(man
, n
, MANDOCERR_MAX
));
276 * Close out a generic explicit macro.
280 blk_close(MACRO_PROT_ARGS
)
283 const struct man_node
*nn
;
297 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
298 if (ntok
== nn
->tok
&& MAN_BLOCK
== nn
->type
)
302 man_pmsg(man
, line
, ppos
, MANDOCERR_NOSCOPE
);
303 if ( ! rew_scope(MAN_BLOCK
, man
, MAN_PP
))
306 man_unscope(man
, nn
, MANDOCERR_MAX
);
314 blk_exp(MACRO_PROT_ARGS
)
320 /* Close out prior implicit scopes. */
322 if ( ! rew_scope(MAN_BLOCK
, man
, tok
))
325 if ( ! man_block_alloc(man
, line
, ppos
, tok
))
327 if ( ! man_head_alloc(man
, line
, ppos
, tok
))
332 if ( ! man_args(man
, line
, pos
, buf
, &p
))
334 if ( ! man_word_alloc(man
, line
, la
, p
))
339 assert(tok
!= MAN_MAX
);
341 for (n
= man
->last
; n
; n
= n
->parent
) {
344 assert(MAN_HEAD
== n
->type
);
345 man_unscope(man
, n
, MANDOCERR_MAX
);
349 return(man_body_alloc(man
, line
, ppos
, tok
));
355 * Parse an implicit-block macro. These contain a MAN_HEAD and a
356 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
357 * scopes, such as `SH' closing out an `SS', are defined in the rew
362 blk_imp(MACRO_PROT_ARGS
)
368 /* Close out prior scopes. */
370 if ( ! rew_scope(MAN_BODY
, man
, tok
))
372 if ( ! rew_scope(MAN_BLOCK
, man
, tok
))
375 /* Allocate new block & head scope. */
377 if ( ! man_block_alloc(man
, line
, ppos
, tok
))
379 if ( ! man_head_alloc(man
, line
, ppos
, tok
))
384 /* Add line arguments. */
388 if ( ! man_args(man
, line
, pos
, buf
, &p
))
390 if ( ! man_word_alloc(man
, line
, la
, p
))
394 /* Close out head and open body (unless MAN_SCOPE). */
396 if (MAN_SCOPED
& man_macros
[tok
].flags
) {
397 /* If we're forcing scope (`TP'), keep it open. */
398 if (MAN_FSCOPED
& man_macros
[tok
].flags
) {
399 man
->flags
|= MAN_BLINE
;
401 } else if (n
== man
->last
) {
402 man
->flags
|= MAN_BLINE
;
407 if ( ! rew_scope(MAN_HEAD
, man
, tok
))
409 return(man_body_alloc(man
, line
, ppos
, tok
));
415 in_line_eoln(MACRO_PROT_ARGS
)
421 if ( ! man_elem_alloc(man
, line
, ppos
, tok
))
428 if ( ! man_args(man
, line
, pos
, buf
, &p
))
430 if ( ! man_word_alloc(man
, line
, la
, p
))
435 * Append MAN_EOS in case the last snipped argument
436 * ends with a dot, e.g. `.IR syslog (3).'
439 if (n
!= man
->last
&&
440 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
), 0))
441 man
->last
->flags
|= MAN_EOS
;
444 * If no arguments are specified and this is MAN_SCOPED (i.e.,
445 * next-line scoped), then set our mode to indicate that we're
446 * waiting for terms to load into our context.
449 if (n
== man
->last
&& MAN_SCOPED
& man_macros
[tok
].flags
) {
450 assert( ! (MAN_NSCOPED
& man_macros
[tok
].flags
));
451 man
->flags
|= MAN_ELINE
;
455 /* Set ignorable context, if applicable. */
457 if (MAN_NSCOPED
& man_macros
[tok
].flags
) {
458 assert( ! (MAN_SCOPED
& man_macros
[tok
].flags
));
459 man
->flags
|= MAN_ILINE
;
462 assert(MAN_ROOT
!= man
->last
->type
);
463 man
->next
= MAN_NEXT_SIBLING
;
466 * Rewind our element scope. Note that when TH is pruned, we'll
467 * be back at the root, so make sure that we don't clobber as
471 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
474 if (man
->last
->type
== MAN_ROOT
)
476 if ( ! man_valid_post(man
))
483 * Same here regarding whether we're back at the root.
486 if (man
->last
->type
!= MAN_ROOT
&& ! man_valid_post(man
))
494 man_macroend(struct man
*man
)
497 return(man_unscope(man
, man
->first
, MANDOCERR_SCOPEEXIT
));
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
);