#include "sh.h"
RCSID("$Id: ed.init.c,v 1.1.1.2 2001/06/28 23:10:47 bbraun Exp $")
#include "ed.h"
#include "ed.term.h"
#include "tc.h"
#include "ed.defns.h"
int Tty_raw_mode = 0;
int MacroLvl = -1;
static int Tty_quote_mode = 0;
static unsigned char vdisable;
int Tty_eight_bit = -1;
extern bool GotTermCaps;
static ttydata_t extty, edtty, tstty;
#define qutty tstty
extern int insource;
#define SHTTY (insource ? OLDSTD : SHIN)
#define uc unsigned char
static unsigned char ttychars[NN_IO][C_NCC] = {
{
(uc)CINTR, (uc)CQUIT, (uc)CERASE, (uc)CKILL,
(uc)CEOF, (uc)CEOL, (uc)CEOL2, (uc)CSWTCH,
(uc)CDSWTCH, (uc)CERASE2, (uc)CSTART, (uc)CSTOP,
(uc)CWERASE, (uc)CSUSP, (uc)CDSUSP, (uc)CREPRINT,
(uc)CDISCARD, (uc)CLNEXT, (uc)CSTATUS, (uc)CPAGE,
(uc)CPGOFF, (uc)CKILL2, (uc)CBRK, (uc)CMIN,
(uc)CTIME
},
{
CINTR, CQUIT, CERASE, CKILL,
_POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
_POSIX_VDISABLE, CERASE2, CSTART, CSTOP,
_POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
CDISCARD, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
_POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 1,
0
},
{
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0
}
};
#ifdef SIG_WINDOW
void
check_window_size(force)
int force;
{
#ifdef BSDSIGS
sigmask_t omask;
#endif
int lins, cols;
#ifdef BSDSIGS
omask = sigblock(sigmask(SIG_WINDOW)) & ~sigmask(SIG_WINDOW);
#else
(void) sighold(SIG_WINDOW);
#endif
if (GetSize(&lins, &cols) || force) {
if (GettingInput) {
ClearLines();
ClearDisp();
MoveToLine(0);
MoveToChar(0);
ChangeSize(lins, cols);
Refresh();
}
else
ChangeSize(lins, cols);
}
#ifdef BSDSIGS
(void) sigsetmask(omask);
#else
(void) sigrelse(SIG_WINDOW);
#endif
}
sigret_t
window_change(snum)
int snum;
{
USE(snum);
#ifdef UNRELSIGS
if (snum > 0)
sigset(snum, window_change);
#endif
check_window_size(0);
#ifndef SIGVOID
return (snum);
#endif
}
#endif
void
ed_set_tty_eight_bit()
{
if (tty_getty(SHTTY, &extty) == -1) {
#ifdef DEBUG_TTY
xprintf("ed_set_tty_eight_bit: tty_getty: %s\n", strerror(errno));
#endif
return;
}
Tty_eight_bit = tty_geteightbit(&extty);
}
int
ed_Setup(rst)
int rst;
{
static int havesetup = 0;
struct varent *imode;
if (havesetup)
return(0);
#if defined(POSIX) && defined(_PC_VDISABLE) && !defined(BSD4_4) && \
!defined(WINNT_NATIVE)
{
long pcret;
if ((pcret = fpathconf(SHTTY, _PC_VDISABLE)) == -1L)
vdisable = (unsigned char) _POSIX_VDISABLE;
else
vdisable = (unsigned char) pcret;
if (vdisable != (unsigned char) _POSIX_VDISABLE && rst != 0)
for (rst = 0; rst < C_NCC; rst++) {
if (ttychars[ED_IO][rst] == (unsigned char) _POSIX_VDISABLE)
ttychars[ED_IO][rst] = vdisable;
if (ttychars[EX_IO][rst] == (unsigned char) _POSIX_VDISABLE)
ttychars[EX_IO][rst] = vdisable;
}
}
#else
vdisable = (unsigned char) _POSIX_VDISABLE;
#endif
if ((imode = adrof(STRinputmode)) != NULL) {
if (!Strcmp(*(imode->vec), STRinsert))
inputmode = MODE_INSERT;
else if (!Strcmp(*(imode->vec), STRoverwrite))
inputmode = MODE_REPLACE;
}
else
inputmode = MODE_INSERT;
ed_InitMaps();
Hist_num = 0;
Expand = 0;
#ifndef WINNT_NATIVE
if (tty_getty(SHTTY, &extty) == -1) {
# ifdef DEBUG_TTY
xprintf("ed_Setup: tty_getty: %s\n", strerror(errno));
# endif
return(-1);
}
tstty = edtty = extty;
T_Speed = tty_getspeed(&extty);
T_Tabs = tty_gettabs(&extty);
Tty_eight_bit = tty_geteightbit(&extty);
# if defined(POSIX) || defined(TERMIO)
extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
extty.d_t.c_iflag |= ttylist[EX_IO][M_INPUT].t_setmask;
extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
extty.d_t.c_oflag |= ttylist[EX_IO][M_OUTPUT].t_setmask;
extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
extty.d_t.c_cflag |= ttylist[EX_IO][M_CONTROL].t_setmask;
extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
extty.d_t.c_lflag |= ttylist[EX_IO][M_LINED].t_setmask;
# if defined(IRIX3_3) && SYSVREL < 4
extty.d_t.c_line = NTTYDISC;
# endif
# else
if (T_Tabs) {
extty.d_t.sg_flags &= ~(ttylist[EX_IO][M_CONTROL].t_clrmask|XTABS);
extty.d_t.sg_flags |= ttylist[EX_IO][M_CONTROL].t_setmask;
}
else {
extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
extty.d_t.sg_flags |= (ttylist[EX_IO][M_CONTROL].t_setmask|XTABS);
}
extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
extty.d_lb |= ttylist[EX_IO][M_LOCAL].t_setmask;
# endif
if (rst) {
if (tty_cooked_mode(&tstty)) {
tty_getchar(&tstty, ttychars[TS_IO]);
for (rst = 0; rst < C_NCC - 2; rst++)
if (ttychars[TS_IO][rst] != vdisable &&
ttychars[ED_IO][rst] != vdisable)
ttychars[ED_IO][rst] = ttychars[TS_IO][rst];
for (rst = 0; rst < C_NCC; rst++)
if (ttychars[TS_IO][rst] != vdisable &&
ttychars[EX_IO][rst] != vdisable)
ttychars[EX_IO][rst] = ttychars[TS_IO][rst];
}
tty_setchar(&extty, ttychars[EX_IO]);
if (tty_setty(SHTTY, &extty) == -1) {
# ifdef DEBUG_TTY
xprintf("ed_Setup: tty_setty: %s\n", strerror(errno));
# endif
return(-1);
}
}
else
tty_setchar(&extty, ttychars[EX_IO]);
# ifdef SIG_WINDOW
(void) sigset(SIG_WINDOW, window_change);
# endif
#else
# ifdef DEBUG
if (rst)
xprintf("rst received in ed_Setup() %d\n", rst);
# endif
#endif
havesetup = 1;
return(0);
}
void
ed_Init()
{
ResetInLine(1);
GettingInput = 0;
LastKill = KillBuf;
#ifdef DEBUG_EDIT
CheckMaps();
#endif
if (ed_Setup(0) == -1)
return;
if (!GotTermCaps)
GetTermCaps();
#ifndef WINNT_NATIVE
# if defined(TERMIO) || defined(POSIX)
edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
edtty.d_t.c_iflag |= ttylist[ED_IO][M_INPUT].t_setmask;
edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
edtty.d_t.c_oflag |= ttylist[ED_IO][M_OUTPUT].t_setmask;
edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
edtty.d_t.c_cflag |= ttylist[ED_IO][M_CONTROL].t_setmask;
edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
edtty.d_t.c_lflag |= ttylist[ED_IO][M_LINED].t_setmask;
# if defined(IRIX3_3) && SYSVREL < 4
edtty.d_t.c_line = NTTYDISC;
# endif
# else
if (T_Tabs) {
edtty.d_t.sg_flags &= ~(ttylist[ED_IO][M_CONTROL].t_clrmask | XTABS);
edtty.d_t.sg_flags |= ttylist[ED_IO][M_CONTROL].t_setmask;
}
else {
edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
edtty.d_t.sg_flags |= (ttylist[ED_IO][M_CONTROL].t_setmask | XTABS);
}
edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
edtty.d_lb |= ttylist[ED_IO][M_LOCAL].t_setmask;
# endif
tty_setchar(&edtty, ttychars[ED_IO]);
#endif
}
int
Rawmode()
{
if (Tty_raw_mode)
return (0);
#ifdef WINNT_NATIVE
do_nt_raw_mode();
#else
# ifdef _IBMR2
tty_setdisc(SHTTY, ED_IO);
# endif
if (tty_getty(SHTTY, &tstty) == -1) {
# ifdef DEBUG_TTY
xprintf("Rawmode: tty_getty: %s\n", strerror(errno));
# endif
return(-1);
}
# if defined(POSIX) || defined(TERMIO)
Tty_eight_bit = tty_geteightbit(&tstty);
T_Speed = tty_getspeed(&tstty);
# ifdef POSIX
if (tty_getspeed(&extty) != T_Speed || tty_getspeed(&edtty) != T_Speed) {
(void) cfsetispeed(&extty.d_t, T_Speed);
(void) cfsetospeed(&extty.d_t, T_Speed);
(void) cfsetispeed(&edtty.d_t, T_Speed);
(void) cfsetospeed(&edtty.d_t, T_Speed);
}
# endif
# else
T_Speed = tty_getspeed(&tstty);
Tty_eight_bit = tty_geteightbit(&tstty);
if (extty.d_t.sg_ispeed != tstty.d_t.sg_ispeed) {
extty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
edtty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
}
if (extty.d_t.sg_ospeed != tstty.d_t.sg_ospeed) {
extty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
edtty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
}
# endif
if (tty_cooked_mode(&tstty)) {
if (tty_gettabs(&tstty) == 0)
T_Tabs = 0;
else
T_Tabs = CanWeTab();
# if defined(POSIX) || defined(TERMIO)
extty.d_t.c_cflag = tstty.d_t.c_cflag;
extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
extty.d_t.c_cflag |= ttylist[EX_IO][M_CONTROL].t_setmask;
edtty.d_t.c_cflag = tstty.d_t.c_cflag;
edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
edtty.d_t.c_cflag |= ttylist[ED_IO][M_CONTROL].t_setmask;
extty.d_t.c_lflag = tstty.d_t.c_lflag;
extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
extty.d_t.c_lflag |= ttylist[EX_IO][M_LINED].t_setmask;
edtty.d_t.c_lflag = tstty.d_t.c_lflag;
edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
edtty.d_t.c_lflag |= ttylist[ED_IO][M_LINED].t_setmask;
extty.d_t.c_iflag = tstty.d_t.c_iflag;
extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
extty.d_t.c_iflag |= ttylist[EX_IO][M_INPUT].t_setmask;
edtty.d_t.c_iflag = tstty.d_t.c_iflag;
edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
edtty.d_t.c_iflag |= ttylist[ED_IO][M_INPUT].t_setmask;
extty.d_t.c_oflag = tstty.d_t.c_oflag;
extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
extty.d_t.c_oflag |= ttylist[EX_IO][M_OUTPUT].t_setmask;
edtty.d_t.c_oflag = tstty.d_t.c_oflag;
edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
edtty.d_t.c_oflag |= ttylist[ED_IO][M_OUTPUT].t_setmask;
# else
extty.d_t.sg_flags = tstty.d_t.sg_flags;
extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
extty.d_t.sg_flags |= ttylist[EX_IO][M_CONTROL].t_setmask;
if (T_Tabs)
extty.d_t.sg_flags &= ~XTABS;
else
extty.d_t.sg_flags |= XTABS;
extty.d_lb = tstty.d_lb;
extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
extty.d_lb |= ttylist[EX_IO][M_LOCAL].t_setmask;
edtty.d_t.sg_flags = extty.d_t.sg_flags;
if (T_Tabs) {
edtty.d_t.sg_flags &=
~(ttylist[ED_IO][M_CONTROL].t_clrmask|XTABS);
edtty.d_t.sg_flags |= ttylist[ED_IO][M_CONTROL].t_setmask;
}
else {
edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
edtty.d_t.sg_flags |=
(ttylist[ED_IO][M_CONTROL].t_setmask|XTABS);
}
edtty.d_lb = tstty.d_lb;
edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
edtty.d_lb |= ttylist[ED_IO][M_LOCAL].t_setmask;
# endif
{
extern int didsetty;
int i;
tty_getchar(&tstty, ttychars[TS_IO]);
for (i = 0; i < C_NCC; i++)
if (ttychars[TS_IO][i] != ttychars[EX_IO][i])
break;
if (i != C_NCC || didsetty) {
didsetty = 0;
for (i = 0; i < C_NCC; i++) {
if (!((ttylist[ED_IO][M_CHAR].t_setmask & C_SH(i))) &&
(ttychars[TS_IO][i] != ttychars[EX_IO][i]))
ttychars[ED_IO][i] = ttychars[TS_IO][i];
if (ttylist[ED_IO][M_CHAR].t_clrmask & C_SH(i))
ttychars[ED_IO][i] = vdisable;
}
tty_setchar(&edtty, ttychars[ED_IO]);
for (i = 0; i < C_NCC; i++) {
if (!((ttylist[EX_IO][M_CHAR].t_setmask & C_SH(i))) &&
(ttychars[TS_IO][i] != ttychars[EX_IO][i]))
ttychars[EX_IO][i] = ttychars[TS_IO][i];
if (ttylist[EX_IO][M_CHAR].t_clrmask & C_SH(i))
ttychars[EX_IO][i] = vdisable;
}
tty_setchar(&extty, ttychars[EX_IO]);
}
}
}
if (tty_setty(SHTTY, &edtty) == -1) {
# ifdef DEBUG_TTY
xprintf("Rawmode: tty_setty: %s\n", strerror(errno));
# endif
return(-1);
}
#endif
Tty_raw_mode = 1;
flush();
return (0);
}
int
Cookedmode()
{
#ifdef WINNT_NATIVE
do_nt_cooked_mode();
#else
signalfun_t orig_intr;
# ifdef _IBMR2
tty_setdisc(SHTTY, EX_IO);
# endif
if (!Tty_raw_mode)
return (0);
# ifdef BSDSIGS
orig_intr = (signalfun_t) signal(SIGINT, SIG_IGN);
# else
# ifdef SIG_HOLD
orig_intr = (signalfun_t) sigset(SIGINT, SIG_HOLD);
if (orig_intr != SIG_HOLD)
(void) sigset(SIGINT, SIG_IGN);
# else
orig_intr = (signalfun_t) sigset(SIGINT, SIG_IGN);
# endif
# endif
if (tty_setty(SHTTY, &extty) == -1) {
# ifdef DEBUG_TTY
xprintf("Cookedmode: tty_setty: %s\n", strerror(errno));
# endif
return -1;
}
# ifdef BSDSIGS
(void) signal(SIGINT, orig_intr);
# else
(void) sigset(SIGINT, orig_intr);
# endif
#endif
Tty_raw_mode = 0;
return (0);
}
void
ResetInLine(macro)
int macro;
{
Cursor = InputBuf;
LastChar = InputBuf;
InputLim = &InputBuf[INBUFSIZE - 2];
Mark = InputBuf;
MetaNext = 0;
CurrentKeyMap = CcKeyMap;
AltKeyMap = 0;
Hist_num = 0;
DoingArg = 0;
Argument = 1;
#ifdef notdef
LastKill = KillBuf;
#endif
LastCmd = F_UNASSIGNED;
if (macro)
MacroLvl = -1;
}
static Char *Input_Line = NULL;
int
Load_input_line()
{
#ifdef SUNOS4
long chrs = 0;
#else
int chrs = 0;
#endif
if (Input_Line)
xfree((ptr_t) Input_Line);
Input_Line = NULL;
if (Tty_raw_mode)
return 0;
#if defined(FIONREAD) && !defined(OREO)
(void) ioctl(SHIN, FIONREAD, (ioctl_t) &chrs);
if (chrs > 0) {
char buf[BUFSIZE];
chrs = read(SHIN, buf, (size_t) min(chrs, BUFSIZE - 1));
if (chrs > 0) {
buf[chrs] = '\0';
Input_Line = Strsave(str2short(buf));
PushMacro(Input_Line);
}
#ifdef convex
if (chrs < 0)
stderror(ERR_SYSTEM, progname, strerror(errno));
#endif
}
#endif
return chrs > 0;
}
void
QuoteModeOn()
{
if (MacroLvl >= 0)
return;
#ifndef WINNT_NATIVE
qutty = edtty;
#if defined(TERMIO) || defined(POSIX)
qutty.d_t.c_iflag &= ~ttylist[QU_IO][M_INPUT].t_clrmask;
qutty.d_t.c_iflag |= ttylist[QU_IO][M_INPUT].t_setmask;
qutty.d_t.c_oflag &= ~ttylist[QU_IO][M_OUTPUT].t_clrmask;
qutty.d_t.c_oflag |= ttylist[QU_IO][M_OUTPUT].t_setmask;
qutty.d_t.c_cflag &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
qutty.d_t.c_cflag |= ttylist[QU_IO][M_CONTROL].t_setmask;
qutty.d_t.c_lflag &= ~ttylist[QU_IO][M_LINED].t_clrmask;
qutty.d_t.c_lflag |= ttylist[QU_IO][M_LINED].t_setmask;
#else
qutty.d_t.sg_flags &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
qutty.d_t.sg_flags |= ttylist[QU_IO][M_CONTROL].t_setmask;
qutty.d_lb &= ~ttylist[QU_IO][M_LOCAL].t_clrmask;
qutty.d_lb |= ttylist[QU_IO][M_LOCAL].t_setmask;
#endif
if (tty_setty(SHTTY, &qutty) == -1) {
#ifdef DEBUG_TTY
xprintf("QuoteModeOn: tty_setty: %s\n", strerror(errno));
#endif
return;
}
#endif
Tty_quote_mode = 1;
return;
}
void
QuoteModeOff()
{
if (!Tty_quote_mode)
return;
Tty_quote_mode = 0;
if (tty_setty(SHTTY, &edtty) == -1) {
#ifdef DEBUG_TTY
xprintf("QuoteModeOff: tty_setty: %s\n", strerror(errno));
#endif
return;
}
return;
}