Remove no longer needed catman periodic via 'make upgrade'.
[dragonfly.git] / contrib / groff / src / libs / libgroff / error.cpp
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992, 2003, 2009
3      Free Software Foundation, Inc.
4      Written by James Clark (jjc@jclark.com)
5
6 This file is part of groff.
7
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "errarg.h"
25 #include "error.h"
26
27 extern void fatal_error_exit();
28
29 enum error_type { WARNING, ERROR, FATAL };
30
31 static 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
76 static 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
87 void 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
95 void 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
103 void 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
111 void 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
122 void 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
133 void 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 }