// tsesc.sr

// Incoming procedures

external
	[
	puts;
	stget;
	tsmakegood;
	tsflush
	resetts
	gets
	errhlta
	stput
	] 

// Incoming statics

external
	[
	tscorrect;
	tsread;
	fnts;
	] 


// Outgoing procedures

external
	[
	processtsesc;
	]

// Local structures

structure STRING:
		[ length		 byte
		 body ↑ 1,255	 byte
		]

// P R O C E S S T S E S C 
//
let processtsesc(charesc,param,fnoFlush;numargs n) be
	[ if n ls 3 then fnoFlush = false
	let tsb = param;
	let cchMac = tsb >> STRING.length;
	test tsread eq false ifso
		[ puts(fnts,$@);
		puts(fnts,charesc);
		puts(fnts,$[);
		for ich = 0 to cchMac-1 do
			puts(fnts,stget(tsb,ich));
		puts(fnts,$]);
		tsmakegood();
		unless fnoFlush then
			tsflush();
		]
	ifnot	[ let tchar = nil;
		let tblchar = table  [ $@;0;$[ ];
		tblchar ! 1 = charesc;
		for i = 0 to 2 do
			[ tchar = gets(fnts);
			unless tchar eq tblchar ! i do
				[ if (i eq 1) & (tchar eq $E) then
					[ resetts();
					tsread = false;
					return;
					] 
				errhlta(99);
				] 
			]
		let tich = 0;
			[ tchar = gets(fnts);
			if tchar eq $] then break;
			if (charesc ne $V) % (tich ls cchMac) then
				[ stput(tsb,tich,tchar);
				tich = tich+1;
				]
			] repeat
		tsb >> STRING.length = tich;
		]
	];