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