home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / msg.c < prev    next >
C/C++ Source or Header  |  1997-05-01  |  4KB  |  190 lines

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