Annotation of mandoc/validate.c, Revision 1.9
1.9 ! kristaps 1: /* $Id: validate.c,v 1.8 2008/12/30 13:43:53 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.2 kristaps 24:
1.9 ! kristaps 25: typedef int (*v_sz)(struct mdoc *, int, int, int);
! 26: typedef int (*v_extra)(struct mdoc *, int, int, int,
! 27: const char *[], int, const struct mdoc_arg *);
1.1 kristaps 28:
1.9 ! kristaps 29: static int assert_eq0(struct mdoc *, int, int, int);
! 30: static int assert_le1(struct mdoc *, int, int, int);
! 31: static int need_eq0(struct mdoc *, int, int, int);
! 32: static int need_eq1(struct mdoc *, int, int, int);
! 33: static int need_ge1(struct mdoc *, int, int, int);
! 34: static int need_le2(struct mdoc *, int, int, int);
! 35: static int want_eq0(struct mdoc *, int, int, int);
! 36: static int want_ge1(struct mdoc *, int, int, int);
! 37: static int v_Sh(struct mdoc *, int, int, int,
! 38: const char *[], int, const struct mdoc_arg *);
! 39: static int v_Bd(struct mdoc *, int, int, int,
! 40: const char *[], int, const struct mdoc_arg *);
! 41: static int v_At(struct mdoc *, int, int, int,
1.8 kristaps 42: const char *[], int, const struct mdoc_arg *);
1.9 ! kristaps 43:
! 44: struct valids {
! 45: v_sz sz;
! 46: v_extra extra;
! 47: };
! 48:
! 49:
! 50: const struct valids mdoc_valids[MDOC_MAX] = {
! 51: { NULL, NULL }, /* \" */
! 52: { NULL, NULL }, /* Dd */
! 53: { NULL, NULL }, /* Dt */
! 54: { NULL, NULL }, /* Os */
! 55: { need_ge1, v_Sh }, /* Sh */
! 56: { need_ge1, NULL }, /* Ss */
! 57: { want_eq0, NULL }, /* Pp */
! 58: { NULL, NULL }, /* D1 */
! 59: { NULL, NULL }, /* Dl */
! 60: { NULL, v_Bd }, /* Bd */
! 61: { NULL, NULL }, /* Ed */
! 62: { NULL, NULL }, /* Bl */
! 63: { NULL, NULL }, /* El */
! 64: { NULL, NULL }, /* It */
! 65: { need_ge1, NULL }, /* Ad */
! 66: { NULL, NULL }, /* An */
! 67: { NULL, NULL }, /* Ar */
! 68: { NULL, NULL }, /* Cd */
! 69: { NULL, NULL }, /* Cm */
! 70: { need_ge1, NULL }, /* Dv */
! 71: { need_ge1, NULL }, /* Er */
! 72: { need_ge1, NULL }, /* Ev */
! 73: { NULL, NULL }, /* Ex */
! 74: { need_ge1, NULL }, /* Fa */
! 75: { NULL, NULL }, /* Fd */
! 76: { NULL, NULL }, /* Fl */
! 77: { NULL, NULL }, /* Fn */
! 78: { want_ge1, NULL }, /* Ft */
! 79: { need_ge1, NULL }, /* Ic */
! 80: { NULL, NULL }, /* In */
! 81: { want_ge1, NULL }, /* Li */
! 82: { want_ge1, NULL }, /* Nd */
! 83: { NULL, NULL }, /* Nm */
! 84: { NULL, NULL }, /* Op */
! 85: { NULL, NULL }, /* Ot */
! 86: { want_ge1, NULL }, /* Pa */
! 87: { NULL, NULL }, /* Rv */
! 88: { NULL, NULL }, /* St */
! 89: { need_ge1, NULL }, /* Va */
! 90: { need_ge1, NULL }, /* Vt */
! 91: { NULL, NULL }, /* Xr */
! 92: { NULL, NULL }, /* %A */
! 93: { NULL, NULL }, /* %B */
! 94: { NULL, NULL }, /* %D */
! 95: { NULL, NULL }, /* %I */
! 96: { NULL, NULL }, /* %J */
! 97: { NULL, NULL }, /* %N */
! 98: { NULL, NULL }, /* %O */
! 99: { NULL, NULL }, /* %P */
! 100: { NULL, NULL }, /* %R */
! 101: { NULL, NULL }, /* %T */
! 102: { NULL, NULL }, /* %V */
! 103: { NULL, NULL }, /* Ac */
! 104: { NULL, NULL }, /* Ao */
! 105: { NULL, NULL }, /* Aq */
! 106: { need_le2, v_At }, /* At */
! 107: { NULL, NULL }, /* Bc */
! 108: { NULL, NULL }, /* Bf */
! 109: { NULL, NULL }, /* Bo */
! 110: { NULL, NULL }, /* Bq */
! 111: { assert_le1, NULL }, /* Bsx */
! 112: { assert_le1, NULL }, /* Bx */
! 113: { NULL, NULL }, /* Db */
! 114: { NULL, NULL }, /* Dc */
! 115: { NULL, NULL }, /* Do */
! 116: { NULL, NULL }, /* Dq */
! 117: { NULL, NULL }, /* Ec */
! 118: { NULL, NULL }, /* Ef */
! 119: { need_ge1, NULL }, /* Em */
! 120: { NULL, NULL }, /* Eo */
! 121: { assert_le1, NULL }, /* Fx */
! 122: { want_ge1, NULL }, /* Ms */
! 123: { NULL, NULL }, /* No */
! 124: { NULL, NULL }, /* Ns */
! 125: { assert_le1, NULL }, /* Nx */
! 126: { assert_le1, NULL }, /* Ox */
! 127: { NULL, NULL }, /* Pc */
! 128: { NULL, NULL }, /* Pf */
! 129: { NULL, NULL }, /* Po */
! 130: { NULL, NULL }, /* Pq */
! 131: { NULL, NULL }, /* Qc */
! 132: { NULL, NULL }, /* Ql */
! 133: { NULL, NULL }, /* Qo */
! 134: { NULL, NULL }, /* Qq */
! 135: { NULL, NULL }, /* Re */
! 136: { NULL, NULL }, /* Rs */
! 137: { NULL, NULL }, /* Sc */
! 138: { NULL, NULL }, /* So */
! 139: { NULL, NULL }, /* Sq */
! 140: { NULL, NULL }, /* Sm */
! 141: { need_ge1, NULL }, /* Sx */
! 142: { need_ge1, NULL }, /* Sy */
! 143: { want_ge1, NULL }, /* Tn */
! 144: { assert_eq0, NULL }, /* Ux */
! 145: { NULL, NULL }, /* Xc */
! 146: { NULL, NULL }, /* Xo */
! 147: { NULL, NULL }, /* Fo */
! 148: { NULL, NULL }, /* Fc */
! 149: { NULL, NULL }, /* Oo */
! 150: { NULL, NULL }, /* Oc */
! 151: { NULL, NULL }, /* Bk */
! 152: { NULL, NULL }, /* Ek */
! 153: { need_eq0, NULL }, /* Bt */
! 154: { need_eq1, NULL }, /* Hf */
! 155: { NULL, NULL }, /* Fr */
! 156: { need_eq0, NULL }, /* Ud */
! 157: };
1.6 kristaps 158:
159:
160: static int
1.9 ! kristaps 161: need_le2(struct mdoc *mdoc, int tok, int pos, int sz)
1.6 kristaps 162: {
1.9 ! kristaps 163: if (sz > 2)
1.8 kristaps 164: return(1);
1.9 ! kristaps 165: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_LE2));
1.6 kristaps 166: }
167:
168:
169: static int
1.9 ! kristaps 170: want_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
1.6 kristaps 171: {
1.9 ! kristaps 172: if (sz > 0)
1.8 kristaps 173: return(1);
1.9 ! kristaps 174: return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1));
1.6 kristaps 175: }
1.1 kristaps 176:
177:
178: static int
1.9 ! kristaps 179: want_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 180: {
1.9 ! kristaps 181: if (sz == 0)
! 182: return(1);
! 183: return(mdoc_warn(mdoc, tok, pos, WARN_ARGS_EQ0));
1.1 kristaps 184: }
185:
186:
187: static int
1.9 ! kristaps 188: need_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 189: {
1.9 ! kristaps 190: if (sz == 0)
! 191: return(1);
! 192: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ0));
1.1 kristaps 193: }
194:
195:
196: static int
1.9 ! kristaps 197: assert_le1(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 198: {
1.8 kristaps 199:
1.9 ! kristaps 200: assert(sz <= 1);
1.8 kristaps 201: return(1);
202: }
1.1 kristaps 203:
204:
1.8 kristaps 205: static int
1.9 ! kristaps 206: assert_eq0(struct mdoc *mdoc, int tok, int pos, int sz)
1.8 kristaps 207: {
1.1 kristaps 208:
1.9 ! kristaps 209: assert(sz == 0);
1.1 kristaps 210: return(1);
211: }
212:
213:
1.8 kristaps 214: static int
1.9 ! kristaps 215: need_eq1(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 216: {
1.9 ! kristaps 217: if (sz == 1)
! 218: return(1);
! 219: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_EQ1));
1.1 kristaps 220: }
221:
222:
1.9 ! kristaps 223: static int
! 224: need_ge1(struct mdoc *mdoc, int tok, int pos, int sz)
1.1 kristaps 225: {
1.9 ! kristaps 226: if (sz > 0)
! 227: return(1);
! 228: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
1.1 kristaps 229: }
230:
231:
1.9 ! kristaps 232: static int
! 233: v_Sh(struct mdoc *mdoc, int tok, int pos,
! 234: int sz, const char *args[],
! 235: int argc, const struct mdoc_arg *argv)
1.1 kristaps 236: {
1.9 ! kristaps 237: enum mdoc_sec sec;
1.2 kristaps 238:
1.9 ! kristaps 239: sec = mdoc_atosec((size_t)sz, args);
! 240: if (SEC_CUSTOM != sec && sec < mdoc->sec_lastn)
! 241: if ( ! mdoc_warn(mdoc, tok, pos, WARN_SEC_OO))
1.8 kristaps 242: return(0);
1.9 ! kristaps 243: if (SEC_BODY == mdoc->sec_last && SEC_NAME != sec)
! 244: return(mdoc_err(mdoc, tok, pos, ERR_SEC_NAME));
1.2 kristaps 245:
1.9 ! kristaps 246: return(1);
1.1 kristaps 247: }
248:
249:
1.9 ! kristaps 250: static int
! 251: v_At(struct mdoc *mdoc, int tok, int pos,
! 252: int sz, const char *args[],
! 253: int argc, const struct mdoc_arg *argv)
1.1 kristaps 254: {
1.9 ! kristaps 255: int i;
1.1 kristaps 256:
1.9 ! kristaps 257: if (0 == sz)
1.8 kristaps 258: return(1);
1.6 kristaps 259:
1.9 ! kristaps 260: i = 0;
! 261: if (ATT_DEFAULT == mdoc_atoatt(args[i]))
! 262: i++;
1.1 kristaps 263:
1.9 ! kristaps 264: for ( ; i < sz; i++) {
! 265: if ( ! mdoc_isdelim(args[i]))
! 266: continue;
! 267: return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_NOPUNCT));
1.8 kristaps 268: }
1.9 ! kristaps 269: return(1);
1.1 kristaps 270: }
271:
272:
1.9 ! kristaps 273: static int
! 274: v_Bd(struct mdoc *mdoc, int tok, int pos,
! 275: int sz, const char *args[],
! 276: int argc, const struct mdoc_arg *argv)
1.1 kristaps 277: {
1.9 ! kristaps 278: struct mdoc_node *node;
1.8 kristaps 279:
280: /*
1.9 ! kristaps 281: * We can't be nested within any other block displays (or really
! 282: * any other kind of display, although Bd is the only multi-line
! 283: * one that will show up).
1.8 kristaps 284: */
1.9 ! kristaps 285: assert(mdoc->last);
1.8 kristaps 286:
1.9 ! kristaps 287: /* LINTED */
! 288: for (node = mdoc->last->parent ; node; node = node->parent) {
! 289: if (node->type != MDOC_BLOCK)
! 290: continue;
! 291: if (node->data.block.tok != MDOC_Bd)
1.8 kristaps 292: continue;
293: break;
294: }
1.9 ! kristaps 295: if (NULL == node)
! 296: return(1);
! 297: return(mdoc_err(mdoc, tok, pos, ERR_SCOPE_NONEST));
1.8 kristaps 298: }
299:
300:
301: int
1.9 ! kristaps 302: mdoc_valid(struct mdoc *mdoc, int tok, int pos,
! 303: int sz, const char *args[],
! 304: int argc, const struct mdoc_arg *argv)
1.8 kristaps 305: {
306:
1.9 ! kristaps 307: assert(tok < MDOC_MAX);
! 308: if (mdoc_valids[tok].sz)
! 309: if ( ! (*mdoc_valids[tok].sz)(mdoc, tok, pos, sz))
1.5 kristaps 310: return(0);
1.8 kristaps 311:
1.9 ! kristaps 312: if (NULL == mdoc_valids[tok].extra)
! 313: return(1);
! 314: return(*mdoc_valids[tok].extra)(mdoc,
! 315: tok, pos, sz, args, argc, argv);
1.1 kristaps 316: }
317:
CVSweb