/* 
        This module is the workhorse of Flist.
        It is here that the commands typed in by the user are:
  
        a: Scanned for special commands
        b: replaced for filename expansion
        c: prepped for ASyncRun
        d: Run as a Task.
  
*/

#include <stdio.h>
#include "flist.h"

/* Some external variables here */

extern struct Window *winptr;
extern int errno;
extern struct FileLock *lock;
extern long mousey;

#define PCB_SETS PRF_SAVEIO|PRF_INTERACTIVE

long DoDOS(str)
char *str;
{
    long ret;

    if (str[0] != 0) {
            ret = spawnproc(str);
            if (!ret)
                    DisplayBeep(NULL);
    }
    return ret;
}

long spawnproc(str)
char *str;
{
        long i, j, ret, ret1, pnt;
        char command[90], args[90], buf[1000], path[1000];
        struct FileHandle *fh;

    /*  In case your wondering, I'm trying to put the window under the 
        mouse. Why? Because I want to make it easy to kill it later! */

        i = (mousey > 119) ? 120: mousey;

        sprintf(buf,"CON:0/%d/640/80/Flist at Work", i);
        fh = Open(buf,MODE_NEWFILE);
        if (fh == NULL) {
                auto_req(" I can't seem to Open CON:");
                return FALSE;
        }

/*  Here be the LARGEST Kluge I have ever run across in AmigaDOS...
    Execute() must be fed the command 'cd current.directory' as part
    of the string, if you expect to be executing out of the current
    directory that is! */

        PathName(lock, path);
        strcpy(buf, "cd \"");
        strcat(buf, path);
        pnt = strlen(buf);
        strcat(buf, "\"\n");
        strcat(buf, str); 

#ifdef DEBUG
        sprintf(path,"Sending -%s- to Execute.",buf);
        SetWindowTitles(winptr,-1L,path);
#endif

        ret = Execute(buf,NULL,fh);
        
        if (!ret){
#ifdef DEBUG 
                sprintf(&buf[0],"*** ERROR *** Execute returns - %ld", IoErr());
                SetWindowTitles(winptr,-1L,buf);
#endif
                DisplayBeep(NULL);
                Close(fh);
                return FALSE;
        } else {

        /* Wait for the user to terminate the window */

                buf[0] = 0x9b;  buf[1] = 0x34;
                buf[2] = 0x30;  buf[3] = 0x3b;
                buf[4] = 0x33;  buf[5] = 0x33;
                buf[6] = 0x6d;  buf[7] = 0x0;
                strcat(buf, " Hit return to continue"); 
                Write(fh, buf, strlen(buf));
                ret1 = Read(fh, buf, 1L);
                Close(fh);
        }
        return TRUE;
}
