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