/* ----------------------------------------------------------------- */
/*     Spoolit Version 1.0 (C) Fourth Level Developments 1994        */
/* ----------------------------------------------------------------- */


/*

TSpool  is  written  by  T.Southwell  and is (C) Fourth Level Developements
1994.   You  may freely distribute these programs and documentation as long
as you do not make a charge of any more than a reasonable copying fee. If 
you wish to revise or update the software then please do but you must then
send a copy of the new version and source code to Fourth Level Developments.
Fourth Level Developments will be willing to have this software incorporated in other software packages but
in other programmes but you must first put your suggestions to us and obtain
written permission before you start work on it.


This part of the print spooler reads in the spool files according to a list
which is stored in t:. The files are written one by one to par: and then
deleted.

Email: dev@flevel.demon.co.uk

*/

#include <stdio.h>
#define block 64L*1024L
#define uselog 1

void* Open();
long Read();
void* AllocMem();
long Seek();
FILE* loghan=0; 
long ll=0;
char logname[160]={"nil:"};

main(argc,argv)
 int argc;
 char **argv;
 {
 void* inhan=0;
 void* outhan=0;
 void* listhan=0;
 char workstr[80];
 long size=0;
 char *buffer=0;
 long place=0;
 int f;
 long rlen=0;
 long rnum=0;

 
 gimmelog();
 printf("Loghan=%lx\n",loghan);
 fprintf(loghan,"Spoolit starts up.\n"); ll++;
 gimmelog();
 
re_make:

    
    while(listhan==0)
         {
         listhan=(void*)Open("t:TSpool-list",1005L);
         if (listhan==0)
            Delay(50L);
         }

    Seek(listhan,place,-1L);
    
    rlen=Read(listhan,&rnum,4L);
    if (rlen!=4)
       {
        Close(listhan); listhan=0;
        Delay(50L);
        goto re_make;
       }
       
    place+=4;
    Close(listhan); listhan=0;

    fprintf(loghan,"New print file number %ld\n",rnum); ll++;
    gimmelog();
    
    sprintf(workstr,"spool:spool%ld",rnum);
    
   
    inhan=(void*)Open(workstr,(long)1005L);
    if (inhan==0)
       {
       Delay(25L);
       inhan=(void*)Open(workstr,(long)1005L);
       if (inhan==0) 
          {
           gimmelog();
           fprintf(loghan,"Can't open file '%s'\n",workstr); ll++;
           goto re_make;
          }
       }

    fprintf(loghan,"Opened file '%s'\n",workstr); ll++;
    
    while (outhan==0)
          {
           gimmelog();
           fprintf(loghan,"Trying to get the parallel port.\n"); ll++;
           outhan=(void*)Open("par:",(long)1006L);
           if (outhan==0)
              Delay(50L);
          }

    fprintf(loghan,"Ok, got the parallel port.\n"); ll++;
    size=block;
    
    while (buffer==0) 
          {
           gimmelog();
           fprintf(loghan,"Trying to allocate %ld bytes of memory.\n",size); ll++;
           buffer=(char*)AllocMem(size,1L);
           if (buffer==0)
              Delay(50L);
          }

    fprintf(loghan,"Ok, got %ld bytes of memory.\n",size); ll++;
 
    while (size==block)
          {
           gimmelog();
           fprintf(loghan,"Try to read %ld bytes.\n",block); ll++;
           size=Read(inhan,(void*)buffer,block);
           fprintf(loghan,"Have read %ld bytes.\n",size); ll++;
           Write(outhan,(void*)buffer,size);
           fprintf(loghan,"Written %ld bytes to the printer.\n",size); ll++;
          }       

    fprintf(loghan,"Shutdown handles.\n"); ll++;
    
    Close(inhan); inhan=0;
    Close(outhan); outhan=0;
    FreeMem(buffer,block); buffer=0;

    fprintf(loghan,"Memory now free.\n"); ll++;

    sprintf(workstr,"delete spool:spool%ld",rnum);    
    Execute(workstr,0L,0L);

    fprintf(loghan,"Deleted file 'spool:spool%ld'\n",rnum); ll++;
    gimmelog();
        
    goto re_make;
 }
gimmelog()
{
 #ifdef uselog
   if ((ll%200)==0)
      {
       printf("Open log a\n");
       if (loghan)
          {
           fclose(loghan);
           loghan=0;
          }

       loghan=fopen("t:spoolit.loga","w");
       fclose(loghan); loghan=0;
       loghan=fopen("t:spoolit.loga","a");
       sprintf(logname,"t:spoolit.loga");
       
       fprintf(loghan,"Spoolit opens new log file a.\n");
      }
   else
   if ((ll%200)==100)
      {
       printf("Open log b\n");
       if (loghan) 
          {
           fclose(loghan);
           loghan=0;
          }
       loghan=fopen("t:spoolit.loga","w");
       fclose(loghan); loghan=0;
       loghan=fopen("t:spoolit.loga","a");
       sprintf(logname,"t:spoolit.logb");

       fprintf(loghan,"Spoolit opens new log file b.\n"); ll++;
      }
   else
   if (loghan)
      {
       fclose(loghan);
       loghan=fopen(logname,"a");
      }
 #endif

 if (loghan==0)
    {
     printf("No loghan is open.\n");
     loghan=fopen("nil:","a");
    }
}