]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
4cb69976f2e8eb08060125e3645a55a8f1496986
[mandoc.git] / mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.227 2018/12/14 05:18:02 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, 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
478 if (buf[*pos] == '\0')
479 return;
480
481 for (;;) {
482 la = *pos;
483 if (mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p) ==
484 ARGS_EOLN)
485 break;
486 dword(mdoc, line, la, p, DELIM_MAX, 1);
487
488 /*
489 * If we encounter end-of-sentence symbols, then trigger
490 * the double-space.
491 *
492 * XXX: it's easy to allow this to propagate outward to
493 * the last symbol, such that `. )' will cause the
494 * correct double-spacing. However, (1) groff isn't
495 * smart enough to do this and (2) it would require
496 * knowing which symbols break this behaviour, for
497 * example, `. ;' shouldn't propagate the double-space.
498 */
499
500 if (mandoc_eos(p, strlen(p)))
501 mdoc->last->flags |= NODE_EOS;
502 }
503 }
504
505 /*
506 * Parse one word.
507 * If it is a macro, call it and return 1.
508 * Otherwise, allocate it and return 0.
509 */
510 static int
511 macro_or_word(MACRO_PROT_ARGS, int parsed)
512 {
513 char *p;
514 int ntok;
515
516 p = buf + ppos;
517 ntok = TOKEN_NONE;
518 if (*p == '"')
519 p++;
520 else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
521 ntok = lookup(mdoc, tok, line, ppos, p);
522
523 if (ntok == TOKEN_NONE) {
524 dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE ||
525 mdoc_macro(tok)->flags & MDOC_JOIN);
526 return 0;
527 } else {
528 if (tok != TOKEN_NONE &&
529 mdoc_macro(tok)->fp == in_line_eoln)
530 rew_elem(mdoc, tok);
531 (*mdoc_macro(ntok)->fp)(mdoc, ntok, line, ppos, pos, buf);
532 if (tok == TOKEN_NONE)
533 append_delims(mdoc, line, pos, buf);
534 return 1;
535 }
536 }
537
538 /*
539 * Close out block partial/full explicit.
540 */
541 static void
542 blk_exp_close(MACRO_PROT_ARGS)
543 {
544 struct roff_node *body; /* Our own body. */
545 struct roff_node *endbody; /* Our own end marker. */
546 struct roff_node *itblk; /* An It block starting later. */
547 struct roff_node *later; /* A sub-block starting later. */
548 struct roff_node *n; /* Search back to our block. */
549 struct roff_node *target; /* For find_pending(). */
550
551 int j, lastarg, maxargs, nl, pending;
552 enum margserr ac;
553 enum roff_tok atok, ntok;
554 char *p;
555
556 nl = MDOC_NEWLINE & mdoc->flags;
557
558 switch (tok) {
559 case MDOC_Ec:
560 maxargs = 1;
561 break;
562 case MDOC_Ek:
563 mdoc->flags &= ~MDOC_KEEP;
564 /* FALLTHROUGH */
565 default:
566 maxargs = 0;
567 break;
568 }
569
570 /* Search backwards for the beginning of our own body. */
571
572 atok = rew_alt(tok);
573 body = NULL;
574 for (n = mdoc->last; n; n = n->parent) {
575 if (n->flags & NODE_ENDED || n->tok != atok ||
576 n->type != ROFFT_BODY || n->end != ENDBODY_NOT)
577 continue;
578 body = n;
579 break;
580 }
581
582 /*
583 * Search backwards for beginnings of blocks,
584 * both of our own and of pending sub-blocks.
585 */
586
587 endbody = itblk = later = NULL;
588 for (n = mdoc->last; n; n = n->parent) {
589 if (n->flags & NODE_ENDED)
590 continue;
591
592 /*
593 * Mismatching end macros can never break anything
594 * and we only care about the breaking of BLOCKs.
595 */
596
597 if (body == NULL || n->type != ROFFT_BLOCK)
598 continue;
599
600 /*
601 * SYNOPSIS name blocks can not be broken themselves,
602 * but they do get broken together with a broken child.
603 */
604
605 if (n->tok == MDOC_Nm) {
606 if (later != NULL)
607 n->flags |= NODE_BROKEN | NODE_ENDED;
608 continue;
609 }
610
611 if (n->tok == MDOC_It) {
612 itblk = n;
613 continue;
614 }
615
616 if (atok == n->tok) {
617
618 /*
619 * Found the start of our own block.
620 * When there is no pending sub block,
621 * just proceed to closing out.
622 */
623
624 if (later == NULL ||
625 (tok == MDOC_El && itblk == NULL))
626 break;
627
628 /*
629 * When there is a pending sub block, postpone
630 * closing out the current block until the
631 * rew_pending() closing out the sub-block.
632 * Mark the place where the formatting - but not
633 * the scope - of the current block ends.
634 */
635
636 mandoc_msg(MANDOCERR_BLK_NEST,
637 line, ppos, "%s breaks %s",
638 roff_name[atok], roff_name[later->tok]);
639
640 endbody = mdoc_endbody_alloc(mdoc, line, ppos,
641 atok, body);
642
643 if (tok == MDOC_El)
644 itblk->flags |= NODE_ENDED | NODE_BROKEN;
645
646 /*
647 * If a block closing macro taking arguments
648 * breaks another block, put the arguments
649 * into the end marker.
650 */
651
652 if (maxargs)
653 mdoc->next = ROFF_NEXT_CHILD;
654 break;
655 }
656
657 /*
658 * Explicit blocks close out description lines, but
659 * even those can get broken together with a child.
660 */
661
662 if (n->tok == MDOC_Nd) {
663 if (later != NULL)
664 n->flags |= NODE_BROKEN | NODE_ENDED;
665 else
666 rew_last(mdoc, n);
667 continue;
668 }
669
670 /* Breaking an open sub block. */
671
672 break_intermediate(mdoc->last, body);
673 n->flags |= NODE_BROKEN;
674 if (later == NULL)
675 later = n;
676 }
677
678 if (body == NULL) {
679 mandoc_msg(MANDOCERR_BLK_NOTOPEN, line, ppos,
680 "%s", roff_name[tok]);
681 if (maxargs && endbody == NULL) {
682 /*
683 * Stray .Ec without previous .Eo:
684 * Break the output line, keep the arguments.
685 */
686 roff_elem_alloc(mdoc, line, ppos, ROFF_br);
687 rew_elem(mdoc, ROFF_br);
688 }
689 } else if (endbody == NULL) {
690 rew_last(mdoc, body);
691 if (maxargs)
692 mdoc_tail_alloc(mdoc, line, ppos, atok);
693 }
694
695 if ((mdoc_macro(tok)->flags & MDOC_PARSED) == 0) {
696 if (buf[*pos] != '\0')
697 mandoc_msg(MANDOCERR_ARG_SKIP, line, ppos,
698 "%s %s", roff_name[tok], buf + *pos);
699 if (endbody == NULL && n != NULL)
700 rew_pending(mdoc, n);
701 return;
702 }
703
704 if (endbody != NULL)
705 n = endbody;
706
707 ntok = TOKEN_NONE;
708 for (j = 0; ; j++) {
709 lastarg = *pos;
710
711 if (j == maxargs && n != NULL)
712 rew_last(mdoc, n);
713
714 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
715 if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
716 break;
717
718 ntok = lookup(mdoc, tok, line, lastarg, p);
719
720 if (ntok == TOKEN_NONE) {
721 dword(mdoc, line, lastarg, p, DELIM_MAX,
722 mdoc_macro(tok)->flags & MDOC_JOIN);
723 continue;
724 }
725
726 if (n != NULL)
727 rew_last(mdoc, n);
728 mdoc->flags &= ~MDOC_NEWLINE;
729 (*mdoc_macro(ntok)->fp)(mdoc, ntok, line, lastarg, pos, buf);
730 break;
731 }
732
733 if (n != NULL) {
734 pending = 0;
735 if (ntok != TOKEN_NONE && n->flags & NODE_BROKEN) {
736 target = n;
737 do
738 target = target->parent;
739 while ( ! (target->flags & NODE_ENDED));
740 pending = find_pending(mdoc, ntok, line, ppos, target);
741 }
742 if ( ! pending)
743 rew_pending(mdoc, n);
744 }
745 if (nl)
746 append_delims(mdoc, line, pos, buf);
747 }
748
749 static void
750 in_line(MACRO_PROT_ARGS)
751 {
752 int la, scope, cnt, firstarg, mayopen, nc, nl;
753 enum roff_tok ntok;
754 enum margserr ac;
755 enum mdelim d;
756 struct mdoc_arg *arg;
757 char *p;
758
759 nl = MDOC_NEWLINE & mdoc->flags;
760
761 /*
762 * Whether we allow ignored elements (those without content,
763 * usually because of reserved words) to squeak by.
764 */
765
766 switch (tok) {
767 case MDOC_An:
768 case MDOC_Ar:
769 case MDOC_Fl:
770 case MDOC_Mt:
771 case MDOC_Nm:
772 case MDOC_Pa:
773 nc = 1;
774 break;
775 default:
776 nc = 0;
777 break;
778 }
779
780 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
781
782 d = DELIM_NONE;
783 firstarg = 1;
784 mayopen = 1;
785 for (cnt = scope = 0;; ) {
786 la = *pos;
787 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
788
789 /*
790 * At the end of a macro line,
791 * opening delimiters do not suppress spacing.
792 */
793
794 if (ac == ARGS_EOLN) {
795 if (d == DELIM_OPEN)
796 mdoc->last->flags &= ~NODE_DELIMO;
797 break;
798 }
799
800 /*
801 * The rest of the macro line is only punctuation,
802 * to be handled by append_delims().
803 * If there were no other arguments,
804 * do not allow the first one to suppress spacing,
805 * even if it turns out to be a closing one.
806 */
807
808 if (ac == ARGS_PUNCT) {
809 if (cnt == 0 && (nc == 0 || tok == MDOC_An))
810 mdoc->flags |= MDOC_NODELIMC;
811 break;
812 }
813
814 ntok = (tok == MDOC_Fn && !cnt) ?
815 TOKEN_NONE : lookup(mdoc, tok, line, la, p);
816
817 /*
818 * In this case, we've located a submacro and must
819 * execute it. Close out scope, if open. If no
820 * elements have been generated, either create one (nc)
821 * or raise a warning.
822 */
823
824 if (ntok != TOKEN_NONE) {
825 if (scope)
826 rew_elem(mdoc, tok);
827 if (nc && ! cnt) {
828 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
829 rew_last(mdoc, mdoc->last);
830 } else if ( ! nc && ! cnt) {
831 mdoc_argv_free(arg);
832 mandoc_msg(MANDOCERR_MACRO_EMPTY,
833 line, ppos, "%s", roff_name[tok]);
834 }
835 (*mdoc_macro(ntok)->fp)(mdoc, ntok,
836 line, la, pos, buf);
837 if (nl)
838 append_delims(mdoc, line, pos, buf);
839 return;
840 }
841
842 /*
843 * Handle punctuation. Set up our scope, if a word;
844 * rewind the scope, if a delimiter; then append the word.
845 */
846
847 if ((d = mdoc_isdelim(p)) != DELIM_NONE) {
848 /*
849 * If we encounter closing punctuation, no word
850 * has been emitted, no scope is open, and we're
851 * allowed to have an empty element, then start
852 * a new scope.
853 */
854 if ((d == DELIM_CLOSE ||
855 (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
856 !cnt && !scope && nc && mayopen) {
857 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
858 scope = 1;
859 cnt++;
860 if (tok == MDOC_Nm)
861 mayopen = 0;
862 }
863 /*
864 * Close out our scope, if one is open, before
865 * any punctuation.
866 */
867 if (scope && tok != MDOC_Lk) {
868 rew_elem(mdoc, tok);
869 scope = 0;
870 if (tok == MDOC_Fn)
871 mayopen = 0;
872 }
873 } else if (mayopen && !scope) {
874 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
875 scope = 1;
876 cnt++;
877 }
878
879 dword(mdoc, line, la, p, d,
880 mdoc_macro(tok)->flags & MDOC_JOIN);
881
882 /*
883 * If the first argument is a closing delimiter,
884 * do not suppress spacing before it.
885 */
886
887 if (firstarg && d == DELIM_CLOSE && !nc)
888 mdoc->last->flags &= ~NODE_DELIMC;
889 firstarg = 0;
890
891 /*
892 * `Fl' macros have their scope re-opened with each new
893 * word so that the `-' can be added to each one without
894 * having to parse out spaces.
895 */
896 if (scope && tok == MDOC_Fl) {
897 rew_elem(mdoc, tok);
898 scope = 0;
899 }
900 }
901
902 if (scope && tok != MDOC_Lk) {
903 rew_elem(mdoc, tok);
904 scope = 0;
905 }
906
907 /*
908 * If no elements have been collected and we're allowed to have
909 * empties (nc), open a scope and close it out. Otherwise,
910 * raise a warning.
911 */
912
913 if ( ! cnt) {
914 if (nc) {
915 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
916 rew_last(mdoc, mdoc->last);
917 } else {
918 mdoc_argv_free(arg);
919 mandoc_msg(MANDOCERR_MACRO_EMPTY,
920 line, ppos, "%s", roff_name[tok]);
921 }
922 }
923 if (nl)
924 append_delims(mdoc, line, pos, buf);
925 if (scope)
926 rew_elem(mdoc, tok);
927 }
928
929 static void
930 blk_full(MACRO_PROT_ARGS)
931 {
932 int la, nl, parsed;
933 struct mdoc_arg *arg;
934 struct roff_node *blk; /* Our own or a broken block. */
935 struct roff_node *head; /* Our own head. */
936 struct roff_node *body; /* Our own body. */
937 struct roff_node *n;
938 enum margserr ac, lac;
939 char *p;
940
941 nl = MDOC_NEWLINE & mdoc->flags;
942
943 if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) {
944 mandoc_msg(MANDOCERR_MACRO_EMPTY,
945 line, ppos, "%s", roff_name[tok]);
946 return;
947 }
948
949 if ((mdoc_macro(tok)->flags & MDOC_EXPLICIT) == 0) {
950
951 /* Here, tok is one of Sh Ss Nm Nd It. */
952
953 blk = NULL;
954 for (n = mdoc->last; n != NULL; n = n->parent) {
955 if (n->flags & NODE_ENDED) {
956 if ( ! (n->flags & NODE_VALID))
957 n->flags |= NODE_BROKEN;
958 continue;
959 }
960 if (n->type != ROFFT_BLOCK)
961 continue;
962
963 if (tok == MDOC_It && n->tok == MDOC_Bl) {
964 if (blk != NULL) {
965 mandoc_msg(MANDOCERR_BLK_BROKEN,
966 line, ppos, "It breaks %s",
967 roff_name[blk->tok]);
968 rew_pending(mdoc, blk);
969 }
970 break;
971 }
972
973 if (mdoc_macro(n->tok)->flags & MDOC_EXPLICIT) {
974 switch (tok) {
975 case MDOC_Sh:
976 case MDOC_Ss:
977 mandoc_msg(MANDOCERR_BLK_BROKEN,
978 line, ppos,
979 "%s breaks %s", roff_name[tok],
980 roff_name[n->tok]);
981 rew_pending(mdoc, n);
982 n = mdoc->last;
983 continue;
984 case MDOC_It:
985 /* Delay in case it's astray. */
986 blk = n;
987 continue;
988 default:
989 break;
990 }
991 break;
992 }
993
994 /* Here, n is one of Sh Ss Nm Nd It. */
995
996 if (tok != MDOC_Sh && (n->tok == MDOC_Sh ||
997 (tok != MDOC_Ss && (n->tok == MDOC_Ss ||
998 (tok != MDOC_It && n->tok == MDOC_It)))))
999 break;
1000
1001 /* Item breaking an explicit block. */
1002
1003 if (blk != NULL) {
1004 mandoc_msg(MANDOCERR_BLK_BROKEN, line, ppos,
1005 "It breaks %s", roff_name[blk->tok]);
1006 rew_pending(mdoc, blk);
1007 blk = NULL;
1008 }
1009
1010 /* Close out prior implicit scopes. */
1011
1012 rew_pending(mdoc, n);
1013 }
1014
1015 /* Skip items outside lists. */
1016
1017 if (tok == MDOC_It && (n == NULL || n->tok != MDOC_Bl)) {
1018 mandoc_msg(MANDOCERR_IT_STRAY,
1019 line, ppos, "It %s", buf + *pos);
1020 roff_elem_alloc(mdoc, line, ppos, ROFF_br);
1021 rew_elem(mdoc, ROFF_br);
1022 return;
1023 }
1024 }
1025
1026 /*
1027 * This routine accommodates implicitly- and explicitly-scoped
1028 * macro openings. Implicit ones first close out prior scope
1029 * (seen above). Delay opening the head until necessary to
1030 * allow leading punctuation to print. Special consideration
1031 * for `It -column', which has phrase-part syntax instead of
1032 * regular child nodes.
1033 */
1034
1035 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1036 blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg);
1037 head = body = NULL;
1038
1039 /*
1040 * Exception: Heads of `It' macros in `-diag' lists are not
1041 * parsed, even though `It' macros in general are parsed.
1042 */
1043
1044 parsed = tok != MDOC_It ||
1045 mdoc->last->parent->tok != MDOC_Bl ||
1046 mdoc->last->parent->norm->Bl.type != LIST_diag;
1047
1048 /*
1049 * The `Nd' macro has all arguments in its body: it's a hybrid
1050 * of block partial-explicit and full-implicit. Stupid.
1051 */
1052
1053 if (tok == MDOC_Nd) {
1054 head = roff_head_alloc(mdoc, line, ppos, tok);
1055 rew_last(mdoc, head);
1056 body = roff_body_alloc(mdoc, line, ppos, tok);
1057 }
1058
1059 if (tok == MDOC_Bk)
1060 mdoc->flags |= MDOC_KEEP;
1061
1062 ac = ARGS_EOLN;
1063 for (;;) {
1064
1065 /*
1066 * If we are right after a tab character,
1067 * do not parse the first word for macros.
1068 */
1069
1070 if (mdoc->flags & MDOC_PHRASEQN) {
1071 mdoc->flags &= ~MDOC_PHRASEQN;
1072 mdoc->flags |= MDOC_PHRASEQF;
1073 }
1074
1075 la = *pos;
1076 lac = ac;
1077 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1078 if (ac == ARGS_EOLN) {
1079 if (lac != ARGS_PHRASE ||
1080 ! (mdoc->flags & MDOC_PHRASEQF))
1081 break;
1082
1083 /*
1084 * This line ends in a tab; start the next
1085 * column now, with a leading blank.
1086 */
1087
1088 if (body != NULL)
1089 rew_last(mdoc, body);
1090 body = roff_body_alloc(mdoc, line, ppos, tok);
1091 roff_word_alloc(mdoc, line, ppos, "\\&");
1092 break;
1093 }
1094
1095 if (tok == MDOC_Bd || tok == MDOC_Bk) {
1096 mandoc_msg(MANDOCERR_ARG_EXCESS, line, la,
1097 "%s ... %s", roff_name[tok], buf + la);
1098 break;
1099 }
1100 if (tok == MDOC_Rs) {
1101 mandoc_msg(MANDOCERR_ARG_SKIP,
1102 line, la, "Rs %s", buf + la);
1103 break;
1104 }
1105 if (ac == ARGS_PUNCT)
1106 break;
1107
1108 /*
1109 * Emit leading punctuation (i.e., punctuation before
1110 * the ROFFT_HEAD) for non-phrase types.
1111 */
1112
1113 if (head == NULL &&
1114 ac != ARGS_PHRASE &&
1115 mdoc_isdelim(p) == DELIM_OPEN) {
1116 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1117 continue;
1118 }
1119
1120 /* Open a head if one hasn't been opened. */
1121
1122 if (head == NULL)
1123 head = roff_head_alloc(mdoc, line, ppos, tok);
1124
1125 if (ac == ARGS_PHRASE) {
1126
1127 /*
1128 * If we haven't opened a body yet, rewind the
1129 * head; if we have, rewind that instead.
1130 */
1131
1132 rew_last(mdoc, body == NULL ? head : body);
1133 body = roff_body_alloc(mdoc, line, ppos, tok);
1134
1135 /* Process to the tab or to the end of the line. */
1136
1137 mdoc->flags |= MDOC_PHRASE;
1138 parse_rest(mdoc, TOKEN_NONE, line, &la, buf);
1139 mdoc->flags &= ~MDOC_PHRASE;
1140
1141 /* There may have been `Ta' macros. */
1142
1143 while (body->next != NULL)
1144 body = body->next;
1145 continue;
1146 }
1147
1148 if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed))
1149 break;
1150 }
1151
1152 if (blk->flags & NODE_VALID)
1153 return;
1154 if (head == NULL)
1155 head = roff_head_alloc(mdoc, line, ppos, tok);
1156 if (nl && tok != MDOC_Bd && tok != MDOC_Bl && tok != MDOC_Rs)
1157 append_delims(mdoc, line, pos, buf);
1158 if (body != NULL)
1159 goto out;
1160 if (find_pending(mdoc, tok, line, ppos, head))
1161 return;
1162
1163 /* Close out scopes to remain in a consistent state. */
1164
1165 rew_last(mdoc, head);
1166 body = roff_body_alloc(mdoc, line, ppos, tok);
1167 out:
1168 if (mdoc->flags & MDOC_FREECOL) {
1169 rew_last(mdoc, body);
1170 rew_last(mdoc, blk);
1171 mdoc->flags &= ~MDOC_FREECOL;
1172 }
1173 }
1174
1175 static void
1176 blk_part_imp(MACRO_PROT_ARGS)
1177 {
1178 int la, nl;
1179 enum margserr ac;
1180 char *p;
1181 struct roff_node *blk; /* saved block context */
1182 struct roff_node *body; /* saved body context */
1183 struct roff_node *n;
1184
1185 nl = MDOC_NEWLINE & mdoc->flags;
1186
1187 /*
1188 * A macro that spans to the end of the line. This is generally
1189 * (but not necessarily) called as the first macro. The block
1190 * has a head as the immediate child, which is always empty,
1191 * followed by zero or more opening punctuation nodes, then the
1192 * body (which may be empty, depending on the macro), then zero
1193 * or more closing punctuation nodes.
1194 */
1195
1196 blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1197 rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1198
1199 /*
1200 * Open the body scope "on-demand", that is, after we've
1201 * processed all our the leading delimiters (open parenthesis,
1202 * etc.).
1203 */
1204
1205 for (body = NULL; ; ) {
1206 la = *pos;
1207 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1208 if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
1209 break;
1210
1211 if (body == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
1212 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1213 continue;
1214 }
1215
1216 if (body == NULL)
1217 body = roff_body_alloc(mdoc, line, ppos, tok);
1218
1219 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1220 break;
1221 }
1222 if (body == NULL)
1223 body = roff_body_alloc(mdoc, line, ppos, tok);
1224
1225 if (find_pending(mdoc, tok, line, ppos, body))
1226 return;
1227
1228 rew_last(mdoc, body);
1229 if (nl)
1230 append_delims(mdoc, line, pos, buf);
1231 rew_pending(mdoc, blk);
1232
1233 /* Move trailing .Ns out of scope. */
1234
1235 for (n = body->child; n && n->next; n = n->next)
1236 /* Do nothing. */ ;
1237 if (n && n->tok == MDOC_Ns)
1238 roff_node_relink(mdoc, n);
1239 }
1240
1241 static void
1242 blk_part_exp(MACRO_PROT_ARGS)
1243 {
1244 int la, nl;
1245 enum margserr ac;
1246 struct roff_node *head; /* keep track of head */
1247 char *p;
1248
1249 nl = MDOC_NEWLINE & mdoc->flags;
1250
1251 /*
1252 * The opening of an explicit macro having zero or more leading
1253 * punctuation nodes; a head with optional single element (the
1254 * case of `Eo'); and a body that may be empty.
1255 */
1256
1257 roff_block_alloc(mdoc, line, ppos, tok);
1258 head = NULL;
1259 for (;;) {
1260 la = *pos;
1261 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1262 if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1263 break;
1264
1265 /* Flush out leading punctuation. */
1266
1267 if (head == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
1268 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1269 continue;
1270 }
1271
1272 if (head == NULL) {
1273 head = roff_head_alloc(mdoc, line, ppos, tok);
1274 if (tok == MDOC_Eo) /* Not parsed. */
1275 dword(mdoc, line, la, p, DELIM_MAX, 0);
1276 rew_last(mdoc, head);
1277 roff_body_alloc(mdoc, line, ppos, tok);
1278 if (tok == MDOC_Eo)
1279 continue;
1280 }
1281
1282 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1283 break;
1284 }
1285
1286 /* Clean-up to leave in a consistent state. */
1287
1288 if (head == NULL) {
1289 rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1290 roff_body_alloc(mdoc, line, ppos, tok);
1291 }
1292 if (nl)
1293 append_delims(mdoc, line, pos, buf);
1294 }
1295
1296 static void
1297 in_line_argn(MACRO_PROT_ARGS)
1298 {
1299 struct mdoc_arg *arg;
1300 char *p;
1301 enum margserr ac;
1302 enum roff_tok ntok;
1303 int state; /* arg#; -1: not yet open; -2: closed */
1304 int la, maxargs, nl;
1305
1306 nl = mdoc->flags & MDOC_NEWLINE;
1307
1308 /*
1309 * A line macro that has a fixed number of arguments (maxargs).
1310 * Only open the scope once the first non-leading-punctuation is
1311 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1312 * keep it open until the maximum number of arguments are
1313 * exhausted.
1314 */
1315
1316 switch (tok) {
1317 case MDOC_Ap:
1318 case MDOC_Ns:
1319 case MDOC_Ux:
1320 maxargs = 0;
1321 break;
1322 case MDOC_Bx:
1323 case MDOC_Es:
1324 case MDOC_Xr:
1325 maxargs = 2;
1326 break;
1327 default:
1328 maxargs = 1;
1329 break;
1330 }
1331
1332 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1333
1334 state = -1;
1335 p = NULL;
1336 for (;;) {
1337 la = *pos;
1338 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1339
1340 if (ac == ARGS_WORD && state == -1 &&
1341 (mdoc_macro(tok)->flags & MDOC_IGNDELIM) == 0 &&
1342 mdoc_isdelim(p) == DELIM_OPEN) {
1343 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1344 continue;
1345 }
1346
1347 if (state == -1 && tok != MDOC_In &&
1348 tok != MDOC_St && tok != MDOC_Xr) {
1349 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1350 state = 0;
1351 }
1352
1353 if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
1354 if (abs(state) < 2 && tok == MDOC_Pf)
1355 mandoc_msg(MANDOCERR_PF_SKIP,
1356 line, ppos, "Pf %s",
1357 p == NULL ? "at eol" : p);
1358 break;
1359 }
1360
1361 if (state == maxargs) {
1362 rew_elem(mdoc, tok);
1363 state = -2;
1364 }
1365
1366 ntok = (tok == MDOC_Pf && state == 0) ?
1367 TOKEN_NONE : lookup(mdoc, tok, line, la, p);
1368
1369 if (ntok != TOKEN_NONE) {
1370 if (state >= 0) {
1371 rew_elem(mdoc, tok);
1372 state = -2;
1373 }
1374 (*mdoc_macro(ntok)->fp)(mdoc, ntok,
1375 line, la, pos, buf);
1376 break;
1377 }
1378
1379 if (mdoc_macro(tok)->flags & MDOC_IGNDELIM ||
1380 mdoc_isdelim(p) == DELIM_NONE) {
1381 if (state == -1) {
1382 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1383 state = 1;
1384 } else if (state >= 0)
1385 state++;
1386 } else if (state >= 0) {
1387 rew_elem(mdoc, tok);
1388 state = -2;
1389 }
1390
1391 dword(mdoc, line, la, p, DELIM_MAX,
1392 mdoc_macro(tok)->flags & MDOC_JOIN);
1393 }
1394
1395 if (state == -1) {
1396 mandoc_msg(MANDOCERR_MACRO_EMPTY,
1397 line, ppos, "%s", roff_name[tok]);
1398 return;
1399 }
1400
1401 if (state == 0 && tok == MDOC_Pf)
1402 append_delims(mdoc, line, pos, buf);
1403 if (state >= 0)
1404 rew_elem(mdoc, tok);
1405 if (nl)
1406 append_delims(mdoc, line, pos, buf);
1407 }
1408
1409 static void
1410 in_line_eoln(MACRO_PROT_ARGS)
1411 {
1412 struct roff_node *n;
1413 struct mdoc_arg *arg;
1414
1415 if ((tok == MDOC_Pp || tok == MDOC_Lp) &&
1416 ! (mdoc->flags & MDOC_SYNOPSIS)) {
1417 n = mdoc->last;
1418 if (mdoc->next == ROFF_NEXT_SIBLING)
1419 n = n->parent;
1420 if (n->tok == MDOC_Nm)
1421 rew_last(mdoc, n->parent);
1422 }
1423
1424 if (buf[*pos] == '\0' &&
1425 (tok == MDOC_Fd || *roff_name[tok] == '%')) {
1426 mandoc_msg(MANDOCERR_MACRO_EMPTY,
1427 line, ppos, "%s", roff_name[tok]);
1428 return;
1429 }
1430
1431 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1432 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1433 if (parse_rest(mdoc, tok, line, pos, buf))
1434 return;
1435 rew_elem(mdoc, tok);
1436 }
1437
1438 /*
1439 * The simplest argument parser available: Parse the remaining
1440 * words until the end of the phrase or line and return 0
1441 * or until the next macro, call that macro, and return 1.
1442 */
1443 static int
1444 parse_rest(struct roff_man *mdoc, enum roff_tok tok,
1445 int line, int *pos, char *buf)
1446 {
1447 int la;
1448
1449 for (;;) {
1450 la = *pos;
1451 if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1452 return 0;
1453 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1454 return 1;
1455 }
1456 }
1457
1458 static void
1459 ctx_synopsis(MACRO_PROT_ARGS)
1460 {
1461
1462 if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
1463 in_line(mdoc, tok, line, ppos, pos, buf);
1464 else if (tok == MDOC_Nm)
1465 blk_full(mdoc, tok, line, ppos, pos, buf);
1466 else {
1467 assert(tok == MDOC_Vt);
1468 blk_part_imp(mdoc, tok, line, ppos, pos, buf);
1469 }
1470 }
1471
1472 /*
1473 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1474 * They're unusual because they're basically free-form text until a
1475 * macro is encountered.
1476 */
1477 static void
1478 phrase_ta(MACRO_PROT_ARGS)
1479 {
1480 struct roff_node *body, *n;
1481
1482 /* Make sure we are in a column list or ignore this macro. */
1483
1484 body = NULL;
1485 for (n = mdoc->last; n != NULL; n = n->parent) {
1486 if (n->flags & NODE_ENDED)
1487 continue;
1488 if (n->tok == MDOC_It && n->type == ROFFT_BODY)
1489 body = n;
1490 if (n->tok == MDOC_Bl && n->end == ENDBODY_NOT)
1491 break;
1492 }
1493
1494 if (n == NULL || n->norm->Bl.type != LIST_column) {
1495 mandoc_msg(MANDOCERR_TA_STRAY, line, ppos, "Ta");
1496 return;
1497 }
1498
1499 /* Advance to the next column. */
1500
1501 rew_last(mdoc, body);
1502 roff_body_alloc(mdoc, line, ppos, MDOC_It);
1503 parse_rest(mdoc, TOKEN_NONE, line, pos, buf);
1504 }