#include <stdio.h>

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>

#include <libraries/dos.h>
#include <libraries/dosextens.h>

main()
{
  BPTR console;
  BPTR input;

  console= Open("*",1005);
  input= Input();

  if( BADDR(input) )
  {
    char buffer[10];
    long notty= 0;

    /* is 'input' a tty? */
    (void)Seek(input, 2L, OFFSET_BEGINNING);
    notty= Seek(input, 0L, OFFSET_CURRENT);

    (void)Seek(input, 0L, OFFSET_END);
    notty+= Seek(input, 0L, OFFSET_BEGINNING);

    printf("notty= %ld\n",notty);

    /*
    */

    if( IsInteractive(input) )
    {
      struct MsgPort *mp;
      long arg[1], res;

      printf("Input() returned an interactive handle.\n");

      mp = ((struct FileHandle *)(BADDR(input)))->fh_Type;
      arg[0]= -1L; /* -1= RAW, 0= COOKED */

      res= SendPacket(mp, ACTION_SCREEN_MODE, arg, 1);
      printf("SendPacket() returned %ld\n",res);
     
      { int i; for(i=0; i<sizeof(buffer); buffer[i++]='\0') ; }

      res= Read(input, buffer, (long)sizeof(buffer)-1 );
      printf("Read() returned %ld, buffer[]= \"%s\"\n",res,buffer);

      arg[0]= 0L; /* 0= COOKED */

      res= SendPacket(mp, ACTION_SCREEN_MODE, arg, 1);
      printf("SendPacket() returned %ld\n",res);
    }
    else
    { printf("Input() is not interactive.\n");
      if(console)
      {
	long res;
	{ int i; for(i=0; i<sizeof(buffer); buffer[i++]='\0') ; }
	res= Read(console, buffer, (long)sizeof(buffer)-1 );
	printf("Read() returned %ld, buffer[]= \"%s\"\n",res,buffer);
      }
      else printf("Open() failed.  No console window?\n");
    }
  }
  else printf("Input() failed.  No console?\n");

  if(console) Close(console);

  exit(0);
}

/*
 * Function - SendPacket written by Phil Lindsay, Carolyn Scheppner, and Andy
 * Finkel. This function will send a packet of the given type to the Message
 * Port supplied.
 */

typedef struct StandardPacket StandardPacket;

long
SendPacket(pid, action, args, nargs)
struct MsgPort *pid;    /* process indentifier ... (handler's message port) */
long action;    /* packet type ... (what you want handler to do) */
long *args;     /* a pointer to an argument list */
long nargs;     /* number of arguments in list */
{
    struct MsgPort *replyport;
    StandardPacket *packet;
    long count, *pargs, res1;

    replyport= (struct MsgPort *)CreatePort(NULL, 0L);
    if (!replyport)
	return(0);

    /* Allocate space for a packet, make it public and clear it */

    packet = (StandardPacket *)
      AllocMem(sizeof(StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);

    if (!packet) {
	DeletePort(replyport);
	return(0);
    }
    packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
    packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
    packet->sp_Pkt.dp_Port = replyport;
    packet->sp_Pkt.dp_Type = action;

    /* copy the args into the packet */

    pargs = &(packet->sp_Pkt.dp_Arg1);      /* address of first argument */
    for (count = 0; count < nargs; count++)
	pargs[count] = args[count];

    PutMsg(pid, &packet->sp_Msg);   /* send packet */

    WaitPort(replyport);
    GetMsg(replyport);

    res1 = packet->sp_Pkt.dp_Res1;

    FreeMem(packet, sizeof(StandardPacket));
    DeletePort(replyport);

    return (res1);
}
