]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
Use "-" rather than "\(hy" for the heads of .Bl -dash and -hyphen lists.
[mandoc.git] / mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.203 2015/10/12 00:08:15 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 }
367
368 static void
369 rew_elem(struct roff_man *mdoc, int tok)
370 {
371 struct roff_node *n;
372
373 n = mdoc->last;
374 if (n->type != ROFFT_ELEM)
375 n = n->parent;
376 assert(n->type == ROFFT_ELEM);
377 assert(tok == n->tok);
378 rew_last(mdoc, n);
379 }
380
381 /*
382 * If there is an open sub-block of the target requiring
383 * explicit close-out, postpone closing out the target until
384 * the rew_pending() call closing out the sub-block.
385 */
386 static int
387 find_pending(struct roff_man *mdoc, int tok, int line, int ppos,
388 struct roff_node *target)
389 {
390 struct roff_node *n;
391 int irc;
392
393 irc = 0;
394 for (n = mdoc->last; n != NULL && n != target; n = n->parent) {
395 if (n->flags & MDOC_ENDED) {
396 if ( ! (n->flags & MDOC_VALID))
397 n->flags |= MDOC_BROKEN;
398 continue;
399 }
400 if (n->type == ROFFT_BLOCK &&
401 mdoc_macros[n->tok].flags & MDOC_EXPLICIT) {
402 irc = 1;
403 n->flags = MDOC_BROKEN;
404 if (target->type == ROFFT_HEAD)
405 target->flags = MDOC_ENDED;
406 else if ( ! (target->flags & MDOC_ENDED)) {
407 mandoc_vmsg(MANDOCERR_BLK_NEST,
408 mdoc->parse, line, ppos,
409 "%s breaks %s", mdoc_macronames[tok],
410 mdoc_macronames[n->tok]);
411 mdoc_endbody_alloc(mdoc, line, ppos,
412 tok, target, ENDBODY_NOSPACE);
413 }
414 }
415 }
416 return irc;
417 }
418
419 /*
420 * Allocate a word and check whether it's punctuation or not.
421 * Punctuation consists of those tokens found in mdoc_isdelim().
422 */
423 static void
424 dword(struct roff_man *mdoc, int line, int col, const char *p,
425 enum mdelim d, int may_append)
426 {
427
428 if (d == DELIM_MAX)
429 d = mdoc_isdelim(p);
430
431 if (may_append &&
432 ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
433 d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT &&
434 mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
435 roff_word_append(mdoc, p);
436 return;
437 }
438
439 roff_word_alloc(mdoc, line, col, p);
440
441 /*
442 * If the word consists of a bare delimiter,
443 * flag the new node accordingly,
444 * unless doing so was vetoed by the invoking macro.
445 * Always clear the veto, it is only valid for one word.
446 */
447
448 if (d == DELIM_OPEN)
449 mdoc->last->flags |= MDOC_DELIMO;
450 else if (d == DELIM_CLOSE &&
451 ! (mdoc->flags & MDOC_NODELIMC) &&
452 mdoc->last->parent->tok != MDOC_Fd)
453 mdoc->last->flags |= MDOC_DELIMC;
454 mdoc->flags &= ~MDOC_NODELIMC;
455 }
456
457 static void
458 append_delims(struct roff_man *mdoc, int line, int *pos, char *buf)
459 {
460 char *p;
461 int la;
462
463 if (buf[*pos] == '\0')
464 return;
465
466 for (;;) {
467 la = *pos;
468 if (mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p) ==
469 ARGS_EOLN)
470 break;
471 dword(mdoc, line, la, p, DELIM_MAX, 1);
472
473 /*
474 * If we encounter end-of-sentence symbols, then trigger
475 * the double-space.
476 *
477 * XXX: it's easy to allow this to propagate outward to
478 * the last symbol, such that `. )' will cause the
479 * correct double-spacing. However, (1) groff isn't
480 * smart enough to do this and (2) it would require
481 * knowing which symbols break this behaviour, for
482 * example, `. ;' shouldn't propagate the double-space.
483 */
484
485 if (mandoc_eos(p, strlen(p)))
486 mdoc->last->flags |= MDOC_EOS;
487 }
488 }
489
490 /*
491 * Parse one word.
492 * If it is a macro, call it and return 1.
493 * Otherwise, allocate it and return 0.
494 */
495 static int
496 macro_or_word(MACRO_PROT_ARGS, int parsed)
497 {
498 char *p;
499 int ntok;
500
501 p = buf + ppos;
502 ntok = TOKEN_NONE;
503 if (*p == '"')
504 p++;
505 else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
506 ntok = lookup(mdoc, tok, line, ppos, p);
507
508 if (ntok == TOKEN_NONE) {
509 dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE ||
510 mdoc_macros[tok].flags & MDOC_JOIN);
511 return 0;
512 } else {
513 if (mdoc_macros[tok].fp == in_line_eoln)
514 rew_elem(mdoc, tok);
515 mdoc_macro(mdoc, ntok, line, ppos, pos, buf);
516 if (tok == TOKEN_NONE)
517 append_delims(mdoc, line, pos, buf);
518 return 1;
519 }
520 }
521
522 /*
523 * Close out block partial/full explicit.
524 */
525 static void
526 blk_exp_close(MACRO_PROT_ARGS)
527 {
528 struct roff_node *body; /* Our own body. */
529 struct roff_node *endbody; /* Our own end marker. */
530 struct roff_node *itblk; /* An It block starting later. */
531 struct roff_node *later; /* A sub-block starting later. */
532 struct roff_node *n; /* Search back to our block. */
533 struct roff_node *target; /* For find_pending(). */
534
535 int j, lastarg, maxargs, nl, pending;
536 enum margserr ac;
537 int atok, ntok;
538 char *p;
539
540 nl = MDOC_NEWLINE & mdoc->flags;
541
542 switch (tok) {
543 case MDOC_Ec:
544 maxargs = 1;
545 break;
546 case MDOC_Ek:
547 mdoc->flags &= ~MDOC_KEEP;
548 /* FALLTHROUGH */
549 default:
550 maxargs = 0;
551 break;
552 }
553
554 /*
555 * Search backwards for beginnings of blocks,
556 * both of our own and of pending sub-blocks.
557 */
558
559 atok = rew_alt(tok);
560 body = endbody = itblk = later = NULL;
561 for (n = mdoc->last; n; n = n->parent) {
562 if (n->flags & MDOC_ENDED) {
563 if ( ! (n->flags & MDOC_VALID))
564 n->flags |= MDOC_BROKEN;
565 continue;
566 }
567
568 /* Remember the start of our own body. */
569
570 if (n->type == ROFFT_BODY && atok == n->tok) {
571 if (n->end == ENDBODY_NOT)
572 body = n;
573 continue;
574 }
575
576 if (n->type != ROFFT_BLOCK || n->tok == MDOC_Nm)
577 continue;
578
579 if (n->tok == MDOC_It) {
580 itblk = n;
581 continue;
582 }
583
584 if (atok == n->tok) {
585 assert(body);
586
587 /*
588 * Found the start of our own block.
589 * When there is no pending sub block,
590 * just proceed to closing out.
591 */
592
593 if (later == NULL ||
594 (tok == MDOC_El && itblk == NULL))
595 break;
596
597 /*
598 * When there is a pending sub block, postpone
599 * closing out the current block until the
600 * rew_pending() closing out the sub-block.
601 * Mark the place where the formatting - but not
602 * the scope - of the current block ends.
603 */
604
605 mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse,
606 line, ppos, "%s breaks %s",
607 mdoc_macronames[atok],
608 mdoc_macronames[later->tok]);
609
610 endbody = mdoc_endbody_alloc(mdoc, line, ppos,
611 atok, body, ENDBODY_SPACE);
612
613 if (tok == MDOC_El)
614 itblk->flags |= MDOC_ENDED | MDOC_BROKEN;
615
616 /*
617 * If a block closing macro taking arguments
618 * breaks another block, put the arguments
619 * into the end marker.
620 */
621
622 if (maxargs)
623 mdoc->next = ROFF_NEXT_CHILD;
624 break;
625 }
626
627 /* Explicit blocks close out description lines. */
628
629 if (n->tok == MDOC_Nd) {
630 rew_last(mdoc, n);
631 continue;
632 }
633
634 /* Breaking an open sub block. */
635
636 n->flags |= MDOC_BROKEN;
637 if (later == NULL)
638 later = n;
639 }
640
641 if (body == NULL) {
642 mandoc_msg(MANDOCERR_BLK_NOTOPEN, mdoc->parse,
643 line, ppos, mdoc_macronames[tok]);
644 if (later != NULL)
645 later->flags &= ~MDOC_BROKEN;
646 if (maxargs && endbody == NULL) {
647 /*
648 * Stray .Ec without previous .Eo:
649 * Break the output line, keep the arguments.
650 */
651 roff_elem_alloc(mdoc, line, ppos, MDOC_br);
652 rew_elem(mdoc, MDOC_br);
653 }
654 } else if (endbody == NULL) {
655 rew_last(mdoc, body);
656 if (maxargs)
657 mdoc_tail_alloc(mdoc, line, ppos, atok);
658 }
659
660 if ( ! (mdoc_macros[tok].flags & MDOC_PARSED)) {
661 if (buf[*pos] != '\0')
662 mandoc_vmsg(MANDOCERR_ARG_SKIP,
663 mdoc->parse, line, ppos,
664 "%s %s", mdoc_macronames[tok],
665 buf + *pos);
666 if (endbody == NULL && n != NULL)
667 rew_pending(mdoc, n);
668 return;
669 }
670
671 if (endbody != NULL)
672 n = endbody;
673
674 ntok = TOKEN_NONE;
675 for (j = 0; ; j++) {
676 lastarg = *pos;
677
678 if (j == maxargs && n != NULL)
679 rew_last(mdoc, n);
680
681 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
682 if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
683 break;
684
685 ntok = ac == ARGS_QWORD ? TOKEN_NONE :
686 lookup(mdoc, tok, line, lastarg, p);
687
688 if (ntok == TOKEN_NONE) {
689 dword(mdoc, line, lastarg, p, DELIM_MAX,
690 MDOC_JOIN & mdoc_macros[tok].flags);
691 continue;
692 }
693
694 if (n != NULL)
695 rew_last(mdoc, n);
696 mdoc->flags &= ~MDOC_NEWLINE;
697 mdoc_macro(mdoc, ntok, line, lastarg, pos, buf);
698 break;
699 }
700
701 if (n != NULL) {
702 if (ntok != TOKEN_NONE && n->flags & MDOC_BROKEN) {
703 target = n;
704 do
705 target = target->parent;
706 while ( ! (target->flags & MDOC_ENDED));
707 pending = find_pending(mdoc, ntok, line, ppos,
708 target);
709 } else
710 pending = 0;
711 if ( ! pending)
712 rew_pending(mdoc, n);
713 }
714 if (nl)
715 append_delims(mdoc, line, pos, buf);
716 }
717
718 static void
719 in_line(MACRO_PROT_ARGS)
720 {
721 int la, scope, cnt, firstarg, mayopen, nc, nl;
722 int ntok;
723 enum margserr ac;
724 enum mdelim d;
725 struct mdoc_arg *arg;
726 char *p;
727
728 nl = MDOC_NEWLINE & mdoc->flags;
729
730 /*
731 * Whether we allow ignored elements (those without content,
732 * usually because of reserved words) to squeak by.
733 */
734
735 switch (tok) {
736 case MDOC_An:
737 case MDOC_Ar:
738 case MDOC_Fl:
739 case MDOC_Mt:
740 case MDOC_Nm:
741 case MDOC_Pa:
742 nc = 1;
743 break;
744 default:
745 nc = 0;
746 break;
747 }
748
749 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
750
751 d = DELIM_NONE;
752 firstarg = 1;
753 mayopen = 1;
754 for (cnt = scope = 0;; ) {
755 la = *pos;
756 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
757
758 /*
759 * At the end of a macro line,
760 * opening delimiters do not suppress spacing.
761 */
762
763 if (ac == ARGS_EOLN) {
764 if (d == DELIM_OPEN)
765 mdoc->last->flags &= ~MDOC_DELIMO;
766 break;
767 }
768
769 /*
770 * The rest of the macro line is only punctuation,
771 * to be handled by append_delims().
772 * If there were no other arguments,
773 * do not allow the first one to suppress spacing,
774 * even if it turns out to be a closing one.
775 */
776
777 if (ac == ARGS_PUNCT) {
778 if (cnt == 0 && (nc == 0 || tok == MDOC_An))
779 mdoc->flags |= MDOC_NODELIMC;
780 break;
781 }
782
783 ntok = (ac == ARGS_QWORD || (tok == MDOC_Fn && !cnt)) ?
784 TOKEN_NONE : lookup(mdoc, tok, line, la, p);
785
786 /*
787 * In this case, we've located a submacro and must
788 * execute it. Close out scope, if open. If no
789 * elements have been generated, either create one (nc)
790 * or raise a warning.
791 */
792
793 if (ntok != TOKEN_NONE) {
794 if (scope)
795 rew_elem(mdoc, tok);
796 if (nc && ! cnt) {
797 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
798 rew_last(mdoc, mdoc->last);
799 } else if ( ! nc && ! cnt) {
800 mdoc_argv_free(arg);
801 mandoc_msg(MANDOCERR_MACRO_EMPTY,
802 mdoc->parse, line, ppos,
803 mdoc_macronames[tok]);
804 }
805 mdoc_macro(mdoc, ntok, line, la, pos, buf);
806 if (nl)
807 append_delims(mdoc, line, pos, buf);
808 return;
809 }
810
811 /*
812 * Non-quote-enclosed punctuation. Set up our scope, if
813 * a word; rewind the scope, if a delimiter; then append
814 * the word.
815 */
816
817 d = ac == ARGS_QWORD ? DELIM_NONE : mdoc_isdelim(p);
818
819 if (DELIM_NONE != d) {
820 /*
821 * If we encounter closing punctuation, no word
822 * has been emitted, no scope is open, and we're
823 * allowed to have an empty element, then start
824 * a new scope.
825 */
826 if ((d == DELIM_CLOSE ||
827 (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
828 !cnt && !scope && nc && mayopen) {
829 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
830 scope = 1;
831 cnt++;
832 if (tok == MDOC_Nm)
833 mayopen = 0;
834 }
835 /*
836 * Close out our scope, if one is open, before
837 * any punctuation.
838 */
839 if (scope)
840 rew_elem(mdoc, tok);
841 scope = 0;
842 if (tok == MDOC_Fn)
843 mayopen = 0;
844 } else if (mayopen && !scope) {
845 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
846 scope = 1;
847 cnt++;
848 }
849
850 dword(mdoc, line, la, p, d,
851 MDOC_JOIN & mdoc_macros[tok].flags);
852
853 /*
854 * If the first argument is a closing delimiter,
855 * do not suppress spacing before it.
856 */
857
858 if (firstarg && d == DELIM_CLOSE && !nc)
859 mdoc->last->flags &= ~MDOC_DELIMC;
860 firstarg = 0;
861
862 /*
863 * `Fl' macros have their scope re-opened with each new
864 * word so that the `-' can be added to each one without
865 * having to parse out spaces.
866 */
867 if (scope && tok == MDOC_Fl) {
868 rew_elem(mdoc, tok);
869 scope = 0;
870 }
871 }
872
873 if (scope)
874 rew_elem(mdoc, tok);
875
876 /*
877 * If no elements have been collected and we're allowed to have
878 * empties (nc), open a scope and close it out. Otherwise,
879 * raise a warning.
880 */
881
882 if ( ! cnt) {
883 if (nc) {
884 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
885 rew_last(mdoc, mdoc->last);
886 } else {
887 mdoc_argv_free(arg);
888 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
889 line, ppos, mdoc_macronames[tok]);
890 }
891 }
892 if (nl)
893 append_delims(mdoc, line, pos, buf);
894 }
895
896 static void
897 blk_full(MACRO_PROT_ARGS)
898 {
899 int la, nl, parsed;
900 struct mdoc_arg *arg;
901 struct roff_node *blk; /* Our own or a broken block. */
902 struct roff_node *head; /* Our own head. */
903 struct roff_node *body; /* Our own body. */
904 struct roff_node *n;
905 enum margserr ac, lac;
906 char *p;
907
908 nl = MDOC_NEWLINE & mdoc->flags;
909
910 if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) {
911 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
912 line, ppos, mdoc_macronames[tok]);
913 return;
914 }
915
916 if ( ! (mdoc_macros[tok].flags & MDOC_EXPLICIT)) {
917
918 /* Here, tok is one of Sh Ss Nm Nd It. */
919
920 blk = NULL;
921 for (n = mdoc->last; n != NULL; n = n->parent) {
922 if (n->flags & MDOC_ENDED) {
923 if ( ! (n->flags & MDOC_VALID))
924 n->flags |= MDOC_BROKEN;
925 continue;
926 }
927 if (n->type != ROFFT_BLOCK)
928 continue;
929
930 if (tok == MDOC_It && n->tok == MDOC_Bl) {
931 if (blk != NULL) {
932 mandoc_vmsg(MANDOCERR_BLK_BROKEN,
933 mdoc->parse, line, ppos,
934 "It breaks %s",
935 mdoc_macronames[blk->tok]);
936 rew_pending(mdoc, blk);
937 }
938 break;
939 }
940
941 if (mdoc_macros[n->tok].flags & MDOC_EXPLICIT) {
942 switch (tok) {
943 case MDOC_Sh:
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 roff_elem_alloc(mdoc, line, ppos, MDOC_br);
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 = roff_head_alloc(mdoc, line, ppos, tok);
1026 rew_last(mdoc, head);
1027 body = roff_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 = roff_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 = roff_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 = roff_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, TOKEN_NONE, 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 = roff_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 = roff_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, roff_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 = roff_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 = roff_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 roff_block_alloc(mdoc, line, ppos, tok);
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 = roff_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 roff_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, roff_head_alloc(mdoc, line, ppos, tok));
1258 roff_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 case MDOC_Ns:
1287 case MDOC_Ux:
1288 maxargs = 0;
1289 break;
1290 case MDOC_Bx:
1291 case MDOC_Es:
1292 case MDOC_Xr:
1293 maxargs = 2;
1294 break;
1295 default:
1296 maxargs = 1;
1297 break;
1298 }
1299
1300 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1301
1302 state = -1;
1303 p = NULL;
1304 for (;;) {
1305 la = *pos;
1306 ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1307
1308 if (ac == ARGS_WORD && state == -1 &&
1309 ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
1310 mdoc_isdelim(p) == DELIM_OPEN) {
1311 dword(mdoc, line, la, p, DELIM_OPEN, 0);
1312 continue;
1313 }
1314
1315 if (state == -1 && tok != MDOC_In &&
1316 tok != MDOC_St && tok != MDOC_Xr) {
1317 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1318 state = 0;
1319 }
1320
1321 if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
1322 if (abs(state) < 2 && tok == MDOC_Pf)
1323 mandoc_vmsg(MANDOCERR_PF_SKIP,
1324 mdoc->parse, line, ppos, "Pf %s",
1325 p == NULL ? "at eol" : p);
1326 break;
1327 }
1328
1329 if (state == maxargs) {
1330 rew_elem(mdoc, tok);
1331 state = -2;
1332 }
1333
1334 ntok = (ac == ARGS_QWORD || (tok == MDOC_Pf && state == 0)) ?
1335 TOKEN_NONE : lookup(mdoc, tok, line, la, p);
1336
1337 if (ntok != TOKEN_NONE) {
1338 if (state >= 0) {
1339 rew_elem(mdoc, tok);
1340 state = -2;
1341 }
1342 mdoc_macro(mdoc, ntok, line, la, pos, buf);
1343 break;
1344 }
1345
1346 if (ac == ARGS_QWORD ||
1347 mdoc_macros[tok].flags & MDOC_IGNDELIM ||
1348 mdoc_isdelim(p) == DELIM_NONE) {
1349 if (state == -1) {
1350 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1351 state = 1;
1352 } else if (state >= 0)
1353 state++;
1354 } else if (state >= 0) {
1355 rew_elem(mdoc, tok);
1356 state = -2;
1357 }
1358
1359 dword(mdoc, line, la, p, DELIM_MAX,
1360 MDOC_JOIN & mdoc_macros[tok].flags);
1361 }
1362
1363 if (state == -1) {
1364 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1365 line, ppos, mdoc_macronames[tok]);
1366 return;
1367 }
1368
1369 if (state == 0 && tok == MDOC_Pf)
1370 append_delims(mdoc, line, pos, buf);
1371 if (state >= 0)
1372 rew_elem(mdoc, tok);
1373 if (nl)
1374 append_delims(mdoc, line, pos, buf);
1375 }
1376
1377 static void
1378 in_line_eoln(MACRO_PROT_ARGS)
1379 {
1380 struct roff_node *n;
1381 struct mdoc_arg *arg;
1382
1383 if ((tok == MDOC_Pp || tok == MDOC_Lp) &&
1384 ! (mdoc->flags & MDOC_SYNOPSIS)) {
1385 n = mdoc->last;
1386 if (mdoc->next == ROFF_NEXT_SIBLING)
1387 n = n->parent;
1388 if (n->tok == MDOC_Nm)
1389 rew_last(mdoc, n->parent);
1390 }
1391
1392 if (buf[*pos] == '\0' &&
1393 (tok == MDOC_Fd || mdoc_macronames[tok][0] == '%')) {
1394 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1395 line, ppos, mdoc_macronames[tok]);
1396 return;
1397 }
1398
1399 mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1400 mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1401 if (parse_rest(mdoc, tok, line, pos, buf))
1402 return;
1403 rew_elem(mdoc, tok);
1404 }
1405
1406 /*
1407 * The simplest argument parser available: Parse the remaining
1408 * words until the end of the phrase or line and return 0
1409 * or until the next macro, call that macro, and return 1.
1410 */
1411 static int
1412 parse_rest(struct roff_man *mdoc, int tok, int line, int *pos, char *buf)
1413 {
1414 int la;
1415
1416 for (;;) {
1417 la = *pos;
1418 if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1419 return 0;
1420 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1421 return 1;
1422 }
1423 }
1424
1425 static void
1426 ctx_synopsis(MACRO_PROT_ARGS)
1427 {
1428
1429 if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
1430 in_line(mdoc, tok, line, ppos, pos, buf);
1431 else if (tok == MDOC_Nm)
1432 blk_full(mdoc, tok, line, ppos, pos, buf);
1433 else {
1434 assert(tok == MDOC_Vt);
1435 blk_part_imp(mdoc, tok, line, ppos, pos, buf);
1436 }
1437 }
1438
1439 /*
1440 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1441 * They're unusual because they're basically free-form text until a
1442 * macro is encountered.
1443 */
1444 static void
1445 phrase_ta(MACRO_PROT_ARGS)
1446 {
1447 struct roff_node *body, *n;
1448
1449 /* Make sure we are in a column list or ignore this macro. */
1450
1451 body = NULL;
1452 for (n = mdoc->last; n != NULL; n = n->parent) {
1453 if (n->flags & MDOC_ENDED)
1454 continue;
1455 if (n->tok == MDOC_It && n->type == ROFFT_BODY)
1456 body = n;
1457 if (n->tok == MDOC_Bl)
1458 break;
1459 }
1460
1461 if (n == NULL || n->norm->Bl.type != LIST_column) {
1462 mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1463 line, ppos, "Ta");
1464 return;
1465 }
1466
1467 /* Advance to the next column. */
1468
1469 rew_last(mdoc, body);
1470 roff_body_alloc(mdoc, line, ppos, MDOC_It);
1471 parse_rest(mdoc, TOKEN_NONE, line, pos, buf);
1472 }