69#define BANNER (4*sizeof(LONG))
70void *malloclist[60000];
71LONG mallocsizes[60000];
72char *mallocstrings[60000];
77extern "C" getdtablesize();
90#error "MALLOCPROTECT": undefine "TRAPSIGNALS" in unix.h first!
92#include "mallocprotect.h"
98#define M_alloc mprotectMalloc
102#ifdef TERMMALLOCDEBUG
103WORD **DebugHeap1, **DebugHeap2;
112UBYTE *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");
151UBYTE 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;
229 if ( stream->type == INPUTSTREAM ) {
234 if ( ReadFile(stream->handle,&c,1) != 1 ) {
237 if ( stream->fileposition == 0 ) {
239 stream->buffersize = 32;
240 stream->
buffer = (UBYTE *)Malloc1(stream->buffersize,
"input stream buffer");
244 if ( stream->
top - stream->
buffer >= stream->buffersize ) {
245 LONG oldsize = stream->buffersize;
246 DoubleBuffer((
void**)&stream->
buffer,(
void**)&stream->
top,
sizeof(UBYTE),
"double input stream buffer");
247 stream->buffersize = stream->
top - stream->
buffer;
256 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
257 if ( c == LINEFEED ) stream->eqnum = 1;
261 if ( stream->type != FILESTREAM )
return(ENDOFSTREAM);
262 if ( stream->fileposition != stream->bufferposition+stream->inbuffer ) {
263 stream->fileposition = stream->bufferposition+stream->inbuffer;
264 SETBASEPOSITION(scrpos,stream->fileposition);
265 SeekFile(stream->handle,&scrpos,SEEK_SET);
267 stream->bufferposition = stream->fileposition;
268 stream->inbuffer = ReadFile(stream->handle,
269 stream->
buffer,stream->buffersize);
270 if ( stream->inbuffer <= 0 )
return(ENDOFSTREAM);
271 stream->
top = stream->
buffer + stream->inbuffer;
273 stream->fileposition = stream->bufferposition + stream->inbuffer;
275 if ( stream->eqnum == 1 ) { stream->eqnum = 0; stream->linenumber++; }
277 if ( c == LINEFEED ) stream->eqnum = 1;
286UBYTE GetFromStream(
STREAM *stream)
289 if ( stream->isnextchar > 0 ) {
290 return(stream->nextchar[--stream->isnextchar]);
292 c1 = ReadFromStream(stream);
293 if ( c1 == LINEFEED || c1 == CARRIAGERETURN ) {
294 c2 = ReadFromStream(stream);
295 if ( c2 == c1 || ( c2 != LINEFEED && c2 != CARRIAGERETURN ) ) {
296 stream->isnextchar = 1;
297 stream->nextchar[0] = c2;
309UBYTE LookInStream(
STREAM *stream)
311 UBYTE c = GetFromStream(stream);
312 UngetFromStream(stream,c);
321STREAM *OpenStream(UBYTE *name,
int type,
int prevarmode,
int raiselow)
324 UBYTE *rhsofvariable, *s, *newname, c;
329 case REVERSEFILESTREAM:
337 handle = LocateFile(&newname,-1);
338 if ( handle < 0 )
return(0);
340 SeekFile(handle,&scrpos,SEEK_END);
341 TELLFILE(handle,&scrpos);
342 filesize = BASEPOSITION(scrpos);
344 SeekFile(handle,&scrpos,SEEK_SET);
345 if ( filesize > AM.MaxStreamSize && type == FILESTREAM )
346 filesize = AM.MaxStreamSize;
347 stream = CreateStream((UBYTE *)
"filestream");
351 stream->
buffer = (UBYTE *)Malloc1(filesize+1,
"name of input stream");
352 stream->inbuffer = ReadFile(handle,stream->
buffer,filesize);
353 if ( type == REVERSEFILESTREAM ) {
354 if ( ReverseStatements(stream) ) {
355 M_free(stream->
buffer,
"name of input stream");
359 stream->
top = stream->
buffer + stream->inbuffer;
361 stream->handle = handle;
362 stream->buffersize = filesize;
363 stream->fileposition = stream->inbuffer;
364 if ( newname != name ) stream->
name = newname;
365 else if ( name ) stream->
name = strDup1(name,
"name of input stream");
368 stream->prevline = stream->linenumber = 1;
372 if ( ( rhsofvariable = GetPreVar(name,WITHERROR) ) == 0 )
return(0);
373 stream = CreateStream((UBYTE *)
"var-stream");
377 stream->inbuffer = s - stream->
buffer;
378 stream->
name = AC.CurrentStream->name;
379 stream->linenumber = AC.CurrentStream->linenumber;
380 stream->prevline = AC.CurrentStream->prevline;
381 stream->eqnum = AC.CurrentStream->eqnum;
382 stream->
pname = strDup1(name,
"stream->pname");
383 stream->olddelay = AP.AllowDelay;
384 s = stream->
pname;
while ( *s ) s++;
385 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
390 if ( ( num = GetDollar(name) ) < 0 ) {
396 s = name;
while ( *s && *s !=
'[' ) s++;
397 if ( *s == 0 )
return(0);
399 if ( ( num = GetDollar(name) ) < 0 )
return(0);
402 if ( *s == 0 || FG.cTable[*s] != 1 || *s ==
']' ) {
403 MesPrint(
"@Illegal factor number for dollar variable");
406 while ( *s && FG.cTable[*s] == 1 ) {
407 numfac = 10*numfac+*s++-
'0';
409 if ( *s !=
']' || s[1] != 0 ) {
410 MesPrint(
"@Illegal factor number for $ variable");
413 stream = CreateStream((UBYTE *)
"dollar-stream");
414 stream->
buffer = stream->
pointer = s = WriteDollarFactorToBuffer(num,numfac,1);
417 stream = CreateStream((UBYTE *)
"dollar-stream");
418 stream->
buffer = stream->
pointer = s = WriteDollarToBuffer(num,1);
422 stream->inbuffer = s - stream->
buffer;
423 stream->
name = AC.CurrentStream->name;
424 stream->linenumber = AC.CurrentStream->linenumber;
425 stream->prevline= AC.CurrentStream->prevline;
426 stream->eqnum = AC.CurrentStream->eqnum;
427 stream->
pname = strDup1(name,
"stream->pname");
428 s = stream->
pname;
while ( *s ) s++;
429 while ( s[-1] ==
'+' || s[-1] ==
'-' ) s--;
432 AO.DollarOutSizeBuffer = 0;
433 AO.DollarOutBuffer = 0;
434 AO.DollarInOutBuffer = 0;
440 stream = CreateStream((UBYTE *)
"calculator");
444 stream->inbuffer = s - stream->
buffer;
445 stream->
name = AC.CurrentStream->name;
446 stream->linenumber = AC.CurrentStream->linenumber;
447 stream->prevline = AC.CurrentStream->prevline;
452 stream = CreateStream((UBYTE *)
"pipe");
456 if ( ( f = popen((
char *)name,
"r") ) == 0 ) {
457 Error0(
"@Cannot create pipe");
459 stream->handle = CreateHandle();
460 RWLOCKW(AM.handlelock);
461 filelist[stream->handle] = (FILES *)f;
462 UNRWLOCK(AM.handlelock);
465 stream->inbuffer = 0;
470 if ( PF.me == MASTER ) {
471 f = popen((
char *)name,
"r");
473 if ( f == 0 ) Error0(
"@Cannot create pipe");
479 stream->handle = CreateHandle();
480 RWLOCKW(AM.handlelock);
481 filelist[stream->handle] = (FILES *)f;
482 UNRWLOCK(AM.handlelock);
485 stream->buffersize = AM.MaxStreamSize;
486 stream->
buffer = (UBYTE *)Malloc1(stream->buffersize,
"pipe buffer");
487 stream->inbuffer = 0;
491 stream->
name = strDup1((UBYTE *)
"pipe",
"pipe");
492 stream->prevline = stream->linenumber = 1;
497#ifdef WITHEXTERNALCHANNEL
498 case EXTERNALCHANNELSTREAM:
501 if( (n=getCurrentExternalChannel()) == 0 )
502 Error0(
"@No current extrenal channel");
503 stream = CreateStream((UBYTE *)
"externalchannel");
504 stream->handle = CreateHandle();
505 tmpn = (
int *)Malloc1(
sizeof(
int),
"external channel handle");
507 RWLOCKW(AM.handlelock);
508 filelist[stream->handle] = (FILES *)tmpn;
509 UNRWLOCK(AM.handlelock);
512 stream->inbuffer = 0;
513 stream->
name = strDup1((UBYTE *)
"externalchannel",
"externalchannel");
514 stream->prevline = stream->linenumber = 1;
525 stream = CreateStream((UBYTE *)
"input stream");
526 stream->handle = CreateHandle();
528 FILES *f = (FILES *)Malloc1(
sizeof(
int),
"input stream handle");
531 f->descriptor = (int)(ssize_t)name;
533 f = name ? (FILES *)name : stdin;
535 RWLOCKW(AM.handlelock);
536 filelist[stream->handle] = f;
537 UNRWLOCK(AM.handlelock);
540 stream->inbuffer = 0;
541 stream->
name = strDup1((UBYTE *)(name ?
"INPUT" :
"STDIN"),
"input stream name");
542 stream->prevline = stream->linenumber = 1;
549 stream->fileposition = -1;
554 stream->bufferposition = 0;
555 stream->isnextchar = 0;
557 stream->previousNoShowInput = AC.NoShowInput;
558 stream->afterwards = raiselow;
559 if ( AC.CurrentStream ) stream->previous = AC.CurrentStream - AC.Streams;
560 else stream->previous = -1;
562 if ( prevarmode == 0 ) stream->prevars = -1;
563 else if ( prevarmode > 0 ) stream->prevars = NumPre;
564 else if ( prevarmode < 0 ) stream->prevars = -prevarmode-1;
565 AC.CurrentStream = stream;
566 if ( type == PREREADSTREAM || type == PREREADSTREAM3 || type == PRECALCSTREAM
567 || type == DOLLARSTREAM ) AC.NoShowInput = 1;
576int LocateFile(UBYTE **name,
int type)
578 int handle, namesize, i;
579 UBYTE *s, *to, *u1, *u2, *newname, *indir;
580 handle = OpenFile((
char *)(*name));
581 if ( handle >= 0 )
return(handle);
582 if ( type == SETUPFILE && AM.SetupFile ) {
583 handle = OpenFile((
char *)(AM.SetupFile));
584 if ( handle >= 0 )
return(handle);
585 MesPrint(
"Could not open setup file %s",(
char *)(AM.SetupFile));
587 namesize = 4; s = *name;
588 while ( *s ) { s++; namesize++; }
589 if ( type == SETUPFILE ) indir = AM.SetupDir;
590 else indir = AM.IncDir;
594 while ( *s ) { s++; i++; }
595 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
596 s = indir; to = newname;
597 while ( *s ) *to++ = *s++;
598 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
600 while ( *s ) *to++ = *s++;
602 handle = OpenFile((
char *)newname);
607 M_free(newname,
"LocateFile, incdir/file");
609 if ( type == SETUPFILE ) {
610 handle = OpenFile(setupfilename);
611 if ( handle >= 0 )
return(handle);
612 s = (UBYTE *)getenv(
"FORMSETUP");
614 handle = OpenFile((
char *)s);
615 if ( handle >= 0 )
return(handle);
616 MesPrint(
"Could not open setup file %s",s);
619 if ( type != SETUPFILE && AM.Path ) {
624 while ( *u1 && *u1 !=
';' ) {
628 while ( *u1 && *u1 !=
':' ) {
629 if ( *u1 ==
'\\' ) u1++;
633 newname = (UBYTE *)Malloc1(namesize+i,
"LocateFile");
634 s = u2; to = newname;
637 if ( *s ==
'\\' ) s++;
641 if ( to > newname && to[-1] != SEPARATOR ) *to++ = SEPARATOR;
643 while ( *s ) *to++ = *s++;
645 handle = OpenFile((
char *)newname);
650 M_free(newname,
"LocateFile Path/file");
654 if ( type != SETUPFILE ) Error1(
"LocateFile: Cannot find file",*name);
665 int newstr = stream->previous, sgn;
666 UBYTE *t, numbuf[24];
669 M_free(stream->
FoldName,
"stream->FoldName");
672 if ( stream->type == FILESTREAM || stream->type == REVERSEFILESTREAM ) {
673 CloseFile(stream->handle);
674 if ( stream->
buffer != 0 ) M_free(stream->
buffer,
"name of input stream");
678 else if ( stream->type == PIPESTREAM ) {
679 RWLOCKW(AM.handlelock);
681 if ( PF.me == MASTER )
683 pclose((FILE *)(filelist[stream->handle]));
684 filelist[stream->handle] = 0;
686 UNRWLOCK(AM.handlelock);
688 if ( stream->
buffer != 0 ) {
689 M_free(stream->
buffer,
"pipe buffer");
696#ifdef WITHEXTERNALCHANNEL
697 else if ( stream->type == EXTERNALCHANNELSTREAM ) {
699 RWLOCKW(AM.handlelock);
700 tmpn = (
int *)(filelist[stream->handle]);
701 filelist[stream->handle] = 0;
703 UNRWLOCK(AM.handlelock);
704 M_free(tmpn,
"external channel handle");
708 else if ( stream->type == INPUTSTREAM ) {
710 RWLOCKW(AM.handlelock);
711 f = filelist[stream->handle];
712 filelist[stream->handle] = 0;
714 UNRWLOCK(AM.handlelock);
715 M_free(f,
"input stream handle");
717 else if ( stream->type == PREVARSTREAM && (
718 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
719 t = stream->
buffer; x = 0; sgn = 1;
720 while ( *t ==
'-' || *t ==
'+' ) {
721 if ( *t ==
'-' ) sgn = -sgn;
724 if ( FG.cTable[*t] == 1 ) {
725 while ( *t && FG.cTable[*t] == 1 ) x = 10*x + *t++ -
'0';
727 if ( stream->afterwards == PRERAISEAFTER ) x = sgn*x + 1;
734 else if ( stream->type == DOLLARSTREAM && (
735 stream->afterwards == PRERAISEAFTER || stream->afterwards == PRELOWERAFTER ) ) {
736 if ( stream->afterwards == PRERAISEAFTER ) x = 1;
738 DollarRaiseLow(stream->
pname,x);
740 else if ( stream->type == PRECALCSTREAM || stream->type == DOLLARSTREAM ) {
741 if ( stream->
buffer ) M_free(stream->
buffer,
"stream->buffer");
744 if ( stream->
name && stream->type != PREVARSTREAM
745 && stream->type != PREREADSTREAM && stream->type != PREREADSTREAM2 && stream->type != PREREADSTREAM3
746 && stream->type != PRECALCSTREAM && stream->type != DOLLARSTREAM ) {
747 M_free(stream->
name,
"stream->name");
751 AC.NoShowInput = stream->previousNoShowInput;
757 if ( stream->prevars >= 0 ) {
758 while ( NumPre > stream->prevars ) {
760 M_free(PreVar[NumPre].name,
"PreVar[NumPre].name");
761 PreVar[NumPre].name = PreVar[NumPre].value = 0;
764 if ( stream->type == PREVARSTREAM ) {
765 AP.AllowDelay = stream->olddelay;
766 ClearMacro(stream->
pname);
767 M_free(stream->
pname,
"stream->pname");
769 else if ( stream->type == DOLLARSTREAM ) {
770 M_free(stream->
pname,
"stream->pname");
773 if ( newstr >= 0 )
return(AC.Streams + newstr);
782STREAM *CreateStream(UBYTE *where)
787 if ( AC.NumStreams >= AC.MaxNumStreams ) {
788 if ( AC.MaxNumStreams == 0 ) numnewstreams = 10;
789 else numnewstreams = 2*AC.MaxNumStreams;
790 newstreams = (
STREAM *)Malloc1(
sizeof(
STREAM)*(numnewstreams+1),
"CreateStream");
791 if ( AC.MaxNumStreams > 0 ) {
792 offset = AC.CurrentStream - AC.Streams;
793 for ( i = 0; i < AC.MaxNumStreams; i++ ) {
794 newstreams[i] = AC.Streams[i];
796 AC.CurrentStream = newstreams + offset;
798 else newstreams[0].previous = -1;
799 AC.MaxNumStreams = numnewstreams;
800 if ( AC.Streams ) M_free(AC.Streams,(
char *)where);
801 AC.Streams = newstreams;
803 newstreams = AC.Streams+AC.NumStreams++;
804 newstreams->
name = 0;
813LONG GetStreamPosition(
STREAM *stream)
815 return(stream->bufferposition + ((LONG)stream->
pointer-(LONG)stream->
buffer));
823VOID PositionStream(
STREAM *stream, LONG position)
826 if ( position >= stream->bufferposition
827 && position < stream->bufferposition + stream->inbuffer ) {
828 stream->
pointer = stream->
buffer + (position-stream->bufferposition);
830 else if ( stream->type == FILESTREAM ) {
831 SETBASEPOSITION(scrpos,position);
832 SeekFile(stream->handle,&scrpos,SEEK_SET);
833 stream->inbuffer = ReadFile(stream->handle,stream->
buffer,stream->buffersize);
835 stream->
top = stream->
buffer + stream->inbuffer;
836 stream->bufferposition = position;
837 stream->fileposition = position + stream->inbuffer;
838 stream->isnextchar = 0;
841 Error0(
"Illegal position for stream");
855int ReverseStatements(
STREAM *stream)
857 UBYTE *spare = (UBYTE *)Malloc1((stream->inbuffer+1)*
sizeof(UBYTE),
"Reverse copy");
858 UBYTE *top = stream->
buffer + stream->inbuffer, *in, *s, *ss, *out;
859 out = spare+stream->inbuffer+1;
863 if ( *s == AP.ComChar ) {
866 if ( s == top ) { *--out =
'\n';
break; }
870irrend: MesPrint(
"@Irregular end of reverse include file.");
874 else if ( *s ==
'\n' ) {
876 while ( ss > in ) *--out = *--ss;
878 if ( out[0] == AP.ComChar && ss+6 < s && out[3] ==
'#' ) {
882 if ( out[4] ==
'[' ) out[4] =
']';
883 else if ( out[4] ==
']' ) out[4] =
'[';
891 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
909 while ( s < top && ( *s ==
' ' || *s ==
'\t' ) ) s++;
910 while ( s < top && *s ==
'\n' ) s++;
911 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
913 while ( ss > in ) *--out = *--ss;
917 else if ( *s ==
'"' ) {
920 if ( *s ==
'"' )
break;
921 if ( *s ==
'\\' ) { s++; }
924 if ( s >= top )
goto irrend;
926 else if ( *s ==
'\\' ) {
928 if ( s >= top )
goto irrend;
933 if ( s >= top && s[-1] !=
'\n' ) *s++ =
'\n';
935 while ( ss > in ) *--out = *--ss;
939 if ( out == spare ) stream->inbuffer++;
940 if ( out > spare+1 ) {
941 MesPrint(
"@Internal error in #reverseinclude instruction.");
944 memcpy((
void *)(stream->
buffer),(
void *)out,(
size_t)(stream->inbuffer*
sizeof(UBYTE)));
945 M_free(spare,
"Reverse copy");
958 int i = CreateHandle();
959 filelist[i] = Ustdout;
964 AR.Fscr[0].handle = -1;
965 AR.Fscr[1].handle = -1;
966 AR.Fscr[2].handle = -1;
967 AR.FoStage4[0].handle = -1;
968 AR.FoStage4[1].handle = -1;
969 AR.infile = &(AR.Fscr[0]);
970 AR.outfile = &(AR.Fscr[1]);
971 AR.hidefile = &(AR.Fscr[2]);
972 AR.StoreData.Handle = -1;
975 AC.MaxNumStreams = 0;
983int OpenFile(
char *name)
988 if ( ( f = Uopen(name,
"rb") ) == 0 )
return(-1);
991 RWLOCKW(AM.handlelock);
993 UNRWLOCK(AM.handlelock);
1002int OpenAddFile(
char *name)
1007 if ( ( f = Uopen(name,
"a+b") ) == 0 )
return(-1);
1010 RWLOCKW(AM.handlelock);
1012 UNRWLOCK(AM.handlelock);
1013 TELLFILE(i,&scrpos);
1014 SeekFile(i,&scrpos,SEEK_SET);
1023int ReOpenFile(
char *name)
1028 if ( ( f = Uopen(name,
"r+b") ) == 0 )
return(-1);
1030 RWLOCKW(AM.handlelock);
1032 UNRWLOCK(AM.handlelock);
1033 TELLFILE(i,&scrpos);
1034 SeekFile(i,&scrpos,SEEK_SET);
1043int CreateFile(
char *name)
1047 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
1049 RWLOCKW(AM.handlelock);
1051 UNRWLOCK(AM.handlelock);
1060int CreateLogFile(
char *name)
1064 if ( ( f = Uopen(name,
"w+b") ) == 0 )
return(-1);
1067 RWLOCKW(AM.handlelock);
1069 UNRWLOCK(AM.handlelock);
1078VOID CloseFile(
int handle)
1080 if ( handle >= 0 ) {
1082 RWLOCKW(AM.handlelock);
1083 f = filelist[handle];
1084 filelist[handle] = 0;
1086 UNRWLOCK(AM.handlelock);
1103 #define COPYFILEBUFSIZE 40960L
1105 size_t countin, countout, sumcount;
1106 char *buffer = NULL;
1108 sumcount = (AM.S0->LargeSize+AM.S0->SmallEsize)*
sizeof(WORD);
1109 if ( sumcount <= COPYFILEBUFSIZE ) {
1110 sumcount = COPYFILEBUFSIZE;
1111 buffer = (
char*)Malloc1(sumcount,
"file copy buffer");
1114 buffer = (
char *)(AM.S0->lBuffer);
1117 in = fopen(source,
"rb");
1119 perror(
"CopyFile: ");
1122 out = fopen(dest,
"wb");
1123 if ( out == NULL ) {
1124 perror(
"CopyFile: ");
1128 while ( !feof(in) ) {
1129 countin = fread(buffer, 1, sumcount, in);
1130 if ( countin != sumcount ) {
1132 perror(
"CopyFile: ");
1136 countout = fwrite(buffer, 1, countin, out);
1137 if ( countin != countout ) {
1138 perror(
"CopyFile: ");
1145 if ( sumcount <= COPYFILEBUFSIZE ) {
1146 M_free(buffer,
"file copy buffer");
1166 RWLOCKW(AM.handlelock);
1168 if ( filelistsize == 0 ) {
1170 filelist = (FILES **)Malloc1(
sizeof(FILES *)*filelistsize,
"file handle");
1171 for ( j = 0; j < filelistsize; j++ ) filelist[j] = 0;
1175 else if ( numinfilelist >= filelistsize ) {
1176 VOID **fl = (VOID **)filelist;
1178 if ( DoubleList((VOID ***)(&fl),&filelistsize,(
int)
sizeof(FILES *),
1179 "list of open files") != 0 ) Terminate(-1);
1180 filelist = (FILES **)fl;
1181 for ( j = i; j < filelistsize; j++ ) filelist[j] = 0;
1182 numinfilelist = i + 1;
1186 for ( j = 0; j < filelistsize; j++ ) {
1187 if ( filelist[j] == 0 ) { i = j;
break; }
1191 filelist[i] = (FILES *)(filelist);
1197 if ( numinfilelist > MAX_OPEN_FILES ) {
1199 UNRWLOCK(AM.handlelock);
1201 MesPrint(
"More than %d open files",MAX_OPEN_FILES);
1202 Error0(
"System limit. This limit is not due to FORM!");
1206 UNRWLOCK(AM.handlelock);
1217LONG ReadFile(
int handle, UBYTE *buffer, LONG size)
1224 RWLOCKR(AM.handlelock);
1225 f = filelist[handle];
1226 UNRWLOCK(AM.handlelock);
1230 r = Uread(b,1,size,f);
1231 if ( r < 0 )
return(r);
1232 if ( r == 0 )
return(inbuf);
1234 if ( r == size )
return(inbuf);
1235 if ( r > size )
return(-1);
1267LONG ReadPosFile(PHEAD
FILEHANDLE *fi, UBYTE *buffer, LONG size, POSITION *pos)
1271 WORD *b = (WORD *)buffer, *t;
1273 if ( fi->handle < 0 ) {
1274 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + BASEPOSITION(*pos));
1276 while ( size > 0 && fi->POfill < fi->POfull ) { *b++ = *t++; size--; }
1279 if ( ISLESSPOS(*pos,fi->POposition) || ISGEPOSINC(*pos,fi->POposition,
1280 ((UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer))) ) {
1285 fi->POposition = *pos;
1286 LOCK(AS.inputslock);
1287 SeekFile(fi->handle,pos,SEEK_SET);
1288 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1289 UNLOCK(AS.inputslock);
1290 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1291 fi->POfill = fi->PObuffer;
1292 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1293 else AR.InHiBuf = retval/
sizeof(WORD);
1296 fi->POfill = (WORD *)((UBYTE *)(fi->PObuffer) + DIFBASE(*pos,fi->POposition));
1298 if ( fi->POfill + size <= fi->POfull ) {
1300 while ( size > 0 ) { *b++ = *t++; size--; }
1304 i = fi->POfull - fi->POfill; t = fi->POfill;
1305 if ( i > size ) i = size;
1307 while ( --i >= 0 ) *b++ = *t++;
1308 if ( size == 0 )
break;
1309 ADDPOS(fi->POposition,(UBYTE *)(fi->POfull)-(UBYTE *)(fi->PObuffer));
1310 LOCK(AS.inputslock);
1311 SeekFile(fi->handle,&(fi->POposition),SEEK_SET);
1312 retval = ReadFile(fi->handle,(UBYTE *)(fi->PObuffer),fi->POsize);
1313 UNLOCK(AS.inputslock);
1314 fi->POfull = fi->PObuffer+retval/
sizeof(WORD);
1315 fi->POfill = fi->PObuffer;
1316 if ( fi != AR.hidefile ) AR.InInBuf = retval/
sizeof(WORD);
1317 else AR.InHiBuf = retval/
sizeof(WORD);
1318 if ( retval == 0 ) { t = fi->POfill;
break; }
1322 retval = (UBYTE *)b - buffer;
1324 ADDPOS(*pos,retval);
1333LONG WriteFileToFile(
int handle, UBYTE *buffer, LONG size)
1336 LONG retval, totalwritten = 0, stilltowrite;
1337 RWLOCKR(AM.handlelock);
1338 f = filelist[handle];
1339 UNRWLOCK(AM.handlelock);
1340 while ( totalwritten < size ) {
1341 stilltowrite = size - totalwritten;
1345 retval = Uwrite((
char *)buffer+totalwritten,1,stilltowrite,f);
1346 if ( retval < 0 )
return(retval);
1347 if ( retval == 0 )
return(totalwritten);
1348 totalwritten += retval;
1353 return(totalwritten);
1357WRITEFILE WriteFile = &WriteFileToFile;
1371VOID SeekFile(
int handle, POSITION *offset,
int origin)
1374 RWLOCKR(AM.handlelock);
1375 f = filelist[handle];
1376 UNRWLOCK(AM.handlelock);
1380 if ( origin == SEEK_SET ) {
1381 Useek(f,BASEPOSITION(*offset),origin);
1382 SETBASEPOSITION(*offset,(Utell(f)));
1385 else if ( origin == SEEK_END ) {
1388 SETBASEPOSITION(*offset,(Utell(f)));
1396LONG TellFile(
int handle)
1399 TELLFILE(handle,&pos);
1403 return(BASEPOSITION(pos));
1406VOID TELLFILE(
int handle, POSITION *position)
1409 RWLOCKR(AM.handlelock);
1410 f = filelist[handle];
1411 UNRWLOCK(AM.handlelock);
1412 SETBASEPOSITION(*position,(Utell(f)));
1420void FlushFile(
int handle)
1423 RWLOCKR(AM.handlelock);
1424 f = filelist[handle];
1425 UNRWLOCK(AM.handlelock);
1434int GetPosFile(
int handle, fpos_t *pospointer)
1437 RWLOCKR(AM.handlelock);
1438 f = filelist[handle];
1439 UNRWLOCK(AM.handlelock);
1440 return(Ugetpos(f,pospointer));
1448int SetPosFile(
int handle, fpos_t *pospointer)
1451 RWLOCKR(AM.handlelock);
1452 f = filelist[handle];
1453 UNRWLOCK(AM.handlelock);
1454 return(Usetpos(f,(fpos_t *)pospointer));
1468VOID SynchFile(
int handle)
1471 if ( handle >= 0 ) {
1472 RWLOCKR(AM.handlelock);
1473 f = filelist[handle];
1474 UNRWLOCK(AM.handlelock);
1490VOID TruncateFile(
int handle)
1493 if ( handle >= 0 ) {
1494 RWLOCKR(AM.handlelock);
1495 f = filelist[handle];
1496 UNRWLOCK(AM.handlelock);
1510int GetChannel(
char *name,
int mode)
1515 for ( i = 0; i < NumOutputChannels; i++ ) {
1516 if ( channels[i].name == 0 )
continue;
1517 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1520 MesPrint(
"&File %s in print statement is not open",name);
1521 MesPrint(
" You should open it first with a #write or #append instruction");
1524 for ( i = 0; i < NumOutputChannels; i++ ) {
1525 if ( channels[i].name == 0 )
break;
1527 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1528 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1529 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1530 ch->
handle = CreateFile(name);
1531 RWLOCKR(AM.handlelock);
1532 f = filelist[ch->
handle];
1533 UNRWLOCK(AM.handlelock);
1546int GetAppendChannel(
char *name)
1551 for ( i = 0; i < NumOutputChannels; i++ ) {
1552 if ( channels[i].name == 0 )
continue;
1553 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 )
return(channels[i].handle);
1555 for ( i = 0; i < NumOutputChannels; i++ ) {
1556 if ( channels[i].name == 0 )
break;
1558 if ( i < NumOutputChannels ) { ch = &(channels[i]); }
1559 else { ch = (
CHANNEL *)FromList(&AC.ChannelList); }
1560 ch->
name = (
char *)strDup1((UBYTE *)name,
"name of channel");
1561 ch->
handle = OpenAddFile(name);
1562 RWLOCKR(AM.handlelock);
1563 f = filelist[ch->
handle];
1564 UNRWLOCK(AM.handlelock);
1576int CloseChannel(
char *name)
1579 for ( i = 0; i < NumOutputChannels; i++ ) {
1580 if ( channels[i].name == 0 )
continue;
1581 if ( channels[i].name[0] == 0 )
continue;
1582 if ( StrCmp((UBYTE *)name,(UBYTE *)(channels[i].name)) == 0 ) {
1583 CloseFile(channels[i].handle);
1584 M_free(channels[i].name,
"CloseChannel");
1585 channels[i].name = 0;
1612 POSITION position, sumsize;
1618 if ( PF.me != MASTER )
return;
1621 if ( AM.PrintTotalSize ) {
1626 scr = AB[0]->R.Fscr;
1630 for ( i = 0; i <=2; i++ ) {
1631 if ( scr[i].handle < 0 ) {
1632 SETBASEPOSITION(position,(scr[i].POfull-scr[i].PObuffer)*
sizeof(WORD));
1635 position = scr[i].filesize;
1637 ADD2POS(sumsize,position);
1646 for ( j = 0; j < AM.totalnumberofthreads; j++ ) {
1648 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1649 position = AT.SS->file.filesize;
1655 ADD2POS(sumsize,position);
1657 if ( AR.FoStage4[0].handle >= 0 ) {
1658 position = AR.FoStage4[0].filesize;
1659 ADD2POS(sumsize,position);
1664 if ( AT.SS && AT.SS->file.handle >= 0 ) {
1665 position = AT.SS->file.filesize;
1666 ADD2POS(sumsize,position);
1668 if ( AR.FoStage4[0].handle >= 0 ) {
1669 position = AR.FoStage4[0].filesize;
1670 ADD2POS(sumsize,position);
1676 ADD2POS(sumsize,AC.StoreFileSize);
1680 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) {
1682 LOCK(AS.MaxExprSizeLock);
1683 if ( ISLESSPOS(AS.MaxExprSize,sumsize) ) AS.MaxExprSize = sumsize;
1684 UNLOCK(AS.MaxExprSizeLock);
1686 AS.MaxExprSize = sumsize;
1700int StrCmp(UBYTE *s1, UBYTE *s2)
1702 while ( *s1 && *s1 == *s2 ) { s1++; s2++; }
1703 return((
int)*s1-(
int)*s2);
1711int StrICmp(UBYTE *s1, UBYTE *s2)
1713 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1714 return((
int)tolower(*s1)-(
int)tolower(*s2));
1722int StrHICmp(UBYTE *s1, UBYTE *s2)
1724 while ( *s1 && tolower(*s1) == *s2 ) { s1++; s2++; }
1725 return((
int)tolower(*s1)-(
int)(*s2));
1733int StrICont(UBYTE *s1, UBYTE *s2)
1735 while ( *s1 && tolower(*s1) == tolower(*s2) ) { s1++; s2++; }
1736 if ( *s1 == 0 )
return(0);
1737 return((
int)tolower(*s1)-(
int)tolower(*s2));
1745int CmpArray(WORD *t1, WORD *t2, WORD n)
1748 for ( i = 0; i < n; i++ ) {
1749 if ( ( x = (
int)(t1[i]-t2[i]) ) != 0 )
return(x);
1759int ConWord(UBYTE *s1, UBYTE *s2)
1761 while ( *s1 && ( tolower(*s1) == tolower(*s2) ) ) { s1++; s2++; }
1762 if ( *s1 == 0 )
return(1);
1774 while ( *s ) { s++; i++; }
1783VOID NumToStr(UBYTE *s, LONG x)
1788 if ( x < 0 ) { *s++ =
'-'; xx = -x; }
1791 *t++ = xx % 10 +
'0';
1794 while ( t > str ) *s++ = *--t;
1807VOID WriteString(
int type, UBYTE *str,
int num)
1811 if ( num > 0 && str[num-1] == 0 ) { num--; }
1812 else if ( num <= 0 || str[num-1] != LINEFEED ) {
1813 AddLineFeed(str,num);
1816 if(type == EXTERNALCHANNELOUT){
1817 if(WriteFile(0,str,num) != num) error = 1;
1820 if ( AM.silent == 0 || type == ERROROUT ) {
1821 if ( type == INPUTOUT ) {
1822 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1823 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1825 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1826 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1828 if ( error ) Terminate(-1);
1841VOID WriteUnfinString(
int type, UBYTE *str,
int num)
1846 if(type == EXTERNALCHANNELOUT){
1847 if(WriteFile(0,str,num) != num) error = 1;
1850 if ( AM.silent == 0 || type == ERROROUT ) {
1851 if ( type == INPUTOUT ) {
1852 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,(UBYTE *)
" ",4) != 4 ) error = 1;
1853 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,(UBYTE *)
" ",4) != 4 ) error = 1;
1855 if ( !AM.FileOnlyFlag && WriteFile(AM.StdOut,str,num) != num ) error = 1;
1856 if ( AC.LogHandle >= 0 && WriteFile(AC.LogHandle,str,num) != num ) error = 1;
1858 if ( error ) Terminate(-1);
1866UBYTE *AddToString(UBYTE *outstring, UBYTE *extrastring,
int par)
1868 UBYTE *s = extrastring, *t, *newstring;
1870 while ( *s ) { s++; }
1872 if ( outstring == 0 ) {
1874 t = outstring = (UBYTE *)Malloc1(n+1,
"AddToString");
1883 t = newstring = (UBYTE *)Malloc1(n+nn+2,
"AddToString");
1886 if ( par == 1 ) *t++ =
',';
1890 M_free(outstring,
"AddToString");
1904UBYTE *strDup1(UBYTE *instring,
char *ifwrong)
1906 UBYTE *s = instring, *to;
1908 to = s = (UBYTE *)Malloc1((s-instring)+1,ifwrong);
1909 while ( *instring ) *to++ = *instring++;
1919UBYTE *EndOfToken(UBYTE *s)
1922 while ( ( c = (UBYTE)(FG.cTable[*s]) ) == 0 || c == 1 ) s++;
1931UBYTE *ToToken(UBYTE *s)
1934 while ( *s && ( c = (UBYTE)(FG.cTable[*s]) ) != 0 && c != 1 ) s++;
1946UBYTE *SkipField(UBYTE *s,
int level)
1949 if ( *s ==
',' && level == 0 )
return(s);
1950 if ( *s ==
'(' ) level++;
1951 else if ( *s ==
')' ) { level--;
if ( level < 0 ) level = 0; }
1952 else if ( *s ==
'[' ) {
1955 else if ( *s ==
'{' ) {
1973WORD ReadSnum(UBYTE **p)
1978 if ( FG.cTable[*s] == 1 ) {
1980 x = ( x << 3 ) + ( x << 1 ) + ( *s++ -
'0' );
1981 if ( x > MAXPOSITIVE )
return(-1);
1982 }
while ( FG.cTable[*s] == 1 );
1997UBYTE *NumCopy(WORD y, UBYTE *to)
2007 do { *s++ = (UBYTE)((x % 10)+
'0'); i++; }
while ( ( x /= 10 ) != 0 );
2011 i = to[j]; to[j] = s[-j]; s[-j] = (UBYTE)i; j--;
2024char *LongCopy(LONG y,
char *to)
2034 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
2038 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
2051char *LongLongCopy(off_t *y,
char *to)
2064 if ( x < 0 ) { x = -x; *to++ =
'-'; }
2066 do { *s++ = (x % 10)+
'0'; i++; }
while ( ( x /= 10 ) != 0 );
2070 i = to[j]; to[j] = s[-j]; s[-j] = (char)i; j--;
2086static char notime[] =
"";
2095 return((UBYTE *)ctime(&tp));
2100 return((UBYTE *)ctime(&tp));
2102 return((UBYTE *)notime);
2116 case 0:
return(set->bit_0);
2117 case 1:
return(set->bit_1);
2118 case 2:
return(set->bit_2);
2119 case 3:
return(set->bit_3);
2120 case 4:
return(set->bit_4);
2121 case 5:
return(set->bit_5);
2122 case 6:
return(set->bit_6);
2123 case 7:
return(set->bit_7);
2137 case 0: set->bit_0=1;
break;
2138 case 1: set->bit_1=1;
break;
2139 case 2: set->bit_2=1;
break;
2140 case 3: set->bit_3=1;
break;
2141 case 4: set->bit_4=1;
break;
2142 case 5: set->bit_5=1;
break;
2143 case 6: set->bit_6=1;
break;
2144 case 7: set->bit_7=1;
break;
2158 case 0: set->bit_0=0;
break;
2159 case 1: set->bit_1=0;
break;
2160 case 2: set->bit_2=0;
break;
2161 case 3: set->bit_3=0;
break;
2162 case 4: set->bit_4=0;
break;
2163 case 5: set->bit_5=0;
break;
2164 case 6: set->bit_6=0;
break;
2165 case 7: set->bit_7=0;
break;
2182 case 0: set->bit_0=(set1->bit_0&&(!set2->bit_0));
break;
2183 case 1: set->bit_1=(set1->bit_1&&(!set2->bit_1));
break;
2184 case 2: set->bit_2=(set1->bit_2&&(!set2->bit_2));
break;
2185 case 3: set->bit_3=(set1->bit_3&&(!set2->bit_3));
break;
2186 case 4: set->bit_4=(set1->bit_4&&(!set2->bit_4));
break;
2187 case 5: set->bit_5=(set1->bit_5&&(!set2->bit_5));
break;
2188 case 6: set->bit_6=(set1->bit_6&&(!set2->bit_6));
break;
2189 case 7: set->bit_7=(set1->bit_7&&(!set2->bit_7));
break;
2190 case 8: set++;set1++;set2++;
2204 if ( mprotectInit() ) exit(0);
2217char *dummymessage =
"Malloc";
2221VOID *Malloc(LONG size)
2230 MesPrint(
"Asking for 0 bytes in Malloc");
2233 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2237 mem = (VOID *)M_alloc(size);
2240 MLOCK(ErrorMessageLock);
2242 Error0(
"No memory!");
2244 MUNLOCK(ErrorMessageLock);
2254 mallocsizes[nummalloclist] = size;
2255 mallocstrings[nummalloclist] = dummymessage;
2256 malloclist[nummalloclist++] = mem;
2257 if ( filelist ) MesPrint(
"Mem0 at 0x%x, %l bytes",mem,size);
2259 int i = nummalloclist-1;
2260 while ( --i >= 0 ) {
2261 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2262 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2263 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x"
2270 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2273 int j = nummalloclist-1, i;
2274 while ( --j >= 0 ) {
2275 t = (
char *)(malloclist[j]);
2276 u = t + mallocsizes[j];
2277 for ( i = 0; i < (int)BANNER; i++ ) {
2279 if ( *t != FILLVALUE || *u != FILLVALUE ) {
2280 MesPrint(
"Writing outside memory for %s",malloclist[i]);
2303VOID *Malloc1(LONG size,
const char *messageifwrong)
2312 MesPrint(
"%wAsking for 0 bytes in Malloc1");
2318 if ( ( size & 7 ) != 0 ) { size = size - ( size&7 ) + 8; }
2322 mem = (VOID *)M_alloc(size);
2325 MLOCK(ErrorMessageLock);
2327 Error1(
"No memory while allocating ",(UBYTE *)messageifwrong);
2329 MUNLOCK(ErrorMessageLock);
2339 mallocsizes[nummalloclist] = size;
2340 mallocstrings[nummalloclist] = (
char *)messageifwrong;
2341 malloclist[nummalloclist++] = mem;
2342 if ( AC.MemDebugFlag && filelist ) MesPrint(
"%wMem1 at 0x%x: %l bytes. %s",mem,size,messageifwrong);
2344 int i = nummalloclist-1;
2345 while ( --i >= 0 ) {
2346 if ( (
char *)mem < (((
char *)malloclist[i]) + mallocsizes[i])
2347 && (
char *)(malloclist[i]) < ((
char *)mem + size) ) {
2348 if ( filelist ) MesPrint(
"This memory overlaps with the block at 0x%x"
2354#ifdef MALLOCDEBUGOUTPUT
2355 printf (
"Malloc1: %s, allocated %li bytes at %.8lx\n",messageifwrong,size,(
unsigned long)mem);
2361 for ( i = 0; i < (int)BANNER; i++ ) { *t++ = FILLVALUE; *--u = FILLVALUE; }
2382void M_free(VOID *x,
const char *where)
2385 char *t = (
char *)x;
2388 x = (
void *)(((
char *)x)-BANNER);
2390 if ( AC.MemDebugFlag ) MesPrint(
"%wFreeing 0x%x: %s",x,where);
2392 for ( i = nummalloclist-1; i >= 0; i-- ) {
2393 if ( x == malloclist[i] ) {
2394 size = mallocsizes[i];
2395 for ( j = i+1; j < nummalloclist; j++ ) {
2396 malloclist[j-1] = malloclist[j];
2397 mallocsizes[j-1] = mallocsizes[j];
2398 mallocstrings[j-1] = mallocstrings[j];
2405 unsigned int xx = ((ULONG)x);
2406 printf(
"Error returning non-allocated address: 0x%x from %s\n"
2413 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2414 if ( *--t != FILLVALUE ) j++;
2417 LONG *tt = (LONG *)x;
2418 MesPrint(
"%w!!!!! Banner has been written in !!!!!: %x %x %x %x",
2419 tt[0],tt[1],tt[2],tt[3]);
2422 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2423 if ( *--t != FILLVALUE ) j++;
2426 LONG *tt = (LONG *)x;
2427 MesPrint(
"%w!!!!! Tail has been written in !!!!!: %x %x %x %x",
2428 tt[0],tt[1],tt[2],tt[3]);
2441#ifdef MALLOCDEBUGOUTPUT
2442 printf (
"M_free: %s, memory freed at %.8lx\n",where,(
unsigned long)x);
2447 mprotectFree((
void *)x);
2461void M_check1() { MesPrint(
"Checking Malloc"); M_check(); }
2465 int i,j,k,error = 0;
2468 for ( i = 0; i < nummalloclist; i++ ) {
2469 t = (
char *)(malloclist[i]);
2470 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2471 if ( *t++ != FILLVALUE ) j++;
2474 tt = (LONG *)(malloclist[i]);
2475 MesPrint(
"%w!!!!! Banner %d (%s) has been written in !!!!!: %x %x %x %x",
2476 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2477 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2480 t = (
char *)(malloclist[i]) + mallocsizes[i];
2481 for ( k = 0, j = 0; k < (int)BANNER; k++ ) {
2482 if ( *--t != FILLVALUE ) j++;
2486 MesPrint(
"%w!!!!! Tail %d (%s) has been written in !!!!!: %x %x %x %x",
2487 i,mallocstrings[i],tt[0],tt[1],tt[2],tt[3]);
2488 tt[0] = tt[1] = tt[2] = tt[3] = 0;
2491 if ( ( mallocstrings[i][0] ==
' ' ) || ( mallocstrings[i][0] ==
'#' ) ) {
2492 MesPrint(
"%w!!!!! Funny mallocstring");
2507 MesPrint(
"We have the following memory allocations left:");
2508 for ( i = 0; i < nummalloclist; i++ ) {
2509 MesPrint(
"0x%x: %l bytes. number %d: '%s'",malloclist[i],mallocsizes[i],i,mallocstrings[i]);
2546#define TERMMEMSTARTNUM 16
2547#define TERMEXTRAWORDS 10
2549VOID TermMallocAddMemory(PHEAD0)
2554 else extra = AT.TermMemMax;
2555 if ( AT.TermMemHeap ) M_free(AT.TermMemHeap,
"TermMalloc");
2556 newbufs = (WORD *)Malloc1(extra*(AM.MaxTer+TERMEXTRAWORDS*
sizeof(WORD)),
"TermMalloc");
2557 AT.TermMemHeap = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2558 for ( i = 0; i < extra; i++ ) {
2559 AT.TermMemHeap[i] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2561#ifdef TERMMALLOCDEBUG
2562 DebugHeap2 = (WORD **)Malloc1((extra+AT.TermMemMax)*
sizeof(WORD *),
"TermMalloc");
2563 for ( i = 0; i < AT.TermMemMax; i++ ) { DebugHeap2[i] = DebugHeap1[i]; }
2564 for ( i = 0; i < extra; i++ ) {
2565 DebugHeap2[i+AT.TermMemMax] = newbufs + i*(AM.MaxTer/
sizeof(WORD)+TERMEXTRAWORDS);
2567 if ( DebugHeap1 ) M_free(DebugHeap1,
"TermMalloc");
2568 DebugHeap1 = DebugHeap2;
2570 AT.TermMemTop = extra;
2571 AT.TermMemMax += extra;
2572#ifdef TERMMALLOCDEBUG
2573 MesPrint(
"AT.TermMemMax is now %l",AT.TermMemMax);
2579WORD *TermMalloc2(PHEAD
char *text)
2581 if ( AT.TermMemTop <= 0 ) TermMallocAddMemory(BHEAD0);
2583#ifdef TERMMALLOCDEBUG
2584 MesPrint(
"TermMalloc: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2587#ifdef MALLOCDEBUGOUTPUT
2588 MesPrint(
"TermMalloc: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,AT.TermMemHeap[AT.TermMemTop-1]);
2592 return(AT.TermMemHeap[--AT.TermMemTop]);
2595VOID TermFree2(PHEAD WORD *TermMem,
char *text)
2597#ifdef TERMMALLOCDEBUG
2601 for ( i = 0; i < AT.TermMemMax; i++ ) {
2602 if ( TermMem == DebugHeap1[i] )
break;
2604 if ( i >= AT.TermMemMax ) {
2605 MesPrint(
" ERROR: TermFree called with an address not given by TermMalloc.");
2610 AT.TermMemHeap[AT.TermMemTop++] = TermMem;
2612#ifdef TERMMALLOCDEBUG
2613 MesPrint(
"TermFree: %s, %d",text,(AT.TermMemMax-AT.TermMemTop));
2615#ifdef MALLOCDEBUGOUTPUT
2616 MesPrint(
"TermFree: %s, %l/%l (%x)",text,AT.TermMemTop,AT.TermMemMax,TermMem);
2646#define NUMBERMEMSTARTNUM 16
2647#define NUMBEREXTRAWORDS 10L
2649#ifdef TERMMALLOCDEBUG
2650UWORD **DebugHeap3, **DebugHeap4;
2653VOID NumberMallocAddMemory(PHEAD0)
2659 else extra = AT.NumberMemMax;
2660 if ( AT.NumberMemHeap ) M_free(AT.NumberMemHeap,
"NumberMalloc");
2661 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"NumberMalloc");
2662 AT.NumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"NumberMalloc");
2663 for ( i = 0; i < extra; i++ ) {
2664 AT.NumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2666#ifdef TERMMALLOCDEBUG
2667 DebugHeap4 = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(WORD *),
"NumberMalloc");
2668 for ( i = 0; i < AT.NumberMemMax; i++ ) { DebugHeap4[i] = DebugHeap3[i]; }
2669 for ( i = 0; i < extra; i++ ) {
2670 DebugHeap4[i+AT.NumberMemMax] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2672 if ( DebugHeap3 ) M_free(DebugHeap3,
"NumberMalloc");
2673 DebugHeap3 = DebugHeap4;
2675 AT.NumberMemTop = extra;
2676 AT.NumberMemMax += extra;
2684UWORD *NumberMalloc2(PHEAD
char *text)
2686 if ( AT.NumberMemTop <= 0 ) NumberMallocAddMemory(BHEAD text);
2688#ifdef MALLOCDEBUGOUTPUT
2689 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2690 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2694 return(AT.NumberMemHeap[--AT.NumberMemTop]);
2697VOID NumberFree2(PHEAD UWORD *NumberMem,
char *text)
2699#ifdef TERMMALLOCDEBUG
2701 for ( i = 0; i < AT.NumberMemMax; i++ ) {
2702 if ( NumberMem == DebugHeap3[i] )
break;
2704 if ( i >= AT.NumberMemMax ) {
2705 MesPrint(
" ERROR: NumberFree called with an address not given by NumberMalloc.");
2710 AT.NumberMemHeap[AT.NumberMemTop++] = NumberMem;
2712#ifdef MALLOCDEBUGOUTPUT
2713 if ( (AT.NumberMemMax-AT.NumberMemTop) > 10 )
2714 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2727VOID CacheNumberMallocAddMemory(PHEAD0)
2733 else extra = AT.CacheNumberMemMax;
2734 if ( AT.CacheNumberMemHeap ) M_free(AT.CacheNumberMemHeap,
"NumberMalloc");
2735 newbufs = (UWORD *)Malloc1(extra*(AM.MaxTal+NUMBEREXTRAWORDS)*
sizeof(UWORD),
"CacheNumberMalloc");
2736 AT.CacheNumberMemHeap = (UWORD **)Malloc1((extra+AT.NumberMemMax)*
sizeof(UWORD *),
"CacheNumberMalloc");
2737 for ( i = 0; i < extra; i++ ) {
2738 AT.CacheNumberMemHeap[i] = newbufs + i*(LONG)(AM.MaxTal+NUMBEREXTRAWORDS);
2740 AT.CacheNumberMemTop = extra;
2741 AT.CacheNumberMemMax += extra;
2746UWORD *CacheNumberMalloc2(PHEAD
char *text)
2748 if ( AT.CacheNumberMemTop <= 0 ) CacheNumberMallocAddMemory(BHEAD0);
2750#ifdef MALLOCDEBUGOUTPUT
2751 MesPrint(
"NumberMalloc: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,AT.NumberMemHeap[AT.NumberMemTop-1]);
2755 return(AT.CacheNumberMemHeap[--AT.CacheNumberMemTop]);
2758VOID CacheNumberFree2(PHEAD UWORD *NumberMem,
char *text)
2761 AT.CacheNumberMemHeap[AT.CacheNumberMemTop++] = NumberMem;
2763#ifdef MALLOCDEBUGOUTPUT
2764 MesPrint(
"NumberFree: %s, %l/%l (%x)",text,AT.NumberMemTop,AT.NumberMemMax,NumberMem);
2779VOID *FromList(
LIST *L)
2788 i = ( L->
num * L->
size ) /
sizeof(
int);
2789 old = (
int *)L->
lijst; newL = (
int *)newlist;
2790 while ( --i >= 0 ) *newL++ = *old++;
2791 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst FromList");
2805VOID *From0List(
LIST *L)
2813 i = ( L->
num * L->
size ) /
sizeof(
int);
2814 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2815 while ( --i >= 0 ) *newL++ = *old++;
2816 i = ( L->
maxnum - L->
num ) /
sizeof(
int);
2817 while ( --i >= 0 ) *newL++ = 0;
2818 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst From0List");
2834VOID *FromVarList(
LIST *L)
2840 else if ( L->
lijst ) {
2842 if ( L == &(AP.DollarList) ) {
2843 if ( L->
maxnum > MAXDOLLARVARIABLES ) L->
maxnum = MAXDOLLARVARIABLES;
2844 if ( L->
num >= MAXDOLLARVARIABLES ) {
2845 MesPrint(
"!!!More than %l objects in list of $-variables",
2846 MAXDOLLARVARIABLES);
2851 if ( L->
maxnum > MAXVARIABLES ) L->
maxnum = MAXVARIABLES;
2852 if ( L->
num >= MAXVARIABLES ) {
2853 MesPrint(
"!!!More than %l objects in list of variables",
2861 i = ( L->
num * L->
size ) /
sizeof(
int);
2862 old = (
int *)(L->
lijst); newL = (
int *)newlist;
2863 while ( --i >= 0 ) *newL++ = *old++;
2864 if ( L->
lijst ) M_free(L->
lijst,
"L->lijst from VarList");
2868 return( ((
char *)(L->
lijst)) + L->
size * ((L->
num)++) );
2876int DoubleList(VOID ***lijst,
int *oldsize,
int objectsize,
char *nameoftype)
2879 LONG i, newsize, fullsize;
2881 static LONG maxlistsize = (LONG)(MAXPOSITIVE);
2882 if ( *lijst == 0 ) {
2883 if ( *oldsize > 0 ) newsize = *oldsize;
2886 else newsize = *oldsize * 2;
2887 if ( newsize > maxlistsize ) {
2888 if ( *oldsize == maxlistsize ) {
2889 MesPrint(
"No memory for extra space in %s",nameoftype);
2892 newsize = maxlistsize;
2894 fullsize = ( newsize * objectsize +
sizeof(VOID *)-1 ) & (-
sizeof(VOID *));
2895 newlist = (VOID **)Malloc1(fullsize,nameoftype);
2897 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(VOID *);
2904 while ( --i >= 0 ) *to++ = *from++;
2906 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2928int DoubleLList(VOID ***lijst, LONG *oldsize,
int objectsize,
char *nameoftype)
2931 LONG i, newsize, fullsize;
2933 static LONG maxlistsize = (LONG)(MAXLONG);
2934 if ( *lijst == 0 ) {
2935 if ( *oldsize > 0 ) newsize = *oldsize;
2938 else newsize = *oldsize * 2;
2939 if ( newsize > maxlistsize ) {
2940 if ( *oldsize == maxlistsize ) {
2941 MesPrint(
"No memory for extra space in %s",nameoftype);
2944 newsize = maxlistsize;
2946 fullsize = ( newsize * objectsize +
sizeof(VOID *)-1 ) & (-
sizeof(VOID *));
2947 newlist = (VOID **)Malloc1(fullsize,nameoftype);
2949 to = newlist; from = *lijst; i = (*oldsize * objectsize)/
sizeof(VOID *);
2956 while ( --i >= 0 ) *to++ = *from++;
2958 if ( *lijst ) M_free(*lijst,
"DoubleLList");
2969#define DODOUBLE(x) { x *s, *t, *u; if ( *start ) { \
2970 oldsize = *(x **)stop - *(x **)start; newsize = 2*oldsize; \
2971 t = u = (x *)Malloc1(newsize*sizeof(x),text); s = *(x **)start; \
2972 for ( i = 0; i < oldsize; i++ ) *t++ = *s++; M_free(*start,"double"); } \
2973 else { newsize = 100; u = (x *)Malloc1(newsize*sizeof(x),text); } \
2974 *start = (void *)u; *stop = (void *)(u+newsize); }
2976void DoubleBuffer(
void **start,
void **stop,
int size,
char *text)
2978 LONG oldsize, newsize, i;
2979 if ( size ==
sizeof(
char) ) DODOUBLE(
char)
2980 else if ( size ==
sizeof(
short) ) DODOUBLE(
short)
2981 else if ( size ==
sizeof(
int) ) DODOUBLE(
int)
2982 else if ( size ==
sizeof(LONG) ) DODOUBLE(LONG)
2983 else if ( size %
sizeof(
int) == 0 ) DODOUBLE(
int)
2985 MesPrint(
"---Cannot handle doubling buffers of size %d",size);
2995#define DOEXPAND(x) { x *newbuffer, *t, *m; \
2996 t = newbuffer = (x *)Malloc1((newsize+2)*type,"ExpandBuffer"); \
2997 if ( *buffer ) { m = (x *)*buffer; i = *oldsize; \
2998 while ( --i >= 0 ) *t++ = *m++; M_free(*buffer,"ExpandBuffer"); \
2999 } *buffer = newbuffer; *oldsize = newsize; }
3001void ExpandBuffer(
void **buffer, LONG *oldsize,
int type)
3004 if ( *oldsize <= 0 ) { newsize = 100; }
3005 else newsize = 2*(*oldsize);
3006 if ( type ==
sizeof(
char) ) DOEXPAND(
char)
3007 else if ( type ==
sizeof(
short) ) DOEXPAND(
short)
3008 else if ( type ==
sizeof(
int) ) DOEXPAND(
int)
3009 else if ( type ==
sizeof(LONG) ) DOEXPAND(LONG)
3010 else if ( type ==
sizeof(POSITION) ) DOEXPAND(POSITION)
3012 MesPrint(
"---Cannot handle expanding buffers with objects of size %d",type);
3025LONG iexp(LONG x,
int p)
3030 if ( x == 0 )
return(0);
3031 if ( p == 0 )
return(1);
3032 sign = x < 0 ? -1 : 1;
3033 if ( sign < 0 && ( p & 1 ) == 0 ) sign = 1;
3035 if ( ux == 1 )
return(sign);
3036 if ( p < 0 )
return(0);
3039 if ( ( p & 1 ) != 0 ) y *= ux;
3043 if ( sign < 0 ) y = -y;
3044 return ULongToLong(y);
3056void ToGeneral(WORD *r, WORD *m, WORD par)
3060 else { m[1] = 0; m += ARGHEAD + 1; }
3064 if ( j >= FUNCTION ) { *m++ = j; *m++ = FUNHEAD; FILLFUN(m) }
3067 case SYMBOL: *m++ = j; *m++ = 4; *m++ = *r++; *m++ = 1;
break;
3069 if ( *r > 0 ) { *m++ = *r; *m++ = 1; *m++ = 3; }
3070 else if ( *r == 0 ) { m--; }
3071 else { *m++ = -*r; *m++ = 1; *m++ = -3; }
3078 *m++ = INDEX; *m++ = 3; *m++ = *r++;
3082 *m++ = 1; *m++ = 1; *m++ = k;
3085 if ( !par ) mm[ARGHEAD] = *mm-ARGHEAD;
3100int ToFast(WORD *r, WORD *m)
3103 if ( *r == ARGHEAD ) { *m++ = -SNUMBER; *m++ = 0;
return(1); }
3104 if ( *r != r[ARGHEAD]+ARGHEAD )
return(0);
3107 if ( r[2] != 1 || r[1] <= 0 )
return(0);
3108 *m++ = -SNUMBER; *m = ( r[3] < 0 ) ? -r[1] : r[1];
return(1);
3111 if ( r[i-1] != 1 || r[i-2] != 1 )
return(0);
3113 if ( r[i] == -3 && r[2] == *r-4 && r[2] == 3 && r[1] == INDEX
3114 && r[3] < MINSPEC ) {}
3117 else if ( r[2] != *r - 4 )
return(0);
3119 if ( *r >= FUNCTION ) {
3120 if ( r[1] <= FUNHEAD ) { *m++ = -*r;
return(1); }
3122 else if ( *r == SYMBOL ) {
3123 if ( r[1] == 4 && r[3] == 1 )
3124 { *m++ = -SYMBOL; *m++ = r[2];
return(1); }
3126 else if ( *r == INDEX ) {
3128 if ( r[2] >= MINSPEC ) {
3129 if ( r[2] >= 0 && r[2] < AM.OffsetIndex ) *m++ = -SNUMBER;
3133 if ( r[5] == -3 ) *m++ = -MINVECTOR;
3134 else *m++ = -VECTOR;
3153WORD ToPolyFunGeneral(PHEAD WORD *term)
3155 WORD *t = term+1, *tt, *to, *to1, *termout, *tstop, *tnext;
3156 WORD numarg, i, change = 0;
3157 tstop = term + *term; tstop -= ABS(tstop[-1]);
3158 termout = to = AT.WorkPointer;
3160 while ( t < tstop ) {
3161 if ( *t == AR.PolyFun ) {
3162 tt = t+FUNHEAD; tnext = t + t[1];
3164 while ( tt < tnext ) { numarg++; NEXTARG(tt); }
3165 if ( numarg == 2 ) {
3168 i = FUNHEAD; NCOPY(to,t,i);
3169 while ( tt < tnext ) {
3171 i = *tt; NCOPY(to,tt,i);
3173 else if ( *tt == -SYMBOL ) {
3174 to1[1] += 6+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3175 *to++ = 8+ARGHEAD; *to++ = 0; FILLARG(to);
3176 *to++ = 8; *to++ = SYMBOL; *to++ = 4; *to++ = tt[1];
3177 *to++ = 1; *to++ = 1; *to++ = 1; *to++ = 3;
3180 else if ( *tt == -SNUMBER ) {
3182 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3183 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3184 *to++ = 4; *to++ = tt[1]; *to++ = 1; *to++ = 3;
3187 else if ( tt[1] < 0 ) {
3188 to1[1] += 2+ARGHEAD; to1[2] |= MUSTCLEANPRF; change++;
3189 *to++ = 4+ARGHEAD; *to++ = 0; FILLARG(to);
3190 *to++ = 4; *to++ = -tt[1]; *to++ = 1; *to++ = -3;
3194 MLOCK(ErrorMessageLock);
3195 MesPrint(
"Internal error: Zero in PolyRatFun");
3196 MUNLOCK(ErrorMessageLock);
3205 i = t[1]; NCOPY(to,t,i)
3209 while ( t < tt ) *to++ = *t++;
3210 *termout = to - termout;
3211 t = term; i = *termout; tt = termout;
3213 AT.WorkPointer = term + *term;
3228int IsLikeVector(WORD *arg)
3230 WORD *sstop, *t, *tstop;
3232 if ( *arg == -VECTOR || *arg == -INDEX )
return(1);
3233 if ( *arg == -SNUMBER && arg[1] >= 0 && arg[1] < AM.OffsetIndex )
3237 sstop = arg + *arg; arg += ARGHEAD;
3238 while ( arg < sstop ) {
3240 tstop = t - ABS(t[-1]);
3242 while ( arg < tstop ) {
3243 if ( *arg == INDEX )
return(1);
3256int AreArgsEqual(WORD *arg1, WORD *arg2)
3259 if ( *arg2 != *arg1 )
return(0);
3262 while ( --i > 0 ) {
if ( arg1[i] != arg2[i] )
return(0); }
3265 else if ( *arg1 <= -FUNCTION )
return(1);
3266 else if ( arg1[1] == arg2[1] )
return(1);
3275int CompareArgs(WORD *arg1, WORD *arg2)
3279 if ( *arg2 < 0 )
return(-1);
3280 i1 = *arg1-ARGHEAD; arg1 += ARGHEAD;
3281 i2 = *arg2-ARGHEAD; arg2 += ARGHEAD;
3282 while ( i1 > 0 && i2 > 0 ) {
3283 if ( *arg1 != *arg2 )
return((
int)(*arg1)-(
int)(*arg2));
3284 i1--; i2--; arg1++; arg2++;
3288 else if ( *arg2 > 0 )
return(1);
3290 if ( *arg1 != *arg2 ) {
3291 if ( *arg1 < *arg2 )
return(-1);
3294 if ( *arg1 <= -FUNCTION )
return(0);
3295 return((
int)(arg1[1])-(
int)(arg2[1]));
3306int CompArg(WORD *s1, WORD *s2)
3309 WORD *st1, *st2, x[7];
3313 if ( *s1 <= -FUNCTION && *s2 <= -FUNCTION ) {
3314 if ( *s1 > *s2 )
return(-1);
3315 if ( *s1 < *s2 )
return(1);
3318 if ( *s1 > *s2 )
return(1);
3319 if ( *s1 < *s2 )
return(-1);
3320 if ( *s1 <= -FUNCTION )
return(0);
3322 if ( *s1 > *s2 )
return(1);
3323 if ( *s1 < *s2 )
return(-1);
3326 x[1] = AT.comsym[3];
3327 x[2] = AT.comnum[1];
3328 x[3] = AT.comnum[3];
3329 x[4] = AT.comind[3];
3330 x[5] = AT.comind[6];
3331 x[6] = AT.comfun[1];
3332 if ( *s1 == -SYMBOL ) {
3333 AT.comsym[3] = s1[1];
3334 st1 = AT.comsym+8; s1 = AT.comsym;
3336 else if ( *s1 == -SNUMBER ) {
3338 AT.comnum[1] = -s1[1]; AT.comnum[3] = -3;
3341 AT.comnum[1] = s1[1]; AT.comnum[3] = 3;
3346 else if ( *s1 == -INDEX || *s1 == -VECTOR ) {
3347 AT.comind[3] = s1[1]; AT.comind[6] = 3;
3348 st1 = AT.comind+7; s1 = AT.comind;
3350 else if ( *s1 == -MINVECTOR ) {
3351 AT.comind[3] = s1[1]; AT.comind[6] = -3;
3352 st1 = AT.comind+7; s1 = AT.comind;
3354 else if ( *s1 <= -FUNCTION ) {
3355 AT.comfun[1] = -*s1;
3356 st1 = AT.comfun+FUNHEAD+4; s1 = AT.comfun;
3362 else if ( *s1 == -ARGWILD ) {
3365 else {
goto argerror; }
3366 st2 = s2 + *s2; s2 += ARGHEAD;
3369 else if ( *s2 < 0 ) {
3370 x[1] = AT.comsym[3];
3371 x[2] = AT.comnum[1];
3372 x[3] = AT.comnum[3];
3373 x[4] = AT.comind[3];
3374 x[5] = AT.comind[6];
3375 x[6] = AT.comfun[1];
3376 if ( *s2 == -SYMBOL ) {
3377 AT.comsym[3] = s2[1];
3378 st2 = AT.comsym+8; s2 = AT.comsym;
3380 else if ( *s2 == -SNUMBER ) {
3382 AT.comnum[1] = -s2[1]; AT.comnum[3] = -3;
3385 else if ( s2[1] == 0 ) {
3386 st2 = AT.comnum+4; s2 = st2;
3389 AT.comnum[1] = s2[1]; AT.comnum[3] = 3;
3394 else if ( *s2 == -INDEX || *s2 == -VECTOR ) {
3395 AT.comind[3] = s2[1]; AT.comind[6] = 3;
3396 st2 = AT.comind+7; s2 = AT.comind;
3398 else if ( *s2 == -MINVECTOR ) {
3399 AT.comind[3] = s2[1]; AT.comind[6] = -3;
3400 st2 = AT.comind+7; s2 = AT.comind;
3402 else if ( *s2 <= -FUNCTION ) {
3403 AT.comfun[1] = -*s2;
3404 st2 = AT.comfun+FUNHEAD+4; s2 = AT.comfun;
3410 else if ( *s2 == -ARGWILD ) {
3413 else {
goto argerror; }
3414 st1 = s1 + *s1; s1 += ARGHEAD;
3418 x[1] = AT.comsym[3];
3419 x[2] = AT.comnum[1];
3420 x[3] = AT.comnum[3];
3421 x[4] = AT.comind[3];
3422 x[5] = AT.comind[6];
3423 x[6] = AT.comfun[1];
3424 st1 = s1 + *s1; st2 = s2 + *s2;
3425 s1 += ARGHEAD; s2 += ARGHEAD;
3427 while ( s1 < st1 && s2 < st2 ) {
3428 if ( ( k = CompareTerms(s1,s2,(WORD)2) ) != 0 ) {
3429 AT.comsym[3] = x[1];
3430 AT.comnum[1] = x[2];
3431 AT.comnum[3] = x[3];
3432 AT.comind[3] = x[4];
3433 AT.comind[6] = x[5];
3434 AT.comfun[1] = x[6];
3437 s1 += *s1; s2 += *s2;
3439 AT.comsym[3] = x[1];
3440 AT.comnum[1] = x[2];
3441 AT.comnum[3] = x[3];
3442 AT.comind[3] = x[4];
3443 AT.comind[6] = x[5];
3444 AT.comfun[1] = x[6];
3445 if ( s1 < st1 )
return(1);
3446 if ( s2 < st2 )
return(-1);
3451 MesPrint(
"Illegal type of short function argument in Normalize");
3452 Terminate(-1);
return(0);
3460#ifdef HAVE_CLOCK_GETTIME
3463#ifdef HAVE_GETTIMEOFDAY
3464#include <sys/time.h>
3466#include <sys/timeb.h>
3482#ifdef HAVE_CLOCK_GETTIME
3484 clock_gettime(CLOCK_MONOTONIC, &ts);
3487 return(((LONG)(ts.tv_sec)-AM.OldSecTime)*100 +
3488 ((LONG)(ts.tv_nsec / 1000000)-AM.OldMilliTime)/10);
3491 AM.OldSecTime = (LONG)(ts.tv_sec);
3492 AM.OldMilliTime = (LONG)(ts.tv_nsec / 1000000);
3496#ifdef HAVE_GETTIMEOFDAY
3499 gettimeofday(&t, NULL);
3500 sec = (LONG)t.tv_sec;
3501 msec = (LONG)(t.tv_usec/1000);
3503 return (sec-AM.OldSecTime)*100 + (msec-AM.OldMilliTime)/10;
3506 AM.OldSecTime = sec;
3507 AM.OldMilliTime = msec;
3515 return(((LONG)(tp.time)-AM.OldSecTime)*100 +
3516 ((LONG)(tp.millitm)-AM.OldMilliTime)/10);
3519 AM.OldSecTime = (LONG)(tp.time);
3520 AM.OldMilliTime = (LONG)(tp.millitm);
3532LONG TimeChildren(WORD par)
3534 if ( par )
return(Timer(1)-AM.OldChildTime);
3535 AM.OldChildTime = Timer(1);
3553 if ( par )
return(Timer(0)-AR.OldTime);
3554 AR.OldTime = Timer(0);
3567 static int initialized = 0;
3568 static HANDLE hProcess;
3569 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3572 if ( !initialized ) {
3573 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
3575 if ( GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3576 PFILETIME pftKernel = &ftKernel;
3577 PFILETIME pftUser = &ftUser;
3578 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3579 return (LONG)(t / 10000);
3585 FILETIME ftCreate, ftExit, ftKernel, ftUser;
3588 hThread = OpenThread(THREAD_QUERY_INFORMATION, FALSE, GetCurrentThreadId());
3590 if ( GetThreadTimes(hThread, &ftCreate, &ftExit, &ftKernel, &ftUser) ) {
3591 PFILETIME pftKernel = &ftKernel;
3592 PFILETIME pftUser = &ftUser;
3593 __int64 t = *(__int64 *)pftKernel + *(__int64 *)pftUser;
3594 lResult = (LONG)(t / 10000);
3596 CloseHandle(hThread);
3603#include <sys/time.h>
3604#include <sys/resource.h>
3605#ifdef WITHPOSIXCLOCK
3614#ifdef WITHPOSIXCLOCK
3633 if ( clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t) ) {
3634 MesPrint(
"Error in getting timing information");
3636 return (LONG)t.tv_sec * 1000 + (LONG)t.tv_nsec / 1000000;
3640 struct rusage rusage;
3642 getrusage(RUSAGE_CHILDREN,&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));
3647 getrusage(RUSAGE_SELF,&rusage);
3648 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3649 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3656#include <sys/time.h>
3657#include <sys/resource.h>
3661 struct rusage rusage;
3663 getrusage(RUSAGE_CHILDREN,&rusage);
3664 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3665 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3668 getrusage(RUSAGE_SELF,&rusage);
3669 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3670 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3675#include <sys/time.h>
3676#include <sys/resource.h>
3680 struct rusage rusage;
3682 getrusage(RUSAGE_CHILDREN,&rusage);
3683 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3684 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3687 getrusage(RUSAGE_SELF,&rusage);
3688 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3689 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3705#include <sys/time.h>
3706#include <sys/resource.h>
3707 struct rusage rusage;
3709 getrusage(RUSAGE_CHILDREN,&rusage);
3710 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3711 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3714 getrusage(RUSAGE_SELF,&rusage);
3715 return(((LONG)(rusage.ru_utime.tv_sec)+(LONG)(rusage.ru_stime.tv_sec))*1000
3716 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3720 clock_t tikken = clock();
3721 return((LONG)tikken/1000);
3723 clock_t t, tikken = clock();
3724 t = tikken % CLK_TCK;
3727 tikken += (t*1000)/CLK_TCK;
3735void times(tbuffer_t *buffer);
3741 if ( par == 1 ) {
return(0); }
3744 return(buffer.proc_user_time * 10);
3757#include <sys/types.h>
3758#include <sys/times.h>
3763#include <sys/time.h>
3764#include <sys/resource.h>
3772 if ( par == 1 ) {
return(0); }
3774 if ( ( AO.wrapnum & 1 ) != 0 ) t ^= 0x80000000;
3780 return(AO.wrap+(t/1000));
3785 ULONG a1, a2, a3, a4;
3786 if ( par == 1 ) {
return(0); }
3788 a1 = (ULONG)buffer.tms_utime;
3792 a2 = 1000*a2 + (a3 >> 16);
3797 ret = (LONG)((a4 << 16) + a3 / CLK_TCK);
3803 struct timezone tzp;
3804 if ( par == 1 ) {
return(0); }
3805 gettimeofday(&tp,&tzp); */
3806 return(tp.tv_sec*1000+tp.tv_usec/1000);
3808 struct rusage rusage;
3810 getrusage(RUSAGE_CHILDREN,&rusage);
3811 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3812 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3815 getrusage(RUSAGE_SELF,&rusage);
3816 return((rusage.ru_utime.tv_sec+rusage.ru_stime.tv_sec)*1000
3817 +(rusage.ru_utime.tv_usec/1000+rusage.ru_stime.tv_usec/1000));
3863 int errorcode = 0, coeffsize;
3864 WORD *t, *tt, *tstop, *endterm, *targ, *targstop, *funstop, *argterm;
3865 endterm = term + *term;
3866 coeffsize = ABS(endterm[-1]);
3867 if ( coeffsize >= *term ) {
3868 MLOCK(ErrorMessageLock);
3869 MesPrint(
"TestTerm: Internal inconsistency in term. Coefficient too big.");
3870 MUNLOCK(ErrorMessageLock);
3874 if ( ( coeffsize < 3 ) || ( ( coeffsize & 1 ) != 1 ) ) {
3875 MLOCK(ErrorMessageLock);
3876 MesPrint(
"TestTerm: Internal inconsistency in term. Wrong size coefficient.");
3877 MUNLOCK(ErrorMessageLock);
3882 tstop = endterm - coeffsize;
3883 while ( t < tstop ) {
3894 MLOCK(ErrorMessageLock);
3895 MesPrint(
"TestTerm: Internal inconsistency in term. L or S number");
3896 MUNLOCK(ErrorMessageLock);
3902 case DOLLAREXPRESSION:
3915 MLOCK(ErrorMessageLock);
3916 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm.");
3917 MUNLOCK(ErrorMessageLock);
3925 MLOCK(ErrorMessageLock);
3926 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm number.");
3927 MUNLOCK(ErrorMessageLock);
3934 if ( *t-FUNCTION >= NumFunctions ) {
3935 MLOCK(ErrorMessageLock);
3936 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number");
3937 MUNLOCK(ErrorMessageLock);
3942 if ( funstop > tstop )
goto subtermsize;
3944 MLOCK(ErrorMessageLock);
3945 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag nonzero.");
3946 MUNLOCK(ErrorMessageLock);
3951 if ( targ > funstop ) {
3952 MLOCK(ErrorMessageLock);
3953 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function size.");
3954 MUNLOCK(ErrorMessageLock);
3958 if ( functions[*t-FUNCTION].spec >= TENSORFUNCTION ) {
3961 while ( targ < funstop ) {
3963 if ( *targ <= -(FUNCTION+NumFunctions) ) {
3964 MLOCK(ErrorMessageLock);
3965 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal function number in argument.");
3966 MUNLOCK(ErrorMessageLock);
3970 if ( *targ <= -FUNCTION ) { targ++; }
3972 if ( ( *targ != -SYMBOL ) && ( *targ != -VECTOR )
3973 && ( *targ != -MINVECTOR )
3974 && ( *targ != -SNUMBER )
3975 && ( *targ != -ARGWILD )
3976 && ( *targ != -INDEX ) ) {
3977 MLOCK(ErrorMessageLock);
3978 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal object in argument.");
3979 MUNLOCK(ErrorMessageLock);
3986 else if ( ( *targ < ARGHEAD ) || ( targ+*targ > funstop ) ) {
3987 MLOCK(ErrorMessageLock);
3988 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal size of argument.");
3989 MUNLOCK(ErrorMessageLock);
3993 else if ( targ[1] != 0 ) {
3994 MLOCK(ErrorMessageLock);
3995 MesPrint(
"TestTerm: Internal inconsistency in term. Dirty flag in argument.");
3996 MUNLOCK(ErrorMessageLock);
4001 targstop = targ + *targ;
4002 argterm = targ + ARGHEAD;
4003 while ( argterm < targstop ) {
4004 if ( ( *argterm < 4 ) || ( argterm + *argterm > targstop ) ) {
4005 MLOCK(ErrorMessageLock);
4006 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal termsize in argument.");
4007 MUNLOCK(ErrorMessageLock);
4012 MLOCK(ErrorMessageLock);
4013 MesPrint(
"TestTerm: Internal inconsistency in term. Called from TestTerm.");
4014 MUNLOCK(ErrorMessageLock);
4018 argterm += *argterm;
4029 MLOCK(ErrorMessageLock);
4030 MesPrint(
"TestTerm: Internal inconsistency in term. Illegal subterm size.");
4031 MUNLOCK(ErrorMessageLock);
int PutPreVar(UBYTE *, UBYTE *, UBYTE *, int)
int PF_Bcast(void *buffer, int count)
LONG PF_WriteFileToFile(int handle, UBYTE *buffer, LONG size)
LONG PF_BroadcastNumber(LONG x)
struct bit_field * one_byte
struct bit_field set_of_char[32]