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