/*static char *SCCSID = "@(#)error.c	6.1 90/11/18";*/
/*static char *SCCSID = "@(#)error.c 13.4 90/09/07";*/
/***	ERROR.C - Routines to handle error conditions
 *
 *	CHKDSK
 *	Gregory A. Jones
 *	Copyright 1988 Microsoft Corporation
 *
 *	Modification history:
 *	G.A. Jones	05/25/88	Original for Pinball 1.0.
 */

#include <setjmp.h>
#include "ldefs.h"
#include "ltypes.h"
#include "globals.h"
#include <pr_chk.h>

extern jmp_buf	chk_error;


/***	chk_exit_error - print informative message and exit
 *
 *	exit_error accepts an error code, prints an appropriate
 *	message, cleans up any allocated  resources, and ends
 *	execution of the program.  Note that this routine never returns.
 *
 *	Since the OS/2 front end programs expect the IFS's dynamic link
 *	routine to return with an errorlevel value (rather than just
 *	terminating the program themselves), chk_exit_error uses the
 *	C library's "longjmp" routine to jump back to an error-handling
 *	clause in the top-level routine.
 *
 *	chk_exit_error (code)
 *
 *	ENTRY		code - error code number, defined in DEFS.H
 *
 *	CALLS		exit
 *			error_msg
 *			close_disk
 *			chk_free_all_memory
 *			longjmp
 *
 *	EFFECTS         Deallocates memory
 *			Unlocks and closes disk
 *			Returns to error-handling clause in chkdsk()
 *
 *	WARNINGS	Does not return
 */
void chk_exit_error (code)
  USHORT code;
{
  UCHAR *p;

#ifdef TRACE
  fprintf (stderr, "chk_exit_error (%d)\n", code);
  fflush (stderr);
#endif

  if (code == OPEN_ERROR) {
    p = disk_name;
    CHK_COMPUTMSG( SYSTEM_MSG, &p, 1, CHK_OPEN_ERROR_STR, msgfilename, STDERR_CODE, NO_RESPONSE,
	       NULL, 0);
  }
  else if (code <= MAX_ERROR_CODE) {
    if (chk_error_messages [code])
      CHK_ERROR_MSG( chk_error_messages [code]);  /* display appropriate message */
  }
  else {
    itoa (code, outbuf, 10);
    p = outbuf;
    CHK_COMPUTMSG( SYSTEM_MSG, &p, 1, CHK_UNKNOWN_ERROR_STR, msgfilename, STDERR_CODE,
	       NO_RESPONSE, NULL, 0);
  }

  close_disk ();			/* unlock disk, close our handle */

  chk_free_all_memory ();		/* free disk map, bitmaps, etc. */

/*
 * Jump back to chkdsk().
 */

  longjmp (chk_error, 0);
}
