]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
Remove `Sp', `Vb', and `Ve' (as per schwarze@'s changes in OpenBSD),
[mandoc.git] / man_validate.c
1 /* $Id: man_validate.c,v 1.52 2010/12/05 16:14:16 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_le1(CHKARGS);
48 static int check_ge2(CHKARGS);
49 static int check_le5(CHKARGS);
50 static int check_par(CHKARGS);
51 static int check_part(CHKARGS);
52 static int check_root(CHKARGS);
53 static int check_sec(CHKARGS);
54 static int check_text(CHKARGS);
55 static int check_title(CHKARGS);
56
57 static int post_AT(CHKARGS);
58 static int post_fi(CHKARGS);
59 static int post_nf(CHKARGS);
60 static int post_TH(CHKARGS);
61 static int post_UC(CHKARGS);
62
63 static v_check posts_at[] = { post_AT, NULL };
64 static v_check posts_eq0[] = { check_eq0, NULL };
65 static v_check posts_fi[] = { check_eq0, post_fi, NULL };
66 static v_check posts_le1[] = { check_le1, NULL };
67 static v_check posts_nf[] = { check_eq0, post_nf, NULL };
68 static v_check posts_par[] = { check_par, NULL };
69 static v_check posts_part[] = { check_part, NULL };
70 static v_check posts_sec[] = { check_sec, NULL };
71 static v_check posts_th[] = { check_ge2, check_le5, check_title, post_TH, NULL };
72 static v_check posts_uc[] = { post_UC, NULL };
73 static v_check pres_bline[] = { check_bline, NULL };
74
75
76 static const struct man_valid man_valids[MAN_MAX] = {
77 { NULL, posts_eq0 }, /* br */
78 { pres_bline, posts_th }, /* TH */
79 { pres_bline, posts_sec }, /* SH */
80 { pres_bline, posts_sec }, /* SS */
81 { pres_bline, posts_par }, /* TP */
82 { pres_bline, posts_par }, /* LP */
83 { pres_bline, posts_par }, /* PP */
84 { pres_bline, posts_par }, /* P */
85 { pres_bline, posts_par }, /* IP */
86 { pres_bline, posts_par }, /* HP */
87 { NULL, NULL }, /* SM */
88 { NULL, NULL }, /* SB */
89 { NULL, NULL }, /* BI */
90 { NULL, NULL }, /* IB */
91 { NULL, NULL }, /* BR */
92 { NULL, NULL }, /* RB */
93 { NULL, NULL }, /* R */
94 { NULL, NULL }, /* B */
95 { NULL, NULL }, /* I */
96 { NULL, NULL }, /* IR */
97 { NULL, NULL }, /* RI */
98 { NULL, posts_eq0 }, /* na */ /* FIXME: should warn only. */
99 { NULL, NULL }, /* i */
100 { NULL, posts_le1 }, /* sp */ /* FIXME: should warn only. */
101 { pres_bline, posts_nf }, /* nf */
102 { pres_bline, posts_fi }, /* fi */
103 { NULL, NULL }, /* r */
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 };
112
113
114 int
115 man_valid_pre(struct man *m, struct man_node *n)
116 {
117 v_check *cp;
118
119 if (MAN_TEXT == n->type)
120 return(1);
121 if (MAN_ROOT == n->type)
122 return(1);
123
124 if (NULL == (cp = man_valids[n->tok].pres))
125 return(1);
126 for ( ; *cp; cp++)
127 if ( ! (*cp)(m, n))
128 return(0);
129 return(1);
130 }
131
132
133 int
134 man_valid_post(struct man *m)
135 {
136 v_check *cp;
137
138 if (MAN_VALID & m->last->flags)
139 return(1);
140 m->last->flags |= MAN_VALID;
141
142 switch (m->last->type) {
143 case (MAN_TEXT):
144 return(check_text(m, m->last));
145 case (MAN_ROOT):
146 return(check_root(m, m->last));
147 default:
148 break;
149 }
150
151 if (NULL == (cp = man_valids[m->last->tok].posts))
152 return(1);
153 for ( ; *cp; cp++)
154 if ( ! (*cp)(m, m->last))
155 return(0);
156
157 return(1);
158 }
159
160
161 static int
162 check_root(CHKARGS)
163 {
164
165 if (MAN_BLINE & m->flags)
166 return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
167 if (MAN_ELINE & m->flags)
168 return(man_nmsg(m, n, MANDOCERR_SCOPEEXIT));
169
170 m->flags &= ~MAN_BLINE;
171 m->flags &= ~MAN_ELINE;
172
173 if (NULL == m->first->child) {
174 man_nmsg(m, n, MANDOCERR_NODOCBODY);
175 return(0);
176 } else if (NULL == m->meta.title) {
177 if ( ! man_nmsg(m, n, MANDOCERR_NOTITLE))
178 return(0);
179 /*
180 * If a title hasn't been set, do so now (by
181 * implication, date and section also aren't set).
182 *
183 * FIXME: this should be in man_action.c.
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 if (isalpha((u_char)*p) && ! isupper((u_char)*p))
208 if ( ! man_nmsg(m, n, MANDOCERR_UPPERCASE))
209 return(0);
210
211 return(1);
212 }
213
214
215 static int
216 check_text(CHKARGS)
217 {
218 char *p;
219 int pos, c;
220 size_t sz;
221
222 for (p = n->string, pos = n->pos + 1; *p; p++, pos++) {
223 sz = strcspn(p, "\t\\");
224 p += (int)sz;
225
226 if ('\0' == *p)
227 break;
228
229 pos += (int)sz;
230
231 if ('\t' == *p) {
232 if (MAN_LITERAL & m->flags)
233 continue;
234 if (man_pmsg(m, n->line, pos, MANDOCERR_BADTAB))
235 continue;
236 return(0);
237 }
238
239 /* Check the special character. */
240
241 c = mandoc_special(p);
242 if (c) {
243 p += c - 1;
244 pos += c - 1;
245 } else
246 man_pmsg(m, n->line, pos, MANDOCERR_BADESCAPE);
247 }
248
249 return(1);
250 }
251
252
253 #define INEQ_DEFINE(x, ineq, name) \
254 static int \
255 check_##name(CHKARGS) \
256 { \
257 if (n->nchild ineq (x)) \
258 return(1); \
259 man_vmsg(m, MANDOCERR_SYNTARGCOUNT, n->line, n->pos, \
260 "line arguments %s %d (have %d)", \
261 #ineq, (x), n->nchild); \
262 return(0); \
263 }
264
265 INEQ_DEFINE(0, ==, eq0)
266 INEQ_DEFINE(1, <=, le1)
267 INEQ_DEFINE(2, >=, ge2)
268 INEQ_DEFINE(5, <=, le5)
269
270
271 static int
272 check_sec(CHKARGS)
273 {
274
275 if (MAN_HEAD == n->type && 0 == n->nchild) {
276 man_nmsg(m, n, MANDOCERR_SYNTARGCOUNT);
277 return(0);
278 } else if (MAN_BODY == n->type && 0 == n->nchild)
279 return(man_nmsg(m, n, MANDOCERR_NOBODY));
280
281 return(1);
282 }
283
284
285 static int
286 check_part(CHKARGS)
287 {
288
289 if (MAN_BODY == n->type && 0 == n->nchild)
290 return(man_nmsg(m, n, MANDOCERR_NOBODY));
291 return(1);
292 }
293
294
295 static int
296 check_par(CHKARGS)
297 {
298
299 if (MAN_BODY == n->type)
300 switch (n->tok) {
301 case (MAN_IP):
302 /* FALLTHROUGH */
303 case (MAN_HP):
304 /* FALLTHROUGH */
305 case (MAN_TP):
306 /* Body-less lists are ok. */
307 break;
308 default:
309 if (n->nchild)
310 break;
311 return(man_nmsg(m, n, MANDOCERR_NOBODY));
312 }
313 if (MAN_HEAD == n->type)
314 switch (n->tok) {
315 case (MAN_PP):
316 /* FALLTHROUGH */
317 case (MAN_P):
318 /* FALLTHROUGH */
319 case (MAN_LP):
320 if (0 == n->nchild)
321 break;
322 return(man_nmsg(m, n, MANDOCERR_ARGSLOST));
323 default:
324 if (n->nchild)
325 break;
326 return(man_nmsg(m, n, MANDOCERR_NOARGS));
327 }
328
329 return(1);
330 }
331
332
333 static int
334 check_bline(CHKARGS)
335 {
336
337 assert( ! (MAN_ELINE & m->flags));
338 if (MAN_BLINE & m->flags) {
339 man_nmsg(m, n, MANDOCERR_SYNTLINESCOPE);
340 return(0);
341 }
342
343 return(1);
344 }
345
346 static int
347 post_TH(CHKARGS)
348 {
349
350 if (m->meta.title)
351 free(m->meta.title);
352 if (m->meta.vol)
353 free(m->meta.vol);
354 if (m->meta.source)
355 free(m->meta.source);
356 if (m->meta.msec)
357 free(m->meta.msec);
358 if (m->meta.rawdate)
359 free(m->meta.rawdate);
360
361 m->meta.title = m->meta.vol = m->meta.rawdate =
362 m->meta.msec = m->meta.source = NULL;
363 m->meta.date = 0;
364
365 /* ->TITLE<- MSEC DATE SOURCE VOL */
366
367 n = n->child;
368 assert(n);
369 m->meta.title = mandoc_strdup(n->string);
370
371 /* TITLE ->MSEC<- DATE SOURCE VOL */
372
373 n = n->next;
374 assert(n);
375 m->meta.msec = mandoc_strdup(n->string);
376
377 /* TITLE MSEC ->DATE<- SOURCE VOL */
378
379 /*
380 * Try to parse the date. If this works, stash the epoch (this
381 * is optimal because we can reformat it in the canonical form).
382 * If it doesn't parse, isn't specified at all, or is an empty
383 * string, then use the current date.
384 */
385
386 n = n->next;
387 if (n && n->string && *n->string) {
388 m->meta.date = mandoc_a2time
389 (MTIME_ISO_8601, n->string);
390 if (0 == m->meta.date) {
391 man_nmsg(m, n, MANDOCERR_BADDATE);
392 m->meta.rawdate = mandoc_strdup(n->string);
393 }
394 } else
395 m->meta.date = time(NULL);
396
397 /* TITLE MSEC DATE ->SOURCE<- VOL */
398
399 if (n && (n = n->next))
400 m->meta.source = mandoc_strdup(n->string);
401
402 /* TITLE MSEC DATE SOURCE ->VOL<- */
403
404 if (n && (n = n->next))
405 m->meta.vol = mandoc_strdup(n->string);
406
407 /*
408 * Remove the `TH' node after we've processed it for our
409 * meta-data.
410 */
411 man_node_delete(m, m->last);
412 return(1);
413 }
414
415 static int
416 post_nf(CHKARGS)
417 {
418
419 if (MAN_LITERAL & m->flags)
420 man_nmsg(m, n, MANDOCERR_SCOPEREP);
421
422 m->flags |= MAN_LITERAL;
423 return(1);
424 }
425
426 static int
427 post_fi(CHKARGS)
428 {
429
430 if ( ! (MAN_LITERAL & m->flags))
431 man_nmsg(m, n, MANDOCERR_NOSCOPE);
432
433 m->flags &= ~MAN_LITERAL;
434 return(1);
435 }
436
437 static int
438 post_UC(CHKARGS)
439 {
440 static const char * const bsd_versions[] = {
441 "3rd Berkeley Distribution",
442 "4th Berkeley Distribution",
443 "4.2 Berkeley Distribution",
444 "4.3 Berkeley Distribution",
445 "4.4 Berkeley Distribution",
446 };
447
448 const char *p, *s;
449
450 n = n->child;
451 n = m->last->child;
452
453 if (NULL == n || MAN_TEXT != n->type)
454 p = bsd_versions[0];
455 else {
456 s = n->string;
457 if (0 == strcmp(s, "3"))
458 p = bsd_versions[0];
459 else if (0 == strcmp(s, "4"))
460 p = bsd_versions[1];
461 else if (0 == strcmp(s, "5"))
462 p = bsd_versions[2];
463 else if (0 == strcmp(s, "6"))
464 p = bsd_versions[3];
465 else if (0 == strcmp(s, "7"))
466 p = bsd_versions[4];
467 else
468 p = bsd_versions[0];
469 }
470
471 if (m->meta.source)
472 free(m->meta.source);
473
474 m->meta.source = mandoc_strdup(p);
475 return(1);
476 }
477
478 static int
479 post_AT(CHKARGS)
480 {
481 static const char * const unix_versions[] = {
482 "7th Edition",
483 "System III",
484 "System V",
485 "System V Release 2",
486 };
487
488 const char *p, *s;
489 struct man_node *nn;
490
491 n = n->child;
492
493 if (NULL == n || MAN_TEXT != n->type)
494 p = unix_versions[0];
495 else {
496 s = n->string;
497 if (0 == strcmp(s, "3"))
498 p = unix_versions[0];
499 else if (0 == strcmp(s, "4"))
500 p = unix_versions[1];
501 else if (0 == strcmp(s, "5")) {
502 nn = n->next;
503 if (nn && MAN_TEXT == nn->type && nn->string[0])
504 p = unix_versions[3];
505 else
506 p = unix_versions[2];
507 } else
508 p = unix_versions[0];
509 }
510
511 if (m->meta.source)
512 free(m->meta.source);
513
514 m->meta.source = mandoc_strdup(p);
515 return(1);
516 }