]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Fix enum/int mixing.
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.51 2010/11/30 15:24:27 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "mandoc.h"
33 #include "libman.h"
34 #include "libmandoc.h"
35
36 #define CHKARGS struct man *m, struct man_node *n
37
38 typedef int (*v_check)(CHKARGS);
39
40 struct man_valid {
41 v_check *pres;
42 v_check *posts;
43 };
44
45 static int check_bline(CHKARGS);
46 static int check_eq0(CHKARGS);
47 static int check_le1(CHKARGS);
48 static int check_ge2(CHKARGS);
49 static int check_le5(CHKARGS);
50 static int check_par(CHKARGS);
51 static int check_part(CHKARGS);
52 static int check_root(CHKARGS);
53 static int check_sec(CHKARGS);
54 static int check_text(CHKARGS);
55 static int check_title(CHKARGS);
56
57 static int post_AT(CHKARGS);
58 static int post_fi(CHKARGS);
59 static int post_nf(CHKARGS);
60 static int post_TH(CHKARGS);
61 static int post_UC(CHKARGS);
62
63 static v_check posts_at[] = { post_AT, NULL };
64 static v_check posts_eq0[] = { check_eq0, NULL };
65 static v_check posts_fi[] = { check_eq0, post_fi, NULL };
66 static v_check posts_le1[] = { check_le1, NULL };
67 static v_check posts_nf[] = { check_eq0, post_nf, NULL };
68 static v_check posts_par[] = { check_par, NULL };
69 static v_check posts_part[] = { check_part, NULL };
70 static v_check posts_sec[] = { check_sec, NULL };
71 static v_check posts_th[] = { check_ge2, check_le5, check_title, post_TH, NULL };
72 static v_check posts_uc[] = { post_UC, NULL };
73 static v_check posts_vb[] = { check_le1, post_nf, NULL };
74 static v_check pres_bline[] = { check_bline, NULL };
75
76
77 static const struct man_valid man_valids[MAN_MAX] = {
78 { NULL, posts_eq0 }, /* br */
79 { pres_bline, posts_th }, /* TH */
80 { pres_bline, posts_sec }, /* SH */
81 { pres_bline, posts_sec }, /* SS */
82 { pres_bline, posts_par }, /* TP */
83 { pres_bline, posts_par }, /* LP */
84 { pres_bline, posts_par }, /* PP */
85 { pres_bline, posts_par }, /* P */
86 { pres_bline, posts_par }, /* IP */
87 { pres_bline, posts_par }, /* HP */
88 { NULL, NULL }, /* SM */
89 { NULL, NULL }, /* SB */
90 { NULL, NULL }, /* BI */
91 { NULL, NULL }, /* IB */
92 { NULL, NULL }, /* BR */
93 { NULL, NULL }, /* RB */
94 { NULL, NULL }, /* R */
95 { NULL, NULL }, /* B */
96 { NULL, NULL }, /* I */
97 { NULL, NULL }, /* IR */
98 { NULL, NULL }, /* RI */
99 { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
100 { NULL, NULL }, /* i */
101 { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
102 { pres_bline, posts_nf }, /* nf */
103 { pres_bline, posts_fi }, /* fi */
104 { NULL, NULL }, /* r */
105 { NULL, NULL }, /* RE */
106 { NULL, posts_part }, /* RS */
107 { NULL, NULL }, /* DT */
108 { NULL, posts_uc }, /* UC */
109 { NULL, NULL }, /* PD */
110 { NULL, posts_le1 }, /* Sp */ /* FIXME: should warn only. */
111 { pres_bline, posts_vb }, /* Vb */ /* FIXME: should warn only. */
112 { pres_bline, posts_fi }, /* Ve */
113 { NULL, posts_at }, /* AT */
114 { NULL, NULL }, /* in */
115 };
116
117
118 int
119 man_valid_pre(struct man *m, struct man_node *n)
120 {
121 v_check *cp;
122
123 if (MAN_TEXT == n->type)
124 return(1);
125 if (MAN_ROOT == n->type)
126 return(1);
127
128 if (NULL == (cp = man_valids[n->tok].pres))
129 return(1);
130 for ( ; *cp; cp++)
131 if ( ! (*cp)(m, n))
132 return(0);
133 return(1);
134 }
135
136
137 int
138 man_valid_post(struct man *m)
139 {
140 v_check *cp;
141
142 if (MAN_VALID & m->last->flags)
143 return(1);
144 m->last->flags |= MAN_VALID;
145
146 switch (m->last->type) {
147 case (MAN_TEXT):
148 return(check_text(m, m->last));
149 case (MAN_ROOT):
150 return(check_root(m, m->last));
151 default:
152 break;
153 }
154
155 if (NULL == (cp = man_valids[m->last->tok].posts))
156 return(1);
157 for ( ; *cp; cp++)
158 if ( ! (*cp)(m, m->last))
159 return(0);
160
161 return(1);
162 }
163
164
165 static int
166 check_root(CHKARGS)
167 {
168
169 if (MAN_BLINE & m->flags)
170 return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
171 if (MAN_ELINE & m->flags)
172 return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
173
174 m->flags &= ~MAN_BLINE;
175 m->flags &= ~MAN_ELINE;
176
177 if (NULL == m->first->child) {
178 man_nmsg(m, n, MANDOCERR_NODOCBODY);
179 return(0);
180 } else if (NULL == m->meta.title) {
181 if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
182 return(0);
183 /*
184 * If a title hasn't been set, do so now (by
185 * implication, date and section also aren't set).
186 *
187 * FIXME: this should be in man_action.c.
188 */
189 m->meta.title = mandoc_strdup("unknown");
190 m->meta.date = time(NULL);
191 m->meta.msec = mandoc_strdup("1");
192 }
193
194 return(1);
195 }
196
197
198 static int
199 check_title(CHKARGS)
200 {
201 const char *p;
202
203 assert(n->child);
204 /* FIXME: is this sufficient? */
205 if ('\0' == *n->child->string) {
206 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
207 return(0);
208 }
209
210 for (p = n->child->string; '\0' != *p; p++)
211 if (isalpha((u_char)*p) && ! isupper((u_char)*p))
212 if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
213 return(0);
214
215 return(1);
216 }
217
218
219 static int
220 check_text(CHKARGS)
221 {
222 char *p;
223 int pos, c;
224 size_t sz;
225
226 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
227 sz = strcspn(p, "\t\\");
228 p += (int)sz;
229
230 if ('\0' == *p)
231 break;
232
233 pos += (int)sz;
234
235 if ('\t' == *p) {
236 if (MAN_LITERAL & m->flags)
237 continue;
238 if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
239 continue;
240 return(0);
241 }
242
243 /* Check the special character. */
244
245 c = mandoc_special(p);
246 if (c) {
247 p += c - 1;
248 pos += c - 1;
249 } else
250 man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
251 }
252
253 return(1);
254 }
255
256
257 #define INEQ_DEFINE(x, ineq, name) \
258 static int \
259 check_##name(CHKARGS) \
260 { \
261 if (n->nchild ineq (x)) \
262 return(1); \
263 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
264 "line arguments %s %d (have %d)", \
265 #ineq, (x), n->nchild); \
266 return(0); \
267 }
268
269 INEQ_DEFINE(0, ==, eq0)
270 INEQ_DEFINE(1, <=, le1)
271 INEQ_DEFINE(2, >=, ge2)
272 INEQ_DEFINE(5, <=, le5)
273
274
275 static int
276 check_sec(CHKARGS)
277 {
278
279 if (MAN_HEAD == n->type && 0 == n->nchild) {
280 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
281 return(0);
282 } else if (MAN_BODY == n->type && 0 == n->nchild)
283 return(man_nmsg(m, n, MANDOCERR_NOBODY));
284
285 return(1);
286 }
287
288
289 static int
290 check_part(CHKARGS)
291 {
292
293 if (MAN_BODY == n->type && 0 == n->nchild)
294 return(man_nmsg(m, n, MANDOCERR_NOBODY));
295 return(1);
296 }
297
298
299 static int
300 check_par(CHKARGS)
301 {
302
303 if (MAN_BODY == n->type)
304 switch (n->tok) {
305 case (MAN_IP):
306 /* FALLTHROUGH */
307 case (MAN_HP):
308 /* FALLTHROUGH */
309 case (MAN_TP):
310 /* Body-less lists are ok. */
311 break;
312 default:
313 if (n->nchild)
314 break;
315 return(man_nmsg(m, n, MANDOCERR_NOBODY));
316 }
317 if (MAN_HEAD == n->type)
318 switch (n->tok) {
319 case (MAN_PP):
320 /* FALLTHROUGH */
321 case (MAN_P):
322 /* FALLTHROUGH */
323 case (MAN_LP):
324 if (0 == n->nchild)
325 break;
326 return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
327 default:
328 if (n->nchild)
329 break;
330 return(man_nmsg(m, n, MANDOCERR_NOARGS));
331 }
332
333 return(1);
334 }
335
336
337 static int
338 check_bline(CHKARGS)
339 {
340
341 assert( ! (MAN_ELINE & m->flags));
342 if (MAN_BLINE & m->flags) {
343 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
344 return(0);
345 }
346
347 return(1);
348 }
349
350 static int
351 post_TH(CHKARGS)
352 {
353
354 if (m->meta.title)
355 free(m->meta.title);
356 if (m->meta.vol)
357 free(m->meta.vol);
358 if (m->meta.source)
359 free(m->meta.source);
360 if (m->meta.msec)
361 free(m->meta.msec);
362 if (m->meta.rawdate)
363 free(m->meta.rawdate);
364
365 m->meta.title = m->meta.vol = m->meta.rawdate =
366 m->meta.msec = m->meta.source = NULL;
367 m->meta.date = 0;
368
369 /* ->TITLE<- MSEC DATE SOURCE VOL */
370
371 n = n->child;
372 assert(n);
373 m->meta.title = mandoc_strdup(n->string);
374
375 /* TITLE ->MSEC<- DATE SOURCE VOL */
376
377 n = n->next;
378 assert(n);
379 m->meta.msec = mandoc_strdup(n->string);
380
381 /* TITLE MSEC ->DATE<- SOURCE VOL */
382
383 /*
384 * Try to parse the date. If this works, stash the epoch (this
385 * is optimal because we can reformat it in the canonical form).
386 * If it doesn't parse, isn't specified at all, or is an empty
387 * string, then use the current date.
388 */
389
390 n = n->next;
391 if (n && n->string && *n->string) {
392 m->meta.date = mandoc_a2time
393 (MTIME_ISO_8601, n->string);
394 if (0 == m->meta.date) {
395 man_nmsg(m, n, MANDOCERR_BADDATE);
396 m->meta.rawdate = mandoc_strdup(n->string);
397 }
398 } else
399 m->meta.date = time(NULL);
400
401 /* TITLE MSEC DATE ->SOURCE<- VOL */
402
403 if (n && (n = n->next))
404 m->meta.source = mandoc_strdup(n->string);
405
406 /* TITLE MSEC DATE SOURCE ->VOL<- */
407
408 if (n && (n = n->next))
409 m->meta.vol = mandoc_strdup(n->string);
410
411 /*
412 * Remove the `TH' node after we've processed it for our
413 * meta-data.
414 */
415 man_node_delete(m, m->last);
416 return(1);
417 }
418
419 static int
420 post_nf(CHKARGS)
421 {
422
423 if (MAN_LITERAL & m->flags)
424 man_nmsg(m, n, MANDOCERR_SCOPEREP);
425
426 m->flags |= MAN_LITERAL;
427 return(1);
428 }
429
430 static int
431 post_fi(CHKARGS)
432 {
433
434 if ( ! (MAN_LITERAL & m->flags))
435 man_nmsg(m, n, MANDOCERR_NOSCOPE);
436
437 m->flags &= ~MAN_LITERAL;
438 return(1);
439 }
440
441 static int
442 post_UC(CHKARGS)
443 {
444 static const char * const bsd_versions[] = {
445 "3rd Berkeley Distribution",
446 "4th Berkeley Distribution",
447 "4.2 Berkeley Distribution",
448 "4.3 Berkeley Distribution",
449 "4.4 Berkeley Distribution",
450 };
451
452 const char *p, *s;
453
454 n = n->child;
455 n = m->last->child;
456
457 if (NULL == n || MAN_TEXT != n->type)
458 p = bsd_versions[0];
459 else {
460 s = n->string;
461 if (0 == strcmp(s, "3"))
462 p = bsd_versions[0];
463 else if (0 == strcmp(s, "4"))
464 p = bsd_versions[1];
465 else if (0 == strcmp(s, "5"))
466 p = bsd_versions[2];
467 else if (0 == strcmp(s, "6"))
468 p = bsd_versions[3];
469 else if (0 == strcmp(s, "7"))
470 p = bsd_versions[4];
471 else
472 p = bsd_versions[0];
473 }
474
475 if (m->meta.source)
476 free(m->meta.source);
477
478 m->meta.source = mandoc_strdup(p);
479 return(1);
480 }
481
482 static int
483 post_AT(CHKARGS)
484 {
485 static const char * const unix_versions[] = {
486 "7th Edition",
487 "System III",
488 "System V",
489 "System V Release 2",
490 };
491
492 const char *p, *s;
493 struct man_node *nn;
494
495 n = n->child;
496
497 if (NULL == n || MAN_TEXT != n->type)
498 p = unix_versions[0];
499 else {
500 s = n->string;
501 if (0 == strcmp(s, "3"))
502 p = unix_versions[0];
503 else if (0 == strcmp(s, "4"))
504 p = unix_versions[1];
505 else if (0 == strcmp(s, "5")) {
506 nn = n->next;
507 if (nn && MAN_TEXT == nn->type && nn->string[0])
508 p = unix_versions[3];
509 else
510 p = unix_versions[2];
511 } else
512 p = unix_versions[0];
513 }
514
515 if (m->meta.source)
516 free(m->meta.source);
517
518 m->meta.source = mandoc_strdup(p);
519 return(1);
520 }