]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
Clean up the validation of .Pp, .PP, .sp, and .br. Make sure all
[mandoc.git] / mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.226 2018/12/04 02:53:51 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2018 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <time.h>
28
29 #include "mandoc.h"
30 #include "roff.h"
31 #include "mdoc.h"
32 #include "libmandoc.h"
33 #include "roff_int.h"
34 #include "libmdoc.h"
35
36 static void blk_full(MACRO_PROT_ARGS);
37 static void blk_exp_close(MACRO_PROT_ARGS);
38 static void blk_part_exp(MACRO_PROT_ARGS);
39 static void blk_part_imp(MACRO_PROT_ARGS);
40 static void ctx_synopsis(MACRO_PROT_ARGS);
41 static void in_line_eoln(MACRO_PROT_ARGS);
42 static void in_line_argn(MACRO_PROT_ARGS);
43 static void in_line(MACRO_PROT_ARGS);
44 static void phrase_ta(MACRO_PROT_ARGS);
45
46 static void append_delims(struct roff_man *, int, int *, char *);
47 static void dword(struct roff_man *, int, int, const char *,
48 enum mdelim, int);
49 static int find_pending(struct roff_man *, enum roff_tok,
50 int, int, struct roff_node *);
51 static int lookup(struct roff_man *, int, int, int, const char *);
52 static int macro_or_word(MACRO_PROT_ARGS, int);
53 static void break_intermediate(struct roff_node *,
54 struct roff_node *);
55 static int parse_rest(struct roff_man *, enum roff_tok,
56 int, int *, char *);
57 static enum roff_tok rew_alt(enum roff_tok);
58 static void rew_elem(struct roff_man *, enum roff_tok);
59 static void rew_last(struct roff_man *, const struct roff_node *);
60 static void rew_pending(struct roff_man *,
61 const struct roff_node *);
62
63 static const struct mdoc_macro mdoc_macros[MDOC_MAX - MDOC_Dd] = {
64 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
65 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
66 { in_line_eoln, MDOC_PROLOGUE }, /* Os */
67 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */
68 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */
69 { in_line_eoln, 0 }, /* Pp */
70 { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */
71 { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */
72 { blk_full, MDOC_EXPLICIT }, /* Bd */
73 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */
74 { blk_full, MDOC_EXPLICIT }, /* Bl */
75 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */
76 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */
77 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
78 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */
79 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
80 MDOC_IGNDELIM | MDOC_JOIN }, /* Ap */
81 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
82 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
83 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
84 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
85 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
86 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
87 { in_line_eoln, 0 }, /* Ex */
88 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
89 { in_line_eoln, 0 }, /* Fd */
90 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
91 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
92 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
93 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
94 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
95 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
96 { blk_full, MDOC_JOIN }, /* Nd */
97 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
98 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
99 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */
100 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
101 { in_line_eoln, 0 }, /* Rv */
102 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
103 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
104 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
105 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
106 { in_line_eoln, MDOC_JOIN }, /* %A */
107 { in_line_eoln, MDOC_JOIN }, /* %B */
108 { in_line_eoln, MDOC_JOIN }, /* %D */
109 { in_line_eoln, MDOC_JOIN }, /* %I */
110 { in_line_eoln, MDOC_JOIN }, /* %J */
111 { in_line_eoln, 0 }, /* %N */
112 { in_line_eoln, MDOC_JOIN }, /* %O */
113 { in_line_eoln, 0 }, /* %P */
114 { in_line_eoln, MDOC_JOIN }, /* %R */
115 { in_line_eoln, MDOC_JOIN }, /* %T */
116 { in_line_eoln, 0 }, /* %V */
117 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
118 MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
119 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
120 MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
121 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
122 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
123 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
124 MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
125 { blk_full, MDOC_EXPLICIT }, /* Bf */
126 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
127 MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
128 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
129 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
130 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
131 { in_line_eoln, 0 }, /* Db */
132 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
133 MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
134 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
135 MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
136 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
137 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
138 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
139 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
140 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
141 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
142 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
143 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* No */
144 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
145 MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
146 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
147 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
148 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
149 MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
150 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
151 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
152 MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
153 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
154 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
155 MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
156 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
157 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
158 MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
159 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
160 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
161 { blk_full, MDOC_EXPLICIT }, /* Rs */
162 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
163 MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
164 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
165 MDOC_EXPLICIT | MDOC_JOIN }, /* So */
166 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
167 { in_line_argn, 0 }, /* Sm */
168 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
169 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
170 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
171 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
172 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
173 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
174 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
175 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
176 MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
177 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
178 MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
179 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
180 MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
181 { blk_full, MDOC_EXPLICIT }, /* Bk */
182 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
183 { in_line_eoln, 0 }, /* Bt */
184 { in_line_eoln, 0 }, /* Hf */
185 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */
186 { in_line_eoln, 0 }, /* Ud */
187 { in_line, 0 }, /* Lb */
188 { in_line_eoln, 0 }, /* Lp */
189 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
190 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
191 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
192 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
193 MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
194 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
195 MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
196 { in_line_eoln, MDOC_JOIN }, /* %C */
197 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */
198 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */
199 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
200 { in_line_eoln, MDOC_JOIN }, /* %Q */
201 { in_line_eoln, 0 }, /* %U */
202 { phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
203 };
204
205
206 const struct mdoc_macro *
207 mdoc_macro(enum roff_tok tok)
208 {
209 assert(tok >= MDOC_Dd && tok < MDOC_MAX);
210 return mdoc_macros + (tok - MDOC_Dd);
211 }
212
213 /*
214 * This is called at the end of parsing. It must traverse up the tree,
215 * closing out open [implicit] scopes. Obviously, open explicit scopes
216 * are errors.
217 */
218 void
219 mdoc_endparse(struct roff_man *mdoc)
220 {
221 struct roff_node *n;
222
223 /* Scan for open explicit scopes. */
224
225 n = mdoc->last->flags & NODE_VALID ?
226 mdoc->last->parent : mdoc->last;
227
228 for ( ; n; n = n->parent)
229 if (n->type == ROFFT_BLOCK &&
230 mdoc_macro(n->tok)->flags & MDOC_EXPLICIT)
231 mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse,
232 n->line, n->pos, roff_name[n->tok]);
233
234 /* Rewind to the first. */
235
236 rew_last(mdoc, mdoc->first);
237 mdoc_state_reset(mdoc);
238 }
239
240 /*
241 * Look up the macro at *p called by "from",
242 * or as a line macro if from == TOKEN_NONE.
243 */
244 static int
245 lookup(struct roff_man *mdoc, int from, int line, int ppos, const char *p)
246 {
247 enum roff_tok res;
248
249 if (mdoc->flags & MDOC_PHRASEQF) {
250 mdoc->flags &= ~MDOC_PHRASEQF;
251 return TOKEN_NONE;
252 }
253 if (from == TOKEN_NONE || mdoc_macro(from)->flags & MDOC_PARSED) {
254 res = roffhash_find(mdoc->mdocmac, p, 0);
255 if (res != TOKEN_NONE) {
256 if (mdoc_macro(res)->flags & MDOC_CALLABLE)
257 return res;
258 mandoc_msg(MANDOCERR_MACRO_CALL,
259 mdoc->parse, line, ppos, p);
260 }
261 }
262 return TOKEN_NONE;
263 }
264
265 /*
266 * Rewind up to and including a specific node.
267 */
268 static void
269 rew_last(struct roff_man *mdoc, const struct roff_node *to)
270 {
271
272 if (to->flags & NODE_VALID)
273 return;
274
275 while (mdoc->last != to) {
276 mdoc_state(mdoc, mdoc->last);
277 mdoc->last->flags |= NODE_VALID | NODE_ENDED;
278 mdoc->last = mdoc->last->parent;
279 }
280 mdoc_state(mdoc, mdoc->last);
281 mdoc->last->flags |= NODE_VALID | NODE_ENDED;
282 mdoc->next = ROFF_NEXT_SIBLING;
283 }
284
285 /*
286 * Rewind up to a specific block, including all blocks that broke it.
287 */
288 static void
289 rew_pending(struct roff_man *mdoc, const struct roff_node *n)
290 {
291
292 for (;;) {
293 rew_last(mdoc, n);
294
295 if (mdoc->last == n) {
296 switch (n->type) {
297 case ROFFT_HEAD:
298 roff_body_alloc(mdoc, n->line, n->pos,
299 n->tok);
300 break;
301 case ROFFT_BLOCK:
302 break;
303 default:
304 return;
305 }
306 if ( ! (n->flags & NODE_BROKEN))
307 return;
308 } else
309 n = mdoc->last;
310
311 for (;;) {
312 if ((n = n->parent) == NULL)
313 return;
314
315 if (n->type == ROFFT_BLOCK ||
316 n->type == ROFFT_HEAD) {
317 if (n->flags & NODE_ENDED)
318 break;
319 else
320 return;
321 }
322 }
323 }
324 }
325
326 /*
327 * For a block closing macro, return the corresponding opening one.
328 * Otherwise, return the macro itself.
329 */
330 static enum roff_tok
331 rew_alt(enum roff_tok tok)
332 {
333 switch (tok) {
334 case MDOC_Ac:
335 return MDOC_Ao;
336 case MDOC_Bc:
337 return MDOC_Bo;
338 case MDOC_Brc:
339 return MDOC_Bro;
340 case MDOC_Dc:
341 return MDOC_Do;
342 case MDOC_Ec:
343 return MDOC_Eo;
344 case MDOC_Ed:
345 return MDOC_Bd;
346 case MDOC_Ef:
347 return MDOC_Bf;
348 case MDOC_Ek:
349 return MDOC_Bk;
350 case MDOC_El:
351 return MDOC_Bl;
352 case MDOC_Fc:
353 return MDOC_Fo;
354 case MDOC_Oc:
355 return MDOC_Oo;
356 case MDOC_Pc:
357 return MDOC_Po;
358 case MDOC_Qc:
359 return MDOC_Qo;
360 case MDOC_Re:
361 return MDOC_Rs;
362 case MDOC_Sc:
363 return MDOC_So;
364 case MDOC_Xc:
365 return MDOC_Xo;
366 default:
367 return tok;
368 }
369 }
370
371 static void
372 rew_elem(struct roff_man *mdoc, enum roff_tok tok)
373 {
374 struct roff_node *n;
375
376 n = mdoc->last;
377 if (n->type != ROFFT_ELEM)
378 n = n->parent;
379 assert(n->type == ROFFT_ELEM);
380 assert(tok == n->tok);
381 rew_last(mdoc, n);
382 }
383
384 static void
385 break_intermediate(struct roff_node *n, struct roff_node *breaker)
386 {
387 if (n != breaker &&
388 n->type != ROFFT_BLOCK && n->type != ROFFT_HEAD &&
389 (n->type != ROFFT_BODY || n->end != ENDBODY_NOT))
390 n = n->parent;
391 while (n != breaker) {
392 if ( ! (n->flags & NODE_VALID))
393 n->flags |= NODE_BROKEN;
394 n = n->parent;
395 }
396 }
397
398 /*
399 * If there is an open sub-block of the target requiring
400 * explicit close-out, postpone closing out the target until
401 * the rew_pending() call closing out the sub-block.
402 */
403 static int
404 find_pending(struct roff_man *mdoc, enum roff_tok tok, int line, int ppos,
405 struct roff_node *target)
406 {
407 struct roff_node *n;
408 int irc;
409
410 if (target->flags & NODE_VALID)
411 return 0;
412
413 irc = 0;
414 for (n = mdoc->last; n != NULL && n != target; n = n->parent) {
415 if (n->flags & NODE_ENDED)
416 continue;
417 if (n->type == ROFFT_BLOCK &&
418 mdoc_macro(n->tok)->flags & MDOC_EXPLICIT) {
419 irc = 1;
420 break_intermediate(mdoc->last, target);
421 if (target->type == ROFFT_HEAD)
422 target->flags |= NODE_ENDED;
423 else if ( ! (target->flags & NODE_ENDED)) {
424 mandoc_vmsg(MANDOCERR_BLK_NEST,
425 mdoc->parse, line, ppos,
426 "%s breaks %s", roff_name[tok],
427 roff_name[n->tok]);
428 mdoc_endbody_alloc(mdoc, line, ppos,
429 tok, target);
430 }
431 }
432 }
433 return irc;
434 }
435
436 /*
437 * Allocate a word and check whether it's punctuation or not.
438 * Punctuation consists of those tokens found in mdoc_isdelim().
439 */
440 static void
441 dword(struct roff_man *mdoc, int line, int col, const char *p,
442 enum mdelim d, int may_append)
443 {
444
445 if (d == DELIM_MAX)
446 d = mdoc_isdelim(p);
447
448 if (may_append &&
449 ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
450 d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT &&
451 mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
452 roff_word_append(mdoc, p);
453 return;
454 }
455
456 roff_word_alloc(mdoc, line, col, p);
457
458 /*
459 * If the word consists of a bare delimiter,
460 * flag the new node accordingly,
461 * unless doing so was vetoed by the invoking macro.
462 * Always clear the veto, it is only valid for one word.
463 */
464
465 if (d == DELIM_OPEN)
466 mdoc->last->flags |= NODE_DELIMO;
467 else if (d == DELIM_CLOSE &&
468 ! (mdoc->flags & MDOC_NODELIMC) &&
469 mdoc->last->parent->tok != MDOC_Fd)
470 mdoc->last->flags |= NODE_DELIMC;
471 mdoc->flags &= ~MDOC_NODELIMC;
472 }
473
474 static void
475 append_delims(struct roff_man *mdoc, int line, int *pos, char *buf)
476 {
477 char *p;
478 int la;
479
480 if (buf[*pos] == '\0')
481 return;
482
483 for (;;) {
484 la = *pos;
485 if (mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p) ==
486 ARGS_EOLN)
487 break;
488 dword(mdoc, line, la, p, DELIM_MAX, 1);
489
490 /*
491 * If we encounter end-of-sentence symbols, then trigger
492 * the double-space.
493 *
494 * XXX: it's easy to allow this to propagate outward to
495 * the last symbol, such that `. )' will cause the
496 * correct double-spacing. However, (1) groff isn't
497 * smart enough to do this and (2) it would require
498 * knowing which symbols break this behaviour, for
499 * example, `. ;' shouldn't propagate the double-space.
500 */
501
502 if (mandoc_eos(p, strlen(p)))
503 mdoc->last->flags |= NODE_EOS;
504 }
505 }
506
507 /*
508 * Parse one word.
509 * If it is a macro, call it and return 1.
510 * Otherwise, allocate it and return 0.
511 */
512 static int
513 macro_or_word(MACRO_PROT_ARGS, int parsed)
514 {
515 char *p;
516 int ntok;
517
518 p = buf + ppos;
519 ntok = TOKEN_NONE;
520 if (*p == '"')
521 p++;
522 else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
523 ntok = lookup(mdoc, tok, line, ppos, p);
524
525 if (ntok == TOKEN_NONE) {
526 dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE ||
527 mdoc_macro(tok)->flags & MDOC_JOIN);
528 return 0;
529 } else {
530 if (tok != TOKEN_NONE &&
531 mdoc_macro(tok)->fp == in_line_eoln)
532 rew_elem(mdoc, tok);
533 (*mdoc_macro(ntok)->fp)(mdoc, ntok, line, ppos, pos, buf);
534 if (tok == TOKEN_NONE)
535 append_delims(mdoc, line, pos, buf);
536 return 1;
537 }
538 }
539
540 /*
541 * Close out block partial/full explicit.
542 */
543 static void
544 blk_exp_close(MACRO_PROT_ARGS)
545 {
546 struct roff_node *body; /* Our own body. */
547 struct roff_node *endbody; /* Our own end marker. */
548 struct roff_node *itblk; /* An It block starting later. */
549 struct roff_node *later; /* A sub-block starting later. */
550 struct roff_node *n; /* Search back to our block. */
551 struct roff_node *target; /* For find_pending(). */
552
553 int j, lastarg, maxargs, nl, pending;
554 enum margserr ac;
555 enum roff_tok atok, ntok;
556 char *p;
557
558 nl = MDOC_NEWLINE & mdoc->flags;
559
560 switch (tok) {
561 case MDOC_Ec:
562 maxargs = 1;
563 break;
564 case MDOC_Ek:
565 mdoc->flags &= ~MDOC_KEEP;
566 /* FALLTHROUGH */
567 default:
568 maxargs = 0;
569 break;
570 }
571
572 /* Search backwards for the beginning of our own body. */
573
574 atok = rew_alt(tok);
575 body = NULL;
576 for (n = mdoc->last; n; n = n->parent) {
577 if (n->flags & NODE_ENDED || n->tok != atok ||
578 n->type != ROFFT_BODY || n->end != ENDBODY_NOT)
579 continue;
580 body = n;
581 break;
582 }
583
584 /*
585 * Search backwards for beginnings of blocks,
586 * both of our own and of pending sub-blocks.
587 */
588
589 endbody = itblk = later = NULL;
590 for (n = mdoc->last; n; n = n->parent) {
591 if (n->flags & NODE_ENDED)
592 continue;
593
594 /*
595 * Mismatching end macros can never break anything
596 * and we only care about the breaking of BLOCKs.
597 */
598
599 if (body == NULL || n->type != ROFFT_BLOCK)
600 continue;
601
602 /*
603 * SYNOPSIS name blocks can not be broken themselves,
604 * but they do get broken together with a broken child.
605 */
606
607 if (n->tok == MDOC_Nm) {
608 if (later != NULL)
609 n->flags |= NODE_BROKEN | NODE_ENDED;
610 continue;
611 }
612
613 if (n->tok == MDOC_It) {
614 itblk = n;
615 continue;
616 }
617
618 if (atok == n->tok) {
619
620 /*
621 * Found the start of our own block.
622 * When there is no pending sub block,
623 * just proceed to closing out.
624 */
625
626 if (later == NULL ||
627 (tok == MDOC_El && itblk == NULL))
628 break;
629
630 /*
631 * When there is a pending sub block, postpone
632 * closing out the current block until the
633 * rew_pending() closing out the sub-block.
634 * Mark the place where the formatting - but not
635 * the scope - of the current block ends.
636 */
637
638 mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse,
639 line, ppos, "%s breaks %s",
640 roff_name[atok], roff_name[later->tok]);
641
642 endbody = mdoc_endbody_alloc(mdoc, line, ppos,
643 atok, body);
644
645 if (tok == MDOC_El)
646 itblk->flags |= NODE_ENDED | NODE_BROKEN;
647
648 /*
649 * If a block closing macro taking arguments
650 * breaks another block, put the arguments
651 * into the end marker.
652 */
653
654 if (maxargs)
655 mdoc->next = ROFF_NEXT_CHILD;
656 break;
657 }
658
659 /*
660 * Explicit blocks close out description lines, but
661 * even those can get broken together with a child.
662 */
663
664 if (n->tok == MDOC_Nd) {
665 if (later != NULL)
666 n->flags |= NODE_BROKEN | NODE_ENDED;
667 else
668 rew_last(mdoc, n);
669 continue;
670 }
671
672 /* Breaking an open sub block. */
673
674 break_intermediate(mdoc->last, body);
675 n->flags |= NODE_BROKEN;
676 if (later == NULL)
677 later = n;
678 }
679
680 if (body == NULL) {
681 mandoc_msg(MANDOCERR_BLK_NOTOPEN, mdoc->parse,
682 line, ppos, roff_name[tok]);
683 if (maxargs && endbody == NULL) {
684 /*
685 * Stray .Ec without previous .Eo:
686 * Break the output line, keep the arguments.
687 */
688 roff_elem_alloc(mdoc, line, ppos, ROFF_br);
689 rew_elem(mdoc, ROFF_br);
690 }
691 } else if (endbody == NULL) {
692 rew_last(mdoc, body);
693 if (maxargs)
694 mdoc_tail_alloc(mdoc, line, ppos, atok);
695 }
696
697 if ((mdoc_macro(tok)->flags & MDOC_PARSED) == 0) {
698 if (buf[*pos] != '\0')
699 mandoc_vmsg(MANDOCERR_ARG_SKIP,
700 mdoc->parse, line, ppos,
701 "%s %s", roff_name[tok],
702 buf + *pos);
703 if (endbody == NULL && n != NULL)
704 rew_pending(mdoc, n);
705 return;
706 }
707
708 if (endbody != NULL)
709 n = endbody;
710
711 ntok = TOKEN_NONE;
712 for (j = 0; ; j++) {
713 lastarg = *pos;
714
715 if (j == maxargs && n != NULL)
716 rew_last(mdoc, n);
717
718 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
719 if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
720 break;
721
722 ntok = lookup(mdoc, tok, line, lastarg, p);
723
724 if (ntok == TOKEN_NONE) {
725 dword(mdoc, line, lastarg, p, DELIM_MAX,
726 mdoc_macro(tok)->flags & MDOC_JOIN);
727 continue;
728 }
729
730 if (n != NULL)
731 rew_last(mdoc, n);
732 mdoc->flags &= ~MDOC_NEWLINE;
733 (*mdoc_macro(ntok)->fp)(mdoc, ntok, line, lastarg, pos, buf);
734 break;
735 }
736
737 if (n != NULL) {
738 pending = 0;
739 if (ntok != TOKEN_NONE && n->flags & NODE_BROKEN) {
740 target = n;
741 do
742 target = target->parent;
743 while ( ! (target->flags & NODE_ENDED));
744 pending = find_pending(mdoc, ntok, line, ppos, target);
745 }
746 if ( ! pending)
747 rew_pending(mdoc, n);
748 }
749 if (nl)
750 append_delims(mdoc, line, pos, buf);
751 }
752
753 static void
754 in_line(MACRO_PROT_ARGS)
755 {
756 int la, scope, cnt, firstarg, mayopen, nc, nl;
757 enum roff_tok ntok;
758 enum margserr ac;
759 enum mdelim d;
760 struct mdoc_arg *arg;
761 char *p;
762
763 nl = MDOC_NEWLINE & mdoc->flags;
764
765 /*
766 * Whether we allow ignored elements (those without content,
767 * usually because of reserved words) to squeak by.
768 */
769
770 switch (tok) {
771 case MDOC_An:
772 case MDOC_Ar:
773 case MDOC_Fl:
774 case MDOC_Mt:
775 case MDOC_Nm:
776 case MDOC_Pa:
777 nc = 1;
778 break;
779 default:
780 nc = 0;
781 break;
782 }
783
784 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
785
786 d = DELIM_NONE;
787 firstarg = 1;
788 mayopen = 1;
789 for (cnt = scope = 0;; ) {
790 la = *pos;
791 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
792
793 /*
794 * At the end of a macro line,
795 * opening delimiters do not suppress spacing.
796 */
797
798 if (ac == ARGS_EOLN) {
799 if (d == DELIM_OPEN)
800 mdoc->last->flags &= ~NODE_DELIMO;
801 break;
802 }
803
804 /*
805 * The rest of the macro line is only punctuation,
806 * to be handled by append_delims().
807 * If there were no other arguments,
808 * do not allow the first one to suppress spacing,
809 * even if it turns out to be a closing one.
810 */
811
812 if (ac == ARGS_PUNCT) {
813 if (cnt == 0 && (nc == 0 || tok == MDOC_An))
814 mdoc->flags |= MDOC_NODELIMC;
815 break;
816 }
817
818 ntok = (tok == MDOC_Fn && !cnt) ?
819 TOKEN_NONE : lookup(mdoc, tok, line, la, p);
820
821 /*
822 * In this case, we've located a submacro and must
823 * execute it. Close out scope, if open. If no
824 * elements have been generated, either create one (nc)
825 * or raise a warning.
826 */
827
828 if (ntok != TOKEN_NONE) {
829 if (scope)
830 rew_elem(mdoc, tok);
831 if (nc && ! cnt) {
832 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
833 rew_last(mdoc, mdoc->last);
834 } else if ( ! nc && ! cnt) {
835 mdoc_argv_free(arg);
836 mandoc_msg(MANDOCERR_MACRO_EMPTY,
837 mdoc->parse, line, ppos,
838 roff_name[tok]);
839 }
840 (*mdoc_macro(ntok)->fp)(mdoc, ntok,
841 line, la, pos, buf);
842 if (nl)
843 append_delims(mdoc, line, pos, buf);
844 return;
845 }
846
847 /*
848 * Handle punctuation. Set up our scope, if a word;
849 * rewind the scope, if a delimiter; then append the word.
850 */
851
852 if ((d = mdoc_isdelim(p)) != DELIM_NONE) {
853 /*
854 * If we encounter closing punctuation, no word
855 * has been emitted, no scope is open, and we're
856 * allowed to have an empty element, then start
857 * a new scope.
858 */
859 if ((d == DELIM_CLOSE ||
860 (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
861 !cnt && !scope && nc && mayopen) {
862 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
863 scope = 1;
864 cnt++;
865 if (tok == MDOC_Nm)
866 mayopen = 0;
867 }
868 /*
869 * Close out our scope, if one is open, before
870 * any punctuation.
871 */
872 if (scope && tok != MDOC_Lk) {
873 rew_elem(mdoc, tok);
874 scope = 0;
875 if (tok == MDOC_Fn)
876 mayopen = 0;
877 }
878 } else if (mayopen && !scope) {
879 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
880 scope = 1;
881 cnt++;
882 }
883
884 dword(mdoc, line, la, p, d,
885 mdoc_macro(tok)->flags & MDOC_JOIN);
886
887 /*
888 * If the first argument is a closing delimiter,
889 * do not suppress spacing before it.
890 */
891
892 if (firstarg && d == DELIM_CLOSE && !nc)
893 mdoc->last->flags &= ~NODE_DELIMC;
894 firstarg = 0;
895
896 /*
897 * `Fl' macros have their scope re-opened with each new
898 * word so that the `-' can be added to each one without
899 * having to parse out spaces.
900 */
901 if (scope && tok == MDOC_Fl) {
902 rew_elem(mdoc, tok);
903 scope = 0;
904 }
905 }
906
907 if (scope && tok != MDOC_Lk) {
908 rew_elem(mdoc, tok);
909 scope = 0;
910 }
911
912 /*
913 * If no elements have been collected and we're allowed to have
914 * empties (nc), open a scope and close it out. Otherwise,
915 * raise a warning.
916 */
917
918 if ( ! cnt) {
919 if (nc) {
920 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
921 rew_last(mdoc, mdoc->last);
922 } else {
923 mdoc_argv_free(arg);
924 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
925 line, ppos, roff_name[tok]);
926 }
927 }
928 if (nl)
929 append_delims(mdoc, line, pos, buf);
930 if (scope)
931 rew_elem(mdoc, tok);
932 }
933
934 static void
935 blk_full(MACRO_PROT_ARGS)
936 {
937 int la, nl, parsed;
938 struct mdoc_arg *arg;
939 struct roff_node *blk; /* Our own or a broken block. */
940 struct roff_node *head; /* Our own head. */
941 struct roff_node *body; /* Our own body. */
942 struct roff_node *n;
943 enum margserr ac, lac;
944 char *p;
945
946 nl = MDOC_NEWLINE & mdoc->flags;
947
948 if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) {
949 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
950 line, ppos, roff_name[tok]);
951 return;
952 }
953
954 if ((mdoc_macro(tok)->flags & MDOC_EXPLICIT) == 0) {
955
956 /* Here, tok is one of Sh Ss Nm Nd It. */
957
958 blk = NULL;
959 for (n = mdoc->last; n != NULL; n = n->parent) {
960 if (n->flags & NODE_ENDED) {
961 if ( ! (n->flags & NODE_VALID))
962 n->flags |= NODE_BROKEN;
963 continue;
964 }
965 if (n->type != ROFFT_BLOCK)
966 continue;
967
968 if (tok == MDOC_It && n->tok == MDOC_Bl) {
969 if (blk != NULL) {
970 mandoc_vmsg(MANDOCERR_BLK_BROKEN,
971 mdoc->parse, line, ppos,
972 "It breaks %s",
973 roff_name[blk->tok]);
974 rew_pending(mdoc, blk);
975 }
976 break;
977 }
978
979 if (mdoc_macro(n->tok)->flags & MDOC_EXPLICIT) {
980 switch (tok) {
981 case MDOC_Sh:
982 case MDOC_Ss:
983 mandoc_vmsg(MANDOCERR_BLK_BROKEN,
984 mdoc->parse, line, ppos,
985 "%s breaks %s", roff_name[tok],
986 roff_name[n->tok]);
987 rew_pending(mdoc, n);
988 n = mdoc->last;
989 continue;
990 case MDOC_It:
991 /* Delay in case it's astray. */
992 blk = n;
993 continue;
994 default:
995 break;
996 }
997 break;
998 }
999
1000 /* Here, n is one of Sh Ss Nm Nd It. */
1001
1002 if (tok != MDOC_Sh && (n->tok == MDOC_Sh ||
1003 (tok != MDOC_Ss && (n->tok == MDOC_Ss ||
1004 (tok != MDOC_It && n->tok == MDOC_It)))))
1005 break;
1006
1007 /* Item breaking an explicit block. */
1008
1009 if (blk != NULL) {
1010 mandoc_vmsg(MANDOCERR_BLK_BROKEN,
1011 mdoc->parse, line, ppos,
1012 "It breaks %s", roff_name[blk->tok]);
1013 rew_pending(mdoc, blk);
1014 blk = NULL;
1015 }
1016
1017 /* Close out prior implicit scopes. */
1018
1019 rew_pending(mdoc, n);
1020 }
1021
1022 /* Skip items outside lists. */
1023
1024 if (tok == MDOC_It && (n == NULL || n->tok != MDOC_Bl)) {
1025 mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
1026 line, ppos, "It %s", buf + *pos);
1027 roff_elem_alloc(mdoc, line, ppos, ROFF_br);
1028 rew_elem(mdoc, ROFF_br);
1029 return;
1030 }
1031 }
1032
1033 /*
1034 * This routine accommodates implicitly- and explicitly-scoped
1035 * macro openings. Implicit ones first close out prior scope
1036 * (seen above). Delay opening the head until necessary to
1037 * allow leading punctuation to print. Special consideration
1038 * for `It -column', which has phrase-part syntax instead of
1039 * regular child nodes.
1040 */
1041
1042 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1043 blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg);
1044 head = body = NULL;
1045
1046 /*
1047 * Exception: Heads of `It' macros in `-diag' lists are not
1048 * parsed, even though `It' macros in general are parsed.
1049 */
1050
1051 parsed = tok != MDOC_It ||
1052 mdoc->last->parent->tok != MDOC_Bl ||
1053 mdoc->last->parent->norm->Bl.type != LIST_diag;
1054
1055 /*
1056 * The `Nd' macro has all arguments in its body: it's a hybrid
1057 * of block partial-explicit and full-implicit. Stupid.
1058 */
1059
1060 if (tok == MDOC_Nd) {
1061 head = roff_head_alloc(mdoc, line, ppos, tok);
1062 rew_last(mdoc, head);
1063 body = roff_body_alloc(mdoc, line, ppos, tok);
1064 }
1065
1066 if (tok == MDOC_Bk)
1067 mdoc->flags |= MDOC_KEEP;
1068
1069 ac = ARGS_EOLN;
1070 for (;;) {
1071
1072 /*
1073 * If we are right after a tab character,
1074 * do not parse the first word for macros.
1075 */
1076
1077 if (mdoc->flags & MDOC_PHRASEQN) {
1078 mdoc->flags &= ~MDOC_PHRASEQN;
1079 mdoc->flags |= MDOC_PHRASEQF;
1080 }
1081
1082 la = *pos;
1083 lac = ac;
1084 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1085 if (ac == ARGS_EOLN) {
1086 if (lac != ARGS_PHRASE ||
1087 ! (mdoc->flags & MDOC_PHRASEQF))
1088 break;
1089
1090 /*
1091 * This line ends in a tab; start the next
1092 * column now, with a leading blank.
1093 */
1094
1095 if (body != NULL)
1096 rew_last(mdoc, body);
1097 body = roff_body_alloc(mdoc, line, ppos, tok);
1098 roff_word_alloc(mdoc, line, ppos, "\\&");
1099 break;
1100 }
1101
1102 if (tok == MDOC_Bd || tok == MDOC_Bk) {
1103 mandoc_vmsg(MANDOCERR_ARG_EXCESS,
1104 mdoc->parse, line, la, "%s ... %s",
1105 roff_name[tok], buf + la);
1106 break;
1107 }
1108 if (tok == MDOC_Rs) {
1109 mandoc_vmsg(MANDOCERR_ARG_SKIP, mdoc->parse,
1110 line, la, "Rs %s", buf + la);
1111 break;
1112 }
1113 if (ac == ARGS_PUNCT)
1114 break;
1115
1116 /*
1117 * Emit leading punctuation (i.e., punctuation before
1118 * the ROFFT_HEAD) for non-phrase types.
1119 */
1120
1121 if (head == NULL &&
1122 ac != ARGS_PHRASE &&
1123 mdoc_isdelim(p) == DELIM_OPEN) {
1124 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1125 continue;
1126 }
1127
1128 /* Open a head if one hasn't been opened. */
1129
1130 if (head == NULL)
1131 head = roff_head_alloc(mdoc, line, ppos, tok);
1132
1133 if (ac == ARGS_PHRASE) {
1134
1135 /*
1136 * If we haven't opened a body yet, rewind the
1137 * head; if we have, rewind that instead.
1138 */
1139
1140 rew_last(mdoc, body == NULL ? head : body);
1141 body = roff_body_alloc(mdoc, line, ppos, tok);
1142
1143 /* Process to the tab or to the end of the line. */
1144
1145 mdoc->flags |= MDOC_PHRASE;
1146 parse_rest(mdoc, TOKEN_NONE, line, &la, buf);
1147 mdoc->flags &= ~MDOC_PHRASE;
1148
1149 /* There may have been `Ta' macros. */
1150
1151 while (body->next != NULL)
1152 body = body->next;
1153 continue;
1154 }
1155
1156 if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed))
1157 break;
1158 }
1159
1160 if (blk->flags & NODE_VALID)
1161 return;
1162 if (head == NULL)
1163 head = roff_head_alloc(mdoc, line, ppos, tok);
1164 if (nl && tok != MDOC_Bd && tok != MDOC_Bl && tok != MDOC_Rs)
1165 append_delims(mdoc, line, pos, buf);
1166 if (body != NULL)
1167 goto out;
1168 if (find_pending(mdoc, tok, line, ppos, head))
1169 return;
1170
1171 /* Close out scopes to remain in a consistent state. */
1172
1173 rew_last(mdoc, head);
1174 body = roff_body_alloc(mdoc, line, ppos, tok);
1175 out:
1176 if (mdoc->flags & MDOC_FREECOL) {
1177 rew_last(mdoc, body);
1178 rew_last(mdoc, blk);
1179 mdoc->flags &= ~MDOC_FREECOL;
1180 }
1181 }
1182
1183 static void
1184 blk_part_imp(MACRO_PROT_ARGS)
1185 {
1186 int la, nl;
1187 enum margserr ac;
1188 char *p;
1189 struct roff_node *blk; /* saved block context */
1190 struct roff_node *body; /* saved body context */
1191 struct roff_node *n;
1192
1193 nl = MDOC_NEWLINE & mdoc->flags;
1194
1195 /*
1196 * A macro that spans to the end of the line. This is generally
1197 * (but not necessarily) called as the first macro. The block
1198 * has a head as the immediate child, which is always empty,
1199 * followed by zero or more opening punctuation nodes, then the
1200 * body (which may be empty, depending on the macro), then zero
1201 * or more closing punctuation nodes.
1202 */
1203
1204 blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1205 rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1206
1207 /*
1208 * Open the body scope "on-demand", that is, after we've
1209 * processed all our the leading delimiters (open parenthesis,
1210 * etc.).
1211 */
1212
1213 for (body = NULL; ; ) {
1214 la = *pos;
1215 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1216 if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
1217 break;
1218
1219 if (body == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
1220 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1221 continue;
1222 }
1223
1224 if (body == NULL)
1225 body = roff_body_alloc(mdoc, line, ppos, tok);
1226
1227 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1228 break;
1229 }
1230 if (body == NULL)
1231 body = roff_body_alloc(mdoc, line, ppos, tok);
1232
1233 if (find_pending(mdoc, tok, line, ppos, body))
1234 return;
1235
1236 rew_last(mdoc, body);
1237 if (nl)
1238 append_delims(mdoc, line, pos, buf);
1239 rew_pending(mdoc, blk);
1240
1241 /* Move trailing .Ns out of scope. */
1242
1243 for (n = body->child; n && n->next; n = n->next)
1244 /* Do nothing. */ ;
1245 if (n && n->tok == MDOC_Ns)
1246 roff_node_relink(mdoc, n);
1247 }
1248
1249 static void
1250 blk_part_exp(MACRO_PROT_ARGS)
1251 {
1252 int la, nl;
1253 enum margserr ac;
1254 struct roff_node *head; /* keep track of head */
1255 char *p;
1256
1257 nl = MDOC_NEWLINE & mdoc->flags;
1258
1259 /*
1260 * The opening of an explicit macro having zero or more leading
1261 * punctuation nodes; a head with optional single element (the
1262 * case of `Eo'); and a body that may be empty.
1263 */
1264
1265 roff_block_alloc(mdoc, line, ppos, tok);
1266 head = NULL;
1267 for (;;) {
1268 la = *pos;
1269 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1270 if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1271 break;
1272
1273 /* Flush out leading punctuation. */
1274
1275 if (head == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
1276 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1277 continue;
1278 }
1279
1280 if (head == NULL) {
1281 head = roff_head_alloc(mdoc, line, ppos, tok);
1282 if (tok == MDOC_Eo) /* Not parsed. */
1283 dword(mdoc, line, la, p, DELIM_MAX, 0);
1284 rew_last(mdoc, head);
1285 roff_body_alloc(mdoc, line, ppos, tok);
1286 if (tok == MDOC_Eo)
1287 continue;
1288 }
1289
1290 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1291 break;
1292 }
1293
1294 /* Clean-up to leave in a consistent state. */
1295
1296 if (head == NULL) {
1297 rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1298 roff_body_alloc(mdoc, line, ppos, tok);
1299 }
1300 if (nl)
1301 append_delims(mdoc, line, pos, buf);
1302 }
1303
1304 static void
1305 in_line_argn(MACRO_PROT_ARGS)
1306 {
1307 struct mdoc_arg *arg;
1308 char *p;
1309 enum margserr ac;
1310 enum roff_tok ntok;
1311 int state; /* arg#; -1: not yet open; -2: closed */
1312 int la, maxargs, nl;
1313
1314 nl = mdoc->flags & MDOC_NEWLINE;
1315
1316 /*
1317 * A line macro that has a fixed number of arguments (maxargs).
1318 * Only open the scope once the first non-leading-punctuation is
1319 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1320 * keep it open until the maximum number of arguments are
1321 * exhausted.
1322 */
1323
1324 switch (tok) {
1325 case MDOC_Ap:
1326 case MDOC_Ns:
1327 case MDOC_Ux:
1328 maxargs = 0;
1329 break;
1330 case MDOC_Bx:
1331 case MDOC_Es:
1332 case MDOC_Xr:
1333 maxargs = 2;
1334 break;
1335 default:
1336 maxargs = 1;
1337 break;
1338 }
1339
1340 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1341
1342 state = -1;
1343 p = NULL;
1344 for (;;) {
1345 la = *pos;
1346 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1347
1348 if (ac == ARGS_WORD && state == -1 &&
1349 (mdoc_macro(tok)->flags & MDOC_IGNDELIM) == 0 &&
1350 mdoc_isdelim(p) == DELIM_OPEN) {
1351 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1352 continue;
1353 }
1354
1355 if (state == -1 && tok != MDOC_In &&
1356 tok != MDOC_St && tok != MDOC_Xr) {
1357 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1358 state = 0;
1359 }
1360
1361 if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
1362 if (abs(state) < 2 && tok == MDOC_Pf)
1363 mandoc_vmsg(MANDOCERR_PF_SKIP,
1364 mdoc->parse, line, ppos, "Pf %s",
1365 p == NULL ? "at eol" : p);
1366 break;
1367 }
1368
1369 if (state == maxargs) {
1370 rew_elem(mdoc, tok);
1371 state = -2;
1372 }
1373
1374 ntok = (tok == MDOC_Pf && state == 0) ?
1375 TOKEN_NONE : lookup(mdoc, tok, line, la, p);
1376
1377 if (ntok != TOKEN_NONE) {
1378 if (state >= 0) {
1379 rew_elem(mdoc, tok);
1380 state = -2;
1381 }
1382 (*mdoc_macro(ntok)->fp)(mdoc, ntok,
1383 line, la, pos, buf);
1384 break;
1385 }
1386
1387 if (mdoc_macro(tok)->flags & MDOC_IGNDELIM ||
1388 mdoc_isdelim(p) == DELIM_NONE) {
1389 if (state == -1) {
1390 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1391 state = 1;
1392 } else if (state >= 0)
1393 state++;
1394 } else if (state >= 0) {
1395 rew_elem(mdoc, tok);
1396 state = -2;
1397 }
1398
1399 dword(mdoc, line, la, p, DELIM_MAX,
1400 mdoc_macro(tok)->flags & MDOC_JOIN);
1401 }
1402
1403 if (state == -1) {
1404 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1405 line, ppos, roff_name[tok]);
1406 return;
1407 }
1408
1409 if (state == 0 && tok == MDOC_Pf)
1410 append_delims(mdoc, line, pos, buf);
1411 if (state >= 0)
1412 rew_elem(mdoc, tok);
1413 if (nl)
1414 append_delims(mdoc, line, pos, buf);
1415 }
1416
1417 static void
1418 in_line_eoln(MACRO_PROT_ARGS)
1419 {
1420 struct roff_node *n;
1421 struct mdoc_arg *arg;
1422
1423 if ((tok == MDOC_Pp || tok == MDOC_Lp) &&
1424 ! (mdoc->flags & MDOC_SYNOPSIS)) {
1425 n = mdoc->last;
1426 if (mdoc->next == ROFF_NEXT_SIBLING)
1427 n = n->parent;
1428 if (n->tok == MDOC_Nm)
1429 rew_last(mdoc, n->parent);
1430 }
1431
1432 if (buf[*pos] == '\0' &&
1433 (tok == MDOC_Fd || *roff_name[tok] == '%')) {
1434 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1435 line, ppos, roff_name[tok]);
1436 return;
1437 }
1438
1439 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1440 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1441 if (parse_rest(mdoc, tok, line, pos, buf))
1442 return;
1443 rew_elem(mdoc, tok);
1444 }
1445
1446 /*
1447 * The simplest argument parser available: Parse the remaining
1448 * words until the end of the phrase or line and return 0
1449 * or until the next macro, call that macro, and return 1.
1450 */
1451 static int
1452 parse_rest(struct roff_man *mdoc, enum roff_tok tok,
1453 int line, int *pos, char *buf)
1454 {
1455 int la;
1456
1457 for (;;) {
1458 la = *pos;
1459 if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1460 return 0;
1461 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1462 return 1;
1463 }
1464 }
1465
1466 static void
1467 ctx_synopsis(MACRO_PROT_ARGS)
1468 {
1469
1470 if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
1471 in_line(mdoc, tok, line, ppos, pos, buf);
1472 else if (tok == MDOC_Nm)
1473 blk_full(mdoc, tok, line, ppos, pos, buf);
1474 else {
1475 assert(tok == MDOC_Vt);
1476 blk_part_imp(mdoc, tok, line, ppos, pos, buf);
1477 }
1478 }
1479
1480 /*
1481 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1482 * They're unusual because they're basically free-form text until a
1483 * macro is encountered.
1484 */
1485 static void
1486 phrase_ta(MACRO_PROT_ARGS)
1487 {
1488 struct roff_node *body, *n;
1489
1490 /* Make sure we are in a column list or ignore this macro. */
1491
1492 body = NULL;
1493 for (n = mdoc->last; n != NULL; n = n->parent) {
1494 if (n->flags & NODE_ENDED)
1495 continue;
1496 if (n->tok == MDOC_It && n->type == ROFFT_BODY)
1497 body = n;
1498 if (n->tok == MDOC_Bl && n->end == ENDBODY_NOT)
1499 break;
1500 }
1501
1502 if (n == NULL || n->norm->Bl.type != LIST_column) {
1503 mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1504 line, ppos, "Ta");
1505 return;
1506 }
1507
1508 /* Advance to the next column. */
1509
1510 rew_last(mdoc, body);
1511 roff_body_alloc(mdoc, line, ppos, MDOC_It);
1512 parse_rest(mdoc, TOKEN_NONE, line, pos, buf);
1513 }