Introduced -devel and -extras subpackages for 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
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 int 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(const char *s, const char *emsg, va_list argp)
43 {
44         char *file;
45         const char *me;
46
47         (void) fflush(output_fp);
48         me = myname;
49         if (strncmp(me, "dgawk", 5) == 0)
50                 me = &myname[1];
51         (void) fprintf(stderr, "%s: ", me);
52 #ifdef GAWKDEBUG
53         if (srcfile != NULL) {
54                 fprintf(stderr, "%s:%d:", srcfile, srcline);
55                 srcfile = NULL;
56         }
57 #endif /* GAWKDEBUG */
58
59         if (sourceline > 0) {
60                 if (source != NULL)
61                         (void) fprintf(stderr, "%s:", source);
62                 else
63                         (void) fprintf(stderr, _("cmd. line:"));
64
65                 (void) fprintf(stderr, "%d: ", sourceline);
66         }
67         if (FNR > 0) {
68                 file = FILENAME_node->var_value->stptr;
69                 (void) putc('(', stderr);
70                 if (file)
71                         (void) fprintf(stderr, "FILENAME=%s ", file);
72                 (void) fprintf(stderr, "FNR=%ld) ", FNR);
73         }
74         (void) fprintf(stderr, "%s", s);
75         vfprintf(stderr, emsg, argp);
76         (void) fprintf(stderr, "\n");
77         (void) fflush(stderr);
78 }
79
80 /* msg --- take a varargs error message and print it */
81
82 void
83 msg(const char *mesg, ...)
84 {
85         va_list args;
86         va_start(args, mesg);
87         err("", mesg, args);
88         va_end(args);
89 }
90
91 /* warning --- print a warning message */
92
93 void
94 warning(const char *mesg, ...)
95 {
96         va_list args;
97         va_start(args, mesg);
98         err(_("warning: "), mesg, args);
99         va_end(args);
100 }
101
102 void
103 error(const char *mesg, ...)
104 {
105         va_list args;
106         va_start(args, mesg);
107         err(_("error: "), mesg, args);
108         va_end(args);
109 }
110
111 /* set_loc --- set location where a fatal error happened */
112
113 void
114 set_loc(const char *file, int line)
115 {
116         srcfile = file;
117         srcline = line;
118
119         /* This stupid line keeps some compilers happy: */
120         file = srcfile; line = srcline;
121 }
122
123 /* r_fatal --- print a fatal error message */
124
125 void
126 r_fatal(const char *mesg, ...)
127 {
128         va_list args;
129         va_start(args, mesg);
130         err(_("fatal: "), mesg, args);
131         va_end(args);
132 #ifdef GAWKDEBUG
133         abort();
134 #endif
135         gawk_exit(EXIT_FATAL);
136 }
137
138 /* gawk_exit --- longjmp out if necessary */
139
140 void
141 gawk_exit(int status)
142 {
143         if (fatal_tag_valid) {
144                 exit_val = status;
145                 longjmp(fatal_tag, 1);
146         }
147         exit(status);
148 }