1 /* $Id: man_macro.c,v 1.87 2014/07/30 23:01:39 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 *);
53 const struct man_macro __man_macros[MAN_MAX] = {
54 { in_line_eoln, MAN_NSCOPED }, /* br */
55 { in_line_eoln, MAN_BSCOPE }, /* TH */
56 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
57 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
58 { blk_imp, MAN_BSCOPE | MAN_SCOPED | MAN_FSCOPED }, /* TP */
59 { blk_imp, MAN_BSCOPE }, /* LP */
60 { blk_imp, MAN_BSCOPE }, /* PP */
61 { blk_imp, MAN_BSCOPE }, /* P */
62 { blk_imp, MAN_BSCOPE }, /* IP */
63 { blk_imp, MAN_BSCOPE }, /* HP */
64 { in_line_eoln, MAN_SCOPED }, /* SM */
65 { in_line_eoln, MAN_SCOPED }, /* SB */
66 { in_line_eoln, 0 }, /* BI */
67 { in_line_eoln, 0 }, /* IB */
68 { in_line_eoln, 0 }, /* BR */
69 { in_line_eoln, 0 }, /* RB */
70 { in_line_eoln, MAN_SCOPED }, /* R */
71 { in_line_eoln, MAN_SCOPED }, /* B */
72 { in_line_eoln, MAN_SCOPED }, /* I */
73 { in_line_eoln, 0 }, /* IR */
74 { in_line_eoln, 0 }, /* RI */
75 { in_line_eoln, MAN_NSCOPED }, /* na */
76 { in_line_eoln, MAN_NSCOPED }, /* sp */
77 { in_line_eoln, MAN_BSCOPE }, /* nf */
78 { in_line_eoln, MAN_BSCOPE }, /* fi */
79 { blk_close, 0 }, /* RE */
80 { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* RS */
81 { in_line_eoln, 0 }, /* DT */
82 { in_line_eoln, 0 }, /* UC */
83 { in_line_eoln, 0 }, /* PD */
84 { in_line_eoln, 0 }, /* AT */
85 { in_line_eoln, 0 }, /* in */
86 { in_line_eoln, 0 }, /* ft */
87 { in_line_eoln, 0 }, /* OP */
88 { in_line_eoln, MAN_BSCOPE }, /* EX */
89 { in_line_eoln, MAN_BSCOPE }, /* EE */
90 { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* UR */
91 { blk_close, 0 }, /* UE */
92 { in_line_eoln, 0 }, /* ll */
95 const struct man_macro * const man_macros = __man_macros;
99 man_unscope(struct man *man, const struct man_node *to)
103 man->next = MAN_NEXT_SIBLING;
108 /* Reached the end of the document? */
110 if (to == NULL && ! (n->flags & MAN_VALID)) {
111 if (man->flags & (MAN_BLINE | MAN_ELINE) &&
112 man_macros[n->tok].flags & MAN_SCOPED) {
113 mandoc_vmsg(MANDOCERR_BLK_LINE,
114 man->parse, n->line, n->pos,
116 man_macronames[n->tok]);
117 if (man->flags & MAN_ELINE)
118 man->flags &= ~MAN_ELINE;
120 assert(n->type == MAN_HEAD);
122 man->flags &= ~MAN_BLINE;
126 man_node_delete(man, man->last);
129 if (n->type == MAN_BLOCK &&
130 man_macros[n->tok].flags & MAN_EXPLICIT)
131 mandoc_msg(MANDOCERR_BLK_NOEND,
132 man->parse, n->line, n->pos,
133 man_macronames[n->tok]);
137 * We might delete the man->last node
138 * in the post-validation phase.
139 * Save a pointer to the parent such that
140 * we know where to continue the iteration.
144 if ( ! man_valid_post(man))
151 rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
154 if (MAN_BLOCK == type && ntok == n->parent->tok &&
155 MAN_BODY == n->parent->type)
157 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
161 * There are three scope levels: scoped to the root (all), scoped to the
162 * section (all less sections), and scoped to subsections (all less
163 * sections and subsections).
166 rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
170 /* We cannot progress beyond the root ever. */
171 if (MAN_ROOT == n->type)
176 /* Normal nodes shouldn't go to the level of the root. */
177 if (MAN_ROOT == n->parent->type)
180 /* Already-validated nodes should be closed out. */
181 if (MAN_VALID & n->flags)
184 /* First: rewind to ourselves. */
185 if (type == n->type && tok == n->tok) {
186 if (MAN_EXPLICIT & man_macros[n->tok].flags)
193 * Next follow the implicit scope-smashings as defined by man.7:
194 * section, sub-section, etc.
201 /* Rewind to a section, if a block. */
202 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
206 /* Preserve empty paragraphs before RS. */
207 if (0 == n->nchild && (MAN_P == n->tok ||
208 MAN_PP == n->tok || MAN_LP == n->tok))
210 /* Rewind to a subsection, if a block. */
211 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
213 /* Rewind to a section, if a block. */
214 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
218 /* Rewind to an offsetter, if a block. */
219 if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
221 /* Rewind to a subsection, if a block. */
222 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
224 /* Rewind to a section, if a block. */
225 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
234 * Rewinding entails ascending the parse tree until a coherent point,
235 * for example, the `SH' macro will close out any intervening `SS'
236 * scopes. When a scope is closed, it must be validated and actioned.
239 rew_scope(enum man_type type, struct man *man, enum mant tok)
244 for (n = man->last; n; n = n->parent) {
246 * Whether we should stop immediately (REW_HALT), stop
247 * and rewind until this point (REW_REWIND), or keep
248 * rewinding (REW_NOHALT).
250 c = rew_dohalt(tok, type, n);
258 * Rewind until the current point. Warn if we're a roff
259 * instruction that's mowing over explicit scopes.
263 return(man_unscope(man, n));
268 * Close out a generic explicit macro.
271 blk_close(MACRO_PROT_ARGS)
274 const struct man_node *nn;
288 for (nn = man->last->parent; nn; nn = nn->parent)
289 if (ntok == nn->tok && MAN_BLOCK == nn->type)
293 mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
294 line, ppos, man_macronames[tok]);
295 if ( ! rew_scope(MAN_BLOCK, man, MAN_PP))
298 man_unscope(man, nn);
304 blk_exp(MACRO_PROT_ARGS)
310 /* Close out prior implicit scopes. */
312 if ( ! rew_scope(MAN_BLOCK, man, tok))
315 if ( ! man_block_alloc(man, line, ppos, tok))
317 if ( ! man_head_alloc(man, line, ppos, tok))
322 if ( ! man_args(man, line, pos, buf, &p))
324 if ( ! man_word_alloc(man, line, la, p))
329 assert(tok != MAN_MAX);
331 for (n = man->last; n; n = n->parent) {
334 assert(MAN_HEAD == n->type);
339 return(man_body_alloc(man, line, ppos, tok));
343 * Parse an implicit-block macro. These contain a MAN_HEAD and a
344 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
345 * scopes, such as `SH' closing out an `SS', are defined in the rew
349 blk_imp(MACRO_PROT_ARGS)
355 /* Close out prior scopes. */
357 if ( ! rew_scope(MAN_BODY, man, tok))
359 if ( ! rew_scope(MAN_BLOCK, man, tok))
362 /* Allocate new block & head scope. */
364 if ( ! man_block_alloc(man, line, ppos, tok))
366 if ( ! man_head_alloc(man, line, ppos, tok))
371 /* Add line arguments. */
375 if ( ! man_args(man, line, pos, buf, &p))
377 if ( ! man_word_alloc(man, line, la, p))
381 /* Close out head and open body (unless MAN_SCOPE). */
383 if (MAN_SCOPED & man_macros[tok].flags) {
384 /* If we're forcing scope (`TP'), keep it open. */
385 if (MAN_FSCOPED & man_macros[tok].flags) {
386 man->flags |= MAN_BLINE;
388 } else if (n == man->last) {
389 man->flags |= MAN_BLINE;
394 if ( ! rew_scope(MAN_HEAD, man, tok))
396 return(man_body_alloc(man, line, ppos, tok));
400 in_line_eoln(MACRO_PROT_ARGS)
406 if ( ! man_elem_alloc(man, line, ppos, tok))
413 if ( ! man_args(man, line, pos, buf, &p))
415 if ( ! man_word_alloc(man, line, la, p))
420 * Append MAN_EOS in case the last snipped argument
421 * ends with a dot, e.g. `.IR syslog (3).'
424 if (n != man->last &&
425 mandoc_eos(man->last->string, strlen(man->last->string)))
426 man->last->flags |= MAN_EOS;
429 * If no arguments are specified and this is MAN_SCOPED (i.e.,
430 * next-line scoped), then set our mode to indicate that we're
431 * waiting for terms to load into our context.
434 if (n == man->last && MAN_SCOPED & man_macros[tok].flags) {
435 assert( ! (MAN_NSCOPED & man_macros[tok].flags));
436 man->flags |= MAN_ELINE;
440 assert(MAN_ROOT != man->last->type);
441 man->next = MAN_NEXT_SIBLING;
444 * Rewind our element scope. Note that when TH is pruned, we'll
445 * be back at the root, so make sure that we don't clobber as
449 for ( ; man->last; man->last = man->last->parent) {
452 if (man->last->type == MAN_ROOT)
454 if ( ! man_valid_post(man))
461 * Same here regarding whether we're back at the root.
464 if (man->last->type != MAN_ROOT && ! man_valid_post(man))
472 man_macroend(struct man *man)
475 return(man_unscope(man, man->first));
479 man_args(struct man *man, int line, int *pos, char *buf, char **v)
484 *v = start = buf + *pos;
485 assert(' ' != *start);
490 *v = mandoc_getarg(man->parse, v, line, pos);