summaryrefslogtreecommitdiffstats
path: root/argv.c
blob: 1af8ce65e8667235663c746854095d4e62def8aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* $Id$ */
/*
 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "private.h"


static	int		 parse_next(struct mdoc *, int, 
				int *, char *, char **);


static int
parse_next(struct mdoc *mdoc, int tok, 
		int *pos, char *buf, char **v)
{

	if (0 == buf[*pos])
		return(0);

	if ('\"' != buf[*pos]) {
		*v = &buf[*pos];

		while (buf[*pos] && ! isspace(buf[*pos]))
			(*pos)++;

		if (0 == buf[*pos])
			return(1);

		buf[(*pos)++] = 0;
		if (0 == buf[*pos])
			return(1);

		while (buf[*pos] && isspace(buf[*pos]))
			(*pos)++;

		if (buf[*pos])
			return(1);

		if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
			return(-1);
		return(1);
	} 

	if ('-' == buf[*pos]) 
		if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_ARGLIKE))
			return(-1);

	*v = &buf[++(*pos)];

	while (buf[*pos] && '\"' != buf[*pos])
		(*pos)++;

	if (0 == buf[*pos]) {
		(void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_UNQUOTE);
		return(-1);
	}

	buf[(*pos)++] = 0;
	if (0 == buf[*pos])
		return(1);

	while (buf[*pos] && isspace(buf[*pos]))
		(*pos)++;

	if (buf[*pos])
		return(1);

	if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
		return(-1);
	return(1);
}


int
mdoc_argv_lookup(int tok, const char *argv)
{

	switch (tok) {
	case (MDOC_Bl):
		if (xstrcmp(argv, "ragged"))
			return(MDOC_Ragged);
		else if (xstrcmp(argv, "unfilled"))
			return(MDOC_Unfilled);
		else if (xstrcmp(argv, "literal"))
			return(MDOC_Literal);
		else if (xstrcmp(argv, "file"))
			return(MDOC_File);
		else if (xstrcmp(argv, "offset"))
			return(MDOC_Offset);
		else if (xstrcmp(argv, "bullet"))
			return(MDOC_Bullet);
		else if (xstrcmp(argv, "dash"))
			return(MDOC_Dash);
		else if (xstrcmp(argv, "hyphen"))
			return(MDOC_Hyphen);
		else if (xstrcmp(argv, "item"))
			return(MDOC_Item);
		else if (xstrcmp(argv, "enum"))
			return(MDOC_Enum);
		else if (xstrcmp(argv, "tag"))
			return(MDOC_Tag);
		else if (xstrcmp(argv, "diag"))
			return(MDOC_Diag);
		else if (xstrcmp(argv, "hang"))
			return(MDOC_Hang);
		else if (xstrcmp(argv, "ohang"))
			return(MDOC_Ohang);
		else if (xstrcmp(argv, "inset"))
			return(MDOC_Inset);
		else if (xstrcmp(argv, "column"))
			return(MDOC_Column);
		else if (xstrcmp(argv, "width"))
			return(MDOC_Width);
		else if (xstrcmp(argv, "compact"))
			return(MDOC_Compact);

		break;
	default:
		abort();
		/* NOTREACHED */
	}

	return(MDOC_ARG_MAX);
}


int
mdoc_argv_parse(struct mdoc *mdoc, int tok, int arg, 
		struct mdoc_arg *v, int *pos, char *buf)
{
	char		*p;
	int		 c, ppos, i;

	v->arg = arg;
	ppos = *pos;

	switch (arg) {
	case(MDOC_Compact):
		/* FALLTHROUGH */
	case(MDOC_Ragged):
		/* FALLTHROUGH */
	case(MDOC_Unfilled):
		/* FALLTHROUGH */
	case(MDOC_Literal):
		/* FALLTHROUGH */
	case(MDOC_File):
		/* FALLTHROUGH */
	case(MDOC_Bullet):
		/* FALLTHROUGH */
	case(MDOC_Dash):
		/* FALLTHROUGH */
	case(MDOC_Hyphen):
		/* FALLTHROUGH */
	case(MDOC_Item):
		/* FALLTHROUGH */
	case(MDOC_Enum):
		/* FALLTHROUGH */
	case(MDOC_Tag):
		/* FALLTHROUGH */
	case(MDOC_Diag):
		/* FALLTHROUGH */
	case(MDOC_Hang):
		/* FALLTHROUGH */
	case(MDOC_Ohang):
		/* FALLTHROUGH */
	case(MDOC_Inset):
		v->sz = 0;
		v->value = NULL;
		break;

	case(MDOC_Width):
		/* FALLTHROUGH */
	case(MDOC_Offset):
		/*
		 * This has a single value for an argument.
		 */
		c = parse_next(mdoc, tok, pos, buf, &p);
		if (-1 == c)
			return(0);
		else if (0 == c)
			return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGVAL));
			
		v->sz = 1;
		v->value = xcalloc(1, sizeof(char *));
		v->value[0] = p;
		break;

	case(MDOC_Column):
		/*
		 * This has several value for a single argument.  We
		 * pre-allocate a pointer array and don't let it exceed
		 * this size.
		 */
		v->sz = 0;
		v->value = xcalloc(MDOC_LINEARG_MAX, sizeof(char *));
		for (i = 0; i < MDOC_LINEARG_MAX; i++) {
			c = parse_next(mdoc, tok, pos, buf, &p);
			if (-1 == c) {
				free(v->value);
				return(0);
			} else if (0 == c)
				break;
			v->value[i] = p;
		}
		if (0 == i) {
			free(v->value);
			return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGVAL));
		} else if (MDOC_LINEARG_MAX == i)
			return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGMANY));

		v->sz = i;
		break;
	default:
		abort();
		/* NOTREACHED */
	}

	return(1);
}


void
mdoc_argv_free(int sz, struct mdoc_arg *arg)
{
	int		 i;

	for (i = 0; i < sz; i++) {
		if (0 == arg[i].sz) {
			assert(NULL == arg[i].value);
			continue;
		}
		assert(arg[i].value);
		free(arg[i].value);
	}
}