/***********************************************\
 *  Echo   -   just a small echo-replacement   *
 *             with some enhancements I've     *
 *             wrote 2 years ago.              *
\***********************************************/

/* This program is *NOT* public domain, but may be copied and distributed on
   a not-for-profit manner. It may be included in Fred's library. Any other
   'Library-Creators' must ask me before including this program on their
   disk (I hate to do that, but there are so many people over here in germany
   that make people pay 10 Marks for some disks, half-full, and with programs
   they take from other disks...)
   Send any bug-reports, letters of appreciation, Porsche 911 or other small
   gifts to the following address: (and I'd really like to know what WBSize
   does on a NTSC Amy...)
   
   Garry Glendown
   Güldene Kammer 35
   6430 Bad Hersfeld
   West Germany
   
   Or for the people lucky enough to get to some Bitnet or Usenet node:
   
   Garry@DGIHRZ01.BITNET
                                                                    */


#include <exec/types.h>

int t,g;
BYTE flag;

UBYTE code[][9]={
	{12,0},				/*Clear*/
	{0x9b,0x4a,0},			/*ClrEOD*/
	{0x9b,1,0x53,0},
	{0x9b,1,0x54,0},		/*Scroll up/down*/
	{0x9b,1,0x41,0},
	{0x9b,1,0x42,0},		/*Cursor up/down*/
	{0x9b,1,';',1,0x48,0},		/*Cursor x/y*/
	{0x9b,1,';',1,';',1,0x6d}	/*Style*/
	};

char st[]={'p','b',0xfe,'i','u'};

char **arv;
int arc;

/* Attention! If the C-Compiler you use splits the CLI-Argument "The Test"
   into two arguments, you'll have to leave this routine in the program.
   If it returns the string `The Text' as one argument, take it out!
   (=Aztec 3.4, probably 3.6, too)     */
String()
{
char y[256];
int g,j;
	y[0]='\0';
	if (arv[t][strlen(arv[t])-1]!='\"') {
		strcat(y,&arv[t][1]);
		do {
			strcat(y," ");
			strcat(y,arv[++t]);
		}
		while (arv[t][strlen(arv[t])-1]!='\"'&&t<arc);
	}
	else strcat (y,&arv[t][1]);
	y[strlen(y)-1]='\0';
	printf("%s\n",y);
	flag=1;
}


main(argc,argv)
int argc;
char *argv[];
{
arv=argv;
arc=argc;
if (argc==1) help();
	for (t=1;t<argc;t++) {
		flag=0;
		if (argv[t][0]=='-') Decode();
		if (argv[t][0]=='"') String();	/* cut this out when using Aztec 3.4 */
		                                /* (see above...) */
		if (!flag) printf("%s\n",argv[t]);
	}
}

Decode()
{
int i,j[3];
char c;
	c=arv[t][1];
	for (i=0;i<3;j[i++]=-1);
	switch (c) {
		case 'c':
			putcode(0,&j);
			break;
		case 'e':
			putcode(1,&j);
			break;
		case 'u':
		case 'd':
		case 'h':
		case 'r':
			j[0]=getvalue();
			if (j[0]==-1) j[0]=1;
			switch (c) {
				case 'u':
					putcode(2,&j);
					break;
				case 'd':
					putcode(3,&j);
					break;
				case 'h':
					putcode(4,&j);
					break;
				case 'r':
					putcode(5,&j);
					break;
			}
			break;
		case 'p':
			j[1]=getvalue();
			if (j[1]==-1) j[1]=1;
			j[0]=getvalue();
			putcode(6,&j);
			break;
		case 's':
			j[2]=0;
			for (i=0;i<=4;i++) if (arv[t][2]==st[i]) j[2]=i;
			code[7][1]=j[2]+0x30;
			j[0]=getvalue();
			if (j[0]==-1) j[0]=1;
			j[0]+=30;
			j[1]=getvalue();
			if (j[1]==-1) j[1]=0;
			j[1]+=40;
			putcode(7,&j);
			break;
	}
	flag=1;
}

putcode(n,a)
int n,*a;
{
int g,z=0;
	for (g=0;g<7;g++) {
		if (code[n][g]==0) g=10;
		else {
			if (code[n][g]==1) {senddigit(a[z]);z++;}
			else putchar(code[n][g]);
		}
	}
}

senddigit(val)
int val;
{
	if (val!=-1) printf("%d",val);
}

int getvalue()
{
int g,h;
	g=sscanf(arv[t+1],"%d",&h);
	if (g==-1) {h=-1;t--;}
	t++;
	return(h);
}


help()
{
printf("*** Echo ***\n");
printf("© 1987-9 by G.Glendown\n");
printf("Options:\n");
printf("-c        Clear the screen\n");
printf("-e        delete from cursor to bottom of screen\n");
printf("-u/d [n]  scroll up/down [n] Lines\n");
printf("-h/r [n]  move cursor up(h) or down(r) [n] lines\n");
printf("-p [x][y] set cursor to postion [x][y]\n");
printf("-s[ubip] [a][b]  Style u,b,i,p, colors [a][b]\n\n");
exit();
}



