1/*	Id: mandoc_msg.c,v 1.6 2019/03/06 15:55:38 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 "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_MAX,
33	MANDOCERR_MAX
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	NULL,
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 all arguments",
228	"skipping excess arguments",
229	"divide by zero",
230
231	"unsupported feature",
232	"input too large",
233	"unsupported control character",
234	"unsupported escape sequence",
235	"unsupported roff request",
236	"nested .while loops",
237	"end of scope with open .while loop",
238	"end of .while loop in inner scope",
239	"cannot continue this .while loop",
240	"eqn delim option in tbl",
241	"unsupported tbl layout modifier",
242	"ignoring macro in table",
243};
244
245static	FILE		*fileptr = NULL;
246static	const char	*filename = NULL;
247static	enum mandocerr	 min_type = MANDOCERR_MAX;
248static	enum mandoclevel rc = MANDOCLEVEL_OK;
249
250
251void
252mandoc_msg_setoutfile(FILE *fp)
253{
254	fileptr = fp;
255}
256
257const char *
258mandoc_msg_getinfilename(void)
259{
260	return filename;
261}
262
263void
264mandoc_msg_setinfilename(const char *fn)
265{
266	filename = fn;
267}
268
269enum mandocerr
270mandoc_msg_getmin(void)
271{
272	return min_type;
273}
274
275void
276mandoc_msg_setmin(enum mandocerr t)
277{
278	min_type = t;
279}
280
281enum mandoclevel
282mandoc_msg_getrc(void)
283{
284	return rc;
285}
286
287void
288mandoc_msg_setrc(enum mandoclevel level)
289{
290	if (rc < level)
291		rc = level;
292}
293
294void
295mandoc_msg(enum mandocerr t, int line, int col, const char *fmt, ...)
296{
297	va_list			 ap;
298	enum mandoclevel	 level;
299
300	if (t < min_type && t != MANDOCERR_FILE)
301		return;
302
303	level = MANDOCLEVEL_UNSUPP;
304	while (t < lowest_type[level])
305		level--;
306	mandoc_msg_setrc(level);
307
308	if (fileptr == NULL)
309		return;
310
311	fprintf(fileptr, "%s:", getprogname());
312	if (filename != NULL)
313		fprintf(fileptr, " %s:", filename);
314
315	if (line > 0)
316		fprintf(fileptr, "%d:%d:", line, col + 1);
317
318	fprintf(fileptr, " %s", level_name[level]);
319	if (type_message[t] != NULL)
320		fprintf(fileptr, ": %s", type_message[t]);
321
322	if (fmt != NULL) {
323		fprintf(fileptr, ": ");
324		va_start(ap, fmt);
325		vfprintf(fileptr, fmt, ap);
326		va_end(ap);
327	}
328	fputc('\n', fileptr);
329}
330