
/*
 * RUN.C
 *
 *  (C)CopyRight 1987 Matthew Dillon, All rights reserved.
 *
 *  RUN handles running of external commands.
 *
 */

#include "shell.h"
#include "libraries/dos.h"

extern long *SysRegs;
extern struct FileLock *Clock;

do_run(str)
char *str;
{
    int i;

#ifdef DEBUG
    if (SDebug) {
	printf ("RUN: %s '>%s' '<%s' '>>%ld'\n", av[0], (Cin_name)?Cin_name:"&", (Cout_name)?Cout_name:"&", Cout_append);
	printf ("av[0]=%08lx [1]=%08lx [2]=%08lx\n", av[0],av[1], av[2]);
    }
#endif
    if (fexecv(av[0], av, Cin_name, Cout_name, Cout_append) >= 0) {
	i = wait();
    } else {
	register long lock;
	register char *copy;
	char buf[128];

	mountrequest(0);
	lock = FindIt(av[0], ".sh", buf);
	mountrequest(1);
#ifdef DEBUG
	if (SDebug)
	    puts("do_run: 1");
#endif
	if (lock == NULL) {
	    perror(av[0]);      /*  fixed 26 Mar 88 */
	    return (-1);
	}
	av[0] = (char *)-1;
	av[1] = (char *)lock;
	copy = malloc(strlen(str)+3);
	strcpy(copy+2,str);
	copy[0] = 'x';
	copy[1] = ' ';
#ifdef DEBUG
	if (SDebug)
	    printf ("Do_source: %s\n", copy);
#endif
	i = do_source(copy);
	free(copy);
    }
    if (Clock != (struct FileLock *)((struct Process *)FindTask(NULL))->pr_CurrentDir) {
	Clock = (struct FileLock *)((struct Process *)FindTask(NULL))->pr_CurrentDir;
	puts("Warning: Current Directory has changed");
    }
    return (i);
}



