]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
improve SQL style: avoid "SELECT *", be explicit in what columns we want;
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.91 2014/04/20 16:46:04 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013, 2014 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 "man.h"
34 #include "mandoc.h"
35 #include "mandoc_aux.h"
36 #include "libman.h"
37 #include "libmandoc.h"
38
39 #define CHKARGS struct man *man, struct man_node *n
40
41 typedef int (*v_check)(CHKARGS);
42
43 struct man_valid {
44 v_check *pres;
45 v_check *posts;
46 };
47
48 static int check_eq0(CHKARGS);
49 static int check_eq2(CHKARGS);
50 static int check_le1(CHKARGS);
51 static int check_ge2(CHKARGS);
52 static int check_le5(CHKARGS);
53 static int check_head1(CHKARGS);
54 static int check_par(CHKARGS);
55 static int check_part(CHKARGS);
56 static int check_root(CHKARGS);
57 static void check_text(CHKARGS);
58
59 static int post_AT(CHKARGS);
60 static int post_IP(CHKARGS);
61 static int post_vs(CHKARGS);
62 static int post_fi(CHKARGS);
63 static int post_ft(CHKARGS);
64 static int post_nf(CHKARGS);
65 static int post_sec(CHKARGS);
66 static int post_TH(CHKARGS);
67 static int post_UC(CHKARGS);
68 static int pre_sec(CHKARGS);
69
70 static v_check posts_at[] = { post_AT, NULL };
71 static v_check posts_br[] = { post_vs, check_eq0, NULL };
72 static v_check posts_eq0[] = { check_eq0, NULL };
73 static v_check posts_eq2[] = { check_eq2, NULL };
74 static v_check posts_fi[] = { check_eq0, post_fi, NULL };
75 static v_check posts_ft[] = { post_ft, NULL };
76 static v_check posts_ip[] = { post_IP, NULL };
77 static v_check posts_le1[] = { check_le1, NULL };
78 static v_check posts_nf[] = { check_eq0, post_nf, NULL };
79 static v_check posts_par[] = { check_par, NULL };
80 static v_check posts_part[] = { check_part, NULL };
81 static v_check posts_sec[] = { post_sec, NULL };
82 static v_check posts_sp[] = { post_vs, check_le1, NULL };
83 static v_check posts_th[] = { check_ge2, check_le5, post_TH, NULL };
84 static v_check posts_uc[] = { post_UC, NULL };
85 static v_check posts_ur[] = { check_head1, check_part, NULL };
86 static v_check pres_sec[] = { pre_sec, NULL };
87
88 static const struct man_valid man_valids[MAN_MAX] = {
89 { NULL, posts_br }, /* br */
90 { NULL, posts_th }, /* TH */
91 { pres_sec, posts_sec }, /* SH */
92 { pres_sec, posts_sec }, /* SS */
93 { NULL, NULL }, /* TP */
94 { NULL, posts_par }, /* LP */
95 { NULL, posts_par }, /* PP */
96 { NULL, posts_par }, /* P */
97 { NULL, posts_ip }, /* IP */
98 { NULL, NULL }, /* HP */
99 { NULL, NULL }, /* SM */
100 { NULL, NULL }, /* SB */
101 { NULL, NULL }, /* BI */
102 { NULL, NULL }, /* IB */
103 { NULL, NULL }, /* BR */
104 { NULL, NULL }, /* RB */
105 { NULL, NULL }, /* R */
106 { NULL, NULL }, /* B */
107 { NULL, NULL }, /* I */
108 { NULL, NULL }, /* IR */
109 { NULL, NULL }, /* RI */
110 { NULL, posts_eq0 }, /* na */
111 { NULL, posts_sp }, /* sp */
112 { NULL, posts_nf }, /* nf */
113 { NULL, posts_fi }, /* fi */
114 { NULL, NULL }, /* RE */
115 { NULL, posts_part }, /* RS */
116 { NULL, NULL }, /* DT */
117 { NULL, posts_uc }, /* UC */
118 { NULL, posts_le1 }, /* PD */
119 { NULL, posts_at }, /* AT */
120 { NULL, NULL }, /* in */
121 { NULL, posts_ft }, /* ft */
122 { NULL, posts_eq2 }, /* OP */
123 { NULL, posts_nf }, /* EX */
124 { NULL, posts_fi }, /* EE */
125 { NULL, posts_ur }, /* UR */
126 { NULL, NULL }, /* UE */
127 { NULL, NULL }, /* ll */
128 };
129
130
131 int
132 man_valid_pre(struct man *man, struct man_node *n)
133 {
134 v_check *cp;
135
136 switch (n->type) {
137 case MAN_TEXT:
138 /* FALLTHROUGH */
139 case MAN_ROOT:
140 /* FALLTHROUGH */
141 case MAN_EQN:
142 /* FALLTHROUGH */
143 case MAN_TBL:
144 return(1);
145 default:
146 break;
147 }
148
149 if (NULL == (cp = man_valids[n->tok].pres))
150 return(1);
151 for ( ; *cp; cp++)
152 if ( ! (*cp)(man, n))
153 return(0);
154 return(1);
155 }
156
157 int
158 man_valid_post(struct man *man)
159 {
160 v_check *cp;
161
162 if (MAN_VALID & man->last->flags)
163 return(1);
164 man->last->flags |= MAN_VALID;
165
166 switch (man->last->type) {
167 case MAN_TEXT:
168 check_text(man, man->last);
169 return(1);
170 case MAN_ROOT:
171 return(check_root(man, man->last));
172 case MAN_EQN:
173 /* FALLTHROUGH */
174 case MAN_TBL:
175 return(1);
176 default:
177 break;
178 }
179
180 if (NULL == (cp = man_valids[man->last->tok].posts))
181 return(1);
182 for ( ; *cp; cp++)
183 if ( ! (*cp)(man, man->last))
184 return(0);
185
186 return(1);
187 }
188
189 static int
190 check_root(CHKARGS)
191 {
192
193 if (MAN_BLINE & man->flags)
194 man_nmsg(man, n, MANDOCERR_SCOPEEXIT);
195 else if (MAN_ELINE & man->flags)
196 man_nmsg(man, n, MANDOCERR_SCOPEEXIT);
197
198 man->flags &= ~MAN_BLINE;
199 man->flags &= ~MAN_ELINE;
200
201 if (NULL == man->first->child) {
202 man_nmsg(man, n, MANDOCERR_NODOCBODY);
203 return(0);
204 } else if (NULL == man->meta.title) {
205 man_nmsg(man, n, MANDOCERR_NOTITLE);
206
207 /*
208 * If a title hasn't been set, do so now (by
209 * implication, date and section also aren't set).
210 */
211
212 man->meta.title = mandoc_strdup("unknown");
213 man->meta.msec = mandoc_strdup("1");
214 man->meta.date = man->quick ? mandoc_strdup("") :
215 mandoc_normdate(man->parse, NULL, n->line, n->pos);
216 }
217
218 return(1);
219 }
220
221 static void
222 check_text(CHKARGS)
223 {
224 char *cp, *p;
225
226 if (MAN_LITERAL & man->flags)
227 return;
228
229 cp = n->string;
230 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
231 man_pmsg(man, n->line, (int)(p - cp), MANDOCERR_BADTAB);
232 }
233
234 #define INEQ_DEFINE(x, ineq, name) \
235 static int \
236 check_##name(CHKARGS) \
237 { \
238 if (n->nchild ineq (x)) \
239 return(1); \
240 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line, n->pos, \
241 "line arguments %s %d (have %d)", \
242 #ineq, (x), n->nchild); \
243 return(1); \
244 }
245
246 INEQ_DEFINE(0, ==, eq0)
247 INEQ_DEFINE(2, ==, eq2)
248 INEQ_DEFINE(1, <=, le1)
249 INEQ_DEFINE(2, >=, ge2)
250 INEQ_DEFINE(5, <=, le5)
251
252 static int
253 check_head1(CHKARGS)
254 {
255
256 if (MAN_HEAD == n->type && 1 != n->nchild)
257 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
258 n->pos, "line arguments eq 1 (have %d)", n->nchild);
259
260 return(1);
261 }
262
263 static int
264 post_ft(CHKARGS)
265 {
266 char *cp;
267 int ok;
268
269 if (0 == n->nchild)
270 return(1);
271
272 ok = 0;
273 cp = n->child->string;
274 switch (*cp) {
275 case '1':
276 /* FALLTHROUGH */
277 case '2':
278 /* FALLTHROUGH */
279 case '3':
280 /* FALLTHROUGH */
281 case '4':
282 /* FALLTHROUGH */
283 case 'I':
284 /* FALLTHROUGH */
285 case 'P':
286 /* FALLTHROUGH */
287 case 'R':
288 if ('\0' == cp[1])
289 ok = 1;
290 break;
291 case 'B':
292 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
293 ok = 1;
294 break;
295 case 'C':
296 if ('W' == cp[1] && '\0' == cp[2])
297 ok = 1;
298 break;
299 default:
300 break;
301 }
302
303 if (0 == ok) {
304 mandoc_vmsg(MANDOCERR_BADFONT, man->parse, n->line,
305 n->pos, "%s", cp);
306 *cp = '\0';
307 }
308
309 if (1 < n->nchild)
310 mandoc_vmsg(MANDOCERR_ARGCOUNT, man->parse, n->line,
311 n->pos, "want one child (have %d)", n->nchild);
312
313 return(1);
314 }
315
316 static int
317 pre_sec(CHKARGS)
318 {
319
320 if (MAN_BLOCK == n->type)
321 man->flags &= ~MAN_LITERAL;
322 return(1);
323 }
324
325 static int
326 post_sec(CHKARGS)
327 {
328
329 if ( ! (MAN_HEAD == n->type && 0 == n->nchild))
330 return(1);
331
332 man_nmsg(man, n, MANDOCERR_SYNTARGCOUNT);
333 return(0);
334 }
335
336 static int
337 check_part(CHKARGS)
338 {
339
340 if (MAN_BODY == n->type && 0 == n->nchild)
341 mandoc_msg(MANDOCERR_ARGCWARN, man->parse, n->line,
342 n->pos, "want children (have none)");
343
344 return(1);
345 }
346
347 static int
348 check_par(CHKARGS)
349 {
350
351 switch (n->type) {
352 case MAN_BLOCK:
353 if (0 == n->body->nchild)
354 man_node_delete(man, n);
355 break;
356 case MAN_BODY:
357 if (0 == n->nchild)
358 man_nmsg(man, n, MANDOCERR_IGNPAR);
359 break;
360 case MAN_HEAD:
361 if (n->nchild)
362 man_nmsg(man, n, MANDOCERR_ARGSLOST);
363 break;
364 default:
365 break;
366 }
367
368 return(1);
369 }
370
371 static int
372 post_IP(CHKARGS)
373 {
374
375 switch (n->type) {
376 case MAN_BLOCK:
377 if (0 == n->head->nchild && 0 == n->body->nchild)
378 man_node_delete(man, n);
379 break;
380 case MAN_BODY:
381 if (0 == n->parent->head->nchild && 0 == n->nchild)
382 man_nmsg(man, n, MANDOCERR_IGNPAR);
383 break;
384 default:
385 break;
386 }
387 return(1);
388 }
389
390 static int
391 post_TH(CHKARGS)
392 {
393 const char *p;
394
395 free(man->meta.title);
396 free(man->meta.vol);
397 free(man->meta.source);
398 free(man->meta.msec);
399 free(man->meta.date);
400
401 man->meta.title = man->meta.vol = man->meta.date =
402 man->meta.msec = man->meta.source = NULL;
403
404 /* ->TITLE<- MSEC DATE SOURCE VOL */
405
406 n = n->child;
407 if (n && n->string) {
408 for (p = n->string; '\0' != *p; p++) {
409 /* Only warn about this once... */
410 if (isalpha((unsigned char)*p) &&
411 ! isupper((unsigned char)*p)) {
412 man_nmsg(man, n, MANDOCERR_UPPERCASE);
413 break;
414 }
415 }
416 man->meta.title = mandoc_strdup(n->string);
417 } else
418 man->meta.title = mandoc_strdup("");
419
420 /* TITLE ->MSEC<- DATE SOURCE VOL */
421
422 if (n)
423 n = n->next;
424 if (n && n->string)
425 man->meta.msec = mandoc_strdup(n->string);
426 else
427 man->meta.msec = mandoc_strdup("");
428
429 /* TITLE MSEC ->DATE<- SOURCE VOL */
430
431 if (n)
432 n = n->next;
433 if (n && n->string && '\0' != n->string[0]) {
434 man->meta.date = man->quick ?
435 mandoc_strdup(n->string) :
436 mandoc_normdate(man->parse, n->string,
437 n->line, n->pos);
438 } else
439 man->meta.date = mandoc_strdup("");
440
441 /* TITLE MSEC DATE ->SOURCE<- VOL */
442
443 if (n && (n = n->next))
444 man->meta.source = mandoc_strdup(n->string);
445
446 /* TITLE MSEC DATE SOURCE ->VOL<- */
447 /* If missing, use the default VOL name for MSEC. */
448
449 if (n && (n = n->next))
450 man->meta.vol = mandoc_strdup(n->string);
451 else if ('\0' != man->meta.msec[0] &&
452 (NULL != (p = mandoc_a2msec(man->meta.msec))))
453 man->meta.vol = mandoc_strdup(p);
454
455 /*
456 * Remove the `TH' node after we've processed it for our
457 * meta-data.
458 */
459 man_node_delete(man, man->last);
460 return(1);
461 }
462
463 static int
464 post_nf(CHKARGS)
465 {
466
467 if (MAN_LITERAL & man->flags)
468 man_nmsg(man, n, MANDOCERR_SCOPEREP);
469
470 man->flags |= MAN_LITERAL;
471 return(1);
472 }
473
474 static int
475 post_fi(CHKARGS)
476 {
477
478 if ( ! (MAN_LITERAL & man->flags))
479 man_nmsg(man, n, MANDOCERR_WNOSCOPE);
480
481 man->flags &= ~MAN_LITERAL;
482 return(1);
483 }
484
485 static int
486 post_UC(CHKARGS)
487 {
488 static const char * const bsd_versions[] = {
489 "3rd Berkeley Distribution",
490 "4th Berkeley Distribution",
491 "4.2 Berkeley Distribution",
492 "4.3 Berkeley Distribution",
493 "4.4 Berkeley Distribution",
494 };
495
496 const char *p, *s;
497
498 n = n->child;
499
500 if (NULL == n || MAN_TEXT != n->type)
501 p = bsd_versions[0];
502 else {
503 s = n->string;
504 if (0 == strcmp(s, "3"))
505 p = bsd_versions[0];
506 else if (0 == strcmp(s, "4"))
507 p = bsd_versions[1];
508 else if (0 == strcmp(s, "5"))
509 p = bsd_versions[2];
510 else if (0 == strcmp(s, "6"))
511 p = bsd_versions[3];
512 else if (0 == strcmp(s, "7"))
513 p = bsd_versions[4];
514 else
515 p = bsd_versions[0];
516 }
517
518 free(man->meta.source);
519 man->meta.source = mandoc_strdup(p);
520 return(1);
521 }
522
523 static int
524 post_AT(CHKARGS)
525 {
526 static const char * const unix_versions[] = {
527 "7th Edition",
528 "System III",
529 "System V",
530 "System V Release 2",
531 };
532
533 const char *p, *s;
534 struct man_node *nn;
535
536 n = n->child;
537
538 if (NULL == n || MAN_TEXT != n->type)
539 p = unix_versions[0];
540 else {
541 s = n->string;
542 if (0 == strcmp(s, "3"))
543 p = unix_versions[0];
544 else if (0 == strcmp(s, "4"))
545 p = unix_versions[1];
546 else if (0 == strcmp(s, "5")) {
547 nn = n->next;
548 if (nn && MAN_TEXT == nn->type && nn->string[0])
549 p = unix_versions[3];
550 else
551 p = unix_versions[2];
552 } else
553 p = unix_versions[0];
554 }
555
556 free(man->meta.source);
557 man->meta.source = mandoc_strdup(p);
558 return(1);
559 }
560
561 static int
562 post_vs(CHKARGS)
563 {
564
565 if (NULL != n->prev)
566 return(1);
567
568 switch (n->parent->tok) {
569 case MAN_SH:
570 /* FALLTHROUGH */
571 case MAN_SS:
572 man_nmsg(man, n, MANDOCERR_IGNPAR);
573 /* FALLTHROUGH */
574 case MAN_MAX:
575 /*
576 * Don't warn about this because it occurs in pod2man
577 * and would cause considerable (unfixable) warnage.
578 */
579 man_node_delete(man, n);
580 break;
581 default:
582 break;
583 }
584
585 return(1);
586 }