xref: /openbsd-src/usr.bin/mandoc/mandoc_msg.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /* $OpenBSD: mandoc_msg.c,v 1.10 2020/09/01 18:24:10 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 	"ignoring excessive spacing in tbl layout",
192 	"tbl without any data cells",
193 	"ignoring data in spanned tbl cell",
194 	"ignoring extra tbl data cells",
195 	"data block open at end of tbl",
196 
197 	/* related to document structure and macros */
198 	"duplicate prologue macro",
199 	"skipping late title macro",
200 	"input stack limit exceeded, infinite loop?",
201 	"skipping bad character",
202 	"skipping unknown macro",
203 	"ignoring request outside macro",
204 	"skipping insecure request",
205 	"skipping item outside list",
206 	"skipping column outside column list",
207 	"skipping end of block that is not open",
208 	"fewer RS blocks open, skipping",
209 	"inserting missing end of block",
210 	"appending missing end of block",
211 
212 	/* related to request and macro arguments */
213 	"escaped character not allowed in a name",
214 	"using macro argument outside macro",
215 	"argument number is not numeric",
216 	"NOT IMPLEMENTED: Bd -file",
217 	"skipping display without arguments",
218 	"missing list type, using -item",
219 	"argument is not numeric, using 1",
220 	"argument is not a character",
221 	"missing manual name, using \"\"",
222 	"uname(3) system call failed, using UNKNOWN",
223 	"unknown standard specifier",
224 	"skipping request without numeric argument",
225 	"excessive shift",
226 	"NOT IMPLEMENTED: .so with absolute path or \"..\"",
227 	".so request failed",
228 	"skipping tag containing whitespace",
229 	"skipping all arguments",
230 	"skipping excess arguments",
231 	"divide by zero",
232 
233 	"unsupported feature",
234 	"input too large",
235 	"unsupported control character",
236 	"unsupported escape sequence",
237 	"unsupported roff request",
238 	"nested .while loops",
239 	"end of scope with open .while loop",
240 	"end of .while loop in inner scope",
241 	"cannot continue this .while loop",
242 	"eqn delim option in tbl",
243 	"unsupported tbl layout modifier",
244 	"ignoring macro in table",
245 
246 	/* bad command line arguments */
247 	NULL,
248 	"bad command line argument",
249 	"duplicate command line argument",
250 	"option has a superfluous value",
251 	"missing option value",
252 	"bad option value",
253 	"duplicate option value",
254 	"no such tag",
255 
256 	/* system errors */
257 	NULL,
258 	"dup",
259 	"exec",
260 	"fdopen",
261 	"fflush",
262 	"fork",
263 	"fstat",
264 	"getline",
265 	"glob",
266 	"gzclose",
267 	"gzdopen",
268 	"mkstemp",
269 	"open",
270 	"pledge",
271 	"read",
272 	"wait",
273 	"write",
274 };
275 
276 static	FILE		*fileptr = NULL;
277 static	const char	*filename = NULL;
278 static	enum mandocerr	 min_type = MANDOCERR_BADARG;
279 static	enum mandoclevel rc = MANDOCLEVEL_OK;
280 
281 
282 void
283 mandoc_msg_setoutfile(FILE *fp)
284 {
285 	fileptr = fp;
286 }
287 
288 const char *
289 mandoc_msg_getinfilename(void)
290 {
291 	return filename;
292 }
293 
294 void
295 mandoc_msg_setinfilename(const char *fn)
296 {
297 	filename = fn;
298 }
299 
300 enum mandocerr
301 mandoc_msg_getmin(void)
302 {
303 	return min_type;
304 }
305 
306 void
307 mandoc_msg_setmin(enum mandocerr t)
308 {
309 	min_type = t;
310 }
311 
312 enum mandoclevel
313 mandoc_msg_getrc(void)
314 {
315 	return rc;
316 }
317 
318 void
319 mandoc_msg_setrc(enum mandoclevel level)
320 {
321 	if (rc < level)
322 		rc = level;
323 }
324 
325 void
326 mandoc_msg(enum mandocerr t, int line, int col, const char *fmt, ...)
327 {
328 	va_list			 ap;
329 	enum mandoclevel	 level;
330 
331 	if (t < min_type)
332 		return;
333 
334 	level = MANDOCLEVEL_SYSERR;
335 	while (t < lowest_type[level])
336 		level--;
337 	mandoc_msg_setrc(level);
338 
339 	if (fileptr == NULL)
340 		return;
341 
342 	fprintf(fileptr, "%s:", getprogname());
343 	if (filename != NULL)
344 		fprintf(fileptr, " %s:", filename);
345 
346 	if (line > 0)
347 		fprintf(fileptr, "%d:%d:", line, col + 1);
348 
349 	fprintf(fileptr, " %s", level_name[level]);
350 	if (type_message[t] != NULL)
351 		fprintf(fileptr, ": %s", type_message[t]);
352 
353 	if (fmt != NULL) {
354 		fprintf(fileptr, ": ");
355 		va_start(ap, fmt);
356 		vfprintf(fileptr, fmt, ap);
357 		va_end(ap);
358 	}
359 	fputc('\n', fileptr);
360 }
361 
362 void
363 mandoc_msg_summary(void)
364 {
365 	if (fileptr != NULL && rc != MANDOCLEVEL_OK)
366 		fprintf(fileptr,
367 		    "%s: see above the output for %s messages\n",
368 		    getprogname(), level_name[rc]);
369 }
370