/*----------------------------------------------------------------------------
   File   :    ClipSave
   Projekt:    --
   Inhalt :    CBOpen  - öffnet ClipBoard
               CBClose - Gegenspieler zu CBOpen
               CBRead  - momentanen Inhalt aus Clibboard lesen
               CBDel   - CLEAR an Clibboard.device
               main
   Version:    0.1
   Datum  :    11.Juni 1991

   Autor  :    Uwe Röhm
   Adresse:    Auber Str. 25,  W-6209 Hohenstein 4
    (Semester) Wörthstr. 18    W-8390 Passau
   Bemerkung:
----------------------------------------------------------------------------*/
#include <exec/types.h>
#include <exec/io.h>
#include <libraries/dos.h>
#include <devices/clipboard.h>
#include <stdio.h>

struct MsgPort   *CBMsgPort;
struct IOClipReq *CBIO;
char             *Buffer;
long              Length;

/*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
   Funktion  :  CBOpen
   Parameter :  long unit  (welche Unit soll geöffnet werden)
   Rückgabe  :  long error (aufgetretener Fehler beim Device-öffnen)

   Aufruf von:  main
   UnterFunks:  --
   Autor     :  Uwe Röhm
   Datum     :  11.06.1991
   Bemerkung:
--  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
long CBOpen (long unit)
{
   long error;

   CBMsgPort = (struct MsgPort *) CreatePort (NULL, NULL);
   CBIO      = (struct IOClipReq *)
               CreateExtIO (CBMsgPort, sizeof (struct IOClipReq));
   error = OpenDevice ("clipboard.device", unit, CBIO, 0);
   if (error)
   {
      DeletePort (CBMsgPort);
      DeleteExtIO (CBIO);
   }
   return (error);
}

/*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
   Funktion  :  CBClose
   Parameter :  --
   Rückgabe  :  --

   Aufruf von:  main
   UnterFunks:  --
   Autor     :  Uwe Röhm
   Datum     :  11.06.1991
   Bemerkung:
--  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
void CBClose ()
{
   if (CBIO)
   {
      CloseDevice (CBIO);
      DeletePort (CBMsgPort);
      DeleteExtIO (CBIO);
   }
}

/*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
   Funktion  :  CBRead
   Parameter :  --
   Rückgabe  :  --

   Aufruf von:  main
   UnterFunks:  --
   Autor     :  Uwe Röhm
   Datum     :  11.06.1991
   Bemerkung:
--  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
void CBRead ()
{
   long alles, len[5];

   CBIO->io_Command = CMD_READ;
   CBIO->io_ClipID  = 0;
   CBIO->io_Offset  = 0;
   CBIO->io_Data    = (char *) len;
   CBIO->io_Length  = 20;
   DoIO (CBIO);

   alles  = len[1];
   Length = len[4];
   Buffer = (char *) malloc (Length);

   CBIO->io_Data = (char *) Buffer;
   CBIO->io_Length = Length;
   DoIO (CBIO);

   CBIO->io_Offset += alles;
   CBIO->io_Data    = NULL;
   CBIO->io_Length  = 1;
   DoIO (CBIO);
}

/*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
   Funktion  :  CBDel
   Parameter :  --
   Rückgabe  :  --

   Aufruf von:  main
   UnterFunks:  --
   Autor     :  Uwe Röhm
   Datum     :  11.06.1991
   Bemerkung:
--  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
void CBDel ()
{
   CBIO->io_Command = CMD_CLEAR;
   CBIO->io_Offset  = 0;
   CBIO->io_ClipID  = 0;
   DoIO (CBIO);
}



main (int argc, char *argv[])
{
   int retcode = 0;
   long wrote;
   FILE *fh;

   if (argc == 2)
   {
      if (CBOpen (0) == 0)
      {
         fh = fopen (argv[1], "w");
         if (fh != NULL)
         {
            CBRead ();
            if (Buffer)
            {
               wrote = fwrite (Buffer,(long) Length,(long) 1, fh);
               if (wrote != 1)
                  retcode = 10;
               CBDel ();
               free (Buffer);
            }
            else
               retcode = 5;
         }
         else
            retcode = 20;
         CBClose ();
      }
      else
         retcode = 5;
   }
   else
     retcode = 10;

   return retcode;
}
