]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
On a new RS nesting level, the saved width starts from the default
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.104 2015/04/03 23:19:15 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2013, 2014, 2015 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 AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 "mandoc.h"
29 #include "roff.h"
30 #include "man.h"
31 #include "libmandoc.h"
32 #include "libman.h"
33
34 static void blk_close(MACRO_PROT_ARGS);
35 static void blk_exp(MACRO_PROT_ARGS);
36 static void blk_imp(MACRO_PROT_ARGS);
37 static void in_line_eoln(MACRO_PROT_ARGS);
38 static int man_args(struct man *, int,
39 int *, char *, char **);
40 static void rew_scope(struct man *, int);
41
42 const struct man_macro __man_macros[MAN_MAX] = {
43 { in_line_eoln, MAN_NSCOPED }, /* br */
44 { in_line_eoln, MAN_BSCOPE }, /* TH */
45 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
46 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
47 { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* TP */
48 { blk_imp, MAN_BSCOPE }, /* LP */
49 { blk_imp, MAN_BSCOPE }, /* PP */
50 { blk_imp, MAN_BSCOPE }, /* P */
51 { blk_imp, MAN_BSCOPE }, /* IP */
52 { blk_imp, MAN_BSCOPE }, /* HP */
53 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
54 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
55 { in_line_eoln, 0 }, /* BI */
56 { in_line_eoln, 0 }, /* IB */
57 { in_line_eoln, 0 }, /* BR */
58 { in_line_eoln, 0 }, /* RB */
59 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
60 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
61 { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
62 { in_line_eoln, 0 }, /* IR */
63 { in_line_eoln, 0 }, /* RI */
64 { in_line_eoln, MAN_NSCOPED }, /* sp */
65 { in_line_eoln, MAN_BSCOPE }, /* nf */
66 { in_line_eoln, MAN_BSCOPE }, /* fi */
67 { blk_close, MAN_BSCOPE }, /* RE */
68 { blk_exp, MAN_BSCOPE }, /* RS */
69 { in_line_eoln, 0 }, /* DT */
70 { in_line_eoln, 0 }, /* UC */
71 { in_line_eoln, MAN_NSCOPED }, /* PD */
72 { in_line_eoln, 0 }, /* AT */
73 { in_line_eoln, 0 }, /* in */
74 { in_line_eoln, 0 }, /* ft */
75 { in_line_eoln, 0 }, /* OP */
76 { in_line_eoln, MAN_BSCOPE }, /* EX */
77 { in_line_eoln, MAN_BSCOPE }, /* EE */
78 { blk_exp, MAN_BSCOPE }, /* UR */
79 { blk_close, MAN_BSCOPE }, /* UE */
80 { in_line_eoln, 0 }, /* ll */
81 };
82
83 const struct man_macro * const man_macros = __man_macros;
84
85
86 void
87 man_unscope(struct man *man, const struct roff_node *to)
88 {
89 struct roff_node *n;
90
91 to = to->parent;
92 n = man->last;
93 while (n != to) {
94
95 /* Reached the end of the document? */
96
97 if (to == NULL && ! (n->flags & MAN_VALID)) {
98 if (man->flags & (MAN_BLINE | MAN_ELINE) &&
99 man_macros[n->tok].flags & MAN_SCOPED) {
100 mandoc_vmsg(MANDOCERR_BLK_LINE,
101 man->parse, n->line, n->pos,
102 "EOF breaks %s",
103 man_macronames[n->tok]);
104 if (man->flags & MAN_ELINE)
105 man->flags &= ~MAN_ELINE;
106 else {
107 assert(n->type == ROFFT_HEAD);
108 n = n->parent;
109 man->flags &= ~MAN_BLINE;
110 }
111 man->last = n;
112 n = n->parent;
113 man_node_delete(man, man->last);
114 continue;
115 }
116 if (n->type == ROFFT_BLOCK &&
117 man_macros[n->tok].fp == blk_exp)
118 mandoc_msg(MANDOCERR_BLK_NOEND,
119 man->parse, n->line, n->pos,
120 man_macronames[n->tok]);
121 }
122
123 /*
124 * We might delete the man->last node
125 * in the post-validation phase.
126 * Save a pointer to the parent such that
127 * we know where to continue the iteration.
128 */
129
130 man->last = n;
131 n = n->parent;
132 man_valid_post(man);
133 }
134
135 /*
136 * If we ended up at the parent of the node we were
137 * supposed to rewind to, that means the target node
138 * got deleted, so add the next node we parse as a child
139 * of the parent instead of as a sibling of the target.
140 */
141
142 man->next = (man->last == to) ?
143 MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
144 }
145
146 /*
147 * Rewinding entails ascending the parse tree until a coherent point,
148 * for example, the `SH' macro will close out any intervening `SS'
149 * scopes. When a scope is closed, it must be validated and actioned.
150 */
151 static void
152 rew_scope(struct man *man, int tok)
153 {
154 struct roff_node *n;
155
156 /* Preserve empty paragraphs before RS. */
157
158 n = man->last;
159 if (tok == MAN_RS && n->nchild == 0 &&
160 (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
161 return;
162
163 for (;;) {
164 if (n->type == ROFFT_ROOT)
165 return;
166 if (n->flags & MAN_VALID) {
167 n = n->parent;
168 continue;
169 }
170 if (n->type != ROFFT_BLOCK) {
171 if (n->parent->type == ROFFT_ROOT) {
172 man_unscope(man, n);
173 return;
174 } else {
175 n = n->parent;
176 continue;
177 }
178 }
179 if (tok != MAN_SH && (n->tok == MAN_SH ||
180 (tok != MAN_SS && (n->tok == MAN_SS ||
181 man_macros[n->tok].fp == blk_exp))))
182 return;
183 man_unscope(man, n);
184 n = man->last;
185 }
186 }
187
188
189 /*
190 * Close out a generic explicit macro.
191 */
192 void
193 blk_close(MACRO_PROT_ARGS)
194 {
195 int ntok;
196 const struct roff_node *nn;
197 char *p;
198 int nrew, target;
199
200 nrew = 1;
201 switch (tok) {
202 case MAN_RE:
203 ntok = MAN_RS;
204 if ( ! man_args(man, line, pos, buf, &p))
205 break;
206 for (nn = man->last->parent; nn; nn = nn->parent)
207 if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
208 nrew++;
209 target = strtol(p, &p, 10);
210 if (*p != '\0')
211 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
212 line, p - buf, "RE ... %s", p);
213 if (target == 0)
214 target = 1;
215 nrew -= target;
216 if (nrew < 1) {
217 mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse,
218 line, ppos, "RE %d", target);
219 return;
220 }
221 break;
222 case MAN_UE:
223 ntok = MAN_UR;
224 break;
225 default:
226 abort();
227 /* NOTREACHED */
228 }
229
230 for (nn = man->last->parent; nn; nn = nn->parent)
231 if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
232 break;
233
234 if (nn == NULL) {
235 mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
236 line, ppos, man_macronames[tok]);
237 rew_scope(man, MAN_PP);
238 } else {
239 line = man->last->line;
240 ppos = man->last->pos;
241 ntok = man->last->tok;
242 man_unscope(man, nn);
243
244 /* Move a trailing paragraph behind the block. */
245
246 if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
247 *pos = strlen(buf);
248 blk_imp(man, ntok, line, ppos, pos, buf);
249 }
250 }
251 }
252
253 void
254 blk_exp(MACRO_PROT_ARGS)
255 {
256 struct roff_node *head;
257 char *p;
258 int la;
259
260 rew_scope(man, tok);
261 man_block_alloc(man, line, ppos, tok);
262 man_head_alloc(man, line, ppos, tok);
263 head = man->last;
264
265 la = *pos;
266 if (man_args(man, line, pos, buf, &p))
267 man_word_alloc(man, line, la, p);
268
269 if (buf[*pos] != '\0')
270 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
271 man->parse, line, *pos, "%s ... %s",
272 man_macronames[tok], buf + *pos);
273
274 man_unscope(man, head);
275 man_body_alloc(man, line, ppos, tok);
276 }
277
278 /*
279 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
280 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
281 * scopes, such as `SH' closing out an `SS', are defined in the rew
282 * routines.
283 */
284 void
285 blk_imp(MACRO_PROT_ARGS)
286 {
287 int la;
288 char *p;
289 struct roff_node *n;
290
291 rew_scope(man, tok);
292 man_block_alloc(man, line, ppos, tok);
293 man_head_alloc(man, line, ppos, tok);
294 n = man->last;
295
296 /* Add line arguments. */
297
298 for (;;) {
299 la = *pos;
300 if ( ! man_args(man, line, pos, buf, &p))
301 break;
302 man_word_alloc(man, line, la, p);
303 }
304
305 /*
306 * For macros having optional next-line scope,
307 * keep the head open if there were no arguments.
308 * For `TP', always keep the head open.
309 */
310
311 if (man_macros[tok].flags & MAN_SCOPED &&
312 (tok == MAN_TP || n == man->last)) {
313 man->flags |= MAN_BLINE;
314 return;
315 }
316
317 /* Close out the head and open the body. */
318
319 man_unscope(man, n);
320 man_body_alloc(man, line, ppos, tok);
321 }
322
323 void
324 in_line_eoln(MACRO_PROT_ARGS)
325 {
326 int la;
327 char *p;
328 struct roff_node *n;
329
330 man_elem_alloc(man, line, ppos, tok);
331 n = man->last;
332
333 for (;;) {
334 if (buf[*pos] != '\0' && (tok == MAN_br ||
335 tok == MAN_fi || tok == MAN_nf)) {
336 mandoc_vmsg(MANDOCERR_ARG_SKIP,
337 man->parse, line, *pos, "%s %s",
338 man_macronames[tok], buf + *pos);
339 break;
340 }
341 if (buf[*pos] != '\0' && man->last != n &&
342 (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
343 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
344 man->parse, line, *pos, "%s ... %s",
345 man_macronames[tok], buf + *pos);
346 break;
347 }
348 la = *pos;
349 if ( ! man_args(man, line, pos, buf, &p))
350 break;
351 if (man_macros[tok].flags & MAN_JOIN &&
352 man->last->type == ROFFT_TEXT)
353 man_word_append(man, p);
354 else
355 man_word_alloc(man, line, la, p);
356 }
357
358 /*
359 * Append MAN_EOS in case the last snipped argument
360 * ends with a dot, e.g. `.IR syslog (3).'
361 */
362
363 if (n != man->last &&
364 mandoc_eos(man->last->string, strlen(man->last->string)))
365 man->last->flags |= MAN_EOS;
366
367 /*
368 * If no arguments are specified and this is MAN_SCOPED (i.e.,
369 * next-line scoped), then set our mode to indicate that we're
370 * waiting for terms to load into our context.
371 */
372
373 if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
374 assert( ! (man_macros[tok].flags & MAN_NSCOPED));
375 man->flags |= MAN_ELINE;
376 return;
377 }
378
379 assert(man->last->type != ROFFT_ROOT);
380 man->next = MAN_NEXT_SIBLING;
381
382 /*
383 * Rewind our element scope. Note that when TH is pruned, we'll
384 * be back at the root, so make sure that we don't clobber as
385 * its sibling.
386 */
387
388 for ( ; man->last; man->last = man->last->parent) {
389 if (man->last == n)
390 break;
391 if (man->last->type == ROFFT_ROOT)
392 break;
393 man_valid_post(man);
394 }
395
396 assert(man->last);
397
398 /*
399 * Same here regarding whether we're back at the root.
400 */
401
402 if (man->last->type != ROFFT_ROOT)
403 man_valid_post(man);
404 }
405
406
407 void
408 man_macroend(struct man *man)
409 {
410
411 man_unscope(man, man->first);
412 }
413
414 static int
415 man_args(struct man *man, int line, int *pos, char *buf, char **v)
416 {
417 char *start;
418
419 assert(*pos);
420 *v = start = buf + *pos;
421 assert(' ' != *start);
422
423 if ('\0' == *start)
424 return(0);
425
426 *v = mandoc_getarg(man->parse, v, line, pos);
427 return(1);
428 }