1 /*****************************************************************************\
2 script.cpp : Implimentation for scripting
4 Copyright (c) 1996 - 2001, Hewlett-Packard Co.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name of Hewlett-Packard nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
22 NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 \*****************************************************************************/
31 #if defined(APDK_CAPTURE)
38 extern char* Version(int bCompressed);
40 Scripter::Scripter(SystemServices* pSS):
51 AsciiScripter::AsciiScripter(SystemServices* pSS)
56 AsciiScripter::~AsciiScripter()
60 BinaryScripter::BinaryScripter(SystemServices* pSS)
65 BinaryScripter::~BinaryScripter()
69 DRIVER_ERROR SystemServices::InitScript(const char* FileName, BOOL ascii, BOOL read)
72 pScripter = new AsciiScripter(this);
73 else pScripter = new BinaryScripter(this);
78 if (pScripter->OpenDebugStreamR(FileName))
85 if (pScripter->OpenDebugStreamW(FileName))
92 DRIVER_ERROR SystemServices::EndScript()
99 if (pScripter->CloseDebugStreamR())
101 else return NO_ERROR;
105 if (pScripter->CloseDebugStreamW())
107 else return NO_ERROR;
114 BOOL Scripter::ParseVer(char* str)
115 // set argProprietary
118 //"[platform-specific information]!!002.001_09-12-00 propscale openimg debug little_endian fonts:CTLU DJ400 DJ540 DJ600 DJ6xx DJ6xxPhoto DJ8xx DJ9xx Aladdin DJ630 eprinter"
124 while ((c!='!') && (c!=EOF))
128 while ((c!=' ') && (c!=EOF))
133 for (int j=0; j<4; j++)
137 if (!strcmp(ver,"open")) // old style
139 else if (!strcmp(ver,"prop")) // old style
144 if (!strcmp(ver,"openscale"))
146 else if (!strcmp(ver,"propscale"))
149 if (!strcmp(ver,"openimg"))
151 else if (!strcmp(ver,"propimg"))
158 BOOL AsciiScripter::OpenDebugStreamR(const char* FileName)
163 strncpy(ScriptFileName,FileName, sizeof(ScriptFileName)-1);
165 ScriptFile = fopen(ScriptFileName, "ra");
167 if (ScriptFile == NULL)
170 char c=fgetc(ScriptFile);
171 fseek(ScriptFile,0, 0);
173 if (c==' ') // spaces were never overwritten with tokencount
175 x = 2000; // arbitrary guess
178 else if (GetDebugInt(x))
185 if (GetDebugString(str,x)) // get $Version
195 BOOL AsciiScripter::CloseDebugStreamR()
197 if (GlobalBuffer != NULL)
198 pSys->FreeMem((BYTE*)GlobalBuffer,FALSE);
200 if (ScriptFile==NULL)
202 return fclose(ScriptFile);
206 BOOL AsciiScripter::OpenDebugStreamW(const char* FileName)
208 strncpy(ScriptFileName,FileName, sizeof(ScriptFileName)-1);
209 ScriptFile = fopen(ScriptFileName, "wa");
211 if (ScriptFile == NULL)
214 pSys->Capturing=TRUE;
218 // set up debugging token strings
219 strcpy(TokString[0],"Job constructor");
220 strcpy(TokString[1],"Job destructor");
221 strcpy(TokString[2],"NewPage");
222 strcpy(TokString[3],"SendRasters");
223 strcpy(TokString[4],"TextOut");
224 strcpy(TokString[5],"UNUSED1");
225 strcpy(TokString[6],"Font destructor");
226 strcpy(TokString[7],"PrintContext constructor");
227 strcpy(TokString[8],"PrintContext destructor");
228 strcpy(TokString[9],"SetPixelsPerRow");
229 strcpy(TokString[10],"RealizeFont");
230 strcpy(TokString[11],"SelectDevice");
231 strcpy(TokString[12],"SelectPrintMode");
232 strcpy(TokString[13],"SetPaperSize");
233 strcpy(TokString[14],"UseBlackOnly");
234 strcpy(TokString[15],"UseColor");
235 strcpy(TokString[16],"SetInputResolution");
236 strcpy(TokString[17],"UNUSED");
237 strcpy(TokString[18],"UNUSED");
238 strcpy(TokString[19],"UNUSED");
239 strcpy(TokString[20],"String");
240 strcpy(TokString[21],"RLE stream");
241 strcpy(TokString[22],"RAW stream");
242 strcpy(TokString[23],"stream token");
243 strcpy(TokString[24],"NULL token");
245 for (int i=0; i<25; i++)
248 fprintf(ScriptFile," ");
250 char version[TEMPLEN];
251 sprintf(version, Version(FALSE) );
252 int len = strlen(version);
254 if (PutDebugString(version, len))
261 BOOL AsciiScripter::CloseDebugStreamW()
263 if (ScriptFile==NULL)
266 if (GlobalBuffer != NULL)
267 pSys->FreeMem((BYTE*)GlobalBuffer,FALSE);
269 fseek(ScriptFile,0, 0);
270 PutDebugInt(TokenCount);
272 pSys->Capturing=FALSE;
275 return fclose(ScriptFile);
279 BOOL AsciiScripter::PutDebugToken(const int token)
282 TokCount[token] = TokCount[token]+1;
284 int res=fprintf(ScriptFile,"%%%0x ",token);
288 res=fprintf(ScriptFile,"\n%s:%d\n",TokString[token], TokCount[token]);
290 return (res < 1); // bad if res<1
294 BOOL AsciiScripter::PutDebugInt(const int data)
296 int res=fprintf(ScriptFile,"%%%0x ",data);
297 return (res < 1); // bad if res<1
301 BOOL AsciiScripter::PutDebugByte(const BYTE data)
302 { return PutDebugInt(data); }
305 BOOL AsciiScripter::PutDebugString(const char* str,const int len)
307 if (PutDebugToken(tokCharPtr))
309 if (PutDebugInt(len))
312 fputc('"',ScriptFile);
313 for (int i=0; i < len; i++)
314 fprintf(ScriptFile,"%c",str[i]);
315 fputc('"',ScriptFile);
321 BOOL AsciiScripter::PutDebugStream(const BYTE* str,const int len)
323 // Does modified run-length-encoding from stream.
324 // len=length of raw stream
325 int total=1; short count,i,j; BYTE current,next,last;
329 if ((len==0) || (str==NULL))
331 PutDebugToken(tokRawStream);
332 fprintf(ScriptFile,"%%%0x ", len);
336 next=0; // suppress stupid compiler warning
337 int alloclen= len*2; // in worst case each byte produces [count=1][byte]
338 if (buffsize < alloclen)
340 if (GlobalBuffer!=NULL)
341 pSys->FreeMem((BYTE*)GlobalBuffer,FALSE);
342 GlobalBuffer = (short*)pSys->AllocMem(alloclen,FALSE);
346 short* buff = GlobalBuffer;
351 const BYTE *startstream=str;
353 for (i=0;i<3;i++) // go through 3 times, for r,g,b
355 short* startbuff=buff;
356 const BYTE* stream=startstream+i;
358 current = *stream; stream+=3;
359 while (total<(len/3))
362 while ((total<(len/3)) && (current == (next= *stream)) )
363 { count++; total++; stream+=3; }
365 { next= *stream; stream+=3; total++; }
366 buff[0]=count; buff[1]=current;
371 // get last item if it is a single
374 buff[0]=count; buff[1]=current;
378 int chunks=(buff-startbuff)/2;
380 { rle=FALSE; break; }
381 else chunkarray[i]=chunks;
386 PutDebugToken(tokRawStream);
387 fprintf(ScriptFile,"%%%0x ", len);
389 fprintf(ScriptFile,"%%%0x ",startstream[i]);
394 PutDebugToken(tokRLEstream);
395 fprintf(ScriptFile,"%%%0x ",len);
397 { count=chunkarray[i];
398 for (j=0;j<count;j++)
399 fprintf(ScriptFile,"%%%0x %%%0x ",thebuff[total+2*j],thebuff[total+2*j+1]);
408 ///////////////////////////////////////////////////////////////////
409 BOOL AsciiScripter::FindPercent()
410 // find our token '%'
411 // return TRUE iff EOF
415 while ((c!='%') && (c!=EOF))
423 char* AsciiScripter::digits()
424 // includes filtering of CR/LF's
433 if (((c>='0')&&(c<='9')) ||
434 ((c>='a')&&(c<='f')) ||
439 { done=TRUE; *str++=c; }
440 else if ((c!=10)&&(c!=13))
441 { scanner[0]=0; done=TRUE; }
449 void AsciiScripter::ReadRLE(int instreamlen, BYTE* outstream)
450 // Does run-length-decoding from stream.
455 BYTE* buff=(BYTE*)pSys->AllocMem(instreamlen*2,FALSE);
456 int colorsize=instreamlen/3;
458 // first fill up 3-part buffer with all reds,then all greens, all blues
460 for (i=0;i<3;i++) // for each color
462 while (total<colorsize)
463 // assumes correct input, so that total count=instreamlen/3
467 sscanf(digits(),"%x ",&count);
470 sscanf(digits(),"%x ",&bi);
472 for (j=0;j<count;j++)
473 buff[ total + (i*colorsize) + j ] = b;
480 for (i=0;i<(instreamlen/3);i++)
482 *outstream++ = buff[i];
483 *outstream++ = buff[i+colorsize];
484 *outstream++ = buff[i+(2*colorsize)];
486 pSys->FreeMem(buff,FALSE);
490 void AsciiScripter::ReadRaw(int instreamlen, BYTE* outstream)
493 for (int i=0;i<instreamlen;i++)
496 sscanf(digits(),"%x ",&bi);
502 ///////////////////////////////////////////////////////////////////
503 BOOL AsciiScripter::GetDebugToken(int& token)
507 if (GetDebugInt(token))
509 if ((token<0)||(token>LAST_TOKEN))
512 // now expect LF<string>LF
517 while ( (c=fgetc(ScriptFile))!=10) ;
518 // successfully ate debug string
526 BOOL AsciiScripter::GetDebugInt(int& data)
530 { data=-1; return TRUE; }
532 res = sscanf(digits(),"%x ",&k);
540 BOOL AsciiScripter::GetDebugByte(BYTE& data)
543 BOOL res=GetDebugInt(temp);
551 BOOL AsciiScripter::GetDebugString(char*& str,int& length)
554 str=NULL; // in case of err rtn
556 for (i=0; i<TEMPLEN; i++)
559 GetDebugInt(res); // look for tokCharPtr
560 if (res != tokCharPtr)
562 GetDebugInt(len); // get length
567 char c=fgetc(ScriptFile);
571 tempStr[i]=fgetc(ScriptFile);
572 while ((i<TEMPLEN) && (tempStr[i] != '"'))
573 tempStr[++i]=fgetc(ScriptFile);
575 if ((i==TEMPLEN) || (i!=len))
586 BOOL AsciiScripter::GetDebugStream(const unsigned int buffersize, BYTE*& buffer)
591 GetDebugToken(token);
592 if (token == tokStream)
594 buffer = (BYTE*)pSys->AllocMem(buffersize,FALSE);
602 if (((unsigned int)len) != buffersize)
605 for (int i=0; i < len; i++)
611 else if ((token == tokRLEstream) || (token == tokRawStream))
620 buffer = (BYTE*)pSys->AllocMem(len,FALSE);
621 if (token == tokRLEstream)
622 ReadRLE(len, buffer);
623 else ReadRaw(len, buffer);
632 /////////////////////////////////////////////////////////////////////
633 BOOL BinaryScripter::OpenDebugStreamR(const char* FileName)
635 strncpy(ScriptFileName,FileName, sizeof(ScriptFileName)-1);
637 ScriptFile = fopen(ScriptFileName, "rb");
639 if (ScriptFile == NULL)
650 if (GetDebugString(ver,x))
660 BOOL BinaryScripter::OpenDebugStreamW(const char* FileName)
662 strncpy(ScriptFileName,FileName, sizeof(ScriptFileName)-1);
663 ScriptFile = fopen(ScriptFileName, "wb");
665 if (ScriptFile == NULL)
668 pSys->Capturing=TRUE;
672 PutDebugInt(-1); // leave space for token count
674 char version[TEMPLEN];
675 sprintf(version, Version(FALSE) );
676 int len = strlen(version);
678 if (PutDebugString(version, len))
684 BOOL BinaryScripter::PutDebugToken(const int token)
688 return PutDebugByte(token);
692 BOOL BinaryScripter::PutDebugInt(const int data)
694 int res=fwrite( &data, sizeof(int), 1, ScriptFile );
699 BOOL BinaryScripter::PutDebugByte(const BYTE data)
701 int res=fputc( data, ScriptFile );
706 BOOL BinaryScripter::PutDebugString(const char* str,const int len)
708 if (PutDebugToken(tokCharPtr))
710 if (PutDebugInt(len))
713 for (int i=0; i < len; i++)
714 if ((fputc(str[i],ScriptFile))==EOF)
720 BOOL BinaryScripter::PutDebugStream(const BYTE* str,const int len)
722 if (PutDebugToken(tokStream))
724 if (PutDebugInt(len))
727 for (int i=0; i < len; i++)
728 if ((fputc(str[i],ScriptFile))==EOF)
735 BOOL BinaryScripter::GetDebugToken(int& token)
741 if (token>LAST_TOKEN)
750 BOOL BinaryScripter::GetDebugInt(int& data)
753 int res = fread(&temp, sizeof(int), 1, ScriptFile);
759 BOOL BinaryScripter::GetDebugByte(BYTE& data)
761 data=fgetc(ScriptFile);
766 BOOL BinaryScripter::GetDebugString(char*& str,int& length)
769 str=NULL; // in case of err rtn
771 for (i=0; i<TEMPLEN; i++)
774 GetDebugToken(res); // look for tokCharPtr
775 if (res != tokCharPtr)
777 GetDebugInt(len); // get length
782 for (i=0; i<len; i++)
783 if ((tempStr[i]=fgetc(ScriptFile))==EOF)
794 BOOL BinaryScripter::GetDebugStream(const unsigned int buffersize, BYTE*& buffer)
799 GetDebugToken(token);
800 if (token != tokStream)
803 buffer = (BYTE*)pSys->AllocMem(buffersize,FALSE);
811 pSys->FreeMem(buffer,FALSE);
815 if (((unsigned int)len) != buffersize)
818 for (int i=0; i < len; i++)
829 #endif //APDK_CAPTURE