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