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