/* This file is part of the origami distrubution. (Amiga Port)
 *
 * Source code written by Thomas Hadig in September 1991
 * Version : 1.6.42 alpha, June 1992
 * (C) 1992 by Thomas Hadig
 * may be distributed unchanged ! (see copyright notes)
 */

/*{{{  #includes*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <libraries/dos.h>
#include <fcntl.h>

#ifdef LATTICE
#include <dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#else
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#endif

#define SHELL_C
#define I_PROMPT_C
#define I_GETTK_C

#undef TRUE
#undef FALSE
#include <local/bool.h>
#include "origami.h"
#include "origami:amiga/h/arexx.h"
/*}}}  */
/*{{{  variables*/
extern TOKEN buffer_keys[];
extern int key_buffer_max;
/*{{{  Arexx variables*/
#ifdef AREXX
#include <exec/ports.h>

extern struct MsgPort *arexxtaskport,*arexxport;
extern struct My_ArexxMessage arexxmsg;
#endif
/*}}}  */
/*}}}  */

/*{{{  subshell*/
int subshell (char *cmd)

 {
  BPTR f;
  int ret;
  char text[]="\n[Press any key to continue !]";
  char *shell;

  if (cmd)
   /*{{{  execute shell command or arexx command*/
   {
#    ifdef AREXX
    /*{{{  handle arexx-port*/
    if (!(strncmp (cmd,"AREXX:",6)))
     {
      if (arexxtaskport)
       {
        if (arexxmsg.ExecMessage.mn_Node.ln_Type == NT_REPLYMSG)
         {
          arexxmsg.ExecMessage.mn_Node.ln_Type = NT_MESSAGE;
          arexxmsg.ExecMessage.mn_ReplyPort = arexxport;
          if ((arexxmsg.ExecMessage.mn_Length = strlen(cmd)-6)<255)
           {
            arexxmsg.class = AREXX_OUT;
            strcpy (arexxmsg.my_message,cmd+6);
            PutMsg (arexxtaskport,(struct Message *)&arexxmsg);
           }
          else message ("ARexx-Command not long !",TRUE);
         }
        else message ("Didn't get reply message from ARexx !",TRUE);
       }
      else message ("No Arexx-Port loaded !",TRUE);
      return 0;
     }
    /*}}}  */
#    endif
    /*{{{  handle shell command*/
    /*{{{  open window*/
    if (!(f=Open ("RAW:0/0/639/199/Origami Shell Command",MODE_OLDFILE)))
      return (system (cmd));
    /*}}}  */
    ret = Execute (cmd,0,f);
    /*{{{  get key and close window*/
    Write (f,text,strlen(text));
    if (!executing_macro) while (Read (f,text,1)!=1);
    Close(f);
    /*}}}  */
    if ((key_buffer_max==0)&&(!executing_macro))
     {
      buffer_keys[0]=O_RETURN;
      key_buffer_max=1;
     }
    return /*ret*/;
    /*}}}  */
   }
   /*}}}  */
  /*{{{  open new shell*/
  if (shell=getenv("SHELL"))
   {
    message ("Sub shell created",TRUE);
    system (shell);
    return;
   }
  message ("Enviroment variable shell not set !",TRUE);
  return /*1*/;
  /*}}}  */
 }
/*}}}  */

/*{{{  amiga lseek patch*/
long my_lseek (int fh, long rpos, int mode)

 {
  long apos;

  if ((apos = lseek (fh,rpos,mode))==-1)
   {
    long end;
    char buffer=0;

    if (mode!=0) return -1;  /* patch only for seek from beginning */
    if ((end = lseek (fh,0,2))==-1) return -1L;
    while (end<=rpos)
     {
      write (fh,&buffer,1);
      end++;
     }
    return lseek (fh,rpos,mode);
   }
  return apos;
 }
/*}}}  */
