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