GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/mdoc_macro.c Lines: 567 572 99.1 %
Date: 2017-11-13 Branches: 456 489 93.3 %

Line Branch Exec Source
1
/*	$OpenBSD: mdoc_macro.c,v 1.181 2017/05/30 16:21:07 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 <sys/types.h>
19
20
#include <assert.h>
21
#include <ctype.h>
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <time.h>
26
27
#include "mandoc.h"
28
#include "roff.h"
29
#include "mdoc.h"
30
#include "libmandoc.h"
31
#include "roff_int.h"
32
#include "libmdoc.h"
33
34
static	void		blk_full(MACRO_PROT_ARGS);
35
static	void		blk_exp_close(MACRO_PROT_ARGS);
36
static	void		blk_part_exp(MACRO_PROT_ARGS);
37
static	void		blk_part_imp(MACRO_PROT_ARGS);
38
static	void		ctx_synopsis(MACRO_PROT_ARGS);
39
static	void		in_line_eoln(MACRO_PROT_ARGS);
40
static	void		in_line_argn(MACRO_PROT_ARGS);
41
static	void		in_line(MACRO_PROT_ARGS);
42
static	void		phrase_ta(MACRO_PROT_ARGS);
43
44
static	void		append_delims(struct roff_man *, int, int *, char *);
45
static	void		dword(struct roff_man *, int, int, const char *,
46
				enum mdelim, int);
47
static	int		find_pending(struct roff_man *, enum roff_tok,
48
				int, int, struct roff_node *);
49
static	int		lookup(struct roff_man *, int, int, int, const char *);
50
static	int		macro_or_word(MACRO_PROT_ARGS, int);
51
static	void		break_intermediate(struct roff_node *,
52
				struct roff_node *);
53
static	int		parse_rest(struct roff_man *, enum roff_tok,
54
				int, int *, char *);
55
static	enum roff_tok	rew_alt(enum roff_tok);
56
static	void		rew_elem(struct roff_man *, enum roff_tok);
57
static	void		rew_last(struct roff_man *, const struct roff_node *);
58
static	void		rew_pending(struct roff_man *,
59
				const struct roff_node *);
60
61
const	struct mdoc_macro __mdoc_macros[MDOC_MAX - MDOC_Dd] = {
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_argn, MDOC_CALLABLE | MDOC_PARSED |
78
			MDOC_IGNDELIM | MDOC_JOIN }, /* Ap */
79
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
80
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
81
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
82
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
83
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
84
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
85
	{ in_line_eoln, 0 }, /* Ex */
86
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
87
	{ in_line_eoln, 0 }, /* Fd */
88
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
89
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
90
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
91
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
92
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
93
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
94
	{ blk_full, MDOC_JOIN }, /* Nd */
95
	{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
96
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
97
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */
98
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
99
	{ in_line_eoln, 0 }, /* Rv */
100
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
101
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
102
	{ ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
103
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
104
	{ in_line_eoln, MDOC_JOIN }, /* %A */
105
	{ in_line_eoln, MDOC_JOIN }, /* %B */
106
	{ in_line_eoln, MDOC_JOIN }, /* %D */
107
	{ in_line_eoln, MDOC_JOIN }, /* %I */
108
	{ in_line_eoln, MDOC_JOIN }, /* %J */
109
	{ in_line_eoln, 0 }, /* %N */
110
	{ in_line_eoln, MDOC_JOIN }, /* %O */
111
	{ in_line_eoln, 0 }, /* %P */
112
	{ in_line_eoln, MDOC_JOIN }, /* %R */
113
	{ in_line_eoln, MDOC_JOIN }, /* %T */
114
	{ in_line_eoln, 0 }, /* %V */
115
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
116
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
117
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
118
			MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
119
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
120
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
121
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
122
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
123
	{ blk_full, MDOC_EXPLICIT }, /* Bf */
124
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
125
			MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
126
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
127
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
128
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
129
	{ in_line_eoln, 0 }, /* Db */
130
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
131
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
132
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
133
			MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
134
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
135
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
136
	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
137
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
138
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
139
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
140
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
141
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* No */
142
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
143
			MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
144
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
145
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
146
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
147
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
148
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
149
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
150
			MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
151
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
152
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
153
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
154
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
155
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
156
			MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
157
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
158
	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
159
	{ blk_full, MDOC_EXPLICIT }, /* Rs */
160
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
161
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
162
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
163
			MDOC_EXPLICIT | MDOC_JOIN }, /* So */
164
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
165
	{ in_line_argn, 0 }, /* Sm */
166
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
167
	{ in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
168
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
169
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
170
	{ blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
171
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
172
	{ blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
173
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
174
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
175
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
176
			MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
177
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
178
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
179
	{ blk_full, MDOC_EXPLICIT }, /* Bk */
180
	{ blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
181
	{ in_line_eoln, 0 }, /* Bt */
182
	{ in_line_eoln, 0 }, /* Hf */
183
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */
184
	{ in_line_eoln, 0 }, /* Ud */
185
	{ in_line, 0 }, /* Lb */
186
	{ in_line_eoln, 0 }, /* Lp */
187
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
188
	{ in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
189
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
190
	{ blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
191
			MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
192
	{ blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
193
			 MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
194
	{ in_line_eoln, MDOC_JOIN }, /* %C */
195
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */
196
	{ blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */
197
	{ in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
198
	{ in_line_eoln, MDOC_JOIN }, /* %Q */
199
	{ in_line_eoln, 0 }, /* %U */
200
	{ phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
201
};
202
const	struct mdoc_macro *const mdoc_macros = __mdoc_macros - MDOC_Dd;
203
204
205
/*
206
 * This is called at the end of parsing.  It must traverse up the tree,
207
 * closing out open [implicit] scopes.  Obviously, open explicit scopes
208
 * are errors.
209
 */
210
void
211
mdoc_endparse(struct roff_man *mdoc)
212
{
213
	struct roff_node *n;
214
215
	/* Scan for open explicit scopes. */
216
217
23015
	n = mdoc->last->flags & NODE_VALID ?
218
5732
	    mdoc->last->parent : mdoc->last;
219
220
46768
	for ( ; n; n = n->parent)
221

23554
		if (n->type == ROFFT_BLOCK &&
222
5931
		    mdoc_macros[n->tok].flags & MDOC_EXPLICIT)
223
72
			mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse,
224
36
			    n->line, n->pos, roff_name[n->tok]);
225
226
	/* Rewind to the first. */
227
228
5761
	rew_last(mdoc, mdoc->first);
229
5761
	mdoc_state_reset(mdoc);
230
5761
}
231
232
/*
233
 * Look up the macro at *p called by "from",
234
 * or as a line macro if from == TOKEN_NONE.
235
 */
236
static int
237
lookup(struct roff_man *mdoc, int from, int line, int ppos, const char *p)
238
{
239
	enum roff_tok	 res;
240
241
848492
	if (mdoc->flags & MDOC_PHRASEQF) {
242
532
		mdoc->flags &= ~MDOC_PHRASEQF;
243
532
		return TOKEN_NONE;
244
	}
245

830812
	if (from == TOKEN_NONE || mdoc_macros[from].flags & MDOC_PARSED) {
246
343610
		res = roffhash_find(mdoc->mdocmac, p, 0);
247
343610
		if (res != TOKEN_NONE) {
248
65433
			if (mdoc_macros[res].flags & MDOC_CALLABLE)
249
65430
				return res;
250
3
			mandoc_msg(MANDOCERR_MACRO_CALL,
251
3
			    mdoc->parse, line, ppos, p);
252
3
		}
253
	}
254
358284
	return TOKEN_NONE;
255
424246
}
256
257
/*
258
 * Rewind up to and including a specific node.
259
 */
260
static void
261
rew_last(struct roff_man *mdoc, const struct roff_node *to)
262
{
263
264
992056
	if (to->flags & NODE_VALID)
265
		return;
266
267
2903412
	while (mdoc->last != to) {
268
967804
		mdoc_state(mdoc, mdoc->last);
269
		mdoc->last->flags |= NODE_VALID | NODE_ENDED;
270
477891
		mdoc->last = mdoc->last->parent;
271
	}
272
	mdoc_state(mdoc, mdoc->last);
273
	mdoc->last->flags |= NODE_VALID | NODE_ENDED;
274
489913
	mdoc->next = ROFF_NEXT_SIBLING;
275
985941
}
276
277
/*
278
 * Rewind up to a specific block, including all blocks that broke it.
279
 */
280
static void
281
rew_pending(struct roff_man *mdoc, const struct roff_node *n)
282
{
283
284
190530
	for (;;) {
285
96042
		rew_last(mdoc, n);
286
287
96042
		if (mdoc->last == n) {
288
190128
			switch (n->type) {
289
			case ROFFT_HEAD:
290
1908
				roff_body_alloc(mdoc, n->line, n->pos,
291
954
				    n->tok);
292
954
				break;
293
			case ROFFT_BLOCK:
294
				break;
295
			default:
296
				return;
297
			}
298
94686
			if ( ! (n->flags & NODE_BROKEN))
299
				return;
300
		} else
301
			n = mdoc->last;
302
303
4367
		for (;;) {
304
4367
			if ((n = n->parent) == NULL)
305
				return;
306
307

7098
			if (n->type == ROFFT_BLOCK ||
308
2731
			    n->type == ROFFT_HEAD) {
309
2745
				if (n->flags & NODE_ENDED)
310
					break;
311
				else
312
					return;
313
			}
314
		}
315
	}
316
94488
}
317
318
/*
319
 * For a block closing macro, return the corresponding opening one.
320
 * Otherwise, return the macro itself.
321
 */
322
static enum roff_tok
323
rew_alt(enum roff_tok tok)
324
{
325




32998
	switch (tok) {
326
	case MDOC_Ac:
327
212
		return MDOC_Ao;
328
	case MDOC_Bc:
329
437
		return MDOC_Bo;
330
	case MDOC_Brc:
331
94
		return MDOC_Bro;
332
	case MDOC_Dc:
333
92
		return MDOC_Do;
334
	case MDOC_Ec:
335
220
		return MDOC_Eo;
336
	case MDOC_Ed:
337
2862
		return MDOC_Bd;
338
	case MDOC_Ef:
339
173
		return MDOC_Bf;
340
	case MDOC_Ek:
341
251
		return MDOC_Bk;
342
	case MDOC_El:
343
7278
		return MDOC_Bl;
344
	case MDOC_Fc:
345
2184
		return MDOC_Fo;
346
	case MDOC_Oc:
347
851
		return MDOC_Oo;
348
	case MDOC_Pc:
349
310
		return MDOC_Po;
350
	case MDOC_Qc:
351
46
		return MDOC_Qo;
352
	case MDOC_Re:
353
562
		return MDOC_Rs;
354
	case MDOC_Sc:
355
38
		return MDOC_So;
356
	case MDOC_Xc:
357
889
		return MDOC_Xo;
358
	default:
359
		return tok;
360
	}
361
16499
}
362
363
static void
364
rew_elem(struct roff_man *mdoc, enum roff_tok tok)
365
{
366
	struct roff_node *n;
367
368
453112
	n = mdoc->last;
369
226556
	if (n->type != ROFFT_ELEM)
370
191870
		n = n->parent;
371
226556
	assert(n->type == ROFFT_ELEM);
372
226556
	assert(tok == n->tok);
373
226556
	rew_last(mdoc, n);
374
226556
}
375
376
static void
377
break_intermediate(struct roff_node *n, struct roff_node *breaker)
378
{
379

4071
	if (n != breaker &&
380

3256
	    n->type != ROFFT_BLOCK && n->type != ROFFT_HEAD &&
381
2440
	    (n->type != ROFFT_BODY || n->end != ENDBODY_NOT))
382
880
		n = n->parent;
383
11230
	while (n != breaker) {
384
3986
		if ( ! (n->flags & NODE_VALID))
385
3984
			n->flags |= NODE_BROKEN;
386
3986
		n = n->parent;
387
	}
388
1629
}
389
390
/*
391
 * If there is an open sub-block of the target requiring
392
 * explicit close-out, postpone closing out the target until
393
 * the rew_pending() call closing out the sub-block.
394
 */
395
static int
396
find_pending(struct roff_man *mdoc, enum roff_tok tok, int line, int ppos,
397
	struct roff_node *target)
398
{
399
	struct roff_node	*n;
400
	int			 irc;
401
402
189208
	if (target->flags & NODE_VALID)
403
40
		return 0;
404
405
	irc = 0;
406

526119
	for (n = mdoc->last; n != NULL && n != target; n = n->parent) {
407
80809
		if (n->flags & NODE_ENDED)
408
			continue;
409

3375
		if (n->type == ROFFT_BLOCK &&
410
1119
		    mdoc_macros[n->tok].flags & MDOC_EXPLICIT) {
411
			irc = 1;
412
1119
			break_intermediate(mdoc->last, target);
413
1119
			if (target->type == ROFFT_HEAD)
414
969
				target->flags |= NODE_ENDED;
415
150
			else if ( ! (target->flags & NODE_ENDED)) {
416
96
				mandoc_vmsg(MANDOCERR_BLK_NEST,
417
96
				    mdoc->parse, line, ppos,
418
96
				    "%s breaks %s", roff_name[tok],
419
96
				    roff_name[n->tok]);
420
96
				mdoc_endbody_alloc(mdoc, line, ppos,
421
				    tok, target);
422
96
			}
423
		}
424
	}
425
94564
	return irc;
426
94604
}
427
428
/*
429
 * Allocate a word and check whether it's punctuation or not.
430
 * Punctuation consists of those tokens found in mdoc_isdelim().
431
 */
432
static void
433
dword(struct roff_man *mdoc, int line, int col, const char *p,
434
		enum mdelim d, int may_append)
435
{
436
437
865506
	if (d == DELIM_MAX)
438
258338
		d = mdoc_isdelim(p);
439
440

481444
	if (may_append &&
441
381596
	    ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) &&
442
324503
	    d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT &&
443
48691
	    mdoc_isdelim(mdoc->last->string) == DELIM_NONE) {
444
48522
		roff_word_append(mdoc, p);
445
48522
		return;
446
	}
447
448
384231
	roff_word_alloc(mdoc, line, col, p);
449
450
	/*
451
	 * If the word consists of a bare delimiter,
452
	 * flag the new node accordingly,
453
	 * unless doing so was vetoed by the invoking macro.
454
	 * Always clear the veto, it is only valid for one word.
455
	 */
456
457
384231
	if (d == DELIM_OPEN)
458
1384
		mdoc->last->flags |= NODE_DELIMO;
459

434407
	else if (d == DELIM_CLOSE &&
460
52064
	    ! (mdoc->flags & MDOC_NODELIMC) &&
461
51560
	    mdoc->last->parent->tok != MDOC_Fd)
462
51554
		mdoc->last->flags |= NODE_DELIMC;
463
384231
	mdoc->flags &= ~MDOC_NODELIMC;
464
816984
}
465
466
static void
467
append_delims(struct roff_man *mdoc, int line, int *pos, char *buf)
468
{
469
488476
	char		*p;
470
	int		 la;
471
472
244238
	if (buf[*pos] == '\0')
473
197453
		return;
474
475
94589
	for (;;) {
476
94589
		la = *pos;
477
94589
		if (mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p) ==
478
		    ARGS_EOLN)
479
			break;
480
47804
		dword(mdoc, line, la, p, DELIM_MAX, 1);
481
482
		/*
483
		 * If we encounter end-of-sentence symbols, then trigger
484
		 * the double-space.
485
		 *
486
		 * XXX: it's easy to allow this to propagate outward to
487
		 * the last symbol, such that `. )' will cause the
488
		 * correct double-spacing.  However, (1) groff isn't
489
		 * smart enough to do this and (2) it would require
490
		 * knowing which symbols break this behaviour, for
491
		 * example, `.  ;' shouldn't propagate the double-space.
492
		 */
493
494
47804
		if (mandoc_eos(p, strlen(p)))
495
17227
			mdoc->last->flags |= NODE_EOS;
496
	}
497
291023
}
498
499
/*
500
 * Parse one word.
501
 * If it is a macro, call it and return 1.
502
 * Otherwise, allocate it and return 0.
503
 */
504
static int
505
macro_or_word(MACRO_PROT_ARGS, int parsed)
506
{
507
	char		*p;
508
	int		 ntok;
509
510
393006
	p = buf + ppos;
511
	ntok = TOKEN_NONE;
512
196503
	if (*p == '"')
513
4990
		p++;
514

382425
	else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT))
515
190903
		ntok = lookup(mdoc, tok, line, ppos, p);
516
517
196503
	if (ntok == TOKEN_NONE) {
518
447689
		dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE ||
519
141241
		    mdoc_macros[tok].flags & MDOC_JOIN);
520
153224
		return 0;
521
	} else {
522

77368
		if (tok != TOKEN_NONE &&
523
34089
		    mdoc_macros[tok].fp == in_line_eoln)
524
			rew_elem(mdoc, tok);
525
43279
		mdoc_macro(mdoc, ntok, line, ppos, pos, buf);
526
43279
		if (tok == TOKEN_NONE)
527
9190
			append_delims(mdoc, line, pos, buf);
528
43279
		return 1;
529
	}
530
196503
}
531
532
/*
533
 * Close out block partial/full explicit.
534
 */
535
static void
536
blk_exp_close(MACRO_PROT_ARGS)
537
{
538
	struct roff_node *body;		/* Our own body. */
539
	struct roff_node *endbody;	/* Our own end marker. */
540
	struct roff_node *itblk;	/* An It block starting later. */
541
	struct roff_node *later;	/* A sub-block starting later. */
542
	struct roff_node *n;		/* Search back to our block. */
543
	struct roff_node *target;	/* For find_pending(). */
544
545
	int		 j, lastarg, maxargs, nl, pending;
546
	enum margserr	 ac;
547
	enum roff_tok	 atok, ntok;
548
33249
	char		*p;
549
550
16750
	nl = MDOC_NEWLINE & mdoc->flags;
551
552
16750
	switch (tok) {
553
	case MDOC_Ec:
554
		maxargs = 1;
555
220
		break;
556
	case MDOC_Ek:
557
251
		mdoc->flags &= ~MDOC_KEEP;
558
		/* FALLTHROUGH */
559
	default:
560
		maxargs = 0;
561
16279
		break;
562
	}
563
564
	/* Search backwards for the beginning of our own body. */
565
566
16499
	atok = rew_alt(tok);
567
	body = NULL;
568
96324
	for (n = mdoc->last; n; n = n->parent) {
569

96713
		if (n->flags & NODE_ENDED || n->tok != atok ||
570
32932
		    n->type != ROFFT_BODY || n->end != ENDBODY_NOT)
571
			continue;
572
		body = n;
573
16466
		break;
574
	}
575
576
	/*
577
	 * Search backwards for beginnings of blocks,
578
	 * both of our own and of pending sub-blocks.
579
	 */
580
581
	endbody = itblk = later = NULL;
582
129256
	for (n = mdoc->last; n; n = n->parent) {
583
129190
		if (n->flags & NODE_ENDED)
584
			continue;
585
586
		/*
587
		 * Mismatching end macros can never break anything
588
		 * and we only care about the breaking of BLOCKs.
589
		 */
590
591
112984
		if (body == NULL || n->type != ROFFT_BLOCK)
592
			continue;
593
594
		/*
595
		 * SYNOPSIS name blocks can not be broken themselves,
596
		 * but they do get broken together with a broken child.
597
		 */
598
599
24170
		if (n->tok == MDOC_Nm) {
600
31
			if (later != NULL)
601
9
				n->flags |= NODE_BROKEN | NODE_ENDED;
602
			continue;
603
		}
604
605
24139
		if (n->tok == MDOC_It) {
606
			itblk = n;
607
7136
			continue;
608
		}
609
610
17003
		if (atok == n->tok) {
611
612
			/*
613
			 * Found the start of our own block.
614
			 * When there is no pending sub block,
615
			 * just proceed to closing out.
616
			 */
617
618
16466
			if (later == NULL ||
619
453
			    (tok == MDOC_El && itblk == NULL))
620
				break;
621
622
			/*
623
			 * When there is a pending sub block, postpone
624
			 * closing out the current block until the
625
			 * rew_pending() closing out the sub-block.
626
			 * Mark the place where the formatting - but not
627
			 * the scope - of the current block ends.
628
			 */
629
630
906
			mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse,
631
			    line, ppos, "%s breaks %s",
632
453
			    roff_name[atok], roff_name[later->tok]);
633
634
453
			endbody = mdoc_endbody_alloc(mdoc, line, ppos,
635
			    atok, body);
636
637
453
			if (tok == MDOC_El)
638
54
				itblk->flags |= NODE_ENDED | NODE_BROKEN;
639
640
			/*
641
			 * If a block closing macro taking arguments
642
			 * breaks another block, put the arguments
643
			 * into the end marker.
644
			 */
645
646
453
			if (maxargs)
647
48
				mdoc->next = ROFF_NEXT_CHILD;
648
			break;
649
		}
650
651
		/*
652
		 * Explicit blocks close out description lines, but
653
		 * even those can get broken together with a child.
654
		 */
655
656
537
		if (n->tok == MDOC_Nd) {
657
27
			if (later != NULL)
658
9
				n->flags |= NODE_BROKEN | NODE_ENDED;
659
			else
660
18
				rew_last(mdoc, n);
661
			continue;
662
		}
663
664
		/* Breaking an open sub block. */
665
666
510
		break_intermediate(mdoc->last, body);
667
510
		n->flags |= NODE_BROKEN;
668
510
		if (later == NULL)
669
453
			later = n;
670
	}
671
672
16499
	if (body == NULL) {
673
66
		mandoc_msg(MANDOCERR_BLK_NOTOPEN, mdoc->parse,
674
33
		    line, ppos, roff_name[tok]);
675
33
		if (maxargs && endbody == NULL) {
676
			/*
677
			 * Stray .Ec without previous .Eo:
678
			 * Break the output line, keep the arguments.
679
			 */
680
9
			roff_elem_alloc(mdoc, line, ppos, ROFF_br);
681
9
			rew_elem(mdoc, ROFF_br);
682
9
		}
683
16466
	} else if (endbody == NULL) {
684
16013
		rew_last(mdoc, body);
685
16013
		if (maxargs)
686
163
			mdoc_tail_alloc(mdoc, line, ppos, atok);
687
	}
688
689
16499
	if ( ! (mdoc_macros[tok].flags & MDOC_PARSED)) {
690
11126
		if (buf[*pos] != '\0')
691
51
			mandoc_vmsg(MANDOCERR_ARG_SKIP,
692
51
			    mdoc->parse, line, ppos,
693
51
			    "%s %s", roff_name[tok],
694
			    buf + *pos);
695
11126
		if (endbody == NULL && n != NULL)
696
11054
			rew_pending(mdoc, n);
697
11126
		return;
698
	}
699
700
5373
	if (endbody != NULL)
701
381
		n = endbody;
702
703
	ntok = TOKEN_NONE;
704
6092
	for (j = 0; ; j++) {
705
6092
		lastarg = *pos;
706
707
6092
		if (j == maxargs && n != NULL)
708
5262
			rew_last(mdoc, n);
709
710
6092
		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
711
6092
		if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
712
			break;
713
714
1585
		ntok = lookup(mdoc, tok, line, lastarg, p);
715
716
1585
		if (ntok == TOKEN_NONE) {
717
1438
			dword(mdoc, line, lastarg, p, DELIM_MAX,
718
719
			    MDOC_JOIN & mdoc_macros[tok].flags);
719
			continue;
720
		}
721
722
866
		if (n != NULL)
723
866
			rew_last(mdoc, n);
724
866
		mdoc->flags &= ~MDOC_NEWLINE;
725
866
		mdoc_macro(mdoc, ntok, line, lastarg, pos, buf);
726
866
		break;
727
	}
728
729
5373
	if (n != NULL) {
730
		pending = 0;
731

6206
		if (ntok != TOKEN_NONE && n->flags & NODE_BROKEN) {
732
			target = n;
733
60
			do
734
120
				target = target->parent;
735
60
			while ( ! (target->flags & NODE_ENDED));
736
60
			pending = find_pending(mdoc, ntok, line, ppos, target);
737
60
		}
738
5340
		if ( ! pending)
739
5313
			rew_pending(mdoc, n);
740
	}
741
5373
	if (nl)
742
4327
		append_delims(mdoc, line, pos, buf);
743
21872
}
744
745
static void
746
in_line(MACRO_PROT_ARGS)
747
{
748
	int		 la, scope, cnt, firstarg, mayopen, nc, nl;
749
	enum roff_tok	 ntok;
750
	enum margserr	 ac;
751
	enum mdelim	 d;
752
307846
	struct mdoc_arg	*arg;
753
153923
	char		*p;
754
755
153923
	nl = MDOC_NEWLINE & mdoc->flags;
756
757
	/*
758
	 * Whether we allow ignored elements (those without content,
759
	 * usually because of reserved words) to squeak by.
760
	 */
761
762

153923
	switch (tok) {
763
	case MDOC_An:
764
	case MDOC_Ar:
765
	case MDOC_Fl:
766
	case MDOC_Mt:
767
	case MDOC_Nm:
768
	case MDOC_Pa:
769
		nc = 1;
770
59439
		break;
771
	default:
772
		nc = 0;
773
94484
		break;
774
	}
775
776
153923
	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
777
778
	d = DELIM_NONE;
779
	firstarg = 1;
780
	mayopen = 1;
781
328140
	for (cnt = scope = 0;; ) {
782
328140
		la = *pos;
783
328140
		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
784
785
		/*
786
		 * At the end of a macro line,
787
		 * opening delimiters do not suppress spacing.
788
		 */
789
790
328140
		if (ac == ARGS_EOLN) {
791
109822
			if (d == DELIM_OPEN)
792
192
				mdoc->last->flags &= ~NODE_DELIMO;
793
			break;
794
		}
795
796
		/*
797
		 * The rest of the macro line is only punctuation,
798
		 * to be handled by append_delims().
799
		 * If there were no other arguments,
800
		 * do not allow the first one to suppress spacing,
801
		 * even if it turns out to be a closing one.
802
		 */
803
804
218318
		if (ac == ARGS_PUNCT) {
805

27110
			if (cnt == 0 && (nc == 0 || tok == MDOC_An))
806
504
				mdoc->flags |= MDOC_NODELIMC;
807
			break;
808
		}
809
810
556556
		ntok = (tok == MDOC_Fn && !cnt) ?
811
172442
		    TOKEN_NONE : lookup(mdoc, tok, line, la, p);
812
813
		/*
814
		 * In this case, we've located a submacro and must
815
		 * execute it.  Close out scope, if open.  If no
816
		 * elements have been generated, either create one (nc)
817
		 * or raise a warning.
818
		 */
819
820
192057
		if (ntok != TOKEN_NONE) {
821
17840
			if (scope)
822
11292
				rew_elem(mdoc, tok);
823
17840
			if (nc && ! cnt) {
824
227
				mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
825
227
				rew_last(mdoc, mdoc->last);
826
17840
			} else if ( ! nc && ! cnt) {
827
672
				mdoc_argv_free(arg);
828
672
				mandoc_msg(MANDOCERR_MACRO_EMPTY,
829
672
				    mdoc->parse, line, ppos,
830
672
				    roff_name[tok]);
831
672
			}
832
17840
			mdoc_macro(mdoc, ntok, line, la, pos, buf);
833
17840
			if (nl)
834
4518
				append_delims(mdoc, line, pos, buf);
835
17840
			return;
836
		}
837
838
		/*
839
		 * Handle punctuation.  Set up our scope, if a word;
840
		 * rewind the scope, if a delimiter; then append the word.
841
		 */
842
843
174217
		if ((d = mdoc_isdelim(p)) != DELIM_NONE) {
844
			/*
845
			 * If we encounter closing punctuation, no word
846
			 * has been emitted, no scope is open, and we're
847
			 * allowed to have an empty element, then start
848
			 * a new scope.
849
			 */
850
6112
			if ((d == DELIM_CLOSE ||
851
9520
			     (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
852
10872
			    !cnt && !scope && nc && mayopen) {
853
453
				mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
854
				scope = 1;
855
453
				cnt++;
856
453
				if (tok == MDOC_Nm)
857
108
					mayopen = 0;
858
			}
859
			/*
860
			 * Close out our scope, if one is open, before
861
			 * any punctuation.
862
			 */
863
6112
			if (scope && tok != MDOC_Lk) {
864
3487
				rew_elem(mdoc, tok);
865
				scope = 0;
866
3487
				if (tok == MDOC_Fn)
867
12
					mayopen = 0;
868
			}
869
168105
		} else if (mayopen && !scope) {
870
146495
			mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
871
			scope = 1;
872
146495
			cnt++;
873
146495
		}
874
875
348434
		dword(mdoc, line, la, p, d,
876
174217
		    mdoc_macros[tok].flags & MDOC_JOIN);
877
878
		/*
879
		 * If the first argument is a closing delimiter,
880
		 * do not suppress spacing before it.
881
		 */
882
883
174217
		if (firstarg && d == DELIM_CLOSE && !nc)
884
1188
			mdoc->last->flags &= ~NODE_DELIMC;
885
		firstarg = 0;
886
887
		/*
888
		 * `Fl' macros have their scope re-opened with each new
889
		 * word so that the `-' can be added to each one without
890
		 * having to parse out spaces.
891
		 */
892
174217
		if (scope && tok == MDOC_Fl) {
893
13622
			rew_elem(mdoc, tok);
894
			scope = 0;
895
13622
		}
896
	}
897
898
136083
	if (scope && tok != MDOC_Lk) {
899
118378
		rew_elem(mdoc, tok);
900
		scope = 0;
901
118378
	}
902
903
	/*
904
	 * If no elements have been collected and we're allowed to have
905
	 * empties (nc), open a scope and close it out.  Otherwise,
906
	 * raise a warning.
907
	 */
908
909
136083
	if ( ! cnt) {
910
9137
		if (nc) {
911
8189
			mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
912
8189
			rew_last(mdoc, mdoc->last);
913
8189
		} else {
914
948
			mdoc_argv_free(arg);
915
1896
			mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
916
948
			    line, ppos, roff_name[tok]);
917
		}
918
	}
919
136083
	if (nl)
920
104855
		append_delims(mdoc, line, pos, buf);
921
136083
	if (scope)
922
169
		rew_elem(mdoc, tok);
923
290006
}
924
925
static void
926
blk_full(MACRO_PROT_ARGS)
927
{
928
166948
	int		  la, nl, parsed;
929
83474
	struct mdoc_arg	 *arg;
930
	struct roff_node *blk; /* Our own or a broken block. */
931
	struct roff_node *head; /* Our own head. */
932
	struct roff_node *body; /* Our own body. */
933
	struct roff_node *n;
934
	enum margserr	  ac, lac;
935
83474
	char		 *p;
936
937
83474
	nl = MDOC_NEWLINE & mdoc->flags;
938
939

88228
	if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) {
940
48
		mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
941
24
		    line, ppos, roff_name[tok]);
942
24
		return;
943
	}
944
945
83450
	if ( ! (mdoc_macros[tok].flags & MDOC_EXPLICIT)) {
946
947
		/* Here, tok is one of Sh Ss Nm Nd It. */
948
949
		blk = NULL;
950
628856
		for (n = mdoc->last; n != NULL; n = n->parent) {
951
289148
			if (n->flags & NODE_ENDED) {
952
60264
				if ( ! (n->flags & NODE_VALID))
953
36
					n->flags |= NODE_BROKEN;
954
				continue;
955
			}
956
228884
			if (n->type != ROFFT_BLOCK)
957
				continue;
958
959

168355
			if (tok == MDOC_It && n->tok == MDOC_Bl) {
960
36774
				if (blk != NULL) {
961
48
					mandoc_vmsg(MANDOCERR_BLK_BROKEN,
962
48
					    mdoc->parse, line, ppos,
963
					    "It breaks %s",
964
48
					    roff_name[blk->tok]);
965
48
					rew_pending(mdoc, blk);
966
48
				}
967
				break;
968
			}
969
970
65028
			if (mdoc_macros[n->tok].flags & MDOC_EXPLICIT) {
971

48273
				switch (tok) {
972
				case MDOC_Sh:
973
				case MDOC_Ss:
974
54
					mandoc_vmsg(MANDOCERR_BLK_BROKEN,
975
54
					    mdoc->parse, line, ppos,
976
54
					    "%s breaks %s", roff_name[tok],
977
54
					    roff_name[n->tok]);
978
54
					rew_pending(mdoc, n);
979
54
					n = mdoc->last;
980
54
					continue;
981
				case MDOC_It:
982
					/* Delay in case it's astray. */
983
					blk = n;
984
117
					continue;
985
				default:
986
					break;
987
				}
988
				break;
989
			}
990
991
			/* Here, n is one of Sh Ss Nm Nd It. */
992
993

103924
			if (tok != MDOC_Sh && (n->tok == MDOC_Sh ||
994

60836
			    (tok != MDOC_Ss && (n->tok == MDOC_Ss ||
995
30391
			     (tok != MDOC_It && n->tok == MDOC_It)))))
996
				break;
997
998
			/* Item breaking an explicit block. */
999
1000
56864
			if (blk != NULL) {
1001
57
				mandoc_vmsg(MANDOCERR_BLK_BROKEN,
1002
57
				    mdoc->parse, line, ppos,
1003
57
				    "It breaks %s", roff_name[blk->tok]);
1004
57
				rew_pending(mdoc, blk);
1005
				blk = NULL;
1006
57
			}
1007
1008
			/* Close out prior implicit scopes. */
1009
1010
56864
			rew_pending(mdoc, n);
1011
56864
		}
1012
1013
		/* Skip items outside lists. */
1014
1015

143685
		if (tok == MDOC_It && (n == NULL || n->tok != MDOC_Bl)) {
1016
90
			mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
1017
45
			    line, ppos, "It %s", buf + *pos);
1018
45
			roff_elem_alloc(mdoc, line, ppos, ROFF_br);
1019
45
			rew_elem(mdoc, ROFF_br);
1020
45
			return;
1021
		}
1022
	}
1023
1024
	/*
1025
	 * This routine accommodates implicitly- and explicitly-scoped
1026
	 * macro openings.  Implicit ones first close out prior scope
1027
	 * (seen above).  Delay opening the head until necessary to
1028
	 * allow leading punctuation to print.  Special consideration
1029
	 * for `It -column', which has phrase-part syntax instead of
1030
	 * regular child nodes.
1031
	 */
1032
1033
83405
	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1034
83405
	blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg);
1035
	head = body = NULL;
1036
1037
	/*
1038
	 * Exception: Heads of `It' macros in `-diag' lists are not
1039
	 * parsed, even though `It' macros in general are parsed.
1040
	 */
1041
1042
83405
	parsed = tok != MDOC_It ||
1043
156953
	    mdoc->last->parent->tok != MDOC_Bl ||
1044
36774
	    mdoc->last->parent->norm->Bl.type != LIST_diag;
1045
1046
	/*
1047
	 * The `Nd' macro has all arguments in its body: it's a hybrid
1048
	 * of block partial-explicit and full-implicit.  Stupid.
1049
	 */
1050
1051
83405
	if (tok == MDOC_Nd) {
1052
5740
		head = roff_head_alloc(mdoc, line, ppos, tok);
1053
5740
		rew_last(mdoc, head);
1054
5740
		body = roff_body_alloc(mdoc, line, ppos, tok);
1055
5740
	}
1056
1057
83405
	if (tok == MDOC_Bk)
1058
260
		mdoc->flags |= MDOC_KEEP;
1059
1060
	ac = ARGS_EOLN;
1061
157996
	for (;;) {
1062
1063
		/*
1064
		 * If we are right after a tab character,
1065
		 * do not parse the first word for macros.
1066
		 */
1067
1068
162645
		if (mdoc->flags & MDOC_PHRASEQN) {
1069
334
			mdoc->flags &= ~MDOC_PHRASEQN;
1070
334
			mdoc->flags |= MDOC_PHRASEQF;
1071
334
		}
1072
1073
162645
		la = *pos;
1074
		lac = ac;
1075
162645
		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1076
162645
		if (ac == ARGS_EOLN) {
1077

65137
			if (lac != ARGS_PHRASE ||
1078
4306
			    ! (mdoc->flags & MDOC_PHRASEQF))
1079
				break;
1080
1081
			/*
1082
			 * This line ends in a tab; start the next
1083
			 * column now, with a leading blank.
1084
			 */
1085
1086
9
			if (body != NULL)
1087
9
				rew_last(mdoc, body);
1088
9
			body = roff_body_alloc(mdoc, line, ppos, tok);
1089
9
			roff_word_alloc(mdoc, line, ppos, "\\&");
1090
9
			break;
1091
		}
1092
1093
101814
		if (tok == MDOC_Bd || tok == MDOC_Bk) {
1094
42
			mandoc_vmsg(MANDOCERR_ARG_EXCESS,
1095
42
			    mdoc->parse, line, la, "%s ... %s",
1096
42
			    roff_name[tok], buf + la);
1097
42
			break;
1098
		}
1099
101772
		if (tok == MDOC_Rs) {
1100
48
			mandoc_vmsg(MANDOCERR_ARG_SKIP, mdoc->parse,
1101
24
			    line, la, "Rs %s", buf + la);
1102
24
			break;
1103
		}
1104
101748
		if (ac == ARGS_PUNCT)
1105
			break;
1106
1107
		/*
1108
		 * Emit leading punctuation (i.e., punctuation before
1109
		 * the ROFFT_HEAD) for non-phrase types.
1110
		 */
1111
1112

261681
		if (head == NULL &&
1113
101748
		    ac != ARGS_PHRASE &&
1114
58185
		    mdoc_isdelim(p) == DELIM_OPEN) {
1115
			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1116
			continue;
1117
		}
1118
1119
		/* Open a head if one hasn't been opened. */
1120
1121
101748
		if (head == NULL)
1122
62491
			head = roff_head_alloc(mdoc, line, ppos, tok);
1123
1124
101748
		if (ac == ARGS_PHRASE) {
1125
1126
			/*
1127
			 * If we haven't opened a body yet, rewind the
1128
			 * head; if we have, rewind that instead.
1129
			 */
1130
1131
4649
			rew_last(mdoc, body == NULL ? head : body);
1132
4649
			body = roff_body_alloc(mdoc, line, ppos, tok);
1133
1134
			/* Process to the tab or to the end of the line. */
1135
1136
4649
			mdoc->flags |= MDOC_PHRASE;
1137
4649
			parse_rest(mdoc, TOKEN_NONE, line, &la, buf);
1138
4649
			mdoc->flags &= ~MDOC_PHRASE;
1139
1140
			/* There may have been `Ta' macros. */
1141
1142
26580
			while (body->next != NULL)
1143
				body = body->next;
1144
4649
			continue;
1145
		}
1146
1147
97099
		if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed))
1148
			break;
1149
	}
1150
1151
83405
	if (blk->flags & NODE_VALID)
1152
9
		return;
1153
83396
	if (head == NULL)
1154
15174
		head = roff_head_alloc(mdoc, line, ppos, tok);
1155
83396
	if (nl && tok != MDOC_Bd && tok != MDOC_Bl && tok != MDOC_Rs)
1156
72610
		append_delims(mdoc, line, pos, buf);
1157
83396
	if (body != NULL)
1158
		goto out;
1159
73350
	if (find_pending(mdoc, tok, line, ppos, head))
1160
960
		return;
1161
1162
	/* Close out scopes to remain in a consistent state. */
1163
1164
72390
	rew_last(mdoc, head);
1165
72390
	body = roff_body_alloc(mdoc, line, ppos, tok);
1166
out:
1167
82436
	if (mdoc->flags & MDOC_FREECOL) {
1168
30
		rew_last(mdoc, body);
1169
30
		rew_last(mdoc, blk);
1170
30
		mdoc->flags &= ~MDOC_FREECOL;
1171
30
	}
1172
165910
}
1173
1174
static void
1175
blk_part_imp(MACRO_PROT_ARGS)
1176
{
1177
	int		  la, nl;
1178
	enum margserr	  ac;
1179
42388
	char		 *p;
1180
	struct roff_node *blk; /* saved block context */
1181
	struct roff_node *body; /* saved body context */
1182
	struct roff_node *n;
1183
1184
21194
	nl = MDOC_NEWLINE & mdoc->flags;
1185
1186
	/*
1187
	 * A macro that spans to the end of the line.  This is generally
1188
	 * (but not necessarily) called as the first macro.  The block
1189
	 * has a head as the immediate child, which is always empty,
1190
	 * followed by zero or more opening punctuation nodes, then the
1191
	 * body (which may be empty, depending on the macro), then zero
1192
	 * or more closing punctuation nodes.
1193
	 */
1194
1195
21194
	blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL);
1196
21194
	rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1197
1198
	/*
1199
	 * Open the body scope "on-demand", that is, after we've
1200
	 * processed all our the leading delimiters (open parenthesis,
1201
	 * etc.).
1202
	 */
1203
1204
38300
	for (body = NULL; ; ) {
1205
38412
		la = *pos;
1206
38412
		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1207
38412
		if (ac == ARGS_EOLN || ac == ARGS_PUNCT)
1208
			break;
1209
1210

48866
		if (body == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
1211
112
			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1212
112
			continue;
1213
		}
1214
1215
27610
		if (body == NULL)
1216
21032
			body = roff_body_alloc(mdoc, line, ppos, tok);
1217
1218
27610
		if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1219
			break;
1220
	}
1221
21194
	if (body == NULL)
1222
162
		body = roff_body_alloc(mdoc, line, ppos, tok);
1223
1224
21194
	if (find_pending(mdoc, tok, line, ppos, body))
1225
96
		return;
1226
1227
21098
	rew_last(mdoc, body);
1228
21098
	if (nl)
1229
16216
		append_delims(mdoc, line, pos, buf);
1230
21098
	rew_pending(mdoc, blk);
1231
1232
	/* Move trailing .Ns out of scope. */
1233
1234

82083
	for (n = body->child; n && n->next; n = n->next)
1235
		/* Do nothing. */ ;
1236

42034
	if (n && n->tok == MDOC_Ns)
1237
49
		mdoc_node_relink(mdoc, n);
1238
42292
}
1239
1240
static void
1241
blk_part_exp(MACRO_PROT_ARGS)
1242
{
1243
	int		  la, nl;
1244
	enum margserr	  ac;
1245
	struct roff_node *head; /* keep track of head */
1246
6504
	char		 *p;
1247
1248
3252
	nl = MDOC_NEWLINE & mdoc->flags;
1249
1250
	/*
1251
	 * The opening of an explicit macro having zero or more leading
1252
	 * punctuation nodes; a head with optional single element (the
1253
	 * case of `Eo'); and a body that may be empty.
1254
	 */
1255
1256
3252
	roff_block_alloc(mdoc, line, ppos, tok);
1257
	head = NULL;
1258
4940
	for (;;) {
1259
5137
		la = *pos;
1260
5137
		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1261
5137
		if (ac == ARGS_PUNCT || ac == ARGS_EOLN)
1262
			break;
1263
1264
		/* Flush out leading punctuation. */
1265
1266

5029
		if (head == NULL && mdoc_isdelim(p) == DELIM_OPEN) {
1267
61
			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1268
61
			continue;
1269
		}
1270
1271
2901
		if (head == NULL) {
1272
2006
			head = roff_head_alloc(mdoc, line, ppos, tok);
1273
2006
			if (tok == MDOC_Eo)  /* Not parsed. */
1274
136
				dword(mdoc, line, la, p, DELIM_MAX, 0);
1275
2006
			rew_last(mdoc, head);
1276
2006
			roff_body_alloc(mdoc, line, ppos, tok);
1277
2006
			if (tok == MDOC_Eo)
1278
136
				continue;
1279
		}
1280
1281
2765
		if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1282
			break;
1283
	}
1284
1285
	/* Clean-up to leave in a consistent state. */
1286
1287
3252
	if (head == NULL) {
1288
1246
		rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok));
1289
1246
		roff_body_alloc(mdoc, line, ppos, tok);
1290
1246
	}
1291
3252
	if (nl)
1292
1623
		append_delims(mdoc, line, pos, buf);
1293
3252
}
1294
1295
static void
1296
in_line_argn(MACRO_PROT_ARGS)
1297
{
1298
71094
	struct mdoc_arg	*arg;
1299
35547
	char		*p;
1300
	enum margserr	 ac;
1301
	enum roff_tok	 ntok;
1302
	int		 state; /* arg#; -1: not yet open; -2: closed */
1303
	int		 la, maxargs, nl;
1304
1305
35547
	nl = mdoc->flags & MDOC_NEWLINE;
1306
1307
	/*
1308
	 * A line macro that has a fixed number of arguments (maxargs).
1309
	 * Only open the scope once the first non-leading-punctuation is
1310
	 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1311
	 * keep it open until the maximum number of arguments are
1312
	 * exhausted.
1313
	 */
1314
1315

35547
	switch (tok) {
1316
	case MDOC_Ap:
1317
	case MDOC_Ns:
1318
	case MDOC_Ux:
1319
		maxargs = 0;
1320
3595
		break;
1321
	case MDOC_Bx:
1322
	case MDOC_Es:
1323
	case MDOC_Xr:
1324
		maxargs = 2;
1325
24617
		break;
1326
	default:
1327
		maxargs = 1;
1328
7335
		break;
1329
	}
1330
1331
35547
	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1332
1333
	state = -1;
1334
35547
	p = NULL;
1335
92002
	for (;;) {
1336
92027
		la = *pos;
1337
92027
		ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1338
1339

122359
		if (ac == ARGS_WORD && state == -1 &&
1340
34244
		    ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) &&
1341
30332
		    mdoc_isdelim(p) == DELIM_OPEN) {
1342
25
			dword(mdoc, line, la, p, DELIM_OPEN, 0);
1343
25
			continue;
1344
		}
1345
1346
276006
		if (state == -1 && tok != MDOC_In &&
1347
184004
		    tok != MDOC_St && tok != MDOC_Xr) {
1348
8685
			mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1349
			state = 0;
1350
8685
		}
1351
1352
92002
		if (ac == ARGS_PUNCT || ac == ARGS_EOLN) {
1353
32102
			if (abs(state) < 2 && tok == MDOC_Pf)
1354
36
				mandoc_vmsg(MANDOCERR_PF_SKIP,
1355
36
				    mdoc->parse, line, ppos, "Pf %s",
1356
36
				    p == NULL ? "at eol" : p);
1357
			break;
1358
		}
1359
1360
59900
		if (state == maxargs) {
1361
4222
			rew_elem(mdoc, tok);
1362
			state = -2;
1363
4222
		}
1364
1365
179116
		ntok = (tok == MDOC_Pf && state == 0) ?
1366
59316
		    TOKEN_NONE : lookup(mdoc, tok, line, la, p);
1367
1368
59900
		if (ntok != TOKEN_NONE) {
1369
3445
			if (state >= 0) {
1370
104
				rew_elem(mdoc, tok);
1371
				state = -2;
1372
104
			}
1373
3445
			mdoc_macro(mdoc, ntok, line, la, pos, buf);
1374
3445
			break;
1375
		}
1376
1377

110855
		if (mdoc_macros[tok].flags & MDOC_IGNDELIM ||
1378
54400
		    mdoc_isdelim(p) == DELIM_NONE) {
1379
56396
			if (state == -1) {
1380
26788
				mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1381
				state = 1;
1382
56396
			} else if (state >= 0)
1383
27916
				state++;
1384
59
		} else if (state >= 0) {
1385
15
			rew_elem(mdoc, tok);
1386
			state = -2;
1387
15
		}
1388
1389
112910
		dword(mdoc, line, la, p, DELIM_MAX,
1390
56455
		    mdoc_macros[tok].flags & MDOC_JOIN);
1391
	}
1392
1393
35547
	if (state == -1) {
1394
148
		mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1395
74
		    line, ppos, roff_name[tok]);
1396
74
		return;
1397
	}
1398
1399
35473
	if (state == 0 && tok == MDOC_Pf)
1400
24
		append_delims(mdoc, line, pos, buf);
1401
35473
	if (state >= 0)
1402
31132
		rew_elem(mdoc, tok);
1403
35473
	if (nl)
1404
30875
		append_delims(mdoc, line, pos, buf);
1405
71020
}
1406
1407
static void
1408
in_line_eoln(MACRO_PROT_ARGS)
1409
{
1410
	struct roff_node	*n;
1411
88210
	struct mdoc_arg		*arg;
1412
1413

67596
	if ((tok == MDOC_Pp || tok == MDOC_Lp) &&
1414
23491
	    ! (mdoc->flags & MDOC_SYNOPSIS)) {
1415
23243
		n = mdoc->last;
1416
23243
		if (mdoc->next == ROFF_NEXT_SIBLING)
1417
23104
			n = n->parent;
1418
23243
		if (n->tok == MDOC_Nm)
1419
43
			rew_last(mdoc, n->parent);
1420
	}
1421
1422

73399
	if (buf[*pos] == '\0' &&
1423
58612
	    (tok == MDOC_Fd || *roff_name[tok] == '%')) {
1424
48
		mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1425
24
		    line, ppos, roff_name[tok]);
1426
24
		return;
1427
	}
1428
1429
44081
	mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1430
44081
	mdoc_elem_alloc(mdoc, line, ppos, tok, arg);
1431
44081
	if (parse_rest(mdoc, tok, line, pos, buf))
1432
		return;
1433
44081
	rew_elem(mdoc, tok);
1434
88186
}
1435
1436
/*
1437
 * The simplest argument parser available: Parse the remaining
1438
 * words until the end of the phrase or line and return 0
1439
 * or until the next macro, call that macro, and return 1.
1440
 */
1441
static int
1442
parse_rest(struct roff_man *mdoc, enum roff_tok tok,
1443
    int line, int *pos, char *buf)
1444
{
1445
	int		 la;
1446
1447
174617
	for (;;) {
1448
117228
		la = *pos;
1449
117228
		if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN)
1450
48199
			return 0;
1451
69029
		if (macro_or_word(mdoc, tok, line, la, pos, buf, 1))
1452
9190
			return 1;
1453
	}
1454
57389
}
1455
1456
static void
1457
ctx_synopsis(MACRO_PROT_ARGS)
1458
{
1459
1460
46698
	if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE))
1461
22105
		in_line(mdoc, tok, line, ppos, pos, buf);
1462
1244
	else if (tok == MDOC_Nm)
1463
1174
		blk_full(mdoc, tok, line, ppos, pos, buf);
1464
	else {
1465
70
		assert(tok == MDOC_Vt);
1466
70
		blk_part_imp(mdoc, tok, line, ppos, pos, buf);
1467
	}
1468
23349
}
1469
1470
/*
1471
 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1472
 * They're unusual because they're basically free-form text until a
1473
 * macro is encountered.
1474
 */
1475
static void
1476
phrase_ta(MACRO_PROT_ARGS)
1477
{
1478
	struct roff_node *body, *n;
1479
1480
	/* Make sure we are in a column list or ignore this macro. */
1481
1482
	body = NULL;
1483
77990
	for (n = mdoc->last; n != NULL; n = n->parent) {
1484
34634
		if (n->flags & NODE_ENDED)
1485
			continue;
1486

43474
		if (n->tok == MDOC_It && n->type == ROFFT_BODY)
1487
8674
			body = n;
1488

34797
		if (n->tok == MDOC_Bl && n->end == ENDBODY_NOT)
1489
			break;
1490
	}
1491
1492

17369
	if (n == NULL || n->norm->Bl.type != LIST_column) {
1493
33
		mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1494
		    line, ppos, "Ta");
1495
33
		return;
1496
	}
1497
1498
	/* Advance to the next column. */
1499
1500
8659
	rew_last(mdoc, body);
1501
8659
	roff_body_alloc(mdoc, line, ppos, MDOC_It);
1502
8659
	parse_rest(mdoc, TOKEN_NONE, line, pos, buf);
1503
17351
}