
#include	"sysdef.h"

/*	EDEND.C							*/
/*								*/
/*	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.							*/
/*								*/
/*								*/
/*	The QUIT and END commands for TED			*/
/*	Also, startup code					*/
/*								*/
/*	10-Oct-84  10:12:51  Brian Nelson			*/

#include	"scerrd.h"
#include	"edswdf.h"
#define	then
#define	SPACE	040
#define	CR	015
#define	LF	012


#define	NEXT_FAILURE	0
#define	NEXT_SUCCESS	1
#define	NEXT_NMF	2
#define	NEXT_CONTROLC	3








static
char yesno1[] = "Please type a filename to override the default, otherwise";
static
char yesno2[] = "type a carriage  return to accept the displayed filename.";

cend()
{
	extern char *infile[] ;
	extern int lowlim,highlim,*maxline,outlun,sts_one ;
	register int status,size,i ;
	char filename[_FILESIZE] ,t_infile[_FILESIZE] ;
	char prompt[_FILESIZE+20] ;

	
	fixfilename(t_infile,infile[0]) ;
	strcpy(prompt,"Output file <") ;
	strcpy(&prompt[strlen(prompt)],t_infile) ;
	strcpy(&prompt[strlen(prompt)],"> ? ") ;
	backup() ;
	if ( (size=getprm(filename,prompt,_FILESIZE)) == 0 )
	  then	status = 0 ;
	  else	{
		if ( (size=cvtstring(filename,size,0204)) == 0 )
		  then strcpy(filename,t_infile) ;
		  else	{
			filename[size] = 0 ;
			if ( checkyesno(filename) )
			  then	{
				tederror(yesno1); tederror(yesno2);
				return(0) ;
				}
			}
		setprotection() ;
		setmbc() ;
		if ( (status=openfi(filename,outlun)) == sys_normal )
		  then	{
			sts_one |= ed_end ;
			savefilename(filename) ;
			status = listlines(1,*maxline-1,outlun) ;
			decout(*maxline-1); putbin(" lines written\015\012",0);
			closef( outlun ) ;
			}
		  else	{
			tederror("Can't create output file") ;
			prsys_error() ;
			}
		}
	clrmbc() ;
	if ( status ) then sts_one |= ed_quit ;
	return( status ) ;
}	




static checkyesno(s)
register char *s ;
{
	char junk[12] ;
	register char *cp,*cpsave ;
	register int i,status ;

	cp = junk ;
	for (i=0; i < strlen(s) && i < 10; i++) *cp++ = toupper(*s++) ;
	*cp = 0 ;
	status = ( strcmp(junk,"Y")   == 0 || strcmp(junk,"N")  == 0 ||
	           strcmp(junk,"YES") == 0 || strcmp(junk,"NO") == 0 ) ;
	return(status) ;
}

	
cquit()
{
	extern int dirindex,lowlim,highlim,*maxline,sts_one ;

	if ( highlim-lowlim < 15 )
	  then	{
		sts_one |= ed_quit ;
		dirindex++ ;
		}
	  else
		if ( getyesno("Are you sure <No> ? ","N") == 1)
		  then	{
			sts_one |= ed_quit ;
			dirindex++ ;
			}
}		

csave()
{
		extern char *infile[] ;
		extern int lowlim, highlim, outlun ;
		register int status ;

		setprotection() ;
		if ( (status=openfi(infile[0],outlun)) == sys_normal )
		  then	{
			xprintf("Saving to %s\n",infile[0]) ;
			status = listlines(lowlim,highlim,outlun) ;
			closef( outlun ) ;
			}
		  else 	prsys_error() ;
}
