Main Page | Class List | Directories | File List | Class Members | File Members

msg.c

Go to the documentation of this file.
00001 /*
00002  * msg.c - routines for error messages
00003  */
00004 
00005 /* 
00006  * Copyright (C) 1986, 1988, 1989, 1991-2001, 2003 the Free Software Foundation, Inc.
00007  * 
00008  * This file is part of GAWK, the GNU implementation of the
00009  * AWK Programming Language.
00010  * 
00011  * GAWK is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015  * 
00016  * GAWK is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  * 
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
00024  */
00025 
00026 #include "awk.h"
00027 
00028 int sourceline = 0;
00029 char *source = NULL;
00030 
00031 static const char *srcfile = NULL;
00032 static int srcline;
00033 
00034 /* err --- print an error message with source line and file and record */
00035 
00036 /* VARARGS2 */
00037 void
00038 err(const char *s, const char *emsg, va_list argp)
00039 {
00040         char *file;
00041 
00042         (void) fflush(stdout);
00043         (void) fprintf(stderr, "%s: ", myname);
00044 #ifdef GAWKDEBUG
00045         if (srcfile != NULL) {
00046                 fprintf(stderr, "%s:%d:", srcfile, srcline);
00047                 srcfile = NULL;
00048         }
00049 #endif /* GAWKDEBUG */
00050         if (sourceline != 0) {
00051                 if (source != NULL)
00052                         (void) fprintf(stderr, "%s:", source);
00053                 else
00054                         (void) fprintf(stderr, _("cmd. line:"));
00055 
00056                 (void) fprintf(stderr, "%d: ", sourceline);
00057         }
00058         if (FNR > 0) {
00059                 file = FILENAME_node->var_value->stptr;
00060                 (void) putc('(', stderr);
00061                 if (file)
00062                         (void) fprintf(stderr, "FILENAME=%s ", file);
00063                 (void) fprintf(stderr, "FNR=%ld) ", FNR);
00064         }
00065         (void) fprintf(stderr, "%s", s);
00066         vfprintf(stderr, emsg, argp);
00067         (void) fprintf(stderr, "\n");
00068         (void) fflush(stderr);
00069 }
00070 
00071 /* msg --- take a varargs error message and print it */
00072 
00073 /*
00074  * Function identifier purposely indented to avoid mangling
00075  * by ansi2knr.  Sigh.
00076  */
00077 
00078 void
00079 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00080   msg(const char *mesg, ...)
00081 #else
00082 /*VARARGS0*/
00083   msg(va_alist)
00084   va_dcl
00085 #endif
00086 {
00087         va_list args;
00088 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00089         va_start(args, mesg);
00090 #else
00091         char *mesg;
00092 
00093         va_start(args);
00094         mesg = va_arg(args, char *);
00095 #endif
00096         err("", mesg, args);
00097         va_end(args);
00098 }
00099 
00100 /* warning --- print a warning message */
00101 
00102 void
00103 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00104   warning(const char *mesg, ...)
00105 #else
00106 /*VARARGS0*/
00107   warning(va_alist)
00108   va_dcl
00109 #endif
00110 {
00111         va_list args;
00112 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00113         va_start(args, mesg);
00114 #else
00115         char *mesg;
00116 
00117         va_start(args);
00118         mesg = va_arg(args, char *);
00119 #endif
00120         err(_("warning: "), mesg, args);
00121         va_end(args);
00122 }
00123 
00124 void
00125 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00126   error(const char *mesg, ...)
00127 #else
00128 /*VARARGS0*/
00129   error(va_alist)
00130   va_dcl
00131 #endif
00132 {
00133         va_list args;
00134 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00135         va_start(args, mesg);
00136 #else
00137         char *mesg;
00138 
00139         va_start(args);
00140         mesg = va_arg(args, char *);
00141 #endif
00142         err(_("error: "), mesg, args);
00143         va_end(args);
00144 }
00145 
00146 /* set_loc --- set location where a fatal error happened */
00147 
00148 void
00149 set_loc(const char *file, int line)
00150 {
00151         srcfile = file;
00152         srcline = line;
00153 
00154         /* This stupid line keeps some compilers happy: */
00155         file = srcfile; line = srcline;
00156 }
00157 
00158 /* fatal --- print an error message and die */
00159 
00160 void
00161 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00162   r_fatal(const char *mesg, ...)
00163 #else
00164 /*VARARGS0*/
00165   r_fatal(va_alist)
00166   va_dcl
00167 #endif
00168 {
00169         va_list args;
00170 #if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
00171         va_start(args, mesg);
00172 #else
00173         char *mesg;
00174 
00175         va_start(args);
00176         mesg = va_arg(args, char *);
00177 #endif
00178         err(_("fatal: "), mesg, args);
00179         va_end(args);
00180 #ifdef GAWKDEBUG
00181         abort();
00182 #endif
00183         exit(2);
00184 }

© sourcejam.com 2005-2008