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