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