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