
#include	"sysdef.h"

/*	ASCII and TYPE commands					*/
/*								*/
/*	04-Oct-84  15:17:57  Brian Nelson			*/
/*								*/
/*	Copyright Notice					*/
/*								*/
/*								*/
/*	C version Copyright (C) 1984 Change Software, Inc.	*/
/*	Copyright (C) 1978 1979 1980 1981 1982 1983 1984 by	*/
/*		      Change Software, Inc.			*/
/*								*/
/*								*/
/*	This software is furnished under a license and may	*/
/*	be  used  and  copied  only in accordance with the	*/
/*	terms of such license and with  the  inclusion  of	*/
/*	the  above copyright notice.  This software or any	*/
/*	other copies thereof may not be provided or other-	*/
/*	wise made available to any other person.  No title	*/
/*	to and ownership of the software is hereby  trans-	*/
/*	ferred.							*/
/*								*/

#include	"scerrd.h"
#include	"tempio.h"

#define	then
#define	CR	015
#define	LF	012
#define	TAB	011


cascii()
{
	extern struct textline *textpointer() ;
	struct textline *textp ;
	extern int lowlim,highlim ;

	int done,i,lpt,lineno,nlines,size ;
	register char *cp,*tp ;
	register int ch ;
	char txtbuf[20], fmtbuffer[80] ;

	lpt = 0 ;

	for (lineno=lowlim; lineno <= highlim && testcc() == 0; lineno++ ) {
		nlines = 0 ;
		textp = textpointer(lineno) ;
		done = ( (ch = nextch(textp,&lpt)) == -1 ) ;

		while ( done == 0 ) {
			size = 12 ;
			cp = fmtbuffer ;
			for (i=50; i>0; i-- ) *cp++ = ' ' ;
			*cp = 0 ;
			tp = txtbuf ;
			for (i=12; i>0; i-- ) *tp++ = ' ' ;
			cp = fmtbuffer ;
			tp = txtbuf ;

			while ( size-- > 0 && done == 0 ) {
				if ( ch < 040 )
				  then *tp++ = 040 ;
				  else *tp++ = ch ;
				cvtdec(cp,ch) ;
				cp++ ; cp++ ;cp++ ;cp++ ;
				done = ( (ch = nextch(textp,&lpt)) == -1 ) ;
				}
			*tp   = 0 ;
			if ( nlines++ == 0 )
			  then
			    xprintf("%6d  ) %s %s\n",lineno,fmtbuffer,txtbuf) ;
			  else
			    xprintf("          %s %s\n",fmtbuffer,txtbuf) ;
		}
	}
}



static nextch(tp,lpt)
register struct textline *tp ;
int *lpt ;
{

	if ( *lpt < tp->len )
	  then	return( tp->c[(*lpt)++] ) ;
	  else	{
		*lpt = 0 ;
		return(-1) ;
		}
}


static cvtdec(s,ch)
char *s ;
register int ch ;
{
	register char *cp ;

	ch &= 0377 ;
	cp = s ;
	*cp++ = ( ch/100 ) + '0' ;
	*cp++ = ( (ch % 100)/10 ) + '0' ;
	*cp++ = ( ch % 10 ) + '0' ;
	*cp++ = ' ' ;
}




ctype(lun)
int lun ;
{
	extern int cmdlun ;
	extern struct textline inline ;

	if ( lun == cmdlun ) then return(0) ;
	sclear() ;
	while ( getline(lun,inline.c,&inline.len) == sys_normal && testcc()==0)
		putbin(inline.c,inline.len) ;
	return(1) ;
}



static char *terms[] = { "<NUL>","<SOH>","<STX>","<ETX>",
			 "<EOT>","<ENQ>","<ACK>","<BEL>",
			 "<BS >","<TAB>","<LF >","<VT >",
			 "<FF >","<CR >","<SO >","<SI >",
			 "<DLE>","<XON>","<DC2>","<XOF>",
			 "<DC4>","<NAK>","<SYN>","<ETB>",
			 "<CAN>","<EM >","<SUB>","<ESC>" } ;

static char unknown[] = "<???>" ;
		
clength()
{
	extern char *getmap(),*strcpy() ;
	extern struct textline *textpointer() ;
	register struct textline *tp ;
	extern int lowlim,highlim ;
	register int length,lineno ;
	char s[64],c[5] ;

	for (lineno=lowlim; lineno<=highlim && testcc() == 0; lineno++ ) {
		tp = textpointer(lineno) ;
		length = tp->len ;
		strcpy(s,"Length ");
		cvtdec(c,length) ;
		c[4] = 0 ;
		strcpy(&s[strlen(s)],c) ;
		strcpy(&s[strlen(s)],"Eoln  ") ;
		if ( length > 1 )
		  then	strcpy(&s[strlen(s)],getmap(tp->c[length-2]));
		strcpy(&s[strlen(s)],"   ");
		strcpy(&s[strlen(s)],getmap(tp->c[length-1])) ;
		xprintf("%6d ) %s\n",lineno,s) ;
		}
}
		
			

static char *getmap(ch)
register int ch ;
{
	if ( ch <= 033 )
	  then	return(terms[ch]) ;
	  else	return(unknown) ;
}




static char setup[]   = "\033<\033[2J\033PpP[1,400]S(N0,I0,E)" ;
static char regmode[] = "\033Pp" ;
static char exitreg[] = "\033\\" ;

cregis()
{
	extern struct textline *textpointer();
	struct textline *tp ;
	extern int *maxline ;
	register int i ;

	putbin(setup,0) ;
	for (i=1; i <*maxline && testcc() == 0 ; i++) {
		tp = textpointer(i) ;
		putbin(regmode,0) ;
		putbin(tp->c,tp->len) ;
		}
	putbin(exitreg,0) ;
	return(1) ;
}


