1 /* Id: man_macro.c,v 1.80 2013/12/31 23:23:10 schwarze Exp */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2013 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 AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
30 #include "libmandoc.h"
39 static int blk_close(MACRO_PROT_ARGS
);
40 static int blk_exp(MACRO_PROT_ARGS
);
41 static int blk_imp(MACRO_PROT_ARGS
);
42 static int in_line_eoln(MACRO_PROT_ARGS
);
43 static int man_args(struct man
*, int,
44 int *, char *, char **);
46 static int rew_scope(enum man_type
,
47 struct man
*, enum mant
);
48 static enum rew
rew_dohalt(enum mant
, enum man_type
,
49 const struct man_node
*);
50 static enum rew
rew_block(enum mant
, enum man_type
,
51 const struct man_node
*);
52 static void rew_warn(struct man
*,
53 struct man_node
*, enum mandocerr
);
55 const struct man_macro __man_macros
[MAN_MAX
] = {
56 { in_line_eoln
, MAN_NSCOPED
}, /* br */
57 { in_line_eoln
, MAN_BSCOPE
}, /* TH */
58 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SH */
59 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
}, /* SS */
60 { blk_imp
, MAN_BSCOPE
| MAN_SCOPED
| MAN_FSCOPED
}, /* TP */
61 { blk_imp
, MAN_BSCOPE
}, /* LP */
62 { blk_imp
, MAN_BSCOPE
}, /* PP */
63 { blk_imp
, MAN_BSCOPE
}, /* P */
64 { blk_imp
, MAN_BSCOPE
}, /* IP */
65 { blk_imp
, MAN_BSCOPE
}, /* HP */
66 { in_line_eoln
, MAN_SCOPED
}, /* SM */
67 { in_line_eoln
, MAN_SCOPED
}, /* SB */
68 { in_line_eoln
, 0 }, /* BI */
69 { in_line_eoln
, 0 }, /* IB */
70 { in_line_eoln
, 0 }, /* BR */
71 { in_line_eoln
, 0 }, /* RB */
72 { in_line_eoln
, MAN_SCOPED
}, /* R */
73 { in_line_eoln
, MAN_SCOPED
}, /* B */
74 { in_line_eoln
, MAN_SCOPED
}, /* I */
75 { in_line_eoln
, 0 }, /* IR */
76 { in_line_eoln
, 0 }, /* RI */
77 { in_line_eoln
, MAN_NSCOPED
}, /* na */
78 { in_line_eoln
, MAN_NSCOPED
}, /* sp */
79 { in_line_eoln
, MAN_BSCOPE
}, /* nf */
80 { in_line_eoln
, MAN_BSCOPE
}, /* fi */
81 { blk_close
, 0 }, /* RE */
82 { blk_exp
, MAN_BSCOPE
| MAN_EXPLICIT
}, /* RS */
83 { in_line_eoln
, 0 }, /* DT */
84 { in_line_eoln
, 0 }, /* UC */
85 { in_line_eoln
, 0 }, /* PD */
86 { in_line_eoln
, 0 }, /* AT */
87 { in_line_eoln
, 0 }, /* in */
88 { in_line_eoln
, 0 }, /* ft */
89 { in_line_eoln
, 0 }, /* OP */
90 { in_line_eoln
, MAN_BSCOPE
}, /* EX */
91 { in_line_eoln
, MAN_BSCOPE
}, /* EE */
92 { blk_exp
, MAN_BSCOPE
| MAN_EXPLICIT
}, /* UR */
93 { blk_close
, 0 }, /* UE */
96 const struct man_macro
* const man_macros
= __man_macros
;
100 * Warn when "n" is an explicit non-roff macro.
103 rew_warn(struct man
*man
, struct man_node
*n
, enum mandocerr er
)
106 if (er
== MANDOCERR_MAX
|| MAN_BLOCK
!= n
->type
)
108 if (MAN_VALID
& n
->flags
)
110 if ( ! (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
))
113 assert(er
< MANDOCERR_FATAL
);
114 man_nmsg(man
, n
, er
);
119 * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
120 * will be used if an explicit block scope is being closed out.
123 man_unscope(struct man
*man
, const struct man_node
*to
,
130 man
->next
= MAN_NEXT_SIBLING
;
133 while (man
->last
!= to
) {
135 * Save the parent here, because we may delete the
136 * man->last node in the post-validation phase and reset
137 * it to man->last->parent, causing a step in the closing
140 n
= man
->last
->parent
;
141 rew_warn(man
, man
->last
, er
);
142 if ( ! man_valid_post(man
))
148 rew_warn(man
, man
->last
, er
);
149 if ( ! man_valid_post(man
))
157 rew_block(enum mant ntok
, enum man_type type
, const struct man_node
*n
)
160 if (MAN_BLOCK
== type
&& ntok
== n
->parent
->tok
&&
161 MAN_BODY
== n
->parent
->type
)
163 return(ntok
== n
->tok
? REW_HALT
: REW_NOHALT
);
168 * There are three scope levels: scoped to the root (all), scoped to the
169 * section (all less sections), and scoped to subsections (all less
170 * sections and subsections).
173 rew_dohalt(enum mant tok
, enum man_type type
, const struct man_node
*n
)
177 /* We cannot progress beyond the root ever. */
178 if (MAN_ROOT
== n
->type
)
183 /* Normal nodes shouldn't go to the level of the root. */
184 if (MAN_ROOT
== n
->parent
->type
)
187 /* Already-validated nodes should be closed out. */
188 if (MAN_VALID
& n
->flags
)
191 /* First: rewind to ourselves. */
192 if (type
== n
->type
&& tok
== n
->tok
) {
193 if (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
)
200 * Next follow the implicit scope-smashings as defined by man.7:
201 * section, sub-section, etc.
208 /* Rewind to a section, if a block. */
209 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
213 /* Preserve empty paragraphs before RS. */
214 if (0 == n
->nchild
&& (MAN_P
== n
->tok
||
215 MAN_PP
== n
->tok
|| MAN_LP
== n
->tok
))
217 /* Rewind to a subsection, if a block. */
218 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
220 /* Rewind to a section, if a block. */
221 if (REW_NOHALT
!= (c
= rew_block(MAN_SH
, type
, n
)))
225 /* Rewind to an offsetter, if a block. */
226 if (REW_NOHALT
!= (c
= rew_block(MAN_RS
, type
, n
)))
228 /* Rewind to a subsection, if a block. */
229 if (REW_NOHALT
!= (c
= rew_block(MAN_SS
, type
, n
)))
231 /* Rewind to a section, if a block. */
232 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 man_type type
, struct man
*man
, enum mant tok
)
253 for (n
= man
->last
; n
; n
= n
->parent
) {
255 * Whether we should stop immediately (REW_HALT), stop
256 * and rewind until this point (REW_REWIND), or keep
257 * rewinding (REW_NOHALT).
259 c
= rew_dohalt(tok
, type
, n
);
267 * Rewind until the current point. Warn if we're a roff
268 * instruction that's mowing over explicit scopes.
272 return(man_unscope(man
, n
, MANDOCERR_MAX
));
277 * Close out a generic explicit macro.
281 blk_close(MACRO_PROT_ARGS
)
284 const struct man_node
*nn
;
298 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
299 if (ntok
== nn
->tok
&& MAN_BLOCK
== nn
->type
)
303 man_pmsg(man
, line
, ppos
, MANDOCERR_NOSCOPE
);
304 if ( ! rew_scope(MAN_BLOCK
, man
, MAN_PP
))
307 man_unscope(man
, nn
, MANDOCERR_MAX
);
315 blk_exp(MACRO_PROT_ARGS
)
321 /* Close out prior implicit scopes. */
323 if ( ! rew_scope(MAN_BLOCK
, man
, tok
))
326 if ( ! man_block_alloc(man
, line
, ppos
, tok
))
328 if ( ! man_head_alloc(man
, line
, ppos
, tok
))
333 if ( ! man_args(man
, line
, pos
, buf
, &p
))
335 if ( ! man_word_alloc(man
, line
, la
, p
))
340 assert(tok
!= MAN_MAX
);
342 for (n
= man
->last
; n
; n
= n
->parent
) {
345 assert(MAN_HEAD
== n
->type
);
346 man_unscope(man
, n
, MANDOCERR_MAX
);
350 return(man_body_alloc(man
, line
, ppos
, tok
));
356 * Parse an implicit-block macro. These contain a MAN_HEAD and a
357 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
358 * scopes, such as `SH' closing out an `SS', are defined in the rew
363 blk_imp(MACRO_PROT_ARGS
)
369 /* Close out prior scopes. */
371 if ( ! rew_scope(MAN_BODY
, man
, tok
))
373 if ( ! rew_scope(MAN_BLOCK
, man
, tok
))
376 /* Allocate new block & head scope. */
378 if ( ! man_block_alloc(man
, line
, ppos
, tok
))
380 if ( ! man_head_alloc(man
, line
, ppos
, tok
))
385 /* Add line arguments. */
389 if ( ! man_args(man
, line
, pos
, buf
, &p
))
391 if ( ! man_word_alloc(man
, line
, la
, p
))
395 /* Close out head and open body (unless MAN_SCOPE). */
397 if (MAN_SCOPED
& man_macros
[tok
].flags
) {
398 /* If we're forcing scope (`TP'), keep it open. */
399 if (MAN_FSCOPED
& man_macros
[tok
].flags
) {
400 man
->flags
|= MAN_BLINE
;
402 } else if (n
== man
->last
) {
403 man
->flags
|= MAN_BLINE
;
408 if ( ! rew_scope(MAN_HEAD
, man
, tok
))
410 return(man_body_alloc(man
, line
, ppos
, tok
));
416 in_line_eoln(MACRO_PROT_ARGS
)
422 if ( ! man_elem_alloc(man
, line
, ppos
, tok
))
429 if ( ! man_args(man
, line
, pos
, buf
, &p
))
431 if ( ! man_word_alloc(man
, line
, la
, p
))
436 * Append MAN_EOS in case the last snipped argument
437 * ends with a dot, e.g. `.IR syslog (3).'
440 if (n
!= man
->last
&&
441 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
442 man
->last
->flags
|= MAN_EOS
;
445 * If no arguments are specified and this is MAN_SCOPED (i.e.,
446 * next-line scoped), then set our mode to indicate that we're
447 * waiting for terms to load into our context.
450 if (n
== man
->last
&& MAN_SCOPED
& man_macros
[tok
].flags
) {
451 assert( ! (MAN_NSCOPED
& man_macros
[tok
].flags
));
452 man
->flags
|= MAN_ELINE
;
456 /* Set ignorable context, if applicable. */
458 if (MAN_NSCOPED
& man_macros
[tok
].flags
) {
459 assert( ! (MAN_SCOPED
& man_macros
[tok
].flags
));
460 man
->flags
|= MAN_ILINE
;
463 assert(MAN_ROOT
!= man
->last
->type
);
464 man
->next
= MAN_NEXT_SIBLING
;
467 * Rewind our element scope. Note that when TH is pruned, we'll
468 * be back at the root, so make sure that we don't clobber as
472 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
475 if (man
->last
->type
== MAN_ROOT
)
477 if ( ! man_valid_post(man
))
484 * Same here regarding whether we're back at the root.
487 if (man
->last
->type
!= MAN_ROOT
&& ! man_valid_post(man
))
495 man_macroend(struct man
*man
)
498 return(man_unscope(man
, man
->first
, MANDOCERR_SCOPEEXIT
));
502 man_args(struct man
*man
, int line
, int *pos
, char *buf
, char **v
)
507 *v
= start
= buf
+ *pos
;
508 assert(' ' != *start
);
513 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);