Home | History | Annotate | Line # | Download | only in dist
      1  1.1.1.2       wiz /* $OpenBSD: mandoc_msg.c,v 1.8 2020/01/19 17:59:01 schwarze Exp $ */
      2      1.1  christos /*
      3  1.1.1.2       wiz  * Copyright (c) 2014-2021 Ingo Schwarze <schwarze (at) openbsd.org>
      4      1.1  christos  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
      5      1.1  christos  *
      6      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      7      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      8      1.1  christos  * copyright notice and this permission notice appear in all copies.
      9      1.1  christos  *
     10      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
     11      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12      1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
     13      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16      1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1.1.2       wiz  *
     18  1.1.1.2       wiz  * Implementation of warning and error messages for mandoc(1).
     19      1.1  christos  */
     20      1.1  christos #include "config.h"
     21      1.1  christos 
     22      1.1  christos #include <stdarg.h>
     23      1.1  christos #include <stdio.h>
     24      1.1  christos #include <stdlib.h>
     25      1.1  christos 
     26      1.1  christos #include "mandoc.h"
     27      1.1  christos 
     28      1.1  christos static	const enum mandocerr lowest_type[MANDOCLEVEL_MAX] = {
     29      1.1  christos 	MANDOCERR_OK,
     30      1.1  christos 	MANDOCERR_OK,
     31      1.1  christos 	MANDOCERR_WARNING,
     32      1.1  christos 	MANDOCERR_ERROR,
     33      1.1  christos 	MANDOCERR_UNSUPP,
     34  1.1.1.2       wiz 	MANDOCERR_BADARG,
     35  1.1.1.2       wiz 	MANDOCERR_SYSERR
     36      1.1  christos };
     37      1.1  christos 
     38      1.1  christos static	const char *const level_name[MANDOCLEVEL_MAX] = {
     39      1.1  christos 	"SUCCESS",
     40      1.1  christos 	"STYLE",
     41      1.1  christos 	"WARNING",
     42      1.1  christos 	"ERROR",
     43      1.1  christos 	"UNSUPP",
     44      1.1  christos 	"BADARG",
     45      1.1  christos 	"SYSERR"
     46      1.1  christos };
     47      1.1  christos 
     48      1.1  christos static	const char *const type_message[MANDOCERR_MAX] = {
     49      1.1  christos 	"ok",
     50      1.1  christos 
     51      1.1  christos 	"base system convention",
     52      1.1  christos 
     53      1.1  christos 	"Mdocdate found",
     54      1.1  christos 	"Mdocdate missing",
     55      1.1  christos 	"unknown architecture",
     56      1.1  christos 	"operating system explicitly specified",
     57      1.1  christos 	"RCS id missing",
     58      1.1  christos 
     59      1.1  christos 	"generic style suggestion",
     60      1.1  christos 
     61      1.1  christos 	"legacy man(7) date format",
     62      1.1  christos 	"normalizing date format to",
     63      1.1  christos 	"lower case character in document title",
     64      1.1  christos 	"duplicate RCS id",
     65      1.1  christos 	"possible typo in section name",
     66      1.1  christos 	"unterminated quoted argument",
     67      1.1  christos 	"useless macro",
     68      1.1  christos 	"consider using OS macro",
     69      1.1  christos 	"errnos out of order",
     70      1.1  christos 	"duplicate errno",
     71  1.1.1.2       wiz 	"referenced manual not found",
     72      1.1  christos 	"trailing delimiter",
     73      1.1  christos 	"no blank before trailing delimiter",
     74      1.1  christos 	"fill mode already enabled, skipping",
     75      1.1  christos 	"fill mode already disabled, skipping",
     76  1.1.1.2       wiz 	"input text line longer than 80 bytes",
     77      1.1  christos 	"verbatim \"--\", maybe consider using \\(em",
     78      1.1  christos 	"function name without markup",
     79      1.1  christos 	"whitespace at end of input line",
     80      1.1  christos 	"bad comment style",
     81      1.1  christos 
     82      1.1  christos 	"generic warning",
     83      1.1  christos 
     84      1.1  christos 	/* related to the prologue */
     85      1.1  christos 	"missing manual title, using UNTITLED",
     86      1.1  christos 	"missing manual title, using \"\"",
     87      1.1  christos 	"missing manual section, using \"\"",
     88      1.1  christos 	"unknown manual section",
     89  1.1.1.2       wiz 	"filename/section mismatch",
     90  1.1.1.2       wiz 	"missing date, using \"\"",
     91      1.1  christos 	"cannot parse date, using it verbatim",
     92      1.1  christos 	"date in the future, using it anyway",
     93      1.1  christos 	"missing Os macro, using \"\"",
     94      1.1  christos 	"late prologue macro",
     95      1.1  christos 	"prologue macros out of order",
     96      1.1  christos 
     97      1.1  christos 	/* related to document structure */
     98      1.1  christos 	".so is fragile, better use ln(1)",
     99      1.1  christos 	"no document body",
    100      1.1  christos 	"content before first section header",
    101      1.1  christos 	"first section is not \"NAME\"",
    102      1.1  christos 	"NAME section without Nm before Nd",
    103      1.1  christos 	"NAME section without description",
    104      1.1  christos 	"description not at the end of NAME",
    105      1.1  christos 	"bad NAME section content",
    106      1.1  christos 	"missing comma before name",
    107      1.1  christos 	"missing description line, using \"\"",
    108      1.1  christos 	"description line outside NAME section",
    109      1.1  christos 	"sections out of conventional order",
    110      1.1  christos 	"duplicate section title",
    111      1.1  christos 	"unexpected section",
    112      1.1  christos 	"cross reference to self",
    113      1.1  christos 	"unusual Xr order",
    114      1.1  christos 	"unusual Xr punctuation",
    115      1.1  christos 	"AUTHORS section without An macro",
    116      1.1  christos 
    117      1.1  christos 	/* related to macros and nesting */
    118      1.1  christos 	"obsolete macro",
    119      1.1  christos 	"macro neither callable nor escaped",
    120      1.1  christos 	"skipping paragraph macro",
    121      1.1  christos 	"moving paragraph macro out of list",
    122      1.1  christos 	"skipping no-space macro",
    123      1.1  christos 	"blocks badly nested",
    124      1.1  christos 	"nested displays are not portable",
    125      1.1  christos 	"moving content out of list",
    126      1.1  christos 	"first macro on line",
    127      1.1  christos 	"line scope broken",
    128      1.1  christos 	"skipping blank line in line scope",
    129      1.1  christos 
    130      1.1  christos 	/* related to missing macro arguments */
    131      1.1  christos 	"skipping empty request",
    132      1.1  christos 	"conditional request controls empty scope",
    133      1.1  christos 	"skipping empty macro",
    134      1.1  christos 	"empty block",
    135      1.1  christos 	"empty argument, using 0n",
    136      1.1  christos 	"missing display type, using -ragged",
    137      1.1  christos 	"list type is not the first argument",
    138      1.1  christos 	"missing -width in -tag list, using 6n",
    139      1.1  christos 	"missing utility name, using \"\"",
    140      1.1  christos 	"missing function name, using \"\"",
    141      1.1  christos 	"empty head in list item",
    142      1.1  christos 	"empty list item",
    143      1.1  christos 	"missing argument, using next line",
    144      1.1  christos 	"missing font type, using \\fR",
    145      1.1  christos 	"unknown font type, using \\fR",
    146      1.1  christos 	"nothing follows prefix",
    147      1.1  christos 	"empty reference block",
    148      1.1  christos 	"missing section argument",
    149      1.1  christos 	"missing -std argument, adding it",
    150      1.1  christos 	"missing option string, using \"\"",
    151      1.1  christos 	"missing resource identifier, using \"\"",
    152      1.1  christos 	"missing eqn box, using \"\"",
    153      1.1  christos 
    154      1.1  christos 	/* related to bad macro arguments */
    155      1.1  christos 	"duplicate argument",
    156      1.1  christos 	"skipping duplicate argument",
    157      1.1  christos 	"skipping duplicate display type",
    158      1.1  christos 	"skipping duplicate list type",
    159      1.1  christos 	"skipping -width argument",
    160      1.1  christos 	"wrong number of cells",
    161      1.1  christos 	"unknown AT&T UNIX version",
    162      1.1  christos 	"comma in function argument",
    163      1.1  christos 	"parenthesis in function name",
    164      1.1  christos 	"unknown library name",
    165      1.1  christos 	"invalid content in Rs block",
    166      1.1  christos 	"invalid Boolean argument",
    167      1.1  christos 	"argument contains two font escapes",
    168      1.1  christos 	"unknown font, skipping request",
    169      1.1  christos 	"odd number of characters in request",
    170      1.1  christos 
    171      1.1  christos 	/* related to plain text */
    172      1.1  christos 	"blank line in fill mode, using .sp",
    173      1.1  christos 	"tab in filled text",
    174      1.1  christos 	"new sentence, new line",
    175      1.1  christos 	"invalid escape sequence",
    176      1.1  christos 	"undefined escape, printing literally",
    177      1.1  christos 	"undefined string, using \"\"",
    178      1.1  christos 
    179      1.1  christos 	/* related to tables */
    180      1.1  christos 	"tbl line starts with span",
    181      1.1  christos 	"tbl column starts with span",
    182      1.1  christos 	"skipping vertical bar in tbl layout",
    183      1.1  christos 
    184      1.1  christos 	"generic error",
    185      1.1  christos 
    186      1.1  christos 	/* related to tables */
    187      1.1  christos 	"non-alphabetic character in tbl options",
    188      1.1  christos 	"skipping unknown tbl option",
    189      1.1  christos 	"missing tbl option argument",
    190      1.1  christos 	"wrong tbl option argument size",
    191      1.1  christos 	"empty tbl layout",
    192      1.1  christos 	"invalid character in tbl layout",
    193      1.1  christos 	"unmatched parenthesis in tbl layout",
    194  1.1.1.2       wiz 	"ignoring excessive spacing in tbl layout",
    195      1.1  christos 	"tbl without any data cells",
    196      1.1  christos 	"ignoring data in spanned tbl cell",
    197      1.1  christos 	"ignoring extra tbl data cells",
    198      1.1  christos 	"data block open at end of tbl",
    199      1.1  christos 
    200      1.1  christos 	/* related to document structure and macros */
    201      1.1  christos 	"duplicate prologue macro",
    202      1.1  christos 	"skipping late title macro",
    203      1.1  christos 	"input stack limit exceeded, infinite loop?",
    204      1.1  christos 	"skipping bad character",
    205      1.1  christos 	"skipping unknown macro",
    206      1.1  christos 	"ignoring request outside macro",
    207      1.1  christos 	"skipping insecure request",
    208      1.1  christos 	"skipping item outside list",
    209      1.1  christos 	"skipping column outside column list",
    210      1.1  christos 	"skipping end of block that is not open",
    211      1.1  christos 	"fewer RS blocks open, skipping",
    212      1.1  christos 	"inserting missing end of block",
    213      1.1  christos 	"appending missing end of block",
    214      1.1  christos 
    215      1.1  christos 	/* related to request and macro arguments */
    216      1.1  christos 	"escaped character not allowed in a name",
    217      1.1  christos 	"using macro argument outside macro",
    218      1.1  christos 	"argument number is not numeric",
    219      1.1  christos 	"NOT IMPLEMENTED: Bd -file",
    220      1.1  christos 	"skipping display without arguments",
    221      1.1  christos 	"missing list type, using -item",
    222      1.1  christos 	"argument is not numeric, using 1",
    223      1.1  christos 	"argument is not a character",
    224      1.1  christos 	"missing manual name, using \"\"",
    225      1.1  christos 	"uname(3) system call failed, using UNKNOWN",
    226      1.1  christos 	"unknown standard specifier",
    227      1.1  christos 	"skipping request without numeric argument",
    228      1.1  christos 	"excessive shift",
    229      1.1  christos 	"NOT IMPLEMENTED: .so with absolute path or \"..\"",
    230      1.1  christos 	".so request failed",
    231  1.1.1.2       wiz 	"skipping tag containing whitespace",
    232      1.1  christos 	"skipping all arguments",
    233      1.1  christos 	"skipping excess arguments",
    234      1.1  christos 	"divide by zero",
    235      1.1  christos 
    236      1.1  christos 	"unsupported feature",
    237      1.1  christos 	"input too large",
    238      1.1  christos 	"unsupported control character",
    239      1.1  christos 	"unsupported escape sequence",
    240      1.1  christos 	"unsupported roff request",
    241      1.1  christos 	"nested .while loops",
    242      1.1  christos 	"end of scope with open .while loop",
    243      1.1  christos 	"end of .while loop in inner scope",
    244      1.1  christos 	"cannot continue this .while loop",
    245      1.1  christos 	"eqn delim option in tbl",
    246      1.1  christos 	"unsupported tbl layout modifier",
    247      1.1  christos 	"ignoring macro in table",
    248  1.1.1.2       wiz 	"skipping tbl in -Tman mode",
    249  1.1.1.2       wiz 	"skipping eqn in -Tman mode",
    250  1.1.1.2       wiz 
    251  1.1.1.2       wiz 	/* bad command line arguments */
    252  1.1.1.2       wiz 	NULL,
    253  1.1.1.2       wiz 	"bad command line argument",
    254  1.1.1.2       wiz 	"duplicate command line argument",
    255  1.1.1.2       wiz 	"option has a superfluous value",
    256  1.1.1.2       wiz 	"missing option value",
    257  1.1.1.2       wiz 	"bad option value",
    258  1.1.1.2       wiz 	"duplicate option value",
    259  1.1.1.2       wiz 	"no such tag",
    260  1.1.1.2       wiz 	"-Tmarkdown unsupported for man(7) input",
    261  1.1.1.2       wiz 
    262  1.1.1.2       wiz 	/* system errors */
    263  1.1.1.2       wiz 	NULL,
    264  1.1.1.2       wiz 	"dup",
    265  1.1.1.2       wiz 	"exec",
    266  1.1.1.2       wiz 	"fdopen",
    267  1.1.1.2       wiz 	"fflush",
    268  1.1.1.2       wiz 	"fork",
    269  1.1.1.2       wiz 	"fstat",
    270  1.1.1.2       wiz 	"getline",
    271  1.1.1.2       wiz 	"glob",
    272  1.1.1.2       wiz 	"gzclose",
    273  1.1.1.2       wiz 	"gzdopen",
    274  1.1.1.2       wiz 	"mkstemp",
    275  1.1.1.2       wiz 	"open",
    276  1.1.1.2       wiz 	"pledge",
    277  1.1.1.2       wiz 	"read",
    278  1.1.1.2       wiz 	"wait",
    279  1.1.1.2       wiz 	"write",
    280      1.1  christos };
    281      1.1  christos 
    282      1.1  christos static	FILE		*fileptr = NULL;
    283      1.1  christos static	const char	*filename = NULL;
    284  1.1.1.2       wiz static	enum mandocerr	 min_type = MANDOCERR_BADARG;
    285      1.1  christos static	enum mandoclevel rc = MANDOCLEVEL_OK;
    286      1.1  christos 
    287      1.1  christos 
    288      1.1  christos void
    289      1.1  christos mandoc_msg_setoutfile(FILE *fp)
    290      1.1  christos {
    291      1.1  christos 	fileptr = fp;
    292      1.1  christos }
    293      1.1  christos 
    294      1.1  christos const char *
    295      1.1  christos mandoc_msg_getinfilename(void)
    296      1.1  christos {
    297      1.1  christos 	return filename;
    298      1.1  christos }
    299      1.1  christos 
    300      1.1  christos void
    301      1.1  christos mandoc_msg_setinfilename(const char *fn)
    302      1.1  christos {
    303      1.1  christos 	filename = fn;
    304      1.1  christos }
    305      1.1  christos 
    306      1.1  christos enum mandocerr
    307      1.1  christos mandoc_msg_getmin(void)
    308      1.1  christos {
    309      1.1  christos 	return min_type;
    310      1.1  christos }
    311      1.1  christos 
    312      1.1  christos void
    313      1.1  christos mandoc_msg_setmin(enum mandocerr t)
    314      1.1  christos {
    315      1.1  christos 	min_type = t;
    316      1.1  christos }
    317      1.1  christos 
    318      1.1  christos enum mandoclevel
    319      1.1  christos mandoc_msg_getrc(void)
    320      1.1  christos {
    321      1.1  christos 	return rc;
    322      1.1  christos }
    323      1.1  christos 
    324      1.1  christos void
    325      1.1  christos mandoc_msg_setrc(enum mandoclevel level)
    326      1.1  christos {
    327      1.1  christos 	if (rc < level)
    328      1.1  christos 		rc = level;
    329      1.1  christos }
    330      1.1  christos 
    331      1.1  christos void
    332      1.1  christos mandoc_msg(enum mandocerr t, int line, int col, const char *fmt, ...)
    333      1.1  christos {
    334      1.1  christos 	va_list			 ap;
    335      1.1  christos 	enum mandoclevel	 level;
    336      1.1  christos 
    337  1.1.1.2       wiz 	if (t < min_type)
    338      1.1  christos 		return;
    339      1.1  christos 
    340  1.1.1.2       wiz 	level = MANDOCLEVEL_SYSERR;
    341      1.1  christos 	while (t < lowest_type[level])
    342      1.1  christos 		level--;
    343      1.1  christos 	mandoc_msg_setrc(level);
    344      1.1  christos 
    345      1.1  christos 	if (fileptr == NULL)
    346      1.1  christos 		return;
    347      1.1  christos 
    348      1.1  christos 	fprintf(fileptr, "%s:", getprogname());
    349      1.1  christos 	if (filename != NULL)
    350      1.1  christos 		fprintf(fileptr, " %s:", filename);
    351      1.1  christos 
    352      1.1  christos 	if (line > 0)
    353      1.1  christos 		fprintf(fileptr, "%d:%d:", line, col + 1);
    354      1.1  christos 
    355      1.1  christos 	fprintf(fileptr, " %s", level_name[level]);
    356      1.1  christos 	if (type_message[t] != NULL)
    357      1.1  christos 		fprintf(fileptr, ": %s", type_message[t]);
    358      1.1  christos 
    359      1.1  christos 	if (fmt != NULL) {
    360      1.1  christos 		fprintf(fileptr, ": ");
    361      1.1  christos 		va_start(ap, fmt);
    362      1.1  christos 		vfprintf(fileptr, fmt, ap);
    363      1.1  christos 		va_end(ap);
    364      1.1  christos 	}
    365      1.1  christos 	fputc('\n', fileptr);
    366      1.1  christos }
    367  1.1.1.2       wiz 
    368  1.1.1.2       wiz void
    369  1.1.1.2       wiz mandoc_msg_summary(void)
    370  1.1.1.2       wiz {
    371  1.1.1.2       wiz 	if (fileptr != NULL && rc != MANDOCLEVEL_OK)
    372  1.1.1.2       wiz 		fprintf(fileptr,
    373  1.1.1.2       wiz 		    "%s: see above the output for %s messages\n",
    374  1.1.1.2       wiz 		    getprogname(), level_name[rc]);
    375  1.1.1.2       wiz }
    376