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