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