/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *
 *  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.
 */

#define KERNEL
#include "ixemul.h"

#include <ctype.h>

#define NO_PROTOTYPES
#include <libraries/arpbase.h>  /* HAS! to be V39 or higher! */
#undef NO_PROTOTYPES

#define BASE_EXT_DECL
#define BASE_PAR_DECL	
#define BASE_PAR_DECL0	
#define BASE_NAME	ix.ix_arp_base
#include <inline/arp.h>

/* 2.0 support */
#include "gcc:include20/utility/tagitem.h"
#include "gcc:include20/dos/dostags.h"
#define BASE_EXT_DECL
#define BASE_PAR_DECL	
#define BASE_PAR_DECL0	
#define BASE_NAME	ix.ix_dos_base
__inline static LONG SystemTagList(BASE_PAR_DECL UBYTE* command, struct TagItem* tags)
{
	BASE_EXT_DECL
	register LONG res __asm("d0");
	register void *a6 __asm ("a6");
	register UBYTE* d1 __asm("d1");
	register struct TagItem* d2 __asm("d2");

	a6 = BASE_NAME;
	d1 = command;
	d2 = tags;
	__asm volatile ("
	jsr a6@(-0x25e)"
	: "=r" (res)
	: "r" (a6), "r" (d1), "r" (d2)
	: "d0", "d1", "a0", "a1", "d2");
	return res;
}


extern int _dos20;
#define alloca __builtin_alloca

int
system (const char *cmd)
{
  int rc, err = 0;
  int stack_size;
  struct CommandLineInterface *CLI;
  struct Process *me;
  int omask;

  /* I retry with our new signal mechanism ;-) */
  omask = syscall (SYS_sigsetmask, ~0);

  me = (struct Process *)FindTask(0);
  CLI = BTOCPTR (me->pr_CLI);
  stack_size = CLI ? CLI->cli_DefaultStack * 4 : me->pr_StackSize;
  if (stack_size <= 4096) stack_size = 250000;

  /* limited support to allow starting of files in the current directory
   * with `./foo'. The better approach would use the __plock() trick to
   * parse the command, LoadSeg it. But then this approach would have to
   * do the whole redirection stuff on its own.. */
  while (isspace (*cmd)) cmd++;
  while (cmd[0] == '.' && cmd[1] == '/') cmd += 2;

  /* before OS2.0, use ARP functions */
  if (! _dos20)
    {
      struct NewShell *nsh; /* only available starting with Arp1.3 !! */

      nsh = alloca (sizeof (*nsh));
      bzero (nsh, sizeof (*nsh));
      
      /* use same stacksize as parent process */
      nsh->nsh_StackSize = stack_size;
      nsh->nsh_Control = EXECUTE_ME;  /* BACKGROUND | EXECUTE */
        
      /* rc should be 0 if the generated pid was >= 0 */
      rc = ASyncRun ((UBYTE *)cmd, 0L, nsh) < 0;
      err = __ioerr_to_errno (IoErr ());
    }
  else
    {
      struct TagItem tags [] = {
	/* a stack of 4K is generally ways too small.. */
	{ NP_StackSize, stack_size, },
	{ TAG_END, 0, }
      };

      rc = SystemTagList ((UBYTE *)cmd, tags);
      err = __ioerr_to_errno (IoErr ());
    }

  syscall (SYS_sigsetmask, omask);

  if (rc > 128)
    errno = EINTR;
  else 
    errno = err;

  return rc;
}
