66 int numinfilelist = 0;
69 #define BANNER (4*sizeof(LONG))
70 void *malloclist[60000];
71 LONG mallocsizes[60000];
72 char *mallocstrings[60000];
73 int nummalloclist = 0;
77 extern "C" getdtablesize();
90 #error "MALLOCPROTECT": undefine "TRAPSIGNALS" in unix.h first!
92 #include "mallocprotect.h"
98 #define M_alloc mprotectMalloc
102 #ifdef TERMMALLOCDEBUG
103 WORD **DebugHeap1, **DebugHeap2;
112 UBYTE *LoadInputFile(UBYTE *filename,
int type)
116 UBYTE *buffer, *name = filename;
118 handle = LocateFile(&name,type);
119 if ( handle < 0 )
return(0);
121 SeekFile(handle,&scrpos,SEEK_END);
122 TELLFILE(handle,&scrpos);
123 filesize = BASEPOSITION(scrpos);
125 SeekFile(handle,&scrpos,SEEK_SET);
126 buffer = (UBYTE *)Malloc1(filesize+2,
"LoadInputFile");
127 if ( ReadFile(handle,buffer,filesize) != filesize ) {
128 Error1(
"Read error for file ",name);
129 M_free(buffer,
"LoadInputFile");
130 if ( name != filename ) M_free(name,
"FromLoadInputFile");
135 if ( type == PROCEDUREFILE || type == SETUPFILE ) {
136 buffer[filesize] =
'\n';
137 buffer[filesize+1] = 0;
140 buffer[filesize] = 0;
142 if ( name != filename ) M_free(name,
"FromLoadInputFile");
151 UBYTE ReadFromStream(
STREAM *stream)
156 if ( stream->type == PIPESTREAM ) {
160 RWLOCKR(AM.handlelock);
161 f = (FILE *)(filelist[stream->handle]);
162 UNRWLOCK(AM.handlelock);
164 if ( cc == EOF )
return(ENDOFSTREAM);
170 if ( PF.me == MASTER ) {
173 RWLOCKR(AM.handlelock);
174 f = (FILE *)filelist[stream->handle];
175 UNRWLOCK(AM.handlelock);
177 end = stream->
buffer + stream->buffersize;
195 stream->inbuffer = len;
196 stream->
top = stream->
buffer + stream->inbuffer;
197 if ( stream->
pointer == stream->
top )
return ENDOFSTREAM;
201 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
202 if ( c == LINEFEED ) stream->eqnum = 1;
207 #ifdef WITHEXTERNALCHANNEL
208 if ( stream->type == EXTERNALCHANNELSTREAM ) {
210 cc = getcFromExtChannel();
217 Error0(
"No current external channel");
223 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
224 if ( c == LINEFEED ) stream->eqnum = 1;
230 if ( stream->type != FILESTREAM )
return(ENDOFSTREAM);
231 if ( stream->fileposition != stream->bufferposition+stream->inbuffer ) {
232 stream->fileposition = stream->bufferposition+stream->inbuffer;
233 SETBASEPOSITION(scrpos,stream->fileposition);
234 SeekFile(stream->handle,&scrpos,SEEK_SET);
236 stream->bufferposition = stream->fileposition;
237 stream->inbuffer = ReadFile(stream->handle,
238 stream->
buffer,stream->buffersize);
239 if ( stream->inbuffer <= 0 )
return(ENDOFSTREAM);
240 stream->
top = stream->
buffer + stream->inbuffer;
242 stream->fileposition = stream->bufferposition + stream->inbuffer;
244 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
246 if ( c == LINEFEED ) stream->eqnum = 1;
255 UBYTE GetFromStream(
STREAM *stream)
258 if ( stream->isnextchar > 0 ) {
259 return(stream->nextchar[--stream->isnextchar]);
261 c1 = ReadFromStream(stream);
262 if ( c1 == LINEFEED || c1 == CARRIAGERETURN ) {
263 c2 = ReadFromStream(stream);
264 if ( c2 == c1 || ( c2 != LINEFEED && c2 != CARRIAGERETURN ) ) {
265 stream->isnextchar = 1;
266 stream->nextchar[0] = c2;
278 UBYTE LookInStream(
STREAM *stream)
280 UBYTE c = GetFromStream(stream);
281 UngetFromStream(stream,c);
290 STREAM *OpenStream(UBYTE *name,
int type,
int prevarmode,
int raiselow)
293 UBYTE *rhsofvariable, *s, *newname, c;
298 case REVERSEFILESTREAM:
306 handle = LocateFile(&newname,-1);
307 if ( handle < 0 )
return(0);
309 SeekFile(handle,&scrpos,SEEK_END);
310 TELLFILE(handle,&scrpos);
311 filesize = BASEPOSITION(scrpos);
313 SeekFile(handle,&scrpos,SEEK_SET);
314 if ( filesize > AM.MaxStreamSize && type == FILESTREAM )
315 filesize = AM.MaxStreamSize;
316 stream = CreateStream((UBYTE *)
"filestream");
320 stream->
buffer = (UBYTE *)Malloc1(filesize+1,
"name of input stream");
321 stream->inbuffer = ReadFile(handle,stream->
buffer,filesize);
322 if ( type == REVERSEFILESTREAM ) {
323 if ( ReverseStatements(stream) ) {
324 M_free(stream->
buffer,
"name of input stream");
328 stream->
top = stream->
buffer + stream->inbuffer;
330 stream->handle = handle;
331 stream->buffersize = filesize;
332 stream->fileposition = stream->inbuffer;
333 if ( newname != name ) stream->
name = newname;
334 else if ( name ) stream->
name = strDup1(name,
"name of input stream");
337 stream->prevline = stream->linenumber = 1;
341 if ( ( rhsofvariable = GetPreVar(name,WITHERROR) ) == 0 )
return(0);
342 stream = CreateStream((UBYTE *)
"var-stream");
346 stream->inbuffer = s - stream->
buffer;
347 stream->
name = AC.CurrentStream->name;
348 stream->linenumber = AC.CurrentStream->linenumber;
349 stream->prevline = AC.CurrentStream->prevline;
350 stream->eqnum = AC.CurrentStream->eqnum;
351 stream->
pname = strDup1(name,
"stream->pname");
352 stream->olddelay = AP.AllowDelay;
353 s = stream->
pname;
while ( *s ) s++;
354 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
359 if ( ( num = GetDollar(name) ) < 0 ) {
365 s = name;
while ( *s && *s !=
'[' ) s++;
366 if ( *s == 0 )
return(0);
368 if ( ( num = GetDollar(name) ) < 0 )
return(0);
371 if ( *s == 0 || FG.cTable[*s] != 1 || *s ==
']' ) {
372 MesPrint(
"@Illegal factor number for dollar variable");
375 while ( *s && FG.cTable[*s] == 1 ) {
376 numfac = 10*numfac+*s++-
'0';
378 if ( *s !=
']' || s[1] != 0 ) {
379 MesPrint(
"@Illegal factor number for $ variable");
382 stream = CreateStream((UBYTE *)
"dollar-stream");
383 stream->
buffer = stream->
pointer = s = WriteDollarFactorToBuffer(num,numfac,1);
386 stream = CreateStream((UBYTE *)
"dollar-stream");
387 stream->
buffer = stream->
pointer = s = WriteDollarToBuffer(num,1);
391 stream->inbuffer = s - stream->
buffer;
392 stream->
name = AC.CurrentStream->name;
393 stream->linenumber = AC.CurrentStream->linenumber;
394 stream->prevline= AC.CurrentStream->prevline;
395 stream->eqnum = AC.CurrentStream->eqnum;
396 stream->
pname = strDup1(name,
"stream->pname");
397 s = stream->
pname;
while ( *s ) s++;
398 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
401 AO.DollarOutSizeBuffer = 0;
402 AO.DollarOutBuffer = 0;
403 AO.DollarInOutBuffer = 0;
409 stream = CreateStream((UBYTE *)
"calculator");
413 stream->inbuffer = s - stream->
buffer;
414 stream->
name = AC.CurrentStream->name;
415 stream->linenumber = AC.CurrentStream->linenumber;
416 stream->prevline = AC.CurrentStream->prevline;
421 stream = CreateStream((UBYTE *)
"pipe");
425 if ( ( f = popen((
char *)name,
"r") ) == 0 ) {
426 Error0(
"@Cannot create pipe");
428 stream->handle = CreateHandle();
429 RWLOCKW(AM.handlelock);
430 filelist[stream->handle] = (FILES *)f;
431 UNRWLOCK(AM.handlelock);
434 stream->inbuffer = 0;
439 if ( PF.me == MASTER ) {
440 f = popen((
char *)name,
"r");
442 if ( f == 0 ) Error0(
"@Cannot create pipe");
448 stream->handle = CreateHandle();
449 RWLOCKW(AM.handlelock);
450 filelist[stream->handle] = (FILES *)f;
451 UNRWLOCK(AM.handlelock);
454 stream->buffersize = AM.MaxStreamSize;
455 stream->
buffer = (UBYTE *)Malloc1(stream->buffersize,
"pipe buffer");
456 stream->inbuffer = 0;
460 stream->
name = strDup1((UBYTE *)
"pipe",
"pipe");
461 stream->prevline = stream->linenumber = 1;
466 #ifdef WITHEXTERNALCHANNEL
467 case EXTERNALCHANNELSTREAM:
470 if( (n=getCurrentExternalChannel()) == 0 )
471 Error0(
"@No current extrenal channel");
472 stream = CreateStream((UBYTE *)
"externalchannel");
473 stream->handle = CreateHandle();
474 tmpn = (
int *)Malloc1(
sizeof(
int),
"external channel handle");
476 RWLOCKW(AM.handlelock);
477 filelist[stream->handle] = (FILES *)tmpn;
478 UNRWLOCK(AM.handlelock);
481 stream->inbuffer = 0;
482 stream->
name = strDup1((UBYTE *)
"externalchannel",
"externalchannel");
483 stream->prevline = stream->linenumber = 1;
491 stream->bufferposition = 0;
492 stream->isnextchar = 0;
494 stream->previousNoShowInput = AC.NoShowInput;
495 stream->afterwards = raiselow;
496 if ( AC.CurrentStream ) stream->previous = AC.CurrentStream - AC.Streams;
497 else stream->previous = -1;
499 if ( prevarmode == 0 ) stream->prevars = -1;
500 else if ( prevarmode > 0 ) stream->prevars = NumPre;
501 else if ( prevarmode < 0 ) stream->prevars = -prevarmode-1;
502 AC.CurrentStream = stream;
503 if ( type == PREREADSTREAM || type == PREREADSTREAM3 || type == PRECALCSTREAM
504 || type == DOLLARSTREAM ) AC.NoShowInput = 1;
513 int LocateFile(UBYTE **name,
int type)
515 int handle, namesize, i;
516 UBYTE *s, *to, *u1, *u2, *newname, *indir;
517 handle = OpenFile((
char *)(*name));
518 if ( handle >= 0 )
return(handle);
519 if ( type == SETUPFILE && AM.SetupFile ) {
520 handle = OpenFile((
char *)(AM.SetupFile));
521 if ( handle >= 0 )
return(handle);
522 MesPrint(
"Could not open setup file %s",(
char *)(AM.SetupFile));
524 namesize = 4; s = *name;
525 while ( *s ) { s++; namesize++; }
526 if ( type == SETUPFILE ) indir = AM.SetupDir;
527 else indir = AM.IncDir;
531 while ( *s ) { s++; i++; }
532 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
533 s = indir; to = newname;
534 while ( *s ) *to++ = *s++;
535 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
537 while ( *s ) *to++ = *s++;
539 handle = OpenFile((
char *)newname);
544 M_free(newname,
"LocateFile, incdir/file");
546 if ( type == SETUPFILE ) {
547 handle = OpenFile(setupfilename);
548 if ( handle >= 0 )
return(handle);
549 s = (UBYTE *)getenv(
"FORMSETUP");
551 handle = OpenFile((
char *)s);
552 if ( handle >= 0 )
return(handle);
553 MesPrint(
"Could not open setup file %s",s);
556 if ( type != SETUPFILE && AM.Path ) {
561 while ( *u1 && *u1 !=
';' ) {
565 while ( *u1 && *u1 !=
':' ) {
566 if ( *u1 ==
'\\' ) u1++;
570 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
571 s = u2; to = newname;
574 if ( *s ==
'\\' ) s++;
578 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
580 while ( *s ) *to++ = *s++;
582 handle = OpenFile((
char *)newname);
587 M_free(newname,
"LocateFile Path/file");
591 if ( type != SETUPFILE ) Error1(
"LocateFile: Cannot find file",*name);
602 int newstr = stream->previous, sgn;
603 UBYTE *t, numbuf[24];
606 M_free(stream->
FoldName,
"stream->FoldName");
609 if ( stream->type == FILESTREAM || stream->type == REVERSEFILESTREAM ) {
610 CloseFile(stream->handle);
611 if ( stream->
buffer != 0 ) M_free(stream->
buffer,
"name of input stream");
615 else if ( stream->type == PIPESTREAM ) {
616 RWLOCKW(AM.handlelock);
618 if ( PF.me == MASTER )
620 pclose((FILE *)(filelist[stream->handle]));
621 filelist[stream->handle] = 0;
623 UNRWLOCK(AM.handlelock);
625 if ( stream->
buffer != 0 ) {
626 M_free(stream->
buffer,
"pipe buffer");
633 #ifdef WITHEXTERNALCHANNEL
634 else if ( stream->type == EXTERNALCHANNELSTREAM ) {
636 RWLOCKW(AM.handlelock);
637 tmpn = (
int *)(filelist[stream->handle]);
638 filelist[stream->handle] = 0;
640 UNRWLOCK(AM.handlelock);
641 M_free(tmpn,
"external channel handle");
645 else if ( stream->type == PREVARSTREAM && (
646 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
647 t = stream->
buffer; x = 0; sgn = 1;
648 while ( *t ==
'-' || *t ==
'+' ) {
649 if ( *t ==
'-' ) sgn = -sgn;
652 if ( FG.cTable[*t] == 1 ) {
653 while ( *t && FG.cTable[*t] == 1 ) x = 10*x + *t++ -
'0';
655 if ( stream->afterwards == PRERAISEAFTER ) x = sgn*x + 1;
662 else if ( stream->type == DOLLARSTREAM && (
663 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
664 if ( stream->afterwards == PRERAISEAFTER ) x = 1;
666 DollarRaiseLow(stream->
pname,x);
668 else if ( stream->type == PRECALCSTREAM || stream->type == DOLLARSTREAM ) {
669 if ( stream->
buffer ) M_free(stream->
buffer,
"stream->buffer");
672 if ( stream->
name && stream->type != PREVARSTREAM
673 && stream->type != PREREADSTREAM && stream->type != PREREADSTREAM2 && stream->type != PREREADSTREAM3
674 && stream->type != PRECALCSTREAM && stream->type != DOLLARSTREAM ) {
675 M_free(stream->
name,
"stream->name");
679 AC.NoShowInput = stream->previousNoShowInput;
685 if ( stream->prevars >= 0 ) {
686 while ( NumPre > stream->prevars ) {
688 M_free(PreVar[NumPre].name,
"PreVar[NumPre].name");
689 PreVar[NumPre].name = PreVar[NumPre].value = 0;
692 if ( stream->type == PREVARSTREAM ) {
693 AP.AllowDelay = stream->olddelay;
694 ClearMacro(stream->
pname);
695 M_free(stream->
pname,
"stream->pname");
697 else if ( stream->type == DOLLARSTREAM ) {
698 M_free(stream->
pname,
"stream->pname");
701 if ( newstr >= 0 )
return(AC.Streams + newstr);
710 STREAM *CreateStream(UBYTE *where)
715 if ( AC.NumStreams >= AC.MaxNumStreams ) {
716 if ( AC.MaxNumStreams == 0 ) numnewstreams = 10;
717 else numnewstreams = 2*AC.MaxNumStreams;
718 newstreams = (
STREAM *)Malloc1(
sizeof(
STREAM)*(numnewstreams+1),
"CreateStream");
719 if ( AC.MaxNumStreams > 0 ) {
720 offset = AC.CurrentStream - AC.Streams;
721 for ( i = 0; i < AC.MaxNumStreams; i++ ) {
722 newstreams[i] = AC.Streams[i];
724 AC.CurrentStream = newstreams + offset;
726 else newstreams[0].previous = -1;
727 AC.MaxNumStreams = numnewstreams;
728 if ( AC.Streams ) M_free(AC.Streams,(
char *)where);
729 AC.Streams = newstreams;
731 newstreams = AC.Streams+AC.NumStreams++;
732 newstreams->
name = 0;
741 LONG GetStreamPosition(
STREAM *stream)
743 return(stream->bufferposition + ((LONG)stream->
pointer-(LONG)stream->
buffer));
751 VOID PositionStream(
STREAM *stream, LONG position)
754 if ( position >= stream->bufferposition
755 && position < stream->bufferposition + stream->inbuffer ) {
756 stream->
pointer = stream->
buffer + (position-stream->bufferposition);
758 else if ( stream->type == FILESTREAM ) {
759 SETBASEPOSITION(scrpos,position);
760 SeekFile(stream->handle,&scrpos,SEEK_SET);
761 stream->inbuffer = ReadFile(stream->handle,stream->
buffer,stream->buffersize);
763 stream->
top = stream->
buffer + stream->inbuffer;
764 stream->bufferposition = position;
765 stream->fileposition = position + stream->inbuffer;
766 stream->isnextchar = 0;
769 Error0(
"Illegal position for stream");
783 int ReverseStatements(
STREAM *stream)
785 UBYTE *spare = (UBYTE *)Malloc1((stream->inbuffer+1)*
sizeof(UBYTE),
"Reverse copy");
786 UBYTE *top = stream->
buffer + stream->inbuffer, *in, *s, *ss, *out;
787 out = spare+stream->inbuffer+1;
791 if ( *s == AP.ComChar ) {
794 if ( s == top ) { *--out =
'\n';
break; }
798 irrend: MesPrint(
"@Irregular end of reverse include file.");
802 else if ( *s ==
'\n' ) {
804 while ( ss > in ) *--out = *--ss;
806 if ( out[0] == AP.ComChar && ss+6 < s && out[3] ==
'#' ) {
810 if ( out[4] ==
'[' ) out[4] =
']';
811 else if ( out[4] ==
']' ) out[4] =
'[';
819 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
837 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
838 while ( s < top && *s ==
'\n' ) s++;
839 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
841 while ( ss > in ) *--out = *--ss;
845 else if ( *s ==
'"' ) {
848 if ( *s ==
'"' )
break;
849 if ( *s ==
'\\' ) { s++; }
852 if ( s >= top )
goto irrend;
854 else if ( *s ==
'\\' ) {
856 if ( s >= top )
goto irrend;
861 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
863 while ( ss > in ) *--out = *--ss;
867 if ( out == spare ) stream->inbuffer++;
868 if ( out > spare+1 ) {
869 MesPrint(
"@Internal error in #reverseinclude instruction.");
872 memcpy((
void *)(stream->
buffer),(
void *)out,(
size_t)(stream->inbuffer*
sizeof(UBYTE)));
873 M_free(spare,
"Reverse copy");
886 int i = CreateHandle();
887 filelist[i] = Ustdout;
892 AR.Fscr[0].handle = -1;
893 AR.Fscr[1].handle = -1;
894 AR.Fscr[2].handle = -1;
895 AR.FoStage4[0].handle = -1;
896 AR.FoStage4[1].handle = -1;
897 AR.infile = &(AR.Fscr[0]);
898 AR.outfile = &(AR.Fscr[1]);
899 AR.hidefile = &(AR.Fscr[2]);
900 AR.StoreData.Handle = -1;
903 AC.MaxNumStreams = 0;
911 int OpenFile(
char *name)
916 if ( ( f = Uopen(name,
"rb") ) == 0 )
return(-1);
919 RWLOCKW(AM.handlelock);
921 UNRWLOCK(AM.handlelock);
930 int OpenAddFile(
char *name)
935 if ( ( f = Uopen(name,
"a+b") ) == 0 )
return(-1);
938 RWLOCKW(AM.handlelock);
940 UNRWLOCK(AM.handlelock);
942 SeekFile(i,&scrpos,SEEK_SET);
951 int ReOpenFile(
char *name)
956 if ( ( f = Uopen(name,
"r+b") ) == 0 )
return(-1);
958 RWLOCKW(AM.handlelock);
960 UNRWLOCK(AM.handlelock);
962 SeekFile(i,&scrpos,SEEK_SET);
971 int CreateFile(
char *name)
975 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
977 RWLOCKW(AM.handlelock);
979 UNRWLOCK(AM.handlelock);
988 int CreateLogFile(
char *name)
992 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
995 RWLOCKW(AM.handlelock);
997 UNRWLOCK(AM.handlelock);
1006 VOID CloseFile(
int handle)
1008 if ( handle >= 0 ) {
1010 RWLOCKW(AM.handlelock);
1011 f = filelist[handle];
1012 filelist[handle] = 0;
1014 UNRWLOCK(AM.handlelock);
1031 #define COPYFILEBUFSIZE 40960L
1033 size_t countin, countout, sumcount;
1034 char *buffer = NULL;
1036 sumcount = (AM.S0->LargeSize+AM.S0->SmallEsize)*
sizeof(WORD);
1037 if ( sumcount <= COPYFILEBUFSIZE ) {
1038 sumcount = COPYFILEBUFSIZE;
1039 buffer = (
char*)Malloc1(sumcount,
"file copy buffer");
1042 buffer = (
char *)(AM.S0->lBuffer);
1045 in = fopen(source,
"rb");
1047 perror(
"CopyFile: ");
1050 out = fopen(dest,
"wb");
1051 if ( out == NULL ) {
1052 perror(
"CopyFile: ");
1056 while ( !feof(in) ) {
1057 countin = fread(buffer, 1, sumcount, in);
1058 if ( countin != sumcount ) {
1060 perror(
"CopyFile: ");
1064 countout = fwrite(buffer, 1, countin, out);
1065 if ( countin != countout ) {
1066 perror(
"CopyFile: ");
1073 if ( sumcount <= COPYFILEBUFSIZE ) {
1074 M_free(buffer,
"file copy buffer");
1094 RWLOCKW(AM.handlelock);
1096 if ( filelistsize == 0 ) {
1098 filelist = (FILES **)Malloc1(
sizeof(FILES *)*filelistsize,
"file handle");
1099 for ( j = 0; j < filelistsize; j++ ) filelist[j] = 0;
1103 else if ( numinfilelist >= filelistsize ) {
1104 VOID **fl = (VOID **)filelist;
1106 if ( DoubleList((VOID ***)(&fl),&filelistsize,(
int)
sizeof(FILES *),
1107 "list of open files") != 0 ) Terminate(-1);
1108 filelist = (FILES **)fl;
1109 for ( j = i; j < filelistsize; j++ ) filelist[j] = 0;
1110 numinfilelist = i + 1;
1114 for ( j = 0; j < filelistsize; j++ ) {
1115 if ( filelist[j] == 0 ) { i = j;
break; }
1119 filelist[i] = (FILES *)(filelist);
1125 if ( numinfilelist > MAX_OPEN_FILES ) {
1127 UNRWLOCK(AM.handlelock);
1129 MesPrint(
"More than %d open files",MAX_OPEN_FILES);
1130 Error0(
"System limit. This limit is not due to FORM!");
1134 UNRWLOCK(AM.handlelock);
1145 LONG ReadFile(
int handle, UBYTE *buffer, LONG size)
1152 RWLOCKR(AM.handlelock);
1153 f = filelist[handle];
1154 UNRWLOCK(AM.handlelock);
1158 r = Uread(b,1,size,f);
1159 if ( r < 0 )
return(r);
1160 if ( r == 0 )
return(inbuf);
1162 if ( r == size )
return(inbuf);
1163 if ( r > size )
return(-1);
1199 WORD *b = (WORD *)buffer, *t;
1201 if ( fi->handle < 0 ) {
1202 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + BASEPOSITION(*pos));
1204 while ( size > 0 && fi->POfill < fi->POfull ) { *b++ = *t++; size--; }
1207 if ( ISLESSPOS(*pos,fi->POposition) || ISGEPOSINC(*pos,fi->POposition,
1208 ((UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer))) ) {
1213 fi->POposition = *pos;
1214 LOCK(AS.inputslock);
1215 SeekFile(fi->handle,pos,SEEK_SET);
1216 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1217 UNLOCK(AS.inputslock);
1218 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1219 fi->POfill = fi->PObuffer;
1220 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1221 else AR.InHiBuf = retval/
sizeof(WORD);
1224 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + DIFBASE(*pos,fi->POposition));
1226 if ( fi->POfill + size <= fi->POfull ) {
1228 while ( size > 0 ) { *b++ = *t++; size--; }
1232 i = fi->POfull - fi->POfill; t = fi->POfill;
1233 if ( i > size ) i = size;
1235 while ( --i >= 0 ) *b++ = *t++;
1236 if ( size == 0 )
break;
1237 ADDPOS(fi->POposition,(UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer));
1238 LOCK(AS.inputslock);
1239 SeekFile(fi->handle,&(fi->POposition),SEEK_SET);
1240 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1241 UNLOCK(AS.inputslock);
1242 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1243 fi->POfill = fi->PObuffer;
1244 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1245 else AR.InHiBuf = retval/
sizeof(WORD);
1246 if ( retval == 0 ) { t = fi->POfill;
break; }
1250 retval = (UBYTE *)b - buffer;
1252 ADDPOS(*pos,retval);
1261 LONG WriteFileToFile(
int handle, UBYTE *buffer, LONG size)
1264 LONG retval, totalwritten = 0, stilltowrite;
1265 RWLOCKR(AM.handlelock);
1266 f = filelist[handle];
1267 UNRWLOCK(AM.handlelock);
1268 while ( totalwritten < size ) {
1269 stilltowrite = size - totalwritten;
1273 retval = Uwrite((
char *)buffer+totalwritten,1,stilltowrite,f);
1274 if ( retval < 0 )
return(retval);
1275 if ( retval == 0 )
return(totalwritten);
1276 totalwritten += retval;
1281 return(totalwritten);
1285 WRITEFILE WriteFile = &WriteFileToFile;
1299 VOID SeekFile(
int handle,
POSITION *offset,
int origin)
1302 RWLOCKR(AM.handlelock);
1303 f = filelist[handle];
1304 UNRWLOCK(AM.handlelock);
1308 if ( origin == SEEK_SET ) {
1309 Useek(f,BASEPOSITION(*offset),origin);
1310 SETBASEPOSITION(*offset,(Utell(f)));
1313 else if ( origin == SEEK_END ) {
1316 SETBASEPOSITION(*offset,(Utell(f)));
1324 LONG TellFile(
int handle)
1327 TELLFILE(handle,&pos);
1331 return(BASEPOSITION(pos));
1334 VOID TELLFILE(
int handle,
POSITION *position)
1337 RWLOCKR(AM.handlelock);
1338 f = filelist[handle];
1339 UNRWLOCK(AM.handlelock);
1340 SETBASEPOSITION(*position,(Utell(f)));
1348 void FlushFile(
int handle)
1351 RWLOCKR(AM.handlelock);
1352 f = filelist[handle];
1353 UNRWLOCK(AM.handlelock);
1362 int GetPosFile(
int handle, fpos_t *pospointer)
1365 RWLOCKR(AM.handlelock);
1366 f = filelist[handle];
1367 UNRWLOCK(AM.handlelock);
1368 return(Ugetpos(f,pospointer));
1376 int SetPosFile(
int handle, fpos_t *pospointer)
1379 RWLOCKR(AM.handlelock);
1380 f = filelist[handle];
1381 UNRWLOCK(AM.handlelock);
1382 return(Usetpos(f,(fpos_t *)pospointer));
1396 VOID SynchFile(
int handle)
1399 if ( handle >= 0 ) {
1400 RWLOCKR(AM.handlelock);
1401 f = filelist[handle];
1402 UNRWLOCK(AM.handlelock);
1418 VOID TruncateFile(
int handle)
1421 if ( handle >= 0 ) {
1422 RWLOCKR(AM.handlelock);
1423 f = filelist[handle];
1424 UNRWLOCK(AM.handlelock);
1438 int GetChannel(
char *name,
int mode)
1443 for ( i = 0; i < NumOutputChannels; i++ ) {
1444 if ( channels[i].name == 0 )
continue;
1445 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1448 MesPrint(
"&File %s in print statement is not open",name);
1449 MesPrint(
" You should open it first with a #write or #append instruction");
1452 for ( i = 0; i < NumOutputChannels; i++ ) {
1453 if ( channels[i].name == 0 )
break;
1455 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1456 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1457 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1458 ch->
handle = CreateFile(name);
1459 RWLOCKR(AM.handlelock);
1460 f = filelist[ch->
handle];
1461 UNRWLOCK(AM.handlelock);
1474 int GetAppendChannel(
char *name)
1479 for ( i = 0; i < NumOutputChannels; i++ ) {
1480 if ( channels[i].name == 0 )
continue;
1481 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1483 for ( i = 0; i < NumOutputChannels; i++ ) {
1484 if ( channels[i].name == 0 )
break;
1486 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1487 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1488 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1489 ch->
handle = OpenAddFile(name);
1490 RWLOCKR(AM.handlelock);
1491 f = filelist[ch->
handle];
1492 UNRWLOCK(AM.handlelock);
1504 int CloseChannel(
char *name)
1507 for ( i = 0; i < NumOutputChannels; i++ ) {
1508 if ( channels[i].name == 0 )
continue;
1509 if ( channels[i].name[0] == 0 )
continue;
1510 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 ) {
1511 CloseFile(channels[i].handle);
1512 M_free(channels[i].name,
"CloseChannel");
1513 channels[i].name = 0;
1538 void UpdateMaxSize()
1546 if ( PF.me != MASTER )
return;
1549 if ( AM.PrintTotalSize ) {
1554 scr = AB[0]->R.Fscr;
1558 for ( i = 0; i <=2; i++ ) {
1559 if ( scr[i].handle < 0 ) {
1560 SETBASEPOSITION(position,(scr[i].POfull-scr[i].PObuffer)*
sizeof(WORD));
1563 position = scr[i].filesize;
1565 ADD2POS(sumsize,position);
1574 for ( j = 0; j < AM.totalnumberofthreads; j++ ) {
1576 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1577 position = AT.SS->file.filesize;
1583 ADD2POS(sumsize,position);
1585 if ( AR.FoStage4[0].handle >= 0 ) {
1586 position = AR.FoStage4[0].filesize;
1587 ADD2POS(sumsize,position);
1592 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1593 position = AT.SS->file.filesize;
1594 ADD2POS(sumsize,position);
1596 if ( AR.FoStage4[0].handle >= 0 ) {
1597 position = AR.FoStage4[0].filesize;
1598 ADD2POS(sumsize,position);
1604 ADD2POS(sumsize,AC.StoreFileSize);
1608 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) {
1610 LOCK(AS.MaxExprSizeLock);
1611 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) AS.MaxExprSize = sumsize;
1612 UNLOCK(AS.MaxExprSizeLock);
1614 AS.MaxExprSize = sumsize;
1628 int StrCmp(UBYTE *s1, UBYTE *s2)
1630 while ( *s1 && *s1 == *s2 ) { s1++; s2++; }
1631 return((
int)*s1-(
int)*s2);
1639 int StrICmp(UBYTE *s1, UBYTE *s2)
1641 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1642 return((
int)tolower(*s1)-(
int)tolower(*s2));
1650 int StrHICmp(UBYTE *s1, UBYTE *s2)
1652 while ( *s1 && tolower(*s1) == *s2 ) { s1++; s2++; }
1653 return((
int)tolower(*s1)-(
int)(*s2));
1661 int StrICont(UBYTE *s1, UBYTE *s2)
1663 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1664 if ( *s1 == 0 )
return(0);
1665 return((
int)tolower(*s1)-(
int)tolower(*s2));
1673 int CmpArray(WORD *t1, WORD *t2, WORD n)
1676 for ( i = 0; i < n; i++ ) {
1677 if ( ( x = (
int)(t1[i]-t2[i]) ) != 0 )
return(x);
1687 int ConWord(UBYTE *s1, UBYTE *s2)
1689 while ( *s1 && ( tolower(*s1) == tolower(*s2) ) ) { s1++; s2++; }
1690 if ( *s1 == 0 )
return(1);
1699 int StrLen(UBYTE *s)
1702 while ( *s ) { s++; i++; }
1711 VOID NumToStr(UBYTE *s, LONG x)
1716 if ( x < 0 ) { *s++ =
'-'; xx = -x; }
1719 *t++ = xx % 10 +
'0';
1722 while ( t > str ) *s++ = *--t;
1735 VOID WriteString(
int type, UBYTE *str,
int num)
1739 if ( num > 0 && str[num-1] == 0 ) { num--; }
1740 else if ( num <= 0 || str[num-1] != LINEFEED ) {
1741 AddLineFeed(str,num);
1744 if(type == EXTERNALCHANNELOUT){
1745 if(WriteFile(0,str,num) != num) error = 1;
1748 if ( AM.silent == 0 || type == ERROROUT ) {
1749 if ( type == INPUTOUT ) {
1750 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1751 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1753 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1754 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1756 if ( error ) Terminate(-1);
1769 VOID WriteUnfinString(
int type, UBYTE *str,
int num)
1774 if(type == EXTERNALCHANNELOUT){
1775 if(WriteFile(0,str,num) != num) error = 1;
1778 if ( AM.silent == 0 || type == ERROROUT ) {
1779 if ( type == INPUTOUT ) {
1780 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1781 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1783 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1784 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1786 if ( error ) Terminate(-1);
1794 UBYTE *AddToString(UBYTE *outstring, UBYTE *extrastring,
int par)
1796 UBYTE *s = extrastring, *t, *newstring;
1798 while ( *s ) { s++; }
1800 if ( outstring == 0 ) {
1802 t = outstring = (UBYTE *)Malloc1(n+1,
"AddToString");
1811 t = newstring = (UBYTE *)Malloc1(n+nn+2,
"AddToString");
1814 if ( par == 1 ) *t++ = ',';
1818 M_free(outstring,"AddToString");
1832 UBYTE *strDup1(UBYTE *instring,
char *ifwrong)
1834 UBYTE *s = instring, *to;
1836 to = s = (UBYTE *)Malloc1((s-instring)+1,ifwrong);
1837 while ( *instring ) *to++ = *instring++;
1847 UBYTE *EndOfToken(UBYTE *s)
1850 while ( ( c = (UBYTE)(FG.cTable[*s]) ) == 0 || c == 1 ) s++;
1859 UBYTE *ToToken(UBYTE *s)
1862 while ( *s && ( c = (UBYTE)(FG.cTable[*s]) ) != 0 && c != 1 ) s++;
1874 UBYTE *SkipField(UBYTE *s,
int level)
1877 if ( *s ==
',' && level == 0 )
return(s);
1878 if ( *s ==
'(' ) level++;
1879 else if ( *s ==
')' ) { level--;
if ( level < 0 ) level = 0; }
1880 else if ( *s ==
'[' ) {
1883 else if ( *s ==
'{' ) {
1901 WORD ReadSnum(UBYTE **p)
1906 if ( FG.cTable[*s] == 1 ) {
1908 x = ( x << 3 ) + ( x << 1 ) + ( *s++ -
'0' );
1909 if ( x > MAXPOSITIVE )
return(-1);
1910 }
while ( FG.cTable[*s] == 1 );
1925 UBYTE *NumCopy(WORD y, UBYTE *to)
1935 do { *s++ = (UBYTE)((x % 10)+
'0'); i++; }
while ( ( x /= 10 ) != 0 );
1939 i = to[j]; to[j] = s[-j]; s[-j] = (UBYTE)i; j--;
1952 char *LongCopy(LONG y,
char *to)
1962 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
1966 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
1979 char *LongLongCopy(off_t *y,
char *to)
1992 if ( x < 0 ) { x = -x; *to++ =
'-'; }
1994 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
1998 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
2014 static char notime[] =
"";
2023 return((UBYTE *)ctime(&tp));
2028 return((UBYTE *)ctime(&tp));
2030 return((UBYTE *)notime);
2044 case 0:
return(set->bit_0);
2045 case 1:
return(set->bit_1);
2046 case 2:
return(set->bit_2);
2047 case 3:
return(set->bit_3);
2048 case 4:
return(set->bit_4);
2049 case 5:
return(set->bit_5);
2050 case 6:
return(set->bit_6);
2051 case 7:
return(set->bit_7);
2065 case 0: set->bit_0=1;
break;
2066 case 1: set->bit_1=1;
break;
2067 case 2: set->bit_2=1;
break;
2068 case 3: set->bit_3=1;
break;
2069 case 4: set->bit_4=1;
break;
2070 case 5: set->bit_5=1;
break;
2071 case 6: set->bit_6=1;
break;
2072 case 7: set->bit_7=1;
break;
2086 case 0: set->bit_0=0;
break;
2087 case 1: set->bit_1=0;
break;
2088 case 2: set->bit_2=0;
break;
2089 case 3: set->bit_3=0;
break;
2090 case 4: set->bit_4=0;
break;
2091 case 5: set->bit_5=0;
break;
2092 case 6: set->bit_6=0;
break;
2093 case 7: set->bit_7=0;
break;
2110 case 0: set->bit_0=(set1->bit_0&&(!set2->bit_0));
break;
2111 case 1: set->bit_1=(set1->bit_1&&(!set2->bit_1));
break;
2112 case 2: set->bit_2=(set1->bit_2&&(!set2->bit_2));
break;
2113 case 3: set->bit_3=(set1->bit_3&&(!set2->bit_3));
break;
2114 case 4: set->bit_4=(set1->bit_4&&(!set2->bit_4));
break;
2115 case 5: set->bit_5=(set1->bit_5&&(!set2->bit_5));
break;
2116 case 6: set->bit_6=(set1->bit_6&&(!set2->bit_6));
break;
2117 case 7: set->bit_7=(set1->bit_7&&(!set2->bit_7));
break;
2118 case 8: set++;set1++;set2++;
2131 #ifdef MALLOCPROTECT
2132 if ( mprotectInit() ) exit(0);
2145 char *dummymessage =
"Malloc";
2149 VOID *Malloc(LONG size)
2158 MesPrint(
"Asking for 0 bytes in Malloc");
2161 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2165 mem = (VOID *)M_alloc(size);
2168 MLOCK(ErrorMessageLock);
2170 Error0(
"No memory!");
2172 MUNLOCK(ErrorMessageLock);
2182 mallocsizes[nummalloclist] = size;
2183 mallocstrings[nummalloclist] = dummymessage;
2184 malloclist[nummalloclist++] = mem;
2185 if ( filelist ) MesPrint(
"Mem0 at 0x%x, %l bytes",mem,size);
2187 int i = nummalloclist-1;
2188 while ( --i >= 0 ) {
2189 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2190 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2191 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x"
2198 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2201 int j = nummalloclist-1, i;
2202 while ( --j >= 0 ) {
2203 t = (
char *)(malloclist[j]);
2204 u = t + mallocsizes[j];
2205 for ( i = 0; i < (int)BANNER; i++ ) {
2207 if ( *t != FILLVALUE || *u != FILLVALUE ) {
2208 MesPrint(
"Writing outside memory for %s",malloclist[i]);
2231 VOID *Malloc1(LONG size,
const char *messageifwrong)
2240 MesPrint(
"%wAsking for 0 bytes in Malloc1");
2246 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2250 mem = (VOID *)M_alloc(size);
2253 MLOCK(ErrorMessageLock);
2255 Error1(
"No memory while allocating ",(UBYTE *)messageifwrong);
2257 MUNLOCK(ErrorMessageLock);
2267 mallocsizes[nummalloclist] = size;
2268 mallocstrings[nummalloclist] = (
char *)messageifwrong;
2269 malloclist[nummalloclist++] = mem;
2270 if ( AC.MemDebugFlag && filelist ) MesPrint(
"%wMem1 at 0x%x: %l bytes. %s",mem,size,messageifwrong);
2272 int i = nummalloclist-1;
2273 while ( --i >= 0 ) {
2274 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2275 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2276 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x"
2282 #ifdef MALLOCDEBUGOUTPUT
2283 printf (
"Malloc1: %s, allocated %li bytes at %.8lx\n",messageifwrong,size,(
unsigned long)mem);
2289 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2310 void M_free(VOID *x,
const char *where)
2313 char *t = (
char *)x;
2316 x = (
void *)(((
char *)x)-BANNER);
2318 if ( AC.MemDebugFlag ) MesPrint(
"%wFreeing 0x%x: %s",x,where);
2320 for ( i = nummalloclist-1; i >= 0; i-- ) {
2321 if ( x == malloclist[i] ) {
2322 size = mallocsizes[i];
2323 for ( j = i+1; j < nummalloclist; j++ ) {
2324 malloclist[j-1] = malloclist[j];
2325 mallocsizes[j-1] = mallocsizes[j];
2326 mallocstrings[j-1] = mallocstrings[j];
2333 unsigned int xx = ((ULONG)x);
2334 printf(
"Error returning non-allocated address: 0x%x from %s\n"
2341 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2342 if ( *--t != FILLVALUE ) j++;
2345 LONG *tt = (LONG *)x;
2346 MesPrint(
"%w!!!!! Banner has been written in !!!!!: %x %x %x %x",
2347 tt[0],tt[1],tt[2],tt[3]);
2350 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2351 if ( *--t != FILLVALUE ) j++;
2354 LONG *tt = (LONG *)x;
2355 MesPrint(
"%w!!!!! Tail has been written in !!!!!: %x %x %x %x",
2356 tt[0],tt[1],tt[2],tt[3]);
2369 #ifdef MALLOCDEBUGOUTPUT
2370 printf (
"M_free: %s, memory freed at %.8lx\n",where,(
unsigned long)x);
2374 #ifdef MALLOCPROTECT
2375 mprotectFree((
void *)x);
2389 void M_check1() { MesPrint(
"Checking Malloc"); M_check(); }
2393 int i,j,k,error = 0;
2396 for ( i = 0; i < nummalloclist; i++ ) {
2397 t = (
char *)(malloclist[i]);
2398 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2399 if ( *t++ != FILLVALUE ) j++;
2402 tt = (LONG *)(malloclist[i]);
2403 MesPrint(
"%w!!!!! Banner %d (%s) has been written in !!!!!: %x %x %x %x",
2404 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2405 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2408 t = (
char *)(malloclist[i]) + mallocsizes[i];
2409 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2410 if ( *--t != FILLVALUE ) j++;
2414 MesPrint(
"%w!!!!! Tail %d (%s) has been written in !!!!!: %x %x %x %x",
2415 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2416 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2419 if ( ( mallocstrings[i][0] ==
' ' ) || ( mallocstrings[i][0] ==
'#' ) ) {
2420 MesPrint(
"%w!!!!! Funny mallocstring");
2435 MesPrint(
"We have the following memory allocations left:");
2436 for ( i = 0; i < nummalloclist; i++ ) {
2437 MesPrint(
"0x%x: %l bytes. number %d: '%s'",malloclist[i],mallocsizes[i],i,mallocstrings[i]);
2474 #define TERMMEMSTARTNUM 16
2475 #define TERMEXTRAWORDS 10
2477 VOID TermMallocAddMemory(PHEAD0)
2482 else extra = AT.TermMemMax;
2483 if ( AT.TermMemHeap ) M_free(AT.TermMemHeap,
"TermMalloc");
2484 newbufs = (WORD *)Malloc1(extra*(AM.MaxTer+TERMEXTRAWORDS*
sizeof(WORD)),
"TermMalloc");
2485 AT.TermMemHeap = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2486 for ( i = 0; i < extra; i++ ) {
2487 AT.TermMemHeap[i] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2489 #ifdef TERMMALLOCDEBUG
2490 DebugHeap2 = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2491 for ( i = 0; i < AT.TermMemMax; i++ ) { DebugHeap2[i] = DebugHeap1[i]; }
2492 for ( i = 0; i < extra; i++ ) {
2493 DebugHeap2[i+AT.TermMemMax] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2495 if ( DebugHeap1 ) M_free(DebugHeap1,
"TermMalloc");
2496 DebugHeap1 = DebugHeap2;
2498 AT.TermMemTop = extra;
2499 AT.TermMemMax += extra;
2500 #ifdef TERMMALLOCDEBUG
2501 MesPrint(
"AT.TermMemMax is now %l",AT.TermMemMax);
2505 #ifndef MEMORYMACROS
2507 WORD *TermMalloc2(PHEAD
char *text)
2509 if ( AT.TermMemTop <= 0 ) TermMallocAddMemory(BHEAD0);
2511 #ifdef TERMMALLOCDEBUG
2512 MesPrint(
"TermMalloc: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2515 #ifdef MALLOCDEBUGOUTPUT
2516 MesPrint(
"TermMalloc: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,AT.TermMemHeap[AT.TermMemTop-1]);
2520 return(AT.TermMemHeap[--AT.TermMemTop]);
2523 VOID TermFree2(PHEAD WORD *TermMem,
char *text)
2525 #ifdef TERMMALLOCDEBUG
2529 for ( i = 0; i < AT.TermMemMax; i++ ) {
2530 if ( TermMem == DebugHeap1[i] )
break;
2532 if ( i >= AT.TermMemMax ) {
2533 MesPrint(
" ERROR: TermFree called with an address not given by TermMalloc.");
2538 AT.TermMemHeap[AT.TermMemTop++] = TermMem;
2540 #ifdef TERMMALLOCDEBUG
2541 MesPrint(
"TermFree: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2543 #ifdef MALLOCDEBUGOUTPUT
2544 MesPrint(
"TermFree: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,TermMem);
2574 #define NUMBERMEMSTARTNUM 16
2575 #define NUMBEREXTRAWORDS 10L
2577 #ifdef TERMMALLOCDEBUG
2578 UWORD **DebugHeap3, **DebugHeap4;
2581 VOID NumberMallocAddMemory(PHEAD0)
2587 else extra = AT.NumberMemMax;
2588 if ( AT.NumberMemHeap ) M_free(AT.NumberMemHeap,
"NumberMalloc");
2589 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"NumberMalloc");
2590 AT.NumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"NumberMalloc");
2591 for ( i = 0; i < extra; i++ ) {
2592 AT.NumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2594 #ifdef TERMMALLOCDEBUG
2595 DebugHeap4 = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(WORD *),
"NumberMalloc");
2596 for ( i = 0; i < AT.NumberMemMax; i++ ) { DebugHeap4[i] = DebugHeap3[i]; }
2597 for ( i = 0; i < extra; i++ ) {
2598 DebugHeap4[i+AT.NumberMemMax] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2600 if ( DebugHeap3 ) M_free(DebugHeap3,
"NumberMalloc");
2601 DebugHeap3 = DebugHeap4;
2603 AT.NumberMemTop = extra;
2604 AT.NumberMemMax += extra;
2610 #ifndef MEMORYMACROS
2612 UWORD *NumberMalloc2(PHEAD
char *text)
2614 if ( AT.NumberMemTop <= 0 ) NumberMallocAddMemory(BHEAD text);
2616 #ifdef MALLOCDEBUGOUTPUT
2617 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2618 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2622 return(AT.NumberMemHeap[--AT.NumberMemTop]);
2625 VOID NumberFree2(PHEAD UWORD *NumberMem,
char *text)
2627 #ifdef TERMMALLOCDEBUG
2629 for ( i = 0; i < AT.NumberMemMax; i++ ) {
2630 if ( NumberMem == DebugHeap3[i] )
break;
2632 if ( i >= AT.NumberMemMax ) {
2633 MesPrint(
" ERROR: NumberFree called with an address not given by NumberMalloc.");
2638 AT.NumberMemHeap[AT.NumberMemTop++] = NumberMem;
2640 #ifdef MALLOCDEBUGOUTPUT
2641 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2642 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2655 VOID CacheNumberMallocAddMemory(PHEAD0)
2661 else extra = AT.CacheNumberMemMax;
2662 if ( AT.CacheNumberMemHeap ) M_free(AT.CacheNumberMemHeap,
"NumberMalloc");
2663 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"CacheNumberMalloc");
2664 AT.CacheNumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"CacheNumberMalloc");
2665 for ( i = 0; i < extra; i++ ) {
2666 AT.CacheNumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2668 AT.CacheNumberMemTop = extra;
2669 AT.CacheNumberMemMax += extra;
2672 #ifndef MEMORYMACROS
2674 UWORD *CacheNumberMalloc2(PHEAD
char *text)
2676 if ( AT.CacheNumberMemTop <= 0 ) CacheNumberMallocAddMemory(BHEAD0);
2678 #ifdef MALLOCDEBUGOUTPUT
2679 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2683 return(AT.CacheNumberMemHeap[--AT.CacheNumberMemTop]);
2686 VOID CacheNumberFree2(PHEAD UWORD *NumberMem,
char *text)
2689 AT.CacheNumberMemHeap[AT.CacheNumberMemTop++] = NumberMem;
2691 #ifdef MALLOCDEBUGOUTPUT
2692 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2707 VOID *FromList(
LIST *L)
2716 i = ( L->
num * L->
size ) /
sizeof(
int);
2717 old = (
int *)L->
lijst; newL = (
int *)newlist;
2718 while ( --i >= 0 ) *newL++ = *old++;
2719 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst FromList");
2733 VOID *From0List(
LIST *L)
2741 i = ( L->
num * L->
size ) /
sizeof(
int);
2742 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2743 while ( --i >= 0 ) *newL++ = *old++;
2744 i = ( L->
maxnum - L->
num ) /
sizeof(
int);
2745 while ( --i >= 0 ) *newL++ = 0;
2746 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst From0List");
2762 VOID *FromVarList(
LIST *L)
2768 else if ( L->
lijst ) {
2770 if ( L == &(AP.DollarList) ) {
2771 if ( L->
maxnum > MAXDOLLARVARIABLES ) L->
maxnum = MAXDOLLARVARIABLES;
2772 if ( L->
num >= MAXDOLLARVARIABLES ) {
2773 MesPrint(
"!!!More than %l objects in list of $-variables",
2774 MAXDOLLARVARIABLES);
2779 if ( L->
maxnum > MAXVARIABLES ) L->
maxnum = MAXVARIABLES;
2780 if ( L->
num >= MAXVARIABLES ) {
2781 MesPrint(
"!!!More than %l objects in list of variables",
2789 i = ( L->
num * L->
size ) /
sizeof(
int);
2790 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2791 while ( --i >= 0 ) *newL++ = *old++;
2792 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst from VarList");
2796 return( ((
char *)(L->
lijst)) + L->
size * ((L->
num)++) );
2804 int DoubleList(VOID ***lijst,
int *oldsize,
int objectsize,
char *nameoftype)
2807 LONG i, newsize, fullsize;
2809 static LONG maxlistsize = (LONG)(MAXPOSITIVE);
2810 if ( *lijst == 0 ) {
2811 if ( *oldsize > 0 ) newsize = *oldsize;
2814 else newsize = *oldsize * 2;
2815 if ( newsize > maxlistsize ) {
2816 if ( *oldsize == maxlistsize ) {
2817 MesPrint(
"No memory for extra space in %s",nameoftype);
2820 newsize = maxlistsize;
2822 fullsize = ( newsize * objectsize +
sizeof(VOID *)-1 ) & (-
sizeof(VOID *));
2823 newlist = (VOID **)Malloc1(fullsize,nameoftype);
2825 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(VOID *);
2832 while ( --i >= 0 ) *to++ = *from++;
2834 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2856 int DoubleLList(VOID ***lijst, LONG *oldsize,
int objectsize,
char *nameoftype)
2859 LONG i, newsize, fullsize;
2861 static LONG maxlistsize = (LONG)(MAXLONG);
2862 if ( *lijst == 0 ) {
2863 if ( *oldsize > 0 ) newsize = *oldsize;
2866 else newsize = *oldsize * 2;
2867 if ( newsize > maxlistsize ) {
2868 if ( *oldsize == maxlistsize ) {
2869 MesPrint(
"No memory for extra space in %s",nameoftype);
2872 newsize = maxlistsize;
2874 fullsize = ( newsize * objectsize +
sizeof(VOID *)-1 ) & (-
sizeof(VOID *));
2875 newlist = (VOID **)Malloc1(fullsize,nameoftype);
2877 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(VOID *);
2884 while ( --i >= 0 ) *to++ = *from++;
2886 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2897 #define DODOUBLE(x) { x *s, *t, *u; if ( *start ) { \
2898 oldsize = *(x **)stop - *(x **)start; newsize = 2*oldsize; \
2899 t = u = (x *)Malloc1(newsize*sizeof(x),text); s = *(x **)start; \
2900 for ( i = 0; i < oldsize; i++ ) *t++ = *s++; M_free(*start,"double"); } \
2901 else { newsize = 100; u = (x *)Malloc1(newsize*sizeof(x),text); } \
2902 *start = (void *)u; *stop = (void *)(u+newsize); }
2904 void DoubleBuffer(
void **start,
void **stop,
int size,
char *text)
2906 LONG oldsize, newsize, i;
2907 if ( size ==
sizeof(
char) ) DODOUBLE(
char)
2908 else if ( size == sizeof(
short) ) DODOUBLE(
short)
2909 else if ( size == sizeof(
int) ) DODOUBLE(
int)
2910 else if ( size == sizeof(LONG) ) DODOUBLE(LONG)
2911 else if ( size % sizeof(
int) == 0 ) DODOUBLE(
int)
2913 MesPrint(
"---Cannot handle doubling buffers of size %d",size);
2923 #define DOEXPAND(x) { x *newbuffer, *t, *m; \
2924 t = newbuffer = (x *)Malloc1((newsize+2)*type,"ExpandBuffer"); \
2925 if ( *buffer ) { m = (x *)*buffer; i = *oldsize; \
2926 while ( --i >= 0 ) *t++ = *m++; M_free(*buffer,"ExpandBuffer"); \
2927 } *buffer = newbuffer; *oldsize = newsize; }
2929 void ExpandBuffer(
void **buffer, LONG *oldsize,
int type)
2932 if ( *oldsize <= 0 ) { newsize = 100; }
2933 else newsize = 2*(*oldsize);
2934 if ( type ==
sizeof(
char) ) DOEXPAND(
char)
2935 else if ( type == sizeof(
short) ) DOEXPAND(
short)
2936 else if ( type == sizeof(
int) ) DOEXPAND(
int)
2937 else if ( type == sizeof(LONG) ) DOEXPAND(LONG)
2940 MesPrint(
"---Cannot handle expanding buffers with objects of size %d",type);
2953 LONG iexp(LONG x,
int p)
2958 if ( x == 0 )
return(0);
2959 if ( p == 0 )
return(1);
2960 sign = x < 0 ? -1 : 1;
2961 if ( sign < 0 && ( p & 1 ) == 0 ) sign = 1;
2963 if ( ux == 1 )
return(sign);
2964 if ( p < 0 )
return(0);
2967 if ( ( p & 1 ) != 0 ) y *= ux;
2971 if ( sign < 0 ) y = -y;
2972 return ULongToLong(y);
2984 void ToGeneral(WORD *r, WORD *m, WORD par)
2988 else { m[1] = 0; m += ARGHEAD + 1; }
2992 if ( j >= FUNCTION ) { *m++ = j; *m++ = FUNHEAD; FILLFUN(m) }
2995 case SYMBOL: *m++ = j; *m++ = 4; *m++ = *r++; *m++ = 1;
break;
2997 if ( *r > 0 ) { *m++ = *r; *m++ = 1; *m++ = 3; }
2998 else if ( *r == 0 ) { m--; }
2999 else { *m++ = -*r; *m++ = 1; *m++ = -3; }
3006 *m++ = INDEX; *m++ = 3; *m++ = *r++;
3010 *m++ = 1; *m++ = 1; *m++ = k;
3013 if ( !par ) mm[ARGHEAD] = *mm-ARGHEAD;
3028 int ToFast(WORD *r, WORD *m)
3031 if ( *r == ARGHEAD ) { *m++ = -SNUMBER; *m++ = 0;
return(1); }
3032 if ( *r != r[ARGHEAD]+ARGHEAD )
return(0);
3035 if ( r[2] != 1 || r[1] <= 0 )
return(0);
3036 *m++ = -SNUMBER; *m = ( r[3] < 0 ) ? -r[1] : r[1];
return(1);
3039 if ( r[i-1] != 1 || r[i-2] != 1 )
return(0);
3041 if ( r[i] == -3 && r[2] == *r-4 && r[2] == 3 && r[1] == INDEX
3042 && r[3] < MINSPEC ) {}
3045 else if ( r[2] != *r - 4 )
return(0);
3047 if ( *r >= FUNCTION ) {
3048 if ( r[1] <= FUNHEAD ) { *m++ = -*r;
return(1); }
3050 else if ( *r == SYMBOL ) {
3051 if ( r[1] == 4 && r[3] == 1 )
3052 { *m++ = -SYMBOL; *m++ = r[2];
return(1); }
3054 else if ( *r == INDEX ) {
3056 if ( r[2] >= MINSPEC ) {
3057 if ( r[2] >= 0 && r[2] < AM.OffsetIndex ) *m++ = -SNUMBER;
3061 if ( r[5] == -3 ) *m++ = -MINVECTOR;
3062 else *m++ = -VECTOR;
3081 WORD ToPolyFunGeneral(PHEAD WORD *term)
3083 WORD *t = term+1, *tt, *to, *to1, *termout, *tstop, *tnext;
3084 WORD numarg, i, change = 0;
3085 tstop = term + *term; tstop -= ABS(tstop[-1]);
3086 termout = to = AT.WorkPointer;
3088 while ( t < tstop ) {
3089 if ( *t == AR.PolyFun ) {
3090 tt = t+FUNHEAD; tnext = t + t[1];
3092 while ( tt < tnext ) { numarg++; NEXTARG(tt); }
3093 if ( numarg == 2 ) {
3096 i = FUNHEAD; NCOPY(to,t,i);
3097 while ( tt < tnext ) {
3099 i = *tt; NCOPY(to,tt,i);
3101 else if ( *tt == -SYMBOL ) {
3102 to1[1] += 6+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3103 *to++ = 8+ARGHEAD; *to++ = 0; FILLARG(to);
3104 *to++ = 8; *to++ = SYMBOL; *to++ = 4; *to++ = tt[1];
3105 *to++ = 1; *to++ = 1; *to++ = 1; *to++ = 3;
3108 else if ( *tt == -SNUMBER ) {
3110 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3111 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3112 *to++ = 4; *to++ = tt[1]; *to++ = 1; *to++ = 3;
3115 else if ( tt[1] < 0 ) {
3116 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3117 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3118 *to++ = 4; *to++ = -tt[1]; *to++ = 1; *to++ = -3;
3122 MLOCK(ErrorMessageLock);
3123 MesPrint(
"Internal error: Zero in PolyRatFun");
3124 MUNLOCK(ErrorMessageLock);
3133 i = t[1]; NCOPY(to,t,i)
3137 while ( t < tt ) *to++ = *t++;
3138 *termout = to - termout;
3139 t = term; i = *termout; tt = termout;
3141 AT.WorkPointer = term + *term;
3156 int IsLikeVector(WORD *arg)
3158 WORD *sstop, *t, *tstop;
3160 if ( *arg == -VECTOR || *arg == -INDEX )
return(1);
3161 if ( *arg == -SNUMBER && arg[1] >= 0 && arg[1] < AM.OffsetIndex )
3165 sstop = arg + *arg; arg += ARGHEAD;
3166 while ( arg < sstop ) {
3168 tstop = t - ABS(t[-1]);
3170 while ( arg < tstop ) {
3171 if ( *arg == INDEX )
return(1);
3184 int AreArgsEqual(WORD *arg1, WORD *arg2)
3187 if ( *arg2 != *arg1 )
return(0);
3190 while ( --i > 0 ) {
if ( arg1[i] != arg2[i] )
return(0); }
3193 else if ( *arg1 <= -FUNCTION )
return(1);
3194 else if ( arg1[1] == arg2[1] )
return(1);
3203 int CompareArgs(WORD *arg1, WORD *arg2)
3207 if ( *arg2 < 0 )
return(-1);
3208 i1 = *arg1-ARGHEAD; arg1 += ARGHEAD;
3209 i2 = *arg2-ARGHEAD; arg2 += ARGHEAD;
3210 while ( i1 > 0 && i2 > 0 ) {
3211 if ( *arg1 != *arg2 )
return((
int)(*arg1)-(
int)(*arg2));
3212 i1--; i2--; arg1++; arg2++;
3216 else if ( *arg2 > 0 )
return(1);
3218 if ( *arg1 != *arg2 ) {
3219 if ( *arg1 < *arg2 )
return(-1);
3222 if ( *arg1 <= -FUNCTION )
return(0);
3223 return((
int)(arg1[1])-(
int)(arg2[1]));
3234 int CompArg(WORD *s1, WORD *s2)
3237 WORD *st1, *st2, x[7];
3241 if ( *s1 <= -FUNCTION && *s2 <= -FUNCTION ) {
3242 if ( *s1 > *s2 )
return(-1);
3243 if ( *s1 < *s2 )
return(1);
3246 if ( *s1 > *s2 )
return(1);
3247 if ( *s1 < *s2 )
return(-1);
3248 if ( *s1 <= -FUNCTION )
return(0);
3250 if ( *s1 > *s2 )
return(1);
3251 if ( *s1 < *s2 )
return(-1);
3254 x[1] = AT.comsym[3];
3255 x[2] = AT.comnum[1];
3256 x[3] = AT.comnum[3];
3257 x[4] = AT.comind[3];
3258 x[5] = AT.comind[6];
3259 x[6] = AT.comfun[1];
3260 if ( *s1 == -SYMBOL ) {
3261 AT.comsym[3] = s1[1];
3262 st1 = AT.comsym+8; s1 = AT.comsym;
3264 else if ( *s1 == -SNUMBER ) {
3266 AT.comnum[1] = -s1[1]; AT.comnum[3] = -3;
3269 AT.comnum[1] = s1[1]; AT.comnum[3] = 3;
3274 else if ( *s1 == -INDEX || *s1 == -VECTOR ) {
3275 AT.comind[3] = s1[1]; AT.comind[6] = 3;
3276 st1 = AT.comind+7; s1 = AT.comind;
3278 else if ( *s1 == -MINVECTOR ) {
3279 AT.comind[3] = s1[1]; AT.comind[6] = -3;
3280 st1 = AT.comind+7; s1 = AT.comind;
3282 else if ( *s1 <= -FUNCTION ) {
3283 AT.comfun[1] = -*s1;
3284 st1 = AT.comfun+FUNHEAD+4; s1 = AT.comfun;
3290 else if ( *s1 == -ARGWILD ) {
3293 else {
goto argerror; }
3294 st2 = s2 + *s2; s2 += ARGHEAD;
3297 else if ( *s2 < 0 ) {
3298 x[1] = AT.comsym[3];
3299 x[2] = AT.comnum[1];
3300 x[3] = AT.comnum[3];
3301 x[4] = AT.comind[3];
3302 x[5] = AT.comind[6];
3303 x[6] = AT.comfun[1];
3304 if ( *s2 == -SYMBOL ) {
3305 AT.comsym[3] = s2[1];
3306 st2 = AT.comsym+8; s2 = AT.comsym;
3308 else if ( *s2 == -SNUMBER ) {
3310 AT.comnum[1] = -s2[1]; AT.comnum[3] = -3;
3313 else if ( s2[1] == 0 ) {
3314 st2 = AT.comnum+4; s2 = st2;
3317 AT.comnum[1] = s2[1]; AT.comnum[3] = 3;
3322 else if ( *s2 == -INDEX || *s2 == -VECTOR ) {
3323 AT.comind[3] = s2[1]; AT.comind[6] = 3;
3324 st2 = AT.comind+7; s2 = AT.comind;
3326 else if ( *s2 == -MINVECTOR ) {
3327 AT.comind[3] = s2[1]; AT.comind[6] = -3;
3328 st2 = AT.comind+7; s2 = AT.comind;
3330 else if ( *s2 <= -FUNCTION ) {
3331 AT.comfun[1] = -*s2;
3332 st2 = AT.comfun+FUNHEAD+4; s2 = AT.comfun;
3338 else if ( *s2 == -ARGWILD ) {
3341 else {
goto argerror; }
3342 st1 = s1 + *s1; s1 += ARGHEAD;
3346 x[1] = AT.comsym[3];
3347 x[2] = AT.comnum[1];
3348 x[3] = AT.comnum[3];
3349 x[4] = AT.comind[3];
3350 x[5] = AT.comind[6];
3351 x[6] = AT.comfun[1];
3352 st1 = s1 + *s1; st2 = s2 + *s2;
3353 s1 += ARGHEAD; s2 += ARGHEAD;
3355 while ( s1 < st1 && s2 < st2 ) {
3356 if ( ( k = CompareTerms(s1,s2,(WORD)2) ) != 0 ) {
3357 AT.comsym[3] = x[1];
3358 AT.comnum[1] = x[2];
3359 AT.comnum[3] = x[3];
3360 AT.comind[3] = x[4];
3361 AT.comind[6] = x[5];
3362 AT.comfun[1] = x[6];
3365 s1 += *s1; s2 += *s2;
3367 AT.comsym[3] = x[1];
3368 AT.comnum[1] = x[2];
3369 AT.comnum[3] = x[3];
3370 AT.comind[3] = x[4];
3371 AT.comind[6] = x[5];
3372 AT.comfun[1] = x[6];
3373 if ( s1 < st1 )
return(1);
3374 if ( s2 < st2 )
return(-1);
3379 MesPrint(
"Illegal type of short function argument in Normalize");
3380 Terminate(-1);
return(0);
3388 #ifdef HAVE_CLOCK_GETTIME
3391 #ifdef HAVE_GETTIMEOFDAY
3392 #include <sys/time.h>
3394 #include <sys/timeb.h>
3410 #ifdef HAVE_CLOCK_GETTIME
3412 clock_gettime(CLOCK_MONOTONIC, &ts);
3415 return(((LONG)(ts.tv_sec)-AM.OldSecTime)*100 +
3416 ((LONG)(ts.tv_nsec / 1000000)-AM.OldMilliTime)/10);
3419 AM.OldSecTime = (LONG)(ts.tv_sec);
3420 AM.OldMilliTime = (LONG)(ts.tv_nsec / 1000000);
3424 #ifdef HAVE_GETTIMEOFDAY
3427 gettimeofday(&t, NULL);
3428 sec = (LONG)t.tv_sec;
3429 msec = (LONG)(t.tv_usec/1000);
3431 return (sec-AM.OldSecTime)*100 + (msec-AM.OldMilliTime)/10;
3434 AM.OldSecTime = sec;
3435 AM.OldMilliTime = msec;
3443 return(((LONG)(tp.time)-AM.OldSecTime)*100 +
3444 ((LONG)(tp.millitm)-AM.OldMilliTime)/10);
3447 AM.OldSecTime = (LONG)(tp.time);
3448 AM.OldMilliTime = (LONG)(tp.millitm);
3460 LONG TimeChildren(WORD par)
3462 if ( par )
return(Timer(1)-AM.OldChildTime);
3463 AM.OldChildTime = Timer(1);
3481 if ( par )
return(Timer(0)-AR.OldTime);
3482 AR.OldTime = Timer(0);
3490 #if defined(WINDOWS)
3494 #ifndef WITHPTHREADS
3495 static int initialized = 0;
3496 static HANDLE hProcess;
3497 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3500 if ( !initialized ) {
3501 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
3503 if ( GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3504 PFILETIME pftKernel = &ftKernel;
3505 PFILETIME pftUser = &ftUser;
3506 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3507 return (LONG)(t / 10000);
3513 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3516 hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, GetCurrentThreadId());
3518 if ( GetThreadTimes(hThread, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3519 PFILETIME pftKernel = &ftKernel;
3520 PFILETIME pftUser = &ftUser;
3521 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3522 lResult = (LONG)(t / 10000);
3524 CloseHandle(hThread);
3531 #include <sys/time.h>
3532 #include <sys/resource.h>
3533 #ifdef WITHPOSIXCLOCK
3542 #ifdef WITHPOSIXCLOCK
3561 if ( clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t) ) {
3562 MesPrint(
"Error in getting timing information");
3564 return (LONG)t.tv_sec * 1000 + (LONG)t.tv_nsec / 1000000;
3568 struct rusage rusage;
3570 getrusage(RUSAGE_CHILDREN,&rusage);
3571 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3572 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3575 getrusage(RUSAGE_SELF,&rusage);
3576 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3577 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3584 #include <sys/time.h>
3585 #include <sys/resource.h>
3589 struct rusage rusage;
3591 getrusage(RUSAGE_CHILDREN,&rusage);
3592 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3593 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3596 getrusage(RUSAGE_SELF,&rusage);
3597 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3598 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3603 #include <sys/time.h>
3604 #include <sys/resource.h>
3608 struct rusage rusage;
3610 getrusage(RUSAGE_CHILDREN,&rusage);
3611 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3612 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3615 getrusage(RUSAGE_SELF,&rusage);
3616 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3617 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3633 #include <sys/time.h>
3634 #include <sys/resource.h>
3635 struct rusage rusage;
3637 getrusage(RUSAGE_CHILDREN,&rusage);
3638 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3639 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3642 getrusage(RUSAGE_SELF,&rusage);
3643 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3644 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3648 clock_t tikken = clock();
3649 return((LONG)tikken/1000);
3651 clock_t t, tikken = clock();
3652 t = tikken % CLK_TCK;
3655 tikken += (t*1000)/CLK_TCK;
3663 void times(tbuffer_t *buffer);
3669 if ( par == 1 ) {
return(0); }
3672 return(buffer.proc_user_time * 10);
3685 #include <sys/types.h>
3686 #include <sys/times.h>
3691 #include <sys/time.h>
3692 #include <sys/resource.h>
3700 if ( par == 1 ) {
return(0); }
3702 if ( ( AO.wrapnum & 1 ) != 0 ) t ^= 0x80000000;
3708 return(AO.wrap+(t/1000));
3713 ULONG a1, a2, a3, a4;
3714 if ( par == 1 ) {
return(0); }
3716 a1 = (ULONG)buffer.tms_utime;
3720 a2 = 1000*a2 + (a3 >> 16);
3725 ret = (LONG)((a4 << 16) + a3 / CLK_TCK);
3731 struct timezone tzp;
3732 if ( par == 1 ) {
return(0); }
3733 gettimeofday(&tp,&tzp); */
3734 return(tp.tv_sec*1000+tp.tv_usec/1000);
3736 struct rusage rusage;
3738 getrusage(RUSAGE_CHILDREN,&rusage);
3739 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3740 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3743 getrusage(RUSAGE_SELF,&rusage);
3744 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3745 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3791 int errorcode = 0, coeffsize;
3792 WORD *t, *tt, *tstop, *endterm, *targ, *targstop, *funstop, *argterm;
3793 endterm = term + *term;
3794 coeffsize = ABS(endterm[-1]);
3795 if ( coeffsize >= *term ) {
3796 MLOCK(ErrorMessageLock);
3797 MesPrint(
"TestTerm: Internal inconsistency in term. Coefficient too big.");
3798 MUNLOCK(ErrorMessageLock);
3802 if ( ( coeffsize < 3 ) || ( ( coeffsize & 1 ) != 1 ) ) {
3803 MLOCK(ErrorMessageLock);
3804 MesPrint(
"TestTerm: Internal inconsistency in term. Wrong size coefficient.");
3805 MUNLOCK(ErrorMessageLock);
3810 tstop = endterm - coeffsize;
3811 while ( t < tstop ) {
3822 MLOCK(ErrorMessageLock);
3823 MesPrint(
"TestTerm: Internal inconsistency in term. L or S number");
3824 MUNLOCK(ErrorMessageLock);
3830 case DOLLAREXPRESSION:
3843 MLOCK(ErrorMessageLock);
3844 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm.");
3845 MUNLOCK(ErrorMessageLock);
3853 MLOCK(ErrorMessageLock);
3854 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm number.");
3855 MUNLOCK(ErrorMessageLock);
3862 if ( *t-FUNCTION >= NumFunctions ) {
3863 MLOCK(ErrorMessageLock);
3864 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number");
3865 MUNLOCK(ErrorMessageLock);
3870 if ( funstop > tstop )
goto subtermsize;
3872 MLOCK(ErrorMessageLock);
3873 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag nonzero.");
3874 MUNLOCK(ErrorMessageLock);
3879 if ( targ > funstop ) {
3880 MLOCK(ErrorMessageLock);
3881 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function size.");
3882 MUNLOCK(ErrorMessageLock);
3886 if ( functions[*t-FUNCTION].spec >= TENSORFUNCTION ) {
3889 while ( targ < funstop ) {
3891 if ( *targ <= -(FUNCTION+NumFunctions) ) {
3892 MLOCK(ErrorMessageLock);
3893 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number in argument.");
3894 MUNLOCK(ErrorMessageLock);
3898 if ( *targ <= -FUNCTION ) { targ++; }
3900 if ( ( *targ != -SYMBOL ) && ( *targ != -VECTOR )
3901 && ( *targ != -MINVECTOR )
3902 && ( *targ != -SNUMBER )
3903 && ( *targ != -ARGWILD )
3904 && ( *targ != -INDEX ) ) {
3905 MLOCK(ErrorMessageLock);
3906 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal object in argument.");
3907 MUNLOCK(ErrorMessageLock);
3914 else if ( ( *targ < ARGHEAD ) || ( targ+*targ > funstop ) ) {
3915 MLOCK(ErrorMessageLock);
3916 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal size of argument.");
3917 MUNLOCK(ErrorMessageLock);
3921 else if ( targ[1] != 0 ) {
3922 MLOCK(ErrorMessageLock);
3923 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag in argument.");
3924 MUNLOCK(ErrorMessageLock);
3929 targstop = targ + *targ;
3930 argterm = targ + ARGHEAD;
3931 while ( argterm < targstop ) {
3932 if ( ( *argterm < 4 ) || ( argterm + *argterm > targstop ) ) {
3933 MLOCK(ErrorMessageLock);
3934 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal termsize in argument.");
3935 MUNLOCK(ErrorMessageLock);
3940 MLOCK(ErrorMessageLock);
3941 MesPrint(
"TestTerm: Internal inconsistency in term. Called from TestTerm.");
3942 MUNLOCK(ErrorMessageLock);
3946 argterm += *argterm;
3957 MLOCK(ErrorMessageLock);
3958 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm size.");
3959 MUNLOCK(ErrorMessageLock);
int PutPreVar(UBYTE *, UBYTE *, UBYTE *, int)
int CopyFile(char *, char *)
LONG PF_BroadcastNumber(LONG x)
int PF_Bcast(void *buffer, int count)
struct bit_field * one_byte
LONG PF_WriteFileToFile(int handle, UBYTE *buffer, LONG size)
struct bit_field set_of_char[32]