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