1 /* $OpenBSD: mandoc_msg.c,v 1.8 2020/01/19 17:59:01 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2014-2020 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_BADARG, 31 MANDOCERR_SYSERR 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 \"\"", 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 "duplicate prologue macro", 195 "skipping late title macro", 196 "input stack limit exceeded, infinite loop?", 197 "skipping bad character", 198 "skipping unknown macro", 199 "ignoring request outside macro", 200 "skipping insecure request", 201 "skipping item outside list", 202 "skipping column outside column list", 203 "skipping end of block that is not open", 204 "fewer RS blocks open, skipping", 205 "inserting missing end of block", 206 "appending missing end of block", 207 208 /* related to request and macro arguments */ 209 "escaped character not allowed in a name", 210 "using macro argument outside macro", 211 "argument number is not numeric", 212 "NOT IMPLEMENTED: Bd -file", 213 "skipping display without arguments", 214 "missing list type, using -item", 215 "argument is not numeric, using 1", 216 "argument is not a character", 217 "missing manual name, using \"\"", 218 "uname(3) system call failed, using UNKNOWN", 219 "unknown standard specifier", 220 "skipping request without numeric argument", 221 "excessive shift", 222 "NOT IMPLEMENTED: .so with absolute path or \"..\"", 223 ".so request failed", 224 "skipping tag containing whitespace", 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 /* bad command line arguments */ 243 NULL, 244 "bad command line argument", 245 "duplicate command line argument", 246 "option has a superfluous value", 247 "missing option value", 248 "bad option value", 249 "duplicate option value", 250 "no such tag", 251 252 /* system errors */ 253 NULL, 254 "dup", 255 "exec", 256 "fdopen", 257 "fflush", 258 "fork", 259 "fstat", 260 "getline", 261 "glob", 262 "gzclose", 263 "gzdopen", 264 "mkstemp", 265 "open", 266 "pledge", 267 "read", 268 "wait", 269 "write", 270 }; 271 272 static FILE *fileptr = NULL; 273 static const char *filename = NULL; 274 static enum mandocerr min_type = MANDOCERR_BADARG; 275 static enum mandoclevel rc = MANDOCLEVEL_OK; 276 277 278 void 279 mandoc_msg_setoutfile(FILE *fp) 280 { 281 fileptr = fp; 282 } 283 284 const char * 285 mandoc_msg_getinfilename(void) 286 { 287 return filename; 288 } 289 290 void 291 mandoc_msg_setinfilename(const char *fn) 292 { 293 filename = fn; 294 } 295 296 enum mandocerr 297 mandoc_msg_getmin(void) 298 { 299 return min_type; 300 } 301 302 void 303 mandoc_msg_setmin(enum mandocerr t) 304 { 305 min_type = t; 306 } 307 308 enum mandoclevel 309 mandoc_msg_getrc(void) 310 { 311 return rc; 312 } 313 314 void 315 mandoc_msg_setrc(enum mandoclevel level) 316 { 317 if (rc < level) 318 rc = level; 319 } 320 321 void 322 mandoc_msg(enum mandocerr t, int line, int col, const char *fmt, ...) 323 { 324 va_list ap; 325 enum mandoclevel level; 326 327 if (t < min_type) 328 return; 329 330 level = MANDOCLEVEL_SYSERR; 331 while (t < lowest_type[level]) 332 level--; 333 mandoc_msg_setrc(level); 334 335 if (fileptr == NULL) 336 return; 337 338 fprintf(fileptr, "%s:", getprogname()); 339 if (filename != NULL) 340 fprintf(fileptr, " %s:", filename); 341 342 if (line > 0) 343 fprintf(fileptr, "%d:%d:", line, col + 1); 344 345 fprintf(fileptr, " %s", level_name[level]); 346 if (type_message[t] != NULL) 347 fprintf(fileptr, ": %s", type_message[t]); 348 349 if (fmt != NULL) { 350 fprintf(fileptr, ": "); 351 va_start(ap, fmt); 352 vfprintf(fileptr, fmt, ap); 353 va_end(ap); 354 } 355 fputc('\n', fileptr); 356 } 357 358 void 359 mandoc_msg_summary(void) 360 { 361 if (fileptr != NULL && rc != MANDOCLEVEL_OK) 362 fprintf(fileptr, 363 "%s: see above the output for %s messages\n", 364 getprogname(), level_name[rc]); 365 } 366