]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
When a file is given on the command line, actually exists, and its name
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.92 2014/12/16 17:26:00 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2013, 2014 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 #include "config.h"
20
21 #include <sys/types.h>
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 void blk_close(MACRO_PROT_ARGS);
40 static void blk_exp(MACRO_PROT_ARGS);
41 static void blk_imp(MACRO_PROT_ARGS);
42 static void in_line_eoln(MACRO_PROT_ARGS);
43 static int man_args(struct man *, int,
44 int *, char *, char **);
45
46 static void 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
53 const struct man_macro __man_macros[MAN_MAX] = {
54 { in_line_eoln, MAN_NSCOPED }, /* br */
55 { in_line_eoln, MAN_BSCOPE }, /* TH */
56 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
57 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
58 { blk_imp, MAN_BSCOPE | MAN_SCOPED | MAN_FSCOPED }, /* TP */
59 { blk_imp, MAN_BSCOPE }, /* LP */
60 { blk_imp, MAN_BSCOPE }, /* PP */
61 { blk_imp, MAN_BSCOPE }, /* P */
62 { blk_imp, MAN_BSCOPE }, /* IP */
63 { blk_imp, MAN_BSCOPE }, /* HP */
64 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
65 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
66 { in_line_eoln, 0 }, /* BI */
67 { in_line_eoln, 0 }, /* IB */
68 { in_line_eoln, 0 }, /* BR */
69 { in_line_eoln, 0 }, /* RB */
70 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
71 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
72 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
73 { in_line_eoln, 0 }, /* IR */
74 { in_line_eoln, 0 }, /* RI */
75 { in_line_eoln, MAN_NSCOPED }, /* na */
76 { in_line_eoln, MAN_NSCOPED }, /* sp */
77 { in_line_eoln, MAN_BSCOPE }, /* nf */
78 { in_line_eoln, MAN_BSCOPE }, /* fi */
79 { blk_close, MAN_BSCOPE }, /* RE */
80 { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* RS */
81 { in_line_eoln, 0 }, /* DT */
82 { in_line_eoln, 0 }, /* UC */
83 { in_line_eoln, 0 }, /* PD */
84 { in_line_eoln, 0 }, /* AT */
85 { in_line_eoln, 0 }, /* in */
86 { in_line_eoln, 0 }, /* ft */
87 { in_line_eoln, 0 }, /* OP */
88 { in_line_eoln, MAN_BSCOPE }, /* EX */
89 { in_line_eoln, MAN_BSCOPE }, /* EE */
90 { blk_exp, MAN_BSCOPE | MAN_EXPLICIT }, /* UR */
91 { blk_close, MAN_BSCOPE }, /* UE */
92 { in_line_eoln, 0 }, /* ll */
93 };
94
95 const struct man_macro * const man_macros = __man_macros;
96
97
98 void
99 man_unscope(struct man *man, const struct man_node *to)
100 {
101 struct man_node *n;
102
103 to = to->parent;
104 n = man->last;
105 while (n != to) {
106
107 /* Reached the end of the document? */
108
109 if (to == NULL && ! (n->flags & MAN_VALID)) {
110 if (man->flags & (MAN_BLINE | MAN_ELINE) &&
111 man_macros[n->tok].flags & MAN_SCOPED) {
112 mandoc_vmsg(MANDOCERR_BLK_LINE,
113 man->parse, n->line, n->pos,
114 "EOF breaks %s",
115 man_macronames[n->tok]);
116 if (man->flags & MAN_ELINE)
117 man->flags &= ~MAN_ELINE;
118 else {
119 assert(n->type == MAN_HEAD);
120 n = n->parent;
121 man->flags &= ~MAN_BLINE;
122 }
123 man->last = n;
124 n = n->parent;
125 man_node_delete(man, man->last);
126 continue;
127 }
128 if (n->type == MAN_BLOCK &&
129 man_macros[n->tok].flags & MAN_EXPLICIT)
130 mandoc_msg(MANDOCERR_BLK_NOEND,
131 man->parse, n->line, n->pos,
132 man_macronames[n->tok]);
133 }
134
135 /*
136 * We might delete the man->last node
137 * in the post-validation phase.
138 * Save a pointer to the parent such that
139 * we know where to continue the iteration.
140 */
141
142 man->last = n;
143 n = n->parent;
144 man_valid_post(man);
145 }
146
147 /*
148 * If we ended up at the parent of the node we were
149 * supposed to rewind to, that means the target node
150 * got deleted, so add the next node we parse as a child
151 * of the parent instead of as a sibling of the target.
152 */
153
154 man->next = (man->last == to) ?
155 MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
156 }
157
158 static enum rew
159 rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
160 {
161
162 if (type == MAN_BLOCK && ntok == n->parent->tok &&
163 n->parent->type == MAN_BODY)
164 return(REW_REWIND);
165 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
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 * Rewinding entails ascending the parse tree until a coherent point,
243 * for example, the `SH' macro will close out any intervening `SS'
244 * scopes. When a scope is closed, it must be validated and actioned.
245 */
246 static void
247 rew_scope(enum man_type type, struct man *man, enum mant tok)
248 {
249 struct man_node *n;
250 enum rew c;
251
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;
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
270 man_unscope(man, n);
271 }
272
273
274 /*
275 * Close out a generic explicit macro.
276 */
277 void
278 blk_close(MACRO_PROT_ARGS)
279 {
280 enum mant ntok;
281 const struct man_node *nn;
282
283 switch (tok) {
284 case MAN_RE:
285 ntok = MAN_RS;
286 break;
287 case MAN_UE:
288 ntok = MAN_UR;
289 break;
290 default:
291 abort();
292 /* NOTREACHED */
293 }
294
295 for (nn = man->last->parent; nn; nn = nn->parent)
296 if (nn->tok == ntok && nn->type == MAN_BLOCK)
297 break;
298
299 if (nn == NULL) {
300 mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
301 line, ppos, man_macronames[tok]);
302 rew_scope(MAN_BLOCK, man, MAN_PP);
303 } else
304 man_unscope(man, nn);
305 }
306
307 void
308 blk_exp(MACRO_PROT_ARGS)
309 {
310 struct man_node *n;
311 int la;
312 char *p;
313
314 rew_scope(MAN_BLOCK, man, tok);
315 man_block_alloc(man, line, ppos, tok);
316 man_head_alloc(man, line, ppos, tok);
317
318 for (;;) {
319 la = *pos;
320 if ( ! man_args(man, line, pos, buf, &p))
321 break;
322 man_word_alloc(man, line, la, p);
323 }
324
325 assert(man);
326 assert(tok != MAN_MAX);
327
328 for (n = man->last; n; n = n->parent)
329 if (n->tok == tok) {
330 assert(n->type == MAN_HEAD);
331 man_unscope(man, n);
332 break;
333 }
334
335 man_body_alloc(man, line, ppos, tok);
336 }
337
338 /*
339 * Parse an implicit-block macro. These contain a MAN_HEAD and a
340 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
341 * scopes, such as `SH' closing out an `SS', are defined in the rew
342 * routines.
343 */
344 void
345 blk_imp(MACRO_PROT_ARGS)
346 {
347 int la;
348 char *p;
349 struct man_node *n;
350
351 rew_scope(MAN_BODY, man, tok);
352 rew_scope(MAN_BLOCK, man, tok);
353 man_block_alloc(man, line, ppos, tok);
354 man_head_alloc(man, line, ppos, tok);
355 n = man->last;
356
357 /* Add line arguments. */
358
359 for (;;) {
360 la = *pos;
361 if ( ! man_args(man, line, pos, buf, &p))
362 break;
363 man_word_alloc(man, line, la, p);
364 }
365
366 /* Close out head and open body (unless MAN_SCOPE). */
367
368 if (man_macros[tok].flags & MAN_SCOPED) {
369 /* If we're forcing scope (`TP'), keep it open. */
370 if (man_macros[tok].flags & MAN_FSCOPED) {
371 man->flags |= MAN_BLINE;
372 return;
373 } else if (n == man->last) {
374 man->flags |= MAN_BLINE;
375 return;
376 }
377 }
378 rew_scope(MAN_HEAD, man, tok);
379 man_body_alloc(man, line, ppos, tok);
380 }
381
382 void
383 in_line_eoln(MACRO_PROT_ARGS)
384 {
385 int la;
386 char *p;
387 struct man_node *n;
388
389 man_elem_alloc(man, line, ppos, tok);
390 n = man->last;
391
392 for (;;) {
393 la = *pos;
394 if ( ! man_args(man, line, pos, buf, &p))
395 break;
396 if (man_macros[tok].flags & MAN_JOIN &&
397 man->last->type == MAN_TEXT)
398 man_word_append(man, p);
399 else
400 man_word_alloc(man, line, la, p);
401 }
402
403 /*
404 * Append MAN_EOS in case the last snipped argument
405 * ends with a dot, e.g. `.IR syslog (3).'
406 */
407
408 if (n != man->last &&
409 mandoc_eos(man->last->string, strlen(man->last->string)))
410 man->last->flags |= MAN_EOS;
411
412 /*
413 * If no arguments are specified and this is MAN_SCOPED (i.e.,
414 * next-line scoped), then set our mode to indicate that we're
415 * waiting for terms to load into our context.
416 */
417
418 if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
419 assert( ! (man_macros[tok].flags & MAN_NSCOPED));
420 man->flags |= MAN_ELINE;
421 return;
422 }
423
424 assert(man->last->type != MAN_ROOT);
425 man->next = MAN_NEXT_SIBLING;
426
427 /*
428 * Rewind our element scope. Note that when TH is pruned, we'll
429 * be back at the root, so make sure that we don't clobber as
430 * its sibling.
431 */
432
433 for ( ; man->last; man->last = man->last->parent) {
434 if (man->last == n)
435 break;
436 if (man->last->type == MAN_ROOT)
437 break;
438 man_valid_post(man);
439 }
440
441 assert(man->last);
442
443 /*
444 * Same here regarding whether we're back at the root.
445 */
446
447 if (man->last->type != MAN_ROOT)
448 man_valid_post(man);
449 }
450
451
452 void
453 man_macroend(struct man *man)
454 {
455
456 man_unscope(man, man->first);
457 }
458
459 static int
460 man_args(struct man *man, int line, int *pos, char *buf, char **v)
461 {
462 char *start;
463
464 assert(*pos);
465 *v = start = buf + *pos;
466 assert(' ' != *start);
467
468 if ('\0' == *start)
469 return(0);
470
471 *v = mandoc_getarg(man->parse, v, line, pos);
472 return(1);
473 }