1152592Sandre// -*- C++ -*-
2152592Sandre/* Copyright (C) 1989, 1990, 1991, 1992, 2003 Free Software Foundation, Inc.
3169464Srwatson     Written by James Clark (jjc@jclark.com)
4169464Srwatson
5169464SrwatsonThis file is part of groff.
6152592Sandre
7152592Sandregroff is free software; you can redistribute it and/or modify it under
8152592Sandrethe terms of the GNU General Public License as published by the Free
9152592SandreSoftware Foundation; either version 2, or (at your option) any later
10152592Sandreversion.
11152592Sandre
12152592Sandregroff is distributed in the hope that it will be useful, but WITHOUT ANY
13152592SandreWARRANTY; without even the implied warranty of MERCHANTABILITY or
14152592SandreFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15152592Sandrefor more details.
16152592Sandre
17152592SandreYou should have received a copy of the GNU General Public License along
18152592Sandrewith groff; see the file COPYING.  If not, write to the Free Software
19152592SandreFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
20152592Sandre
21152592Sandre#include <stdio.h>
22152592Sandre#include <stdlib.h>
23152592Sandre#include <string.h>
24152592Sandre#include "errarg.h"
25152592Sandre#include "error.h"
26152592Sandre
27152592Sandreextern void fatal_error_exit();
28152592Sandre
29152592Sandreenum error_type { WARNING, ERROR, FATAL };
30152592Sandre
31152592Sandrestatic void do_error_with_file_and_line(const char *filename,
32172467Ssilby					const char *source_filename,
33172467Ssilby					int lineno,
34172467Ssilby					error_type type,
35152592Sandre					const char *format,
36152592Sandre					const errarg &arg1,
37152592Sandre					const errarg &arg2,
38152592Sandre					const errarg &arg3)
39152592Sandre{
40152592Sandre  int need_space = 0;
41152592Sandre  if (program_name) {
42152592Sandre    fprintf(stderr, "%s:", program_name);
43152592Sandre    need_space = 1;
44152592Sandre  }
45152592Sandre  if (lineno >= 0 && filename != 0) {
46152592Sandre    if (strcmp(filename, "-") == 0)
47152592Sandre      filename = "<standard input>";
48152592Sandre    if (source_filename != 0)
49152592Sandre      fprintf(stderr, "%s (%s):%d:", filename, source_filename, lineno);
50152592Sandre    else
51152592Sandre      fprintf(stderr, "%s:%d:", filename, lineno);
52152592Sandre    need_space = 1;
53152592Sandre  }
54196019Srwatson  switch (type) {
55152592Sandre  case FATAL:
56152592Sandre    fputs("fatal error:", stderr);
57152592Sandre    need_space = 1;
58152592Sandre    break;
59152592Sandre  case ERROR:
60152592Sandre    break;
61152592Sandre  case WARNING:
62152592Sandre    fputs("warning:", stderr);
63152592Sandre    need_space = 1;
64152592Sandre    break;
65152592Sandre  }
66152592Sandre  if (need_space)
67152592Sandre    fputc(' ', stderr);
68272868Shrs  errprint(format, arg1, arg2, arg3);
69272869Shrs  fputc('\n', stderr);
70272869Shrs  fflush(stderr);
71272868Shrs  if (type == FATAL)
72272868Shrs    fatal_error_exit();
73152592Sandre}
74272868Shrs
75272869Shrs
76272869Shrsstatic void do_error(error_type type,
77152592Sandre		     const char *format,
78272868Shrs		     const errarg &arg1,
79152592Sandre		     const errarg &arg2,
80272868Shrs		     const errarg &arg3)
81272869Shrs{
82272868Shrs  do_error_with_file_and_line(current_filename, current_source_filename,
83152592Sandre			      current_lineno, type, format, arg1, arg2, arg3);
84152592Sandre}
85152592Sandre
86152592Sandre
87169464Srwatsonvoid error(const char *format,
88169464Srwatson	   const errarg &arg1,
89169464Srwatson	   const errarg &arg2,
90169464Srwatson	   const errarg &arg3)
91169464Srwatson{
92169464Srwatson  do_error(ERROR, format, arg1, arg2, arg3);
93169464Srwatson}
94169464Srwatson
95169464Srwatsonvoid warning(const char *format,
96169464Srwatson	     const errarg &arg1,
97152592Sandre	     const errarg &arg2,
98152592Sandre	     const errarg &arg3)
99152592Sandre{
100152592Sandre  do_error(WARNING, format, arg1, arg2, arg3);
101152592Sandre}
102152592Sandre
103152592Sandrevoid fatal(const char *format,
104152592Sandre	   const errarg &arg1,
105152592Sandre	   const errarg &arg2,
106188578Sluigi	   const errarg &arg3)
107152592Sandre{
108152592Sandre  do_error(FATAL, format, arg1, arg2, arg3);
109169464Srwatson}
110272868Shrs
111152592Sandrevoid error_with_file_and_line(const char *filename,
112272868Shrs			      int lineno,
113152592Sandre			      const char *format,
114152592Sandre			      const errarg &arg1,
115152592Sandre			      const errarg &arg2,
116152592Sandre			      const errarg &arg3)
117152592Sandre{
118152592Sandre  do_error_with_file_and_line(filename, 0, lineno,
119152592Sandre			      ERROR, format, arg1, arg2, arg3);
120152592Sandre}
121152592Sandre
122152592Sandrevoid warning_with_file_and_line(const char *filename,
123152592Sandre				int lineno,
124152592Sandre				const char *format,
125152592Sandre				const errarg &arg1,
126152592Sandre				const errarg &arg2,
127152592Sandre				const errarg &arg3)
128152592Sandre{
129152592Sandre  do_error_with_file_and_line(filename, 0, lineno,
130152592Sandre			      WARNING, format, arg1, arg2, arg3);
131152592Sandre}
132152592Sandre
133152592Sandrevoid fatal_with_file_and_line(const char *filename,
134152592Sandre			      int lineno,
135152592Sandre			      const char *format,
136152592Sandre			      const errarg &arg1,
137152592Sandre			      const errarg &arg2,
138152592Sandre			      const errarg &arg3)
139152592Sandre{
140152592Sandre  do_error_with_file_and_line(filename, 0, lineno,
141152592Sandre			      FATAL, format, arg1, arg2, arg3);
142152592Sandre}
143152592Sandre