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