Annotation of mandoc/validate.c, Revision 1.13
1.13 ! kristaps 1: /* $Id: validate.c,v 1.12 2009/01/02 09:29:06 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <assert.h>
1.8 kristaps 20: #include <stdlib.h>
1.1 kristaps 21:
22: #include "private.h"
23:
1.11 kristaps 24: /* FIXME: `.St' can only have one argument set. */
1.2 kristaps 25:
1.11 kristaps 26: typedef int (*v_args_sz)(struct mdoc *, int, int, int);
27: typedef int (*v_args)(struct mdoc *, int, int, int,
1.9 kristaps 28: const char *[], int, const struct mdoc_arg *);
1.11 kristaps 29: typedef int (*v_post)(struct mdoc *, int, int);
1.1 kristaps 30:
1.11 kristaps 31: static int need_head_child(struct mdoc *, int, int);
1.12 kristaps 32: static int no_head_child(struct mdoc *, int, int);
1.11 kristaps 33:
34: static int assert_eq0(struct mdoc *, int, int, int);
35: static int assert_le1(struct mdoc *, int, int, int);
36: static int need_eq0(struct mdoc *, int, int, int);
37: static int need_eq1(struct mdoc *, int, int, int);
38: static int need_ge1(struct mdoc *, int, int, int);
39: static int need_le2(struct mdoc *, int, int, int);
40: static int want_eq0(struct mdoc *, int, int, int);
41: static int want_ge1(struct mdoc *, int, int, int);
42:
43: static int args_ref(struct mdoc *, int, int, int,
44: const char *[], int, const struct mdoc_arg *);
45: static int args_bool(struct mdoc *, int, int, int,
46: const char *[], int, const struct mdoc_arg *);
47: static int args_Sh(struct mdoc *, int, int, int,
1.9 kristaps 48: const char *[], int, const struct mdoc_arg *);
1.11 kristaps 49: static int args_blocknest(struct mdoc *, int, int, int,
1.9 kristaps 50: const char *[], int, const struct mdoc_arg *);
1.11 kristaps 51: static int args_At(struct mdoc *, int, int, int,
52: const char *[], int, const struct mdoc_arg *);
53: static int args_Xr(struct mdoc *, int, int, int,
1.8 kristaps 54: const char *[], int, const struct mdoc_arg *);
1.9 kristaps 55:
56: struct valids {
1.11 kristaps 57: v_args_sz sz;
58: v_args args;
59: v_post post;
1.9 kristaps 60: };
61:
62:
1.12 kristaps 63: /*
64: * FIXME: have arrays of function pointers in case we want multiple
65: * check callbacks per macro.
66: */
67:
1.9 kristaps 68: const struct valids mdoc_valids[MDOC_MAX] = {
1.11 kristaps 69: { NULL, NULL, NULL }, /* \" */
70: { NULL, NULL, NULL }, /* Dd */ /* TODO */
71: { NULL, NULL, NULL }, /* Dt */ /* TODO */
72: { NULL, NULL, NULL }, /* Os */ /* TODO */
73: { NULL, args_Sh, NULL }, /* Sh */
74: { NULL, NULL, NULL }, /* Ss */
75: { want_eq0, NULL, NULL }, /* Pp */
76: { NULL, args_blocknest, need_head_child }, /* D1 */
77: { NULL, args_blocknest, need_head_child }, /* Dl */
78: { NULL, args_blocknest, NULL }, /* Bd */
1.12 kristaps 79: { NULL, NULL, no_head_child }, /* Ed */
1.11 kristaps 80: { NULL, NULL, NULL }, /* Bl */
1.12 kristaps 81: { NULL, NULL, no_head_child }, /* El */
1.11 kristaps 82: { NULL, NULL, NULL }, /* It */
83: { need_ge1, NULL, NULL }, /* Ad */
1.13 ! kristaps 84: { NULL, NULL, NULL }, /* An */ /* FIXME: no args & argv. */
1.11 kristaps 85: { NULL, NULL, NULL }, /* Ar */
86: { need_ge1, NULL, NULL }, /* Cd */
87: { NULL, NULL, NULL }, /* Cm */
88: { need_ge1, NULL, NULL }, /* Dv */
89: { need_ge1, NULL, NULL }, /* Er */
90: { need_ge1, NULL, NULL }, /* Ev */
91: { NULL, NULL, NULL }, /* Ex */
92: { need_ge1, NULL, NULL }, /* Fa */
93: { NULL, NULL, NULL }, /* Fd */
94: { NULL, NULL, NULL }, /* Fl */
95: { need_ge1, NULL, NULL }, /* Fn */
96: { want_ge1, NULL, NULL }, /* Ft */
97: { need_ge1, NULL, NULL }, /* Ic */
98: { need_eq1, NULL, NULL }, /* In */
99: { want_ge1, NULL, NULL }, /* Li */
100: { want_ge1, NULL, NULL }, /* Nd */
101: { NULL, NULL, NULL }, /* Nm */
102: { NULL, NULL, NULL }, /* Op */
103: { NULL, NULL, NULL }, /* Ot */
104: { want_ge1, NULL, NULL }, /* Pa */
105: { NULL, NULL, NULL }, /* Rv */
106: { NULL, NULL, NULL }, /* St */
107: { need_ge1, NULL, NULL }, /* Va */
108: { need_ge1, NULL, NULL }, /* Vt */
109: { need_le2, args_Xr, NULL }, /* Xr */
110: { need_ge1, args_ref, NULL }, /* %A */
111: { need_ge1, args_ref, NULL }, /* %B */
112: { need_ge1, args_ref, NULL }, /* %D */
113: { need_ge1, args_ref, NULL }, /* %I */
114: { need_ge1, args_ref, NULL }, /* %J */
115: { need_ge1, args_ref, NULL }, /* %N */
116: { need_ge1, args_ref, NULL }, /* %O */
117: { need_ge1, args_ref, NULL }, /* %P */
118: { need_ge1, args_ref, NULL }, /* %R */
119: { need_ge1, args_ref, NULL }, /* %T */
120: { need_ge1, args_ref, NULL }, /* %V */
121: { NULL, NULL, NULL }, /* Ac */
122: { NULL, NULL, NULL }, /* Ao */
123: { NULL, NULL, NULL }, /* Aq */
124: { need_le2, args_At, NULL }, /* At */
125: { NULL, NULL, NULL }, /* Bc */
126: { NULL, NULL, NULL }, /* Bf */
127: { NULL, NULL, NULL }, /* Bo */
128: { NULL, NULL, NULL }, /* Bq */
129: { assert_le1, NULL, NULL }, /* Bsx */
130: { assert_le1, NULL, NULL }, /* Bx */
131: { need_eq1, args_bool, NULL }, /* Db */
132: { NULL, NULL, NULL }, /* Dc */
133: { NULL, NULL, NULL }, /* Do */
134: { NULL, NULL, NULL }, /* Dq */
135: { NULL, NULL, NULL }, /* Ec */
1.13 ! kristaps 136: { NULL, NULL, NULL }, /* Ef */ /* -symbolic, etc. */
1.11 kristaps 137: { need_ge1, NULL, NULL }, /* Em */
138: { NULL, NULL, NULL }, /* Eo */
139: { assert_le1, NULL, NULL }, /* Fx */
140: { want_ge1, NULL, NULL }, /* Ms */
141: { NULL, NULL, NULL }, /* No */
142: { NULL, NULL, NULL }, /* Ns */
143: { assert_le1, NULL, NULL }, /* Nx */
144: { assert_le1, NULL, NULL }, /* Ox */
145: { NULL, NULL, NULL }, /* Pc */
1.13 ! kristaps 146: { NULL, NULL, NULL }, /* Pf */ /* 2 or more arguments */
1.11 kristaps 147: { NULL, NULL, NULL }, /* Po */
148: { NULL, NULL, NULL }, /* Pq */
149: { NULL, NULL, NULL }, /* Qc */
150: { NULL, NULL, NULL }, /* Ql */
151: { NULL, NULL, NULL }, /* Qo */
152: { NULL, NULL, NULL }, /* Qq */
153: { NULL, NULL, NULL }, /* Re */
154: { NULL, NULL, NULL }, /* Rs */
155: { NULL, NULL, NULL }, /* Sc */
156: { NULL, NULL, NULL }, /* So */
157: { NULL, NULL, NULL }, /* Sq */
158: { need_eq1, args_bool, NULL }, /* Sm */
159: { need_ge1, NULL, NULL }, /* Sx */
160: { need_ge1, NULL, NULL }, /* Sy */
161: { want_ge1, NULL, NULL }, /* Tn */
162: { assert_eq0, NULL, NULL }, /* Ux */
163: { NULL, NULL, NULL }, /* Xc */
164: { NULL, NULL, NULL }, /* Xo */
165: { NULL, NULL, NULL }, /* Fo */
166: { NULL, NULL, NULL }, /* Fc */
167: { NULL, NULL, NULL }, /* Oo */
168: { NULL, NULL, NULL }, /* Oc */
169: { NULL, NULL, NULL }, /* Bk */
170: { NULL, NULL, NULL }, /* Ek */
171: { need_eq0, NULL, NULL }, /* Bt */
172: { need_eq1, NULL, NULL }, /* Hf */
173: { NULL, NULL, NULL }, /* Fr */
174: { need_eq0, NULL, NULL }, /* Ud */
1.9 kristaps 175: };
1.6 kristaps 176:
177:
178: static int
1.9 kristaps 179: need_le2(struct mdoc *mdoc, int tok, int pos, int sz)
1.6 kristaps 180: {
1.11 kristaps 181: if (sz <= 2)
1.8 kristaps 182: return(1);
1.9 kristaps 183: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2));
1.6 kristaps 184: }
185:
186:
187: static int
1.9 kristaps 188: want_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
1.6 kristaps 189: {
1.9 kristaps 190: if (sz > 0)
1.8 kristaps 191: return(1);
1.9 kristaps 192: return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1));
1.6 kristaps 193: }
1.1 kristaps 194:
195:
196: static int
1.9 kristaps 197: want_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 198: {
1.9 kristaps 199: if (sz == 0)
200: return(1);
201: return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0));
1.1 kristaps 202: }
203:
204:
205: static int
1.9 kristaps 206: need_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 207: {
1.9 kristaps 208: if (sz == 0)
209: return(1);
210: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ0));
1.1 kristaps 211: }
212:
213:
214: static int
1.9 kristaps 215: assert_le1(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 216: {
1.8 kristaps 217:
1.9 kristaps 218: assert(sz <= 1);
1.8 kristaps 219: return(1);
220: }
1.1 kristaps 221:
222:
1.8 kristaps 223: static int
1.9 kristaps 224: assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
1.8 kristaps 225: {
1.1 kristaps 226:
1.9 kristaps 227: assert(sz == 0);
1.1 kristaps 228: return(1);
229: }
230:
231:
1.8 kristaps 232: static int
1.9 kristaps 233: need_eq1(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 234: {
1.9 kristaps 235: if (sz == 1)
236: return(1);
237: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ1));
1.1 kristaps 238: }
239:
240:
1.9 kristaps 241: static int
242: need_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 243: {
1.9 kristaps 244: if (sz > 0)
245: return(1);
246: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
1.1 kristaps 247: }
248:
249:
1.9 kristaps 250: static int
1.12 kristaps 251: no_head_child(struct mdoc *mdoc, int tok, int pos)
252: {
253:
254: /* TODO */
255: return(1);
256: }
257:
258:
259: static int
1.11 kristaps 260: need_head_child(struct mdoc *mdoc, int tok, int pos)
261: {
262: struct mdoc_node *n;
263:
264: assert(mdoc->last);
265: n = mdoc->last;
266:
267: assert(MDOC_BLOCK == n->type);
268: assert(n->child);
269:
270: n = n->child;
271:
272: if (MDOC_HEAD != n->type)
273: return(mdoc_err(mdoc, tok, pos, ERR_CHILD_HEAD));
274: if (n->child)
275: return(1);
276: return(mdoc_err(mdoc, tok, pos, ERR_CHILD_HEAD));
277: }
278:
279:
280: static int
281: args_Sh(struct mdoc *mdoc, int tok, int pos,
1.9 kristaps 282: int sz, const char *args[],
283: int argc, const struct mdoc_arg *argv)
1.1 kristaps 284: {
1.9 kristaps 285: enum mdoc_sec sec;
1.2 kristaps 286:
1.9 kristaps 287: sec = mdoc_atosec((size_t)sz, args);
288: if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn)
289: if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO))
1.8 kristaps 290: return(0);
1.9 kristaps 291: if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec)
292: return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME));
1.2 kristaps 293:
1.9 kristaps 294: return(1);
1.1 kristaps 295: }
296:
297:
1.9 kristaps 298: static int
1.11 kristaps 299: args_bool(struct mdoc *mdoc, int tok, int pos,
300: int sz, const char *args[],
301: int argc, const struct mdoc_arg *argv)
302: {
303:
304: assert(1 == sz);
305: if (xstrcmp(args[0], "on"))
306: return(1);
307: if (xstrcmp(args[0], "off"))
308: return(1);
309: return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGBAD));
310: }
311:
312:
313: static int
314: args_ref(struct mdoc *mdoc, int tok, int pos,
315: int sz, const char *args[],
316: int argc, const struct mdoc_arg *argv)
317: {
318: struct mdoc_node *n;
319:
320: assert(mdoc->last);
321: for (n = mdoc->last ; n; n = n->parent) {
322: if (MDOC_BLOCK != n->type)
323: continue;
324: if (MDOC_Rs != n->data.block.tok)
325: break;
326: return(1);
327: }
328:
329: return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NOCTX));
330: }
331:
332:
333: static int
334: args_Xr(struct mdoc *mdoc, int tok, int pos,
335: int sz, const char *args[],
336: int argc, const struct mdoc_arg *argv)
337: {
338:
339: if (1 == sz)
340: return(1);
341: if (0 == sz)
342: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
343:
344: if (MSEC_DEFAULT == mdoc_atomsec(args[1]))
345: return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGFORM));
346: return(1);
347: }
348:
349:
350: static int
351: args_At(struct mdoc *mdoc, int tok, int pos,
1.9 kristaps 352: int sz, const char *args[],
353: int argc, const struct mdoc_arg *argv)
1.1 kristaps 354: {
1.9 kristaps 355: int i;
1.1 kristaps 356:
1.9 kristaps 357: if (0 == sz)
1.8 kristaps 358: return(1);
1.6 kristaps 359:
1.9 kristaps 360: i = 0;
361: if (ATT_DEFAULT == mdoc_atoatt(args[i]))
362: i++;
1.1 kristaps 363:
1.9 kristaps 364: for ( ; i < sz; i++) {
365: if ( ! mdoc_isdelim(args[i]))
366: continue;
367: return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT));
1.8 kristaps 368: }
1.9 kristaps 369: return(1);
1.1 kristaps 370: }
371:
372:
1.9 kristaps 373: static int
1.11 kristaps 374: args_blocknest(struct mdoc *mdoc, int tok, int pos,
1.9 kristaps 375: int sz, const char *args[],
376: int argc, const struct mdoc_arg *argv)
1.1 kristaps 377: {
1.9 kristaps 378: struct mdoc_node *node;
1.8 kristaps 379:
380: /*
1.9 kristaps 381: * We can't be nested within any other block displays (or really
382: * any other kind of display, although Bd is the only multi-line
383: * one that will show up).
1.8 kristaps 384: */
1.9 kristaps 385: assert(mdoc->last);
1.8 kristaps 386:
1.9 kristaps 387: /* LINTED */
1.11 kristaps 388: for (node = mdoc->last; node; node = node->parent) {
1.9 kristaps 389: if (node->type != MDOC_BLOCK)
390: continue;
391: if (node->data.block.tok != MDOC_Bd)
1.8 kristaps 392: continue;
393: break;
394: }
1.9 kristaps 395: if (NULL == node)
396: return(1);
397: return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST));
1.8 kristaps 398: }
399:
400:
401: int
1.11 kristaps 402: mdoc_valid_pre(struct mdoc *mdoc, int tok, int pos,
1.9 kristaps 403: int sz, const char *args[],
404: int argc, const struct mdoc_arg *argv)
1.8 kristaps 405: {
406:
1.9 kristaps 407: assert(tok < MDOC_MAX);
408: if (mdoc_valids[tok].sz)
409: if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz))
1.5 kristaps 410: return(0);
1.8 kristaps 411:
1.11 kristaps 412: if (NULL == mdoc_valids[tok].args)
1.9 kristaps 413: return(1);
1.11 kristaps 414: return((*mdoc_valids[tok].args)(mdoc,
415: tok, pos, sz, args, argc, argv));
1.1 kristaps 416: }
417:
1.11 kristaps 418:
419: int
420: mdoc_valid_post(struct mdoc *mdoc, int tok, int pos)
421: {
422:
423: if (NULL == mdoc_valids[tok].post)
424: return(1);
425: return((*mdoc_valids[tok].post)(mdoc, tok, pos));
426: }
CVSweb