/*
    UnixConsole.c, Copyright 1999 (c) by George Fankhauser,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
 
	File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/IO/unix/Drivers/UnixConsole.c,v $
 	Author(s):             George Fankhauser
 	Affiliation:           ETH Zuerich, TIK
 	Version:               $Revision: 2922 $
 	Creation Date:         
 	Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
	
	$Log: UnixConsole.c,v $
	Revision 1.2  2001/01/10 11:10:41  cjeker
	added dynamic IODevice structure
	
	Revision 1.1  2000/11/15 10:03:34  ruf
	IO Directory structure change
	
	Revision 1.3  1999/12/13 21:48:26  ruf
	GNU General Public Licence Update
	
	Revision 1.2  1999/05/13 17:05:26  jeker
	Initial revision
	
*/

#include "Messages.h"
#include "IODevice.h"
#include "Drivers/UnixConsole.h"

#include "Unix.h"

IODeviceDesc unixConsDesc = {
                              NULL, NULL,
                              "stdio", (Address)0,
                              NULL, /* buffer must be allocated by driver */
                              -1, /* no interrupt */
                              NULL, /* no interrupt handler */
                              0, /* thread id filled in after start */
                              devUnixConsole_read, devUnixConsole_write,
                              devUnixConsole_close, devUnixConsole_init,
                              NULL /* private msg handler */,
                              FALSE, NULL /* data extension */
                            };

Error devUnixConsole_init(IODevice this)
{
  return IO_INITOK;
}

#include <sys/uio.h>

Error devUnixConsole_read(IODevice this, ThreadId threadId, char* buffer, long int* size)
{
  *size = read(STDIN_FILENO, buffer, *size);
  if (*size == -1)
    return IO_READFAILED;
  else
    return IO_READOK;
}


Error devUnixConsole_write(IODevice this, ThreadId threadId, char* buffer, long int* size)
{
  *size = write(STDOUT_FILENO, buffer, *size);
  if (*size == -1)
    return IO_WRITEFAILED;
  else
    return IO_WRITEOK;
}


Error devUnixConsole_close(IODevice this)
{
  /* this message is forwarded from ioThread */

  /* release buffers if any */

  return IO_CLOSEOK;
}
