/***************************************************************************/
/*     PrintFile - Written By Lars Siden Wednesday 07-Oct-92 18:45:40      */
/*     Use this source as you want. It's just an example to my brother.    */
/*     I take no responsibilty for damage using this program or source.    */
/*     Have FUN! And call Tea 42 BBS +46-8-712 40 67 - Xenolink - SWEDEN   */
/***************************************************************************/

#include <proto/dos.h>  /* The necessary Includes */
#include <proto/exec.h>
#include <exec/types.h>        
#include <exec/errors.h>       
#include <exec/memory.h>
#include <devices/printer.h>
#include <exec/io.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <so:devices/printfile_protos.h> /* Protos for this example       */
				      /* Change PATH to fit your needs */
#define BUFFER_SIZE 256               /* Buffer size for READ() <bytes>*/     

union printerIO {                     /* A complete Printer Req block  */      
 
 	struct IOStdReq    ios;     
	struct IODRPReq    iodrp;  
	struct IOPrtCmdReq iopc;    
};

struct MsgPort *replymp = NULL;       /* Our Reply Port */
struct FileHandle *in;                /* FilePointer for READ() */
struct DosLibrary *DOSBase;           /* We will open the dos.library */

union printerIO *printer_req = NULL; 

char *buffer; 

void printtext(                    /* Declare our Printer function */
	union printerIO *ioreq,  
	BYTE *data,              
	ULONG length);

void shutitdown();                 /* Declare our shut-down function */

int len,error;	/* len = Number of read bytes by READ()    */
		/* error = check if IO error when printing */ 

char text[80];  /* This is a dummy string for Sprintf()    */                  

main(int argc, char *argv[])

{
	if((DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",0L))==0);

	if(argc<2)  /* If no FILE name given -> */
	{
		sprintf(text,"%s","\nMissing File Name, Nothing to print! \n\n");
		Write(Output(),text,strlen(text));
		exit(0);
	}

	/* Open The File , If fail then quit */
	if((in=(struct FileHandle *)Open(argv[1],MODE_OLDFILE))==0) 
	{
		sprintf(text,"%s %s %s","\nCouldn't open file",argv[1],"\n\n");
	        Write(Output(),text,strlen(text));
		exit(0);
	}

	/* Open the reply port that we will communicate through */
	replymp = (struct MsgPort *) CreatePort(NULL,0); 
	if( ! replymp)      
		{
			Close((BPTR)in);
			exit(0);
		}

	printer_req = (union printerIO *)        
		CreateExtIO(replymp,sizeof(union printerIO));

	if ( ! printer_req)
	{
		DeletePort(replymp);
		Close((BPTR)in);
		exit(0);
	}

	if((OpenDevice("printer.device",0,(struct IORequest *)printer_req,0))!=0)
	{
		DeletePort(replymp); 
		DeleteExtIO(printer_req);
		Close((BPTR)in);
		exit(0);
	}	

	/* Allocate memory for our little file buffer */
	buffer = (char *) AllocMem(BUFFER_SIZE,MEMF_CLEAR);
		
	do {  /* Loop for reading buffer and printing it */
	     len=Read((BPTR)in,buffer,BUFFER_SIZE);
	     printtext(printer_req,buffer,len);
	}
	while(len!=0);	

	shutitdown(); 
}

/*************************************************************************/

void printtext (union printerIO *ioreq, BYTE *data, ULONG length)

{

	ioreq -> ios.io_Command = CMD_WRITE;    /* We want to write */
	ioreq -> ios.io_Data    = (APTR) data;  /* Pointer to data  */
	ioreq -> ios.io_Length  = length;       /* How many chars ? */

	/* Prints text and waits for the printer to finish. We don't want */
	/* to overflow the buffer */
	error=DoIO((struct IORequest *)ioreq);  
						  
	if(error != 0) /* error ? */
		shutitdown();
}
/*************************************************************************/

void shutitdown()

{
	CloseDevice((struct IORequest *)printer_req);

	DeleteExtIO(printer_req); 

	DeletePort(replymp);         

	FreeMem(buffer,BUFFER_SIZE); 

	Close((BPTR)in);             

	CloseLibrary((struct Library *)DOSBase);
	
	exit(0);                     
}
