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