summaryrefslogtreecommitdiffstats
path: root/validate.c
blob: 0f34066e3dde21a96c48938adf89a851a752bed6 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* $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 <stdlib.h>

#include "private.h"

/* FIXME: `.St' can only have one argument set. */

typedef int	(*v_args_sz)(struct mdoc *, int, int, int);
typedef int	(*v_args)(struct mdoc *, int, int, 
			int, const char *[],
			int, const struct mdoc_arg *);
typedef int	(*v_tree)(struct mdoc *, int, int);


struct	valids {
	v_args_sz sz;
	v_args	  args;
	v_tree	  tree_pre;
	v_tree	  tree_post;
};


static	int	  assert_eq0(struct mdoc *, int, int, int);
static	int	  assert_le1(struct mdoc *, int, int, int);
static	int	  need_eq0(struct mdoc *, int, int, int);
static	int	  need_eq1(struct mdoc *, int, int, int);
static	int	  need_ge1(struct mdoc *, int, int, int);
static	int	  need_le2(struct mdoc *, int, int, int);
static	int	  want_eq0(struct mdoc *, int, int, int);
static	int	  want_ge1(struct mdoc *, int, int, int);

static	int	  tree_pre_ref(struct mdoc *, int, int);
static	int	  tree_pre_display(struct mdoc *, int, int);

static	int	  tree_post_onlyhead(struct mdoc *, int, int);
static	int	  tree_post_onlybody(struct mdoc *, int, int);
static	int	  tree_post_warnemptybody(struct mdoc *, int, int);

static	int	  args_bool(struct mdoc *, int, int, 
			int, const char *[], 
			int, const struct mdoc_arg *);
static	int	  args_sh(struct mdoc *, int, int, 
			int, const char *[], 
			int, const struct mdoc_arg *);
static	int	  args_an(struct mdoc *, int, int, 
			int, const char *[], 
			int, const struct mdoc_arg *);
static	int	  args_nopunct(struct mdoc *, int, int, 
			int, const char *[], 
			int, const struct mdoc_arg *);
static	int	  args_xr(struct mdoc *, int, int, 
			int, const char *[], 
			int, const struct mdoc_arg *);


const	struct valids mdoc_valids[MDOC_MAX] = {
	{ NULL, NULL, NULL, NULL }, /* \" */
	{ NULL, NULL, NULL, NULL }, /* Dd */ /* TODO */
	{ NULL, NULL, NULL, NULL }, /* Dt */ /* TODO */
	{ NULL, NULL, NULL, NULL }, /* Os */ /* TODO */
	{ want_ge1, args_sh, NULL, NULL }, /* Sh */ /* FIXME: preceding Pp. */
	{ want_ge1, NULL, NULL, NULL }, /* Ss */ /* FIXME: preceding Pp. */
	{ want_eq0, NULL, NULL, NULL }, /* Pp */ 
	{ assert_eq0, NULL, tree_pre_display, tree_post_onlyhead }, /* D1 */
	{ assert_eq0, NULL, tree_pre_display, tree_post_onlyhead }, /* Dl */
	{ want_eq0, NULL, tree_pre_display, tree_post_warnemptybody }, /* Bd */ /* FIXME: preceding Pp. */
	{ assert_eq0, NULL, NULL, tree_post_onlybody }, /* Ed */
	{ want_eq0, NULL, NULL, NULL }, /* Bl */ /* FIXME: preceding Pp. */
	{ assert_eq0, NULL, NULL, tree_post_onlybody }, /* El */
	{ NULL, NULL, NULL, NULL }, /* It */
	{ need_ge1, NULL, NULL, NULL }, /* Ad */ 
	{ NULL, args_an, NULL, NULL }, /* An */
	{ NULL, NULL, NULL, NULL }, /* Ar */
	{ need_ge1, NULL, NULL, NULL }, /* Cd */
	{ NULL, NULL, NULL, NULL }, /* Cm */
	{ need_ge1, NULL, NULL, NULL }, /* Dv */ 
	{ need_ge1, NULL, NULL, NULL }, /* Er */ 
	{ need_ge1, NULL, NULL, NULL }, /* Ev */ 
	{ NULL, NULL, NULL, NULL }, /* Ex */
	{ need_ge1, NULL, NULL, NULL }, /* Fa */ 
	{ NULL, NULL, NULL, NULL }, /* Fd */ 
	{ NULL, NULL, NULL, NULL }, /* Fl */
	{ need_ge1, NULL, NULL, NULL }, /* Fn */ 
	{ want_ge1, NULL, NULL, NULL }, /* Ft */ 
	{ need_ge1, NULL, NULL, NULL }, /* Ic */ 
	{ need_eq1, NULL, NULL, NULL }, /* In */ 
	{ want_ge1, NULL, NULL, NULL }, /* Li */
	{ want_ge1, NULL, NULL, NULL }, /* Nd */ 
	{ NULL, NULL, NULL, NULL }, /* Nm */ 
	{ NULL, NULL, NULL, NULL }, /* Op */
	{ NULL, NULL, NULL, NULL }, /* Ot */
	{ want_ge1, NULL, NULL, NULL }, /* Pa */
	{ NULL, NULL, NULL, NULL }, /* Rv */
	{ NULL, NULL, NULL, NULL }, /* St */
	{ need_ge1, NULL, NULL, NULL }, /* Va */
	{ need_ge1, NULL, NULL, NULL }, /* Vt */ 
	{ need_le2, args_xr, NULL, NULL }, /* Xr */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %A */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %B */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %D */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %I */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %J */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %N */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %O */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %P */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %R */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %T */
	{ need_ge1, NULL, tree_pre_ref, NULL }, /* %V */
	{ NULL, NULL, NULL, NULL }, /* Ac */
	{ NULL, NULL, NULL, NULL }, /* Ao */
	{ NULL, NULL, NULL, NULL }, /* Aq */
	{ need_le2, args_nopunct, NULL, NULL }, /* At */ /* FIXME */
	{ NULL, NULL, NULL, NULL }, /* Bc */
	{ NULL, NULL, NULL, NULL }, /* Bf */ 
	{ NULL, NULL, NULL, NULL }, /* Bo */
	{ NULL, NULL, NULL, NULL }, /* Bq */
	{ assert_le1, NULL, NULL, NULL }, /* Bsx */
	{ assert_le1, NULL, NULL, NULL }, /* Bx */
	{ need_eq1, args_bool, NULL, NULL }, /* Db */
	{ NULL, NULL, NULL, NULL }, /* Dc */
	{ NULL, NULL, NULL, NULL }, /* Do */
	{ NULL, NULL, NULL, NULL }, /* Dq */
	{ NULL, NULL, NULL, NULL }, /* Ec */
	{ NULL, NULL, NULL, NULL }, /* Ef */ /* -symbolic, etc. */
	{ need_ge1, NULL, NULL, NULL }, /* Em */ 
	{ NULL, NULL, NULL, NULL }, /* Eo */
	{ assert_le1, NULL, NULL, NULL }, /* Fx */
	{ want_ge1, NULL, NULL, NULL }, /* Ms */
	{ NULL, NULL, NULL, NULL }, /* No */
	{ NULL, NULL, NULL, NULL }, /* Ns */
	{ assert_le1, NULL, NULL, NULL }, /* Nx */
	{ assert_le1, NULL, NULL, NULL }, /* Ox */
	{ NULL, NULL, NULL, NULL }, /* Pc */
	{ NULL, NULL, NULL, NULL }, /* Pf */ /* 2 or more arguments */
	{ NULL, NULL, NULL, NULL }, /* Po */
	{ NULL, NULL, NULL, NULL }, /* Pq */ /* FIXME: ignore following Sh/Ss */
	{ NULL, NULL, NULL, NULL }, /* Qc */
	{ NULL, NULL, NULL, NULL }, /* Ql */
	{ NULL, NULL, NULL, NULL }, /* Qo */
	{ NULL, NULL, NULL, NULL }, /* Qq */
	{ NULL, NULL, NULL, NULL }, /* Re */
	{ NULL, NULL, NULL, NULL }, /* Rs */
	{ NULL, NULL, NULL, NULL }, /* Sc */
	{ NULL, NULL, NULL, NULL }, /* So */
	{ NULL, NULL, NULL, NULL }, /* Sq */
	{ need_eq1, args_bool, NULL, NULL }, /* Sm */
	{ need_ge1, NULL, NULL, NULL }, /* Sx */
	{ need_ge1, NULL, NULL, NULL }, /* Sy */
	{ want_ge1, NULL, NULL, NULL }, /* Tn */
	{ assert_eq0, NULL, NULL, NULL }, /* Ux */
	{ NULL, NULL, NULL, NULL }, /* Xc */
	{ NULL, NULL, NULL, NULL }, /* Xo */
	{ NULL, NULL, NULL, NULL }, /* Fo */ 
	{ NULL, NULL, NULL, NULL }, /* Fc */ 
	{ NULL, NULL, NULL, NULL }, /* Oo */
	{ NULL, NULL, NULL, NULL }, /* Oc */
	{ NULL, NULL, NULL, NULL }, /* Bk */
	{ NULL, NULL, NULL, NULL }, /* Ek */
	{ need_eq0, NULL, NULL, NULL }, /* Bt */
	{ need_eq1, NULL, NULL, NULL }, /* Hf */
	{ NULL, NULL, NULL, NULL }, /* Fr */
	{ need_eq0, NULL, NULL, NULL }, /* Ud */
};


static int
need_le2(struct mdoc *mdoc, int tok, int pos, int sz)
{
	if (sz <= 2)
		return(1);
	return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2));
}


static int
want_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
{
	if (sz > 0)
		return(1);
	return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1));
}


static int
want_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
{
	if (sz == 0)
		return(1);
	return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0));
}


static int
need_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
{
	if (sz == 0)
		return(1);
	return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ0));
}


static int
assert_le1(struct mdoc *mdoc, int tok, int pos, int sz)
{

	assert(sz <= 1);
	return(1);
}


static int
assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
{

	assert(sz == 0);
	return(1);
}


static int
need_eq1(struct mdoc *mdoc, int tok, int pos, int sz)
{
	if (sz == 1)
		return(1);
	return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ1));
}


static int
need_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
{
	if (sz > 0)
		return(1);
	return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
}


static int
tree_post_onlybody(struct mdoc *mdoc, int tok, int pos)
{
	struct mdoc_node *n;

	assert(mdoc->last);
	n = mdoc->last;

	assert(MDOC_BLOCK == n->type);
	assert(n->child);

	if (MDOC_BODY == n->child->type) {
		if (n->child->child)
			return(1);
		return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_EMPTYBODY));
	}

	return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_CHILDBODY));
}


static int
tree_post_warnemptybody(struct mdoc *mdoc, int tok, int pos)
{
	struct mdoc_node *n;

	assert(mdoc->last);
	n = mdoc->last;

	assert(MDOC_BLOCK == n->type);
	assert(n->child);

	for (n = n->child; n; n = n->next)
		if (MDOC_BODY == n->type)
			break;

	if (n && n->child)
		return(1);
	return(mdoc_warn(mdoc, tok, pos, WARN_SYNTAX_EMPTYBODY));
}


static int
tree_post_onlyhead(struct mdoc *mdoc, int tok, int pos)
{
	struct mdoc_node *n;

	assert(mdoc->last);
	n = mdoc->last;

	assert(MDOC_BLOCK == n->type);
	assert(n->child);

	n = n->child;

	if (MDOC_HEAD != n->type) 
		return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_CHILDHEAD));
	if (n->child)
		return(1);
	return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_EMPTYHEAD));
}


static int
args_an(struct mdoc *mdoc, int tok, int pos, 
		int sz, const char *args[],
		int argc, const struct mdoc_arg *argv)
{

	printf("argc=%d, sz=%d\n", argc, sz);
	if (0 != argc && 0 != sz) 
		return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0));
	return(1);
}


static int
args_sh(struct mdoc *mdoc, int tok, int pos, 
		int sz, const char *args[],
		int argc, const struct mdoc_arg *argv)
{
	enum mdoc_sec	 sec;

	sec = mdoc_atosec((size_t)sz, args);
	if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn)
		if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO))
			return(0);
	if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec)
		return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME));

	return(1);
}


static int
args_bool(struct mdoc *mdoc, int tok, int pos, 
		int sz, const char *args[],
		int argc, const struct mdoc_arg *argv)
{
	int	 	 i;

	for (i = 0; i < sz; i++) {
		if (xstrcmp(args[i], "on"))
			continue;
		if (xstrcmp(args[i], "off"))
			continue;
		return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGBAD));
	}
	return(1);
}


static int
tree_pre_ref(struct mdoc *mdoc, int tok, int pos)
{
	struct mdoc_node *n;

	assert(mdoc->last);
	for (n = mdoc->last ; n; n = n->parent) {
		if (MDOC_BLOCK != n->type)
			continue;
		if (MDOC_Rs != n->data.block.tok)
			break;
		return(1);
	}

	return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NOCTX));
}


static int
args_xr(struct mdoc *mdoc, int tok, int pos, 
		int sz, const char *args[],
		int argc, const struct mdoc_arg *argv)
{

	if (1 == sz)
		return(1);
	if (0 == sz)
		return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
	if (MSEC_DEFAULT == mdoc_atomsec(args[1]))
		return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGFORM));
	return(1);
}


static int
args_nopunct(struct mdoc *mdoc, int tok, int pos, 
		int sz, const char *args[],
		int argc, const struct mdoc_arg *argv)
{
	int		 i;

	if (0 == sz)
		return(1);

	i = 0;
	if (ATT_DEFAULT == mdoc_atoatt(args[i]))
		i++;
	for ( ; i < sz; i++) {
		if ( ! mdoc_isdelim(args[i]))
			continue;
		return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT));
	}
	return(1);
}


static int
tree_pre_display(struct mdoc *mdoc, int tok, int pos)
{
	struct mdoc_node *node;

	assert(mdoc->last);

	/* Displays may not be nested in other displays. */

	/* LINTED */
	for (node = mdoc->last; node; node = node->parent) {
		if (node->type != MDOC_BLOCK)
			continue;
		if (node->data.block.tok != MDOC_Bd)
			continue;
		break;
	}
	if (NULL == node)
		return(1);
	return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST));
}


int
mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos, 
		int argc, const struct mdoc_arg *argv)
{

	assert(tok < MDOC_MAX);
	if (mdoc_valids[tok].sz) 
		if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz))
			return(0);
	if (mdoc_valids[tok].args) 
		if ( ! (*mdoc_valids[tok].args)(mdoc, tok, pos, 
					sz, args, argc, argv))
			return(0);
	if (mdoc_valids[tok].tree_pre) 
		if ( ! (*mdoc_valids[tok].tree_pre)(mdoc, tok, pos))
			return(0);
	return(1);
}


int
mdoc_valid_post(struct mdoc *mdoc, int tok, int pos,
		int argc, const struct mdoc_arg *argv)
{

	if (mdoc_valids[tok].tree_post)
		return((*mdoc_valids[tok].tree_post)(mdoc, tok, pos));
	return(1);
}