head	1.9;
access;
symbols
	perseant-exfatfs-base-20250801:1.9
	perseant-exfatfs-base-20240630:1.9
	perseant-exfatfs:1.9.0.36
	perseant-exfatfs-base:1.9
	cjep_sun2x:1.9.0.34
	cjep_sun2x-base:1.9
	cjep_staticlib_x-base1:1.9
	cjep_staticlib_x:1.9.0.32
	cjep_staticlib_x-base:1.9
	phil-wifi-20200421:1.9
	phil-wifi-20200411:1.9
	phil-wifi-20200406:1.9
	pgoyette-compat-merge-20190127:1.9
	pgoyette-compat-20190127:1.9
	pgoyette-compat-20190118:1.9
	pgoyette-compat-1226:1.9
	pgoyette-compat-1126:1.9
	pgoyette-compat-1020:1.9
	pgoyette-compat-0930:1.9
	pgoyette-compat-0906:1.9
	pgoyette-compat-0728:1.9
	pgoyette-compat-0625:1.9
	pgoyette-compat-0521:1.9
	pgoyette-compat-0502:1.9
	pgoyette-compat-0422:1.9
	pgoyette-compat-0415:1.9
	pgoyette-compat-0407:1.9
	pgoyette-compat-0330:1.9
	pgoyette-compat-0322:1.9
	pgoyette-compat-0315:1.9
	pgoyette-compat:1.9.0.30
	pgoyette-compat-base:1.9
	perseant-stdc-iso10646:1.9.0.28
	perseant-stdc-iso10646-base:1.9
	prg-localcount2-base3:1.9
	prg-localcount2-base2:1.9
	prg-localcount2-base1:1.9
	prg-localcount2:1.9.0.26
	prg-localcount2-base:1.9
	pgoyette-localcount-20170426:1.9
	bouyer-socketcan-base1:1.9
	pgoyette-localcount-20170320:1.9
	bouyer-socketcan:1.9.0.24
	bouyer-socketcan-base:1.9
	pgoyette-localcount-20170107:1.9
	pgoyette-localcount-20161104:1.9
	localcount-20160914:1.9
	pgoyette-localcount-20160806:1.9
	pgoyette-localcount-20160726:1.9
	pgoyette-localcount:1.9.0.22
	pgoyette-localcount-base:1.9
	netbsd-5-2-3-RELEASE:1.9
	netbsd-5-1-5-RELEASE:1.9
	yamt-pagecache-base9:1.9
	yamt-pagecache-tag8:1.9
	tls-earlyentropy:1.9.0.18
	tls-earlyentropy-base:1.9
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.9
	riastradh-drm2-base3:1.9
	netbsd-5-2-2-RELEASE:1.9
	netbsd-5-1-4-RELEASE:1.9
	netbsd-5-2-1-RELEASE:1.9
	netbsd-5-1-3-RELEASE:1.9
	agc-symver:1.9.0.20
	agc-symver-base:1.9
	tls-maxphys-base:1.9
	yamt-pagecache-base8:1.9
	netbsd-5-2:1.9.0.16
	yamt-pagecache-base7:1.9
	netbsd-5-2-RELEASE:1.9
	netbsd-5-2-RC1:1.9
	yamt-pagecache-base6:1.9
	yamt-pagecache-base5:1.9
	yamt-pagecache-base4:1.9
	netbsd-5-1-2-RELEASE:1.9
	netbsd-5-1-1-RELEASE:1.9
	yamt-pagecache-base3:1.9
	yamt-pagecache-base2:1.9
	yamt-pagecache:1.9.0.14
	yamt-pagecache-base:1.9
	bouyer-quota2-nbase:1.9
	bouyer-quota2:1.9.0.12
	bouyer-quota2-base:1.9
	matt-nb5-pq3:1.9.0.10
	matt-nb5-pq3-base:1.9
	netbsd-5-1:1.9.0.8
	netbsd-5-1-RELEASE:1.9
	netbsd-5-1-RC4:1.9
	netbsd-5-1-RC3:1.9
	netbsd-5-1-RC2:1.9
	netbsd-5-1-RC1:1.9
	netbsd-5-0-2-RELEASE:1.9
	netbsd-5-0-1-RELEASE:1.9
	jym-xensuspend-nbase:1.9
	netbsd-5-0:1.9.0.6
	netbsd-5-0-RELEASE:1.9
	netbsd-5-0-RC4:1.9
	netbsd-5-0-RC3:1.9
	netbsd-5-0-RC2:1.9
	jym-xensuspend:1.9.0.4
	jym-xensuspend-base:1.9
	netbsd-5-0-RC1:1.9
	netbsd-5:1.9.0.2
	netbsd-5-base:1.9
	matt-mips64-base2:1.8
	matt-mips64:1.8.0.32
	mjf-devfs2:1.8.0.30
	mjf-devfs2-base:1.8
	netbsd-4-0-1-RELEASE:1.8
	wrstuden-revivesa-base-3:1.8
	wrstuden-revivesa-base-2:1.8
	wrstuden-fixsa-newbase:1.8
	wrstuden-revivesa-base-1:1.8
	yamt-pf42-base4:1.8
	yamt-pf42-base3:1.8
	hpcarm-cleanup-nbase:1.8
	yamt-pf42-baseX:1.8
	yamt-pf42-base2:1.8
	wrstuden-revivesa:1.8.0.28
	wrstuden-revivesa-base:1.8
	yamt-pf42:1.8.0.26
	yamt-pf42-base:1.8
	keiichi-mipv6-nbase:1.8
	keiichi-mipv6:1.8.0.24
	keiichi-mipv6-base:1.8
	matt-armv6-nbase:1.8
	matt-armv6-prevmlocking:1.8
	wrstuden-fixsa-base-1:1.8
	netbsd-4-0:1.8.0.22
	netbsd-4-0-RELEASE:1.8
	cube-autoconf:1.8.0.20
	cube-autoconf-base:1.8
	netbsd-4-0-RC5:1.8
	netbsd-4-0-RC4:1.8
	netbsd-4-0-RC3:1.8
	netbsd-4-0-RC2:1.8
	netbsd-4-0-RC1:1.8
	matt-armv6:1.8.0.18
	matt-armv6-base:1.8
	matt-mips64-base:1.8
	hpcarm-cleanup:1.8.0.16
	hpcarm-cleanup-base:1.8
	netbsd-3-1-1-RELEASE:1.8
	netbsd-3-0-3-RELEASE:1.8
	wrstuden-fixsa:1.8.0.14
	wrstuden-fixsa-base:1.8
	abandoned-netbsd-4-base:1.8
	abandoned-netbsd-4:1.8.0.8
	netbsd-3-1:1.8.0.10
	netbsd-3-1-RELEASE:1.8
	netbsd-3-0-2-RELEASE:1.8
	netbsd-3-1-RC4:1.8
	netbsd-3-1-RC3:1.8
	netbsd-3-1-RC2:1.8
	netbsd-3-1-RC1:1.8
	netbsd-4:1.8.0.12
	netbsd-4-base:1.8
	chap-midi-nbase:1.8
	netbsd-3-0-1-RELEASE:1.8
	chap-midi:1.8.0.6
	chap-midi-base:1.8
	netbsd-3-0:1.8.0.4
	netbsd-3-0-RELEASE:1.8
	netbsd-3-0-RC6:1.8
	netbsd-3-0-RC5:1.8
	netbsd-3-0-RC4:1.8
	netbsd-3-0-RC3:1.8
	netbsd-3-0-RC2:1.8
	netbsd-3-0-RC1:1.8
	netbsd-2-0-3-RELEASE:1.7.4.2
	netbsd-2-1:1.7.6.1.0.2
	netbsd-2-1-RELEASE:1.7.6.1
	netbsd-2-1-RC6:1.7.6.1
	netbsd-2-1-RC5:1.7.6.1
	netbsd-2-1-RC4:1.7.6.1
	netbsd-2-1-RC3:1.7.6.1
	netbsd-2-1-RC2:1.7.6.1
	netbsd-2-1-RC1:1.7.6.1
	netbsd-2-0-2-RELEASE:1.7.4.2
	netbsd-3:1.8.0.2
	netbsd-3-base:1.8
	netbsd-2-0-1-RELEASE:1.7
	netbsd-2:1.7.0.6
	netbsd-2-base:1.7
	netbsd-2-0-RELEASE:1.7
	netbsd-2-0-RC5:1.7
	netbsd-2-0-RC4:1.7
	netbsd-2-0-RC3:1.7
	netbsd-2-0-RC2:1.7
	netbsd-2-0-RC1:1.7
	netbsd-2-0:1.7.0.4
	netbsd-2-0-base:1.7
	netbsd-1-6-PATCH002-RELEASE:1.7
	netbsd-1-6-PATCH002:1.7
	netbsd-1-6-PATCH002-RC4:1.7
	netbsd-1-6-PATCH002-RC3:1.7
	netbsd-1-6-PATCH002-RC2:1.7
	netbsd-1-6-PATCH002-RC1:1.7
	netbsd-1-6-PATCH001:1.7
	netbsd-1-6-PATCH001-RELEASE:1.7
	netbsd-1-6-PATCH001-RC3:1.7
	netbsd-1-6-PATCH001-RC2:1.7
	netbsd-1-6-PATCH001-RC1:1.7
	fvdl_fs64_base:1.7
	netbsd-1-6-RELEASE:1.7
	netbsd-1-6-RC3:1.7
	netbsd-1-6-RC2:1.7
	netbsd-1-6-RC1:1.7
	netbsd-1-6:1.7.0.2
	netbsd-1-6-base:1.7
	netbsd-1-5-PATCH003:1.3
	netbsd-1-5-PATCH002:1.3
	netbsd-1-5-PATCH001:1.3
	nvi_1_79:1.1.1.2
	netbsd-1-5-RELEASE:1.3
	netbsd-1-5-BETA2:1.3
	netbsd-1-5-BETA:1.3
	netbsd-1-4-PATCH003:1.2
	netbsd-1-5-ALPHA2:1.3
	netbsd-1-5:1.3.0.2
	netbsd-1-5-base:1.3
	minoura-xpg4dl-base:1.2
	minoura-xpg4dl:1.2.0.8
	netbsd-1-4-PATCH002:1.2
	wrstuden-devbsize-19991221:1.2
	wrstuden-devbsize:1.2.0.6
	wrstuden-devbsize-base:1.2
	comdex-fall-1999:1.2.0.4
	comdex-fall-1999-base:1.2
	netbsd-1-4-PATCH001:1.2
	netbsd-1-4-RELEASE:1.2
	netbsd-1-4:1.2.0.2
	netbsd-1-4-base:1.2
	netbsd-1-3-PATCH003:1.1.1.1
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.1.1
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.1.1
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.1.1
	netbsd-1-3-PATCH002:1.1.1.1
	netbsd-1-3-PATCH001:1.1.1.1
	netbsd-1-3-RELEASE:1.1.1.1
	netbsd-1-3-BETA:1.1.1.1
	netbsd-1-3:1.1.1.1.0.2
	netbsd-1-3-base:1.1.1.1
	netbsd-1-2-PATCH001:1.1.1.1
	netbsd-1-2-RELEASE:1.1.1.1
	netbsd-1-2-BETA:1.1.1.1
	netbsd-1-2:1.1.1.1.0.4
	netbsd-1-2-base:1.1.1.1
	nvi_1_66:1.1.1.1
	BOSTIC:1.1.1;
locks; strict;
comment	@ * @;


1.9
date	2008.10.29.16.49.34;	author christos;	state dead;
branches;
next	1.8;

1.8
date	2005.02.12.12.53.22;	author aymeric;	state Exp;
branches;
next	1.7;

1.7
date	2002.04.09.01.47.30;	author thorpej;	state Exp;
branches
	1.7.4.1
	1.7.6.1;
next	1.6;

1.6
date	2001.05.02.21.15.19;	author windsor;	state Exp;
branches;
next	1.5;

1.5
date	2001.05.01.16.46.11;	author aymeric;	state Exp;
branches;
next	1.4;

1.4
date	2001.03.31.11.37.45;	author aymeric;	state Exp;
branches;
next	1.3;

1.3
date	2000.05.31.19.49.23;	author jdc;	state Exp;
branches;
next	1.2;

1.2
date	98.01.09.08.06.20;	author perry;	state Exp;
branches
	1.2.8.1;
next	1.1;

1.1
date	96.05.20.01.58.19;	author mrg;	state Exp;
branches
	1.1.1.1;
next	;

1.7.4.1
date	2005.02.12.12.24.12;	author aymeric;	state Exp;
branches;
next	1.7.4.2;

1.7.4.2
date	2005.02.12.12.46.26;	author aymeric;	state Exp;
branches;
next	;

1.7.6.1
date	2005.05.06.14.42.14;	author riz;	state Exp;
branches;
next	;

1.2.8.1
date	2000.06.23.16.40.08;	author minoura;	state Exp;
branches;
next	;

1.1.1.1
date	96.05.20.01.58.19;	author mrg;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	2001.03.31.11.29.51;	author aymeric;	state Exp;
branches;
next	;


desc
@@


1.9
log
@bye old vi!
@
text
@/*	$NetBSD: cl_funcs.c,v 1.8 2005/02/12 12:53:22 aymeric Exp $	*/

/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
#if 0
static const char sccsid[] = "@@(#)cl_funcs.c	10.50 (Berkeley) 9/24/96";
#else
__RCSID("$NetBSD: cl_funcs.c,v 1.8 2005/02/12 12:53:22 aymeric Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <ctype.h>
#include <curses.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "../common/common.h"
#include "../vi/vi.h"
#include "cl.h"

/*
 * cl_addstr --
 *	Add len bytes from the string at the cursor, advancing the cursor.
 *
 * PUBLIC: int cl_addstr __P((SCR *, const char *, size_t));
 */
int
cl_addstr(sp, str, len)
	SCR *sp;
	const char *str;
	size_t len;
{
	CL_PRIVATE *clp;
	size_t oldy, oldx;
	int iv;

	clp = CLP(sp);

	/*
	 * If ex isn't in control, it's the last line of the screen and
	 * it's a split screen, use inverse video.
	 */
	iv = 0;
	getyx(stdscr, oldy, oldx);
	if (!F_ISSET(sp, SC_SCR_EXWROTE) &&
	    oldy == RLNO(sp, LASTLINE(sp)) && IS_SPLIT(sp)) {
		iv = 1;
		(void)standout();
	}

	if (addnstr(str, len) == ERR)
		return (1);

	if (iv)
		(void)standend();
	return (0);
}

/*
 * cl_attr --
 *	Toggle a screen attribute on/off.
 *
 * PUBLIC: int cl_attr __P((SCR *, scr_attr_t, int));
 */
int
cl_attr(sp, attribute, on)
	SCR *sp;
	scr_attr_t attribute;
	int on;
{
	CL_PRIVATE *clp;

	clp = CLP(sp);

	switch (attribute) {
	case SA_ALTERNATE:
	/*
	 * !!!
	 * There's a major layering violation here.  The problem is that the
	 * X11 xterm screen has what's known as an "alternate" screen.  Some
	 * xterm termcap/terminfo entries include sequences to switch to/from
	 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
	 * Vi runs in the alternate screen, so that you are returned to the
	 * same screen contents on exit from vi that you had when you entered
	 * vi.  Further, when you run :shell, or :!date or similar ex commands,
	 * you also see the original screen contents.  This wasn't deliberate
	 * on vi's part, it's just that it historically sent terminal init/end
	 * sequences at those times, and the addition of the alternate screen
	 * sequences to the strings changed the behavior of vi.  The problem
	 * caused by this is that we don't want to switch back to the alternate
	 * screen while getting a new command from the user, when the user is
	 * continuing to enter ex commands, e.g.:
	 *
	 *	:!date				<<< switch to original screen
	 *	[Hit return to continue]	<<< prompt user to continue
	 *	:command			<<< get command from user
	 *
	 * Note that the :command input is a true vi input mode, e.g., input
	 * maps and abbreviations are being done.  So, we need to be able to
	 * switch back into the vi screen mode, without flashing the screen. 
	 *
	 * To make matters worse, the curses initscr() and endwin() calls will
	 * do this automatically -- so, this attribute isn't as controlled by
	 * the higher level screen as closely as one might like.
	 */
	if (on) {
		if (clp->ti_te != TI_SENT) {
			clp->ti_te = TI_SENT;
			if (clp->smcup == NULL)
				(void)cl_getcap(sp, "smcup", &clp->smcup);
			if (clp->smcup != NULL)
				(void)tputs(clp->smcup, 1, cl_putchar);
		}
	} else
		if (clp->ti_te != TE_SENT) {
			clp->ti_te = TE_SENT;
			if (clp->rmcup == NULL)
				(void)cl_getcap(sp, "rmcup", &clp->rmcup);
			if (clp->rmcup != NULL)
				(void)tputs(clp->rmcup, 1, cl_putchar);
			(void)fflush(stdout);
		}
		(void)fflush(stdout);
		break;
	case SA_INVERSE:
		if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)) {
			if (clp->smso == NULL)
				return (1);
			if (on)
				(void)tputs(clp->smso, 1, cl_putchar);
			else
				(void)tputs(clp->rmso, 1, cl_putchar);
			(void)fflush(stdout);
		} else {
			if (on)
				(void)standout();
			else
				(void)standend();
		}
		break;
	default:
		abort();
	}
	return (0);
}

/*
 * cl_baud --
 *	Return the baud rate.
 *
 * PUBLIC: int cl_baud __P((SCR *, u_long *));
 */
int
cl_baud(sp, ratep)
	SCR *sp;
	u_long *ratep;
{
	CL_PRIVATE *clp;

	/*
	 * XXX
	 * There's no portable way to get a "baud rate" -- cfgetospeed(3)
	 * returns the value associated with some #define, which we may
	 * never have heard of, or which may be a purely local speed.  Vi
	 * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
	 * Try and detect the slow ones, and default to fast.
	 */
	clp = CLP(sp);
	switch (cfgetospeed(&clp->orig)) {
	case B50:
	case B75:
	case B110:
	case B134:
	case B150:
	case B200:
	case B300:
	case B600:
		*ratep = 600;
		break;
	case B1200:
		*ratep = 1200;
		break;
	default:
		*ratep = 9600;
		break;
	}
	return (0);
}

/*
 * cl_bell --
 *	Ring the bell/flash the screen.
 *
 * PUBLIC: int cl_bell __P((SCR *));
 */
int
cl_bell(sp)
	SCR *sp;
{
	if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE))
		(void)write(STDOUT_FILENO, "\07", 1);		/* \a */
	else {
		/*
		 * Vi has an edit option which determines if the terminal
		 * should be beeped or the screen flashed.
		 */
		if (O_ISSET(sp, O_FLASH))
			(void)flash();
		else
			(void)beep();
	}
	return (0);
}

/*
 * cl_clrtoeol --
 *	Clear from the current cursor to the end of the line.
 *
 * PUBLIC: int cl_clrtoeol __P((SCR *));
 */
int
cl_clrtoeol(sp)
	SCR *sp;
{
	return (clrtoeol() == ERR);
}

/*
 * cl_cursor --
 *	Return the current cursor position.
 *
 * PUBLIC: int cl_cursor __P((SCR *, size_t *, size_t *));
 */
int
cl_cursor(sp, yp, xp)
	SCR *sp;
	size_t *yp, *xp;
{
	/*
	 * The curses screen support splits a single underlying curses screen
	 * into multiple screens to support split screen semantics.  For this
	 * reason the returned value must be adjusted to be relative to the
	 * current screen, and not absolute.  Screens that implement the split
	 * using physically distinct screens won't need this hack.
	 */
	getyx(stdscr, *yp, *xp);
	*yp -= sp->woff;
	return (0);
}

/*
 * cl_deleteln --
 *	Delete the current line, scrolling all lines below it.
 *
 * PUBLIC: int cl_deleteln __P((SCR *));
 */
int
cl_deleteln(sp)
	SCR *sp;
{
	CHAR_T ch;
	CL_PRIVATE *clp;
	size_t col, lno, spcnt, oldy, oldx;

	clp = CLP(sp);

	/*
	 * This clause is required because the curses screen uses reverse
	 * video to delimit split screens.  If the screen does not do this,
	 * this code won't be necessary.
	 *
	 * If the bottom line was in reverse video, rewrite it in normal
	 * video before it's scrolled.
	 *
	 * Check for the existence of a chgat function; XSI requires it, but
	 * historic implementations of System V curses don't.   If it's not
	 * a #define, we'll fall back to doing it by hand, which is slow but
	 * acceptable.
	 *
	 * By hand means walking through the line, retrieving and rewriting
	 * each character.  Curses has no EOL marker, so track strings of
	 * spaces, and copy the trailing spaces only if there's a non-space
	 * character following.
	 */
	if (!F_ISSET(sp, SC_SCR_EXWROTE) && IS_SPLIT(sp)) {
		getyx(stdscr, oldy, oldx);
#ifdef mvchgat
		mvchgat(RLNO(sp, LASTLINE(sp)), 0, -1, A_NORMAL, 0, NULL);
#else
		for (lno = RLNO(sp, LASTLINE(sp)), col = spcnt = 0;;) {
			(void)move(lno, col);
			ch = winch(stdscr);
			if (isblank(ch))
				++spcnt;
			else {
				(void)move(lno, col - spcnt);
				for (; spcnt > 0; --spcnt)
					(void)addch(' ');
				(void)addch(ch);
			}
			if (++col >= sp->cols)
				break;
		}
#endif
		(void)move(oldy, oldx);
	}

	/*
	 * The bottom line is expected to be blank after this operation,
	 * and other screens must support that semantic.
	 */
	return (deleteln() == ERR);
}

/* 
 * cl_ex_adjust --
 *	Adjust the screen for ex.  This routine is purely for standalone
 *	ex programs.  All special purpose, all special case.
 *
 * PUBLIC: int cl_ex_adjust __P((SCR *, exadj_t));
 */
int
cl_ex_adjust(sp, action)
	SCR *sp;
	exadj_t action;
{
	CL_PRIVATE *clp;
	int cnt;

	clp = CLP(sp);
	switch (action) {
	case EX_TERM_SCROLL:
		/* Move the cursor up one line if that's possible. */
		if (clp->cuu1 != NULL)
			(void)tputs(clp->cuu1, 1, cl_putchar);
		else if (clp->cup != NULL)
			(void)tputs(tgoto(clp->cup,
			    0, LINES - 2), 1, cl_putchar);
		else
			return (0);
		/* FALLTHROUGH */
	case EX_TERM_CE:
		/* Clear the line. */
		if (clp->el != NULL) {
			(void)putchar('\r');
			(void)tputs(clp->el, 1, cl_putchar);
		} else {
			/*
			 * Historically, ex didn't erase the line, so, if the
			 * displayed line was only a single glyph, and <eof>
			 * was more than one glyph, the output would not fully
			 * overwrite the user's input.  To fix this, output
			 * the maxiumum character number of spaces.  Note,
			 * this won't help if the user entered extra prompt
			 * or <blank> characters before the command character.
			 * We'd have to do a lot of work to make that work, and
			 * it's almost certainly not worth the effort.
			 */
			for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
				(void)putchar('\b');
			for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
				(void)putchar(' ');
			(void)putchar('\r');
			(void)fflush(stdout);
		}
		break;
	default:
		abort();
	}
	return (0);
}

/*
 * cl_insertln --
 *	Push down the current line, discarding the bottom line.
 *
 * PUBLIC: int cl_insertln __P((SCR *));
 */
int
cl_insertln(sp)
	SCR *sp;
{
	/*
	 * The current line is expected to be blank after this operation,
	 * and the screen must support that semantic.
	 */
	return (insertln() == ERR);
}

/*
 * cl_keyval --
 *	Return the value for a special key.
 *
 * PUBLIC: int cl_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
 */
int
cl_keyval(sp, val, chp, dnep)
	SCR *sp;
	scr_keyval_t val;
	CHAR_T *chp;
	int *dnep;
{
	CL_PRIVATE *clp;

	/*
	 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
	 * VWERASE is a 4BSD extension.
	 */
	clp = CLP(sp);
	switch (val) {
	case KEY_VEOF:
		*dnep = (*chp = clp->orig.c_cc[VEOF]) == _POSIX_VDISABLE;
		break;
	case KEY_VERASE:
		*dnep = (*chp = clp->orig.c_cc[VERASE]) == _POSIX_VDISABLE;
		break;
	case KEY_VKILL:
		*dnep = (*chp = clp->orig.c_cc[VKILL]) == _POSIX_VDISABLE;
		break;
#ifdef VWERASE
	case KEY_VWERASE:
		*dnep = (*chp = clp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE;
		break;
#endif
	default:
		*dnep = 1;
		break;
	}
	return (0);
}

/*
 * cl_move --
 *	Move the cursor.
 *
 * PUBLIC: int cl_move __P((SCR *, size_t, size_t));
 */
int
cl_move(sp, lno, cno)
	SCR *sp;
	size_t lno, cno;
{
	/* See the comment in cl_cursor. */
	if (move(RLNO(sp, lno), cno) == ERR) {
		msgq(sp, M_ERR,
		    "Error: move: l(%lu) c(%lu) o(%lu)", (u_long)lno, (u_long)cno, (u_long)sp->woff);
		return (1);
	}
	return (0);
}

/*
 * cl_refresh --
 *	Refresh the screen.
 *
 * PUBLIC: int cl_refresh __P((SCR *, int));
 */
int
cl_refresh(sp, repaint)
	SCR *sp;
	int repaint;
{
	CL_PRIVATE *clp;

	clp = CLP(sp);

	/*
	 * If we received a killer signal, we're done, there's no point
	 * in refreshing the screen.
	 */
	if (clp->killersig)
		return (0);

	/*
	 * If repaint is set, the editor is telling us that we don't know
	 * what's on the screen, so we have to repaint from scratch.
	 *
	 * In the curses library, doing wrefresh(curscr) is okay, but the
	 * screen flashes when we then apply the refresh() to bring it up
	 * to date.  So, use clearok().
	 */
	if (repaint)
		clearok(curscr, 1);
	return (refresh() == ERR);
}

/*
 * cl_rename --
 *	Rename the file.
 *
 * PUBLIC: int cl_rename __P((SCR *, char *, int));
 */
int
cl_rename(sp, name, on)
	SCR *sp;
	char *name;
	int on;
{
	GS *gp;
	CL_PRIVATE *clp;
	char *ttype;

	gp = sp->gp;
	clp = CLP(sp);

	ttype = OG_STR(gp, GO_TERM);

	/*
	 * XXX
	 * We can only rename windows for xterm.
	 */
	if (on) {
		if (F_ISSET(clp, CL_RENAME_OK) &&
		    !strncmp(ttype, "xterm", sizeof("xterm") - 1)) {
			F_SET(clp, CL_RENAME);
			(void)printf(XTERM_RENAME, name);
			(void)fflush(stdout);
		}
	} else
		if (F_ISSET(clp, CL_RENAME)) {
			F_CLR(clp, CL_RENAME);
			(void)printf(XTERM_RENAME, ttype);
			(void)fflush(stdout);
		}
	return (0);
}

/*
 * cl_suspend --
 *	Suspend a screen.
 *
 * PUBLIC: int cl_suspend __P((SCR *, int *));
 */
int
cl_suspend(sp, allowedp)
	SCR *sp;
	int *allowedp;
{
	struct termios t;
	CL_PRIVATE *clp;
	GS *gp;
	size_t oldy, oldx;
	int changed;

	gp = sp->gp;
	clp = CLP(sp);
	*allowedp = 1;

	/*
	 * The ex implementation of this function isn't needed by screens not
	 * supporting ex commands that require full terminal canonical mode
	 * (e.g. :suspend).
	 *
	 * The vi implementation of this function isn't needed by screens not
	 * supporting vi process suspension, i.e. any screen that isn't backed
	 * by a UNIX shell.
	 *
	 * Setting allowedp to 0 will cause the editor to reject the command.
	 */
	if (F_ISSET(sp, SC_EX)) { 
		/* Save the terminal settings, and restore the original ones. */
		if (F_ISSET(clp, CL_STDIN_TTY)) {
			(void)tcgetattr(STDIN_FILENO, &t);
			(void)tcsetattr(STDIN_FILENO,
			    TCSASOFT | TCSADRAIN, &clp->orig);
		}

		/* Stop the process group. */
		(void)kill(0, SIGTSTP);

		/* Time passes ... */

		/* Restore terminal settings. */
		if (F_ISSET(clp, CL_STDIN_TTY))
			(void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t);
		return (0);
	}

	/*
	 * Move to the lower left-hand corner of the screen.
	 *
	 * XXX
	 * Not sure this is necessary in System V implementations, but it
	 * shouldn't hurt.
	 */
	getyx(stdscr, oldy, oldx);
	(void)move(LINES - 1, 0);
	(void)refresh();

	/*
	 * Temporarily end the screen.  System V introduced a semantic where
	 * endwin() could be restarted.  We use it because restarting curses
	 * from scratch often fails in System V.  4BSD curses didn't support
	 * restarting after endwin(), so we have to do what clean up we can
	 * without calling it.
	 */
#ifdef HAVE_BSD_CURSES
	/* Save the terminal settings. */
	(void)tcgetattr(STDIN_FILENO, &t);
#endif

	/* Restore the cursor keys to normal mode. */
	(void)keypad(stdscr, FALSE);

	/* Restore the window name. */
	(void)cl_rename(sp, NULL, 0);

#ifdef HAVE_BSD_CURSES
	(void)cl_attr(sp, SA_ALTERNATE, 0);
#else
	(void)endwin();
#endif
	/*
	 * XXX
	 * Restore the original terminal settings.  This is bad -- the
	 * reset can cause character loss from the tty queue.  However,
	 * we can't call endwin() in BSD curses implementations, and too
	 * many System V curses implementations don't get it right.
	 */
	(void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);

	/* Stop the process group. */
	(void)kill(0, SIGTSTP);

	/* Time passes ... */

	/*
	 * If we received a killer signal, we're done.  Leave everything
	 * unchanged.  In addition, the terminal has already been reset
	 * correctly, so leave it alone.
	 */
	if (clp->killersig) {
		F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
		return (0);
	}

#ifdef HAVE_BSD_CURSES
	/* Restore terminal settings. */
	if (F_ISSET(clp, CL_STDIN_TTY))
		(void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t);

	(void)cl_attr(sp, SA_ALTERNATE, 1);
#endif

	/* Set the window name. */
	(void)cl_rename(sp, sp->frp->name, 1);

	/* Put the cursor keys into application mode. */
	(void)keypad(stdscr, TRUE);

	/* Refresh and repaint the screen. */
	(void)move(oldy, oldx);
	(void)cl_refresh(sp, 1);

	/* If the screen changed size, set the SIGWINCH bit. */
	if (cl_ssize(sp, 1, NULL, NULL, &changed))
		return (1);
	if (changed)
		F_SET(CLP(sp), CL_SIGWINCH);

	return (0);
}

/*
 * cl_usage --
 *	Print out the curses usage messages.
 * 
 * PUBLIC: void cl_usage __P((void));
 */
void
cl_usage()
{
#define	USAGE "\
usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
	(void)fprintf(stderr, "%s", USAGE);
#undef	USAGE
}

#ifdef DEBUG

int gdbrefresh __P((void));

/*
 * gdbrefresh --
 *	Stub routine so can flush out curses screen changes using gdb.
 */
int
gdbrefresh(void)
{
	refresh();
	return (0);		/* XXX Convince gdb to run it. */
}
#endif
@


1.8
log
@Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.7 2002/04/09 01:47:30 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.7
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.6 2001/05/02 21:15:19 windsor Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD");
@


1.7.6.1
log
@Pull up revision 1.8 (requested by aymeric in ticket #1195):
Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.7 2002/04/09 01:47:30 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.7.4.1
log
@Fix the RCSID's to be $NetBSD$ instead of $NetBSD
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.7 2002/04/09 01:47:30 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.7.4.2
log
@Backout previous. Sorry.
@
text
@d19 1
a19 1
__RCSID("$NetBSD");
@


1.6
log
@recent addition of -Wall to Makefile broke the build on some arch
remove warnings on /sparc (through typecasting)
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.5 2001/05/01 16:46:11 aymeric Exp $	*/
d14 1
d16 1
d18 3
@


1.5
log
@shutup most (non-intrusive) gcc warnings
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.4 2001/03/31 11:37:45 aymeric Exp $	*/
d461 1
a461 1
		    "Error: move: l(%u) c(%u) o(%u)", lno, cno, sp->woff);
@


1.4
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.3 2000/05/31 19:49:23 jdc Exp $	*/
d696 3
d704 1
a704 1
gdbrefresh()
@


1.3
log
@Handle xterm's alternate screen when entering or leaving ex mode, e.g.
":!ls", so that the screen is not changed before the "Press any key"
message.  Taken from v1.79.
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.2 1998/01/09 08:06:20 perry Exp $	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)cl_funcs.c	10.40 (Berkeley) 5/16/96";
d478 11
d506 1
a506 1
 * PUBLIC: int cl_rename __P((SCR *));
d509 1
a509 1
cl_rename(sp)
d511 2
d514 27
a540 1
	return (0);			/* Curses doesn't care. */
d577 1
a577 1
		if (F_ISSET(gp, G_STDIN_TTY)) {
d589 1
a589 1
		if (F_ISSET(gp, G_STDIN_TTY))
d620 3
d642 10
d654 1
a654 1
	if (F_ISSET(gp, G_STDIN_TTY))
d659 4
d689 2
a690 2
usage: ex [-eFRrsv] [-c command] [-t tag] [-w size] [file ...]\n\
usage: vi [-eFlRrv] [-c command] [-t tag] [-w size] [file ...]\n"
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d88 2
d91 49
a141 1
			clp = CLP(sp);
d582 1
a582 6
	/* Send the terminal end sequence. */
	if (clp->rmcup == NULL)
		(void)cl_getcap(sp, "rmcup", &clp->rmcup);
	if (clp->rmcup != NULL)
		(void)tputs(clp->rmcup, 1, cl_putchar);
	(void)fflush(stdout);
d605 1
a605 6
	/* Send the terminal initialization sequence. */
	if (clp->smcup == NULL)
		(void)cl_getcap(sp, "smcup", &clp->smcup);
	if (clp->smcup != NULL)
		(void)tputs(clp->smcup, 1, cl_putchar);
	(void)fflush(stdout);
@


1.2.8.1
log
@Sync w/ netbsd-1-5-base.
@
text
@d1 1
a1 1
/*	$NetBSD: cl_funcs.c,v 1.3 2000/05/31 19:49:23 jdc Exp $	*/
a87 2
	clp = CLP(sp);

a88 49
	case SA_ALTERNATE:
	/*
	 * !!!
	 * There's a major layering violation here.  The problem is that the
	 * X11 xterm screen has what's known as an "alternate" screen.  Some
	 * xterm termcap/terminfo entries include sequences to switch to/from
	 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
	 * Vi runs in the alternate screen, so that you are returned to the
	 * same screen contents on exit from vi that you had when you entered
	 * vi.  Further, when you run :shell, or :!date or similar ex commands,
	 * you also see the original screen contents.  This wasn't deliberate
	 * on vi's part, it's just that it historically sent terminal init/end
	 * sequences at those times, and the addition of the alternate screen
	 * sequences to the strings changed the behavior of vi.  The problem
	 * caused by this is that we don't want to switch back to the alternate
	 * screen while getting a new command from the user, when the user is
	 * continuing to enter ex commands, e.g.:
	 *
	 *	:!date				<<< switch to original screen
	 *	[Hit return to continue]	<<< prompt user to continue
	 *	:command			<<< get command from user
	 *
	 * Note that the :command input is a true vi input mode, e.g., input
	 * maps and abbreviations are being done.  So, we need to be able to
	 * switch back into the vi screen mode, without flashing the screen. 
	 *
	 * To make matters worse, the curses initscr() and endwin() calls will
	 * do this automatically -- so, this attribute isn't as controlled by
	 * the higher level screen as closely as one might like.
	 */
	if (on) {
		if (clp->ti_te != TI_SENT) {
			clp->ti_te = TI_SENT;
			if (clp->smcup == NULL)
				(void)cl_getcap(sp, "smcup", &clp->smcup);
			if (clp->smcup != NULL)
				(void)tputs(clp->smcup, 1, cl_putchar);
		}
	} else
		if (clp->ti_te != TE_SENT) {
			clp->ti_te = TE_SENT;
			if (clp->rmcup == NULL)
				(void)cl_getcap(sp, "rmcup", &clp->rmcup);
			if (clp->rmcup != NULL)
				(void)tputs(clp->rmcup, 1, cl_putchar);
			(void)fflush(stdout);
		}
		(void)fflush(stdout);
		break;
d91 1
d532 6
a537 1
	(void)cl_attr(sp, SA_ALTERNATE, 0);
d560 6
a565 1
	(void)cl_attr(sp, SA_ALTERNATE, 1);
@


1.1
log
@Initial revision
@
text
@d1 2
@


1.1.1.1
log
@import of nvi 1.66
@
text
@@


1.1.1.2
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)cl_funcs.c	10.50 (Berkeley) 9/24/96";
a85 2
	clp = CLP(sp);

a86 49
	case SA_ALTERNATE:
	/*
	 * !!!
	 * There's a major layering violation here.  The problem is that the
	 * X11 xterm screen has what's known as an "alternate" screen.  Some
	 * xterm termcap/terminfo entries include sequences to switch to/from
	 * that alternate screen as part of the ti/te (smcup/rmcup) strings.
	 * Vi runs in the alternate screen, so that you are returned to the
	 * same screen contents on exit from vi that you had when you entered
	 * vi.  Further, when you run :shell, or :!date or similar ex commands,
	 * you also see the original screen contents.  This wasn't deliberate
	 * on vi's part, it's just that it historically sent terminal init/end
	 * sequences at those times, and the addition of the alternate screen
	 * sequences to the strings changed the behavior of vi.  The problem
	 * caused by this is that we don't want to switch back to the alternate
	 * screen while getting a new command from the user, when the user is
	 * continuing to enter ex commands, e.g.:
	 *
	 *	:!date				<<< switch to original screen
	 *	[Hit return to continue]	<<< prompt user to continue
	 *	:command			<<< get command from user
	 *
	 * Note that the :command input is a true vi input mode, e.g., input
	 * maps and abbreviations are being done.  So, we need to be able to
	 * switch back into the vi screen mode, without flashing the screen. 
	 *
	 * To make matters worse, the curses initscr() and endwin() calls will
	 * do this automatically -- so, this attribute isn't as controlled by
	 * the higher level screen as closely as one might like.
	 */
	if (on) {
		if (clp->ti_te != TI_SENT) {
			clp->ti_te = TI_SENT;
			if (clp->smcup == NULL)
				(void)cl_getcap(sp, "smcup", &clp->smcup);
			if (clp->smcup != NULL)
				(void)tputs(clp->smcup, 1, cl_putchar);
		}
	} else
		if (clp->ti_te != TE_SENT) {
			clp->ti_te = TE_SENT;
			if (clp->rmcup == NULL)
				(void)cl_getcap(sp, "rmcup", &clp->rmcup);
			if (clp->rmcup != NULL)
				(void)tputs(clp->rmcup, 1, cl_putchar);
			(void)fflush(stdout);
		}
		(void)fflush(stdout);
		break;
d89 1
a425 11
	CL_PRIVATE *clp;

	clp = CLP(sp);

	/*
	 * If we received a killer signal, we're done, there's no point
	 * in refreshing the screen.
	 */
	if (clp->killersig)
		return (0);

d443 1
a443 1
 * PUBLIC: int cl_rename __P((SCR *, char *, int));
d446 1
a446 1
cl_rename(sp, name, on)
a447 2
	char *name;
	int on;
d449 1
a449 27
	GS *gp;
	CL_PRIVATE *clp;
	char *ttype;

	gp = sp->gp;
	clp = CLP(sp);

	ttype = OG_STR(gp, GO_TERM);

	/*
	 * XXX
	 * We can only rename windows for xterm.
	 */
	if (on) {
		if (F_ISSET(clp, CL_RENAME_OK) &&
		    !strncmp(ttype, "xterm", sizeof("xterm") - 1)) {
			F_SET(clp, CL_RENAME);
			(void)printf(XTERM_RENAME, name);
			(void)fflush(stdout);
		}
	} else
		if (F_ISSET(clp, CL_RENAME)) {
			F_CLR(clp, CL_RENAME);
			(void)printf(XTERM_RENAME, ttype);
			(void)fflush(stdout);
		}
	return (0);
d486 1
a486 1
		if (F_ISSET(clp, CL_STDIN_TTY)) {
d498 1
a498 1
		if (F_ISSET(clp, CL_STDIN_TTY))
a528 3
	/* Restore the window name. */
	(void)cl_rename(sp, NULL, 0);

d530 6
a535 1
	(void)cl_attr(sp, SA_ALTERNATE, 0);
a552 10
	/*
	 * If we received a killer signal, we're done.  Leave everything
	 * unchanged.  In addition, the terminal has already been reset
	 * correctly, so leave it alone.
	 */
	if (clp->killersig) {
		F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
		return (0);
	}

d555 1
a555 1
	if (F_ISSET(clp, CL_STDIN_TTY))
d558 6
a563 1
	(void)cl_attr(sp, SA_ALTERNATE, 1);
a564 4

	/* Set the window name. */
	(void)cl_rename(sp, sp->frp->name, 1);

d591 2
a592 2
usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
@

