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