]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
bugfix: make sure all variables are properly initialized
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.81 2014/03/30 19:47:48 schwarze Exp $ */
2 /*
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>
6 *
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.
10 *
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.
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "man.h"
29 #include "mandoc.h"
30 #include "libmandoc.h"
31 #include "libman.h"
32
33 enum rew {
34 REW_REWIND,
35 REW_NOHALT,
36 REW_HALT
37 };
38
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 **);
45
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);
54
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 */
95 };
96
97 const struct man_macro * const man_macros = __man_macros;
98
99
100 /*
101 * Warn when "n" is an explicit non-roff macro.
102 */
103 static void
104 rew_warn(struct man *man, struct man_node *n, enum mandocerr er)
105 {
106
107 if (er == MANDOCERR_MAX || MAN_BLOCK != n->type)
108 return;
109 if (MAN_VALID & n->flags)
110 return;
111 if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
112 return;
113
114 assert(er < MANDOCERR_FATAL);
115 man_nmsg(man, n, er);
116 }
117
118
119 /*
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.
122 */
123 int
124 man_unscope(struct man *man, const struct man_node *to,
125 enum mandocerr er)
126 {
127 struct man_node *n;
128
129 assert(to);
130
131 man->next = MAN_NEXT_SIBLING;
132
133 /* LINTED */
134 while (man->last != to) {
135 /*
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
139 * out to be lost.
140 */
141 n = man->last->parent;
142 rew_warn(man, man->last, er);
143 if ( ! man_valid_post(man))
144 return(0);
145 man->last = n;
146 assert(man->last);
147 }
148
149 rew_warn(man, man->last, er);
150 if ( ! man_valid_post(man))
151 return(0);
152
153 return(1);
154 }
155
156
157 static enum rew
158 rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
159 {
160
161 if (MAN_BLOCK == type && ntok == n->parent->tok &&
162 MAN_BODY == n->parent->type)
163 return(REW_REWIND);
164 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
165 }
166
167
168 /*
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).
172 */
173 static enum rew
174 rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
175 {
176 enum rew c;
177
178 /* We cannot progress beyond the root ever. */
179 if (MAN_ROOT == n->type)
180 return(REW_HALT);
181
182 assert(n->parent);
183
184 /* Normal nodes shouldn't go to the level of the root. */
185 if (MAN_ROOT == n->parent->type)
186 return(REW_REWIND);
187
188 /* Already-validated nodes should be closed out. */
189 if (MAN_VALID & n->flags)
190 return(REW_NOHALT);
191
192 /* First: rewind to ourselves. */
193 if (type == n->type && tok == n->tok) {
194 if (MAN_EXPLICIT & man_macros[n->tok].flags)
195 return(REW_HALT);
196 else
197 return(REW_REWIND);
198 }
199
200 /*
201 * Next follow the implicit scope-smashings as defined by man.7:
202 * section, sub-section, etc.
203 */
204
205 switch (tok) {
206 case (MAN_SH):
207 break;
208 case (MAN_SS):
209 /* Rewind to a section, if a block. */
210 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
211 return(c);
212 break;
213 case (MAN_RS):
214 /* Preserve empty paragraphs before RS. */
215 if (0 == n->nchild && (MAN_P == n->tok ||
216 MAN_PP == n->tok || MAN_LP == n->tok))
217 return(REW_HALT);
218 /* Rewind to a subsection, if a block. */
219 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
220 return(c);
221 /* Rewind to a section, if a block. */
222 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
223 return(c);
224 break;
225 default:
226 /* Rewind to an offsetter, if a block. */
227 if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
228 return(c);
229 /* Rewind to a subsection, if a block. */
230 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
231 return(c);
232 /* Rewind to a section, if a block. */
233 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
234 return(c);
235 break;
236 }
237
238 return(REW_NOHALT);
239 }
240
241
242 /*
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.
246 */
247 static int
248 rew_scope(enum man_type type, struct man *man, enum mant tok)
249 {
250 struct man_node *n;
251 enum rew c;
252
253 /* LINTED */
254 for (n = man->last; n; n = n->parent) {
255 /*
256 * Whether we should stop immediately (REW_HALT), stop
257 * and rewind until this point (REW_REWIND), or keep
258 * rewinding (REW_NOHALT).
259 */
260 c = rew_dohalt(tok, type, n);
261 if (REW_HALT == c)
262 return(1);
263 if (REW_REWIND == c)
264 break;
265 }
266
267 /*
268 * Rewind until the current point. Warn if we're a roff
269 * instruction that's mowing over explicit scopes.
270 */
271 assert(n);
272
273 return(man_unscope(man, n, MANDOCERR_MAX));
274 }
275
276
277 /*
278 * Close out a generic explicit macro.
279 */
280 /* ARGSUSED */
281 int
282 blk_close(MACRO_PROT_ARGS)
283 {
284 enum mant ntok;
285 const struct man_node *nn;
286
287 switch (tok) {
288 case (MAN_RE):
289 ntok = MAN_RS;
290 break;
291 case (MAN_UE):
292 ntok = MAN_UR;
293 break;
294 default:
295 abort();
296 /* NOTREACHED */
297 }
298
299 for (nn = man->last->parent; nn; nn = nn->parent)
300 if (ntok == nn->tok && MAN_BLOCK == nn->type)
301 break;
302
303 if (NULL == nn) {
304 man_pmsg(man, line, ppos, MANDOCERR_NOSCOPE);
305 if ( ! rew_scope(MAN_BLOCK, man, MAN_PP))
306 return(0);
307 } else
308 man_unscope(man, nn, MANDOCERR_MAX);
309
310 return(1);
311 }
312
313
314 /* ARGSUSED */
315 int
316 blk_exp(MACRO_PROT_ARGS)
317 {
318 struct man_node *n;
319 int la;
320 char *p;
321
322 /* Close out prior implicit scopes. */
323
324 if ( ! rew_scope(MAN_BLOCK, man, tok))
325 return(0);
326
327 if ( ! man_block_alloc(man, line, ppos, tok))
328 return(0);
329 if ( ! man_head_alloc(man, line, ppos, tok))
330 return(0);
331
332 for (;;) {
333 la = *pos;
334 if ( ! man_args(man, line, pos, buf, &p))
335 break;
336 if ( ! man_word_alloc(man, line, la, p))
337 return(0);
338 }
339
340 assert(man);
341 assert(tok != MAN_MAX);
342
343 for (n = man->last; n; n = n->parent) {
344 if (n->tok != tok)
345 continue;
346 assert(MAN_HEAD == n->type);
347 man_unscope(man, n, MANDOCERR_MAX);
348 break;
349 }
350
351 return(man_body_alloc(man, line, ppos, tok));
352 }
353
354
355
356 /*
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
360 * routines.
361 */
362 /* ARGSUSED */
363 int
364 blk_imp(MACRO_PROT_ARGS)
365 {
366 int la;
367 char *p;
368 struct man_node *n;
369
370 /* Close out prior scopes. */
371
372 if ( ! rew_scope(MAN_BODY, man, tok))
373 return(0);
374 if ( ! rew_scope(MAN_BLOCK, man, tok))
375 return(0);
376
377 /* Allocate new block & head scope. */
378
379 if ( ! man_block_alloc(man, line, ppos, tok))
380 return(0);
381 if ( ! man_head_alloc(man, line, ppos, tok))
382 return(0);
383
384 n = man->last;
385
386 /* Add line arguments. */
387
388 for (;;) {
389 la = *pos;
390 if ( ! man_args(man, line, pos, buf, &p))
391 break;
392 if ( ! man_word_alloc(man, line, la, p))
393 return(0);
394 }
395
396 /* Close out head and open body (unless MAN_SCOPE). */
397
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;
402 return(1);
403 } else if (n == man->last) {
404 man->flags |= MAN_BLINE;
405 return(1);
406 }
407 }
408
409 if ( ! rew_scope(MAN_HEAD, man, tok))
410 return(0);
411 return(man_body_alloc(man, line, ppos, tok));
412 }
413
414
415 /* ARGSUSED */
416 int
417 in_line_eoln(MACRO_PROT_ARGS)
418 {
419 int la;
420 char *p;
421 struct man_node *n;
422
423 if ( ! man_elem_alloc(man, line, ppos, tok))
424 return(0);
425
426 n = man->last;
427
428 for (;;) {
429 la = *pos;
430 if ( ! man_args(man, line, pos, buf, &p))
431 break;
432 if ( ! man_word_alloc(man, line, la, p))
433 return(0);
434 }
435
436 /*
437 * Append MAN_EOS in case the last snipped argument
438 * ends with a dot, e.g. `.IR syslog (3).'
439 */
440
441 if (n != man->last &&
442 mandoc_eos(man->last->string, strlen(man->last->string)))
443 man->last->flags |= MAN_EOS;
444
445 /*
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.
449 */
450
451 if (n == man->last && MAN_SCOPED & man_macros[tok].flags) {
452 assert( ! (MAN_NSCOPED & man_macros[tok].flags));
453 man->flags |= MAN_ELINE;
454 return(1);
455 }
456
457 /* Set ignorable context, if applicable. */
458
459 if (MAN_NSCOPED & man_macros[tok].flags) {
460 assert( ! (MAN_SCOPED & man_macros[tok].flags));
461 man->flags |= MAN_ILINE;
462 }
463
464 assert(MAN_ROOT != man->last->type);
465 man->next = MAN_NEXT_SIBLING;
466
467 /*
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
470 * its sibling.
471 */
472
473 for ( ; man->last; man->last = man->last->parent) {
474 if (man->last == n)
475 break;
476 if (man->last->type == MAN_ROOT)
477 break;
478 if ( ! man_valid_post(man))
479 return(0);
480 }
481
482 assert(man->last);
483
484 /*
485 * Same here regarding whether we're back at the root.
486 */
487
488 if (man->last->type != MAN_ROOT && ! man_valid_post(man))
489 return(0);
490
491 return(1);
492 }
493
494
495 int
496 man_macroend(struct man *man)
497 {
498
499 return(man_unscope(man, man->first, MANDOCERR_SCOPEEXIT));
500 }
501
502 static int
503 man_args(struct man *man, int line, int *pos, char *buf, char **v)
504 {
505 char *start;
506
507 assert(*pos);
508 *v = start = buf + *pos;
509 assert(' ' != *start);
510
511 if ('\0' == *start)
512 return(0);
513
514 *v = mandoc_getarg(man->parse, v, line, pos);
515 return(1);
516 }