]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Clean-up in libman: make all calls to man_*msg not check return value.
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.64 2011/03/17 11:56:17 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 "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 man_pmsg(m, n->line, pos, MANDOCERR_BADTAB);
227 continue;
228 }
229
230 /* Check the special character. */
231
232 c = mandoc_special(p);
233 if (c) {
234 p += c - 1;
235 pos += c - 1;
236 } else
237 man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
238 }
239
240 return(1);
241 }
242
243
244 #define INEQ_DEFINE(x, ineq, name) \
245 static int \
246 check_##name(CHKARGS) \
247 { \
248 if (n->nchild ineq (x)) \
249 return(1); \
250 man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos, \
251 "line arguments %s %d (have %d)", \
252 #ineq, (x), n->nchild); \
253 return(1); \
254 }
255
256 INEQ_DEFINE(0, ==, eq0)
257 INEQ_DEFINE(1, <=, le1)
258 INEQ_DEFINE(2, >=, ge2)
259 INEQ_DEFINE(5, <=, le5)
260
261 static int
262 check_ft(CHKARGS)
263 {
264 char *cp;
265 int ok;
266
267 if (0 == n->nchild)
268 return(1);
269
270 ok = 0;
271 cp = n->child->string;
272 switch (*cp) {
273 case ('1'):
274 /* FALLTHROUGH */
275 case ('2'):
276 /* FALLTHROUGH */
277 case ('3'):
278 /* FALLTHROUGH */
279 case ('4'):
280 /* FALLTHROUGH */
281 case ('I'):
282 /* FALLTHROUGH */
283 case ('P'):
284 /* FALLTHROUGH */
285 case ('R'):
286 if ('\0' == cp[1])
287 ok = 1;
288 break;
289 case ('B'):
290 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
291 ok = 1;
292 break;
293 case ('C'):
294 if ('W' == cp[1] && '\0' == cp[2])
295 ok = 1;
296 break;
297 default:
298 break;
299 }
300
301 if (0 == ok) {
302 man_vmsg(m, MANDOCERR_BADFONT,
303 n->line, n->pos, "%s", cp);
304 *cp = '\0';
305 }
306
307 if (1 < n->nchild)
308 man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos,
309 "want one child (have %d)", n->nchild);
310
311 return(1);
312 }
313
314 static int
315 check_sec(CHKARGS)
316 {
317
318 if (MAN_HEAD == n->type && 0 == n->nchild) {
319 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
320 return(0);
321 } else if (MAN_BODY == n->type && 0 == n->nchild)
322 man_nmsg(m, n, MANDOCERR_NOBODY);
323
324 return(1);
325 }
326
327
328 static int
329 check_part(CHKARGS)
330 {
331
332 if (MAN_BODY == n->type && 0 == n->nchild)
333 man_nmsg(m, n, MANDOCERR_NOBODY);
334
335 return(1);
336 }
337
338
339 static int
340 check_par(CHKARGS)
341 {
342
343 switch (n->type) {
344 case (MAN_BLOCK):
345 if (0 == n->body->nchild)
346 man_node_delete(m, n);
347 break;
348 case (MAN_BODY):
349 if (0 == n->nchild)
350 man_nmsg(m, n, MANDOCERR_IGNPAR);
351 break;
352 case (MAN_HEAD):
353 if (n->nchild)
354 man_nmsg(m, n, MANDOCERR_ARGSLOST);
355 break;
356 default:
357 break;
358 }
359
360 return(1);
361 }
362
363
364 static int
365 check_bline(CHKARGS)
366 {
367
368 assert( ! (MAN_ELINE & m->flags));
369 if (MAN_BLINE & m->flags) {
370 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
371 return(0);
372 }
373
374 return(1);
375 }
376
377 static int
378 post_TH(CHKARGS)
379 {
380 const char *p;
381 int line, pos;
382
383 if (m->meta.title)
384 free(m->meta.title);
385 if (m->meta.vol)
386 free(m->meta.vol);
387 if (m->meta.source)
388 free(m->meta.source);
389 if (m->meta.msec)
390 free(m->meta.msec);
391 if (m->meta.date)
392 free(m->meta.date);
393
394 line = n->line;
395 pos = n->pos;
396 m->meta.title = m->meta.vol = m->meta.date =
397 m->meta.msec = m->meta.source = NULL;
398
399 /* ->TITLE<- MSEC DATE SOURCE VOL */
400
401 n = n->child;
402 if (n && n->string) {
403 for (p = n->string; '\0' != *p; p++) {
404 /* Only warn about this once... */
405 if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
406 man_nmsg(m, n, MANDOCERR_UPPERCASE);
407 break;
408 }
409 }
410 m->meta.title = mandoc_strdup(n->string);
411 } else
412 m->meta.title = mandoc_strdup("");
413
414 /* TITLE ->MSEC<- DATE SOURCE VOL */
415
416 if (n)
417 n = n->next;
418 if (n && n->string)
419 m->meta.msec = mandoc_strdup(n->string);
420 else
421 m->meta.msec = mandoc_strdup("");
422
423 /* TITLE MSEC ->DATE<- SOURCE VOL */
424
425 if (n)
426 n = n->next;
427 if (n)
428 pos = n->pos;
429 m->meta.date = mandoc_normdate(n ? n->string : NULL,
430 m->msg, m->data, line, pos);
431
432 /* TITLE MSEC DATE ->SOURCE<- VOL */
433
434 if (n && (n = n->next))
435 m->meta.source = mandoc_strdup(n->string);
436
437 /* TITLE MSEC DATE SOURCE ->VOL<- */
438
439 if (n && (n = n->next))
440 m->meta.vol = mandoc_strdup(n->string);
441
442 /*
443 * Remove the `TH' node after we've processed it for our
444 * meta-data.
445 */
446 man_node_delete(m, m->last);
447 return(1);
448 }
449
450 static int
451 post_nf(CHKARGS)
452 {
453
454 if (MAN_LITERAL & m->flags)
455 man_nmsg(m, n, MANDOCERR_SCOPEREP);
456
457 m->flags |= MAN_LITERAL;
458 return(1);
459 }
460
461 static int
462 post_fi(CHKARGS)
463 {
464
465 if ( ! (MAN_LITERAL & m->flags))
466 man_nmsg(m, n, MANDOCERR_WNOSCOPE);
467
468 m->flags &= ~MAN_LITERAL;
469 return(1);
470 }
471
472 static int
473 post_UC(CHKARGS)
474 {
475 static const char * const bsd_versions[] = {
476 "3rd Berkeley Distribution",
477 "4th Berkeley Distribution",
478 "4.2 Berkeley Distribution",
479 "4.3 Berkeley Distribution",
480 "4.4 Berkeley Distribution",
481 };
482
483 const char *p, *s;
484
485 n = n->child;
486 n = m->last->child;
487
488 if (NULL == n || MAN_TEXT != n->type)
489 p = bsd_versions[0];
490 else {
491 s = n->string;
492 if (0 == strcmp(s, "3"))
493 p = bsd_versions[0];
494 else if (0 == strcmp(s, "4"))
495 p = bsd_versions[1];
496 else if (0 == strcmp(s, "5"))
497 p = bsd_versions[2];
498 else if (0 == strcmp(s, "6"))
499 p = bsd_versions[3];
500 else if (0 == strcmp(s, "7"))
501 p = bsd_versions[4];
502 else
503 p = bsd_versions[0];
504 }
505
506 if (m->meta.source)
507 free(m->meta.source);
508
509 m->meta.source = mandoc_strdup(p);
510 return(1);
511 }
512
513 static int
514 post_AT(CHKARGS)
515 {
516 static const char * const unix_versions[] = {
517 "7th Edition",
518 "System III",
519 "System V",
520 "System V Release 2",
521 };
522
523 const char *p, *s;
524 struct man_node *nn;
525
526 n = n->child;
527
528 if (NULL == n || MAN_TEXT != n->type)
529 p = unix_versions[0];
530 else {
531 s = n->string;
532 if (0 == strcmp(s, "3"))
533 p = unix_versions[0];
534 else if (0 == strcmp(s, "4"))
535 p = unix_versions[1];
536 else if (0 == strcmp(s, "5")) {
537 nn = n->next;
538 if (nn && MAN_TEXT == nn->type && nn->string[0])
539 p = unix_versions[3];
540 else
541 p = unix_versions[2];
542 } else
543 p = unix_versions[0];
544 }
545
546 if (m->meta.source)
547 free(m->meta.source);
548
549 m->meta.source = mandoc_strdup(p);
550 return(1);
551 }