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