1 /* ELLE - Copyright 1982, 1984, 1987 by Ken Harrenstien, SRI International
2 * This software is quasi-public; it may be used freely with
3 * like software, but may NOT be sold or made part of licensed
4 * products without permission of the author.
6 /* EESITE Site dependent frobs
7 * Primarily TS_ routines for TTY control. Most site-dependent
8 * routine is TS_INP for detection of TTY input.
16 #include <signal.h> /* For SIGTSTP in ts_pause */
21 int tsf_pause
= 0; /* Set if ts_pause works. Ref'd by equit in e_main */
23 #if !(SYSV || BBN) /* SYSV and BBN have weird tty calls */
26 #include <sys/ioctl.h>
28 struct termios origterm
, newterm
;
31 /* Normal V6 declarations, must provide explicitly */
43 /* Normal V7 UNIX declarations, can use include file */
47 struct sgttyb nstate
; /* Both V6 and V7 */
48 struct sgttyb ostate
; /* Both V6 and V7 */
49 #endif /*!(SYSV || BBN)*/
53 #if BBN /* BBN system frobs */
54 #include "/sys/sys/h/modtty.h"
59 #if DNTTY /* DN TTY frobs */
61 char partab
[2]; /* to satisfy obscene ref in tty.h */
65 #if (UCB || TOPS20) /* UCB, TOPS20 additional frobs */
66 #include <sys/ioctl.h> /* For ts_inp() and tldisc */
68 struct tchars otchars
, ntchars
; /* Original and new tchars */
70 #endif /*(UCB || TOPS20)*/
72 #if SYSV /* System V (and PC/IX) crocks */
74 #include <sys/ioctl.h>
76 struct termio
/* terminal i/o status flags */
77 origterm
, /* status of terminal at start of ELLE */
78 newterm
; /* status of terminal when using ELLE */
82 * Ask system if terminal input is available (on file descriptor 0).
83 * Returns non-zero if so, else returns zero.
84 * Very important that this call NOT hang or block in any way,
85 * because it is used to detect type-ahead by the user;
86 * return should be immediate whether or not input is waiting.
90 #if BBN /* Idiosyncratic */
92 capac (0, &cap_buf
[0], 4);
96 #if (DNTTY || ONYX) /* Have "empty()" syscall */
97 return(empty(0) ? 0 : 1);
98 #endif /*DNTTY || ONYX*/
99 #if (UCB || TOPS20) /* Have FIONREAD ioctl */
101 if(ioctl(0,FIONREAD
,&retval
)) /* If this call fails, */
102 return(0); /* assume no input waiting */
103 return((retval
? 1 : 0));
104 #endif /*UCB || TOPS20*/
107 ioctl(0, TIOCQUERY
, &retval
);
108 return((retval
? 1 : 0));
111 struct sgttyb iocbuf
;
112 ioctl(0, TIOCQCNT
, &iocbuf
);
113 return(iocbuf
.sg_ispeed
!= 0 );
116 #if !(BBN||COHERENT||DNTTY||ONYX||TOPS20||UCB||VENIX86)
117 return(0); /* Default - never any type-ahead, sigh */
123 * Get terminal information from system, initialize things for
124 * ts_enter and ts_exit. This is called before t_init.
125 * Must set "trm_ospeed".
130 signal(16,1); /* DN peculiar - turn off ctl-A */
133 #if !(MINIX || SYSV || BBN) /* Normal UNIX stuff */
134 ioctl(1, TIOCGETP
, &ostate
); /* Remember old state */
135 nstate
= ostate
; /* Set up edit-mode state vars */
136 nstate
.sg_flags
|= RAW
; /* We'll want raw mode */
137 nstate
.sg_flags
&= ~(ECHO
|CRMOD
); /* with no echoing */
138 trm_ospeed
= ostate
.sg_ospeed
;
141 /* Get around 4.1+ remote/local flow control bug (from Gosmacs) */
142 ioctl(0, TIOCGETC
, &otchars
); /* Save original tchars */
144 ntchars
.t_startc
= -1; /* Kill start/stop */
145 ntchars
.t_stopc
= -1;
146 ioctl(0, TIOCSETC
, &ntchars
);
147 #endif /*IMAGEN && UCB*/
148 #endif /*!(SYSV || BBN)*/
151 modtty(1, M_GET
| M_MODES
, &ostate
, sizeof(ostate
)); /* Save old */
152 modtty(1, M_GET
| M_MODES
, &nstate
, sizeof(nstate
)); /* Setup new */
153 nstate
.t_erase
= nstate
.t_kill
= nstate
.t_intr
= nstate
.t_esc
=
154 nstate
.t_eof
= nstate
.t_replay
= 0377;
155 nstate
.t_quit
= BELL
; /* ^G */
156 nstate
.t_breaks
= TB_ALL
; /* break on all */
157 nstate
.t_iflags
&= ~TI_ECHO
& ~TI_NOSPCL
& ~TI_CRMOD
;
158 /* no echos, specials on, no CR -> LF*/
159 nstate
.t_iflags
|= TI_CLR_MSB
; /* ignore parity */
160 nstate
.t_oflags
&= ~TO_CRMOD
& ~TO_AUTONL
; /* no CR -> NL */
161 if (trm_flags
& NOXONOFF
)
162 nstate
.t_oflags
&= ~TO_XONXOFF
;
164 nstate
.t_oflags
|= TO_XONXOFF
;
166 nstate
.t_oflags
|= TO_CLR_MSB
; /* no special high bits */
167 nstate
.t_pagelen
= 0; /* no paging of output */
168 trm_ospeed
= ostate
.t_ospeed
;
172 tcgetattr(0, &origterm
); /* How things are now */
173 newterm
= origterm
; /* Save them for restore on exit */
176 newterm
.c_iflag
|= IGNBRK
; /* Ignore break conditions.*/
177 newterm
.c_iflag
&= ~INLCR
; /* Don't map NL to CR on input */
178 newterm
.c_iflag
&= ~ICRNL
; /* Don't map CR to NL on input */
179 newterm
.c_iflag
&= ~BRKINT
; /* Do not signal on break.*/
180 newterm
.c_iflag
&= ~IXON
; /* Disable start/stop output control.*/
181 newterm
.c_iflag
&= ~IXOFF
; /* Disable start/stop input control.*/
184 newterm
.c_oflag
&= ~OPOST
; /* Disable output processing */
186 /* line discipline */
187 newterm
.c_lflag
&= ~ISIG
; /* Disable signals.*/
188 newterm
.c_lflag
&= ~ICANON
; /* Want to disable canonical I/O */
189 newterm
.c_lflag
&= ~ECHO
; /* Disable echo.*/
190 newterm
.c_lflag
&= ~ECHONL
; /* Disable separate NL echo.*/
191 newterm
.c_lflag
&= ~IEXTEN
; /* Disable input extensions.*/
193 newterm
.c_cc
[VMIN
] = 1; /* Min. chars. on input (immed) */
194 newterm
.c_cc
[VTIME
] = 0; /* Min. time delay on input (immed) */
197 tcsetattr(0, TCSANOW
, &newterm
);
201 ioctl(0, TCGETA
, &origterm
); /* How things are now */
202 newterm
= origterm
; /* Save them for restore on exit */
205 newterm
.c_iflag
|= IGNBRK
; /* Ignore break conditions.*/
206 newterm
.c_iflag
&= ~INLCR
; /* Don't map NL to CR on input */
207 newterm
.c_iflag
&= ~ICRNL
; /* Don't map CR to NL on input */
208 newterm
.c_iflag
&= ~BRKINT
; /* Do not signal on break.*/
209 newterm
.c_iflag
&= ~IXON
; /* Disable start/stop output control.*/
210 newterm
.c_iflag
&= ~IXOFF
; /* Disable start/stop input control.*/
212 /* line discipline */
213 newterm
.c_lflag
&= ~ISIG
; /* Disable signals.*/
214 newterm
.c_lflag
&= ~ICANON
; /* Want to disable canonical I/O */
215 newterm
.c_lflag
&= ~ECHO
; /* Disable echo.*/
217 newterm
.c_cc
[4] = 1; /* Min. chars. on input (immed) */
218 newterm
.c_cc
[5] = 1; /* Min. time delay on input (immed) */
221 ioctl(0, TCSETA
, &newterm
);
226 ioctl(0, TIOCGETD
, &tldisc
); /* Find line discipline */
228 /* The flag IGN_JOB_CONTROL has been introduced to allow job control haters
229 * to simply ignore the whole thing. When ELLE is compiled with
230 * -DIGN_JOB_CONTROL, it will exit properly when the Return to Superior
231 * command is executed.
234 #ifndef IGN_JOB_CONTROL
235 if(tldisc
== NTTYDISC
) tsf_pause
= 1;
240 #endif /*UCB || TOPS20*/
244 * Tell system to enter right terminal mode for editing.
245 * This is called before t_enter.
249 #if !(MINIX || SYSV || BBN)
250 ioctl(1, TIOCSETP
, &nstate
);
252 ioctl(0, TIOCSETC
, &ntchars
); /* Restore new tchars */
253 #endif /*IMAGEN && UCB*/
254 #endif /*!(SYSV||BBN)*/
257 modtty (1, M_SET
| M_MODES
, &nstate
, sizeof (nstate
));
261 /* Make it behave as previously defined in ts_init */
262 tcsetattr(0, TCSANOW
, &newterm
);
266 /* Make it behave as previously defined in ts_init */
267 ioctl(0, TCSETA
, &newterm
);
270 #if DNTTY /* DN hackery! Enable 8-bit input so as to read meta bit. */
272 { tpoke(TH_CSET
,T_2FLGS2
,EEI
); /* Enable ints */
273 tpoke(TH_CSETB
,T_QUIT
, 0377); /* Turn off QUIT intrpt */
275 else if(trm_flags
& TF_METAKEY
)
276 tpoke(TH_CSET
,T_2FLGS2
,T2_LITIN
); /* Turn on 8-bit input! */
281 * Tell system to restore old terminal mode (we are leaving edit mode).
282 * This is called after t_exit.
288 tpoke(TH_CCLR
,T_2FLGS2
,EEI
); /* Turn off EEI bit */
289 else if(trm_flags
& TF_METAKEY
)
290 tpoke(TH_CCLR
,T_2FLGS2
,T2_LITIN
); /* Turn off 8-bit input */
293 #if !(MINIX || SYSV || BBN)
294 ioctl(1, TIOCSETP
, &ostate
); /* SYSV and BBN don't use stty */
296 ioctl(0, TIOCSETC
, &otchars
); /* Restore original tchars */
297 #endif /*IMAGEN && UCB*/
298 #endif /*!(SYSV || BBN)*/
301 modtty (1, M_SET
| M_MODES
, &ostate
, sizeof (ostate
));
305 tcsetattr(0, TCSANOW
, &origterm
);
309 ioctl(0, TCSETA
, &origterm
);
314 int thkcmd
[] { 0, 0, -1 };
320 if(ttyhak(0,&thkcmd
) < 0)
322 else return(thkcmd
[1]);
327 /* TS_PAUSE - Stop process and return control of TTY to superior.
328 * There is also a flag variable, TSF_PAUSE, which indicates
329 * whether or not this routine will actually do anything.
344 signal(SIGTSTP
, SIG_DFL
);
346 #define mask(s) (1 << ((s)-1))
347 sigsetmask(sigblock(0) &~ mask(SIGTSTP
));
351 sigblock(mask(SIGTSTP
));
360 struct winsize winsize
;
362 if (ioctl(1, TIOCGWINSZ
, &winsize
) == 0) {
363 if (winsize
.ws_row
!= 0) scr_ht
= winsize
.ws_row
;
364 if (winsize
.ws_col
!= 0) scr_wid
= winsize
.ws_col
;