]>
git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
1 /* $Id: man_macro.c,v 1.81 2014/03/30 19:47:48 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 */
94 { in_line_eoln
, 0 }, /* ll */
97 const struct man_macro
* const man_macros
= __man_macros
;
101 * Warn when "n" is an explicit non-roff macro.
104 rew_warn(struct man
*man
, struct man_node
*n
, enum mandocerr er
)
107 if (er
== MANDOCERR_MAX
|| MAN_BLOCK
!= n
->type
)
109 if (MAN_VALID
& n
->flags
)
111 if ( ! (MAN_EXPLICIT
& man_macros
[n
->tok
].flags
))
114 assert(er
< MANDOCERR_FATAL
);
115 man_nmsg(man
, n
, er
);
120 * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
121 * will be used if an explicit block scope is being closed out.
124 man_unscope(struct man
*man
, const struct man_node
*to
,
131 man
->next
= MAN_NEXT_SIBLING
;
134 while (man
->last
!= to
) {
136 * Save the parent here, because we may delete the
137 * man->last node in the post-validation phase and reset
138 * it to man->last->parent, causing a step in the closing
141 n
= man
->last
->parent
;
142 rew_warn(man
, man
->last
, er
);
143 if ( ! man_valid_post(man
))
149 rew_warn(man
, man
->last
, er
);
150 if ( ! man_valid_post(man
))
158 rew_block(enum mant ntok
, enum man_type type
, const struct man_node
*n
)
161 if (MAN_BLOCK
== type
&& ntok
== n
->parent
->tok
&&
162 MAN_BODY
== n
->parent
->type
)
164 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 man_type type
, const struct man_node
*n
)
178 /* We cannot progress beyond the root ever. */
179 if (MAN_ROOT
== n
->type
)
184 /* Normal nodes shouldn't go to the level of the root. */
185 if (MAN_ROOT
== n
->parent
->type
)
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_EXPLICIT
& man_macros
[n
->tok
].flags
)
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
)))
243 * Rewinding entails ascending the parse tree until a coherent point,
244 * for example, the `SH' macro will close out any intervening `SS'
245 * scopes. When a scope is closed, it must be validated and actioned.
248 rew_scope(enum man_type type
, struct man
*man
, enum mant tok
)
254 for (n
= man
->last
; n
; n
= n
->parent
) {
256 * Whether we should stop immediately (REW_HALT), stop
257 * and rewind until this point (REW_REWIND), or keep
258 * rewinding (REW_NOHALT).
260 c
= rew_dohalt(tok
, type
, n
);
268 * Rewind until the current point. Warn if we're a roff
269 * instruction that's mowing over explicit scopes.
273 return(man_unscope(man
, n
, MANDOCERR_MAX
));
278 * Close out a generic explicit macro.
282 blk_close(MACRO_PROT_ARGS
)
285 const struct man_node
*nn
;
299 for (nn
= man
->last
->parent
; nn
; nn
= nn
->parent
)
300 if (ntok
== nn
->tok
&& MAN_BLOCK
== nn
->type
)
304 man_pmsg(man
, line
, ppos
, MANDOCERR_NOSCOPE
);
305 if ( ! rew_scope(MAN_BLOCK
, man
, MAN_PP
))
308 man_unscope(man
, nn
, MANDOCERR_MAX
);
316 blk_exp(MACRO_PROT_ARGS
)
322 /* Close out prior implicit scopes. */
324 if ( ! rew_scope(MAN_BLOCK
, man
, tok
))
327 if ( ! man_block_alloc(man
, line
, ppos
, tok
))
329 if ( ! man_head_alloc(man
, line
, ppos
, tok
))
334 if ( ! man_args(man
, line
, pos
, buf
, &p
))
336 if ( ! man_word_alloc(man
, line
, la
, p
))
341 assert(tok
!= MAN_MAX
);
343 for (n
= man
->last
; n
; n
= n
->parent
) {
346 assert(MAN_HEAD
== n
->type
);
347 man_unscope(man
, n
, MANDOCERR_MAX
);
351 return(man_body_alloc(man
, line
, ppos
, tok
));
357 * Parse an implicit-block macro. These contain a MAN_HEAD and a
358 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
359 * scopes, such as `SH' closing out an `SS', are defined in the rew
364 blk_imp(MACRO_PROT_ARGS
)
370 /* Close out prior scopes. */
372 if ( ! rew_scope(MAN_BODY
, man
, tok
))
374 if ( ! rew_scope(MAN_BLOCK
, man
, tok
))
377 /* Allocate new block & head scope. */
379 if ( ! man_block_alloc(man
, line
, ppos
, tok
))
381 if ( ! man_head_alloc(man
, line
, ppos
, tok
))
386 /* Add line arguments. */
390 if ( ! man_args(man
, line
, pos
, buf
, &p
))
392 if ( ! man_word_alloc(man
, line
, la
, p
))
396 /* Close out head and open body (unless MAN_SCOPE). */
398 if (MAN_SCOPED
& man_macros
[tok
].flags
) {
399 /* If we're forcing scope (`TP'), keep it open. */
400 if (MAN_FSCOPED
& man_macros
[tok
].flags
) {
401 man
->flags
|= MAN_BLINE
;
403 } else if (n
== man
->last
) {
404 man
->flags
|= MAN_BLINE
;
409 if ( ! rew_scope(MAN_HEAD
, man
, tok
))
411 return(man_body_alloc(man
, line
, ppos
, tok
));
417 in_line_eoln(MACRO_PROT_ARGS
)
423 if ( ! man_elem_alloc(man
, line
, ppos
, tok
))
430 if ( ! man_args(man
, line
, pos
, buf
, &p
))
432 if ( ! man_word_alloc(man
, line
, la
, p
))
437 * Append MAN_EOS in case the last snipped argument
438 * ends with a dot, e.g. `.IR syslog (3).'
441 if (n
!= man
->last
&&
442 mandoc_eos(man
->last
->string
, strlen(man
->last
->string
)))
443 man
->last
->flags
|= MAN_EOS
;
446 * If no arguments are specified and this is MAN_SCOPED (i.e.,
447 * next-line scoped), then set our mode to indicate that we're
448 * waiting for terms to load into our context.
451 if (n
== man
->last
&& MAN_SCOPED
& man_macros
[tok
].flags
) {
452 assert( ! (MAN_NSCOPED
& man_macros
[tok
].flags
));
453 man
->flags
|= MAN_ELINE
;
457 /* Set ignorable context, if applicable. */
459 if (MAN_NSCOPED
& man_macros
[tok
].flags
) {
460 assert( ! (MAN_SCOPED
& man_macros
[tok
].flags
));
461 man
->flags
|= MAN_ILINE
;
464 assert(MAN_ROOT
!= man
->last
->type
);
465 man
->next
= MAN_NEXT_SIBLING
;
468 * Rewind our element scope. Note that when TH is pruned, we'll
469 * be back at the root, so make sure that we don't clobber as
473 for ( ; man
->last
; man
->last
= man
->last
->parent
) {
476 if (man
->last
->type
== MAN_ROOT
)
478 if ( ! man_valid_post(man
))
485 * Same here regarding whether we're back at the root.
488 if (man
->last
->type
!= MAN_ROOT
&& ! man_valid_post(man
))
496 man_macroend(struct man
*man
)
499 return(man_unscope(man
, man
->first
, MANDOCERR_SCOPEEXIT
));
503 man_args(struct man
*man
, int line
, int *pos
, char *buf
, char **v
)
508 *v
= start
= buf
+ *pos
;
509 assert(' ' != *start
);
514 *v
= mandoc_getarg(man
->parse
, v
, line
, pos
);