]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
71bc225f39e4f2728ae7116146743bd1c3a01c14
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.133 2018/08/26 16:21:24 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012-2015, 2017, 2018 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 "roff_int.h"
33 #include "libman.h"
34
35 static void blk_close(MACRO_PROT_ARGS);
36 static void blk_exp(MACRO_PROT_ARGS);
37 static void blk_imp(MACRO_PROT_ARGS);
38 static void in_line_eoln(MACRO_PROT_ARGS);
39 static int man_args(struct roff_man *, int,
40 int *, char *, char **);
41 static void rew_scope(struct roff_man *, enum roff_tok);
42
43 static const struct man_macro man_macros[MAN_MAX - MAN_TH] = {
44 { in_line_eoln, MAN_XSCOPE }, /* TH */
45 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SH */
46 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SS */
47 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TP */
48 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TQ */
49 { blk_imp, MAN_XSCOPE }, /* LP */
50 { blk_imp, MAN_XSCOPE }, /* PP */
51 { blk_imp, MAN_XSCOPE }, /* P */
52 { blk_imp, MAN_XSCOPE }, /* IP */
53 { blk_imp, MAN_XSCOPE }, /* HP */
54 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SM */
55 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SB */
56 { in_line_eoln, 0 }, /* BI */
57 { in_line_eoln, 0 }, /* IB */
58 { in_line_eoln, 0 }, /* BR */
59 { in_line_eoln, 0 }, /* RB */
60 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* R */
61 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* B */
62 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* I */
63 { in_line_eoln, 0 }, /* IR */
64 { in_line_eoln, 0 }, /* RI */
65 { in_line_eoln, MAN_NSCOPED }, /* nf */
66 { in_line_eoln, MAN_NSCOPED }, /* fi */
67 { blk_close, MAN_XSCOPE }, /* RE */
68 { blk_exp, MAN_XSCOPE }, /* 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, MAN_NSCOPED }, /* in */
74 { blk_imp, MAN_XSCOPE }, /* SY */
75 { blk_close, MAN_XSCOPE }, /* YS */
76 { in_line_eoln, 0 }, /* OP */
77 { in_line_eoln, MAN_XSCOPE }, /* EX */
78 { in_line_eoln, MAN_XSCOPE }, /* EE */
79 { blk_exp, MAN_XSCOPE }, /* UR */
80 { blk_close, MAN_XSCOPE }, /* UE */
81 { blk_exp, MAN_XSCOPE }, /* MT */
82 { blk_close, MAN_XSCOPE }, /* ME */
83 };
84
85
86 const struct man_macro *
87 man_macro(enum roff_tok tok)
88 {
89 assert(tok >= MAN_TH && tok <= MAN_MAX);
90 return man_macros + (tok - MAN_TH);
91 }
92
93 void
94 man_unscope(struct roff_man *man, const struct roff_node *to)
95 {
96 struct roff_node *n;
97
98 to = to->parent;
99 n = man->last;
100 while (n != to) {
101
102 /* Reached the end of the document? */
103
104 if (to == NULL && ! (n->flags & NODE_VALID)) {
105 if (man->flags & (MAN_BLINE | MAN_ELINE) &&
106 man_macro(n->tok)->flags &
107 (MAN_BSCOPED | MAN_NSCOPED)) {
108 mandoc_vmsg(MANDOCERR_BLK_LINE,
109 man->parse, n->line, n->pos,
110 "EOF breaks %s", roff_name[n->tok]);
111 if (man->flags & MAN_ELINE)
112 man->flags &= ~MAN_ELINE;
113 else {
114 assert(n->type == ROFFT_HEAD);
115 n = n->parent;
116 man->flags &= ~MAN_BLINE;
117 }
118 man->last = n;
119 n = n->parent;
120 roff_node_delete(man, man->last);
121 continue;
122 }
123 if (n->type == ROFFT_BLOCK &&
124 man_macro(n->tok)->fp == blk_exp)
125 mandoc_msg(MANDOCERR_BLK_NOEND,
126 man->parse, n->line, n->pos,
127 roff_name[n->tok]);
128 }
129
130 /*
131 * We might delete the man->last node
132 * in the post-validation phase.
133 * Save a pointer to the parent such that
134 * we know where to continue the iteration.
135 */
136
137 man->last = n;
138 n = n->parent;
139 man->last->flags |= NODE_VALID;
140 }
141
142 /*
143 * If we ended up at the parent of the node we were
144 * supposed to rewind to, that means the target node
145 * got deleted, so add the next node we parse as a child
146 * of the parent instead of as a sibling of the target.
147 */
148
149 man->next = (man->last == to) ?
150 ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING;
151 }
152
153 /*
154 * Rewinding entails ascending the parse tree until a coherent point,
155 * for example, the `SH' macro will close out any intervening `SS'
156 * scopes. When a scope is closed, it must be validated and actioned.
157 */
158 static void
159 rew_scope(struct roff_man *man, enum roff_tok tok)
160 {
161 struct roff_node *n;
162
163 /* Preserve empty paragraphs before RS. */
164
165 n = man->last;
166 if (tok == MAN_RS && n->child == NULL &&
167 (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
168 return;
169
170 for (;;) {
171 if (n->type == ROFFT_ROOT)
172 return;
173 if (n->flags & NODE_VALID) {
174 n = n->parent;
175 continue;
176 }
177 if (n->type != ROFFT_BLOCK) {
178 if (n->parent->type == ROFFT_ROOT) {
179 man_unscope(man, n);
180 return;
181 } else {
182 n = n->parent;
183 continue;
184 }
185 }
186 if (tok != MAN_SH && (n->tok == MAN_SH ||
187 (tok != MAN_SS && (n->tok == MAN_SS ||
188 man_macro(n->tok)->fp == blk_exp))))
189 return;
190 man_unscope(man, n);
191 n = man->last;
192 }
193 }
194
195
196 /*
197 * Close out a generic explicit macro.
198 */
199 void
200 blk_close(MACRO_PROT_ARGS)
201 {
202 enum roff_tok ctok, ntok;
203 const struct roff_node *nn;
204 char *p;
205 int cline, cpos, nrew, target;
206
207 nrew = 1;
208 switch (tok) {
209 case MAN_RE:
210 ntok = MAN_RS;
211 if ( ! man_args(man, line, pos, buf, &p))
212 break;
213 for (nn = man->last->parent; nn; nn = nn->parent)
214 if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
215 nrew++;
216 target = strtol(p, &p, 10);
217 if (*p != '\0')
218 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
219 line, p - buf, "RE ... %s", p);
220 if (target == 0)
221 target = 1;
222 nrew -= target;
223 if (nrew < 1) {
224 mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse,
225 line, ppos, "RE %d", target);
226 return;
227 }
228 break;
229 case MAN_YS:
230 ntok = MAN_SY;
231 break;
232 case MAN_UE:
233 ntok = MAN_UR;
234 break;
235 case MAN_ME:
236 ntok = MAN_MT;
237 break;
238 default:
239 abort();
240 }
241
242 for (nn = man->last->parent; nn; nn = nn->parent)
243 if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
244 break;
245
246 if (nn == NULL) {
247 mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
248 line, ppos, roff_name[tok]);
249 rew_scope(man, MAN_PP);
250 if (tok == MAN_RE) {
251 roff_elem_alloc(man, line, ppos, ROFF_br);
252 man->last->flags |= NODE_LINE |
253 NODE_VALID | NODE_ENDED;
254 man->next = ROFF_NEXT_SIBLING;
255 }
256 return;
257 }
258
259 cline = man->last->line;
260 cpos = man->last->pos;
261 ctok = man->last->tok;
262 man_unscope(man, nn);
263
264 if (tok == MAN_RE && nn->head->aux > 0)
265 roff_setreg(man->roff, "an-margin", nn->head->aux, '-');
266
267 /* Trailing text. */
268
269 if (buf[*pos] != '\0') {
270 roff_word_alloc(man, line, ppos, buf + *pos);
271 man->last->flags |= NODE_DELIMC;
272 if (mandoc_eos(man->last->string, strlen(man->last->string)))
273 man->last->flags |= NODE_EOS;
274 }
275
276 /* Move a trailing paragraph behind the block. */
277
278 if (ctok == MAN_LP || ctok == MAN_PP || ctok == MAN_P) {
279 *pos = strlen(buf);
280 blk_imp(man, ctok, cline, cpos, pos, buf);
281 }
282
283 /* Synopsis blocks need an explicit end marker for spacing. */
284
285 if (tok == MAN_YS && man->last == nn) {
286 roff_elem_alloc(man, line, ppos, tok);
287 man_unscope(man, man->last);
288 }
289 }
290
291 void
292 blk_exp(MACRO_PROT_ARGS)
293 {
294 struct roff_node *head;
295 char *p;
296 int la;
297
298 if (tok == MAN_RS)
299 rew_scope(man, tok);
300 roff_block_alloc(man, line, ppos, tok);
301 head = roff_head_alloc(man, line, ppos, tok);
302
303 la = *pos;
304 if (man_args(man, line, pos, buf, &p)) {
305 roff_word_alloc(man, line, la, p);
306 if (tok == MAN_RS) {
307 if (roff_getreg(man->roff, "an-margin") == 0)
308 roff_setreg(man->roff, "an-margin",
309 7 * 24, '=');
310 if ((head->aux = strtod(p, NULL) * 24.0) > 0)
311 roff_setreg(man->roff, "an-margin",
312 head->aux, '+');
313 }
314 }
315
316 if (buf[*pos] != '\0')
317 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, line,
318 *pos, "%s ... %s", roff_name[tok], buf + *pos);
319
320 man_unscope(man, head);
321 roff_body_alloc(man, line, ppos, tok);
322 }
323
324 /*
325 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
326 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
327 * scopes, such as `SH' closing out an `SS', are defined in the rew
328 * routines.
329 */
330 void
331 blk_imp(MACRO_PROT_ARGS)
332 {
333 int la;
334 char *p;
335 struct roff_node *n;
336
337 rew_scope(man, tok);
338 n = roff_block_alloc(man, line, ppos, tok);
339 if (n->tok == MAN_SH || n->tok == MAN_SS)
340 man->flags &= ~MAN_LITERAL;
341 n = roff_head_alloc(man, line, ppos, tok);
342
343 /* Add line arguments. */
344
345 for (;;) {
346 la = *pos;
347 if ( ! man_args(man, line, pos, buf, &p))
348 break;
349 roff_word_alloc(man, line, la, p);
350 }
351
352 /*
353 * For macros having optional next-line scope,
354 * keep the head open if there were no arguments.
355 * For `TP' and `TQ', always keep the head open.
356 */
357
358 if (man_macro(tok)->flags & MAN_BSCOPED &&
359 (tok == MAN_TP || tok == MAN_TQ || n == man->last)) {
360 man->flags |= MAN_BLINE;
361 return;
362 }
363
364 /* Close out the head and open the body. */
365
366 man_unscope(man, n);
367 roff_body_alloc(man, line, ppos, tok);
368 }
369
370 void
371 in_line_eoln(MACRO_PROT_ARGS)
372 {
373 int la;
374 char *p;
375 struct roff_node *n;
376
377 roff_elem_alloc(man, line, ppos, tok);
378 n = man->last;
379
380 for (;;) {
381 if (buf[*pos] != '\0' && (tok == MAN_fi || tok == MAN_nf)) {
382 mandoc_vmsg(MANDOCERR_ARG_SKIP,
383 man->parse, line, *pos, "%s %s",
384 roff_name[tok], buf + *pos);
385 break;
386 }
387 if (buf[*pos] != '\0' && man->last != n && tok == MAN_PD) {
388 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
389 man->parse, line, *pos, "%s ... %s",
390 roff_name[tok], buf + *pos);
391 break;
392 }
393 la = *pos;
394 if ( ! man_args(man, line, pos, buf, &p))
395 break;
396 if (man_macro(tok)->flags & MAN_JOIN &&
397 man->last->type == ROFFT_TEXT)
398 roff_word_append(man, p);
399 else
400 roff_word_alloc(man, line, la, p);
401 }
402
403 /*
404 * Append NODE_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 |= NODE_EOS;
411
412 /*
413 * If no arguments are specified and this is MAN_ESCOPED (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_macro(tok)->flags & MAN_ESCOPED) {
419 man->flags |= MAN_ELINE;
420 return;
421 }
422
423 assert(man->last->type != ROFFT_ROOT);
424 man->next = ROFF_NEXT_SIBLING;
425
426 /* Rewind our element scope. */
427
428 for ( ; man->last; man->last = man->last->parent) {
429 man_state(man, man->last);
430 if (man->last == n)
431 break;
432 }
433
434 /* Rewind next-line scoped ancestors, if any. */
435
436 if (man_macro(tok)->flags & MAN_ESCOPED)
437 man_descope(man, line, ppos, NULL);
438 }
439
440 void
441 man_endparse(struct roff_man *man)
442 {
443
444 man_unscope(man, man->first);
445 man->flags &= ~MAN_LITERAL;
446 }
447
448 static int
449 man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
450 {
451 char *start;
452
453 assert(*pos);
454 *v = start = buf + *pos;
455 assert(' ' != *start);
456
457 if ('\0' == *start)
458 return 0;
459
460 *v = mandoc_getarg(man->parse, v, line, pos);
461 return 1;
462 }