]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Plan9 has a man(7) implementation that looks extremely archaic,
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.56 2010/12/08 10:58:22 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "mandoc.h"
33 #include "libman.h"
34 #include "libmandoc.h"
35
36 #define CHKARGS struct man *m, struct man_node *n
37
38 typedef int (*v_check)(CHKARGS);
39
40 struct man_valid {
41 v_check *pres;
42 v_check *posts;
43 };
44
45 static int check_bline(CHKARGS);
46 static int check_eq0(CHKARGS);
47 static int check_ft(CHKARGS);
48 static int check_le1(CHKARGS);
49 static int check_ge2(CHKARGS);
50 static int check_le5(CHKARGS);
51 static int check_par(CHKARGS);
52 static int check_part(CHKARGS);
53 static int check_root(CHKARGS);
54 static int check_sec(CHKARGS);
55 static int check_text(CHKARGS);
56 static int check_title(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, check_title, 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, posts_par }, /* TP */
84 { pres_bline, posts_par }, /* LP */
85 { pres_bline, posts_par }, /* PP */
86 { pres_bline, posts_par }, /* P */
87 { pres_bline, posts_par }, /* IP */
88 { pres_bline, posts_par }, /* 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 if (MAN_TEXT == n->type)
121 return(1);
122 if (MAN_ROOT == n->type)
123 return(1);
124
125 if (NULL == (cp = man_valids[n->tok].pres))
126 return(1);
127 for ( ; *cp; cp++)
128 if ( ! (*cp)(m, n))
129 return(0);
130 return(1);
131 }
132
133
134 int
135 man_valid_post(struct man *m)
136 {
137 v_check *cp;
138
139 if (MAN_VALID & m->last->flags)
140 return(1);
141 m->last->flags |= MAN_VALID;
142
143 switch (m->last->type) {
144 case (MAN_TEXT):
145 return(check_text(m, m->last));
146 case (MAN_ROOT):
147 return(check_root(m, m->last));
148 default:
149 break;
150 }
151
152 if (NULL == (cp = man_valids[m->last->tok].posts))
153 return(1);
154 for ( ; *cp; cp++)
155 if ( ! (*cp)(m, m->last))
156 return(0);
157
158 return(1);
159 }
160
161
162 static int
163 check_root(CHKARGS)
164 {
165
166 if (MAN_BLINE & m->flags)
167 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
168 else if (MAN_ELINE & m->flags)
169 man_nmsg(m, n, MANDOCERR_SCOPEEXIT);
170
171 m->flags &= ~MAN_BLINE;
172 m->flags &= ~MAN_ELINE;
173
174 if (NULL == m->first->child) {
175 man_nmsg(m, n, MANDOCERR_NODOCBODY);
176 return(0);
177 } else if (NULL == m->meta.title) {
178 man_nmsg(m, n, MANDOCERR_NOTITLE);
179
180 /*
181 * If a title hasn't been set, do so now (by
182 * implication, date and section also aren't set).
183 */
184
185 m->meta.title = mandoc_strdup("unknown");
186 m->meta.date = time(NULL);
187 m->meta.msec = mandoc_strdup("1");
188 }
189
190 return(1);
191 }
192
193
194 static int
195 check_title(CHKARGS)
196 {
197 const char *p;
198
199 assert(n->child);
200 /* FIXME: is this sufficient? */
201 if ('\0' == *n->child->string) {
202 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
203 return(0);
204 }
205
206 for (p = n->child->string; '\0' != *p; p++)
207 /* Only warn about this once... */
208 if (isalpha((u_char)*p) && ! isupper((u_char)*p)) {
209 man_nmsg(m, n, MANDOCERR_UPPERCASE);
210 break;
211 }
212
213 return(1);
214 }
215
216
217 static int
218 check_text(CHKARGS)
219 {
220 char *p;
221 int pos, c;
222 size_t sz;
223
224 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
225 sz = strcspn(p, "\t\\");
226 p += (int)sz;
227
228 if ('\0' == *p)
229 break;
230
231 pos += (int)sz;
232
233 if ('\t' == *p) {
234 if (MAN_LITERAL & m->flags)
235 continue;
236 if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
237 continue;
238 return(0);
239 }
240
241 /* Check the special character. */
242
243 c = mandoc_special(p);
244 if (c) {
245 p += c - 1;
246 pos += c - 1;
247 } else
248 man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
249 }
250
251 return(1);
252 }
253
254
255 #define INEQ_DEFINE(x, ineq, name) \
256 static int \
257 check_##name(CHKARGS) \
258 { \
259 if (n->nchild ineq (x)) \
260 return(1); \
261 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
262 "line arguments %s %d (have %d)", \
263 #ineq, (x), n->nchild); \
264 return(0); \
265 }
266
267 INEQ_DEFINE(0, ==, eq0)
268 INEQ_DEFINE(1, <=, le1)
269 INEQ_DEFINE(2, >=, ge2)
270 INEQ_DEFINE(5, <=, le5)
271
272 static int
273 check_ft(CHKARGS)
274 {
275 char *cp;
276 int ok;
277
278 if (0 == n->nchild)
279 return(1);
280
281 ok = 0;
282 cp = n->child->string;
283 switch (*cp) {
284 case ('1'):
285 /* FALLTHROUGH */
286 case ('2'):
287 /* FALLTHROUGH */
288 case ('3'):
289 /* FALLTHROUGH */
290 case ('4'):
291 /* FALLTHROUGH */
292 case ('I'):
293 /* FALLTHROUGH */
294 case ('P'):
295 /* FALLTHROUGH */
296 case ('R'):
297 if ('\0' == cp[1])
298 ok = 1;
299 break;
300 case ('B'):
301 if ('\0' == cp[1] || ('I' == cp[1] && '\0' == cp[2]))
302 ok = 1;
303 break;
304 case ('C'):
305 if ('W' == cp[1] && '\0' == cp[2])
306 ok = 1;
307 break;
308 default:
309 break;
310 }
311
312 if (0 == ok) {
313 man_vmsg(m, MANDOCERR_BADFONT,
314 n->line, n->pos, "%s", cp);
315 *cp = '\0';
316 }
317
318 if (1 < n->nchild)
319 man_vmsg(m, MANDOCERR_ARGCOUNT, n->line, n->pos,
320 "want one child (have %d)", n->nchild);
321
322 return(1);
323 }
324
325 static int
326 check_sec(CHKARGS)
327 {
328
329 if (MAN_HEAD == n->type && 0 == n->nchild) {
330 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
331 return(0);
332 } else 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_part(CHKARGS)
341 {
342
343 if (MAN_BODY == n->type && 0 == n->nchild)
344 man_nmsg(m, n, MANDOCERR_NOBODY);
345
346 return(1);
347 }
348
349
350 static int
351 check_par(CHKARGS)
352 {
353
354 if (MAN_BODY == n->type)
355 switch (n->tok) {
356 case (MAN_IP):
357 /* FALLTHROUGH */
358 case (MAN_HP):
359 /* FALLTHROUGH */
360 case (MAN_TP):
361 /* Body-less lists are ok. */
362 break;
363 default:
364 if (0 == n->nchild)
365 man_nmsg(m, n, MANDOCERR_NOBODY);
366 break;
367 }
368 if (MAN_HEAD == n->type)
369 switch (n->tok) {
370 case (MAN_PP):
371 /* FALLTHROUGH */
372 case (MAN_P):
373 /* FALLTHROUGH */
374 case (MAN_LP):
375 if (n->nchild)
376 man_nmsg(m, n, MANDOCERR_ARGSLOST);
377 break;
378 default:
379 break;
380 }
381
382 return(1);
383 }
384
385
386 static int
387 check_bline(CHKARGS)
388 {
389
390 assert( ! (MAN_ELINE & m->flags));
391 if (MAN_BLINE & m->flags) {
392 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
393 return(0);
394 }
395
396 return(1);
397 }
398
399 static int
400 post_TH(CHKARGS)
401 {
402
403 if (m->meta.title)
404 free(m->meta.title);
405 if (m->meta.vol)
406 free(m->meta.vol);
407 if (m->meta.source)
408 free(m->meta.source);
409 if (m->meta.msec)
410 free(m->meta.msec);
411 if (m->meta.rawdate)
412 free(m->meta.rawdate);
413
414 m->meta.title = m->meta.vol = m->meta.rawdate =
415 m->meta.msec = m->meta.source = NULL;
416 m->meta.date = 0;
417
418 /* ->TITLE<- MSEC DATE SOURCE VOL */
419
420 n = n->child;
421 assert(n);
422 m->meta.title = mandoc_strdup(n->string);
423
424 /* TITLE ->MSEC<- DATE SOURCE VOL */
425
426 n = n->next;
427 assert(n);
428 m->meta.msec = mandoc_strdup(n->string);
429
430 /* TITLE MSEC ->DATE<- SOURCE VOL */
431
432 /*
433 * Try to parse the date. If this works, stash the epoch (this
434 * is optimal because we can reformat it in the canonical form).
435 * If it doesn't parse, isn't specified at all, or is an empty
436 * string, then use the current date.
437 */
438
439 n = n->next;
440 if (n && n->string && *n->string) {
441 m->meta.date = mandoc_a2time
442 (MTIME_ISO_8601, n->string);
443 if (0 == m->meta.date) {
444 man_nmsg(m, n, MANDOCERR_BADDATE);
445 m->meta.rawdate = mandoc_strdup(n->string);
446 }
447 } else
448 m->meta.date = time(NULL);
449
450 /* TITLE MSEC DATE ->SOURCE<- VOL */
451
452 if (n && (n = n->next))
453 m->meta.source = mandoc_strdup(n->string);
454
455 /* TITLE MSEC DATE SOURCE ->VOL<- */
456
457 if (n && (n = n->next))
458 m->meta.vol = mandoc_strdup(n->string);
459
460 /*
461 * Remove the `TH' node after we've processed it for our
462 * meta-data.
463 */
464 man_node_delete(m, m->last);
465 return(1);
466 }
467
468 static int
469 post_nf(CHKARGS)
470 {
471
472 if (MAN_LITERAL & m->flags)
473 man_nmsg(m, n, MANDOCERR_SCOPEREP);
474
475 m->flags |= MAN_LITERAL;
476 return(1);
477 }
478
479 static int
480 post_fi(CHKARGS)
481 {
482
483 if ( ! (MAN_LITERAL & m->flags))
484 man_nmsg(m, n, MANDOCERR_NOSCOPE);
485
486 m->flags &= ~MAN_LITERAL;
487 return(1);
488 }
489
490 static int
491 post_UC(CHKARGS)
492 {
493 static const char * const bsd_versions[] = {
494 "3rd Berkeley Distribution",
495 "4th Berkeley Distribution",
496 "4.2 Berkeley Distribution",
497 "4.3 Berkeley Distribution",
498 "4.4 Berkeley Distribution",
499 };
500
501 const char *p, *s;
502
503 n = n->child;
504 n = m->last->child;
505
506 if (NULL == n || MAN_TEXT != n->type)
507 p = bsd_versions[0];
508 else {
509 s = n->string;
510 if (0 == strcmp(s, "3"))
511 p = bsd_versions[0];
512 else if (0 == strcmp(s, "4"))
513 p = bsd_versions[1];
514 else if (0 == strcmp(s, "5"))
515 p = bsd_versions[2];
516 else if (0 == strcmp(s, "6"))
517 p = bsd_versions[3];
518 else if (0 == strcmp(s, "7"))
519 p = bsd_versions[4];
520 else
521 p = bsd_versions[0];
522 }
523
524 if (m->meta.source)
525 free(m->meta.source);
526
527 m->meta.source = mandoc_strdup(p);
528 return(1);
529 }
530
531 static int
532 post_AT(CHKARGS)
533 {
534 static const char * const unix_versions[] = {
535 "7th Edition",
536 "System III",
537 "System V",
538 "System V Release 2",
539 };
540
541 const char *p, *s;
542 struct man_node *nn;
543
544 n = n->child;
545
546 if (NULL == n || MAN_TEXT != n->type)
547 p = unix_versions[0];
548 else {
549 s = n->string;
550 if (0 == strcmp(s, "3"))
551 p = unix_versions[0];
552 else if (0 == strcmp(s, "4"))
553 p = unix_versions[1];
554 else if (0 == strcmp(s, "5")) {
555 nn = n->next;
556 if (nn && MAN_TEXT == nn->type && nn->string[0])
557 p = unix_versions[3];
558 else
559 p = unix_versions[2];
560 } else
561 p = unix_versions[0];
562 }
563
564 if (m->meta.source)
565 free(m->meta.source);
566
567 m->meta.source = mandoc_strdup(p);
568 return(1);
569 }