]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
Clean up messages regarding excess arguments:
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.82 2014/04/20 16:46:04 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 * Rewind scope. If a code "er" != MANDOCERR_MAX has been provided, it
120 * will be used if an explicit block scope is being closed out.
121 */
122 int
123 man_unscope(struct man *man, const struct man_node *to,
124 enum mandocerr er)
125 {
126 struct man_node *n;
127
128 assert(to);
129
130 man->next = MAN_NEXT_SIBLING;
131
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 static enum rew
155 rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
156 {
157
158 if (MAN_BLOCK == type && ntok == n->parent->tok &&
159 MAN_BODY == n->parent->type)
160 return(REW_REWIND);
161 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
162 }
163
164 /*
165 * There are three scope levels: scoped to the root (all), scoped to the
166 * section (all less sections), and scoped to subsections (all less
167 * sections and subsections).
168 */
169 static enum rew
170 rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
171 {
172 enum rew c;
173
174 /* We cannot progress beyond the root ever. */
175 if (MAN_ROOT == n->type)
176 return(REW_HALT);
177
178 assert(n->parent);
179
180 /* Normal nodes shouldn't go to the level of the root. */
181 if (MAN_ROOT == n->parent->type)
182 return(REW_REWIND);
183
184 /* Already-validated nodes should be closed out. */
185 if (MAN_VALID & n->flags)
186 return(REW_NOHALT);
187
188 /* First: rewind to ourselves. */
189 if (type == n->type && tok == n->tok) {
190 if (MAN_EXPLICIT & man_macros[n->tok].flags)
191 return(REW_HALT);
192 else
193 return(REW_REWIND);
194 }
195
196 /*
197 * Next follow the implicit scope-smashings as defined by man.7:
198 * section, sub-section, etc.
199 */
200
201 switch (tok) {
202 case MAN_SH:
203 break;
204 case MAN_SS:
205 /* Rewind to a section, if a block. */
206 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
207 return(c);
208 break;
209 case MAN_RS:
210 /* Preserve empty paragraphs before RS. */
211 if (0 == n->nchild && (MAN_P == n->tok ||
212 MAN_PP == n->tok || MAN_LP == n->tok))
213 return(REW_HALT);
214 /* Rewind to a subsection, if a block. */
215 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
216 return(c);
217 /* Rewind to a section, if a block. */
218 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
219 return(c);
220 break;
221 default:
222 /* Rewind to an offsetter, if a block. */
223 if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
224 return(c);
225 /* Rewind to a subsection, if a block. */
226 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
227 return(c);
228 /* Rewind to a section, if a block. */
229 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
230 return(c);
231 break;
232 }
233
234 return(REW_NOHALT);
235 }
236
237 /*
238 * Rewinding entails ascending the parse tree until a coherent point,
239 * for example, the `SH' macro will close out any intervening `SS'
240 * scopes. When a scope is closed, it must be validated and actioned.
241 */
242 static int
243 rew_scope(enum man_type type, struct man *man, enum mant tok)
244 {
245 struct man_node *n;
246 enum rew c;
247
248 for (n = man->last; n; n = n->parent) {
249 /*
250 * Whether we should stop immediately (REW_HALT), stop
251 * and rewind until this point (REW_REWIND), or keep
252 * rewinding (REW_NOHALT).
253 */
254 c = rew_dohalt(tok, type, n);
255 if (REW_HALT == c)
256 return(1);
257 if (REW_REWIND == c)
258 break;
259 }
260
261 /*
262 * Rewind until the current point. Warn if we're a roff
263 * instruction that's mowing over explicit scopes.
264 */
265 assert(n);
266
267 return(man_unscope(man, n, MANDOCERR_MAX));
268 }
269
270
271 /*
272 * Close out a generic explicit macro.
273 */
274 int
275 blk_close(MACRO_PROT_ARGS)
276 {
277 enum mant ntok;
278 const struct man_node *nn;
279
280 switch (tok) {
281 case MAN_RE:
282 ntok = MAN_RS;
283 break;
284 case MAN_UE:
285 ntok = MAN_UR;
286 break;
287 default:
288 abort();
289 /* NOTREACHED */
290 }
291
292 for (nn = man->last->parent; nn; nn = nn->parent)
293 if (ntok == nn->tok && MAN_BLOCK == nn->type)
294 break;
295
296 if (NULL == nn) {
297 man_pmsg(man, line, ppos, MANDOCERR_NOSCOPE);
298 if ( ! rew_scope(MAN_BLOCK, man, MAN_PP))
299 return(0);
300 } else
301 man_unscope(man, nn, MANDOCERR_MAX);
302
303 return(1);
304 }
305
306 int
307 blk_exp(MACRO_PROT_ARGS)
308 {
309 struct man_node *n;
310 int la;
311 char *p;
312
313 /* Close out prior implicit scopes. */
314
315 if ( ! rew_scope(MAN_BLOCK, man, tok))
316 return(0);
317
318 if ( ! man_block_alloc(man, line, ppos, tok))
319 return(0);
320 if ( ! man_head_alloc(man, line, ppos, tok))
321 return(0);
322
323 for (;;) {
324 la = *pos;
325 if ( ! man_args(man, line, pos, buf, &p))
326 break;
327 if ( ! man_word_alloc(man, line, la, p))
328 return(0);
329 }
330
331 assert(man);
332 assert(tok != MAN_MAX);
333
334 for (n = man->last; n; n = n->parent) {
335 if (n->tok != tok)
336 continue;
337 assert(MAN_HEAD == n->type);
338 man_unscope(man, n, MANDOCERR_MAX);
339 break;
340 }
341
342 return(man_body_alloc(man, line, ppos, tok));
343 }
344
345 /*
346 * Parse an implicit-block macro. These contain a MAN_HEAD and a
347 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
348 * scopes, such as `SH' closing out an `SS', are defined in the rew
349 * routines.
350 */
351 int
352 blk_imp(MACRO_PROT_ARGS)
353 {
354 int la;
355 char *p;
356 struct man_node *n;
357
358 /* Close out prior scopes. */
359
360 if ( ! rew_scope(MAN_BODY, man, tok))
361 return(0);
362 if ( ! rew_scope(MAN_BLOCK, man, tok))
363 return(0);
364
365 /* Allocate new block & head scope. */
366
367 if ( ! man_block_alloc(man, line, ppos, tok))
368 return(0);
369 if ( ! man_head_alloc(man, line, ppos, tok))
370 return(0);
371
372 n = man->last;
373
374 /* Add line arguments. */
375
376 for (;;) {
377 la = *pos;
378 if ( ! man_args(man, line, pos, buf, &p))
379 break;
380 if ( ! man_word_alloc(man, line, la, p))
381 return(0);
382 }
383
384 /* Close out head and open body (unless MAN_SCOPE). */
385
386 if (MAN_SCOPED & man_macros[tok].flags) {
387 /* If we're forcing scope (`TP'), keep it open. */
388 if (MAN_FSCOPED & man_macros[tok].flags) {
389 man->flags |= MAN_BLINE;
390 return(1);
391 } else if (n == man->last) {
392 man->flags |= MAN_BLINE;
393 return(1);
394 }
395 }
396
397 if ( ! rew_scope(MAN_HEAD, man, tok))
398 return(0);
399 return(man_body_alloc(man, line, ppos, tok));
400 }
401
402 int
403 in_line_eoln(MACRO_PROT_ARGS)
404 {
405 int la;
406 char *p;
407 struct man_node *n;
408
409 if ( ! man_elem_alloc(man, line, ppos, tok))
410 return(0);
411
412 n = man->last;
413
414 for (;;) {
415 la = *pos;
416 if ( ! man_args(man, line, pos, buf, &p))
417 break;
418 if ( ! man_word_alloc(man, line, la, p))
419 return(0);
420 }
421
422 /*
423 * Append MAN_EOS in case the last snipped argument
424 * ends with a dot, e.g. `.IR syslog (3).'
425 */
426
427 if (n != man->last &&
428 mandoc_eos(man->last->string, strlen(man->last->string)))
429 man->last->flags |= MAN_EOS;
430
431 /*
432 * If no arguments are specified and this is MAN_SCOPED (i.e.,
433 * next-line scoped), then set our mode to indicate that we're
434 * waiting for terms to load into our context.
435 */
436
437 if (n == man->last && MAN_SCOPED & man_macros[tok].flags) {
438 assert( ! (MAN_NSCOPED & man_macros[tok].flags));
439 man->flags |= MAN_ELINE;
440 return(1);
441 }
442
443 /* Set ignorable context, if applicable. */
444
445 if (MAN_NSCOPED & man_macros[tok].flags) {
446 assert( ! (MAN_SCOPED & man_macros[tok].flags));
447 man->flags |= MAN_ILINE;
448 }
449
450 assert(MAN_ROOT != man->last->type);
451 man->next = MAN_NEXT_SIBLING;
452
453 /*
454 * Rewind our element scope. Note that when TH is pruned, we'll
455 * be back at the root, so make sure that we don't clobber as
456 * its sibling.
457 */
458
459 for ( ; man->last; man->last = man->last->parent) {
460 if (man->last == n)
461 break;
462 if (man->last->type == MAN_ROOT)
463 break;
464 if ( ! man_valid_post(man))
465 return(0);
466 }
467
468 assert(man->last);
469
470 /*
471 * Same here regarding whether we're back at the root.
472 */
473
474 if (man->last->type != MAN_ROOT && ! man_valid_post(man))
475 return(0);
476
477 return(1);
478 }
479
480
481 int
482 man_macroend(struct man *man)
483 {
484
485 return(man_unscope(man, man->first, MANDOCERR_SCOPEEXIT));
486 }
487
488 static int
489 man_args(struct man *man, int line, int *pos, char *buf, char **v)
490 {
491 char *start;
492
493 assert(*pos);
494 *v = start = buf + *pos;
495 assert(' ' != *start);
496
497 if ('\0' == *start)
498 return(0);
499
500 *v = mandoc_getarg(man->parse, v, line, pos);
501 return(1);
502 }