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