]> git.cameronkatri.com Git - mandoc.git/blob - man_macro.c
Minimal implementation of the \h (horizontal motion) escape sequence.
[mandoc.git] / man_macro.c
1 /* $Id: man_macro.c,v 1.120 2017/05/05 15:17:32 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 /* Move a trailing paragraph behind the block. */
239
240 if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
241 *pos = strlen(buf);
242 blk_imp(man, ntok, line, ppos, pos, buf);
243 }
244 }
245 }
246
247 void
248 blk_exp(MACRO_PROT_ARGS)
249 {
250 struct roff_node *head;
251 char *p;
252 int la;
253
254 rew_scope(man, tok);
255 roff_block_alloc(man, line, ppos, tok);
256 head = roff_head_alloc(man, line, ppos, tok);
257
258 la = *pos;
259 if (man_args(man, line, pos, buf, &p))
260 roff_word_alloc(man, line, la, p);
261
262 if (buf[*pos] != '\0')
263 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse, line,
264 *pos, "%s ... %s", roff_name[tok], buf + *pos);
265
266 man_unscope(man, head);
267 roff_body_alloc(man, line, ppos, tok);
268 }
269
270 /*
271 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a
272 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other
273 * scopes, such as `SH' closing out an `SS', are defined in the rew
274 * routines.
275 */
276 void
277 blk_imp(MACRO_PROT_ARGS)
278 {
279 int la;
280 char *p;
281 struct roff_node *n;
282
283 rew_scope(man, tok);
284 n = roff_block_alloc(man, line, ppos, tok);
285 if (n->tok == MAN_SH || n->tok == MAN_SS)
286 man->flags &= ~MAN_LITERAL;
287 n = roff_head_alloc(man, line, ppos, tok);
288
289 /* Add line arguments. */
290
291 for (;;) {
292 la = *pos;
293 if ( ! man_args(man, line, pos, buf, &p))
294 break;
295 roff_word_alloc(man, line, la, p);
296 }
297
298 /*
299 * For macros having optional next-line scope,
300 * keep the head open if there were no arguments.
301 * For `TP', always keep the head open.
302 */
303
304 if (man_macros[tok].flags & MAN_SCOPED &&
305 (tok == MAN_TP || n == man->last)) {
306 man->flags |= MAN_BLINE;
307 return;
308 }
309
310 /* Close out the head and open the body. */
311
312 man_unscope(man, n);
313 roff_body_alloc(man, line, ppos, tok);
314 }
315
316 void
317 in_line_eoln(MACRO_PROT_ARGS)
318 {
319 int la;
320 char *p;
321 struct roff_node *n;
322
323 roff_elem_alloc(man, line, ppos, tok);
324 n = man->last;
325
326 for (;;) {
327 if (buf[*pos] != '\0' && (tok == MAN_fi || tok == MAN_nf)) {
328 mandoc_vmsg(MANDOCERR_ARG_SKIP,
329 man->parse, line, *pos, "%s %s",
330 roff_name[tok], buf + *pos);
331 break;
332 }
333 if (buf[*pos] != '\0' && man->last != n && tok == MAN_PD) {
334 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
335 man->parse, line, *pos, "%s ... %s",
336 roff_name[tok], buf + *pos);
337 break;
338 }
339 la = *pos;
340 if ( ! man_args(man, line, pos, buf, &p))
341 break;
342 if (man_macros[tok].flags & MAN_JOIN &&
343 man->last->type == ROFFT_TEXT)
344 roff_word_append(man, p);
345 else
346 roff_word_alloc(man, line, la, p);
347 }
348
349 /*
350 * Append NODE_EOS in case the last snipped argument
351 * ends with a dot, e.g. `.IR syslog (3).'
352 */
353
354 if (n != man->last &&
355 mandoc_eos(man->last->string, strlen(man->last->string)))
356 man->last->flags |= NODE_EOS;
357
358 /*
359 * If no arguments are specified and this is MAN_SCOPED (i.e.,
360 * next-line scoped), then set our mode to indicate that we're
361 * waiting for terms to load into our context.
362 */
363
364 if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
365 assert( ! (man_macros[tok].flags & MAN_NSCOPED));
366 man->flags |= MAN_ELINE;
367 return;
368 }
369
370 assert(man->last->type != ROFFT_ROOT);
371 man->next = ROFF_NEXT_SIBLING;
372
373 /* Rewind our element scope. */
374
375 for ( ; man->last; man->last = man->last->parent) {
376 man_state(man, man->last);
377 if (man->last == n)
378 break;
379 }
380 }
381
382 void
383 man_endparse(struct roff_man *man)
384 {
385
386 man_unscope(man, man->first);
387 man->flags &= ~MAN_LITERAL;
388 }
389
390 static int
391 man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
392 {
393 char *start;
394
395 assert(*pos);
396 *v = start = buf + *pos;
397 assert(' ' != *start);
398
399 if ('\0' == *start)
400 return 0;
401
402 *v = mandoc_getarg(man->parse, v, line, pos);
403 return 1;
404 }