GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/mdoc_argv.c Lines: 172 179 96.1 %
Date: 2017-11-13 Branches: 146 159 91.8 %

Line Branch Exec Source
1
/*	$OpenBSD: mdoc_argv.c,v 1.71 2017/05/30 16:21:07 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4
 * Copyright (c) 2012, 2014-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 <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
25
#include "mandoc_aux.h"
26
#include "mandoc.h"
27
#include "roff.h"
28
#include "mdoc.h"
29
#include "libmandoc.h"
30
#include "roff_int.h"
31
#include "libmdoc.h"
32
33
#define	MULTI_STEP	 5 /* pre-allocate argument values */
34
#define	DELIMSZ		 6 /* max possible size of a delimiter */
35
36
enum	argsflag {
37
	ARGSFL_NONE = 0,
38
	ARGSFL_DELIM, /* handle delimiters of [[::delim::][ ]+]+ */
39
	ARGSFL_TABSEP /* handle tab/`Ta' separated phrases */
40
};
41
42
enum	argvflag {
43
	ARGV_NONE, /* no args to flag (e.g., -split) */
44
	ARGV_SINGLE, /* one arg to flag (e.g., -file xxx)  */
45
	ARGV_MULTI /* multiple args (e.g., -column xxx yyy) */
46
};
47
48
struct	mdocarg {
49
	enum argsflag	 flags;
50
	const enum mdocargt *argvs;
51
};
52
53
static	void		 argn_free(struct mdoc_arg *, int);
54
static	enum margserr	 args(struct roff_man *, int, int *,
55
				char *, enum argsflag, char **);
56
static	int		 args_checkpunct(const char *, int);
57
static	void		 argv_multi(struct roff_man *, int,
58
				struct mdoc_argv *, int *, char *);
59
static	void		 argv_single(struct roff_man *, int,
60
				struct mdoc_argv *, int *, char *);
61
62
static	const enum argvflag argvflags[MDOC_ARG_MAX] = {
63
	ARGV_NONE,	/* MDOC_Split */
64
	ARGV_NONE,	/* MDOC_Nosplit */
65
	ARGV_NONE,	/* MDOC_Ragged */
66
	ARGV_NONE,	/* MDOC_Unfilled */
67
	ARGV_NONE,	/* MDOC_Literal */
68
	ARGV_SINGLE,	/* MDOC_File */
69
	ARGV_SINGLE,	/* MDOC_Offset */
70
	ARGV_NONE,	/* MDOC_Bullet */
71
	ARGV_NONE,	/* MDOC_Dash */
72
	ARGV_NONE,	/* MDOC_Hyphen */
73
	ARGV_NONE,	/* MDOC_Item */
74
	ARGV_NONE,	/* MDOC_Enum */
75
	ARGV_NONE,	/* MDOC_Tag */
76
	ARGV_NONE,	/* MDOC_Diag */
77
	ARGV_NONE,	/* MDOC_Hang */
78
	ARGV_NONE,	/* MDOC_Ohang */
79
	ARGV_NONE,	/* MDOC_Inset */
80
	ARGV_MULTI,	/* MDOC_Column */
81
	ARGV_SINGLE,	/* MDOC_Width */
82
	ARGV_NONE,	/* MDOC_Compact */
83
	ARGV_NONE,	/* MDOC_Std */
84
	ARGV_NONE,	/* MDOC_Filled */
85
	ARGV_NONE,	/* MDOC_Words */
86
	ARGV_NONE,	/* MDOC_Emphasis */
87
	ARGV_NONE,	/* MDOC_Symbolic */
88
	ARGV_NONE	/* MDOC_Symbolic */
89
};
90
91
static	const enum mdocargt args_Ex[] = {
92
	MDOC_Std,
93
	MDOC_ARG_MAX
94
};
95
96
static	const enum mdocargt args_An[] = {
97
	MDOC_Split,
98
	MDOC_Nosplit,
99
	MDOC_ARG_MAX
100
};
101
102
static	const enum mdocargt args_Bd[] = {
103
	MDOC_Ragged,
104
	MDOC_Unfilled,
105
	MDOC_Filled,
106
	MDOC_Literal,
107
	MDOC_File,
108
	MDOC_Offset,
109
	MDOC_Compact,
110
	MDOC_Centred,
111
	MDOC_ARG_MAX
112
};
113
114
static	const enum mdocargt args_Bf[] = {
115
	MDOC_Emphasis,
116
	MDOC_Literal,
117
	MDOC_Symbolic,
118
	MDOC_ARG_MAX
119
};
120
121
static	const enum mdocargt args_Bk[] = {
122
	MDOC_Words,
123
	MDOC_ARG_MAX
124
};
125
126
static	const enum mdocargt args_Bl[] = {
127
	MDOC_Bullet,
128
	MDOC_Dash,
129
	MDOC_Hyphen,
130
	MDOC_Item,
131
	MDOC_Enum,
132
	MDOC_Tag,
133
	MDOC_Diag,
134
	MDOC_Hang,
135
	MDOC_Ohang,
136
	MDOC_Inset,
137
	MDOC_Column,
138
	MDOC_Width,
139
	MDOC_Offset,
140
	MDOC_Compact,
141
	MDOC_Nested,
142
	MDOC_ARG_MAX
143
};
144
145
static	const struct mdocarg __mdocargs[MDOC_MAX - MDOC_Dd] = {
146
	{ ARGSFL_NONE, NULL }, /* Dd */
147
	{ ARGSFL_NONE, NULL }, /* Dt */
148
	{ ARGSFL_NONE, NULL }, /* Os */
149
	{ ARGSFL_NONE, NULL }, /* Sh */
150
	{ ARGSFL_NONE, NULL }, /* Ss */
151
	{ ARGSFL_NONE, NULL }, /* Pp */
152
	{ ARGSFL_DELIM, NULL }, /* D1 */
153
	{ ARGSFL_DELIM, NULL }, /* Dl */
154
	{ ARGSFL_NONE, args_Bd }, /* Bd */
155
	{ ARGSFL_NONE, NULL }, /* Ed */
156
	{ ARGSFL_NONE, args_Bl }, /* Bl */
157
	{ ARGSFL_NONE, NULL }, /* El */
158
	{ ARGSFL_NONE, NULL }, /* It */
159
	{ ARGSFL_DELIM, NULL }, /* Ad */
160
	{ ARGSFL_DELIM, args_An }, /* An */
161
	{ ARGSFL_DELIM, NULL }, /* Ap */
162
	{ ARGSFL_DELIM, NULL }, /* Ar */
163
	{ ARGSFL_DELIM, NULL }, /* Cd */
164
	{ ARGSFL_DELIM, NULL }, /* Cm */
165
	{ ARGSFL_DELIM, NULL }, /* Dv */
166
	{ ARGSFL_DELIM, NULL }, /* Er */
167
	{ ARGSFL_DELIM, NULL }, /* Ev */
168
	{ ARGSFL_NONE, args_Ex }, /* Ex */
169
	{ ARGSFL_DELIM, NULL }, /* Fa */
170
	{ ARGSFL_NONE, NULL }, /* Fd */
171
	{ ARGSFL_DELIM, NULL }, /* Fl */
172
	{ ARGSFL_DELIM, NULL }, /* Fn */
173
	{ ARGSFL_DELIM, NULL }, /* Ft */
174
	{ ARGSFL_DELIM, NULL }, /* Ic */
175
	{ ARGSFL_DELIM, NULL }, /* In */
176
	{ ARGSFL_DELIM, NULL }, /* Li */
177
	{ ARGSFL_NONE, NULL }, /* Nd */
178
	{ ARGSFL_DELIM, NULL }, /* Nm */
179
	{ ARGSFL_DELIM, NULL }, /* Op */
180
	{ ARGSFL_DELIM, NULL }, /* Ot */
181
	{ ARGSFL_DELIM, NULL }, /* Pa */
182
	{ ARGSFL_NONE, args_Ex }, /* Rv */
183
	{ ARGSFL_DELIM, NULL }, /* St */
184
	{ ARGSFL_DELIM, NULL }, /* Va */
185
	{ ARGSFL_DELIM, NULL }, /* Vt */
186
	{ ARGSFL_DELIM, NULL }, /* Xr */
187
	{ ARGSFL_NONE, NULL }, /* %A */
188
	{ ARGSFL_NONE, NULL }, /* %B */
189
	{ ARGSFL_NONE, NULL }, /* %D */
190
	{ ARGSFL_NONE, NULL }, /* %I */
191
	{ ARGSFL_NONE, NULL }, /* %J */
192
	{ ARGSFL_NONE, NULL }, /* %N */
193
	{ ARGSFL_NONE, NULL }, /* %O */
194
	{ ARGSFL_NONE, NULL }, /* %P */
195
	{ ARGSFL_NONE, NULL }, /* %R */
196
	{ ARGSFL_NONE, NULL }, /* %T */
197
	{ ARGSFL_NONE, NULL }, /* %V */
198
	{ ARGSFL_DELIM, NULL }, /* Ac */
199
	{ ARGSFL_NONE, NULL }, /* Ao */
200
	{ ARGSFL_DELIM, NULL }, /* Aq */
201
	{ ARGSFL_DELIM, NULL }, /* At */
202
	{ ARGSFL_DELIM, NULL }, /* Bc */
203
	{ ARGSFL_NONE, args_Bf }, /* Bf */
204
	{ ARGSFL_NONE, NULL }, /* Bo */
205
	{ ARGSFL_DELIM, NULL }, /* Bq */
206
	{ ARGSFL_DELIM, NULL }, /* Bsx */
207
	{ ARGSFL_DELIM, NULL }, /* Bx */
208
	{ ARGSFL_NONE, NULL }, /* Db */
209
	{ ARGSFL_DELIM, NULL }, /* Dc */
210
	{ ARGSFL_NONE, NULL }, /* Do */
211
	{ ARGSFL_DELIM, NULL }, /* Dq */
212
	{ ARGSFL_DELIM, NULL }, /* Ec */
213
	{ ARGSFL_NONE, NULL }, /* Ef */
214
	{ ARGSFL_DELIM, NULL }, /* Em */
215
	{ ARGSFL_NONE, NULL }, /* Eo */
216
	{ ARGSFL_DELIM, NULL }, /* Fx */
217
	{ ARGSFL_DELIM, NULL }, /* Ms */
218
	{ ARGSFL_DELIM, NULL }, /* No */
219
	{ ARGSFL_DELIM, NULL }, /* Ns */
220
	{ ARGSFL_DELIM, NULL }, /* Nx */
221
	{ ARGSFL_DELIM, NULL }, /* Ox */
222
	{ ARGSFL_DELIM, NULL }, /* Pc */
223
	{ ARGSFL_DELIM, NULL }, /* Pf */
224
	{ ARGSFL_NONE, NULL }, /* Po */
225
	{ ARGSFL_DELIM, NULL }, /* Pq */
226
	{ ARGSFL_DELIM, NULL }, /* Qc */
227
	{ ARGSFL_DELIM, NULL }, /* Ql */
228
	{ ARGSFL_NONE, NULL }, /* Qo */
229
	{ ARGSFL_DELIM, NULL }, /* Qq */
230
	{ ARGSFL_NONE, NULL }, /* Re */
231
	{ ARGSFL_NONE, NULL }, /* Rs */
232
	{ ARGSFL_DELIM, NULL }, /* Sc */
233
	{ ARGSFL_NONE, NULL }, /* So */
234
	{ ARGSFL_DELIM, NULL }, /* Sq */
235
	{ ARGSFL_NONE, NULL }, /* Sm */
236
	{ ARGSFL_DELIM, NULL }, /* Sx */
237
	{ ARGSFL_DELIM, NULL }, /* Sy */
238
	{ ARGSFL_DELIM, NULL }, /* Tn */
239
	{ ARGSFL_DELIM, NULL }, /* Ux */
240
	{ ARGSFL_DELIM, NULL }, /* Xc */
241
	{ ARGSFL_NONE, NULL }, /* Xo */
242
	{ ARGSFL_NONE, NULL }, /* Fo */
243
	{ ARGSFL_DELIM, NULL }, /* Fc */
244
	{ ARGSFL_NONE, NULL }, /* Oo */
245
	{ ARGSFL_DELIM, NULL }, /* Oc */
246
	{ ARGSFL_NONE, args_Bk }, /* Bk */
247
	{ ARGSFL_NONE, NULL }, /* Ek */
248
	{ ARGSFL_NONE, NULL }, /* Bt */
249
	{ ARGSFL_NONE, NULL }, /* Hf */
250
	{ ARGSFL_DELIM, NULL }, /* Fr */
251
	{ ARGSFL_NONE, NULL }, /* Ud */
252
	{ ARGSFL_DELIM, NULL }, /* Lb */
253
	{ ARGSFL_NONE, NULL }, /* Lp */
254
	{ ARGSFL_DELIM, NULL }, /* Lk */
255
	{ ARGSFL_DELIM, NULL }, /* Mt */
256
	{ ARGSFL_DELIM, NULL }, /* Brq */
257
	{ ARGSFL_NONE, NULL }, /* Bro */
258
	{ ARGSFL_DELIM, NULL }, /* Brc */
259
	{ ARGSFL_NONE, NULL }, /* %C */
260
	{ ARGSFL_NONE, NULL }, /* Es */
261
	{ ARGSFL_DELIM, NULL }, /* En */
262
	{ ARGSFL_DELIM, NULL }, /* Dx */
263
	{ ARGSFL_NONE, NULL }, /* %Q */
264
	{ ARGSFL_NONE, NULL }, /* %U */
265
	{ ARGSFL_NONE, NULL }, /* Ta */
266
};
267
static	const struct mdocarg *const mdocargs = __mdocargs - MDOC_Dd;
268
269
270
/*
271
 * Parse flags and their arguments from the input line.
272
 * These come in the form -flag [argument ...].
273
 * Some flags take no argument, some one, some multiple.
274
 */
275
void
276
mdoc_argv(struct roff_man *mdoc, int line, enum roff_tok tok,
277
	struct mdoc_arg **reta, int *pos, char *buf)
278
{
279
633912
	struct mdoc_argv	  tmpv;
280
	struct mdoc_argv	**retv;
281
	const enum mdocargt	 *argtable;
282
	char			 *argname;
283
316956
	int			  ipos, retc;
284
	char			  savechar;
285
286
316956
	*reta = NULL;
287
288
	/* Which flags does this macro support? */
289
290
316956
	assert(tok >= MDOC_Dd && tok < MDOC_MAX);
291
316956
	argtable = mdocargs[tok].argvs;
292
316956
	if (argtable == NULL)
293
303967
		return;
294
295
	/* Loop over the flags on the input line. */
296
297
12989
	ipos = *pos;
298
70974
	while (buf[ipos] == '-') {
299
300
		/* Seek to the first unescaped space. */
301
302
280026
		for (argname = buf + ++ipos; buf[ipos] != '\0'; ipos++)
303

154325
			if (buf[ipos] == ' ' && buf[ipos - 1] != '\\')
304
				break;
305
306
		/*
307
		 * We want to nil-terminate the word to look it up.
308
		 * But we may not have a flag, in which case we need
309
		 * to restore the line as-is.  So keep around the
310
		 * stray byte, which we'll reset upon exiting.
311
		 */
312
313
22612
		if ((savechar = buf[ipos]) != '\0')
314
18462
			buf[ipos++] = '\0';
315
316
		/*
317
		 * Now look up the word as a flag.  Use temporary
318
		 * storage that we'll copy into the node's flags.
319
		 */
320
321
361522
		while ((tmpv.arg = *argtable++) != MDOC_ARG_MAX)
322
180647
			if ( ! strcmp(argname, mdoc_argnames[tmpv.arg]))
323
				break;
324
325
		/* If it isn't a flag, restore the saved byte. */
326
327
22612
		if (tmpv.arg == MDOC_ARG_MAX) {
328
114
			if (savechar != '\0')
329
90
				buf[ipos - 1] = savechar;
330
			break;
331
		}
332
333
		/* Read to the next word (the first argument). */
334
335
44998
		while (buf[ipos] == ' ')
336
1
			ipos++;
337
338
		/* Parse the arguments of the flag. */
339
340
32053
		tmpv.line  = line;
341
32053
		tmpv.pos   = *pos;
342
32053
		tmpv.sz    = 0;
343
32053
		tmpv.value = NULL;
344
345
32053
		switch (argvflags[tmpv.arg]) {
346
		case ARGV_SINGLE:
347
9128
			argv_single(mdoc, line, &tmpv, &ipos, buf);
348
9128
			break;
349
		case ARGV_MULTI:
350
427
			argv_multi(mdoc, line, &tmpv, &ipos, buf);
351
427
			break;
352
		case ARGV_NONE:
353
			break;
354
		}
355
356
		/* Append to the return values. */
357
358
22498
		if (*reta == NULL)
359
11238
			*reta = mandoc_calloc(1, sizeof(**reta));
360
361
22498
		retc = ++(*reta)->argc;
362
22498
		retv = &(*reta)->argv;
363
22498
		*retv = mandoc_reallocarray(*retv, retc, sizeof(**retv));
364
22498
		memcpy(*retv + retc - 1, &tmpv, sizeof(**retv));
365
366
		/* Prepare for parsing the next flag. */
367
368
22498
		*pos = ipos;
369
22498
		argtable = mdocargs[tok].argvs;
370
	}
371
329945
}
372
373
void
374
mdoc_argv_free(struct mdoc_arg *p)
375
{
376
	int		 i;
377
378
25716
	if (NULL == p)
379
1620
		return;
380
381
11238
	if (p->refcnt) {
382
11238
		--(p->refcnt);
383
11238
		if (p->refcnt)
384
			return;
385
	}
386
11238
	assert(p->argc);
387
388
67472
	for (i = (int)p->argc - 1; i >= 0; i--)
389
22498
		argn_free(p, i);
390
391
11238
	free(p->argv);
392
11238
	free(p);
393
24096
}
394
395
static void
396
argn_free(struct mdoc_arg *p, int iarg)
397
{
398
	struct mdoc_argv *arg;
399
	int		  j;
400
401
44996
	arg = &p->argv[iarg];
402
403

31930
	if (arg->sz && arg->value) {
404
39148
		for (j = (int)arg->sz - 1; j >= 0; j--)
405
10142
			free(arg->value[j]);
406
9432
		free(arg->value);
407
9432
	}
408
409
44996
	for (--p->argc; iarg < (int)p->argc; iarg++)
410
		p->argv[iarg] = p->argv[iarg+1];
411
22498
}
412
413
enum margserr
414
mdoc_args(struct roff_man *mdoc, int line, int *pos,
415
	char *buf, enum roff_tok tok, char **v)
416
{
417
	struct roff_node *n;
418
1688540
	char		 *v_local;
419
	enum argsflag	  fl;
420
421
844270
	if (v == NULL)
422
117228
		v = &v_local;
423
2412930
	fl = tok == TOKEN_NONE ? ARGSFL_NONE : mdocargs[tok].flags;
424
425
	/*
426
	 * We know that we're in an `It', so it's reasonable to expect
427
	 * us to be sitting in a `Bl'.  Someday this may not be the case
428
	 * (if we allow random `It's sitting out there), so provide a
429
	 * safe fall-back into the default behaviour.
430
	 */
431
432
844270
	if (tok == MDOC_It) {
433
262174
		for (n = mdoc->last; n != NULL; n = n->parent) {
434
131087
			if (n->tok != MDOC_Bl)
435
				continue;
436
51172
			if (n->norm->Bl.type == LIST_column)
437
8991
				fl = ARGSFL_TABSEP;
438
			break;
439
		}
440
	}
441
442
1688540
	return args(mdoc, line, pos, buf, fl, v);
443
844270
}
444
445
static enum margserr
446
args(struct roff_man *mdoc, int line, int *pos,
447
		char *buf, enum argsflag fl, char **v)
448
{
449
1709510
	char		*p;
450
	int		 pairs;
451
452
854755
	if (buf[*pos] == '\0') {
453

294987
		if (mdoc->flags & MDOC_PHRASELIT &&
454
28
		    ! (mdoc->flags & MDOC_PHRASE)) {
455
			mandoc_msg(MANDOCERR_ARG_QUOTE,
456
			    mdoc->parse, line, *pos, NULL);
457
			mdoc->flags &= ~MDOC_PHRASELIT;
458
		}
459
294959
		return ARGS_EOLN;
460
	}
461
462
559796
	*v = buf + *pos;
463
464

887851
	if (fl == ARGSFL_DELIM && args_checkpunct(buf, *pos))
465
46782
		return ARGS_PUNCT;
466
467
	/*
468
	 * Tabs in `It' lines in `Bl -column' can't be escaped.
469
	 * Phrases are reparsed for `Ta' and other macros later.
470
	 */
471
472
513014
	if (fl == ARGSFL_TABSEP) {
473
4649
		if ((p = strchr(*v, '\t')) != NULL) {
474
475
			/*
476
			 * Words right before and right after
477
			 * tab characters are not parsed,
478
			 * unless there is a blank in between.
479
			 */
480
481

704
			if (p > buf && p[-1] != ' ')
482
316
				mdoc->flags |= MDOC_PHRASEQL;
483
352
			if (p[1] != ' ')
484
334
				mdoc->flags |= MDOC_PHRASEQN;
485
486
			/*
487
			 * One or more blanks after a tab cause
488
			 * one leading blank in the next column.
489
			 * So skip all but one of them.
490
			 */
491
492
352
			*pos += (int)(p - *v) + 1;
493

749
			while (buf[*pos] == ' ' && buf[*pos + 1] == ' ')
494
				(*pos)++;
495
496
			/*
497
			 * A tab at the end of an input line
498
			 * switches to the next column.
499
			 */
500
501

695
			if (buf[*pos] == '\0' || buf[*pos + 1] == '\0')
502
84
				mdoc->flags |= MDOC_PHRASEQN;
503
		} else {
504
4297
			p = strchr(*v, '\0');
505
4297
			if (p[-1] == ' ')
506
				mandoc_msg(MANDOCERR_SPACE_EOL,
507
				    mdoc->parse, line, *pos, NULL);
508
4297
			*pos += (int)(p - *v);
509
		}
510
511
		/* Skip any trailing blank characters. */
512

18776
		while (p > *v && p[-1] == ' ' &&
513
90
		    (p - 1 == *v || p[-2] != '\\'))
514
45
			p--;
515
4649
		*p = '\0';
516
517
4649
		return ARGS_PHRASE;
518
	}
519
520
	/*
521
	 * Process a quoted literal.  A quote begins with a double-quote
522
	 * and ends with a double-quote NOT preceded by a double-quote.
523
	 * NUL-terminate the literal in place.
524
	 * Collapse pairs of quotes inside quoted literals.
525
	 * Whitespace is NOT involved in literal termination.
526
	 */
527
528

1016702
	if (mdoc->flags & MDOC_PHRASELIT || buf[*pos] == '\"') {
529
26242
		if ( ! (mdoc->flags & MDOC_PHRASELIT))
530
26214
			*v = &buf[++(*pos)];
531
532
26242
		if (mdoc->flags & MDOC_PHRASE)
533
5037
			mdoc->flags |= MDOC_PHRASELIT;
534
535
		pairs = 0;
536
750816
		for ( ; buf[*pos]; (*pos)++) {
537
			/* Move following text left after quoted quotes. */
538
375344
			if (pairs)
539
54
				buf[*pos - pairs] = buf[*pos];
540
375344
			if ('\"' != buf[*pos])
541
				continue;
542
			/* Unquoted quotes end quoted args. */
543
26196
			if ('\"' != buf[*pos + 1])
544
				break;
545
			/* Quoted quotes collapse. */
546
18
			pairs++;
547
18
			(*pos)++;
548
18
		}
549
26242
		if (pairs)
550
12
			buf[*pos - pairs] = '\0';
551
552
26242
		if (buf[*pos] == '\0') {
553
64
			if ( ! (mdoc->flags & MDOC_PHRASE))
554
36
				mandoc_msg(MANDOCERR_ARG_QUOTE,
555
36
				    mdoc->parse, line, *pos, NULL);
556
64
			return ARGS_WORD;
557
		}
558
559
26178
		mdoc->flags &= ~MDOC_PHRASELIT;
560
26178
		buf[(*pos)++] = '\0';
561
562
26178
		if ('\0' == buf[*pos])
563
15675
			return ARGS_WORD;
564
565
43388
		while (' ' == buf[*pos])
566
11191
			(*pos)++;
567
568
10503
		if ('\0' == buf[*pos])
569
18
			mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
570
			    line, *pos, NULL);
571
572
10503
		return ARGS_WORD;
573
	}
574
575
482123
	p = &buf[*pos];
576
482123
	*v = mandoc_getarg(mdoc->parse, &p, line, pos);
577
578
	/*
579
	 * After parsing the last word in this phrase,
580
	 * tell lookup() whether or not to interpret it.
581
	 */
582
583

705854
	if (*p == '\0' && mdoc->flags & MDOC_PHRASEQL) {
584
289
		mdoc->flags &= ~MDOC_PHRASEQL;
585
289
		mdoc->flags |= MDOC_PHRASEQF;
586
289
	}
587
482123
	return ARGS_WORD;
588
854755
}
589
590
/*
591
 * Check if the string consists only of space-separated closing
592
 * delimiters.  This is a bit of a dance: the first must be a close
593
 * delimiter, but it may be followed by middle delimiters.  Arbitrary
594
 * whitespace may separate these tokens.
595
 */
596
static int
597
args_checkpunct(const char *buf, int i)
598
{
599
	int		 j;
600
656110
	char		 dbuf[DELIMSZ];
601
	enum mdelim	 d;
602
603
	/* First token must be a close-delimiter. */
604
605

4254546
	for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
606
1128921
		dbuf[j] = buf[i];
607
608
328055
	if (DELIMSZ == j)
609
110208
		return 0;
610
611
217847
	dbuf[j] = '\0';
612
217847
	if (DELIM_CLOSE != mdoc_isdelim(dbuf))
613
167429
		return 0;
614
615
110064
	while (' ' == buf[i])
616
4614
		i++;
617
618
	/* Remaining must NOT be open/none. */
619
620
102978
	while (buf[i]) {
621
		j = 0;
622

37332
		while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
623
8662
			dbuf[j++] = buf[i++];
624
625
4707
		if (DELIMSZ == j)
626
338
			return 0;
627
628
4369
		dbuf[j] = '\0';
629
4369
		d = mdoc_isdelim(dbuf);
630
4369
		if (DELIM_NONE == d || DELIM_OPEN == d)
631
3298
			return 0;
632
633
2370
		while (' ' == buf[i])
634
114
			i++;
635
	}
636
637
46782
	return '\0' == buf[i];
638
328055
}
639
640
static void
641
argv_multi(struct roff_man *mdoc, int line,
642
		struct mdoc_argv *v, int *pos, char *buf)
643
{
644
	enum margserr	 ac;
645
854
	char		*p;
646
647
1538
	for (v->sz = 0; ; v->sz++) {
648
1538
		if (buf[*pos] == '-')
649
			break;
650
1357
		ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
651
1357
		if (ac == ARGS_EOLN)
652
			break;
653
654
1111
		if (v->sz % MULTI_STEP == 0)
655
850
			v->value = mandoc_reallocarray(v->value,
656
425
			    v->sz + MULTI_STEP, sizeof(char *));
657
658
1111
		v->value[(int)v->sz] = mandoc_strdup(p);
659
	}
660
427
}
661
662
static void
663
argv_single(struct roff_man *mdoc, int line,
664
		struct mdoc_argv *v, int *pos, char *buf)
665
{
666
	enum margserr	 ac;
667
18256
	char		*p;
668
669
9128
	ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
670
9128
	if (ac == ARGS_EOLN)
671
123
		return;
672
673
9005
	v->sz = 1;
674
9005
	v->value = mandoc_malloc(sizeof(char *));
675
9005
	v->value[0] = mandoc_strdup(p);
676
18133
}