man_macro.c revision 1.6 1 1.6 christos /* Id: man_macro.c,v 1.144 2019/01/05 18:59:46 schwarze Exp */
2 1.1 joerg /*
3 1.2 christos * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
4 1.6 christos * Copyright (c) 2012-2015, 2017-2019 Ingo Schwarze <schwarze (at) openbsd.org>
5 1.2 christos * Copyright (c) 2013 Franco Fichtner <franco (at) lastsummer.de>
6 1.1 joerg *
7 1.1 joerg * Permission to use, copy, modify, and distribute this software for any
8 1.1 joerg * purpose with or without fee is hereby granted, provided that the above
9 1.1 joerg * copyright notice and this permission notice appear in all copies.
10 1.1 joerg *
11 1.3 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 1.1 joerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.3 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 1.1 joerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 joerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 joerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 joerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 joerg */
19 1.2 christos #include "config.h"
20 1.2 christos
21 1.2 christos #include <sys/types.h>
22 1.2 christos
23 1.1 joerg #include <assert.h>
24 1.1 joerg #include <ctype.h>
25 1.6 christos #include <stdio.h>
26 1.1 joerg #include <stdlib.h>
27 1.1 joerg #include <string.h>
28 1.1 joerg
29 1.3 christos #include "mandoc.h"
30 1.3 christos #include "roff.h"
31 1.2 christos #include "man.h"
32 1.2 christos #include "libmandoc.h"
33 1.3 christos #include "roff_int.h"
34 1.1 joerg #include "libman.h"
35 1.1 joerg
36 1.2 christos static void blk_close(MACRO_PROT_ARGS);
37 1.2 christos static void blk_exp(MACRO_PROT_ARGS);
38 1.2 christos static void blk_imp(MACRO_PROT_ARGS);
39 1.2 christos static void in_line_eoln(MACRO_PROT_ARGS);
40 1.3 christos static int man_args(struct roff_man *, int,
41 1.2 christos int *, char *, char **);
42 1.5 christos static void rew_scope(struct roff_man *, enum roff_tok);
43 1.1 joerg
44 1.6 christos static const struct man_macro man_macros[MAN_MAX - MAN_TH] = {
45 1.6 christos { in_line_eoln, MAN_XSCOPE }, /* TH */
46 1.6 christos { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SH */
47 1.6 christos { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SS */
48 1.6 christos { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TP */
49 1.6 christos { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TQ */
50 1.6 christos { blk_imp, MAN_XSCOPE }, /* LP */
51 1.6 christos { blk_imp, MAN_XSCOPE }, /* PP */
52 1.6 christos { blk_imp, MAN_XSCOPE }, /* P */
53 1.6 christos { blk_imp, MAN_XSCOPE }, /* IP */
54 1.6 christos { blk_imp, MAN_XSCOPE }, /* HP */
55 1.6 christos { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SM */
56 1.6 christos { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SB */
57 1.1 joerg { in_line_eoln, 0 }, /* BI */
58 1.1 joerg { in_line_eoln, 0 }, /* IB */
59 1.1 joerg { in_line_eoln, 0 }, /* BR */
60 1.1 joerg { in_line_eoln, 0 }, /* RB */
61 1.6 christos { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* R */
62 1.6 christos { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* B */
63 1.6 christos { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* I */
64 1.1 joerg { in_line_eoln, 0 }, /* IR */
65 1.1 joerg { in_line_eoln, 0 }, /* RI */
66 1.6 christos { blk_close, MAN_XSCOPE }, /* RE */
67 1.6 christos { blk_exp, MAN_XSCOPE }, /* RS */
68 1.1 joerg { in_line_eoln, 0 }, /* DT */
69 1.1 joerg { in_line_eoln, 0 }, /* UC */
70 1.3 christos { in_line_eoln, MAN_NSCOPED }, /* PD */
71 1.2 christos { in_line_eoln, 0 }, /* AT */
72 1.5 christos { in_line_eoln, MAN_NSCOPED }, /* in */
73 1.6 christos { blk_imp, MAN_XSCOPE }, /* SY */
74 1.6 christos { blk_close, MAN_XSCOPE }, /* YS */
75 1.2 christos { in_line_eoln, 0 }, /* OP */
76 1.6 christos { in_line_eoln, MAN_XSCOPE }, /* EX */
77 1.6 christos { in_line_eoln, MAN_XSCOPE }, /* EE */
78 1.6 christos { blk_exp, MAN_XSCOPE }, /* UR */
79 1.6 christos { blk_close, MAN_XSCOPE }, /* UE */
80 1.6 christos { blk_exp, MAN_XSCOPE }, /* MT */
81 1.6 christos { blk_close, MAN_XSCOPE }, /* ME */
82 1.1 joerg };
83 1.1 joerg
84 1.1 joerg
85 1.6 christos const struct man_macro *
86 1.6 christos man_macro(enum roff_tok tok)
87 1.6 christos {
88 1.6 christos assert(tok >= MAN_TH && tok <= MAN_MAX);
89 1.6 christos return man_macros + (tok - MAN_TH);
90 1.6 christos }
91 1.6 christos
92 1.2 christos void
93 1.3 christos man_unscope(struct roff_man *man, const struct roff_node *to)
94 1.1 joerg {
95 1.3 christos struct roff_node *n;
96 1.1 joerg
97 1.2 christos to = to->parent;
98 1.2 christos n = man->last;
99 1.2 christos while (n != to) {
100 1.2 christos
101 1.2 christos /* Reached the end of the document? */
102 1.2 christos
103 1.4 christos if (to == NULL && ! (n->flags & NODE_VALID)) {
104 1.2 christos if (man->flags & (MAN_BLINE | MAN_ELINE) &&
105 1.6 christos man_macro(n->tok)->flags &
106 1.6 christos (MAN_BSCOPED | MAN_NSCOPED)) {
107 1.6 christos mandoc_msg(MANDOCERR_BLK_LINE,
108 1.6 christos n->line, n->pos,
109 1.5 christos "EOF breaks %s", roff_name[n->tok]);
110 1.2 christos if (man->flags & MAN_ELINE)
111 1.2 christos man->flags &= ~MAN_ELINE;
112 1.2 christos else {
113 1.3 christos assert(n->type == ROFFT_HEAD);
114 1.2 christos n = n->parent;
115 1.2 christos man->flags &= ~MAN_BLINE;
116 1.2 christos }
117 1.2 christos man->last = n;
118 1.2 christos n = n->parent;
119 1.3 christos roff_node_delete(man, man->last);
120 1.2 christos continue;
121 1.2 christos }
122 1.3 christos if (n->type == ROFFT_BLOCK &&
123 1.6 christos man_macro(n->tok)->fp == blk_exp)
124 1.2 christos mandoc_msg(MANDOCERR_BLK_NOEND,
125 1.6 christos n->line, n->pos, "%s",
126 1.5 christos roff_name[n->tok]);
127 1.2 christos }
128 1.1 joerg
129 1.2 christos /*
130 1.2 christos * We might delete the man->last node
131 1.2 christos * in the post-validation phase.
132 1.2 christos * Save a pointer to the parent such that
133 1.2 christos * we know where to continue the iteration.
134 1.2 christos */
135 1.2 christos
136 1.2 christos man->last = n;
137 1.2 christos n = n->parent;
138 1.4 christos man->last->flags |= NODE_VALID;
139 1.1 joerg }
140 1.1 joerg
141 1.2 christos /*
142 1.2 christos * If we ended up at the parent of the node we were
143 1.2 christos * supposed to rewind to, that means the target node
144 1.2 christos * got deleted, so add the next node we parse as a child
145 1.2 christos * of the parent instead of as a sibling of the target.
146 1.2 christos */
147 1.2 christos
148 1.2 christos man->next = (man->last == to) ?
149 1.3 christos ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING;
150 1.1 joerg }
151 1.1 joerg
152 1.1 joerg /*
153 1.1 joerg * Rewinding entails ascending the parse tree until a coherent point,
154 1.1 joerg * for example, the `SH' macro will close out any intervening `SS'
155 1.1 joerg * scopes. When a scope is closed, it must be validated and actioned.
156 1.1 joerg */
157 1.2 christos static void
158 1.5 christos rew_scope(struct roff_man *man, enum roff_tok tok)
159 1.1 joerg {
160 1.3 christos struct roff_node *n;
161 1.3 christos
162 1.3 christos /* Preserve empty paragraphs before RS. */
163 1.3 christos
164 1.3 christos n = man->last;
165 1.3 christos if (tok == MAN_RS && n->child == NULL &&
166 1.3 christos (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
167 1.3 christos return;
168 1.1 joerg
169 1.3 christos for (;;) {
170 1.3 christos if (n->type == ROFFT_ROOT)
171 1.3 christos return;
172 1.4 christos if (n->flags & NODE_VALID) {
173 1.3 christos n = n->parent;
174 1.3 christos continue;
175 1.3 christos }
176 1.3 christos if (n->type != ROFFT_BLOCK) {
177 1.3 christos if (n->parent->type == ROFFT_ROOT) {
178 1.3 christos man_unscope(man, n);
179 1.3 christos return;
180 1.3 christos } else {
181 1.3 christos n = n->parent;
182 1.3 christos continue;
183 1.3 christos }
184 1.3 christos }
185 1.3 christos if (tok != MAN_SH && (n->tok == MAN_SH ||
186 1.3 christos (tok != MAN_SS && (n->tok == MAN_SS ||
187 1.6 christos man_macro(n->tok)->fp == blk_exp))))
188 1.2 christos return;
189 1.3 christos man_unscope(man, n);
190 1.3 christos n = man->last;
191 1.1 joerg }
192 1.1 joerg }
193 1.1 joerg
194 1.1 joerg
195 1.2 christos /*
196 1.2 christos * Close out a generic explicit macro.
197 1.2 christos */
198 1.2 christos void
199 1.1 joerg blk_close(MACRO_PROT_ARGS)
200 1.1 joerg {
201 1.6 christos enum roff_tok ctok, ntok;
202 1.3 christos const struct roff_node *nn;
203 1.6 christos char *p, *ep;
204 1.6 christos int cline, cpos, la, nrew, target;
205 1.1 joerg
206 1.2 christos nrew = 1;
207 1.1 joerg switch (tok) {
208 1.2 christos case MAN_RE:
209 1.1 joerg ntok = MAN_RS;
210 1.6 christos la = *pos;
211 1.2 christos if ( ! man_args(man, line, pos, buf, &p))
212 1.2 christos break;
213 1.2 christos for (nn = man->last->parent; nn; nn = nn->parent)
214 1.3 christos if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
215 1.2 christos nrew++;
216 1.6 christos target = strtol(p, &ep, 10);
217 1.6 christos if (*ep != '\0')
218 1.6 christos mandoc_msg(MANDOCERR_ARG_EXCESS, line,
219 1.6 christos la + (buf[la] == '"') + (int)(ep - p),
220 1.6 christos "RE ... %s", ep);
221 1.6 christos free(p);
222 1.2 christos if (target == 0)
223 1.2 christos target = 1;
224 1.2 christos nrew -= target;
225 1.2 christos if (nrew < 1) {
226 1.6 christos mandoc_msg(MANDOCERR_RE_NOTOPEN,
227 1.2 christos line, ppos, "RE %d", target);
228 1.2 christos return;
229 1.2 christos }
230 1.2 christos break;
231 1.6 christos case MAN_YS:
232 1.6 christos ntok = MAN_SY;
233 1.6 christos break;
234 1.2 christos case MAN_UE:
235 1.2 christos ntok = MAN_UR;
236 1.1 joerg break;
237 1.5 christos case MAN_ME:
238 1.5 christos ntok = MAN_MT;
239 1.5 christos break;
240 1.1 joerg default:
241 1.1 joerg abort();
242 1.1 joerg }
243 1.1 joerg
244 1.2 christos for (nn = man->last->parent; nn; nn = nn->parent)
245 1.3 christos if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
246 1.1 joerg break;
247 1.1 joerg
248 1.2 christos if (nn == NULL) {
249 1.6 christos mandoc_msg(MANDOCERR_BLK_NOTOPEN,
250 1.6 christos line, ppos, "%s", roff_name[tok]);
251 1.3 christos rew_scope(man, MAN_PP);
252 1.6 christos if (tok == MAN_RE) {
253 1.6 christos roff_elem_alloc(man, line, ppos, ROFF_br);
254 1.6 christos man->last->flags |= NODE_LINE |
255 1.6 christos NODE_VALID | NODE_ENDED;
256 1.6 christos man->next = ROFF_NEXT_SIBLING;
257 1.2 christos }
258 1.6 christos return;
259 1.6 christos }
260 1.6 christos
261 1.6 christos cline = man->last->line;
262 1.6 christos cpos = man->last->pos;
263 1.6 christos ctok = man->last->tok;
264 1.6 christos man_unscope(man, nn);
265 1.6 christos
266 1.6 christos if (tok == MAN_RE && nn->head->aux > 0)
267 1.6 christos roff_setreg(man->roff, "an-margin", nn->head->aux, '-');
268 1.6 christos
269 1.6 christos /* Trailing text. */
270 1.6 christos
271 1.6 christos if (buf[*pos] != '\0') {
272 1.6 christos roff_word_alloc(man, line, ppos, buf + *pos);
273 1.6 christos man->last->flags |= NODE_DELIMC;
274 1.6 christos if (mandoc_eos(man->last->string, strlen(man->last->string)))
275 1.6 christos man->last->flags |= NODE_EOS;
276 1.6 christos }
277 1.6 christos
278 1.6 christos /* Move a trailing paragraph behind the block. */
279 1.6 christos
280 1.6 christos if (ctok == MAN_LP || ctok == MAN_PP || ctok == MAN_P) {
281 1.6 christos *pos = strlen(buf);
282 1.6 christos blk_imp(man, ctok, cline, cpos, pos, buf);
283 1.6 christos }
284 1.6 christos
285 1.6 christos /* Synopsis blocks need an explicit end marker for spacing. */
286 1.6 christos
287 1.6 christos if (tok == MAN_YS && man->last == nn) {
288 1.6 christos roff_elem_alloc(man, line, ppos, tok);
289 1.6 christos man_unscope(man, man->last);
290 1.2 christos }
291 1.2 christos }
292 1.2 christos
293 1.2 christos void
294 1.2 christos blk_exp(MACRO_PROT_ARGS)
295 1.2 christos {
296 1.3 christos struct roff_node *head;
297 1.2 christos char *p;
298 1.2 christos int la;
299 1.1 joerg
300 1.6 christos if (tok == MAN_RS) {
301 1.6 christos rew_scope(man, tok);
302 1.6 christos man->flags |= ROFF_NONOFILL;
303 1.6 christos }
304 1.3 christos roff_block_alloc(man, line, ppos, tok);
305 1.3 christos head = roff_head_alloc(man, line, ppos, tok);
306 1.2 christos
307 1.2 christos la = *pos;
308 1.5 christos if (man_args(man, line, pos, buf, &p)) {
309 1.3 christos roff_word_alloc(man, line, la, p);
310 1.5 christos if (tok == MAN_RS) {
311 1.5 christos if (roff_getreg(man->roff, "an-margin") == 0)
312 1.5 christos roff_setreg(man->roff, "an-margin",
313 1.5 christos 7 * 24, '=');
314 1.5 christos if ((head->aux = strtod(p, NULL) * 24.0) > 0)
315 1.5 christos roff_setreg(man->roff, "an-margin",
316 1.5 christos head->aux, '+');
317 1.5 christos }
318 1.6 christos free(p);
319 1.5 christos }
320 1.2 christos
321 1.2 christos if (buf[*pos] != '\0')
322 1.6 christos mandoc_msg(MANDOCERR_ARG_EXCESS, line, *pos,
323 1.6 christos "%s ... %s", roff_name[tok], buf + *pos);
324 1.2 christos
325 1.2 christos man_unscope(man, head);
326 1.3 christos roff_body_alloc(man, line, ppos, tok);
327 1.6 christos man->flags &= ~ROFF_NONOFILL;
328 1.1 joerg }
329 1.1 joerg
330 1.1 joerg /*
331 1.3 christos * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
332 1.3 christos * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
333 1.1 joerg * scopes, such as `SH' closing out an `SS', are defined in the rew
334 1.1 joerg * routines.
335 1.1 joerg */
336 1.2 christos void
337 1.1 joerg blk_imp(MACRO_PROT_ARGS)
338 1.1 joerg {
339 1.2 christos int la;
340 1.1 joerg char *p;
341 1.3 christos struct roff_node *n;
342 1.1 joerg
343 1.3 christos rew_scope(man, tok);
344 1.6 christos man->flags |= ROFF_NONOFILL;
345 1.6 christos if (tok == MAN_SH || tok == MAN_SS)
346 1.6 christos man->flags &= ~ROFF_NOFILL;
347 1.6 christos roff_block_alloc(man, line, ppos, tok);
348 1.3 christos n = roff_head_alloc(man, line, ppos, tok);
349 1.1 joerg
350 1.1 joerg /* Add line arguments. */
351 1.1 joerg
352 1.1 joerg for (;;) {
353 1.1 joerg la = *pos;
354 1.2 christos if ( ! man_args(man, line, pos, buf, &p))
355 1.1 joerg break;
356 1.3 christos roff_word_alloc(man, line, la, p);
357 1.6 christos free(p);
358 1.1 joerg }
359 1.1 joerg
360 1.3 christos /*
361 1.3 christos * For macros having optional next-line scope,
362 1.3 christos * keep the head open if there were no arguments.
363 1.6 christos * For `TP' and `TQ', always keep the head open.
364 1.3 christos */
365 1.1 joerg
366 1.6 christos if (man_macro(tok)->flags & MAN_BSCOPED &&
367 1.6 christos (tok == MAN_TP || tok == MAN_TQ || n == man->last)) {
368 1.3 christos man->flags |= MAN_BLINE;
369 1.3 christos return;
370 1.1 joerg }
371 1.3 christos
372 1.3 christos /* Close out the head and open the body. */
373 1.3 christos
374 1.3 christos man_unscope(man, n);
375 1.3 christos roff_body_alloc(man, line, ppos, tok);
376 1.6 christos man->flags &= ~ROFF_NONOFILL;
377 1.1 joerg }
378 1.1 joerg
379 1.2 christos void
380 1.1 joerg in_line_eoln(MACRO_PROT_ARGS)
381 1.1 joerg {
382 1.2 christos int la;
383 1.1 joerg char *p;
384 1.3 christos struct roff_node *n;
385 1.1 joerg
386 1.3 christos roff_elem_alloc(man, line, ppos, tok);
387 1.2 christos n = man->last;
388 1.1 joerg
389 1.6 christos if (tok == MAN_EX)
390 1.6 christos man->flags |= ROFF_NOFILL;
391 1.6 christos else if (tok == MAN_EE)
392 1.6 christos man->flags &= ~ROFF_NOFILL;
393 1.6 christos
394 1.1 joerg for (;;) {
395 1.5 christos if (buf[*pos] != '\0' && man->last != n && tok == MAN_PD) {
396 1.6 christos mandoc_msg(MANDOCERR_ARG_EXCESS, line, *pos,
397 1.6 christos "%s ... %s", roff_name[tok], buf + *pos);
398 1.2 christos break;
399 1.2 christos }
400 1.1 joerg la = *pos;
401 1.2 christos if ( ! man_args(man, line, pos, buf, &p))
402 1.2 christos break;
403 1.6 christos if (man_macro(tok)->flags & MAN_JOIN &&
404 1.3 christos man->last->type == ROFFT_TEXT)
405 1.3 christos roff_word_append(man, p);
406 1.2 christos else
407 1.3 christos roff_word_alloc(man, line, la, p);
408 1.6 christos free(p);
409 1.2 christos }
410 1.1 joerg
411 1.2 christos /*
412 1.4 christos * Append NODE_EOS in case the last snipped argument
413 1.2 christos * ends with a dot, e.g. `.IR syslog (3).'
414 1.2 christos */
415 1.2 christos
416 1.2 christos if (n != man->last &&
417 1.2 christos mandoc_eos(man->last->string, strlen(man->last->string)))
418 1.4 christos man->last->flags |= NODE_EOS;
419 1.2 christos
420 1.2 christos /*
421 1.6 christos * If no arguments are specified and this is MAN_ESCOPED (i.e.,
422 1.2 christos * next-line scoped), then set our mode to indicate that we're
423 1.2 christos * waiting for terms to load into our context.
424 1.2 christos */
425 1.1 joerg
426 1.6 christos if (n == man->last && man_macro(tok)->flags & MAN_ESCOPED) {
427 1.2 christos man->flags |= MAN_ELINE;
428 1.2 christos return;
429 1.1 joerg }
430 1.1 joerg
431 1.3 christos assert(man->last->type != ROFFT_ROOT);
432 1.3 christos man->next = ROFF_NEXT_SIBLING;
433 1.1 joerg
434 1.3 christos /* Rewind our element scope. */
435 1.1 joerg
436 1.2 christos for ( ; man->last; man->last = man->last->parent) {
437 1.6 christos man->last->flags |= NODE_VALID;
438 1.2 christos if (man->last == n)
439 1.1 joerg break;
440 1.1 joerg }
441 1.6 christos
442 1.6 christos /* Rewind next-line scoped ancestors, if any. */
443 1.6 christos
444 1.6 christos if (man_macro(tok)->flags & MAN_ESCOPED)
445 1.6 christos man_descope(man, line, ppos, NULL);
446 1.2 christos }
447 1.2 christos
448 1.2 christos void
449 1.3 christos man_endparse(struct roff_man *man)
450 1.2 christos {
451 1.6 christos man_unscope(man, man->meta.first);
452 1.1 joerg }
453 1.1 joerg
454 1.2 christos static int
455 1.3 christos man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
456 1.1 joerg {
457 1.2 christos char *start;
458 1.1 joerg
459 1.2 christos assert(*pos);
460 1.2 christos *v = start = buf + *pos;
461 1.2 christos assert(' ' != *start);
462 1.1 joerg
463 1.2 christos if ('\0' == *start)
464 1.3 christos return 0;
465 1.1 joerg
466 1.6 christos *v = roff_getarg(man->roff, v, line, pos);
467 1.3 christos return 1;
468 1.1 joerg }
469