;/* Execute me in order to compile me.
sc resize nostackcheck data=far stringmerge optimize
slink resize.o to resize lib lib:amiga.lib  sc sd nd
quit
*/

#include <string.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <clib/alib_stdio_protos.h>

/* This program asks the console device for a string which describes the number
of lines and columns available in the current CLI. Then, it sets the local variables
LINES and COLUMNS to those values. */

int _main(void) {

	int i;
	unsigned char buf[64], *p, *q;
	struct Library *DOSBase;

	if (!(DOSBase = OpenLibrary("dos.library\0$VER: resize 37.1 (24.10.93)", 37))) return(30);

	SetMode(Input(), 1);

	Write(Output(), "\2330 q", 4);

	i = Read(Input(), buf, sizeof(buf)-1);
	if (i>=0) buf[i] = 0;

	SetMode(Input(), 0);

	p = buf;

	while(*p && *p != '\233') p++;

	if (strlen(p)<9 || p[1] != '1' || p[2] != ';' || p[3] != '1' || p[4] != ';' ) {
		PutStr("Unknown character, exiting\n");
		goto GameOver;
	}

	q = (p += 5);

	while(*p>='0' && *p<='9' || *p == ' ') p++;

	if (*p != ';') {
		PutStr("Unknown character, exiting\n");
		goto GameOver;
	}

	SetVar("LINES", q, p-q, 0);

	q = ++p;

	while(*p>='0' && *p<='9' || *p == ' ') p++;

	if (*p != 'r') {
		PutStr("Unknown character, exiting\n");
		goto GameOver;
	}

	SetVar("COLUMNS", q, p-q, 0);

GameOver:
	CloseLibrary(DOSBase);
	return(0);
}
