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