1 /* $NetBSD: error.cpp,v 1.1.1.1 2016/01/13 18:41:48 christos Exp $ */ 2 3 // -*- C++ -*- 4 /* Copyright (C) 1989, 1990, 1991, 1992, 2003 Free Software Foundation, Inc. 5 Written by James Clark (jjc@jclark.com) 6 7 This file is part of groff. 8 9 groff is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free 11 Software Foundation; either version 2, or (at your option) any later 12 version. 13 14 groff is distributed in the hope that it will be useful, but WITHOUT ANY 15 WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17 for more details. 18 19 You should have received a copy of the GNU General Public License along 20 with groff; see the file COPYING. If not, write to the Free Software 21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include "errarg.h" 27 #include "error.h" 28 29 extern void fatal_error_exit(); 30 31 enum error_type { WARNING, ERROR, FATAL }; 32 33 static void do_error_with_file_and_line(const char *filename, 34 const char *source_filename, 35 int lineno, 36 error_type type, 37 const char *format, 38 const errarg &arg1, 39 const errarg &arg2, 40 const errarg &arg3) 41 { 42 int need_space = 0; 43 if (program_name) { 44 fprintf(stderr, "%s:", program_name); 45 need_space = 1; 46 } 47 if (lineno >= 0 && filename != 0) { 48 if (strcmp(filename, "-") == 0) 49 filename = "<standard input>"; 50 if (source_filename != 0) 51 fprintf(stderr, "%s (%s):%d:", filename, source_filename, lineno); 52 else 53 fprintf(stderr, "%s:%d:", filename, lineno); 54 need_space = 1; 55 } 56 switch (type) { 57 case FATAL: 58 fputs("fatal error:", stderr); 59 need_space = 1; 60 break; 61 case ERROR: 62 break; 63 case WARNING: 64 fputs("warning:", stderr); 65 need_space = 1; 66 break; 67 } 68 if (need_space) 69 fputc(' ', stderr); 70 errprint(format, arg1, arg2, arg3); 71 fputc('\n', stderr); 72 fflush(stderr); 73 if (type == FATAL) 74 fatal_error_exit(); 75 } 76 77 78 static void do_error(error_type type, 79 const char *format, 80 const errarg &arg1, 81 const errarg &arg2, 82 const errarg &arg3) 83 { 84 do_error_with_file_and_line(current_filename, current_source_filename, 85 current_lineno, type, format, arg1, arg2, arg3); 86 } 87 88 89 void error(const char *format, 90 const errarg &arg1, 91 const errarg &arg2, 92 const errarg &arg3) 93 { 94 do_error(ERROR, format, arg1, arg2, arg3); 95 } 96 97 void warning(const char *format, 98 const errarg &arg1, 99 const errarg &arg2, 100 const errarg &arg3) 101 { 102 do_error(WARNING, format, arg1, arg2, arg3); 103 } 104 105 void fatal(const char *format, 106 const errarg &arg1, 107 const errarg &arg2, 108 const errarg &arg3) 109 { 110 do_error(FATAL, format, arg1, arg2, arg3); 111 } 112 113 void error_with_file_and_line(const char *filename, 114 int lineno, 115 const char *format, 116 const errarg &arg1, 117 const errarg &arg2, 118 const errarg &arg3) 119 { 120 do_error_with_file_and_line(filename, 0, lineno, 121 ERROR, format, arg1, arg2, arg3); 122 } 123 124 void warning_with_file_and_line(const char *filename, 125 int lineno, 126 const char *format, 127 const errarg &arg1, 128 const errarg &arg2, 129 const errarg &arg3) 130 { 131 do_error_with_file_and_line(filename, 0, lineno, 132 WARNING, format, arg1, arg2, arg3); 133 } 134 135 void fatal_with_file_and_line(const char *filename, 136 int lineno, 137 const char *format, 138 const errarg &arg1, 139 const errarg &arg2, 140 const errarg &arg3) 141 { 142 do_error_with_file_and_line(filename, 0, lineno, 143 FATAL, format, arg1, arg2, arg3); 144 } 145