1 /*
2  * msg.c - routines for error messages
3  */
4 
5 /*
6  * Copyright (C) 1986, 1988, 1989, 1991-2000 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 /* Multi-byte extension added Aug., 1994 by t^2 (Takahiro Tanimoto)
26    Last change: May 1, 1995 by t^2  */
27 
28 #include "awk.h"
29 
30 long sourceline = 0;
31 char *source = NULL;
32 
33 static char *srcfile = NULL;
34 static int srcline;
35 
36 /* prototype needed for ansi / gcc */
37 void err P((const char *s, const char *emsg, va_list argp));
38 
39 /* err --- print an error message with source line and file and record */
40 
41 /* VARARGS2 */
42 void
err(s,emsg,argp)43 err(s, emsg, argp)
44 const char *s;
45 const char *emsg;
46 va_list argp;
47 {
48 	char *file;
49 
50 	(void) fflush(stdout);
51 	(void) fprintf(stderr, "%s: ", myname);
52 #ifdef DEBUG
53 	if (srcfile != NULL) {
54 		fprintf(stderr, "%s:%d:", srcfile, srcline);
55 		srcfile = NULL;
56 	}
57 #endif /* DEBUG */
58 	if (sourceline != 0) {
59 		if (source != NULL)
60 			(void) fprintf(stderr, "%s:", source);
61 		else
62 			(void) fprintf(stderr, "cmd. line:");
63 
64 		(void) fprintf(stderr, "%ld: ", sourceline);
65 	}
66 	if (FNR > 0) {
67 		file = FILENAME_node->var_value->stptr;
68 		(void) putc('(', stderr);
69 		if (file)
70 			(void) fprintf(stderr, "FILENAME=%s ", file);
71 		(void) fprintf(stderr, "FNR=%ld) ", FNR);
72 	}
73 	(void) fprintf(stderr, s);
74 	vfprintf(stderr, emsg, argp);
75 	(void) fprintf(stderr, "\n");
76 	(void) fflush(stderr);
77 }
78 
79 /* msg --- take a varargs error message and print it */
80 
81 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
82 void
msg(char * mesg,...)83 msg(char *mesg, ...)
84 #else
85 /*VARARGS0*/
86 void
87 msg(va_alist)
88 va_dcl
89 #endif
90 {
91 	va_list args;
92 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
93 	va_start(args, mesg);
94 #else
95 	char *mesg;
96 
97 	va_start(args);
98 	mesg = va_arg(args, char *);
99 #endif
100 	err("", mesg, args);
101 	va_end(args);
102 }
103 
104 /* warning --- print a warning message */
105 
106 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
107 void
warning(char * mesg,...)108 warning(char *mesg, ...)
109 #else
110 /*VARARGS0*/
111 void
112 warning(va_alist)
113 va_dcl
114 #endif
115 {
116 	va_list args;
117 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
118 	va_start(args, mesg);
119 #else
120 	char *mesg;
121 
122 	va_start(args);
123 	mesg = va_arg(args, char *);
124 #endif
125 	err("warning: ", mesg, args);
126 	va_end(args);
127 }
128 
129 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
130 void
error(char * mesg,...)131 error(char *mesg, ...)
132 #else
133 /*VARARGS0*/
134 void
135 error(va_alist)
136 va_dcl
137 #endif
138 {
139 	va_list args;
140 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
141 	va_start(args, mesg);
142 #else
143 	char *mesg;
144 
145 	va_start(args);
146 	mesg = va_arg(args, char *);
147 #endif
148 	err("error: ", mesg, args);
149 	va_end(args);
150 }
151 
152 /* set_loc --- set location where a fatal error happened */
153 
154 void
set_loc(file,line)155 set_loc(file, line)
156 char *file;
157 int line;
158 {
159 	srcfile = file;
160 	srcline = line;
161 }
162 
163 /* fatal --- print an error message and die */
164 
165 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
166 void
r_fatal(char * mesg,...)167 r_fatal(char *mesg, ...)
168 #else
169 /*VARARGS0*/
170 void
171 r_fatal(va_alist)
172 va_dcl
173 #endif
174 {
175 	va_list args;
176 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
177 	va_start(args, mesg);
178 #else
179 	char *mesg;
180 
181 	va_start(args);
182 	mesg = va_arg(args, char *);
183 #endif
184 	err("fatal: ", mesg, args);
185 	va_end(args);
186 #ifdef DEBUG
187 	abort();
188 #endif
189 	exit(2);
190 }
191 
192