void uadd( char *item ) { /* *+ * Name: * uadd * * Purpose: * Add a string to the list of unoutput strings. * * Description: * This routine adds a string to the list of unoutput strings. Every * character encountered by the lexer should be added to this list * as it is encountered. * * Parameters: * item = char * * The string to be added. * * Authors: * MBT: Mark Taylor (STARLINK) * * History: * 10-DEC-1999 (MBT): * Initial revision. *- */ ulast->next = (ELEMENT *) memok( malloc( sizeof( ELEMENT ) ) ); ulast = ulast->next; ulast->next = NULL; ulast->text = (char *) memok( malloc( strlen( item ) + 1 ) ); strcpy( ulast->text, item ); }
void cappend( char c ) { /* *+ * Name: * cappend * * Purpose: * Append a character to the preval string. * * Description: * This routine appends a single character to the preval string. * If the character is '<', '>' or '&', then it is replaced in the * preval string by the appropriate HTML entity reference. * * Authors: * MBT: Mark Taylor (STARLINK) * * History: * 10-DEC-1999 (MBT): * Initial revision. *- */ /* Switch on the value of the character. */ switch( c ) { /* If it needs to be replaced by an entity reference, do so via sappend. */ case '<': sappend( "<" ); break; case '>': sappend( ">" ); break; case '&': sappend( "&" ); break; /* Otherwise it's just a single character: extend allocation if necessary and add the new character. */ default: if ( preleng + 1 > prealloc ) { if ( prealloc == 0 ) preval = (char *) memok( malloc( BUFINC + 1 ) ); else preval = (char *) memok( realloc( preval, prealloc + BUFINC + 1 ) ); prealloc++; } preval[ preleng ] = c; preval[ ++preleng ] = '\0'; } }
void sappend( const char *s ) { /* *+ * Name: * sappend * * Purpose: * Append a string to the preval string. * * Description: * This routine appends a string to the preval string. If the preval * string is not long enough to hold the new one, then more space * is allocated for it. * * Authors: * MBT: Mark Taylor (STARLINK) * * History: * 10-DEC-1999 (MBT): * Initial revision. *- */ /* Local variables. */ int leng; /* See how many extra characters are to be added to preval. */ leng = strlen( s ); /* Extend the allocated space if necessary. */ while ( preleng + leng > prealloc ) { if ( prealloc == 0 ) { preval = (char *) memok( malloc( BUFINC + 1 ) ); *preval = '\0'; } else { preval = (char *) memok( realloc( preval, prealloc + BUFINC + 1 ) ); } prealloc += BUFINC; } /* Append the string to preval. */ strcat( preval, s ); preleng += leng; }
char *ucontent() { /* *+ * Name: * ucontent * * Purpose: * Get unoutput string. * * Description: * This routine returns a string which consists of all the strings * which have had uadd() called on them since the last call to * unew(). * * Authors: * MBT: Mark Taylor (STARLINK) * * History: * 10-DEC-1999 (MBT): * Initial revision. *- */ ELEMENT *i; char *text; int j, leng; /* Work out how long the whole string is. */ leng = 0; for ( i = ufirst; i != NULL; i = i->next ) { leng += strlen( i->text ); } /* Allocate space for the string. */ text = (char *) memok( malloc( leng + 1 ) ); j = 0; /* Copy the lexically encountered items into the string sequentially. */ for ( i = ufirst; i != NULL; i = i->next ) { strcpy( text + j, i->text ); j += strlen( i->text ); } text[ j ] = '\0'; /* Return the concatenated string. */ return( text ); }
void preserve_data(void) { int i; int ok=true; stamp_structure *s; switch (dimension) { case LOGISTIC: ok = memok((sizeof(fflockstruct1))); break; case HENON: ok = memok((sizeof(fflockstruct2))); break; case YORKE: #ifdef OLDWAY ok = memok((sizeof(fflockstruct2))); break; #endif case LORENZ: ok = memok((sizeof(fflockstruct3))); break; } if (!ok) { stamp_err = 1; ErrorBox("Not enough memory to preserve stamp."); return; } /* get rid of the highest */ if (stamp_data[3]) { s = stamp_data[3]; switch (s->dimension) { case LOGISTIC: free(s->s.lo.fflock1ptr); s->s.lo.fflock1ptr = NULL; break; case HENON: free(s->s.h.fflock2ptr); s->s.h.fflock2ptr = NULL; break; #ifdef OLDWAY case YORKE: free(s->s.y.fflock2ptr); s->s.y.fflock2ptr = NULL; break; #endif case LORENZ: free(s->s.lz.fflock3ptr); s->s.lz.fflock3ptr = NULL; break; } free(s); s = NULL; } for (i = 3; i > 0; i--) stamp_data[i] = stamp_data[i - 1]; s = malloc(sizeof(stamp_structure)); s->dimension = dimension; (*stampers[dimension - 1]) (s); stamp_data[0] = s; }
void restore_data(int n) { stamp_structure s; fflockstruct1 *fs1; fflockstruct2 *fs2; fflockstruct3 *fs3; if (!stamp_data[n]) return; s = *stamp_data[n]; /* copy it, we might loose it */ switch (s.dimension) { case LOGISTIC: if (!memok(sizeof(fflockstruct1)) || (fs1 = malloc(sizeof(fflockstruct1))) == NULL) { ErrorBox("Not enough memory to restore stamp."); return; } memcpy(fs1, s.s.lo.fflock1ptr, sizeof(fflockstruct1)); s.s.lo.fflock1ptr = fs1; break; case HENON: if (!memok(sizeof(fflockstruct2)) || (fs2 = malloc(sizeof(fflockstruct2))) == NULL) { ErrorBox("Not enough memory to restore stamp."); return; } memcpy(fs2, s.s.h.fflock2ptr, sizeof(fflockstruct2)); s.s.h.fflock2ptr = fs2; break; case YORKE: #ifdef OLDWAY if (!memok(sizeof(fflockstruct2)) || (fs2 = malloc(sizeof(fflockstruct2))) == NULL) { ErrorBox("Not enough memory to restore stamp."); return; } memcpy(fs2, s.s.y.fflock2ptr, sizeof(fflockstruct2)); s.s.y.fflock2ptr = fs2; #endif break; case LORENZ: if (!memok(sizeof(fflockstruct3)) || (fs3 = malloc(sizeof(fflockstruct3))) == NULL) { ErrorBox("Not enough memory to restore stamp."); return; } memcpy(fs3, s.s.lz.fflock3ptr, sizeof(fflockstruct3)); s.s.lz.fflock3ptr = fs3; break; } erasecursor(); if (Stamping) { preserve_data(); slide_stamps(); } /* the restorer will free the proper flockptr */ (*restorers[s.dimension - 1]) (&s); }
int GifDisplay(char *filename) { int i, j; int c; int colors; union REGS regs; struct SREGS sregs; unsigned char ctab[17]; int ret = GIF_COOLMAN; if (!memok(16133L)) { /* Added mallocs in decoder.c */ ErrorBox("Not enough memory to view a Gif file."); return OUT_OF_MEMORY; } giffd = fopen(filename, "rb"); if (!giffd) { FileError(filename,NULL); return -1; } gifrow = 0; gifcol = 0; safe_alloc = 1; if ((rowbuf = malloc(512)) == NULL) { ret = OUT_OF_MEMORY; goto TRUNCOUT; } rowbufptr = 512; /* to get it started */ fread(&gif, 1, sizeof(gif), giffd); colors = (1 << ((gif.colpix & PIXMASK) + 1)); if (colors > 16 && hasVGA) { regs.h.ah = 0; regs.h.al = OurMode = 0x13; int86(0x10,®s,®s); } else { if (gif.h > 350 && hasVGA) OurMode = 0x12; else OurMode = 0x10; regs.h.ah = 0; regs.h.al = OurMode; int86(0x10,®s,®s); } if (gif.colpix & COLTAB) { fread(cmap, 1, colors * 3, giffd); /* Now set up the colormap if appropriate */ for (i = 0; i < colors * 3; i++) cmap[i] >>= 2; if (hasVGA) { /* * This is a VGA. Just write the map, shifted * appropriately. */ regs.h.ah = 0x10; regs.h.al = 0x12; regs.x.bx = 0; regs.x.cx = colors; regs.x.dx = FP_OFF(cmap); sregs.es = FP_SEG(cmap); int86x(0x10, ®s, ®s, &sregs); /* We'll also want to set the 0-16 map here? */ for (i = 0; i < 16; i++) ctab[i] = i; ctab[16] = 0; regs.h.ah = 0x10; regs.h.al = 0x2; regs.x.dx = FP_OFF(ctab); sregs.es = FP_SEG(ctab); int86x(0x10, ®s, ®s, &sregs); } else { for (i = 0; i < 16; i++) { for (j = 0; j < 64; j++) { unsigned char *p = defaultpalette[j]; if (p[0] == cmap[i * 3] && p[1] == cmap[i * 3 + 1] && p[2] == cmap[i * 3 + 2]) break; } ctab[i] = j; } ctab[i] = 0; regs.h.ah = 0x10; regs.h.al = 0x2; regs.x.dx = FP_OFF(ctab); sregs.es = FP_SEG(ctab); int86x(0x10, ®s, ®s, &sregs); } }
char *scat( int n, ... ) { /*+ * Name: * scat * * Invocation: * string = scat( n, ... ) * * Purpose: * Concatenate a list of strings. * * Arguments: * n = int * The number of strings to be concatenated. n may be zero, in * which case a newly allocated string of length zero is returned. * sp1, sp2, ... = char * * The other arguments are all strings, and there are n of them. * free() is called on each of them, so they must have been malloc'd * at some time in the past, and must not be used subsequent to * passing to this function. * * Return value: * string = char * * The return value is a string containing the concatenation of * all the strings supplied. It is obtained using malloc, so * should be free'd at some time in the future. * * Description: * This routine returns a newly malloc'd string which is the concatenation * of all the strings supplied to it as arguments. Each of those arguments * gets free'd by this routine, so they must have been malloc'd (probably * by this routine) in the past, and must not be referred to again after * calling this routine. * * Authors: * MBT: Mark Taylor (STARLINK) * * History: * 10-DEC-1999 (MBT): * Initial revision. *- */ /* Local variables. */ va_list ap; int len, i; char *string, *sp; /* Work out the length of the final string. */ len = 0; va_start( ap, n ); for ( i = 0; i < n; i++ ) { sp = va_arg( ap, char * ); len += strlen( sp ); } va_end( ap ); /* Allocate the memory we will need, and initialise it. */ string = (char *) memok( malloc( len + 1 ) ); *string = '\0'; /* Copy the arguments into the allocated space. We free the storage used by each argument as we go along. */ va_start( ap, n ); for ( i = 0; i < n; i++ ) { sp = va_arg( ap, char * ); strcat( string, sp ); free( sp ); } va_end( ap ); /* Return. */ return( string ); }
void PushRect(rect * R, int *err) { /* We don't really use ERR, but its for compatibility */ savelist *lastsave = savetail; long savesize = ImageSize(R); /* And we don't give a shit about size. */ savetail = farcalloc(sizeof(savelist), 1L); savetail->prev = lastsave; savetail->im = NULL; if (memok(savesize)) { safe_alloc = 1; savetail->im = (image far *) farcalloc(savesize, 1L); } savetail->R = farmalloc(sizeof(rect)); *(savetail->R) = *R; if (!savetail->im) { char tbuf[128]; char tbuf2[128]; FILE *fd; image *tmp; char *pathname = getenv("TMP"); unsigned char drive; struct dfree dtable; long dfree; #ifdef AXTDEBUG printf("\nLook out, I'm pushing to disk."); #endif /* Find out if there is enough space left on the thing */ if (pathname && access(pathname,0)) pathname = NULL; if (pathname && pathname[1] == ':') drive = toupper(pathname[0]) - 'A' + 1; else drive = 0; getdfree(drive, &dtable); if (dtable.df_sclus == 0xffff && drive) { drive = 0; getdfree(drive, &dtable); pathname = NULL; /* and so this doesn't happen again */ putenv("TMP="); } dfree = (long) dtable.df_avail * (long) dtable.df_bsec * (long) dtable.df_sclus; if (dfree > savesize) { rect R1; sprintf(tbuf, "%lx.dat", savetail); TempFileName(tbuf2, tbuf); fd = fopen(tbuf2, "wb"); if (fd) { int i; unsigned long cl = farcoreleft(); int lines; long n; if (cl > 2048L) cl -= 2048L; /* Safety margin */ R1 = *R; R1.Ymax = R1.Ymin; i = 1; while (i < R->Ymax) { R1.Ymax = R1.Ymin + i; n = ImageSize(&R1); if (n < cl) { savesize = n; lines = i; } i *= 2; } /* * Let's see how many rows at a time we can * save */ savetail->lines = lines; savetail->blocks = (R->Ymax - R->Ymin + 1) / lines; tmp = farmalloc(savesize); //setvbuf(fd, (char *) tmp, _IOFBF, (size_t) savesize); R1 = *R; R1.Ymax = R1.Ymin + lines - 1; for (i = R->Ymin; i <= R->Ymax; i += lines) { R1.Ymax = min(R1.Ymax, R->Ymax); ReadImage(&R1, tmp); fwrite(tmp, 1, (int) ImageSize(&R1), fd); OffsetRect(&R1, 0, lines); } fclose(fd); farfree(tmp); tmp = NULL; savetail->filename = strdup(tbuf2); *err = 0; return; } } sprintf(tbuf, "Can't allocate %ld bytes", savesize); ErrorBox(tbuf); *err = 1; farfree(savetail->R); savetail->R = NULL; farfree(savetail); savetail = NULL; savetail = lastsave; } else { ReadImage(savetail->R, savetail->im); *err = 0; } }
int main(int argc, char **argv) { unsigned short grafboard; extern int disk_error_handler(int errval, int ax, int bp, int si); char *p; allocatebuffers(); if (init_mem_err) { cprintf("\r\n\r\nSorry, not enough memory to run Toy Universes.\r\n"); return -1; } harderr(disk_error_handler); /* Find out if we have a VGA or EGA at all. */ grafboard = QueryGrafix(); if ((grafboard & 0x200) != 0x200) { printf("This programs requires EGA capability.\n"); exit(-1); } if (grafboard == 0xffff || InitGrafix(-EGA640x350) < 0) { printf("Metagraphics not installed. Execute the command:\n"); printf("metashel /i\n"); printf("and then try again.\n"); exit(-1); } vgaflag = -1; while (argc > 1) { if (argv[1][0] == '-') { switch (argv[1][1]) { case 'e': vgaflag = 0; break; case 'v': vgaflag = 1; break; } } argc--; argv++; } if (vgaflag == -1) { if ((grafboard & 0x300) == 0x300) vgaflag = 1; else vgaflag = 0; } Comm = QueryComm(); if (Comm == MsDriver) InitMouse(MsDriver); else if (Comm == MsCOM1) InitMouse(MsCOM1); else if (Comm == MsCOM2) InitMouse(MsCOM2); else if (Comm & 3) InitMouse(COM1); /* * Probably wrong. Need to check for MS mouse address in some special * way. */ randomize(); p = searchpath("system16.fnt"); if (p) LoadFont(p); installmode(); load_preset_palettes(); usepalette(); current_main_item = 0; usepalette(); TWICE(initialize_buttons()); ShowCursor(); if (allocatefailflag) ErrorBox("Not enough memory for hi-res."); /* Lets see if there is enough for a later gif */ if (!memok(20712L)) /* Added up mallocs in comprs.c */ ErrorBox("There may not be enough memory to save or view a Gif."); prog_init = 1; if (!setjmp(beatit)) { while (!exitflag) { rebuildflag = 0; if (newcaflag && !donescreenflag) { loadlookuptable(increment, maxstate); newcaflag = 0; } if (newcaoflag) { unsigned char *p1,*p2; static int firsttime = true; switch(caotype) { case CA_HODGE: p1 = (char *)HODGE_colortable; p2 = HODGE_ct; break; case CA_EAT: p1 = (char *)EAT_colortable; p2 = EAT_ct; break; case CA_TUBE: p1 = (char *)TUBE_colortable; p2 = TUBE_ct; break; case CA_NLUKY: p1 = (char *)NLUKY_colortable; p2 = NLUKY_ct; break; } memcpy(vgacolortable,p1,16*3); if (!hasVGA) memcpy(egacolortable,p2,16); if (!firsttime) { TWICE(initialize_numbers()); } else firsttime = false; usepalette(); newcaoflag = 0; current_main_item = -1; } if (blankflag) { blankbuffers(); blankflag = 0; } if (randomizeflag) { carandomize(); randomizeflag = 0; } while (!exitflag && !rebuildflag) { if (onestep || !stopped) { if (display_mode == HI) hiresupdate(); else loresupdate(); if (onestep) onestep--; } if (spinflag && (!stopped || (iteration++ > spinspeed))) { if (spinflag == 1) spinpalette(); else revspinpalette(); iteration = 0; } checkkeyboard(); if (newcaflag) rebuildflag = 1; } } } StopMouse(); StopEvent(); grayflag = 0; grayscale(); SetDisplay(TextPg0); return exitflag; }