/*	rawcon.c
 *  (c) Copyright 1991 by Ben Eng, All Rights Reserved
 *	
 */

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <exec/tasks.h>
#include <exec/ports.h>
#include <exec/execbase.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>
#include <workbench/startup.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#include "make.h"

void
SetRawMode( long flag )
{
    struct Process *process;
    struct MsgPort *conport;
    struct MsgPort *replyport;
    struct StandardPacket *pkt;

    if( !(process = Global.me )) return; /* find this task */
	/* msgport for CON: */
    conport = (struct MsgPort *) process->pr_ConsoleTask;

    replyport=&process->pr_MsgPort;

    pkt=(struct StandardPacket *) malloc(sizeof(struct StandardPacket));

    pkt->sp_Msg.mn_Node.ln_Name=(char *) &pkt->sp_Pkt;
    pkt->sp_Pkt.dp_Port=replyport;
    pkt->sp_Pkt.dp_Type=994L;           /* set raw mode */
    pkt->sp_Pkt.dp_Link=&pkt->sp_Msg;
    pkt->sp_Pkt.dp_Arg1=flag;           /* TRUE for on, FALSE for off */

    PutMsg(conport,&pkt->sp_Msg);
    WaitPort(replyport);
    Remove((struct Node *)&pkt->sp_Msg);

    free(pkt);
}

/* findwindow.c - utility routine to find window of a CLI. */
struct Window *
FindWindow( BPTR file )
{
   extern struct DosLibrary *DOSBase;
   struct Window *win;
   struct Process *proc;
   struct CommandLineInterface *cli;
   struct InfoData *id;
   struct StandardPacket *pkt;
   struct FileHandle *fh;
   long ret1,ret2;

   if (id=(struct InfoData *)
     AllocMem(sizeof(struct InfoData),MEMF_PUBLIC|MEMF_CLEAR)) {
      if (pkt=(struct StandardPacket *)
        AllocMem(sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR)) {
         proc=(struct Process *) FindTask(NULL);
         if (cli=(struct CommandLineInterface *) (proc->pr_CLI<<2)) {
            ret1=cli->cli_ReturnCode;
            ret2=cli->cli_Result2;
            if (IsInteractive(file)) {
               pkt->sp_Msg.mn_Node.ln_Name=(char *) &(pkt->sp_Pkt);
               pkt->sp_Pkt.dp_Link=&(pkt->sp_Msg);
               pkt->sp_Pkt.dp_Port=&(proc->pr_MsgPort);
               pkt->sp_Pkt.dp_Type=ACTION_DISK_INFO;
               pkt->sp_Pkt.dp_Arg1=((ULONG) id)>>2;
               fh=(struct FileHandle *) (file<<2);
               PutMsg(fh->fh_Type,(struct Message *) pkt);
               WaitPort(&(proc->pr_MsgPort));
               GetMsg(&(proc->pr_MsgPort));
               win=(struct Window *) id->id_VolumeNode;
            }
            cli->cli_Result2=ret2;
            cli->cli_ReturnCode=ret1;
         }
         FreeMem(pkt,sizeof(struct StandardPacket));
      }
      FreeMem(id,sizeof(struct InfoData));
   }
   return(win);
}

