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