/*
 * MAIN.C
 *
 * Matthew Dillon, 24 Feb 1986
 * (c)1986 Matthew Dillon     9 October 1986
 *
 * Version 2.07M by Steve Drew 10-Sep-87
 *
 * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
 *
 */

char shellname[]="Shell V4.01A";
char shellctr []="CshCounter";

int aux; /* for use with aux: driver */
char *oldtitle;
char trueprompt[100];
char Inline[260];
struct IntuitionBase *IntuitionBase;
struct Window *w;
struct ArpBase *ArpBase;

main(argc, argv)
register char *argv[];
{
#if RAW_CONSOLE
	char *rawgets();
#endif

register unsigned int i;
extern int Enable_Abort;
char buf[10];
static char pipe1[32], pipe2[32];
struct Window *getwindow();

ArpBase=(struct ArpBase *)OpenLibrary("arp.library",34L);
if (ArpBase==NULL) { printf("No arp library\n"); exit(0); }

Forbid();
i=0;
if (Getenv(shellctr,buf,10L)) {
	i=(int)(long)Atol(buf);
	if (IoErr()) i=0;
	}
sprintf(buf,"%d",i+1);
Setenv(shellctr,buf);
Permit();

IntuitionBase=(struct IntuitionBase *)ArpBase->IntuiBase;

stdin->_flags	|= 0x80;	/* make sure we're set as a tty */
stdout->_flags	|= 0x80;	/* in case of redirection in .login */
Close(_devtab[2].fd);
_devtab[2].mode |= O_STDIO;
_devtab[2].fd = _devtab[1].fd;	/* set stderr to Output() otherwise */
				/* don't work with aux driver */
Myprocess = (struct Process *)FindTask(0L);
Mycli=(struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);
w=getwindow();

oldtitle=(char *)(w->Title);

Pipe1 = pipe1;
Pipe2 = pipe2;
sprintf(pipe1, "ram:pipe1_%ld", Myprocess);
sprintf(pipe2, "ram:pipe2_%ld", Myprocess);

sprintf(buf,"%ld",Myprocess->pr_TaskNum);
set_var(LEVEL_SET, "_clinumber", buf);

set_var(LEVEL_SET, v_titlebar, shellname);
set_var(LEVEL_SET, v_prompt,
	(IsInteractive(Input())) ? "\23337m%p> \2330m" : "");
set_var(LEVEL_SET, v_hist, "20");
set_var(LEVEL_SET, v_lasterr, "0");
set_var(LEVEL_SET, v_stat, "0");
set_var(LEVEL_SET, v_path, "RAM:,RAM:c/,df0:c/,df1:c/,sys:system/");
set_var(LEVEL_SET, "_insert", "1");
set_var(LEVEL_SET, "f1", "cdir df0:\15");
set_var(LEVEL_SET, "F1", "cdir df1:\15");
set_var(LEVEL_SET, "f3", "cdir RAM:\15");
set_var(LEVEL_SET, "F3", "cdir vd0:\15");
set_var(LEVEL_SET, "f4", "cd df0:\15");
set_var(LEVEL_SET, "F4", "cd df1:\15");
set_var(LEVEL_SET, "f5", "cls; ls\15");
set_var(LEVEL_SET, "F5", "cdir ");
set_var(LEVEL_SET, "f6", "lc\15");
set_var(LEVEL_SET, "f7", "info\15");
set_var(LEVEL_SET, "F7", "assign \15");
set_var(LEVEL_SET, "f8", "window -lf\15");
set_var(LEVEL_SET, "F8", "window -sb\15");
set_var(LEVEL_SET, "f10", "cls\15");
set_var(LEVEL_SET, "F10", "exit\15");
set_var(LEVEL_ALIAS, "cls", "echo -n ^l");
set_var(LEVEL_ALIAS, "lc", "ls -s");
set_var(LEVEL_ALIAS, "kr", "rm -r RAM:* >NIL:");
set_var(LEVEL_ALIAS, "cdir", "%q cd $q; cls; dir");
set_var(LEVEL_ALIAS, "exit", "endcli;quit");
set_var(LEVEL_ALIAS, "lp", "cat >PRT:");
seterr();
if (Myprocess->pr_CurrentDir == NULL)
	do_cd("x :");
else do_pwd(NULL);
Enable_Abort = 0;

if (exists(av[1] = "S:.login")) do_source("x S:.login");

for (i = 1; i < argc; ++i) {
	if (!strcmp(argv[i],"-c")) {
		Inline[0] = ' ';
		Inline[1] = '\0';
		while (++i < argc)
			{ strcat(Inline,argv[i]); strcat(Inline," "); }
		exec_command(Inline);
		main_exit(Lastresult);
		}
	if (!strcmp(argv[i],"-a")) { aux = 1; continue; }
	sprintf (Inline, "source %s",argv[i]);
	av[1] = argv[i];
	do_source (Inline);
	}
for (;;) {
   if (breakcheck())
	while (WaitForChar(Input(), 100L) || stdin->_bp < stdin->_bend)
		gets(Inline);
   clearerr(stdin);  /* prevent acidental quit */
#if RAW_CONSOLE
   if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
#else
   printf("%s", disable ? "_ " : trueprompt);
   fflush(stdout);
   if (Quit || !gets(Inline)) main_exit(0);
#endif
   breakreset();
   if (*Inline) exec_command(Inline);
   }
}

main_exit(n)
{
register unsigned short i;
char buf[10];

Getenv(shellctr,buf,10L);
i=(int)Atol(buf);
sprintf(buf,"%d",i-1);
Setenv(shellctr,buf);
SetWindowTitles(w,oldtitle,-1L);
for (i=1; i<MAXMYFILES; i++) myclose((int)i);
ArpExit(0L,0L);			/* Intuition need not to be closed */
}

breakcheck()
{
return (int)(SetSignal(0L,0L) & SIGBREAKF_CTRL_C);
}

breakreset()
{
SetSignal(0L, SIGBREAKF_CTRL_C);
}

dobreak()
{
if (breakcheck()) { printf("^C\n"); return(1); }
return(0);
}

/* 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);
}

_wb_parse()
{
}

do_howmany()
{
char buf[10];

Getenv(shellctr, buf, 10L);
printf("Shell(s) running: %s\n",buf);
}

struct Window *getwindow()
{
struct InfoData *infodata;
struct Window *win;
long args[8];

infodata=AllocMem((long)sizeof(struct InfoData),MEMF_CLEAR | MEMF_PUBLIC);
args[0]=(long)infodata >> 2;
SendPacket(ACTION_DISK_INFO,args,Myprocess->pr_ConsoleTask);
win=(struct Window *)infodata->id_VolumeNode;
FreeMem(infodata,(long)sizeof(struct InfoData));
return win;
}
