]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
Make the character table available to libroff so it can check the
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.89 2014/08/18 16:36:54 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 #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 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
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 }, /* SM */
65 { in_line_eoln, MAN_SCOPED }, /* 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 }, /* R */
71 { in_line_eoln, MAN_SCOPED }, /* B */
72 { in_line_eoln, MAN_SCOPED }, /* 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, 0 }, /* 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, 0 }, /* UE */
92 { in_line_eoln, 0 }, /* ll */
93 };
94
95 const struct man_macro * const man_macros = __man_macros;
96
97
98 int
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 if ( ! man_valid_post(man))
145 return(0);
146 }
147
148 /*
149 * If we ended up at the parent of the node we were
150 * supposed to rewind to, that means the target node
151 * got deleted, so add the next node we parse as a child
152 * of the parent instead of as a sibling of the target.
153 */
154
155 man->next = (man->last == to) ?
156 MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
157
158 return(1);
159 }
160
161 static enum rew
162 rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
163 {
164
165 if (MAN_BLOCK == type && ntok == n->parent->tok &&
166 MAN_BODY == n->parent->type)
167 return(REW_REWIND);
168 return(ntok == n->tok ? REW_HALT : REW_NOHALT);
169 }
170
171 /*
172 * There are three scope levels: scoped to the root (all), scoped to the
173 * section (all less sections), and scoped to subsections (all less
174 * sections and subsections).
175 */
176 static enum rew
177 rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
178 {
179 enum rew c;
180
181 /* We cannot progress beyond the root ever. */
182 if (MAN_ROOT == n->type)
183 return(REW_HALT);
184
185 assert(n->parent);
186
187 /* Normal nodes shouldn't go to the level of the root. */
188 if (MAN_ROOT == n->parent->type)
189 return(REW_REWIND);
190
191 /* Already-validated nodes should be closed out. */
192 if (MAN_VALID & n->flags)
193 return(REW_NOHALT);
194
195 /* First: rewind to ourselves. */
196 if (type == n->type && tok == n->tok) {
197 if (MAN_EXPLICIT & man_macros[n->tok].flags)
198 return(REW_HALT);
199 else
200 return(REW_REWIND);
201 }
202
203 /*
204 * Next follow the implicit scope-smashings as defined by man.7:
205 * section, sub-section, etc.
206 */
207
208 switch (tok) {
209 case MAN_SH:
210 break;
211 case MAN_SS:
212 /* Rewind to a section, if a block. */
213 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
214 return(c);
215 break;
216 case MAN_RS:
217 /* Preserve empty paragraphs before RS. */
218 if (0 == n->nchild && (MAN_P == n->tok ||
219 MAN_PP == n->tok || MAN_LP == n->tok))
220 return(REW_HALT);
221 /* Rewind to a subsection, if a block. */
222 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
223 return(c);
224 /* Rewind to a section, if a block. */
225 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
226 return(c);
227 break;
228 default:
229 /* Rewind to an offsetter, if a block. */
230 if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
231 return(c);
232 /* Rewind to a subsection, if a block. */
233 if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
234 return(c);
235 /* Rewind to a section, if a block. */
236 if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
237 return(c);
238 break;
239 }
240
241 return(REW_NOHALT);
242 }
243
244 /*
245 * Rewinding entails ascending the parse tree until a coherent point,
246 * for example, the `SH' macro will close out any intervening `SS'
247 * scopes. When a scope is closed, it must be validated and actioned.
248 */
249 static int
250 rew_scope(enum man_type type, struct man *man, enum mant tok)
251 {
252 struct man_node *n;
253 enum rew c;
254
255 for (n = man->last; n; n = n->parent) {
256 /*
257 * Whether we should stop immediately (REW_HALT), stop
258 * and rewind until this point (REW_REWIND), or keep
259 * rewinding (REW_NOHALT).
260 */
261 c = rew_dohalt(tok, type, n);
262 if (REW_HALT == c)
263 return(1);
264 if (REW_REWIND == c)
265 break;
266 }
267
268 /*
269 * Rewind until the current point. Warn if we're a roff
270 * instruction that's mowing over explicit scopes.
271 */
272 assert(n);
273
274 return(man_unscope(man, n));
275 }
276
277
278 /*
279 * Close out a generic explicit macro.
280 */
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 mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
305 line, ppos, man_macronames[tok]);
306 if ( ! rew_scope(MAN_BLOCK, man, MAN_PP))
307 return(0);
308 } else
309 man_unscope(man, nn);
310
311 return(1);
312 }
313
314 int
315 blk_exp(MACRO_PROT_ARGS)
316 {
317 struct man_node *n;
318 int la;
319 char *p;
320
321 /* Close out prior implicit scopes. */
322
323 if ( ! rew_scope(MAN_BLOCK, man, tok))
324 return(0);
325
326 if ( ! man_block_alloc(man, line, ppos, tok))
327 return(0);
328 if ( ! man_head_alloc(man, line, ppos, tok))
329 return(0);
330
331 for (;;) {
332 la = *pos;
333 if ( ! man_args(man, line, pos, buf, &p))
334 break;
335 if ( ! man_word_alloc(man, line, la, p))
336 return(0);
337 }
338
339 assert(man);
340 assert(tok != MAN_MAX);
341
342 for (n = man->last; n; n = n->parent) {
343 if (n->tok != tok)
344 continue;
345 assert(MAN_HEAD == n->type);
346 man_unscope(man, n);
347 break;
348 }
349
350 return(man_body_alloc(man, line, ppos, tok));
351 }
352
353 /*
354 * Parse an implicit-block macro. These contain a MAN_HEAD and a
355 * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
356 * scopes, such as `SH' closing out an `SS', are defined in the rew
357 * routines.
358 */
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 int
411 in_line_eoln(MACRO_PROT_ARGS)
412 {
413 int la;
414 char *p;
415 struct man_node *n;
416
417 if ( ! man_elem_alloc(man, line, ppos, tok))
418 return(0);
419
420 n = man->last;
421
422 for (;;) {
423 la = *pos;
424 if ( ! man_args(man, line, pos, buf, &p))
425 break;
426 if ( ! man_word_alloc(man, line, la, p))
427 return(0);
428 }
429
430 /*
431 * Append MAN_EOS in case the last snipped argument
432 * ends with a dot, e.g. `.IR syslog (3).'
433 */
434
435 if (n != man->last &&
436 mandoc_eos(man->last->string, strlen(man->last->string)))
437 man->last->flags |= MAN_EOS;
438
439 /*
440 * If no arguments are specified and this is MAN_SCOPED (i.e.,
441 * next-line scoped), then set our mode to indicate that we're
442 * waiting for terms to load into our context.
443 */
444
445 if (n == man->last && MAN_SCOPED & man_macros[tok].flags) {
446 assert( ! (MAN_NSCOPED & man_macros[tok].flags));
447 man->flags |= MAN_ELINE;
448 return(1);
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));
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 }