1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-18 07:16:23 -05:00
uemacs/termio.c

272 lines
6.6 KiB
C
Raw Normal View History

/* termio.c -- implements termio.h */
2020-11-02 21:37:39 -05:00
#if !defined( POSIX)
#include "termio.h"
/* TERMIO.C
*
* The functions in this file negotiate with the operating system for
* characters, and write characters in a barely buffered fashion on the display.
* All operating systems.
*
* modified by Petri Kutvonen
*/
#include <stdio.h>
#include <stdlib.h>
#include "estruct.h"
2013-10-10 23:20:06 -04:00
#include "retcode.h"
#include "utf8.h"
2013-04-30 22:06:08 -04:00
#include <unistd.h>
#include <sys/ioctl.h>
int ttrow = HUGE ; /* Row location of HW cursor */
int ttcol = HUGE ; /* Column location of HW cursor */
#if USG /* System V */
#include <signal.h>
#include <termio.h>
#include <fcntl.h>
static int kbdflgs ; /* saved keyboard fd flags */
static int kbdpoll ; /* in O_NDELAY mode */
static int kbdqp ; /* there is a char in kbdq */
static char kbdq ; /* char we've already read */
static struct termio otermio ; /* original terminal characteristics */
static struct termio ntermio ; /* characteristics to use inside */
#if XONXOFF
#define XXMASK 0016000
#endif
#endif
#if BSD
#include <sgtty.h> /* for stty/gtty functions */
#include <signal.h>
struct sgttyb ostate; /* saved tty state */
struct sgttyb nstate; /* values for editor mode */
struct tchars otchars; /* Saved terminal special character set */
#if XONXOFF
struct tchars ntchars = { 0xff, 0xff, 0x11, 0x13, 0xff, 0xff };
/* A lot of nothing and XON/XOFF */
#else
struct tchars ntchars = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
/* A lot of nothing */
#endif
#if BSD & PKCODE
struct ltchars oltchars; /* Saved terminal local special character set */
struct ltchars nltchars = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
/* A lot of nothing */
#endif
#if BSD
#include <sys/ioctl.h> /* to get at the typeahead */
extern int rtfrmshell(); /* return from suspended shell */
#define TBUFSIZ 128
char tobuf[TBUFSIZ]; /* terminal output buffer */
#endif
#endif
#if SVR4
extern int rtfrmshell(); /* return from suspended shell */
#define TBUFSIZ 128
char tobuf[TBUFSIZ]; /* terminal output buffer */
#endif
/*
* This function is called once to set up the terminal device streams.
2019-07-03 20:06:13 -04:00
* On CPM it is a no-op.
*/
uemacs: fix sparse warnings, making file-local symbols static. ansi.c:255:6: warning: symbol 'ansihello' was not declared. Should it be static? epath.h:11:6: warning: symbol 'pathname' was not declared. Should it be static? display.c:36:7: warning: symbol 'vscreen' was not declared. Should it be static? display.c:38:7: warning: symbol 'pscreen' was not declared. Should it be static? display.c:927:5: warning: symbol 'updateline' was not declared. Should it be static? evar.h:20:6: warning: symbol 'uv' was not declared. Should it be static? evar.h:24:6: warning: symbol 'envars' was not declared. Should it be static? evar.h:128:7: warning: symbol 'funcs' was not declared. Should it be static? fileio.c:14:6: warning: symbol 'ffp' was not declared. Should it be static? fileio.c:15:5: warning: symbol 'eofflag' was not declared. Should it be static? ibmpc.c:505:6: warning: symbol 'ibmhello' was not declared. Should it be static? isearch.c:36:5: warning: symbol 'saved_get_char' was not declared. Should it be static? isearch.c:37:5: warning: symbol 'eaten_char' was not declared. Should it be static? isearch.c:41:5: warning: symbol 'cmd_buff' was not declared. Should it be static? isearch.c:42:5: warning: symbol 'cmd_offset' was not declared. Should it be static? isearch.c:43:5: warning: symbol 'cmd_reexecute' was not declared. Should it be static? line.c:21:6: warning: symbol 'ykbuf' was not declared. Should it be static? line.c:22:5: warning: symbol 'ykboff' was not declared. Should it be static? lock.c:17:6: warning: symbol 'lname' was not declared. Should it be static? lock.c:18:5: warning: symbol 'numlocks' was not declared. Should it be static? vmsvt.c:521:6: warning: symbol 'hellovms' was not declared. Should it be static? vt52.c:181:6: warning: symbol 'vt52hello' was not declared. Should it be static? Signed-off-by: Thiago Farina <tfransosi@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2010-01-29 19:17:02 -05:00
void ttopen(void)
{
#if USG
ioctl(0, TCGETA, &otermio); /* save old settings */
ntermio.c_iflag = 0; /* setup new settings */
#if XONXOFF
ntermio.c_iflag = otermio.c_iflag & XXMASK; /* save XON/XOFF P.K. */
#endif
ntermio.c_oflag = 0;
ntermio.c_cflag = otermio.c_cflag;
ntermio.c_lflag = 0;
ntermio.c_line = otermio.c_line;
ntermio.c_cc[VMIN] = 1;
ntermio.c_cc[VTIME] = 0;
#if PKCODE
ioctl(0, TCSETAW, &ntermio); /* and activate them */
#else
ioctl(0, TCSETA, &ntermio); /* and activate them */
#endif
kbdflgs = fcntl(0, F_GETFL, 0);
kbdpoll = FALSE;
#endif
#if BSD
gtty(0, &ostate); /* save old state */
gtty(0, &nstate); /* get base of new state */
#if XONXOFF
nstate.sg_flags |= (CBREAK | TANDEM);
#else
nstate.sg_flags |= RAW;
#endif
nstate.sg_flags &= ~(ECHO | CRMOD); /* no echo for now... */
stty(0, &nstate); /* set mode */
ioctl(0, TIOCGETC, &otchars); /* Save old characters */
ioctl(0, TIOCSETC, &ntchars); /* Place new character into K */
#if BSD & PKCODE
ioctl(0, TIOCGLTC, &oltchars); /* Save old local characters */
ioctl(0, TIOCSLTC, &nltchars); /* New local characters */
#endif
#if BSD
/* provide a smaller terminal output buffer so that
the type ahead detection works better (more often) */
setbuffer(stdout, &tobuf[0], TBUFSIZ);
signal(SIGTSTP, SIG_DFL); /* set signals so that we can */
signal(SIGCONT, rtfrmshell); /* suspend & restart emacs */
#endif
#endif
#if SVR4
/* provide a smaller terminal output buffer so that
the type ahead detection works better (more often) */
setvbuf(stdout, &tobuf[0], _IOFBF, TBUFSIZ);
signal(SIGTSTP, SIG_DFL); /* set signals so that we can */
signal(SIGCONT, rtfrmshell); /* suspend & restart emacs */
TTflush();
#endif
/* on all screens we are not sure of the initial position
of the cursor */
ttrow = 999;
ttcol = 999;
}
/*
* This function gets called just before we go back home to the command
2019-07-03 20:06:13 -04:00
* interpreter.
* Another no-operation on CPM.
*/
uemacs: fix sparse warnings, making file-local symbols static. ansi.c:255:6: warning: symbol 'ansihello' was not declared. Should it be static? epath.h:11:6: warning: symbol 'pathname' was not declared. Should it be static? display.c:36:7: warning: symbol 'vscreen' was not declared. Should it be static? display.c:38:7: warning: symbol 'pscreen' was not declared. Should it be static? display.c:927:5: warning: symbol 'updateline' was not declared. Should it be static? evar.h:20:6: warning: symbol 'uv' was not declared. Should it be static? evar.h:24:6: warning: symbol 'envars' was not declared. Should it be static? evar.h:128:7: warning: symbol 'funcs' was not declared. Should it be static? fileio.c:14:6: warning: symbol 'ffp' was not declared. Should it be static? fileio.c:15:5: warning: symbol 'eofflag' was not declared. Should it be static? ibmpc.c:505:6: warning: symbol 'ibmhello' was not declared. Should it be static? isearch.c:36:5: warning: symbol 'saved_get_char' was not declared. Should it be static? isearch.c:37:5: warning: symbol 'eaten_char' was not declared. Should it be static? isearch.c:41:5: warning: symbol 'cmd_buff' was not declared. Should it be static? isearch.c:42:5: warning: symbol 'cmd_offset' was not declared. Should it be static? isearch.c:43:5: warning: symbol 'cmd_reexecute' was not declared. Should it be static? line.c:21:6: warning: symbol 'ykbuf' was not declared. Should it be static? line.c:22:5: warning: symbol 'ykboff' was not declared. Should it be static? lock.c:17:6: warning: symbol 'lname' was not declared. Should it be static? lock.c:18:5: warning: symbol 'numlocks' was not declared. Should it be static? vmsvt.c:521:6: warning: symbol 'hellovms' was not declared. Should it be static? vt52.c:181:6: warning: symbol 'vt52hello' was not declared. Should it be static? Signed-off-by: Thiago Farina <tfransosi@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2010-01-29 19:17:02 -05:00
void ttclose(void)
{
#if USG
#if PKCODE
ioctl(0, TCSETAW, &otermio); /* restore terminal settings */
#else
ioctl(0, TCSETA, &otermio); /* restore terminal settings */
#endif
fcntl(0, F_SETFL, kbdflgs);
#endif
#if BSD
stty(0, &ostate);
ioctl(0, TIOCSETC, &otchars); /* Place old character into K */
#if BSD & PKCODE
ioctl(0, TIOCSLTC, &oltchars); /* Place old local character into K */
#endif
#endif
}
/*
* Write a character to the display.
*/
int ttputc( unicode_t c) {
char utf8[ 4] ;
int bytes ;
bytes = unicode_to_utf8( c, utf8) ;
fwrite( utf8, 1, bytes, stdout) ;
return 0 ;
}
/*
* Flush terminal buffer. Does real work where the terminal output is buffered
* up. A no-operation on systems where byte at a time terminal I/O is done.
*/
2013-04-30 22:06:08 -04:00
void ttflush( void) {
#if USG | BSD
/*
* Add some terminal output success checking, sometimes an orphaned
* process may be left looping on SunOS 4.1.
*
* How to recover here, or is it best just to exit and lose
* everything?
*
* jph, 8-Oct-1993
*/
#include <errno.h>
int status;
status = fflush(stdout);
if (status != 0 && errno != EAGAIN) {
exit(errno);
}
#endif
}
/*
* Read a character from the terminal, performing no editing and doing no echo
2019-07-03 20:06:13 -04:00
* at all.
* Very simple on CPM, because the system can do exactly what you want.
*/
2013-04-30 22:06:08 -04:00
int ttgetc( void) {
#if BSD
return 255 & fgetc(stdin); /* 8BIT P.K. */
#endif
#if USG
if (kbdqp)
kbdqp = FALSE;
else {
if (kbdpoll && fcntl(0, F_SETFL, kbdflgs) < 0)
return FALSE;
kbdpoll = FALSE;
while (read(0, &kbdq, 1) != 1);
}
return kbdq & 255;
#endif
}
#if TYPEAH
/* typahead: Check to see if any characters are already in the
keyboard buffer
*/
2013-04-30 22:06:08 -04:00
int typahead( void)
{
#if BSD
int x; /* holds # of pending chars */
return (ioctl(0, FIONREAD, &x) < 0) ? 0 : x;
#endif
#if USG
if (!kbdqp) {
if (!kbdpoll && fcntl(0, F_SETFL, kbdflgs | O_NDELAY) < 0)
return FALSE;
#if PKCODE
kbdpoll = 1;
#endif
kbdqp = (1 == read(0, &kbdq, 1));
}
return kbdqp;
#endif
#if !UNIX
return FALSE;
#endif
}
#endif
2015-11-24 21:42:33 -05:00
#else
typedef void _pedantic_empty_translation_unit ;
#endif /* not POSIX */
2015-11-24 21:42:33 -05:00
/* end of termio.c */