]> git.cameronkatri.com Git - mandoc.git/blob - man_validate.c
quoted words are not parsed for defined keys
[mandoc.git] / man_validate.c
1 /* $OpenBSD$ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2017 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "mandoc_aux.h"
32 #include "mandoc.h"
33 #include "roff.h"
34 #include "man.h"
35 #include "libmandoc.h"
36 #include "roff_int.h"
37 #include "libman.h"
38
39 #define CHKARGS struct roff_man *man, struct roff_node *n
40
41 typedef void (*v_check)(CHKARGS);
42
43 static void check_par(CHKARGS);
44 static void check_part(CHKARGS);
45 static void check_root(CHKARGS);
46 static void check_text(CHKARGS);
47
48 static void post_AT(CHKARGS);
49 static void post_IP(CHKARGS);
50 static void post_OP(CHKARGS);
51 static void post_TH(CHKARGS);
52 static void post_UC(CHKARGS);
53 static void post_UR(CHKARGS);
54 static void post_in(CHKARGS);
55 static void post_vs(CHKARGS);
56
57 static const v_check __man_valids[MAN_MAX - MAN_TH] = {
58 post_TH, /* TH */
59 NULL, /* SH */
60 NULL, /* SS */
61 NULL, /* TP */
62 check_par, /* LP */
63 check_par, /* PP */
64 check_par, /* P */
65 post_IP, /* IP */
66 NULL, /* HP */
67 NULL, /* SM */
68 NULL, /* SB */
69 NULL, /* BI */
70 NULL, /* IB */
71 NULL, /* BR */
72 NULL, /* RB */
73 NULL, /* R */
74 NULL, /* B */
75 NULL, /* I */
76 NULL, /* IR */
77 NULL, /* RI */
78 NULL, /* nf */
79 NULL, /* fi */
80 NULL, /* RE */
81 check_part, /* RS */
82 NULL, /* DT */
83 post_UC, /* UC */
84 NULL, /* PD */
85 post_AT, /* AT */
86 post_in, /* in */
87 post_OP, /* OP */
88 NULL, /* EX */
89 NULL, /* EE */
90 post_UR, /* UR */
91 NULL, /* UE */
92 };
93 static const v_check *man_valids = __man_valids - MAN_TH;
94
95
96 void
97 man_node_validate(struct roff_man *man)
98 {
99 struct roff_node *n;
100 const v_check *cp;
101
102 n = man->last;
103 man->last = man->last->child;
104 while (man->last != NULL) {
105 man_node_validate(man);
106 if (man->last == n)
107 man->last = man->last->child;
108 else
109 man->last = man->last->next;
110 }
111
112 man->last = n;
113 man->next = ROFF_NEXT_SIBLING;
114 switch (n->type) {
115 case ROFFT_TEXT:
116 check_text(man, n);
117 break;
118 case ROFFT_ROOT:
119 check_root(man, n);
120 break;
121 case ROFFT_EQN:
122 case ROFFT_TBL:
123 break;
124 default:
125 if (n->tok < ROFF_MAX) {
126 switch (n->tok) {
127 case ROFF_br:
128 case ROFF_sp:
129 post_vs(man, n);
130 break;
131 default:
132 roff_validate(man);
133 break;
134 }
135 break;
136 }
137 assert(n->tok >= MAN_TH && n->tok < MAN_MAX);
138 cp = man_valids + n->tok;
139 if (*cp)
140 (*cp)(man, n);
141 if (man->last == n)
142 man_state(man, n);
143 break;
144 }
145 }
146
147 static void
148 check_root(CHKARGS)
149 {
150
151 assert((man->flags & (MAN_BLINE | MAN_ELINE)) == 0);
152
153 if (NULL == man->first->child)
154 mandoc_msg(MANDOCERR_DOC_EMPTY, man->parse,
155 n->line, n->pos, NULL);
156 else
157 man->meta.hasbody = 1;
158
159 if (NULL == man->meta.title) {
160 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
161 n->line, n->pos, NULL);
162
163 /*
164 * If a title hasn't been set, do so now (by
165 * implication, date and section also aren't set).
166 */
167
168 man->meta.title = mandoc_strdup("");
169 man->meta.msec = mandoc_strdup("");
170 man->meta.date = man->quick ? mandoc_strdup("") :
171 mandoc_normdate(man, NULL, n->line, n->pos);
172 }
173
174 if (man->meta.os_e &&
175 (man->meta.rcsids & (1 << man->meta.os_e)) == 0)
176 mandoc_msg(MANDOCERR_RCS_MISSING, man->parse, 0, 0, NULL);
177 }
178
179 static void
180 check_text(CHKARGS)
181 {
182 char *cp, *p;
183
184 if (MAN_LITERAL & man->flags)
185 return;
186
187 cp = n->string;
188 for (p = cp; NULL != (p = strchr(p, '\t')); p++)
189 mandoc_msg(MANDOCERR_FI_TAB, man->parse,
190 n->line, n->pos + (p - cp), NULL);
191 }
192
193 static void
194 post_OP(CHKARGS)
195 {
196
197 if (n->child == NULL)
198 mandoc_msg(MANDOCERR_OP_EMPTY, man->parse,
199 n->line, n->pos, "OP");
200 else if (n->child->next != NULL && n->child->next->next != NULL) {
201 n = n->child->next->next;
202 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
203 n->line, n->pos, "OP ... %s", n->string);
204 }
205 }
206
207 static void
208 post_UR(CHKARGS)
209 {
210
211 if (n->type == ROFFT_HEAD && n->child == NULL)
212 mandoc_vmsg(MANDOCERR_UR_NOHEAD, man->parse,
213 n->line, n->pos, "UR");
214 check_part(man, n);
215 }
216
217 static void
218 check_part(CHKARGS)
219 {
220
221 if (n->type == ROFFT_BODY && n->child == NULL)
222 mandoc_msg(MANDOCERR_BLK_EMPTY, man->parse,
223 n->line, n->pos, roff_name[n->tok]);
224 }
225
226 static void
227 check_par(CHKARGS)
228 {
229
230 switch (n->type) {
231 case ROFFT_BLOCK:
232 if (n->body->child == NULL)
233 roff_node_delete(man, n);
234 break;
235 case ROFFT_BODY:
236 if (n->child == NULL)
237 mandoc_vmsg(MANDOCERR_PAR_SKIP,
238 man->parse, n->line, n->pos,
239 "%s empty", roff_name[n->tok]);
240 break;
241 case ROFFT_HEAD:
242 if (n->child != NULL)
243 mandoc_vmsg(MANDOCERR_ARG_SKIP,
244 man->parse, n->line, n->pos, "%s %s%s",
245 roff_name[n->tok], n->child->string,
246 n->child->next != NULL ? " ..." : "");
247 break;
248 default:
249 break;
250 }
251 }
252
253 static void
254 post_IP(CHKARGS)
255 {
256
257 switch (n->type) {
258 case ROFFT_BLOCK:
259 if (n->head->child == NULL && n->body->child == NULL)
260 roff_node_delete(man, n);
261 break;
262 case ROFFT_BODY:
263 if (n->parent->head->child == NULL && n->child == NULL)
264 mandoc_vmsg(MANDOCERR_PAR_SKIP,
265 man->parse, n->line, n->pos,
266 "%s empty", roff_name[n->tok]);
267 break;
268 default:
269 break;
270 }
271 }
272
273 static void
274 post_TH(CHKARGS)
275 {
276 struct roff_node *nb;
277 const char *p;
278
279 free(man->meta.title);
280 free(man->meta.vol);
281 free(man->meta.os);
282 free(man->meta.msec);
283 free(man->meta.date);
284
285 man->meta.title = man->meta.vol = man->meta.date =
286 man->meta.msec = man->meta.os = NULL;
287
288 nb = n;
289
290 /* ->TITLE<- MSEC DATE OS VOL */
291
292 n = n->child;
293 if (n && n->string) {
294 for (p = n->string; '\0' != *p; p++) {
295 /* Only warn about this once... */
296 if (isalpha((unsigned char)*p) &&
297 ! isupper((unsigned char)*p)) {
298 mandoc_vmsg(MANDOCERR_TITLE_CASE,
299 man->parse, n->line,
300 n->pos + (p - n->string),
301 "TH %s", n->string);
302 break;
303 }
304 }
305 man->meta.title = mandoc_strdup(n->string);
306 } else {
307 man->meta.title = mandoc_strdup("");
308 mandoc_msg(MANDOCERR_TH_NOTITLE, man->parse,
309 nb->line, nb->pos, "TH");
310 }
311
312 /* TITLE ->MSEC<- DATE OS VOL */
313
314 if (n)
315 n = n->next;
316 if (n && n->string)
317 man->meta.msec = mandoc_strdup(n->string);
318 else {
319 man->meta.msec = mandoc_strdup("");
320 mandoc_vmsg(MANDOCERR_MSEC_MISSING, man->parse,
321 nb->line, nb->pos, "TH %s", man->meta.title);
322 }
323
324 /* TITLE MSEC ->DATE<- OS VOL */
325
326 if (n)
327 n = n->next;
328 if (n && n->string && '\0' != n->string[0]) {
329 man->meta.date = man->quick ?
330 mandoc_strdup(n->string) :
331 mandoc_normdate(man, n->string, n->line, n->pos);
332 } else {
333 man->meta.date = mandoc_strdup("");
334 mandoc_msg(MANDOCERR_DATE_MISSING, man->parse,
335 n ? n->line : nb->line,
336 n ? n->pos : nb->pos, "TH");
337 }
338
339 /* TITLE MSEC DATE ->OS<- VOL */
340
341 if (n && (n = n->next))
342 man->meta.os = mandoc_strdup(n->string);
343 else if (man->defos != NULL)
344 man->meta.os = mandoc_strdup(man->defos);
345 man->meta.os_e = man->meta.os == NULL ? MDOC_OS_OTHER :
346 strstr(man->meta.os, "OpenBSD") != NULL ? MDOC_OS_OPENBSD :
347 strstr(man->meta.os, "NetBSD") != NULL ? MDOC_OS_NETBSD :
348 MDOC_OS_OTHER;
349
350 /* TITLE MSEC DATE OS ->VOL<- */
351 /* If missing, use the default VOL name for MSEC. */
352
353 if (n && (n = n->next))
354 man->meta.vol = mandoc_strdup(n->string);
355 else if ('\0' != man->meta.msec[0] &&
356 (NULL != (p = mandoc_a2msec(man->meta.msec))))
357 man->meta.vol = mandoc_strdup(p);
358
359 if (n != NULL && (n = n->next) != NULL)
360 mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
361 n->line, n->pos, "TH ... %s", n->string);
362
363 /*
364 * Remove the `TH' node after we've processed it for our
365 * meta-data.
366 */
367 roff_node_delete(man, man->last);
368 }
369
370 static void
371 post_UC(CHKARGS)
372 {
373 static const char * const bsd_versions[] = {
374 "3rd Berkeley Distribution",
375 "4th Berkeley Distribution",
376 "4.2 Berkeley Distribution",
377 "4.3 Berkeley Distribution",
378 "4.4 Berkeley Distribution",
379 };
380
381 const char *p, *s;
382
383 n = n->child;
384
385 if (n == NULL || n->type != ROFFT_TEXT)
386 p = bsd_versions[0];
387 else {
388 s = n->string;
389 if (0 == strcmp(s, "3"))
390 p = bsd_versions[0];
391 else if (0 == strcmp(s, "4"))
392 p = bsd_versions[1];
393 else if (0 == strcmp(s, "5"))
394 p = bsd_versions[2];
395 else if (0 == strcmp(s, "6"))
396 p = bsd_versions[3];
397 else if (0 == strcmp(s, "7"))
398 p = bsd_versions[4];
399 else
400 p = bsd_versions[0];
401 }
402
403 free(man->meta.os);
404 man->meta.os = mandoc_strdup(p);
405 }
406
407 static void
408 post_AT(CHKARGS)
409 {
410 static const char * const unix_versions[] = {
411 "7th Edition",
412 "System III",
413 "System V",
414 "System V Release 2",
415 };
416
417 struct roff_node *nn;
418 const char *p, *s;
419
420 n = n->child;
421
422 if (n == NULL || n->type != ROFFT_TEXT)
423 p = unix_versions[0];
424 else {
425 s = n->string;
426 if (0 == strcmp(s, "3"))
427 p = unix_versions[0];
428 else if (0 == strcmp(s, "4"))
429 p = unix_versions[1];
430 else if (0 == strcmp(s, "5")) {
431 nn = n->next;
432 if (nn != NULL &&
433 nn->type == ROFFT_TEXT &&
434 nn->string[0] != '\0')
435 p = unix_versions[3];
436 else
437 p = unix_versions[2];
438 } else
439 p = unix_versions[0];
440 }
441
442 free(man->meta.os);
443 man->meta.os = mandoc_strdup(p);
444 }
445
446 static void
447 post_in(CHKARGS)
448 {
449 char *s;
450
451 if (n->parent->tok != MAN_TP ||
452 n->parent->type != ROFFT_HEAD ||
453 n->child == NULL ||
454 *n->child->string == '+' ||
455 *n->child->string == '-')
456 return;
457 mandoc_asprintf(&s, "+%s", n->child->string);
458 free(n->child->string);
459 n->child->string = s;
460 }
461
462 static void
463 post_vs(CHKARGS)
464 {
465
466 if (NULL != n->prev)
467 return;
468
469 switch (n->parent->tok) {
470 case MAN_SH:
471 case MAN_SS:
472 case MAN_PP:
473 case MAN_LP:
474 case MAN_P:
475 mandoc_vmsg(MANDOCERR_PAR_SKIP, man->parse, n->line, n->pos,
476 "%s after %s", roff_name[n->tok],
477 roff_name[n->parent->tok]);
478 /* FALLTHROUGH */
479 case TOKEN_NONE:
480 /*
481 * Don't warn about this because it occurs in pod2man
482 * and would cause considerable (unfixable) warnage.
483 */
484 roff_node_delete(man, n);
485 break;
486 default:
487 break;
488 }
489 }