cpperror.c 3.58 KB
Newer Older
Per Bothner committed
1
/* Default error handlers for CPP Library.
Kaveh R. Ghazi committed
2
   Copyright (C) 1986, 87, 89, 92 - 95, 1998 Free Software Foundation, Inc.
Per Bothner committed
3
   Written by Per Bothner, 1994.
Jeff Law committed
4
   Based on CCCP program by Paul Rubin, June 1986
Per Bothner committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18
   Adapted to ANSI C, Richard Stallman, Jan 1987

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Richard Kenner committed
19
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Per Bothner committed
20 21 22 23 24

 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */

25 26
#ifndef EMACS
#include "config.h"
27 28 29
#include "system.h"
#else
#include <stdio.h>
30 31
#endif /* not EMACS */

32 33
#include "cpplib.h"

Per Bothner committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* Print the file names and line numbers of the #include
   commands which led to the current file.  */

void
cpp_print_containing_files (pfile)
     cpp_reader *pfile;
{
  cpp_buffer *ip;
  int first = 1;

  /* If stack of files hasn't changed since we last printed
     this info, don't repeat it.  */
  if (pfile->input_stack_listing_current)
    return;

  ip = cpp_file_buffer (pfile);

  /* Give up if we don't find a source file.  */
  if (ip == NULL)
    return;

  /* Find the other, outer source files.  */
  while ((ip = CPP_PREV_BUFFER (ip)), ip != CPP_NULL_BUFFER (pfile))
    {
      long line, col;
      cpp_buf_line_and_col (ip, &line, &col);
      if (ip->fname != NULL)
	{
	  if (first)
	    {
	      first = 0;
	      fprintf (stderr, "In file included");
	    }
	  else
	    fprintf (stderr, ",\n                ");
	}

Kaveh R. Ghazi committed
71
      fprintf (stderr, " from %s:%ld", ip->nominal_fname, line);
Per Bothner committed
72 73 74 75 76 77 78 79 80
    }
  if (! first)
    fprintf (stderr, ":\n");

  /* Record we have printed the status as of this time.  */
  pfile->input_stack_listing_current = 1;
}

void
81
cpp_file_line_for_message (pfile, filename, line, column)
Per Bothner committed
82
     cpp_reader *pfile;
83 84
     char *filename;
     int line, column;
Per Bothner committed
85
{
86 87
  if (column > 0)
    fprintf (stderr, "%s:%d:%d: ", filename, line, column);
Per Bothner committed
88
  else
89
    fprintf (stderr, "%s:%d: ", filename, line);
Per Bothner committed
90 91
}

92
/* IS_ERROR is 2 for "fatal" error, 1 for error, 0 for warning */
Mike Stump committed
93 94 95

void
cpp_message (pfile, is_error, msg, arg1, arg2, arg3)
96
     int is_error;
Per Bothner committed
97 98 99 100
     cpp_reader *pfile;
     char *msg;
     char *arg1, *arg2, *arg3;
{
101
  if (!is_error)
Per Bothner committed
102
    fprintf (stderr, "warning: ");
103 104 105 106
  else if (is_error == 2)
    pfile->errors = CPP_FATAL_LIMIT;
  else if (pfile->errors < CPP_FATAL_LIMIT)
    pfile->errors++;
Per Bothner committed
107 108 109 110
  fprintf (stderr, msg, arg1, arg2, arg3);
  fprintf (stderr, "\n");
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124
/* Same as cpp_error, except we consider the error to be "fatal",
   such as inconsistent options.  I.e. there is little point in continuing.
   (We do not exit, to support use of cpplib as a library.
   Instead, it is the caller's responsibility to check
   CPP_FATAL_ERRORS.  */

void
cpp_fatal (pfile, str, arg)
     cpp_reader *pfile;
     char *str, *arg;
{
  fprintf (stderr, "%s: ", progname);
  cpp_message (pfile, 2, str, arg);
}
Per Bothner committed
125 126 127 128 129 130 131 132 133 134

void
cpp_pfatal_with_name (pfile, name)
     cpp_reader *pfile;
     char *name;
{
  cpp_perror_with_name (pfile, name);
#ifdef VMS
  exit (vaxc$errno);
#else
135
  exit (FATAL_EXIT_CODE);
Per Bothner committed
136 137
#endif
}