/* chat.c: general send/receive */
#include <stdio.h>
#include <cx.h>
#define EFN 12
main(argc,argv)
int		argc;
char		**argv;
{extern int	esc_msk;
 char		ascii[7];
 struct rad50	task;
 int		buf[15];
 int		dsw;
 char		*p;
 char		*q;
 int		printf();
 char		bigbuf[100];
 esc_msk = 0xFF;
 while	(1)
	{printf("%s: task to send to:",*argv);
	 gets(ascii);
	 ascii[6]=0;
	 if	(*ascii)
		{ascr50(6,ascii,&task);
		 printf("%s: message to send to task %s:",*argv,ascii);
		 zero(buf,30);
		 gets(bigbuf);
		 p = bigbuf;
		 q = buf;
		 while	(*q++ = esc(&p))	p++;
		 wblock(buf,26,printf);
		 dsw = sdat(&task,buf,EFN);
		 printf("%s: sdat(%s)=%oo\n",*argv,ascii,dsw);
		 dsw = ustp(&task);
		 printf("%s: ustp(%s)=%oo\n",*argv,ascii,dsw);
		}
	 ;
	 printf("%s: task to receive from:",*argv);
	 zero(ascii,6);
	 gets(ascii);
	 ascr50(6,ascii,&task);
	 ascii[6] = 0;
	 do
		{zero(buf,30);
		 dsw = rcst(&task,buf);
		 printf("%s: rcst(%s)=%oo\n",*argv,ascii,dsw);
		}
	 while	(dsw == IS_SET)
	 ;
	 /* here with something received */
	 r50toa(ascii,buf,2);
	 ascii[6]=0;
	 printf("%s:received \"%s\" from task:%s\n",*argv,buf+2,ascii);
	 wblock(buf+2,26,printf);
	}
 ;
}

wblock(buf,len,pf)
char	*buf;
int	len;
int	(*pf)();
{int	max;
 int	i;
 while	(len)
	{max = len>8 ? 8 : len;
	 for	(i = 0; i<max; i++)	(*pf)("%2x",buf[i]&0xFF);
	 for	(; i<8; i++)		(*pf)("  ");
	 				(*pf)("\t");
	 for	(i=0; i<max; i++)
		{(*pf)(" ");
		 wchar(buf[i],pf);
		}
	 ;
					(*pf)("\n");
	 len -= max;
	 buf += max;
	}
 ;
}

wchar(c,pf)
char	c;
int	(*pf)();
{(*pf)(c&0x80?"'":" ");
 c &= 0x7F;
 if	(c<040)
	{c+=0100;
	 (*pf)("^");
	}
 else
	{(*pf)(" ");
	}
 ;
 (*pf)("%c",c);
}
