 
/*
 * MAIN.C
 *
 * Matthew Dillon, 24 Feb 1986
 * version 2.01A (Manx ver) by Steve Drew 27 Sep 1986 
 * mods for 16 bit compiling 4 Oct 1986. Steve Drew.
 *
 * el main routine.
 */
 
#include "shell.h"

#define STDBUF 256

extern char *btocstr();
extern char Dir_name[];
extern struct FileLock *Clock; 

char Inline[256];
char stdin_buff[STDBUF];
char stdout_buff[STDBUF]; 
main(argc, argv)
register char *argv[];
{
   char *prompt;
   register int i;
   extern int Enable_Abort;
   init_vars();
   init();
   getcd();
   
   Enable_Abort = 0;
     
   for (i = 1; i < argc; ++i) {
      strcpy (Inline, "source ");
      strcat (Inline, argv[i]);
      do_source (Inline, 0);
   }
   for (;;) {
      if ((prompt = get_var (LEVEL_SET, V_PROMPT)) == NULL)
         prompt = "echo -n \"$ \"";
      if (breakcheck()) {
         while (WaitForChar(Input(), 1L))
            gets(Inline);
      }
      ++H_stack;
      exec_command (prompt);
      --H_stack;
      if (Quit || !gets(Inline))
         main_exit(0);
      breakreset();
      if (*Inline)
         exec_command(Inline);
      fflush(stdout);
   }
}
 
init_vars()
{
   if (IsInteractive(Input()))
      set_var (LEVEL_SET, V_PROMPT, "echo -n \"$ \"");
   else
      set_var (LEVEL_SET, V_PROMPT, "");
   set_var (LEVEL_SET, V_HIST,   "20");
   set_var (LEVEL_SET, V_LASTERROR, "0");
}
 
init()
{
   static char pipe1[32], pipe2[32];


   Myprocess = (struct Process *)FindTask(0L); 
   stdin->_buff = stdin_buff;
   stdin->_buflen = STDBUF;
   stdout->_buff = stdout_buff;
   stdout->_buflen = STDBUF; 

   Uniq  = (long)Myprocess;
   Pipe1 = pipe1;
   Pipe2 = pipe2;
   sprintf (pipe1, "ram:pipe1_%ld", Uniq);
   sprintf (pipe2, "ram:pipe2_%ld", Uniq);
}
 
 
main_exit(n)
{
   exit (n);
}
 
breakcheck()
{
   if (SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
      return (1);
   else
      return (0);
}
 
breakreset()
{
   SetSignal(0L, SIGBREAKF_CTRL_C);
}

   /* set the current lock and extract the cd'd string  */
   
getcd()
{

   Clock = (struct FileLock *)( (ULONG)Myprocess->pr_CurrentDir);
   if (!Clock) exec_command("cd DF0:"); /* must be boot device */
   btocstr(
        ((struct CommandLineInterface *)BADDR(Myprocess->pr_CLI))->cli_SetName,
   	Dir_name);
}


/* this routine causes manx to use this Chk_Abort() rather than it's own */
/* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
/* is zero).  Since we want to check for our own ^C's 			 */

Chk_Abort()
{
return(0);
}


