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