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