xref: /openbsd-src/usr.bin/mandoc/mandoc_msg.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /* $OpenBSD: mandoc_msg.c,v 1.9 2020/04/24 11:58:02 schwarze Exp $ */
2 /*
3  * Copyright (c) 2014-2020 Ingo Schwarze <schwarze@openbsd.org>
4  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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  * Implementation of warning and error messages for mandoc(1).
19  */
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "mandoc.h"
25 
26 static	const enum mandocerr lowest_type[MANDOCLEVEL_MAX] = {
27 	MANDOCERR_OK,
28 	MANDOCERR_OK,
29 	MANDOCERR_WARNING,
30 	MANDOCERR_ERROR,
31 	MANDOCERR_UNSUPP,
32 	MANDOCERR_BADARG,
33 	MANDOCERR_SYSERR
34 };
35 
36 static	const char *const level_name[MANDOCLEVEL_MAX] = {
37 	"SUCCESS",
38 	"STYLE",
39 	"WARNING",
40 	"ERROR",
41 	"UNSUPP",
42 	"BADARG",
43 	"SYSERR"
44 };
45 
46 static	const char *const type_message[MANDOCERR_MAX] = {
47 	"ok",
48 
49 	"base system convention",
50 
51 	"Mdocdate found",
52 	"Mdocdate missing",
53 	"unknown architecture",
54 	"operating system explicitly specified",
55 	"RCS id missing",
56 	"referenced manual not found",
57 
58 	"generic style suggestion",
59 
60 	"legacy man(7) date format",
61 	"normalizing date format to",
62 	"lower case character in document title",
63 	"duplicate RCS id",
64 	"possible typo in section name",
65 	"unterminated quoted argument",
66 	"useless macro",
67 	"consider using OS macro",
68 	"errnos out of order",
69 	"duplicate errno",
70 	"trailing delimiter",
71 	"no blank before trailing delimiter",
72 	"fill mode already enabled, skipping",
73 	"fill mode already disabled, skipping",
74 	"verbatim \"--\", maybe consider using \\(em",
75 	"function name without markup",
76 	"whitespace at end of input line",
77 	"bad comment style",
78 
79 	"generic warning",
80 
81 	/* related to the prologue */
82 	"missing manual title, using UNTITLED",
83 	"missing manual title, using \"\"",
84 	"missing manual section, using \"\"",
85 	"unknown manual section",
86 	"filename/section mismatch",
87 	"missing date, using \"\"",
88 	"cannot parse date, using it verbatim",
89 	"date in the future, using it anyway",
90 	"missing Os macro, using \"\"",
91 	"late prologue macro",
92 	"prologue macros out of order",
93 
94 	/* related to document structure */
95 	".so is fragile, better use ln(1)",
96 	"no document body",
97 	"content before first section header",
98 	"first section is not \"NAME\"",
99 	"NAME section without Nm before Nd",
100 	"NAME section without description",
101 	"description not at the end of NAME",
102 	"bad NAME section content",
103 	"missing comma before name",
104 	"missing description line, using \"\"",
105 	"description line outside NAME section",
106 	"sections out of conventional order",
107 	"duplicate section title",
108 	"unexpected section",
109 	"cross reference to self",
110 	"unusual Xr order",
111 	"unusual Xr punctuation",
112 	"AUTHORS section without An macro",
113 
114 	/* related to macros and nesting */
115 	"obsolete macro",
116 	"macro neither callable nor escaped",
117 	"skipping paragraph macro",
118 	"moving paragraph macro out of list",
119 	"skipping no-space macro",
120 	"blocks badly nested",
121 	"nested displays are not portable",
122 	"moving content out of list",
123 	"first macro on line",
124 	"line scope broken",
125 	"skipping blank line in line scope",
126 
127 	/* related to missing macro arguments */
128 	"skipping empty request",
129 	"conditional request controls empty scope",
130 	"skipping empty macro",
131 	"empty block",
132 	"empty argument, using 0n",
133 	"missing display type, using -ragged",
134 	"list type is not the first argument",
135 	"missing -width in -tag list, using 6n",
136 	"missing utility name, using \"\"",
137 	"missing function name, using \"\"",
138 	"empty head in list item",
139 	"empty list item",
140 	"missing argument, using next line",
141 	"missing font type, using \\fR",
142 	"unknown font type, using \\fR",
143 	"nothing follows prefix",
144 	"empty reference block",
145 	"missing section argument",
146 	"missing -std argument, adding it",
147 	"missing option string, using \"\"",
148 	"missing resource identifier, using \"\"",
149 	"missing eqn box, using \"\"",
150 
151 	/* related to bad macro arguments */
152 	"duplicate argument",
153 	"skipping duplicate argument",
154 	"skipping duplicate display type",
155 	"skipping duplicate list type",
156 	"skipping -width argument",
157 	"wrong number of cells",
158 	"unknown AT&T UNIX version",
159 	"comma in function argument",
160 	"parenthesis in function name",
161 	"unknown library name",
162 	"invalid content in Rs block",
163 	"invalid Boolean argument",
164 	"argument contains two font escapes",
165 	"unknown font, skipping request",
166 	"odd number of characters in request",
167 
168 	/* related to plain text */
169 	"blank line in fill mode, using .sp",
170 	"tab in filled text",
171 	"new sentence, new line",
172 	"invalid escape sequence",
173 	"undefined escape, printing literally",
174 	"undefined string, using \"\"",
175 
176 	/* related to tables */
177 	"tbl line starts with span",
178 	"tbl column starts with span",
179 	"skipping vertical bar in tbl layout",
180 
181 	"generic error",
182 
183 	/* related to tables */
184 	"non-alphabetic character in tbl options",
185 	"skipping unknown tbl option",
186 	"missing tbl option argument",
187 	"wrong tbl option argument size",
188 	"empty tbl layout",
189 	"invalid character in tbl layout",
190 	"unmatched parenthesis in tbl layout",
191 	"tbl without any data cells",
192 	"ignoring data in spanned tbl cell",
193 	"ignoring extra tbl data cells",
194 	"data block open at end of tbl",
195 
196 	/* related to document structure and macros */
197 	"duplicate prologue macro",
198 	"skipping late title macro",
199 	"input stack limit exceeded, infinite loop?",
200 	"skipping bad character",
201 	"skipping unknown macro",
202 	"ignoring request outside macro",
203 	"skipping insecure request",
204 	"skipping item outside list",
205 	"skipping column outside column list",
206 	"skipping end of block that is not open",
207 	"fewer RS blocks open, skipping",
208 	"inserting missing end of block",
209 	"appending missing end of block",
210 
211 	/* related to request and macro arguments */
212 	"escaped character not allowed in a name",
213 	"using macro argument outside macro",
214 	"argument number is not numeric",
215 	"NOT IMPLEMENTED: Bd -file",
216 	"skipping display without arguments",
217 	"missing list type, using -item",
218 	"argument is not numeric, using 1",
219 	"argument is not a character",
220 	"missing manual name, using \"\"",
221 	"uname(3) system call failed, using UNKNOWN",
222 	"unknown standard specifier",
223 	"skipping request without numeric argument",
224 	"excessive shift",
225 	"NOT IMPLEMENTED: .so with absolute path or \"..\"",
226 	".so request failed",
227 	"skipping tag containing whitespace",
228 	"skipping all arguments",
229 	"skipping excess arguments",
230 	"divide by zero",
231 
232 	"unsupported feature",
233 	"input too large",
234 	"unsupported control character",
235 	"unsupported escape sequence",
236 	"unsupported roff request",
237 	"nested .while loops",
238 	"end of scope with open .while loop",
239 	"end of .while loop in inner scope",
240 	"cannot continue this .while loop",
241 	"eqn delim option in tbl",
242 	"unsupported tbl layout modifier",
243 	"ignoring macro in table",
244 
245 	/* bad command line arguments */
246 	NULL,
247 	"bad command line argument",
248 	"duplicate command line argument",
249 	"option has a superfluous value",
250 	"missing option value",
251 	"bad option value",
252 	"duplicate option value",
253 	"no such tag",
254 
255 	/* system errors */
256 	NULL,
257 	"dup",
258 	"exec",
259 	"fdopen",
260 	"fflush",
261 	"fork",
262 	"fstat",
263 	"getline",
264 	"glob",
265 	"gzclose",
266 	"gzdopen",
267 	"mkstemp",
268 	"open",
269 	"pledge",
270 	"read",
271 	"wait",
272 	"write",
273 };
274 
275 static	FILE		*fileptr = NULL;
276 static	const char	*filename = NULL;
277 static	enum mandocerr	 min_type = MANDOCERR_BADARG;
278 static	enum mandoclevel rc = MANDOCLEVEL_OK;
279 
280 
281 void
282 mandoc_msg_setoutfile(FILE *fp)
283 {
284 	fileptr = fp;
285 }
286 
287 const char *
288 mandoc_msg_getinfilename(void)
289 {
290 	return filename;
291 }
292 
293 void
294 mandoc_msg_setinfilename(const char *fn)
295 {
296 	filename = fn;
297 }
298 
299 enum mandocerr
300 mandoc_msg_getmin(void)
301 {
302 	return min_type;
303 }
304 
305 void
306 mandoc_msg_setmin(enum mandocerr t)
307 {
308 	min_type = t;
309 }
310 
311 enum mandoclevel
312 mandoc_msg_getrc(void)
313 {
314 	return rc;
315 }
316 
317 void
318 mandoc_msg_setrc(enum mandoclevel level)
319 {
320 	if (rc < level)
321 		rc = level;
322 }
323 
324 void
325 mandoc_msg(enum mandocerr t, int line, int col, const char *fmt, ...)
326 {
327 	va_list			 ap;
328 	enum mandoclevel	 level;
329 
330 	if (t < min_type)
331 		return;
332 
333 	level = MANDOCLEVEL_SYSERR;
334 	while (t < lowest_type[level])
335 		level--;
336 	mandoc_msg_setrc(level);
337 
338 	if (fileptr == NULL)
339 		return;
340 
341 	fprintf(fileptr, "%s:", getprogname());
342 	if (filename != NULL)
343 		fprintf(fileptr, " %s:", filename);
344 
345 	if (line > 0)
346 		fprintf(fileptr, "%d:%d:", line, col + 1);
347 
348 	fprintf(fileptr, " %s", level_name[level]);
349 	if (type_message[t] != NULL)
350 		fprintf(fileptr, ": %s", type_message[t]);
351 
352 	if (fmt != NULL) {
353 		fprintf(fileptr, ": ");
354 		va_start(ap, fmt);
355 		vfprintf(fileptr, fmt, ap);
356 		va_end(ap);
357 	}
358 	fputc('\n', fileptr);
359 }
360 
361 void
362 mandoc_msg_summary(void)
363 {
364 	if (fileptr != NULL && rc != MANDOCLEVEL_OK)
365 		fprintf(fileptr,
366 		    "%s: see above the output for %s messages\n",
367 		    getprogname(), level_name[rc]);
368 }
369