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