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