Imported Upstream version 1.22.4
[platform/upstream/groff.git] / src / libs / libgroff / error.cpp
1 // -*- C++ -*-
2 /* Copyright (C) 1989-2018 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   if (need_space) {
54     fputc(' ', stderr);
55     need_space = 0;
56   }
57   switch (type) {
58   case FATAL:
59     fputs("fatal error:", stderr);
60     need_space = 1;
61     break;
62   case ERROR:
63     break;
64   case WARNING:
65     fputs("warning:", stderr);
66     need_space = 1;
67     break;
68   }
69   if (need_space)
70     fputc(' ', stderr);
71   errprint(format, arg1, arg2, arg3);
72   fputc('\n', stderr);
73   fflush(stderr);
74   if (type == FATAL)
75     fatal_error_exit();
76 }
77       
78
79 static void do_error(error_type type, 
80                      const char *format, 
81                      const errarg &arg1,
82                      const errarg &arg2,
83                      const errarg &arg3)
84 {
85   do_error_with_file_and_line(current_filename, current_source_filename,
86                               current_lineno, type, format, arg1, arg2, arg3);
87 }
88
89
90 void error(const char *format, 
91            const errarg &arg1,
92            const errarg &arg2,
93            const errarg &arg3)
94 {
95   do_error(ERROR, format, arg1, arg2, arg3);
96 }
97
98 void warning(const char *format, 
99              const errarg &arg1,
100              const errarg &arg2,
101              const errarg &arg3)
102 {
103   do_error(WARNING, format, arg1, arg2, arg3);
104 }
105
106 void fatal(const char *format, 
107            const errarg &arg1,
108            const errarg &arg2,
109            const errarg &arg3)
110 {
111   do_error(FATAL, format, arg1, arg2, arg3);
112 }
113
114 void error_with_file_and_line(const char *filename,
115                               int lineno,
116                               const char *format, 
117                               const errarg &arg1,
118                               const errarg &arg2,
119                               const errarg &arg3)
120 {
121   do_error_with_file_and_line(filename, 0, lineno, 
122                               ERROR, format, arg1, arg2, arg3);
123 }
124
125 void warning_with_file_and_line(const char *filename,
126                                 int lineno,
127                                 const char *format, 
128                                 const errarg &arg1,
129                                 const errarg &arg2,
130                                 const errarg &arg3)
131 {
132   do_error_with_file_and_line(filename, 0, lineno, 
133                               WARNING, format, arg1, arg2, arg3);
134 }
135
136 void fatal_with_file_and_line(const char *filename,
137                               int lineno,
138                               const char *format, 
139                               const errarg &arg1,
140                               const errarg &arg2,
141                               const errarg &arg3)
142 {
143   do_error_with_file_and_line(filename, 0, lineno, 
144                               FATAL, format, arg1, arg2, arg3);
145 }