#include #include "mxalloc.h" #include "player.h" #include "header.h" #define AUTO_NAME "autoexec.sys" /* default free memory, below the sound sample */ static long keep_free = 100000l; /* flag that tells if a sound sample is loaded */ static int sound_loaded = 0; /* copy of bit 1 (DMA) in the sound cookie */ static int DMAsound = 0; /* empty command line, for use when starting programs */ const COMMAND empty_command = {0,""}; /* the most resent pressed key */ static char CurrentKey = 0; /* default key flag (don't wait on keypress) */ static int DefaultKey = 0; long atol(char *s) /* me own StringToLong routine, using TC's costed 1 Kbyte !!! */ {int n; long l; l = 0; while (*s) { n = *s++ - '0'; if (n>=0 && n<=9) l = 10*l + n; else break; } return l; } int execute_line(char *line) {int file, segm_repeat, no_exec; long length; char *tmp; no_exec = 0; if (*line == '?') /* is it a conditional instruction ? */ { no_exec = 1; /* if condition, then assume don't execute */ do { if (*++line == CurrentKey) /* condition true ? */ no_exec = 0; /* ok, then execute rest of line... */ ++line; } while (*line == '?'); /* is it still a conditional ? */ } if (no_exec) return 0; switch (*line++) { case 'X': /* break instruction execution */ return 1; case 'W': /* print a string and Wait for keyboard input */ Cconws(line); if (!DefaultKey || Cconis()) /* If there is a Key waiting in the keyboard buffer * OR there is no default key - then read key from buffer */ CurrentKey = (char)(Cconin() & 0xff); Cconws("\r\n"); DefaultKey = 0; break; case 'P': /* print a string */ Cconws(line); Cconws("\r\n"); break; case 'D': /* set default key for the next 'wait for key' */ DefaultKey = 1; CurrentKey = *line; break; case '>': /* start sound sample */ if (!DMAsound) break; if (sound_loaded) { STe_end(0); sound_loaded = 0; } if ( (file = Fopen(line, 0)) <= 0 ) goto sn_err; length = Fseek(0, file, 2); Fseek(0, file, 0); /* allocate memory for the 'free' section and the sound. * this way we don't put a sample where somone else expects * code or data... */ if ((tmp = Mxalloc(length+keep_free, MX_STON)) > 0l) { if (!LoadSegm(file, &segm_repeat, length, (long *)tmp + keep_free)) { SetPlayFreq(PlayFreq); STe_segment((long *)tmp + keep_free, segm_repeat); sound_loaded = 1; } /* now free the memory, hoping that no one else allocate * it!! */ Mfree(tmp); } Fclose(file); if (!sound_loaded) goto sn_err; break; case '#': /* set free memory, below samples */ keep_free = atol(line); break; case '!': /* execute program */ if (Pexec(0, line, &empty_command, 0l) < 0) goto ex_err; break; default:; } return 0; sn_err: Cconws("AUTO: can't play the file: "); goto w_name; ex_err: Cconws("AUTO: Can't execute the file: "); w_name: Cconws(line); Cconws("\r\n"); NoLoad: return 0; } main() {int file, store_line, skip_remaining; long length, lines, l, snd; char *p, *file_buffer; char **line_buffer; /* turn on the cursor */ Cursconf(1, 0); /* check if the computer have DMA sound */ if (get_cookie('_SND', &snd) && (snd & 2)) /* DMA sound? */ DMAsound = 1; /* read the AUTO file into memory */ if ((file = Fopen(AUTO_NAME,0)) < 0) goto op_err; length = Fseek(0l,file,2); Fseek(0l,file,0); file_buffer = Mxalloc(length,MX_TTPR); Fread(file,length,file_buffer); Fclose(file); /* calculate an estimate of the number of lines */ p = file_buffer; lines = 0; for(l=0; l ' ' && store_line && !skip_remaining) { line_buffer[lines++] = p; store_line = 0; } if (*p == '\n' || *p == '\r') /* end of line */ { *p = '\0'; store_line = 1; skip_remaining = 0; } ++p; } /* execute each line in the file. */ l = 0; while (lines--) if (execute_line(line_buffer[l++])) /* stop executing if execute_line() returns 1 */ break; if (sound_loaded) STe_end(1); /* clean up, and return */ Mfree(line_buffer); Mfree(file_buffer); return 0; op_err: Cconws( "AUTO: can't open \"" AUTO_NAME "\" for input!\r\n"); return 0; }