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