1/*
2 * msg.c - routines for error messages
3 */
4
5/*
6 * Copyright (C) 1986, 1988, 1989, 1991-2001, 2003 the Free Software Foundation, Inc.
7 *
8 * This file is part of GAWK, the GNU implementation of the
9 * AWK Programming Language.
10 *
11 * GAWK is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * GAWK is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
24 */
25
26#include "awk.h"
27
28int sourceline = 0;
29char *source = NULL;
30
31static const char *srcfile = NULL;
32static int srcline;
33
34/* err --- print an error message with source line and file and record */
35
36/* VARARGS2 */
37void
38err(const char *s, const char *emsg, va_list argp)
39{
40	char *file;
41
42	(void) fflush(stdout);
43	(void) fprintf(stderr, "%s: ", myname);
44#ifdef GAWKDEBUG
45	if (srcfile != NULL) {
46		fprintf(stderr, "%s:%d:", srcfile, srcline);
47		srcfile = NULL;
48	}
49#endif /* GAWKDEBUG */
50	if (sourceline != 0) {
51		if (source != NULL)
52			(void) fprintf(stderr, "%s:", source);
53		else
54			(void) fprintf(stderr, _("cmd. line:"));
55
56		(void) fprintf(stderr, "%d: ", sourceline);
57	}
58	if (FNR > 0) {
59		file = FILENAME_node->var_value->stptr;
60		(void) putc('(', stderr);
61		if (file)
62			(void) fprintf(stderr, "FILENAME=%s ", file);
63		(void) fprintf(stderr, "FNR=%ld) ", FNR);
64	}
65	(void) fprintf(stderr, "%s", s);
66	vfprintf(stderr, emsg, argp);
67	(void) fprintf(stderr, "\n");
68	(void) fflush(stderr);
69}
70
71/* msg --- take a varargs error message and print it */
72
73/*
74 * Function identifier purposely indented to avoid mangling
75 * by ansi2knr.  Sigh.
76 */
77
78void
79#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
80  msg(const char *mesg, ...)
81#else
82/*VARARGS0*/
83  msg(va_alist)
84  va_dcl
85#endif
86{
87	va_list args;
88#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
89	va_start(args, mesg);
90#else
91	char *mesg;
92
93	va_start(args);
94	mesg = va_arg(args, char *);
95#endif
96	err("", mesg, args);
97	va_end(args);
98}
99
100/* warning --- print a warning message */
101
102void
103#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
104  warning(const char *mesg, ...)
105#else
106/*VARARGS0*/
107  warning(va_alist)
108  va_dcl
109#endif
110{
111	va_list args;
112#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
113	va_start(args, mesg);
114#else
115	char *mesg;
116
117	va_start(args);
118	mesg = va_arg(args, char *);
119#endif
120	err(_("warning: "), mesg, args);
121	va_end(args);
122}
123
124void
125#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
126  error(const char *mesg, ...)
127#else
128/*VARARGS0*/
129  error(va_alist)
130  va_dcl
131#endif
132{
133	va_list args;
134#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
135	va_start(args, mesg);
136#else
137	char *mesg;
138
139	va_start(args);
140	mesg = va_arg(args, char *);
141#endif
142	err(_("error: "), mesg, args);
143	va_end(args);
144}
145
146/* set_loc --- set location where a fatal error happened */
147
148void
149set_loc(const char *file, int line)
150{
151	srcfile = file;
152	srcline = line;
153
154	/* This stupid line keeps some compilers happy: */
155	file = srcfile; line = srcline;
156}
157
158/* fatal --- print an error message and die */
159
160void
161#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
162  r_fatal(const char *mesg, ...)
163#else
164/*VARARGS0*/
165  r_fatal(va_alist)
166  va_dcl
167#endif
168{
169	va_list args;
170#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
171	va_start(args, mesg);
172#else
173	char *mesg;
174
175	va_start(args);
176	mesg = va_arg(args, char *);
177#endif
178	err(_("fatal: "), mesg, args);
179	va_end(args);
180#ifdef GAWKDEBUG
181	abort();
182#endif
183	exit(2);
184}
185