home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * Spooler.C *
- * *
- * Line Printer Daemon using TCP/IP printer protocol *
- * *
- * -------------- The spool file maintenance routines -------------- *
- * *
- * Written by Casper Boon, July, 1992. *
- * *
- * © 1992, Casper Boon. *
- * *
- ************************************************************************/
-
- #include "LPD.H"
- #include "lpdProtos.H"
- #include "Spooler.H"
- #include "BackGrounder.H"
-
-
- Boolean printing = FALSE;
- extern integer spool_dir;
- extern prtHandle printers;
- extern char *cfA;
-
- static printParm prints[4];
-
-
- /************************************************************************
- ************************************************************************/
- /* print the next buffer load */
- void Poke_Spooler()
- {
- integer fRef;
- Byte printer[100];
- Str255 name_buf;
- integer which_prt, cfRef;
- ctl_def cctl;
- prtPtr prtr = *printers;
- integer nPrtrs = GetHandleSize((Handle)printers) / sizeof(prtRecord);
-
- if (noAction) return; /* debugging */
-
- if (printing)
- {
- if DEBUGGING log_printf("Spooler poked while printing\n");
- return;
- }
-
- if ( GetFile(name_buf, &cctl) ) /* find a control file */
- {
- Quitable = FALSE;
- printing = TRUE;
-
- which_prt = 2;
- while (nPrtrs--)
- {
- if ( strcmp(cctl.printer, prtr->in_name)==0 )
- break;
- prtr++;
- }
-
- if (nPrtrs < 0)
- {
- Handle elog = NewHandle(0);
- char buf[128];
-
- sprintf(buf, "No such printer?? \"%s\"\n", cctl.printer);
- log_printf(buf);
-
- LogError("\015\012\015\012Printing to ", elog);
- LogError(cctl.printer, elog);
- LogError("\015\012\015\012", elog);
- LogError(buf, elog);
- if (mailing) SendMail(cctl.owner, cctl.host, elog, 0);
- DisposHandle(elog);
-
- DeleteSpoolFile(name_buf);
- printing = FALSE;
- Quitable = TRUE;
- Poke_Spooler();
- return;
- }
- else if DEBUGGING
- log_printf("No files found when spooler poked\n");
-
- name_buf[1] = cfA[0]; /* make sure! */
- if (FSOpenRO(name_buf, spool_dir, &cfRef))
- return ; /* error opening control file */
-
- prints[which_prt=nPrtrs].prtr = prtr;
-
- if (prtr->outname[0])
- prints[which_prt].printer = prtr->outname;
- else
- prints[which_prt].printer = NIL;
- prints[which_prt].cfRef = cfRef;
- BlockMove(name_buf, prints[which_prt].name_buf, name_buf[0] + 1);
-
- BlockMove((Ptr)&cctl, (Ptr)&prints[which_prt].cctl, sizeof(cctl));
-
- prints[which_prt].elog = NewHandle(0);
-
- /* print all files for this job */
- NextFile(which_prt);
- }
- }
-
-
- /************************************************************************
- ************************************************************************/
- void NextFile(integer which_prt)
- {
- integer count = 0;
- char buf[100];
- Byte prt_name_buf[100];
- Boolean literal, found = FALSE;
- LongInt bytes;
- OSErr err;
-
- while ( ReadLine(prints[which_prt].cfRef, buf, 98) )
- {
- if (buf[0] != 'f' && buf[0] != 'l') /* the spool file name ? */
- continue;
-
- literal = (buf[0] == 'l');
-
- BlockMove(&buf[1], &prt_name_buf[1], strlen(&buf[1]));
- prt_name_buf[0] = strlen(&buf[1]);
-
- while ( ReadLine(prints[which_prt].cfRef, buf, 98) )
- { /* look for source file name */
- if (buf[0] == 'N') /* the source file name ? */
- break;
- }
-
- log_printf("Printing %s for %s@%s on \"%s\"\n",
- &buf[1], prints[which_prt].cctl.owner,
- prints[which_prt].cctl.host,
- prints[which_prt].cctl.printer);
-
- /* add the name of the file to the error log */
- prints[which_prt].prev_len = GetHandleSize(prints[which_prt].elog);
- LogError("\015\012\015\012Printing file ", prints[which_prt].elog);
- LogError(&buf[1], prints[which_prt].elog);
- LogError("\015\012\015\012", prints[which_prt].elog);
- prints[which_prt].old_len = GetHandleSize(prints[which_prt].elog);
-
-
- if ((err=FSOpenRO(prt_name_buf, spool_dir, &prints[which_prt].fRef))==noErr)
- {
- if DEBUGGING
- log_printf("Now starting %p, (%d)\n",
- prt_name_buf, prints[which_prt].fRef);
- if (prints[which_prt].prtr->postscript /*is a postscript printer*/)
- {
- if DEBUGGING
- log_printf("Checking for postscript\n");
- bytes = 2L; FSRead(prints[which_prt].fRef, &bytes, buf);
- SetFPos(prints[which_prt].fRef, fsFromStart, 0L); /* set the file bck to the start */
-
- if (bytes < 2) /* an error */
- {
- if DEBUGGING
- log_printf("%p is not long enough\n", prt_name_buf);
-
- FSClose(prints[which_prt].fRef);
- continue;
- }
- /* the following test for postscript is not the best. It checks if *
- * either the first or second byte in a file is a %. This covers *
- * the DOS postscript generators which like to put a page feed as *
- * the first byte before the %!. */
- else if ((buf[0] != '%') && (buf[1] != '%'))
- { /* not a postscript file */
- if DEBUGGING
- log_printf("%p is NOT postscript\n", prt_name_buf);
-
- if (!literal && which_prt != 1) /* not an ImageWriter */
- {
- #ifdef TEXTPS
- prints[which_prt].fRef =
- textps(prt_name_buf, prints[which_prt].fRef);
- if (stopped)
- {
- stopped = FALSE;
- continue;
- }
- #else
- LogError("This is NOT POSTSCRIPT\015\012", prints[which_prt].elog);
- continue;
- #endif
- }
- }
- else
- {
- if DEBUGGING
- log_printf("%p IS a postscript\n", prt_name_buf);
-
- /* we are ready to print this one */
- }
- }
- }
- else /* file failed to open */
- {
- log_printf("Could not open %p (%d)\n", prt_name_buf, err);
- continue;
- }
-
- found = TRUE;
- break;
- }
-
- if (!found) /* done all files for this print job */
- {
- FSClose(prints[which_prt].cfRef);
- if ( ( GetHandleSize(prints[which_prt].elog) > 0 ) && mailing )
- SendMail(prints[which_prt].cctl.owner, prints[which_prt].cctl.host, prints[which_prt].elog, 0);
- DeleteSpoolFile(prints[which_prt].name_buf);
- DisposHandle(prints[which_prt].elog);
- printing = FALSE;
- Quitable = TRUE;
- Poke_Spooler();
- return;
- }
-
- prints[which_prt].pstate = 0;
- if (prints[which_prt].prtr->pap)
- PAPDownLoad(prints[which_prt].fRef,
- prints[which_prt].prtr->postscript,
- prints[which_prt].printer,
- prints[which_prt].elog,
- &prints[which_prt].pstate);
- else
- SerialPrint(prints[which_prt].fRef,
- prints[which_prt].prtr->postscript,
- prints[which_prt].printer,
- prints[which_prt].prtr->params,
- prints[which_prt].elog,
- &prints[which_prt].pstate);
-
- Background(&prints[which_prt].pstate, PrintFileDone, (Ptr)which_prt);
- }
-
-
- /************************************************************************
- ************************************************************************/
- void PrintFileDone(integer flag, Ptr param)
- {
- integer index = ((LongWord)param & 0xFFF);
-
- FSClose(prints[index].fRef);
-
- /* if the size hasn't changed, might as well delete the file name */
- if (prints[index].old_len == GetHandleSize(prints[index].elog) )
- SetHandleSize(prints[index].elog, prints[index].prev_len);
- else
- LogError("\015\012\015\012", prints[index].elog);
-
- NextFile(index);
- }
-
-
- /************************************************************************
- ************************************************************************/
- integer GetFile(StringPtr name_buf, ctl_def *ctl)
- {
- integer index = 1;
-
- if ( !EnumerateFiles(spool_dir, &index, name_buf, ctl) )
- return FALSE;
-
- if DEBUGGING
- log_printf("Printing file %p\n", name_buf);
-
- ctl->nextFile = 0;
- return TRUE;
- }
-
- /************************************************************************
- ************************************************************************/
- void SendPostscriptFile()
- {
- SFReply reply;
- Point aPoint;
- SFTypeList types;
- integer fRef, err, pstate;
- extern Boolean printing;
- Handle elog;
-
- if (printing)
- {
- SysBeep(5);
- return;
- }
-
- aPoint.h = aPoint.v = 80;
- types[0] = 'TEXT';
- SFGetFile(aPoint, NIL, NIL, 1, types, NIL, &reply);
-
- if ( reply.good == 0 )
- return;
-
- err = FSOpen((StringPtr)reply.fName, reply.vRefNum, &fRef);
- if ( err == -49 ) /* already open somewhere */
- err = FSOpenRO((StringPtr)reply.fName, reply.vRefNum, &fRef);
-
- if ( !err )
- {
- pstate = 1;
- elog = NewHandle(0);
- PAPDownLoad(fRef, TRUE, NIL, elog, &pstate);
- while (pstate > 0)
- MainEvents(everyEvent);
- DisposHandle(elog);
- }
-
- FSClose(fRef);
- }
-