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