89 int CoCreateSpectator(UBYTE *inp)
91 UBYTE *p, *q, *filename, c, cc;
92 WORD c1, c2, numexpr = 0, specnum, HadOne = 0;
94 while ( *inp ==
',' ) inp++;
95 if ( ( q = SkipAName(inp) ) == 0 || q[-1] ==
'_' ) {
96 MesPrint(
"&Illegal name for expression");
100 if ( GetVar(inp,&c1,&c2,ALLVARIABLES,NOAUTO) != NAMENOTFOUND ) {
101 if ( c2 == CEXPRESSION &&
102 Expressions[c1].status == DROPSPECTATOREXPRESSION ) {
104 Expressions[numexpr].status = SPECTATOREXPRESSION;
108 MesPrint(
"&The name %s has been used already.",inp);
114 while ( *p ==
',' ) p++;
115 if ( *p !=
'"' )
goto Syntax;
117 while ( *p && *p !=
'"' ) {
118 if ( *p ==
'\\' ) p++;
121 if ( *p !=
'"' )
goto Syntax;
123 while ( *q && ( *q ==
',' || *q ==
' ' || *q ==
'\t' ) ) q++;
124 if ( *q )
goto Syntax;
130 numexpr = EntVar(CEXPRESSION,inp,SPECTATOREXPRESSION,0,0,0);
131 fh = AllocFileHandle(1,(
char *)filename);
135 if ( AM.NumSpectatorFiles >= AM.SizeForSpectatorFiles || AM.SpectatorFiles == 0 ) {
138 if ( AM.SizeForSpectatorFiles == 0 ) {
140 AM.NumSpectatorFiles = AM.SizeForSpectatorFiles = 0;
142 else newsize = AM.SizeForSpectatorFiles*2;
144 for ( i = 0; i < AM.NumSpectatorFiles; i++ )
145 newspectators[i] = AM.SpectatorFiles[i];
146 for ( ; i < newsize; i++ ) {
147 newspectators[i].fh = 0;
148 newspectators[i].name = 0;
149 newspectators[i].exprnumber = -1;
150 newspectators[i].flags = 0;
151 PUTZERO(newspectators[i].position);
152 PUTZERO(newspectators[i].readpos);
154 AM.SizeForSpectatorFiles = newsize;
155 if ( AM.SpectatorFiles != 0 ) M_free(AM.SpectatorFiles,
"Spectators");
156 AM.SpectatorFiles = newspectators;
157 specnum = AM.NumSpectatorFiles++;
160 for ( specnum = 0; specnum < AM.SizeForSpectatorFiles; specnum++ ) {
161 if ( AM.SpectatorFiles[specnum].name == 0 )
break;
163 AM.NumSpectatorFiles++;
165 PUTZERO(AM.SpectatorFiles[specnum].position);
166 AM.SpectatorFiles[specnum].name = (
char *)(strDup1(inp,
"Spectator expression name"));
167 AM.SpectatorFiles[specnum].fh = fh;
168 AM.SpectatorFiles[specnum].exprnumber = numexpr;
172 MesPrint(
"&Proper syntax is: CreateSpectator,exprname,\"filename\";");
181 int CoToSpectator(UBYTE *inp)
186 while ( *inp ==
',' ) inp++;
187 if ( ( q = SkipAName(inp) ) == 0 || q[-1] ==
'_' ) {
188 MesPrint(
"&Illegal name for expression");
191 if ( *q != 0 )
goto Syntax;
192 if ( GetVar(inp,&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
193 c1 != CEXPRESSION ) {
194 MesPrint(
"&%s is not a valid expression.",inp);
197 if ( Expressions[numexpr].status != SPECTATOREXPRESSION ) {
198 MesPrint(
"&%s is not an active spectator.",inp);
201 for ( i = 0; i < AM.SizeForSpectatorFiles; i++ ) {
202 if ( AM.SpectatorFiles[i].name != 0 ) {
203 if ( StrCmp((UBYTE *)(AM.SpectatorFiles[i].name),(UBYTE *)(inp)) == 0 )
break;
206 if ( i >= AM.SizeForSpectatorFiles ) {
207 MesPrint(
"&Spectator %s not found.",inp);
210 if ( ( AM.SpectatorFiles[i].flags & READSPECTATORFLAG ) != 0 ) {
211 MesPrint(
"&Spectator %s: It is not permitted to read from and write to the same spectator in one module.",inp);
214 AM.SpectatorFiles[i].exprnumber = numexpr;
215 Add3Com(TYPETOSPECTATOR,i);
220 AC.mparallelflag |= NOPARALLEL_SPECTATOR;
224 MesPrint(
"&Proper syntax is: ToSpectator,exprname;");
233 int CoRemoveSpectator(UBYTE *inp)
239 while ( *inp ==
',' ) inp++;
240 if ( ( q = SkipAName(inp) ) == 0 || q[-1] ==
'_' ) {
241 MesPrint(
"&Illegal name for expression");
244 if ( *q != 0 )
goto Syntax;
245 if ( GetVar(inp,&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
246 c1 != CEXPRESSION ) {
247 MesPrint(
"&%s is not a valid expression.",inp);
250 if ( Expressions[numexpr].status != SPECTATOREXPRESSION ) {
251 MesPrint(
"&%s is not a spectator.",inp);
254 for ( i = 0; i < AM.SizeForSpectatorFiles; i++ ) {
255 if ( StrCmp((UBYTE *)(AM.SpectatorFiles[i].name),(UBYTE *)(inp)) == 0 )
break;
257 if ( i >= AM.SizeForSpectatorFiles ) {
258 MesPrint(
"&Spectator %s not found.",inp);
261 sp = AM.SpectatorFiles+i;
262 Expressions[numexpr].status = DROPSPECTATOREXPRESSION;
263 if ( sp->fh->
handle != -1 ) {
264 CloseFile(sp->fh->
handle);
266 remove(sp->fh->name);
268 M_free(sp->fh,
"Temporary FileHandle");
269 M_free(sp->name,
"Spectator expression name");
271 PUTZERO(sp->position);
272 PUTZERO(sp->readpos);
277 AM.NumSpectatorFiles--;
280 MesPrint(
"&Proper syntax is: RemoveSpectator,exprname;");
289 int CoEmptySpectator(UBYTE *inp)
295 while ( *inp ==
',' ) inp++;
296 if ( ( q = SkipAName(inp) ) == 0 || q[-1] ==
'_' ) {
297 MesPrint(
"&Illegal name for expression");
300 if ( *q != 0 )
goto Syntax;
301 if ( GetVar(inp,&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
302 c1 != CEXPRESSION ) {
303 MesPrint(
"&%s is not a valid expression.",inp);
306 if ( Expressions[numexpr].status != SPECTATOREXPRESSION ) {
307 MesPrint(
"&%s is not a spectator.",inp);
310 for ( i = 0; i < AM.SizeForSpectatorFiles; i++ ) {
311 if ( StrCmp((UBYTE *)(AM.SpectatorFiles[i].name),(UBYTE *)(inp)) == 0 )
break;
313 if ( i >= AM.SizeForSpectatorFiles ) {
314 MesPrint(
"&Spectator %s not found.",inp);
317 sp = AM.SpectatorFiles+i;
318 if ( sp->fh->
handle != -1 ) {
319 CloseFile(sp->fh->
handle);
321 remove(sp->fh->name);
323 sp->fh->POfill = sp->fh->POfull = sp->fh->PObuffer;
324 PUTZERO(sp->position);
325 PUTZERO(sp->readpos);
328 MesPrint(
"&Proper syntax is: EmptySpectator,exprname;");
341 int PutInSpectator(WORD *term,WORD specnum)
346 SPECTATOR *sp = &(AM.SpectatorFiles[specnum]);
349 if ( ( i = *term ) <= 0 )
return(0);
350 LOCK(fi->pthreadslock);
354 if ( p >= fi->POstop ) {
356 if ( ( RetCode = CreateFile(fi->name) ) >= 0 ) {
357 fi->
handle = (WORD)RetCode;
358 PUTZERO(fi->filesize);
359 PUTZERO(fi->POposition);
362 MLOCK(ErrorMessageLock);
363 MesPrint(
"Cannot create spectator file %s",fi->name);
364 MUNLOCK(ErrorMessageLock);
365 UNLOCK(fi->pthreadslock);
369 SeekFile(fi->
handle,&(sp->position),SEEK_SET);
370 if ( ( RetCode = WriteFile(fi->
handle,(UBYTE *)(fi->PObuffer),fi->POsize) ) != fi->POsize ) {
371 MLOCK(ErrorMessageLock);
372 MesPrint(
"Error during spectator write. Disk full?");
373 MesPrint(
"Attempt to write %l bytes on file %d at position %15p",
374 fi->POsize,fi->
handle,&(fi->POposition));
375 MesPrint(
"RetCode = %l, Buffer address = %l",RetCode,(LONG)(fi->PObuffer));
376 MUNLOCK(ErrorMessageLock);
377 UNLOCK(fi->pthreadslock);
380 ADDPOS(fi->filesize,fi->POsize);
382 ADDPOS(sp->position,fi->POsize);
383 fi->POposition = sp->position;
387 fi->POfull = fi->POfill = p;
388 Expressions[AM.SpectatorFiles[specnum].exprnumber].counter++;
389 UNLOCK(fi->pthreadslock);
398 void FlushSpectators(VOID)
405 if ( AM.NumSpectatorFiles <= 0 )
return;
406 for ( i = 0; i < AM.SizeForSpectatorFiles; i++, sp++ ) {
407 if ( sp->name == 0 )
continue;
409 if ( ( sp->flags & READSPECTATORFLAG ) != 0 ) {
410 sp->flags &= ~READSPECTATORFLAG;
411 fh->POfill = fh->PObuffer;
413 SeekFile(fh->
handle,&(sp->position),SEEK_SET);
414 fh->POposition = sp->position;
418 if ( fh->POfill <= fh->PObuffer )
continue;
420 if ( ( RetCode = CreateFile(fh->name) ) >= 0 ) {
421 PUTZERO(fh->filesize);
422 PUTZERO(fh->POposition);
423 fh->
handle = (WORD)RetCode;
426 MLOCK(ErrorMessageLock);
427 MesPrint(
"Cannot create spectator file %s",fh->name);
428 MUNLOCK(ErrorMessageLock);
431 PUTZERO(sp->position);
433 SeekFile(fh->
handle,&(sp->position),SEEK_SET);
434 size = (fh->POfill - fh->PObuffer)*
sizeof(WORD);
435 if ( ( RetCode = WriteFile(fh->
handle,(UBYTE *)(fh->PObuffer),size) ) != size ) {
436 MLOCK(ErrorMessageLock);
437 MesPrint(
"Write error synching spectator file. Disk full?");
438 MesPrint(
"Attempt to write %l bytes on file %s at position %15p",
439 size,fh->name,&(sp->position));
440 MUNLOCK(ErrorMessageLock);
443 fh->POfill = fh->PObuffer;
444 SeekFile(fh->
handle,&(sp->position),SEEK_END);
445 fh->POposition = sp->position;
455 int CoCopySpectator(UBYTE *inp)
458 UBYTE *q, c, *exprname, *p;
459 WORD c1, c2, numexpr;
460 int specnum, error = 0;
462 while ( *inp ==
',' ) inp++;
463 if ( ( q = SkipAName(inp) ) == 0 || q[-1] ==
'_' ) {
464 MesPrint(
"&Illegal name for expression");
467 if ( *q == 0 )
goto Syntax;
469 if ( GetVar(inp,&c1,&c2,ALLVARIABLES,NOAUTO) != NAMENOTFOUND ) {
470 MesPrint(
"&%s is the name of an existing variable.",inp);
473 numexpr = EntVar(CEXPRESSION,inp,LOCALEXPRESSION,0,0,0);
477 while ( *q ==
' ' || *q ==
',' || *q ==
'\t' ) q++;
478 if ( *q !=
'=' )
goto Syntax;
480 while ( *q ==
' ' || *q ==
',' || *q ==
'\t' ) q++;
482 if ( ( q = SkipAName(inp) ) == 0 || q[-1] ==
'_' ) {
483 MesPrint(
"&Illegal name for spectator expression");
486 if ( *q != 0 )
goto Syntax;
487 if ( AM.NumSpectatorFiles <= 0 ) {
488 MesPrint(
"&CopySpectator: There are no spectator expressions!");
491 sp = AM.SpectatorFiles;
492 for ( specnum = 0; specnum < AM.SizeForSpectatorFiles; specnum++, sp++ ) {
493 if ( sp->name != 0 ) {
494 if ( StrCmp((UBYTE *)(sp->name),(UBYTE *)(inp)) == 0 )
break;
497 if ( specnum >= AM.SizeForSpectatorFiles ) {
498 MesPrint(
"&Spectator %s not found.",inp);
501 sp->flags |= READSPECTATORFLAG;
502 PUTZERO(sp->fh->POposition);
503 PUTZERO(sp->readpos);
504 sp->fh->POfill = sp->fh->PObuffer;
505 if ( sp->fh->
handle >= 0 ) {
506 SeekFile(sp->fh->
handle,&(sp->fh->POposition),SEEK_SET);
520 OldWork = w = AT.WorkPointer;
521 *w++ = TYPEEXPRESSION;
525 AR.CurExpr = numexpr;
526 *w++ = SUBEXPRESSION;
536 SeekScratch(AR.outfile,&pos);
537 Expressions[numexpr].counter = 1;
538 Expressions[numexpr].onfile = pos;
539 Expressions[numexpr].whichbuffer = 0;
541 Expressions[numexpr].partodo = AC.inparallelflag;
543 OldWork[2] = w - OldWork - 3;
546 if (
PutOut(BHEAD OldWork+2,&pos,AR.outfile,0) < 0 ) {
548 MesPrint(
"&Cannot create expression %s",exprname);
553 OldWork[2] = 4+SUBEXPSIZE;
554 OldWork[4] = SUBEXPSIZE;
555 OldWork[5] = numexpr;
556 OldWork[SUBEXPSIZE+3] = 1;
557 OldWork[SUBEXPSIZE+4] = 1;
558 OldWork[SUBEXPSIZE+5] = 3;
559 OldWork[SUBEXPSIZE+6] = 0;
560 if (
PutOut(BHEAD OldWork+2,&pos,AR.outfile,0) < 0
563 MesPrint(
"&Cannot create expression %s",exprname);
567 AR.outfile->POfull = AR.outfile->POfill;
569 OldWork[2] = numexpr;
571 AT.WorkPointer = OldWork;
572 if ( AC.dumnumflag ) Add2Com(TYPEDETCURDUM)
578 AC.mparallelflag |= NOPARALLEL_SPECTATOR;
582 MesPrint(
"&Proper syntax is: CopySpectator,exprname=spectatorname;");
593 WORD GetFromSpectator(WORD *term,WORD specnum)
595 SPECTATOR *sp = &(AM.SpectatorFiles[specnum]);
597 WORD i, size, *t = term;
599 if ( fh-> handle < 0 ) {
609 if ( ISZEROPOS(sp->readpos) ) {
611 SeekFile(fh->
handle,&(sp->readpos),SEEK_SET);
612 InIn = ReadFile(fh->
handle,(UBYTE *)(fh->PObuffer),fh->POsize);
613 if ( InIn < 0 || ( InIn & 1 ) ) {
614 MLOCK(ErrorMessageLock);
615 MesPrint(
"Error reading information for %s spectator",sp->name);
616 MUNLOCK(ErrorMessageLock);
619 InIn /=
sizeof(WORD);
620 if ( InIn == 0 ) { *term = 0;
return(0); }
621 SeekFile(fh->
handle,&(sp->readpos),SEEK_CUR);
622 fh->POposition = sp->readpos;
623 fh->POfull = fh->PObuffer+InIn;
624 fh->POfill = fh->PObuffer;
626 if ( fh->POfill == fh->POfull ) {
627 if ( ISLESSPOS(sp->readpos,sp->position) )
goto FillBuffer;
631 size = *fh->POfill++; *t++ = size;
632 for ( i = 1; i < size; i++ ) {
633 if ( fh->POfill >= fh->POfull ) {
634 SeekFile(fh->
handle,&(sp->readpos),SEEK_SET);
635 InIn = ReadFile(fh->
handle,(UBYTE *)(fh->PObuffer),fh->POsize);
636 if ( InIn < 0 || ( InIn & 1 ) ) {
637 MLOCK(ErrorMessageLock);
638 MesPrint(
"Error reading information for %s spectator",sp->name);
639 MUNLOCK(ErrorMessageLock);
642 InIn /=
sizeof(WORD);
644 MLOCK(ErrorMessageLock);
645 MesPrint(
"Reading incomplete information for %s spectator",sp->name);
646 MUNLOCK(ErrorMessageLock);
649 SeekFile(fh->
handle,&(sp->readpos),SEEK_CUR);
650 fh->POposition = sp->readpos;
651 fh->POfull = fh->PObuffer+InIn;
652 fh->POfill = fh->PObuffer;
654 *t++ = *fh->POfill++;
667 void ClearSpectators(WORD par)
672 if ( AM.NumSpectatorFiles > 0 ) {
673 for ( i = 0; i < AM.SizeForSpectatorFiles; i++, sp++ ) {
674 if ( sp->name == 0 )
continue;
675 if ( ( sp->flags & GLOBALSPECTATORFLAG ) == 1 && par == STOREMODULE )
continue;
677 if ( GetVar((UBYTE *)(sp->name),&c1,&numexpr,ALLVARIABLES,NOAUTO) == NAMENOTFOUND ||
678 c1 != CEXPRESSION ) {
679 MesPrint(
"&%s is not a valid expression.",sp->name);
682 Expressions[numexpr].status = DROPPEDEXPRESSION;
683 if ( sp->fh->
handle != -1 ) {
684 CloseFile(sp->fh->
handle);
686 remove(sp->fh->name);
688 M_free(sp->fh,
"Temporary FileHandle");
689 M_free(sp->name,
"Spectator expression name");
690 PUTZERO(sp->position);
695 AM.NumSpectatorFiles--;
WORD PutOut(PHEAD WORD *, POSITION *, FILEHANDLE *, WORD)
WORD FlushOut(POSITION *, FILEHANDLE *, int)