/*------------------------------------*
 | File: CLEANUP.c - MLO 900131 V1.00 |
 | These routines perform cleanup for |
 | program termination.               |
 *------------------------------------*/

#include "rpn.h"
#include "proto.h"

extern struct Window *Wrpn;
extern struct Window *Wreg;

static void cleanMess(struct Window *pW);

void cleanup(
  int code                      /* Completion code */
)
{/*--------------------------------------*
  | Frees all Intuition resources and    |
  | exits with the given completion code |
  *--------------------------------------*/

  if (Wreg != NULL) {
    CloseWindow(Wreg);
  }

  if (Wrpn != NULL) {
    ClearMenuStrip(Wrpn);
    cleanMess(Wrpn);
    CloseWindow(Wrpn);
  }
  deallMem();
  exit(code);
}

static void cleanMess(
  struct Window *pW
)
{/*------------------------------------------*
  | Local function. Replies to all Intuition |
  | messages pending for the given window.   |
  *------------------------------------------*/

  struct IntuiMessage *pIM;

  while ( (pIM = (struct IntuiMessage *) GetMsg(pW->UserPort)) != NULL) {
    ReplyMsg(pIM);
  }
}
