]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
Multiple empty .PPs don't add a vspace either
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.45 2010/05/17 10:50:32 joerg Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
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.
8 *
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.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "libman.h"
27
28 enum rew {
29 REW_REWIND,
30 REW_NOHALT,
31 REW_HALT
32 };
33
34 static int blk_close(MACRO_PROT_ARGS);
35 static int blk_exp(MACRO_PROT_ARGS);
36 static int blk_imp(MACRO_PROT_ARGS);
37 static int in_line_eoln(MACRO_PROT_ARGS);
38
39 static int rew_scope(enum man_type,
40 struct man *, enum mant);
41 static enum rew rew_dohalt(enum mant, enum man_type,
42 const struct man_node *);
43 static enum rew rew_block(enum mant, enum man_type,
44 const struct man_node *);
45 static int rew_warn(struct man *,
46 struct man_node *, enum merr);
47
48 const struct man_macro __man_macros[MAN_MAX] = {
49 { in_line_eoln, MAN_NSCOPED }, /* br */
50 { in_line_eoln, 0 }, /* TH */
51 { blk_imp, MAN_SCOPED }, /* SH */
52 { blk_imp, MAN_SCOPED }, /* SS */
53 { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
54 { blk_imp, 0 }, /* LP */
55 { blk_imp, 0 }, /* PP */
56 { blk_imp, 0 }, /* P */
57 { blk_imp, 0 }, /* IP */
58 { blk_imp, 0 }, /* HP */
59 { in_line_eoln, MAN_SCOPED }, /* SM */
60 { in_line_eoln, MAN_SCOPED }, /* SB */
61 { in_line_eoln, 0 }, /* BI */
62 { in_line_eoln, 0 }, /* IB */
63 { in_line_eoln, 0 }, /* BR */
64 { in_line_eoln, 0 }, /* RB */
65 { in_line_eoln, MAN_SCOPED }, /* R */
66 { in_line_eoln, MAN_SCOPED }, /* B */
67 { in_line_eoln, MAN_SCOPED }, /* I */
68 { in_line_eoln, 0 }, /* IR */
69 { in_line_eoln, 0 }, /* RI */
70 { in_line_eoln, MAN_NSCOPED }, /* na */
71 { in_line_eoln, 0 }, /* i */
72 { in_line_eoln, MAN_NSCOPED }, /* sp */
73 { in_line_eoln, 0 }, /* nf */
74 { in_line_eoln, 0 }, /* fi */
75 { in_line_eoln, 0 }, /* r */
76 { blk_close, 0 }, /* RE */
77 { blk_exp, MAN_EXPLICIT }, /* RS */
78 { in_line_eoln, 0 }, /* DT */
79 { in_line_eoln, 0 }, /* UC */
80 { in_line_eoln, 0 }, /* PD */
81 { in_line_eoln, MAN_NSCOPED }, /* Sp */
82 { in_line_eoln, 0 }, /* Vb */
83 { in_line_eoln, 0 }, /* Ve */
84 { in_line_eoln, 0 }, /* AT */
85 };
86
87 const struct man_macro * const man_macros = __man_macros;
88
89
90 /*
91 * Warn when "n" is an explicit non-roff macro.
92 */
93 static int
94 rew_warn(struct man *m, struct man_node *n, enum merr er)
95 {
96
97 if (er == WERRMAX || MAN_BLOCK != n->type)
98 return(1);
99 if (MAN_VALID & n->flags)
100 return(1);
101 if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
102 return(1);
103 return(man_nwarn(m, n, er));
104 }
105
106
107 /*
108 * Rewind scope. If a code "er" != WERRMAX has been provided, it will
109 * be used if an explicit block scope is being closed out.
110 */
111 int
112 man_unscope(struct man *m, const struct man_node *n, enum merr er)
113 {
114
115 assert(n);
116
117 /* LINTED */
118 while (m->last != n) {
119 if ( ! rew_warn(m, m->last, er))
120 return(0);
121 if ( ! man_valid_post(m))
122 return(0);
123 if ( ! man_action_post(m))
124 return(0);
125 m->last = m->last->parent;
126 assert(m->last);
127 }
128
129 if ( ! rew_warn(m, m->last, er))
130 return(0);
131 if ( ! man_valid_post(m))
132 return(0);
133 if ( ! man_action_post(m))
134 return(0);
135
136 m->next = MAN_ROOT == m->last->type ?
137 MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
138
139 return(1);
140 }
141
142
143 static enum rew
144 rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
145 {
146
147 if (MAN_BLOCK == type && ntok == n->parent->tok &&
148 MAN_BODY == n->parent->type)
149 return(REW_REWIND);
150 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
151 }
152
153
154 /*
155 * There are three scope levels: scoped to the root (all), scoped to the
156 * section (all less sections), and scoped to subsections (all less
157 * sections and subsections).
158 */
159 static enum rew
160 rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
161 {
162 enum rew c;
163
164 /* We cannot progress beyond the root ever. */
165 if (MAN_ROOT == n->type)
166 return(REW_HALT);
167
168 assert(n->parent);
169
170 /* Normal nodes shouldn't go to the level of the root. */
171 if (MAN_ROOT == n->parent->type)
172 return(REW_REWIND);
173
174 /* Already-validated nodes should be closed out. */
175 if (MAN_VALID & n->flags)
176 return(REW_NOHALT);
177
178 /* First: rewind to ourselves. */
179 if (type == n->type && tok == n->tok)
180 return(REW_REWIND);
181
182 /*
183 * Next follow the implicit scope-smashings as defined by man.7:
184 * section, sub-section, etc.
185 */
186
187 switch (tok) {
188 case (MAN_SH):
189 break;
190 case (MAN_SS):
191 /* Rewind to a section, if a block. */
192 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
193 return(c);
194 break;
195 case (MAN_RS):
196 /* Rewind to a subsection, if a block. */
197 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
198 return(c);
199 /* Rewind to a section, if a block. */
200 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
201 return(c);
202 break;
203 default:
204 /* Rewind to an offsetter, if a block. */
205 if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
206 return(c);
207 /* Rewind to a subsection, if a block. */
208 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
209 return(c);
210 /* Rewind to a section, if a block. */
211 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
212 return(c);
213 break;
214 }
215
216 return(REW_NOHALT);
217 }
218
219
220 /*
221 * Rewinding entails ascending the parse tree until a coherent point,
222 * for example, the `SH' macro will close out any intervening `SS'
223 * scopes. When a scope is closed, it must be validated and actioned.
224 */
225 static int
226 rew_scope(enum man_type type, struct man *m, enum mant tok)
227 {
228 struct man_node *n;
229 enum rew c;
230
231 /* LINTED */
232 for (n = m->last; n; n = n->parent) {
233 /*
234 * Whether we should stop immediately (REW_HALT), stop
235 * and rewind until this point (REW_REWIND), or keep
236 * rewinding (REW_NOHALT).
237 */
238 c = rew_dohalt(tok, type, n);
239 if (REW_HALT == c)
240 return(1);
241 if (REW_REWIND == c)
242 break;
243 }
244
245 /*
246 * Rewind until the current point. Warn if we're a roff
247 * instruction that's mowing over explicit scopes.
248 */
249 assert(n);
250
251 return(man_unscope(m, n, WERRMAX));
252 }
253
254
255 /*
256 * Close out a generic explicit macro.
257 */
258 /* ARGSUSED */
259 int
260 blk_close(MACRO_PROT_ARGS)
261 {
262 enum mant ntok;
263 const struct man_node *nn;
264
265 switch (tok) {
266 case (MAN_RE):
267 ntok = MAN_RS;
268 break;
269 default:
270 abort();
271 /* NOTREACHED */
272 }
273
274 for (nn = m->last->parent; nn; nn = nn->parent)
275 if (ntok == nn->tok)
276 break;
277
278 if (NULL == nn)
279 if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
280 return(0);
281
282 if ( ! rew_scope(MAN_BODY, m, ntok))
283 return(0);
284 if ( ! rew_scope(MAN_BLOCK, m, ntok))
285 return(0);
286
287 return(1);
288 }
289
290
291 int
292 blk_exp(MACRO_PROT_ARGS)
293 {
294 int w, la;
295 char *p;
296
297 /*
298 * Close out prior scopes. "Regular" explicit macros cannot be
299 * nested, but we allow roff macros to be placed just about
300 * anywhere.
301 */
302
303 if ( ! rew_scope(MAN_BODY, m, tok))
304 return(0);
305 if ( ! rew_scope(MAN_BLOCK, m, tok))
306 return(0);
307
308 if ( ! man_block_alloc(m, line, ppos, tok))
309 return(0);
310 if ( ! man_head_alloc(m, line, ppos, tok))
311 return(0);
312
313 for (;;) {
314 la = *pos;
315 w = man_args(m, line, pos, buf, &p);
316
317 if (-1 == w)
318 return(0);
319 if (0 == w)
320 break;
321
322 if ( ! man_word_alloc(m, line, la, p))
323 return(0);
324 }
325
326 assert(m);
327 assert(tok != MAN_MAX);
328
329 if ( ! rew_scope(MAN_HEAD, m, tok))
330 return(0);
331 return(man_body_alloc(m, line, ppos, tok));
332 }
333
334
335
336 /*
337 * Parse an implicit-block macro. These contain a MAN_HEAD and a
338 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
339 * scopes, such as `SH' closing out an `SS', are defined in the rew
340 * routines.
341 */
342 int
343 blk_imp(MACRO_PROT_ARGS)
344 {
345 int w, la;
346 char *p;
347 struct man_node *n;
348
349 /* Close out prior scopes. */
350
351 if ( ! rew_scope(MAN_BODY, m, tok))
352 return(0);
353 if ( ! rew_scope(MAN_BLOCK, m, tok))
354 return(0);
355
356 /* Allocate new block & head scope. */
357
358 if ( ! man_block_alloc(m, line, ppos, tok))
359 return(0);
360 if ( ! man_head_alloc(m, line, ppos, tok))
361 return(0);
362
363 n = m->last;
364
365 /* Add line arguments. */
366
367 for (;;) {
368 la = *pos;
369 w = man_args(m, line, pos, buf, &p);
370
371 if (-1 == w)
372 return(0);
373 if (0 == w)
374 break;
375
376 if ( ! man_word_alloc(m, line, la, p))
377 return(0);
378 }
379
380 /* Close out head and open body (unless MAN_SCOPE). */
381
382 if (MAN_SCOPED & man_macros[tok].flags) {
383 /* If we're forcing scope (`TP'), keep it open. */
384 if (MAN_FSCOPED & man_macros[tok].flags) {
385 m->flags |= MAN_BLINE;
386 return(1);
387 } else if (n == m->last) {
388 m->flags |= MAN_BLINE;
389 return(1);
390 }
391 }
392
393 if ( ! rew_scope(MAN_HEAD, m, tok))
394 return(0);
395 return(man_body_alloc(m, line, ppos, tok));
396 }
397
398
399 int
400 in_line_eoln(MACRO_PROT_ARGS)
401 {
402 int w, la;
403 char *p;
404 struct man_node *n;
405
406 if ( ! man_elem_alloc(m, line, ppos, tok))
407 return(0);
408
409 n = m->last;
410
411 for (;;) {
412 la = *pos;
413 w = man_args(m, line, pos, buf, &p);
414
415 if (-1 == w)
416 return(0);
417 if (0 == w)
418 break;
419 if ( ! man_word_alloc(m, line, la, p))
420 return(0);
421 }
422
423 /*
424 * If no arguments are specified and this is MAN_SCOPED (i.e.,
425 * next-line scoped), then set our mode to indicate that we're
426 * waiting for terms to load into our context.
427 */
428
429 if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
430 assert( ! (MAN_NSCOPED & man_macros[tok].flags));
431 m->flags |= MAN_ELINE;
432 return(1);
433 }
434
435 /* Set ignorable context, if applicable. */
436
437 if (MAN_NSCOPED & man_macros[tok].flags) {
438 assert( ! (MAN_SCOPED & man_macros[tok].flags));
439 m->flags |= MAN_ILINE;
440 }
441
442 /*
443 * Rewind our element scope. Note that when TH is pruned, we'll
444 * be back at the root, so make sure that we don't clobber as
445 * its sibling.
446 */
447
448 for ( ; m->last; m->last = m->last->parent) {
449 if (m->last == n)
450 break;
451 if (m->last->type == MAN_ROOT)
452 break;
453 if ( ! man_valid_post(m))
454 return(0);
455 if ( ! man_action_post(m))
456 return(0);
457 }
458
459 assert(m->last);
460
461 /*
462 * Same here regarding whether we're back at the root.
463 */
464
465 if (m->last->type != MAN_ROOT && ! man_valid_post(m))
466 return(0);
467 if (m->last->type != MAN_ROOT && ! man_action_post(m))
468 return(0);
469
470 m->next = MAN_ROOT == m->last->type ?
471 MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
472
473 return(1);
474 }
475
476
477 int
478 man_macroend(struct man *m)
479 {
480
481 return(man_unscope(m, m->first, WEXITSCOPE));
482 }
483