/* gmcr.c - test gmcr */
/* needs special $$init() which DOES NOT gmcr$ itself */

#include <algol68.h>

#define BUFLEN (82)

char 	buf[BUFLEN];/* gmcr buffer */
int	dsw;	/* saved dsw from gmcr() */
char *	n;	/* end: just past end of gmcr buffer */
char *	q;	/* scans buffer */
int	r;	/* controls which tit we print in octal */
int	$$narg=1;/* DONT do a gmcr$ before main() */
char	c;	/* */
BOOL	more;	/* TRUE while we expect more command line */
char command[1000]; /* build command image here */
char *	p;	/* scans command[] */

main()
BEGIN
	n = buf + BUFLEN;
	p = command;
	/*
	 * This loops over all the continued buffers if user has
	 * learned about '-' on end of line.
	 */
	FOR	(more=TRUE;  more;  more = ((dsw>0) && (buf[1+dsw]=='-')) )
	DO	try();
		FOR	(q=buf+2; q<buf+1+dsw; q++)
		DO	*p++ = *q;
		OD
		IF	(*q!='-')
		THEN	*p++ = *q;
		FI
	OD
	*p = 0;
	printf("command without terminator:\n\"%s\"\n",command);
END

try()
BEGIN
	dsw = gmcr(buf);
	printf("dsw=%oo, terminator=%oo   buf follows\n",dsw,buf[2+dsw]);
	FOR	(r=6; r>=0; r-=3)
	DO	FOR	(q=buf; q<n; q++)
		DO	putchar( '0'+((((*q)&0xFF)>>r)&0x7) );
		OD
		putchar('\n');
	OD
	FOR	(q=buf; q<n; q++)
	DO	c = (*q) & 0x7F;
		IF	(c==127 || c<' ')
		THEN	c = ' ';
		FI
		putchar(c);
	OD
	putchar('\n');
END

/* end gmcr.c */
