Import mdocml-1.13.1
[dragonfly.git] / contrib / mdocml / mdoc_macro.c
CommitLineData
070c62a6 1/* $Id: mdoc_macro.c,v 1.139 2014/08/01 17:27:44 schwarze Exp $ */
80387638 2/*
f88b6c16 3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
7888c61d 4 * Copyright (c) 2010, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
80387638
SW
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#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
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
60e1e752 29#include "mdoc.h"
80387638
SW
30#include "mandoc.h"
31#include "libmdoc.h"
32#include "libmandoc.h"
33
34enum rew { /* see rew_dohalt() */
35 REWIND_NONE,
36 REWIND_THIS,
37 REWIND_MORE,
38 REWIND_FORCE,
39 REWIND_LATER,
40 REWIND_ERROR
41};
42
070c62a6
FF
43static int blk_full(MACRO_PROT_ARGS);
44static int blk_exp_close(MACRO_PROT_ARGS);
45static int blk_part_exp(MACRO_PROT_ARGS);
46static int blk_part_imp(MACRO_PROT_ARGS);
47static int ctx_synopsis(MACRO_PROT_ARGS);
48static int in_line_eoln(MACRO_PROT_ARGS);
49static int in_line_argn(MACRO_PROT_ARGS);
50static int in_line(MACRO_PROT_ARGS);
51static int phrase_ta(MACRO_PROT_ARGS);
80387638 52
7888c61d
FF
53static int dword(struct mdoc *, int, int, const char *,
54 enum mdelim, int);
070c62a6 55static int append_delims(struct mdoc *,
80387638
SW
56 int, int *, char *);
57static enum mdoct lookup(enum mdoct, const char *);
58static enum mdoct lookup_raw(const char *);
59static int make_pending(struct mdoc_node *, enum mdoct,
60 struct mdoc *, int, int);
070c62a6
FF
61static int phrase(struct mdoc *, int, int, char *);
62static enum mdoct rew_alt(enum mdoct);
63static enum rew rew_dohalt(enum mdoct, enum mdoc_type,
80387638 64 const struct mdoc_node *);
070c62a6
FF
65static int rew_elem(struct mdoc *, enum mdoct);
66static int rew_last(struct mdoc *,
80387638 67 const struct mdoc_node *);
070c62a6 68static int rew_sub(enum mdoc_type, struct mdoc *,
80387638
SW
69 enum mdoct, int, int);
70
71const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
7888c61d 72 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ap */
80387638
SW
73 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
74 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
75 { in_line_eoln, MDOC_PROLOGUE }, /* Os */
7888c61d
FF
76 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */
77 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */
78 { in_line_eoln, 0 }, /* Pp */
79 { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */
80 { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */
80387638 81 { blk_full, MDOC_EXPLICIT }, /* Bd */
7888c61d 82 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */
80387638 83 { blk_full, MDOC_EXPLICIT }, /* Bl */
7888c61d
FF
84 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */
85 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */
86 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
87 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */
80387638 88 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
070c62a6 89 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
80387638 90 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
7888c61d
FF
91 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
92 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
93 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
80387638 94 { in_line_eoln, 0 }, /* Ex */
7888c61d
FF
95 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
96 { in_line_eoln, 0 }, /* Fd */
80387638 97 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
7888c61d
FF
98 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
99 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
070c62a6 100 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
80387638 101 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
7888c61d
FF
102 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
103 { blk_full, MDOC_JOIN }, /* Nd */
104 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
80387638 105 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
070c62a6 106 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */
80387638
SW
107 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
108 { in_line_eoln, 0 }, /* Rv */
7888c61d 109 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
80387638 110 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
7888c61d 111 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
80387638 112 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
7888c61d
FF
113 { in_line_eoln, MDOC_JOIN }, /* %A */
114 { in_line_eoln, MDOC_JOIN }, /* %B */
115 { in_line_eoln, MDOC_JOIN }, /* %D */
116 { in_line_eoln, MDOC_JOIN }, /* %I */
117 { in_line_eoln, MDOC_JOIN }, /* %J */
80387638 118 { in_line_eoln, 0 }, /* %N */
7888c61d 119 { in_line_eoln, MDOC_JOIN }, /* %O */
80387638 120 { in_line_eoln, 0 }, /* %P */
7888c61d
FF
121 { in_line_eoln, MDOC_JOIN }, /* %R */
122 { in_line_eoln, MDOC_JOIN }, /* %T */
80387638 123 { in_line_eoln, 0 }, /* %V */
7888c61d
FF
124 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
125 MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
126 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
127 MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
128 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
80387638 129 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
7888c61d
FF
130 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
131 MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
132 { blk_full, MDOC_EXPLICIT }, /* Bf */
133 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
134 MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
135 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
80387638
SW
136 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
137 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
138 { in_line_eoln, 0 }, /* Db */
7888c61d
FF
139 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
140 MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
141 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
142 MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
143 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
144 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
145 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
146 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
80387638
SW
147 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
148 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
149 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
7888c61d
FF
150 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
151 MDOC_IGNDELIM | MDOC_JOIN }, /* No */
152 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
153 MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
80387638
SW
154 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
155 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
7888c61d
FF
156 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
157 MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
80387638 158 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
7888c61d
FF
159 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
160 MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
161 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
162 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
163 MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
164 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
165 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
166 MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
167 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
168 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
80387638 169 { blk_full, MDOC_EXPLICIT }, /* Rs */
7888c61d
FF
170 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
171 MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
172 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
173 MDOC_EXPLICIT | MDOC_JOIN }, /* So */
174 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
80387638 175 { in_line_eoln, 0 }, /* Sm */
7888c61d
FF
176 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
177 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
80387638 178 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
7888c61d 179 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
80387638
SW
180 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
181 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
7888c61d
FF
182 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
183 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
184 MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
185 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
186 MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
187 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
188 MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
80387638 189 { blk_full, MDOC_EXPLICIT }, /* Bk */
7888c61d 190 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
80387638
SW
191 { in_line_eoln, 0 }, /* Bt */
192 { in_line_eoln, 0 }, /* Hf */
070c62a6 193 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */
80387638
SW
194 { in_line_eoln, 0 }, /* Ud */
195 { in_line, 0 }, /* Lb */
7888c61d
FF
196 { in_line_eoln, 0 }, /* Lp */
197 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
198 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
199 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
200 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
201 MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
202 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
203 MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
204 { in_line_eoln, MDOC_JOIN }, /* %C */
070c62a6
FF
205 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */
206 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */
80387638 207 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
7888c61d 208 { in_line_eoln, MDOC_JOIN }, /* %Q */
80387638
SW
209 { in_line_eoln, 0 }, /* br */
210 { in_line_eoln, 0 }, /* sp */
211 { in_line_eoln, 0 }, /* %U */
7888c61d 212 { phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
070c62a6 213 { in_line_eoln, 0 }, /* ll */
80387638
SW
214};
215
216const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
217
218
219/*
220 * This is called at the end of parsing. It must traverse up the tree,
221 * closing out open [implicit] scopes. Obviously, open explicit scopes
222 * are errors.
223 */
224int
f88b6c16 225mdoc_macroend(struct mdoc *mdoc)
80387638
SW
226{
227 struct mdoc_node *n;
228
229 /* Scan for open explicit scopes. */
230
f88b6c16 231 n = MDOC_VALID & mdoc->last->flags ?
070c62a6 232 mdoc->last->parent : mdoc->last;
80387638
SW
233
234 for ( ; n; n = n->parent)
235 if (MDOC_BLOCK == n->type &&
236 MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
070c62a6
FF
237 mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse,
238 n->line, n->pos, mdoc_macronames[n->tok]);
80387638
SW
239
240 /* Rewind to the first. */
241
f88b6c16 242 return(rew_last(mdoc, mdoc->first));
80387638
SW
243}
244
80387638
SW
245/*
246 * Look up a macro from within a subsequent context.
247 */
248static enum mdoct
249lookup(enum mdoct from, const char *p)
250{
80387638
SW
251
252 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
253 return(MDOC_MAX);
254 return(lookup_raw(p));
255}
256
80387638
SW
257/*
258 * Lookup a macro following the initial line macro.
259 */
260static enum mdoct
261lookup_raw(const char *p)
262{
263 enum mdoct res;
264
265 if (MDOC_MAX == (res = mdoc_hash_find(p)))
266 return(MDOC_MAX);
267 if (MDOC_CALLABLE & mdoc_macros[res].flags)
268 return(res);
269 return(MDOC_MAX);
270}
271
80387638
SW
272static int
273rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
274{
60e1e752 275 struct mdoc_node *n, *np;
80387638
SW
276
277 assert(to);
278 mdoc->next = MDOC_NEXT_SIBLING;
279
070c62a6 280
80387638 281 while (mdoc->last != to) {
60e1e752
SW
282 /*
283 * Save the parent here, because we may delete the
f88b6c16
FF
284 * mdoc->last node in the post-validation phase and reset
285 * it to mdoc->last->parent, causing a step in the closing
60e1e752
SW
286 * out to be lost.
287 */
288 np = mdoc->last->parent;
80387638
SW
289 if ( ! mdoc_valid_post(mdoc))
290 return(0);
291 n = mdoc->last;
60e1e752 292 mdoc->last = np;
80387638
SW
293 assert(mdoc->last);
294 mdoc->last->last = n;
295 }
296
297 return(mdoc_valid_post(mdoc));
298}
299
80387638
SW
300/*
301 * For a block closing macro, return the corresponding opening one.
302 * Otherwise, return the macro itself.
303 */
304static enum mdoct
305rew_alt(enum mdoct tok)
306{
307 switch (tok) {
070c62a6 308 case MDOC_Ac:
80387638 309 return(MDOC_Ao);
070c62a6 310 case MDOC_Bc:
80387638 311 return(MDOC_Bo);
070c62a6 312 case MDOC_Brc:
80387638 313 return(MDOC_Bro);
070c62a6 314 case MDOC_Dc:
80387638 315 return(MDOC_Do);
070c62a6 316 case MDOC_Ec:
80387638 317 return(MDOC_Eo);
070c62a6 318 case MDOC_Ed:
80387638 319 return(MDOC_Bd);
070c62a6 320 case MDOC_Ef:
80387638 321 return(MDOC_Bf);
070c62a6 322 case MDOC_Ek:
80387638 323 return(MDOC_Bk);
070c62a6 324 case MDOC_El:
80387638 325 return(MDOC_Bl);
070c62a6 326 case MDOC_Fc:
80387638 327 return(MDOC_Fo);
070c62a6 328 case MDOC_Oc:
80387638 329 return(MDOC_Oo);
070c62a6 330 case MDOC_Pc:
80387638 331 return(MDOC_Po);
070c62a6 332 case MDOC_Qc:
80387638 333 return(MDOC_Qo);
070c62a6 334 case MDOC_Re:
80387638 335 return(MDOC_Rs);
070c62a6 336 case MDOC_Sc:
80387638 337 return(MDOC_So);
070c62a6 338 case MDOC_Xc:
80387638
SW
339 return(MDOC_Xo);
340 default:
341 return(tok);
342 }
343 /* NOTREACHED */
344}
345
80387638
SW
346/*
347 * Rewinding to tok, how do we have to handle *p?
348 * REWIND_NONE: *p would delimit tok, but no tok scope is open
349 * inside *p, so there is no need to rewind anything at all.
350 * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
351 * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
352 * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
353 * REWIND_LATER: *p is explicit and still open, postpone rewinding.
354 * REWIND_ERROR: No tok block is open at all.
355 */
356static enum rew
070c62a6 357rew_dohalt(enum mdoct tok, enum mdoc_type type,
80387638
SW
358 const struct mdoc_node *p)
359{
360
361 /*
362 * No matching token, no delimiting block, no broken block.
363 * This can happen when full implicit macros are called for
364 * the first time but try to rewind their previous
365 * instance anyway.
366 */
367 if (MDOC_ROOT == p->type)
368 return(MDOC_BLOCK == type &&
369 MDOC_EXPLICIT & mdoc_macros[tok].flags ?
370 REWIND_ERROR : REWIND_NONE);
371
372 /*
070c62a6 373 * When starting to rewind, skip plain text
80387638
SW
374 * and nodes that have already been rewound.
375 */
376 if (MDOC_TEXT == p->type || MDOC_VALID & p->flags)
377 return(REWIND_MORE);
378
379 /*
380 * The easiest case: Found a matching token.
381 * This applies to both blocks and elements.
382 */
383 tok = rew_alt(tok);
384 if (tok == p->tok)
385 return(p->end ? REWIND_NONE :
386 type == p->type ? REWIND_THIS : REWIND_MORE);
387
388 /*
389 * While elements do require rewinding for themselves,
390 * they never affect rewinding of other nodes.
391 */
392 if (MDOC_ELEM == p->type)
393 return(REWIND_MORE);
394
395 /*
396 * Blocks delimited by our target token get REWIND_MORE.
070c62a6 397 * Blocks delimiting our target token get REWIND_NONE.
80387638
SW
398 */
399 switch (tok) {
070c62a6 400 case MDOC_Bl:
80387638
SW
401 if (MDOC_It == p->tok)
402 return(REWIND_MORE);
403 break;
070c62a6 404 case MDOC_It:
80387638
SW
405 if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
406 return(REWIND_NONE);
407 break;
408 /*
409 * XXX Badly nested block handling still fails badly
410 * when one block is breaking two blocks of the same type.
411 * This is an incomplete and extremely ugly workaround,
412 * required to let the OpenBSD tree build.
413 */
070c62a6 414 case MDOC_Oo:
80387638
SW
415 if (MDOC_Op == p->tok)
416 return(REWIND_MORE);
417 break;
070c62a6 418 case MDOC_Nm:
80387638 419 return(REWIND_NONE);
070c62a6 420 case MDOC_Nd:
80387638 421 /* FALLTHROUGH */
070c62a6 422 case MDOC_Ss:
80387638
SW
423 if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
424 return(REWIND_NONE);
425 /* FALLTHROUGH */
070c62a6
FF
426 case MDOC_Sh:
427 if (MDOC_ROOT == p->parent->type)
428 return(REWIND_THIS);
80387638
SW
429 if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
430 MDOC_Sh == p->tok)
431 return(REWIND_MORE);
432 break;
433 default:
434 break;
435 }
436
437 /*
438 * Default block rewinding rules.
439 * In particular, always skip block end markers,
440 * and let all blocks rewind Nm children.
441 */
442 if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
443 (MDOC_BLOCK == p->type &&
444 ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
445 return(REWIND_MORE);
446
447 /*
448 * By default, closing out full blocks
449 * forces closing of broken explicit blocks,
450 * while closing out partial blocks
451 * allows delayed rewinding by default.
452 */
453 return (&blk_full == mdoc_macros[tok].fp ?
454 REWIND_FORCE : REWIND_LATER);
455}
456
80387638
SW
457static int
458rew_elem(struct mdoc *mdoc, enum mdoct tok)
459{
460 struct mdoc_node *n;
461
462 n = mdoc->last;
463 if (MDOC_ELEM != n->type)
464 n = n->parent;
465 assert(MDOC_ELEM == n->type);
466 assert(tok == n->tok);
467
468 return(rew_last(mdoc, n));
469}
470
80387638
SW
471/*
472 * We are trying to close a block identified by tok,
473 * but the child block *broken is still open.
474 * Thus, postpone closing the tok block
475 * until the rew_sub call closing *broken.
476 */
477static int
478make_pending(struct mdoc_node *broken, enum mdoct tok,
f88b6c16 479 struct mdoc *mdoc, int line, int ppos)
80387638
SW
480{
481 struct mdoc_node *breaker;
482
483 /*
484 * Iterate backwards, searching for the block matching tok,
485 * that is, the block breaking the *broken block.
486 */
487 for (breaker = broken->parent; breaker; breaker = breaker->parent) {
488
489 /*
490 * If the *broken block had already been broken before
491 * and we encounter its breaker, make the tok block
492 * pending on the inner breaker.
493 * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
494 * becomes "[A broken=[B [C->B B] tok=A] C]"
495 * and finally "[A [B->A [C->B B] A] C]".
496 */
497 if (breaker == broken->pending) {
498 broken = breaker;
499 continue;
500 }
501
502 if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
503 continue;
504 if (MDOC_BODY == broken->type)
505 broken = broken->parent;
506
507 /*
508 * Found the breaker.
509 * If another, outer breaker is already pending on
510 * the *broken block, we must not clobber the link
511 * to the outer breaker, but make it pending on the
512 * new, now inner breaker.
513 * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
514 * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
515 * and finally "[A [B->A [C->B A] B] C]".
516 */
517 if (broken->pending) {
518 struct mdoc_node *taker;
519
520 /*
521 * If the breaker had also been broken before,
522 * it cannot take on the outer breaker itself,
523 * but must hand it on to its own breakers.
524 * Graphically, this is the following situation:
525 * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
526 * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
527 */
528 taker = breaker;
529 while (taker->pending)
530 taker = taker->pending;
531 taker->pending = broken->pending;
532 }
533 broken->pending = breaker;
070c62a6
FF
534 mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse, line, ppos,
535 "%s breaks %s", mdoc_macronames[tok],
536 mdoc_macronames[broken->tok]);
80387638
SW
537 return(1);
538 }
539
540 /*
541 * Found no matching block for tok.
542 * Are you trying to close a block that is not open?
543 */
544 return(0);
545}
546
80387638 547static int
070c62a6 548rew_sub(enum mdoc_type t, struct mdoc *mdoc,
80387638
SW
549 enum mdoct tok, int line, int ppos)
550{
551 struct mdoc_node *n;
552
f88b6c16 553 n = mdoc->last;
80387638
SW
554 while (n) {
555 switch (rew_dohalt(tok, t, n)) {
070c62a6 556 case REWIND_NONE:
80387638 557 return(1);
070c62a6 558 case REWIND_THIS:
7888c61d
FF
559 n->lastline = line -
560 (MDOC_NEWLINE & mdoc->flags &&
561 ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
80387638 562 break;
070c62a6
FF
563 case REWIND_FORCE:
564 mandoc_vmsg(MANDOCERR_BLK_BROKEN, mdoc->parse,
565 line, ppos, "%s breaks %s",
566 mdoc_macronames[tok],
567 mdoc_macronames[n->tok]);
80387638 568 /* FALLTHROUGH */
070c62a6 569 case REWIND_MORE:
7888c61d
FF
570 n->lastline = line -
571 (MDOC_NEWLINE & mdoc->flags ? 1 : 0);
80387638
SW
572 n = n->parent;
573 continue;
070c62a6 574 case REWIND_LATER:
f88b6c16 575 if (make_pending(n, tok, mdoc, line, ppos) ||
80387638
SW
576 MDOC_BLOCK != t)
577 return(1);
578 /* FALLTHROUGH */
070c62a6
FF
579 case REWIND_ERROR:
580 mandoc_msg(MANDOCERR_BLK_NOTOPEN,
581 mdoc->parse, line, ppos,
582 mdoc_macronames[tok]);
80387638
SW
583 return(1);
584 }
585 break;
586 }
587
588 assert(n);
f88b6c16 589 if ( ! rew_last(mdoc, n))
80387638
SW
590 return(0);
591
592 /*
593 * The current block extends an enclosing block.
594 * Now that the current block ends, close the enclosing block, too.
595 */
596 while (NULL != (n = n->pending)) {
f88b6c16 597 if ( ! rew_last(mdoc, n))
80387638
SW
598 return(0);
599 if (MDOC_HEAD == n->type &&
f88b6c16 600 ! mdoc_body_alloc(mdoc, n->line, n->pos, n->tok))
80387638
SW
601 return(0);
602 }
603
604 return(1);
605}
606
60e1e752
SW
607/*
608 * Allocate a word and check whether it's punctuation or not.
609 * Punctuation consists of those tokens found in mdoc_isdelim().
610 */
611static int
7888c61d
FF
612dword(struct mdoc *mdoc, int line, int col, const char *p,
613 enum mdelim d, int may_append)
60e1e752 614{
070c62a6 615
60e1e752
SW
616 if (DELIM_MAX == d)
617 d = mdoc_isdelim(p);
618
7888c61d
FF
619 if (may_append &&
620 ! ((MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF) & mdoc->flags) &&
621 DELIM_NONE == d && MDOC_TEXT == mdoc->last->type &&
622 DELIM_NONE == mdoc_isdelim(mdoc->last->string)) {
623 mdoc_word_append(mdoc, p);
624 return(1);
625 }
626
f88b6c16 627 if ( ! mdoc_word_alloc(mdoc, line, col, p))
60e1e752
SW
628 return(0);
629
630 if (DELIM_OPEN == d)
f88b6c16 631 mdoc->last->flags |= MDOC_DELIMO;
a4c7eb57
SW
632
633 /*
634 * Closing delimiters only suppress the preceding space
635 * when they follow something, not when they start a new
636 * block or element, and not when they follow `No'.
637 *
638 * XXX Explicitly special-casing MDOC_No here feels
639 * like a layering violation. Find a better way
640 * and solve this in the code related to `No'!
641 */
642
f88b6c16 643 else if (DELIM_CLOSE == d && mdoc->last->prev &&
070c62a6
FF
644 mdoc->last->prev->tok != MDOC_No &&
645 mdoc->last->parent->tok != MDOC_Fd)
f88b6c16 646 mdoc->last->flags |= MDOC_DELIMC;
60e1e752
SW
647
648 return(1);
649}
80387638
SW
650
651static int
f88b6c16 652append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
80387638
SW
653{
654 int la;
655 enum margserr ac;
656 char *p;
657
658 if ('\0' == buf[*pos])
659 return(1);
660
661 for (;;) {
662 la = *pos;
f88b6c16 663 ac = mdoc_zargs(mdoc, line, pos, buf, &p);
80387638
SW
664
665 if (ARGS_ERROR == ac)
666 return(0);
667 else if (ARGS_EOLN == ac)
668 break;
669
7888c61d 670 dword(mdoc, line, la, p, DELIM_MAX, 1);
80387638
SW
671
672 /*
673 * If we encounter end-of-sentence symbols, then trigger
674 * the double-space.
675 *
a4c7eb57 676 * XXX: it's easy to allow this to propagate outward to
80387638
SW
677 * the last symbol, such that `. )' will cause the
678 * correct double-spacing. However, (1) groff isn't
679 * smart enough to do this and (2) it would require
680 * knowing which symbols break this behaviour, for
a4c7eb57 681 * example, `. ;' shouldn't propagate the double-space.
80387638 682 */
070c62a6 683 if (mandoc_eos(p, strlen(p)))
f88b6c16 684 mdoc->last->flags |= MDOC_EOS;
80387638
SW
685 }
686
687 return(1);
688}
689
80387638 690/*
070c62a6 691 * Close out block partial/full explicit.
80387638
SW
692 */
693static int
694blk_exp_close(MACRO_PROT_ARGS)
695{
696 struct mdoc_node *body; /* Our own body. */
697 struct mdoc_node *later; /* A sub-block starting later. */
698 struct mdoc_node *n; /* For searching backwards. */
699
070c62a6 700 int j, lastarg, maxargs, flushed, nl;
80387638
SW
701 enum margserr ac;
702 enum mdoct atok, ntok;
703 char *p;
704
f88b6c16 705 nl = MDOC_NEWLINE & mdoc->flags;
80387638
SW
706
707 switch (tok) {
070c62a6 708 case MDOC_Ec:
80387638
SW
709 maxargs = 1;
710 break;
070c62a6 711 case MDOC_Ek:
7888c61d 712 mdoc->flags &= ~MDOC_KEEP;
80387638
SW
713 default:
714 maxargs = 0;
715 break;
716 }
717
718 /*
719 * Search backwards for beginnings of blocks,
720 * both of our own and of pending sub-blocks.
721 */
722 atok = rew_alt(tok);
723 body = later = NULL;
f88b6c16 724 for (n = mdoc->last; n; n = n->parent) {
80387638
SW
725 if (MDOC_VALID & n->flags)
726 continue;
727
728 /* Remember the start of our own body. */
729 if (MDOC_BODY == n->type && atok == n->tok) {
730 if (ENDBODY_NOT == n->end)
731 body = n;
732 continue;
733 }
734
735 if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
736 continue;
737 if (atok == n->tok) {
738 assert(body);
739
740 /*
741 * Found the start of our own block.
742 * When there is no pending sub block,
743 * just proceed to closing out.
744 */
745 if (NULL == later)
746 break;
747
070c62a6 748 /*
80387638
SW
749 * When there is a pending sub block,
750 * postpone closing out the current block
751 * until the rew_sub() closing out the sub-block.
752 */
f88b6c16 753 make_pending(later, tok, mdoc, line, ppos);
80387638
SW
754
755 /*
756 * Mark the place where the formatting - but not
757 * the scope - of the current block ends.
758 */
f88b6c16 759 if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
80387638
SW
760 atok, body, ENDBODY_SPACE))
761 return(0);
762 break;
763 }
764
765 /*
766 * When finding an open sub block, remember the last
767 * open explicit block, or, in case there are only
768 * implicit ones, the first open implicit block.
769 */
770 if (later &&
771 MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
772 continue;
f88b6c16 773 if (MDOC_It != n->tok)
80387638
SW
774 later = n;
775 }
776
070c62a6
FF
777 if ( ! (MDOC_PARSED & mdoc_macros[tok].flags)) {
778 if ('\0' != buf[*pos])
779 mandoc_vmsg(MANDOCERR_ARG_SKIP,
780 mdoc->parse, line, ppos,
781 "%s %s", mdoc_macronames[tok],
782 buf + *pos);
f88b6c16 783 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
80387638 784 return(0);
f88b6c16 785 return(rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos));
80387638
SW
786 }
787
f88b6c16 788 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
80387638
SW
789 return(0);
790
070c62a6 791 if (NULL == later && maxargs > 0)
f88b6c16 792 if ( ! mdoc_tail_alloc(mdoc, line, ppos, rew_alt(tok)))
80387638
SW
793 return(0);
794
795 for (flushed = j = 0; ; j++) {
796 lastarg = *pos;
797
798 if (j == maxargs && ! flushed) {
f88b6c16 799 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
80387638
SW
800 return(0);
801 flushed = 1;
802 }
803
f88b6c16 804 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
805
806 if (ARGS_ERROR == ac)
807 return(0);
808 if (ARGS_PUNCT == ac)
809 break;
810 if (ARGS_EOLN == ac)
811 break;
812
813 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
814
815 if (MDOC_MAX == ntok) {
7888c61d
FF
816 if ( ! dword(mdoc, line, lastarg, p, DELIM_MAX,
817 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
818 return(0);
819 continue;
820 }
821
822 if ( ! flushed) {
f88b6c16 823 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
80387638
SW
824 return(0);
825 flushed = 1;
826 }
f88b6c16
FF
827
828 mdoc->flags &= ~MDOC_NEWLINE;
829
830 if ( ! mdoc_macro(mdoc, ntok, line, lastarg, pos, buf))
80387638
SW
831 return(0);
832 break;
833 }
834
f88b6c16 835 if ( ! flushed && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
80387638
SW
836 return(0);
837
838 if ( ! nl)
839 return(1);
f88b6c16 840 return(append_delims(mdoc, line, pos, buf));
80387638
SW
841}
842
80387638
SW
843static int
844in_line(MACRO_PROT_ARGS)
845{
846 int la, scope, cnt, nc, nl;
847 enum margverr av;
848 enum mdoct ntok;
849 enum margserr ac;
850 enum mdelim d;
851 struct mdoc_arg *arg;
852 char *p;
853
f88b6c16 854 nl = MDOC_NEWLINE & mdoc->flags;
80387638
SW
855
856 /*
857 * Whether we allow ignored elements (those without content,
858 * usually because of reserved words) to squeak by.
859 */
860
861 switch (tok) {
070c62a6 862 case MDOC_An:
80387638 863 /* FALLTHROUGH */
070c62a6 864 case MDOC_Ar:
80387638 865 /* FALLTHROUGH */
070c62a6 866 case MDOC_Fl:
80387638 867 /* FALLTHROUGH */
070c62a6 868 case MDOC_Mt:
80387638 869 /* FALLTHROUGH */
070c62a6 870 case MDOC_Nm:
80387638 871 /* FALLTHROUGH */
070c62a6 872 case MDOC_Pa:
80387638
SW
873 nc = 1;
874 break;
875 default:
876 nc = 0;
877 break;
878 }
879
880 for (arg = NULL;; ) {
881 la = *pos;
f88b6c16 882 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
80387638
SW
883
884 if (ARGV_WORD == av) {
885 *pos = la;
886 break;
070c62a6 887 }
80387638
SW
888 if (ARGV_EOLN == av)
889 break;
890 if (ARGV_ARG == av)
891 continue;
892
893 mdoc_argv_free(arg);
894 return(0);
895 }
896
897 for (cnt = scope = 0;; ) {
898 la = *pos;
f88b6c16 899 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
900
901 if (ARGS_ERROR == ac)
902 return(0);
903 if (ARGS_EOLN == ac)
904 break;
905 if (ARGS_PUNCT == ac)
906 break;
907
908 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
909
070c62a6 910 /*
80387638
SW
911 * In this case, we've located a submacro and must
912 * execute it. Close out scope, if open. If no
913 * elements have been generated, either create one (nc)
914 * or raise a warning.
915 */
916
917 if (MDOC_MAX != ntok) {
f88b6c16 918 if (scope && ! rew_elem(mdoc, tok))
80387638
SW
919 return(0);
920 if (nc && 0 == cnt) {
070c62a6
FF
921 if ( ! mdoc_elem_alloc(mdoc,
922 line, ppos, tok, arg))
80387638 923 return(0);
f88b6c16 924 if ( ! rew_last(mdoc, mdoc->last))
80387638
SW
925 return(0);
926 } else if ( ! nc && 0 == cnt) {
927 mdoc_argv_free(arg);
070c62a6
FF
928 mandoc_msg(MANDOCERR_MACRO_EMPTY,
929 mdoc->parse, line, ppos,
930 mdoc_macronames[tok]);
80387638 931 }
60e1e752 932
f88b6c16 933 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
80387638
SW
934 return(0);
935 if ( ! nl)
936 return(1);
f88b6c16 937 return(append_delims(mdoc, line, pos, buf));
070c62a6 938 }
80387638 939
070c62a6 940 /*
80387638
SW
941 * Non-quote-enclosed punctuation. Set up our scope, if
942 * a word; rewind the scope, if a delimiter; then append
070c62a6 943 * the word.
80387638
SW
944 */
945
946 d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
947
948 if (DELIM_NONE != d) {
949 /*
950 * If we encounter closing punctuation, no word
951 * has been omitted, no scope is open, and we're
952 * allowed to have an empty element, then start
953 * a new scope. `Ar', `Fl', and `Li', only do
954 * this once per invocation. There may be more
955 * of these (all of them?).
956 */
070c62a6
FF
957 if (0 == cnt && (nc || MDOC_Li == tok) &&
958 DELIM_CLOSE == d && ! scope) {
959 if ( ! mdoc_elem_alloc(mdoc,
960 line, ppos, tok, arg))
80387638 961 return(0);
070c62a6
FF
962 if (MDOC_Ar == tok || MDOC_Li == tok ||
963 MDOC_Fl == tok)
80387638
SW
964 cnt++;
965 scope = 1;
966 }
967 /*
968 * Close out our scope, if one is open, before
969 * any punctuation.
970 */
f88b6c16 971 if (scope && ! rew_elem(mdoc, tok))
80387638
SW
972 return(0);
973 scope = 0;
974 } else if ( ! scope) {
f88b6c16 975 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
80387638
SW
976 return(0);
977 scope = 1;
978 }
979
980 if (DELIM_NONE == d)
981 cnt++;
60e1e752 982
7888c61d
FF
983 if ( ! dword(mdoc, line, la, p, d,
984 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
985 return(0);
986
987 /*
988 * `Fl' macros have their scope re-opened with each new
989 * word so that the `-' can be added to each one without
990 * having to parse out spaces.
991 */
992 if (scope && MDOC_Fl == tok) {
f88b6c16 993 if ( ! rew_elem(mdoc, tok))
80387638
SW
994 return(0);
995 scope = 0;
996 }
997 }
998
f88b6c16 999 if (scope && ! rew_elem(mdoc, tok))
80387638
SW
1000 return(0);
1001
1002 /*
1003 * If no elements have been collected and we're allowed to have
1004 * empties (nc), open a scope and close it out. Otherwise,
1005 * raise a warning.
1006 */
1007
1008 if (nc && 0 == cnt) {
f88b6c16 1009 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
80387638 1010 return(0);
f88b6c16 1011 if ( ! rew_last(mdoc, mdoc->last))
80387638
SW
1012 return(0);
1013 } else if ( ! nc && 0 == cnt) {
1014 mdoc_argv_free(arg);
070c62a6
FF
1015 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1016 line, ppos, mdoc_macronames[tok]);
80387638
SW
1017 }
1018
1019 if ( ! nl)
1020 return(1);
f88b6c16 1021 return(append_delims(mdoc, line, pos, buf));
80387638
SW
1022}
1023
80387638
SW
1024static int
1025blk_full(MACRO_PROT_ARGS)
1026{
36342e81 1027 int la, nl, nparsed;
80387638
SW
1028 struct mdoc_arg *arg;
1029 struct mdoc_node *head; /* save of head macro */
1030 struct mdoc_node *body; /* save of body macro */
1031 struct mdoc_node *n;
1032 enum mdoc_type mtt;
1033 enum mdoct ntok;
1034 enum margserr ac, lac;
1035 enum margverr av;
1036 char *p;
1037
f88b6c16 1038 nl = MDOC_NEWLINE & mdoc->flags;
80387638 1039
070c62a6
FF
1040 /* Skip items outside lists. */
1041
1042 if (tok == MDOC_It) {
1043 for (n = mdoc->last; n; n = n->parent)
1044 if (n->tok == MDOC_Bl)
1045 break;
1046 if (n == NULL) {
1047 mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
1048 line, ppos, "It %s", buf + *pos);
1049 if ( ! mdoc_elem_alloc(mdoc, line, ppos,
1050 MDOC_br, NULL))
1051 return(0);
1052 return(rew_elem(mdoc, MDOC_br));
1053 }
1054 }
1055
80387638
SW
1056 /* Close out prior implicit scope. */
1057
1058 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
f88b6c16 1059 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
80387638 1060 return(0);
f88b6c16 1061 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
80387638
SW
1062 return(0);
1063 }
1064
1065 /*
a4c7eb57 1066 * This routine accommodates implicitly- and explicitly-scoped
80387638
SW
1067 * macro openings. Implicit ones first close out prior scope
1068 * (seen above). Delay opening the head until necessary to
1069 * allow leading punctuation to print. Special consideration
1070 * for `It -column', which has phrase-part syntax instead of
1071 * regular child nodes.
1072 */
1073
1074 for (arg = NULL;; ) {
1075 la = *pos;
f88b6c16 1076 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
80387638
SW
1077
1078 if (ARGV_WORD == av) {
1079 *pos = la;
1080 break;
070c62a6 1081 }
80387638
SW
1082
1083 if (ARGV_EOLN == av)
1084 break;
1085 if (ARGV_ARG == av)
1086 continue;
1087
1088 mdoc_argv_free(arg);
1089 return(0);
1090 }
1091
f88b6c16 1092 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
80387638
SW
1093 return(0);
1094
1095 head = body = NULL;
1096
36342e81
SW
1097 /*
1098 * Exception: Heads of `It' macros in `-diag' lists are not
1099 * parsed, even though `It' macros in general are parsed.
1100 */
1101 nparsed = MDOC_It == tok &&
070c62a6
FF
1102 MDOC_Bl == mdoc->last->parent->tok &&
1103 LIST_diag == mdoc->last->parent->norm->Bl.type;
36342e81 1104
80387638
SW
1105 /*
1106 * The `Nd' macro has all arguments in its body: it's a hybrid
1107 * of block partial-explicit and full-implicit. Stupid.
1108 */
1109
1110 if (MDOC_Nd == tok) {
f88b6c16 1111 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
80387638 1112 return(0);
f88b6c16
FF
1113 head = mdoc->last;
1114 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
80387638 1115 return(0);
f88b6c16 1116 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1117 return(0);
f88b6c16 1118 body = mdoc->last;
7888c61d
FF
1119 }
1120
1121 if (MDOC_Bk == tok)
1122 mdoc->flags |= MDOC_KEEP;
80387638
SW
1123
1124 ac = ARGS_ERROR;
1125
1126 for ( ; ; ) {
1127 la = *pos;
1128 /* Initialise last-phrase-type with ARGS_PEND. */
1129 lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
f88b6c16 1130 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
1131
1132 if (ARGS_PUNCT == ac)
1133 break;
1134
1135 if (ARGS_ERROR == ac)
1136 return(0);
1137
1138 if (ARGS_EOLN == ac) {
1139 if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1140 break;
1141 /*
1142 * This is necessary: if the last token on a
1143 * line is a `Ta' or tab, then we'll get
1144 * ARGS_EOLN, so we must be smart enough to
1145 * reopen our scope if the last parse was a
1146 * phrase or partial phrase.
1147 */
f88b6c16 1148 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
80387638 1149 return(0);
f88b6c16 1150 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1151 return(0);
f88b6c16 1152 body = mdoc->last;
80387638
SW
1153 break;
1154 }
1155
070c62a6 1156 /*
80387638
SW
1157 * Emit leading punctuation (i.e., punctuation before
1158 * the MDOC_HEAD) for non-phrase types.
1159 */
1160
070c62a6
FF
1161 if (NULL == head &&
1162 ARGS_PEND != ac &&
1163 ARGS_PHRASE != ac &&
1164 ARGS_PPHRASE != ac &&
1165 ARGS_QWORD != ac &&
1166 DELIM_OPEN == mdoc_isdelim(p)) {
7888c61d 1167 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
80387638
SW
1168 return(0);
1169 continue;
1170 }
1171
1172 /* Open a head if one hasn't been opened. */
1173
1174 if (NULL == head) {
f88b6c16 1175 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
80387638 1176 return(0);
f88b6c16 1177 head = mdoc->last;
80387638
SW
1178 }
1179
070c62a6
FF
1180 if (ARGS_PHRASE == ac ||
1181 ARGS_PEND == ac ||
1182 ARGS_PPHRASE == ac) {
80387638
SW
1183 /*
1184 * If we haven't opened a body yet, rewind the
1185 * head; if we have, rewind that instead.
1186 */
1187
1188 mtt = body ? MDOC_BODY : MDOC_HEAD;
f88b6c16 1189 if ( ! rew_sub(mtt, mdoc, tok, line, ppos))
80387638 1190 return(0);
070c62a6 1191
80387638
SW
1192 /* Then allocate our body context. */
1193
f88b6c16 1194 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1195 return(0);
f88b6c16 1196 body = mdoc->last;
80387638
SW
1197
1198 /*
1199 * Process phrases: set whether we're in a
1200 * partial-phrase (this effects line handling)
1201 * then call down into the phrase parser.
1202 */
1203
1204 if (ARGS_PPHRASE == ac)
f88b6c16 1205 mdoc->flags |= MDOC_PPHRASE;
80387638 1206 if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
f88b6c16 1207 mdoc->flags |= MDOC_PPHRASE;
80387638 1208
f88b6c16 1209 if ( ! phrase(mdoc, line, la, buf))
80387638
SW
1210 return(0);
1211
f88b6c16 1212 mdoc->flags &= ~MDOC_PPHRASE;
80387638
SW
1213 continue;
1214 }
1215
070c62a6
FF
1216 ntok = nparsed || ARGS_QWORD == ac ?
1217 MDOC_MAX : lookup(tok, p);
80387638
SW
1218
1219 if (MDOC_MAX == ntok) {
7888c61d
FF
1220 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1221 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
1222 return(0);
1223 continue;
1224 }
1225
f88b6c16 1226 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
80387638
SW
1227 return(0);
1228 break;
1229 }
1230
1231 if (NULL == head) {
f88b6c16 1232 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
80387638 1233 return(0);
f88b6c16 1234 head = mdoc->last;
80387638 1235 }
070c62a6 1236
f88b6c16 1237 if (nl && ! append_delims(mdoc, line, pos, buf))
80387638
SW
1238 return(0);
1239
1240 /* If we've already opened our body, exit now. */
1241
1242 if (NULL != body)
1243 goto out;
1244
1245 /*
1246 * If there is an open (i.e., unvalidated) sub-block requiring
1247 * explicit close-out, postpone switching the current block from
1248 * head to body until the rew_sub() call closing out that
1249 * sub-block.
1250 */
f88b6c16 1251 for (n = mdoc->last; n && n != head; n = n->parent) {
070c62a6
FF
1252 if (MDOC_BLOCK == n->type &&
1253 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1254 ! (MDOC_VALID & n->flags)) {
80387638
SW
1255 n->pending = head;
1256 return(1);
1257 }
1258 }
1259
1260 /* Close out scopes to remain in a consistent state. */
1261
f88b6c16 1262 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
80387638 1263 return(0);
f88b6c16 1264 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638
SW
1265 return(0);
1266
1267out:
f88b6c16 1268 if ( ! (MDOC_FREECOL & mdoc->flags))
80387638
SW
1269 return(1);
1270
f88b6c16 1271 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
80387638 1272 return(0);
f88b6c16 1273 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
80387638
SW
1274 return(0);
1275
f88b6c16 1276 mdoc->flags &= ~MDOC_FREECOL;
80387638
SW
1277 return(1);
1278}
1279
80387638
SW
1280static int
1281blk_part_imp(MACRO_PROT_ARGS)
1282{
1283 int la, nl;
1284 enum mdoct ntok;
1285 enum margserr ac;
1286 char *p;
1287 struct mdoc_node *blk; /* saved block context */
1288 struct mdoc_node *body; /* saved body context */
1289 struct mdoc_node *n;
1290
f88b6c16 1291 nl = MDOC_NEWLINE & mdoc->flags;
80387638
SW
1292
1293 /*
1294 * A macro that spans to the end of the line. This is generally
1295 * (but not necessarily) called as the first macro. The block
1296 * has a head as the immediate child, which is always empty,
1297 * followed by zero or more opening punctuation nodes, then the
1298 * body (which may be empty, depending on the macro), then zero
1299 * or more closing punctuation nodes.
1300 */
1301
f88b6c16 1302 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
80387638
SW
1303 return(0);
1304
f88b6c16 1305 blk = mdoc->last;
80387638 1306
f88b6c16 1307 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
80387638 1308 return(0);
f88b6c16 1309 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
80387638
SW
1310 return(0);
1311
1312 /*
1313 * Open the body scope "on-demand", that is, after we've
1314 * processed all our the leading delimiters (open parenthesis,
1315 * etc.).
1316 */
1317
1318 for (body = NULL; ; ) {
1319 la = *pos;
f88b6c16 1320 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
1321
1322 if (ARGS_ERROR == ac)
1323 return(0);
1324 if (ARGS_EOLN == ac)
1325 break;
1326 if (ARGS_PUNCT == ac)
1327 break;
1328
1329 if (NULL == body && ARGS_QWORD != ac &&
070c62a6 1330 DELIM_OPEN == mdoc_isdelim(p)) {
7888c61d 1331 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
80387638
SW
1332 return(0);
1333 continue;
7888c61d 1334 }
80387638
SW
1335
1336 if (NULL == body) {
f88b6c16 1337 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1338 return(0);
f88b6c16 1339 body = mdoc->last;
80387638
SW
1340 }
1341
1342 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1343
1344 if (MDOC_MAX == ntok) {
7888c61d
FF
1345 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1346 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
1347 return(0);
1348 continue;
1349 }
1350
f88b6c16 1351 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
80387638
SW
1352 return(0);
1353 break;
1354 }
1355
1356 /* Clean-ups to leave in a consistent state. */
1357
1358 if (NULL == body) {
f88b6c16 1359 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1360 return(0);
f88b6c16 1361 body = mdoc->last;
80387638
SW
1362 }
1363
80387638
SW
1364 /*
1365 * If there is an open sub-block requiring explicit close-out,
1366 * postpone closing out the current block
1367 * until the rew_sub() call closing out the sub-block.
1368 */
f88b6c16 1369 for (n = mdoc->last; n && n != body && n != blk->parent;
070c62a6 1370 n = n->parent) {
80387638
SW
1371 if (MDOC_BLOCK == n->type &&
1372 MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1373 ! (MDOC_VALID & n->flags)) {
f88b6c16
FF
1374 make_pending(n, tok, mdoc, line, ppos);
1375 if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
80387638
SW
1376 tok, body, ENDBODY_NOSPACE))
1377 return(0);
1378 return(1);
1379 }
1380 }
070c62a6 1381 assert(n == body);
80387638 1382
070c62a6 1383 if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
80387638
SW
1384 return(0);
1385
1386 /* Standard appending of delimiters. */
1387
f88b6c16 1388 if (nl && ! append_delims(mdoc, line, pos, buf))
80387638
SW
1389 return(0);
1390
1391 /* Rewind scope, if applicable. */
1392
070c62a6 1393 if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
80387638
SW
1394 return(0);
1395
f88b6c16
FF
1396 /* Move trailing .Ns out of scope. */
1397
1398 for (n = body->child; n && n->next; n = n->next)
1399 /* Do nothing. */ ;
1400 if (n && MDOC_Ns == n->tok)
1401 mdoc_node_relink(mdoc, n);
1402
80387638
SW
1403 return(1);
1404}
1405
80387638
SW
1406static int
1407blk_part_exp(MACRO_PROT_ARGS)
1408{
1409 int la, nl;
1410 enum margserr ac;
1411 struct mdoc_node *head; /* keep track of head */
1412 struct mdoc_node *body; /* keep track of body */
1413 char *p;
1414 enum mdoct ntok;
1415
f88b6c16 1416 nl = MDOC_NEWLINE & mdoc->flags;
80387638
SW
1417
1418 /*
1419 * The opening of an explicit macro having zero or more leading
1420 * punctuation nodes; a head with optional single element (the
1421 * case of `Eo'); and a body that may be empty.
1422 */
1423
f88b6c16 1424 if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
070c62a6 1425 return(0);
80387638
SW
1426
1427 for (head = body = NULL; ; ) {
1428 la = *pos;
f88b6c16 1429 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
1430
1431 if (ARGS_ERROR == ac)
1432 return(0);
1433 if (ARGS_PUNCT == ac)
1434 break;
1435 if (ARGS_EOLN == ac)
1436 break;
1437
1438 /* Flush out leading punctuation. */
1439
1440 if (NULL == head && ARGS_QWORD != ac &&
070c62a6 1441 DELIM_OPEN == mdoc_isdelim(p)) {
80387638 1442 assert(NULL == body);
7888c61d 1443 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
80387638
SW
1444 return(0);
1445 continue;
7888c61d 1446 }
80387638
SW
1447
1448 if (NULL == head) {
1449 assert(NULL == body);
f88b6c16 1450 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
80387638 1451 return(0);
f88b6c16 1452 head = mdoc->last;
80387638
SW
1453 }
1454
1455 /*
1456 * `Eo' gobbles any data into the head, but most other
1457 * macros just immediately close out and begin the body.
1458 */
1459
1460 if (NULL == body) {
1461 assert(head);
1462 /* No check whether it's a macro! */
1463 if (MDOC_Eo == tok)
7888c61d 1464 if ( ! dword(mdoc, line, la, p, DELIM_MAX, 0))
80387638
SW
1465 return(0);
1466
f88b6c16 1467 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
80387638 1468 return(0);
f88b6c16 1469 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1470 return(0);
f88b6c16 1471 body = mdoc->last;
80387638
SW
1472
1473 if (MDOC_Eo == tok)
1474 continue;
1475 }
1476
1477 assert(NULL != head && NULL != body);
1478
1479 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1480
1481 if (MDOC_MAX == ntok) {
7888c61d
FF
1482 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1483 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
1484 return(0);
1485 continue;
1486 }
1487
f88b6c16 1488 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
80387638
SW
1489 return(0);
1490 break;
1491 }
1492
1493 /* Clean-up to leave in a consistent state. */
1494
36342e81 1495 if (NULL == head)
f88b6c16 1496 if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
80387638 1497 return(0);
80387638
SW
1498
1499 if (NULL == body) {
f88b6c16 1500 if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
80387638 1501 return(0);
f88b6c16 1502 if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
80387638 1503 return(0);
80387638
SW
1504 }
1505
1506 /* Standard appending of delimiters. */
1507
1508 if ( ! nl)
1509 return(1);
f88b6c16 1510 return(append_delims(mdoc, line, pos, buf));
80387638
SW
1511}
1512
80387638
SW
1513static int
1514in_line_argn(MACRO_PROT_ARGS)
1515{
1516 int la, flushed, j, maxargs, nl;
1517 enum margserr ac;
1518 enum margverr av;
1519 struct mdoc_arg *arg;
1520 char *p;
1521 enum mdoct ntok;
1522
f88b6c16 1523 nl = MDOC_NEWLINE & mdoc->flags;
80387638
SW
1524
1525 /*
1526 * A line macro that has a fixed number of arguments (maxargs).
1527 * Only open the scope once the first non-leading-punctuation is
1528 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1529 * keep it open until the maximum number of arguments are
1530 * exhausted.
1531 */
1532
1533 switch (tok) {
070c62a6 1534 case MDOC_Ap:
80387638 1535 /* FALLTHROUGH */
070c62a6 1536 case MDOC_No:
80387638 1537 /* FALLTHROUGH */
070c62a6 1538 case MDOC_Ns:
80387638 1539 /* FALLTHROUGH */
070c62a6 1540 case MDOC_Ux:
80387638
SW
1541 maxargs = 0;
1542 break;
070c62a6 1543 case MDOC_Bx:
60e1e752 1544 /* FALLTHROUGH */
070c62a6
FF
1545 case MDOC_Es:
1546 /* FALLTHROUGH */
1547 case MDOC_Xr:
80387638
SW
1548 maxargs = 2;
1549 break;
1550 default:
1551 maxargs = 1;
1552 break;
1553 }
1554
1555 for (arg = NULL; ; ) {
1556 la = *pos;
f88b6c16 1557 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
80387638
SW
1558
1559 if (ARGV_WORD == av) {
1560 *pos = la;
1561 break;
070c62a6 1562 }
80387638
SW
1563
1564 if (ARGV_EOLN == av)
1565 break;
1566 if (ARGV_ARG == av)
1567 continue;
1568
1569 mdoc_argv_free(arg);
1570 return(0);
1571 }
1572
1573 for (flushed = j = 0; ; ) {
1574 la = *pos;
f88b6c16 1575 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
1576
1577 if (ARGS_ERROR == ac)
1578 return(0);
1579 if (ARGS_PUNCT == ac)
1580 break;
1581 if (ARGS_EOLN == ac)
1582 break;
1583
070c62a6
FF
1584 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1585 ARGS_QWORD != ac && 0 == j &&
1586 DELIM_OPEN == mdoc_isdelim(p)) {
7888c61d 1587 if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
80387638
SW
1588 return(0);
1589 continue;
1590 } else if (0 == j)
070c62a6 1591 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
80387638
SW
1592 return(0);
1593
1594 if (j == maxargs && ! flushed) {
f88b6c16 1595 if ( ! rew_elem(mdoc, tok))
80387638
SW
1596 return(0);
1597 flushed = 1;
1598 }
1599
1600 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1601
1602 if (MDOC_MAX != ntok) {
f88b6c16 1603 if ( ! flushed && ! rew_elem(mdoc, tok))
80387638
SW
1604 return(0);
1605 flushed = 1;
f88b6c16 1606 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
80387638
SW
1607 return(0);
1608 j++;
1609 break;
1610 }
1611
1612 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
070c62a6
FF
1613 ARGS_QWORD != ac &&
1614 ! flushed &&
1615 DELIM_NONE != mdoc_isdelim(p)) {
f88b6c16 1616 if ( ! rew_elem(mdoc, tok))
80387638
SW
1617 return(0);
1618 flushed = 1;
1619 }
1620
7888c61d
FF
1621 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1622 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
1623 return(0);
1624 j++;
1625 }
1626
070c62a6 1627 if (0 == j && ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
80387638
SW
1628 return(0);
1629
1630 /* Close out in a consistent state. */
1631
f88b6c16 1632 if ( ! flushed && ! rew_elem(mdoc, tok))
80387638
SW
1633 return(0);
1634 if ( ! nl)
1635 return(1);
f88b6c16 1636 return(append_delims(mdoc, line, pos, buf));
80387638
SW
1637}
1638
80387638
SW
1639static int
1640in_line_eoln(MACRO_PROT_ARGS)
1641{
1642 int la;
1643 enum margserr ac;
1644 enum margverr av;
1645 struct mdoc_arg *arg;
1646 char *p;
1647 enum mdoct ntok;
1648
1649 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1650
1651 if (tok == MDOC_Pp)
f88b6c16 1652 rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
80387638
SW
1653
1654 /* Parse macro arguments. */
1655
1656 for (arg = NULL; ; ) {
1657 la = *pos;
f88b6c16 1658 av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
80387638
SW
1659
1660 if (ARGV_WORD == av) {
1661 *pos = la;
1662 break;
1663 }
070c62a6 1664 if (ARGV_EOLN == av)
80387638
SW
1665 break;
1666 if (ARGV_ARG == av)
1667 continue;
1668
1669 mdoc_argv_free(arg);
1670 return(0);
1671 }
1672
1673 /* Open element scope. */
1674
f88b6c16 1675 if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
80387638
SW
1676 return(0);
1677
1678 /* Parse argument terms. */
1679
1680 for (;;) {
1681 la = *pos;
f88b6c16 1682 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
80387638
SW
1683
1684 if (ARGS_ERROR == ac)
1685 return(0);
1686 if (ARGS_EOLN == ac)
1687 break;
1688
1689 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1690
1691 if (MDOC_MAX == ntok) {
7888c61d
FF
1692 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1693 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
1694 return(0);
1695 continue;
1696 }
1697
f88b6c16 1698 if ( ! rew_elem(mdoc, tok))
80387638 1699 return(0);
f88b6c16 1700 return(mdoc_macro(mdoc, ntok, line, la, pos, buf));
80387638
SW
1701 }
1702
1703 /* Close out (no delimiters). */
1704
f88b6c16 1705 return(rew_elem(mdoc, tok));
80387638
SW
1706}
1707
80387638
SW
1708static int
1709ctx_synopsis(MACRO_PROT_ARGS)
1710{
1711 int nl;
1712
f88b6c16 1713 nl = MDOC_NEWLINE & mdoc->flags;
80387638
SW
1714
1715 /* If we're not in the SYNOPSIS, go straight to in-line. */
f88b6c16
FF
1716 if ( ! (MDOC_SYNOPSIS & mdoc->flags))
1717 return(in_line(mdoc, tok, line, ppos, pos, buf));
80387638
SW
1718
1719 /* If we're a nested call, same place. */
1720 if ( ! nl)
f88b6c16 1721 return(in_line(mdoc, tok, line, ppos, pos, buf));
80387638
SW
1722
1723 /*
1724 * XXX: this will open a block scope; however, if later we end
1725 * up formatting the block scope, then child nodes will inherit
1726 * the formatting. Be careful.
1727 */
1728 if (MDOC_Nm == tok)
f88b6c16 1729 return(blk_full(mdoc, tok, line, ppos, pos, buf));
80387638 1730 assert(MDOC_Vt == tok);
f88b6c16 1731 return(blk_part_imp(mdoc, tok, line, ppos, pos, buf));
80387638
SW
1732}
1733
80387638
SW
1734/*
1735 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1736 * They're unusual because they're basically free-form text until a
1737 * macro is encountered.
1738 */
1739static int
f88b6c16 1740phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
80387638
SW
1741{
1742 int la, pos;
1743 enum margserr ac;
1744 enum mdoct ntok;
1745 char *p;
1746
1747 for (pos = ppos; ; ) {
1748 la = pos;
1749
f88b6c16 1750 ac = mdoc_zargs(mdoc, line, &pos, buf, &p);
80387638
SW
1751
1752 if (ARGS_ERROR == ac)
1753 return(0);
1754 if (ARGS_EOLN == ac)
1755 break;
1756
1757 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1758
1759 if (MDOC_MAX == ntok) {
7888c61d 1760 if ( ! dword(mdoc, line, la, p, DELIM_MAX, 1))
80387638
SW
1761 return(0);
1762 continue;
1763 }
1764
f88b6c16 1765 if ( ! mdoc_macro(mdoc, ntok, line, la, &pos, buf))
80387638 1766 return(0);
f88b6c16 1767 return(append_delims(mdoc, line, &pos, buf));
80387638
SW
1768 }
1769
1770 return(1);
1771}
1772
80387638
SW
1773static int
1774phrase_ta(MACRO_PROT_ARGS)
1775{
f88b6c16 1776 struct mdoc_node *n;
80387638
SW
1777 int la;
1778 enum mdoct ntok;
1779 enum margserr ac;
1780 char *p;
1781
f88b6c16
FF
1782 /* Make sure we are in a column list or ignore this macro. */
1783 n = mdoc->last;
1784 while (NULL != n && MDOC_Bl != n->tok)
1785 n = n->parent;
1786 if (NULL == n || LIST_column != n->norm->Bl.type) {
070c62a6
FF
1787 mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1788 line, ppos, "Ta");
f88b6c16
FF
1789 return(1);
1790 }
80387638 1791
f88b6c16
FF
1792 /* Advance to the next column. */
1793 if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos))
80387638 1794 return(0);
f88b6c16 1795 if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It))
80387638
SW
1796 return(0);
1797
1798 for (;;) {
1799 la = *pos;
f88b6c16 1800 ac = mdoc_zargs(mdoc, line, pos, buf, &p);
80387638
SW
1801
1802 if (ARGS_ERROR == ac)
1803 return(0);
1804 if (ARGS_EOLN == ac)
1805 break;
1806
1807 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1808
1809 if (MDOC_MAX == ntok) {
7888c61d
FF
1810 if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1811 MDOC_JOIN & mdoc_macros[tok].flags))
80387638
SW
1812 return(0);
1813 continue;
1814 }
1815
f88b6c16 1816 if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
80387638 1817 return(0);
f88b6c16 1818 return(append_delims(mdoc, line, pos, buf));
80387638
SW
1819 }
1820
1821 return(1);
1822}