1114402Sru// -*- C++ -*-
2114402Sru/* Copyright (C) 1989, 1990, 1991, 1992, 2003 Free Software Foundation, Inc.
3114402Sru     Written by James Clark (jjc@jclark.com)
4114402Sru
5114402SruThis file is part of groff.
6114402Sru
7114402Srugroff is free software; you can redistribute it and/or modify it under
8114402Sruthe terms of the GNU General Public License as published by the Free
9114402SruSoftware Foundation; either version 2, or (at your option) any later
10114402Sruversion.
11114402Sru
12114402Srugroff is distributed in the hope that it will be useful, but WITHOUT ANY
13114402SruWARRANTY; without even the implied warranty of MERCHANTABILITY or
14114402SruFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15114402Srufor more details.
16114402Sru
17114402SruYou should have received a copy of the GNU General Public License along
18114402Sruwith groff; see the file COPYING.  If not, write to the Free Software
19151497SruFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
20114402Sru
21114402Sru#include <stdio.h>
22114402Sru#include <stdlib.h>
23114402Sru#include <string.h>
24114402Sru#include "errarg.h"
25114402Sru#include "error.h"
26114402Sru
27114402Sruextern void fatal_error_exit();
28114402Sru
29114402Sruenum error_type { WARNING, ERROR, FATAL };
30114402Sru
31114402Srustatic void do_error_with_file_and_line(const char *filename,
32114402Sru					const char *source_filename,
33114402Sru					int lineno,
34114402Sru					error_type type,
35114402Sru					const char *format,
36114402Sru					const errarg &arg1,
37114402Sru					const errarg &arg2,
38114402Sru					const errarg &arg3)
39114402Sru{
40114402Sru  int need_space = 0;
41114402Sru  if (program_name) {
42114402Sru    fprintf(stderr, "%s:", program_name);
43114402Sru    need_space = 1;
44114402Sru  }
45114402Sru  if (lineno >= 0 && filename != 0) {
46114402Sru    if (strcmp(filename, "-") == 0)
47114402Sru      filename = "<standard input>";
48114402Sru    if (source_filename != 0)
49114402Sru      fprintf(stderr, "%s (%s):%d:", filename, source_filename, lineno);
50114402Sru    else
51114402Sru      fprintf(stderr, "%s:%d:", filename, lineno);
52114402Sru    need_space = 1;
53114402Sru  }
54114402Sru  switch (type) {
55114402Sru  case FATAL:
56114402Sru    fputs("fatal error:", stderr);
57114402Sru    need_space = 1;
58114402Sru    break;
59114402Sru  case ERROR:
60114402Sru    break;
61114402Sru  case WARNING:
62114402Sru    fputs("warning:", stderr);
63114402Sru    need_space = 1;
64114402Sru    break;
65114402Sru  }
66114402Sru  if (need_space)
67114402Sru    fputc(' ', stderr);
68114402Sru  errprint(format, arg1, arg2, arg3);
69114402Sru  fputc('\n', stderr);
70114402Sru  fflush(stderr);
71114402Sru  if (type == FATAL)
72114402Sru    fatal_error_exit();
73114402Sru}
74114402Sru
75114402Sru
76114402Srustatic void do_error(error_type type,
77114402Sru		     const char *format,
78114402Sru		     const errarg &arg1,
79114402Sru		     const errarg &arg2,
80114402Sru		     const errarg &arg3)
81114402Sru{
82114402Sru  do_error_with_file_and_line(current_filename, current_source_filename,
83114402Sru			      current_lineno, type, format, arg1, arg2, arg3);
84114402Sru}
85114402Sru
86114402Sru
87114402Sruvoid error(const char *format,
88114402Sru	   const errarg &arg1,
89114402Sru	   const errarg &arg2,
90114402Sru	   const errarg &arg3)
91114402Sru{
92114402Sru  do_error(ERROR, format, arg1, arg2, arg3);
93114402Sru}
94114402Sru
95114402Sruvoid warning(const char *format,
96114402Sru	     const errarg &arg1,
97114402Sru	     const errarg &arg2,
98114402Sru	     const errarg &arg3)
99114402Sru{
100114402Sru  do_error(WARNING, format, arg1, arg2, arg3);
101114402Sru}
102114402Sru
103114402Sruvoid fatal(const char *format,
104114402Sru	   const errarg &arg1,
105114402Sru	   const errarg &arg2,
106114402Sru	   const errarg &arg3)
107114402Sru{
108114402Sru  do_error(FATAL, format, arg1, arg2, arg3);
109114402Sru}
110114402Sru
111114402Sruvoid error_with_file_and_line(const char *filename,
112114402Sru			      int lineno,
113114402Sru			      const char *format,
114114402Sru			      const errarg &arg1,
115114402Sru			      const errarg &arg2,
116114402Sru			      const errarg &arg3)
117114402Sru{
118114402Sru  do_error_with_file_and_line(filename, 0, lineno,
119114402Sru			      ERROR, format, arg1, arg2, arg3);
120114402Sru}
121114402Sru
122114402Sruvoid warning_with_file_and_line(const char *filename,
123114402Sru				int lineno,
124114402Sru				const char *format,
125114402Sru				const errarg &arg1,
126114402Sru				const errarg &arg2,
127114402Sru				const errarg &arg3)
128114402Sru{
129114402Sru  do_error_with_file_and_line(filename, 0, lineno,
130114402Sru			      WARNING, format, arg1, arg2, arg3);
131114402Sru}
132114402Sru
133114402Sruvoid fatal_with_file_and_line(const char *filename,
134114402Sru			      int lineno,
135114402Sru			      const char *format,
136114402Sru			      const errarg &arg1,
137114402Sru			      const errarg &arg2,
138114402Sru			      const errarg &arg3)
139114402Sru{
140114402Sru  do_error_with_file_and_line(filename, 0, lineno,
141114402Sru			      FATAL, format, arg1, arg2, arg3);
142114402Sru}
143