/* prterror.c */
/*
The contents of this file are hereby released to the public domain.

                                 -- Rahul Dhesi 1986/11/14

*/
#include "options.h"
#include <stdio.h>      /* to make various.h includable */
#include "various.h"
/* General error handler.  Input format:

   parameter 1:  'w', 'e', or 'f'.

      'm':  message
      'M':  message without preceding identification
      'w':  WARNING
      'e':  ERROR
      'f':  FATAL
      'F':  FATAL but program doesn't exist immediately

   All text printed is preceded by "Zoo:  " or "Ooz:  "  depending
   upon conditional compilation, except in the case of 'M' messages
   which are printed without any text being added.

   For messages, the text supplied is printed if and only if the global
   variable "quiet" is zero.  Control then returns to the caller.

   For warnings, errors, and fatal errors, the variable "quiet" is ignored.

   For warnings and errors, the error message is preceded by the "WARNING:"
   or "ERROR".  The error message is printed and control returns to the
   caller.

   For fatal errors, the error message is preceded by "FATAL:" and an
   error message is printed.  If the option was 'f', the program exits with 
   a status of 1.  If the option was 'F', control returns to the caller and 
   it is assumed that the caller will do any cleaning up necessary and then 
   exit with an error status.

   parameter 2:  The format control string for printf.   
   parameters 3 and 4 are passed along to printf.

   Note:  printf() is always supplied parameters 3 and 4, even if they were
   not on the parameter list passed to prterror().  It is assumed that 
   printf() will only use as many as are specified by the format string.
   There is a small chance that on some machines, this will cause some
   kind of stack exception (e.g. if the hardware maintains tag bits in
   each word on the stack and doesn't allow certain types of data to
   be arbitrarily accessed).  This is just a theory so far.
*/

extern int quiet;

/* These declarations must be equivalent to those in errors.i */
char no_match[] = "No files matched.\n";
char failed_consistency[] = "Archive header failed consistency check.\n";
char invalid_header[] = "Invalid or corrupted archive.\n";
char internal_error[]="Internal error.\n";
char disk_full[]      = "I/O error or disk full.\n";
char bad_directory[]  = "Directory entry in archive is invalid.\n";
char no_memory[] = "Ran out of memory.\n";

#ifndef OOZ
char wrong_version[]=
   "Zoo %d.%d or later is needed to fully\nmanipulate this archive.\n";
char cant_process[] = 
   "The rest of the archive (%lu bytes) cannot be processed.\n";
char option_ignored[] = "Ignoring option %c.\n";
char inv_option[] = "Option %c is invalid.\n";
char bad_crc[] = "\007Bad CRC, %s probably corrupted\n";
#endif

#ifdef OOZ
char could_not_open[] = "Could not open ";
#else
char could_not_open[] = "Could not open %s.\n";
#endif

/*VARARGS2*/
prterror(level, format, a, b, c)
register int level;
char *format, *a, *b, *c;

{
   char string[120];       /* local format string */
   *string = '\0';         /* get a null string to begin with */

#ifdef OOZ
   strcpy (string, "Ooz:  ");
#else
   strcpy (string, "Zoo:  ");
#endif

   switch (level) {
      case 'M': *string = '\0';                    /* fall through to 'm' */
      case 'm': if (quiet) return; break;
      case 'w': strcat (string, "WARNING:  "); break;
      case 'e': strcat (string, "ERROR:  ");   break;
      case 'F':
      case 'f': strcat (string, "FATAL:  ");   break;
      default: prterror ('f', internal_error);  /* slick recursive call */
   }

   strcat (string, format);      /* just append supplied message */

#ifdef OOZ
   putstr (string);
   putstr (a);
   putstr (b);
#else
   printf (string, a, b, c);     /* and print the whole thing */
#endif

   if (level == 'f')       /* and abort on fatal error 'f' but not 'F' */
      exit (1);
}

