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