| 1 | /* $Id: man_validate.c,v 1.33 2010/03/29 10:10:35 kristaps Exp $ */ |
| 2 | /* |
| 3 | * Copyright (c) 2008, 2009 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 above |
| 7 | * copyright notice and this permission notice appear in all copies. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | */ |
| 17 | #include <sys/types.h> |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdlib.h> |
| 25 | |
| 26 | #include "libman.h" |
| 27 | #include "libmandoc.h" |
| 28 | |
| 29 | #define CHKARGS struct man *m, const struct man_node *n |
| 30 | |
| 31 | typedef int (*v_check)(CHKARGS); |
| 32 | |
| 33 | struct man_valid { |
| 34 | v_check *pres; |
| 35 | v_check *posts; |
| 36 | }; |
| 37 | |
| 38 | static int check_bline(CHKARGS); |
| 39 | static int check_eq0(CHKARGS); |
| 40 | static int check_le1(CHKARGS); |
| 41 | static int check_ge2(CHKARGS); |
| 42 | static int check_le5(CHKARGS); |
| 43 | static int check_par(CHKARGS); |
| 44 | static int check_part(CHKARGS); |
| 45 | static int check_roff(CHKARGS); |
| 46 | static int check_root(CHKARGS); |
| 47 | static int check_sec(CHKARGS); |
| 48 | static int check_text(CHKARGS); |
| 49 | static int check_title(CHKARGS); |
| 50 | |
| 51 | static v_check posts_eq0[] = { check_eq0, NULL }; |
| 52 | static v_check posts_th[] = { check_ge2, check_le5, check_title, NULL }; |
| 53 | static v_check posts_par[] = { check_par, NULL }; |
| 54 | static v_check posts_part[] = { check_part, NULL }; |
| 55 | static v_check posts_sec[] = { check_sec, NULL }; |
| 56 | static v_check posts_le1[] = { check_le1, NULL }; |
| 57 | static v_check pres_bline[] = { check_bline, NULL }; |
| 58 | static v_check pres_roff[] = { check_roff, NULL }; |
| 59 | |
| 60 | static const struct man_valid man_valids[MAN_MAX] = { |
| 61 | { NULL, posts_eq0 }, /* br */ |
| 62 | { pres_bline, posts_th }, /* TH */ |
| 63 | { pres_bline, posts_sec }, /* SH */ |
| 64 | { pres_bline, posts_sec }, /* SS */ |
| 65 | { pres_bline, posts_par }, /* TP */ |
| 66 | { pres_bline, posts_par }, /* LP */ |
| 67 | { pres_bline, posts_par }, /* PP */ |
| 68 | { pres_bline, posts_par }, /* P */ |
| 69 | { pres_bline, posts_par }, /* IP */ |
| 70 | { pres_bline, posts_par }, /* HP */ |
| 71 | { NULL, NULL }, /* SM */ |
| 72 | { NULL, NULL }, /* SB */ |
| 73 | { NULL, NULL }, /* BI */ |
| 74 | { NULL, NULL }, /* IB */ |
| 75 | { NULL, NULL }, /* BR */ |
| 76 | { NULL, NULL }, /* RB */ |
| 77 | { NULL, NULL }, /* R */ |
| 78 | { NULL, NULL }, /* B */ |
| 79 | { NULL, NULL }, /* I */ |
| 80 | { NULL, NULL }, /* IR */ |
| 81 | { NULL, NULL }, /* RI */ |
| 82 | { NULL, posts_eq0 }, /* na */ |
| 83 | { NULL, NULL }, /* i */ |
| 84 | { NULL, posts_le1 }, /* sp */ |
| 85 | { pres_bline, posts_eq0 }, /* nf */ |
| 86 | { pres_bline, posts_eq0 }, /* fi */ |
| 87 | { NULL, NULL }, /* r */ |
| 88 | { NULL, NULL }, /* RE */ |
| 89 | { NULL, posts_part }, /* RS */ |
| 90 | { NULL, NULL }, /* DT */ |
| 91 | { NULL, NULL }, /* UC */ |
| 92 | { NULL, NULL }, /* PD */ |
| 93 | { NULL, posts_eq0 }, /* Sp */ |
| 94 | { pres_bline, posts_le1 }, /* Vb */ |
| 95 | { pres_bline, posts_eq0 }, /* Ve */ |
| 96 | { pres_roff, NULL }, /* de */ |
| 97 | { pres_roff, NULL }, /* dei */ |
| 98 | { pres_roff, NULL }, /* am */ |
| 99 | { pres_roff, NULL }, /* ami */ |
| 100 | { pres_roff, NULL }, /* ig */ |
| 101 | { NULL, NULL }, /* . */ |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | int |
| 106 | man_valid_pre(struct man *m, const struct man_node *n) |
| 107 | { |
| 108 | v_check *cp; |
| 109 | |
| 110 | if (MAN_TEXT == n->type) |
| 111 | return(1); |
| 112 | if (MAN_ROOT == n->type) |
| 113 | return(1); |
| 114 | |
| 115 | if (NULL == (cp = man_valids[n->tok].pres)) |
| 116 | return(1); |
| 117 | for ( ; *cp; cp++) |
| 118 | if ( ! (*cp)(m, n)) |
| 119 | return(0); |
| 120 | return(1); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | int |
| 125 | man_valid_post(struct man *m) |
| 126 | { |
| 127 | v_check *cp; |
| 128 | |
| 129 | if (MAN_VALID & m->last->flags) |
| 130 | return(1); |
| 131 | m->last->flags |= MAN_VALID; |
| 132 | |
| 133 | switch (m->last->type) { |
| 134 | case (MAN_TEXT): |
| 135 | return(check_text(m, m->last)); |
| 136 | case (MAN_ROOT): |
| 137 | return(check_root(m, m->last)); |
| 138 | default: |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | if (NULL == (cp = man_valids[m->last->tok].posts)) |
| 143 | return(1); |
| 144 | for ( ; *cp; cp++) |
| 145 | if ( ! (*cp)(m, m->last)) |
| 146 | return(0); |
| 147 | |
| 148 | return(1); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static int |
| 153 | check_root(CHKARGS) |
| 154 | { |
| 155 | |
| 156 | if (MAN_BLINE & m->flags) |
| 157 | return(man_nwarn(m, n, WEXITSCOPE)); |
| 158 | if (MAN_ELINE & m->flags) |
| 159 | return(man_nwarn(m, n, WEXITSCOPE)); |
| 160 | |
| 161 | m->flags &= ~MAN_BLINE; |
| 162 | m->flags &= ~MAN_ELINE; |
| 163 | |
| 164 | if (NULL == m->first->child) |
| 165 | return(man_nerr(m, n, WNODATA)); |
| 166 | if (NULL == m->meta.title) |
| 167 | return(man_nerr(m, n, WNOTITLE)); |
| 168 | |
| 169 | return(1); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static int |
| 174 | check_title(CHKARGS) |
| 175 | { |
| 176 | const char *p; |
| 177 | |
| 178 | assert(n->child); |
| 179 | if ('\0' == *n->child->string) |
| 180 | return(man_nerr(m, n, WNOTITLE)); |
| 181 | |
| 182 | for (p = n->child->string; '\0' != *p; p++) |
| 183 | if (isalpha((u_char)*p) && ! isupper((u_char)*p)) |
| 184 | if ( ! man_nwarn(m, n, WTITLECASE)) |
| 185 | return(0); |
| 186 | |
| 187 | return(1); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static int |
| 192 | check_text(CHKARGS) |
| 193 | { |
| 194 | const char *p; |
| 195 | int pos, c; |
| 196 | |
| 197 | assert(n->string); |
| 198 | |
| 199 | for (p = n->string, pos = n->pos + 1; *p; p++, pos++) { |
| 200 | if ('\\' == *p) { |
| 201 | c = mandoc_special(p); |
| 202 | if (c) { |
| 203 | p += c - 1; |
| 204 | pos += c - 1; |
| 205 | continue; |
| 206 | } |
| 207 | if ( ! (MAN_IGN_ESCAPE & m->pflags)) |
| 208 | return(man_perr(m, n->line, pos, WESCAPE)); |
| 209 | if ( ! man_pwarn(m, n->line, pos, WESCAPE)) |
| 210 | return(0); |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | if ('\t' == *p || isprint((u_char)*p)) |
| 215 | continue; |
| 216 | |
| 217 | if (MAN_IGN_CHARS & m->pflags) |
| 218 | return(man_pwarn(m, n->line, pos, WNPRINT)); |
| 219 | return(man_perr(m, n->line, pos, WNPRINT)); |
| 220 | } |
| 221 | |
| 222 | return(1); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | #define INEQ_DEFINE(x, ineq, name) \ |
| 227 | static int \ |
| 228 | check_##name(CHKARGS) \ |
| 229 | { \ |
| 230 | if (n->nchild ineq (x)) \ |
| 231 | return(1); \ |
| 232 | return(man_verr(m, n->line, n->pos, \ |
| 233 | "expected line arguments %s %d, have %d", \ |
| 234 | #ineq, (x), n->nchild)); \ |
| 235 | } |
| 236 | |
| 237 | INEQ_DEFINE(0, ==, eq0) |
| 238 | INEQ_DEFINE(1, <=, le1) |
| 239 | INEQ_DEFINE(2, >=, ge2) |
| 240 | INEQ_DEFINE(5, <=, le5) |
| 241 | |
| 242 | |
| 243 | static int |
| 244 | check_sec(CHKARGS) |
| 245 | { |
| 246 | |
| 247 | if (MAN_BODY == n->type && 0 == n->nchild) |
| 248 | return(man_nwarn(m, n, WBODYARGS)); |
| 249 | if (MAN_HEAD == n->type && 0 == n->nchild) |
| 250 | return(man_nerr(m, n, WHEADARGS)); |
| 251 | return(1); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | static int |
| 256 | check_part(CHKARGS) |
| 257 | { |
| 258 | |
| 259 | if (MAN_BODY == n->type && 0 == n->nchild) |
| 260 | return(man_nwarn(m, n, WBODYARGS)); |
| 261 | return(1); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | static int |
| 266 | check_par(CHKARGS) |
| 267 | { |
| 268 | |
| 269 | if (MAN_BODY == n->type) |
| 270 | switch (n->tok) { |
| 271 | case (MAN_IP): |
| 272 | /* FALLTHROUGH */ |
| 273 | case (MAN_HP): |
| 274 | /* FALLTHROUGH */ |
| 275 | case (MAN_TP): |
| 276 | /* Body-less lists are ok. */ |
| 277 | break; |
| 278 | default: |
| 279 | if (n->nchild) |
| 280 | break; |
| 281 | return(man_nwarn(m, n, WBODYARGS)); |
| 282 | } |
| 283 | if (MAN_HEAD == n->type) |
| 284 | switch (n->tok) { |
| 285 | case (MAN_PP): |
| 286 | /* FALLTHROUGH */ |
| 287 | case (MAN_P): |
| 288 | /* FALLTHROUGH */ |
| 289 | case (MAN_LP): |
| 290 | if (0 == n->nchild) |
| 291 | break; |
| 292 | return(man_nwarn(m, n, WNHEADARGS)); |
| 293 | default: |
| 294 | if (n->nchild) |
| 295 | break; |
| 296 | return(man_nwarn(m, n, WHEADARGS)); |
| 297 | } |
| 298 | |
| 299 | return(1); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | static int |
| 304 | check_bline(CHKARGS) |
| 305 | { |
| 306 | |
| 307 | assert( ! (MAN_ELINE & m->flags)); |
| 308 | if (MAN_BLINE & m->flags) |
| 309 | return(man_nerr(m, n, WLNSCOPE)); |
| 310 | |
| 311 | return(1); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | static int |
| 316 | check_roff(CHKARGS) |
| 317 | { |
| 318 | |
| 319 | if (MAN_BLOCK != n->type) |
| 320 | return(1); |
| 321 | |
| 322 | for (n = n->parent; n; n = n->parent) |
| 323 | if (MAN_de == n->tok || MAN_dei == n->tok || |
| 324 | MAN_am == n->tok || |
| 325 | MAN_ami == n->tok || |
| 326 | MAN_ig == n->tok) |
| 327 | return(man_nerr(m, n, WROFFNEST)); |
| 328 | |
| 329 | return(1); |
| 330 | } |