Import mdocml-1.11.3
[dragonfly.git] / contrib / mdocml / man_validate.c
CommitLineData
a4c7eb57 1/* $Id: man_validate.c,v 1.69 2011/04/13 09:57:08 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);
a4c7eb57 57static void 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):
a4c7eb57
SW
154 check_text(m, m->last);
155 return(1);
80387638
SW
156 case (MAN_ROOT):
157 return(check_root(m, m->last));
60e1e752
SW
158 case (MAN_EQN):
159 /* FALLTHROUGH */
80387638
SW
160 case (MAN_TBL):
161 return(1);
162 default:
163 break;
164 }
165
166 if (NULL == (cp = man_valids[m->last->tok].posts))
167 return(1);
168 for ( ; *cp; cp++)
169 if ( ! (*cp)(m, m->last))
170 return(0);
171
172 return(1);
173}
174
175
176static int
177check_root(CHKARGS)
178{
179
180 if (MAN_BLINE & m->flags)
181 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
182 else if (MAN_ELINE & m->flags)
183 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
184
185 m->flags &= ~MAN_BLINE;
186 m->flags &= ~MAN_ELINE;
187
188 if (NULL == m->first->child) {
189 man_nmsg(m, n, MANDOCERR_NODOCBODY);
190 return(0);
191 } else if (NULL == m->meta.title) {
192 man_nmsg(m, n, MANDOCERR_NOTITLE);
193
194 /*
195 * If a title hasn't been set, do so now (by
196 * implication, date and section also aren't set).
197 */
198
199 m->meta.title = mandoc_strdup("unknown");
80387638 200 m->meta.msec = mandoc_strdup("1");
60e1e752
SW
201 m->meta.date = mandoc_normdate
202 (m->parse, NULL, n->line, n->pos);
80387638
SW
203 }
204
205 return(1);
206}
207
a4c7eb57 208static void
80387638
SW
209check_text(CHKARGS)
210{
a4c7eb57
SW
211 char *p, *pp, *cpp;
212 int pos;
80387638
SW
213 size_t sz;
214
a4c7eb57
SW
215 p = n->string;
216 pos = n->pos + 1;
80387638 217
a4c7eb57
SW
218 while ('\0' != *p) {
219 sz = strcspn(p, "\t\\");
80387638 220
a4c7eb57 221 p += (int)sz;
80387638
SW
222 pos += (int)sz;
223
224 if ('\t' == *p) {
a4c7eb57
SW
225 if ( ! (MAN_LITERAL & m->flags))
226 man_pmsg(m, n->line, pos, MANDOCERR_BADTAB);
227 p++;
228 pos++;
60e1e752 229 continue;
a4c7eb57
SW
230 } else if ('\0' == *p)
231 break;
80387638 232
a4c7eb57
SW
233 pos++;
234 pp = ++p;
80387638 235
a4c7eb57
SW
236 if (ESCAPE_ERROR == mandoc_escape
237 ((const char **)&pp, NULL, NULL)) {
80387638 238 man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
a4c7eb57
SW
239 break;
240 }
80387638 241
a4c7eb57
SW
242 cpp = p;
243 while (NULL != (cpp = memchr(cpp, ASCII_HYPH, pp - cpp)))
244 *cpp = '-';
80387638 245
a4c7eb57
SW
246 pos += pp - p;
247 p = pp;
248 }
249}
80387638
SW
250
251#define INEQ_DEFINE(x, ineq, name) \
252static int \
253check_##name(CHKARGS) \
254{ \
255 if (n->nchild ineq (x)) \
256 return(1); \
60e1e752 257 mandoc_vmsg(MANDOCERR_ARGCOUNT, m->parse, n->line, n->pos, \
80387638
SW
258 "line arguments %s %d (have %d)", \
259 #ineq, (x), n->nchild); \
60e1e752 260 return(1); \
80387638
SW
261}
262
263INEQ_DEFINE(0, ==, eq0)
264INEQ_DEFINE(1, <=, le1)
265INEQ_DEFINE(2, >=, ge2)
266INEQ_DEFINE(5, <=, le5)
267
268static int
269check_ft(CHKARGS)
270{
271 char *cp;
272 int ok;
273
274 if (0 == n->nchild)
275 return(1);
276
277 ok = 0;
278 cp = n->child->string;
279 switch (*cp) {
280 case ('1'):
281 /* FALLTHROUGH */
282 case ('2'):
283 /* FALLTHROUGH */
284 case ('3'):
285 /* FALLTHROUGH */
286 case ('4'):
287 /* FALLTHROUGH */
288 case ('I'):
289 /* FALLTHROUGH */
290 case ('P'):
291 /* FALLTHROUGH */
292 case ('R'):
293 if ('\0' == cp[1])
294 ok = 1;
295 break;
296 case ('B'):
297 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
298 ok = 1;
299 break;
300 case ('C'):
301 if ('W' == cp[1] && '\0' == cp[2])
302 ok = 1;
303 break;
304 default:
305 break;
306 }
307
308 if (0 == ok) {
60e1e752
SW
309 mandoc_vmsg
310 (MANDOCERR_BADFONT, m->parse,
311 n->line, n->pos, "%s", cp);
80387638
SW
312 *cp = '\0';
313 }
314
315 if (1 < n->nchild)
60e1e752
SW
316 mandoc_vmsg
317 (MANDOCERR_ARGCOUNT, m->parse, n->line,
318 n->pos, "want one child (have %d)",
319 n->nchild);
80387638
SW
320
321 return(1);
322}
323
324static int
325check_sec(CHKARGS)
326{
327
a4c7eb57
SW
328 if ( ! (MAN_HEAD == n->type && 0 == n->nchild))
329 return(1);
80387638 330
a4c7eb57
SW
331 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
332 return(0);
80387638
SW
333}
334
335
336static int
337check_part(CHKARGS)
338{
339
340 if (MAN_BODY == n->type && 0 == n->nchild)
60e1e752
SW
341 mandoc_msg(MANDOCERR_ARGCWARN, m->parse, n->line,
342 n->pos, "want children (have none)");
80387638
SW
343
344 return(1);
345}
346
347
348static int
349check_par(CHKARGS)
350{
351
60e1e752
SW
352 switch (n->type) {
353 case (MAN_BLOCK):
354 if (0 == n->body->nchild)
355 man_node_delete(m, n);
356 break;
357 case (MAN_BODY):
358 if (0 == n->nchild)
359 man_nmsg(m, n, MANDOCERR_IGNPAR);
360 break;
361 case (MAN_HEAD):
362 if (n->nchild)
363 man_nmsg(m, n, MANDOCERR_ARGSLOST);
364 break;
365 default:
366 break;
367 }
80387638
SW
368
369 return(1);
370}
371
372
373static int
374check_bline(CHKARGS)
375{
376
377 assert( ! (MAN_ELINE & m->flags));
378 if (MAN_BLINE & m->flags) {
379 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
380 return(0);
381 }
382
383 return(1);
384}
385
386static int
387post_TH(CHKARGS)
388{
60e1e752
SW
389 const char *p;
390 int line, pos;
80387638
SW
391
392 if (m->meta.title)
393 free(m->meta.title);
394 if (m->meta.vol)
395 free(m->meta.vol);
396 if (m->meta.source)
397 free(m->meta.source);
398 if (m->meta.msec)
399 free(m->meta.msec);
60e1e752
SW
400 if (m->meta.date)
401 free(m->meta.date);
80387638 402
60e1e752
SW
403 line = n->line;
404 pos = n->pos;
405 m->meta.title = m->meta.vol = m->meta.date =
80387638 406 m->meta.msec = m->meta.source = NULL;
80387638
SW
407
408 /* ->TITLE<- MSEC DATE SOURCE VOL */
409
410 n = n->child;
60e1e752
SW
411 if (n && n->string) {
412 for (p = n->string; '\0' != *p; p++) {
413 /* Only warn about this once... */
414 if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
415 man_nmsg(m, n, MANDOCERR_UPPERCASE);
416 break;
417 }
418 }
419 m->meta.title = mandoc_strdup(n->string);
420 } else
421 m->meta.title = mandoc_strdup("");
80387638
SW
422
423 /* TITLE ->MSEC<- DATE SOURCE VOL */
424
60e1e752
SW
425 if (n)
426 n = n->next;
427 if (n && n->string)
428 m->meta.msec = mandoc_strdup(n->string);
429 else
430 m->meta.msec = mandoc_strdup("");
80387638
SW
431
432 /* TITLE MSEC ->DATE<- SOURCE VOL */
433
60e1e752
SW
434 if (n)
435 n = n->next;
436 if (n)
437 pos = n->pos;
438 m->meta.date = mandoc_normdate
439 (m->parse, n ? n->string : NULL, line, pos);
80387638
SW
440
441 /* TITLE MSEC DATE ->SOURCE<- VOL */
442
443 if (n && (n = n->next))
444 m->meta.source = mandoc_strdup(n->string);
445
446 /* TITLE MSEC DATE SOURCE ->VOL<- */
447
448 if (n && (n = n->next))
449 m->meta.vol = mandoc_strdup(n->string);
450
451 /*
452 * Remove the `TH' node after we've processed it for our
453 * meta-data.
454 */
455 man_node_delete(m, m->last);
456 return(1);
457}
458
459static int
460post_nf(CHKARGS)
461{
462
463 if (MAN_LITERAL & m->flags)
464 man_nmsg(m, n, MANDOCERR_SCOPEREP);
465
466 m->flags |= MAN_LITERAL;
467 return(1);
468}
469
470static int
471post_fi(CHKARGS)
472{
473
474 if ( ! (MAN_LITERAL & m->flags))
60e1e752 475 man_nmsg(m, n, MANDOCERR_WNOSCOPE);
80387638
SW
476
477 m->flags &= ~MAN_LITERAL;
478 return(1);
479}
480
481static int
482post_UC(CHKARGS)
483{
484 static const char * const bsd_versions[] = {
485 "3rd Berkeley Distribution",
486 "4th Berkeley Distribution",
487 "4.2 Berkeley Distribution",
488 "4.3 Berkeley Distribution",
489 "4.4 Berkeley Distribution",
490 };
491
492 const char *p, *s;
493
494 n = n->child;
495 n = m->last->child;
496
497 if (NULL == n || MAN_TEXT != n->type)
498 p = bsd_versions[0];
499 else {
500 s = n->string;
501 if (0 == strcmp(s, "3"))
502 p = bsd_versions[0];
503 else if (0 == strcmp(s, "4"))
504 p = bsd_versions[1];
505 else if (0 == strcmp(s, "5"))
506 p = bsd_versions[2];
507 else if (0 == strcmp(s, "6"))
508 p = bsd_versions[3];
509 else if (0 == strcmp(s, "7"))
510 p = bsd_versions[4];
511 else
512 p = bsd_versions[0];
513 }
514
515 if (m->meta.source)
516 free(m->meta.source);
517
518 m->meta.source = mandoc_strdup(p);
519 return(1);
520}
521
522static int
523post_AT(CHKARGS)
524{
525 static const char * const unix_versions[] = {
526 "7th Edition",
527 "System III",
528 "System V",
529 "System V Release 2",
530 };
531
532 const char *p, *s;
533 struct man_node *nn;
534
535 n = n->child;
536
537 if (NULL == n || MAN_TEXT != n->type)
538 p = unix_versions[0];
539 else {
540 s = n->string;
541 if (0 == strcmp(s, "3"))
542 p = unix_versions[0];
543 else if (0 == strcmp(s, "4"))
544 p = unix_versions[1];
545 else if (0 == strcmp(s, "5")) {
546 nn = n->next;
547 if (nn && MAN_TEXT == nn->type && nn->string[0])
548 p = unix_versions[3];
549 else
550 p = unix_versions[2];
551 } else
552 p = unix_versions[0];
553 }
554
555 if (m->meta.source)
556 free(m->meta.source);
557
558 m->meta.source = mandoc_strdup(p);
559 return(1);
560}