/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *  Portions Copyright (C) 1994 Rafael W. Luebbert
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id: ix_startup.c,v 1.5 1994/06/19 15:13:22 rluebbert Exp $
 *
 *  $Log: ix_startup.c,v $
 *  Revision 1.5  1994/06/19  15:13:22  rluebbert
 *  *** empty log message ***
 *
 *  Revision 1.3  1992/08/09  20:55:51  amiga
 *  import sysbase
 *
 *  Revision 1.2  1992/07/04  19:18:21  mwild
 *  remove SIGWINCH handler before returning
 *
 * Revision 1.1  1992/05/14  19:55:40  mwild
 * Initial revision
 *
 */

#define KERNEL
#include "ixemul.h"
#include "kprintf.h"

#define exit_buf u.u_jmp_buf
extern struct ExecBase *SysBase;
/* this just jumps right back into ix_startup() */


/*
 * Note: I kept the partition into startup and _main(), although in this
 *       case, both functions could be done in one function, since this is
 *       a library, and the user can't override _main anyway but globally...
 */

int
ix_startup (char *aline, int alen,
	    int expand, char *wb_default_window, u_int main, int *real_errno)
{
  struct Process	*me		= (struct Process *) SysBase->ThisTask;
  int			exit_val;
  struct WBStartup	*wb_msg;
  int			fd;
  /*
   * The following code to reset the fpu might not be necessary, BUT since
   * a CLI shell doesn't spawn a new process when executing a command - it 
   * insteads calls the command like a subroutine - it depends on the Shell
   * whether the fpu is setup correctly. And I don't like to depend on any
   * thing ;-)
   */

if(gotanfpu())
	resetfpu();
  /* first deal with WB messages, since those HAVE to be answered properly,
   * even if we should fail later (memory, whatever..) */

  if (! me->pr_CLI)
    {
      /* we have been started by Workbench. Get the StartupMsg */
      WaitPort (& me->pr_MsgPort);
      wb_msg = (struct WBStartup *) GetMsg (& me->pr_MsgPort);
      /* further processing in _main () */
    }
  else
    {
      /* for usage by sys_exit() for example */
      KPRINTF (("CLI command line '%s'\n", aline));
      u.u_argline = aline;
      u.u_arglinelen = alen;
    }


  u.u_expand_cmd_line = expand;
  if (real_errno) u.u_errno = real_errno;
  KPRINTF (("&errno = %lx\n", real_errno));


  exit_val = setjmp (exit_buf);
  if (! exit_val)
    {
      /* from now on it's safe to allow signals */
      syscall (SYS_sigsetmask, 0);
      /* the first time thru call the program */
      KPRINTF (("calling __main()\n"));
      exit_val = me->pr_CLI ? syscall (SYS__main, aline, alen, main)
			    : syscall (SYS__main, wb_msg, wb_default_window, main);
      KPRINTF (("exit_val = %ld\n", exit_val));
    }
  else
    /* in this case we came from a longjmp-call */
	{
    exit_val --;
	}
  ix_remove_sigwinch ();

  /* had to move the closing of files out of ix_close(), as close() may 
     actually wait for the last packet to arrive, and inside ix_close() we're
     inside Forbid, and may thus never wait! */

  /* close all files */
  for (fd = 0; fd < NOFILE; fd++) 
    if (u.u_ofile[fd]) syscall (SYS_close, fd);
  /* if at all possible, free memory before entering Forbid ! (Semaphore
     problems..) */
  all_free ();
  /* if started from workbench, Forbid(), since on reply WB will deallocate
   * our task... */
  if (! me->pr_CLI)
    {
      Forbid ();
      ReplyMsg ((struct Message *) wb_msg);
    }

  return exit_val;
}

void
_exit (int retcode)
{
	longjmp (exit_buf, retcode + 1);
}
