
#include	"sysdef.h"




#define	largea		0101
#define	largez		0132
#define	smalla		0141
#define	smallz		0172
#define	tab		'\011'
#define	TAB		'\011'


trimjunk(s,len)
char *s;
int len;
{
	return(cvtstring(s,len,01207));
}


upcase(s,len)
register char *s;
int len;
{
	register int i ;
	for (i=len;i>0;i--) { *s = toupper(*s); s++ ; }
	return(len) ;
}


lowcase(s,len)
register char *s;
int len;
{
	register int i ;
	for (i=len;i>0;i--) { *s = tolower(*s); s++ ; }
	return(len) ;
}



/*	CVTSTRING	cvt$$ equivalent				*/
/*									*/
/*	newlength = cvtstring( string,length,patternmask )		*/
/*									*/
/*	   1	DISCARD ALL PARITY BITS					*/
/*	   2	DISCARD ALL SPACES & TABS				*/
/*	   4	DISCARD CR LF FF ESC RO					*/
/*	  10	DISCARD LEADING SPACES & TABS				*/
/*	  20	REDUCE SPACES & TABS TO A SINGLE SPACE			*/
/*	  40	CONVERT LC TO UC					*/
/*	 100	CONVERT [ TO ( AND ] TO )				*/
/*	 200	DISCARD TRAILING SPACES & TABS				*/
/*	 400	PRESERVE QUOTED SUBSTRINGS				*/
/*	1000	DISCARD ALL CHARACTERS < 40 OR =177			*/
/*	2000	UC to LC						*/

#define	c_par	01
#define	c_spac	02
#define	c_crlf	04
#define	c_lspa	010
#define	c_sspa	020
#define	c_lcuc	040
#define	c_brac	0100
#define	c_tspa	0200
#define	c_quot	0400
#define	c_prt	01000
#define	c_uclc	02000


#define	CR	015
#define	FF	014
#define	LF	012
#define	SPACE	040
#define	TAB	011
#define	SQ	047
#define	DQ	042
#define	then


cvtstring(s,inplen,bitmask)
char *s ;
int inplen,bitmask ;
{
	int	inaquote,retlength,spacetab,usech ;
	char	lastch,quoch ;
	register int i ;
	register char *op,ch ;
	char *cp ;


	cp = s ;
	op = s ;
	inaquote = 0 ;
	quoch = '\0' ;
	lastch = '\0' ;


	cp = s ;
	if ( bitmask & c_lspa )
	  then	{
		for (i=inplen; i>0 && (*cp == 040 || *cp == TAB); i--) cp++ ;
		if ( (inplen=i) == 0) then return(0) ;
		}
	retlength = 0 ;

	for (i=inplen; i>0 ;i--) {
		ch = *cp++ & 0377 ;

		usech = 1 ;
		if ( inaquote == 0 )
		  then	{
		    if ( c_par & bitmask ) then ch &= 0177 ;
		    spacetab = white( ch ) ;
		    if ((c_spac & bitmask)!=0 && spacetab )
		      then usech = 0 ;
		    if ((c_sspa & bitmask)!=0 && spacetab && white(lastch))
		      then usech = 0 ;
		    if ((c_crlf & bitmask)!=0 && (isdelimiter(ch)||ch==0) )
		      then usech = 0 ;
		    if ((c_lcuc & bitmask)!=0) ch=toupper(ch);
		    if ((c_uclc & bitmask)!=0) ch=tolower(ch);
			}
		if ( (c_quot & bitmask)!=0 && ( ch == SQ || ch == DQ ) )
		  then {
			if ( inaquote == 0 )
			  then	{
				inaquote = 1 ;
				quoch = ch ;
				}
			  else if ( ch == quoch ) then inaquote = 0 ;
			}
		if ( (c_prt & bitmask)!=0 && (ch < 040 || ch > 0176) )
		  then usech = 0 ;
		if (usech)
		  then	{
			*op++ = ch ;
			retlength++ ;
			}
		lastch = ch ;
	}
	if ( (bitmask & c_tspa) != 0 && retlength )
	  then	{
		cp = &s[retlength-1] ;
	        for (;retlength>0 && (*cp==040 || *cp==TAB);retlength--) cp-- ;
		}
	return( retlength ) ;
}


static white(ch)
register char ch ;
{
	return( ch == TAB || ch == SPACE ) ;
}




#define	SPACE	040
#define	TAB	011

detab(s2,s1,s1len)
register char *s1,*s2 ;
int s1len ;
{
	extern int maxlen ;
	int j,s2len ;
	register int i ;

	s2len = 0 ;
	for ( i=s1len; i > 0 && s2len < maxlen; i-- ) {
		if ( *s1 != TAB )
		  then	{
			s2len++ ;
			*s2++ = *s1++ ;
			}
		  else	{
			s1++ ;
			for ( j = 8-(s2len % 8); j>0 && s2len < maxlen; j--)
				{
				*s2++ = SPACE ;
				s2len++ ;
				}
			}
	}
	return(s2len) ;
}

			
