forked from hughbarney/pEmacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
termio.c
102 lines (91 loc) · 2.18 KB
/
termio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* The functions in this file negotiate with the operating system for
* characters, and write characters in a barely buffered fashion on the
* display
*/
#undef CTRL
#include <termios.h>
#include <unistd.h>
#include <stdlib.h> /* exit */
#include <signal.h>
#include <stdio.h> /* puts(3), setbuffer(3), ... */
#include <sys/ioctl.h> /* to get at the typeahead */
extern void mlwrite();
extern void debug(char *,...);
void ttopen ();
void ttclose ();
void ttputc (int c);
void ttflush ();
int ttgetc ();
int typahead ();
#define TBUFSIZ 128
char tobuf[TBUFSIZ]; /* terminal output buffer */
struct termios ostate, nstate;
/*
* This function is called once to set up the terminal device streams.
*/
void ttopen ()
{
/* save terminal flags */
if ((tcgetattr(0, &ostate) < 0) || (tcgetattr(0, &nstate) < 0))
{
puts ("Can't read terminal capabilites\n");
exit (1);
}
cfmakeraw(&nstate); /* set raw mode */
nstate.c_cc[VMIN] = 1;
nstate.c_cc[VTIME] = 0; /* block indefinitely for a single char */
if (tcsetattr(0, TCSADRAIN, &nstate) < 0)
{
puts ("Can't set terminal mode\n");
exit (1);
}
/* 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);
}
/*
* This function gets called just before we go back home to the command
* interpreter
*/
void ttclose ()
{
ttflush ();
if (tcsetattr(0, TCSADRAIN, &ostate) < 0)
{
puts ("Can't restore terminal flags");
exit (1);
}
}
/*
* Write a character to the display
*/
void ttputc (int c)
{
fputc (c, stdout);
}
/*
* 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
*/
void ttflush ()
{
tcdrain (0);
fflush (stdout);
}
/*
* Read a character from the terminal, performing no editing and doing no echo
* at all
*/
int ttgetc ()
{
return (127 & fgetc (stdin));
}
/* typahead: Check to see if any characters are already in the keyboard buffer
*/
int typahead ()
{
int x; /* holds # of pending chars */
return ((ioctl (0, FIONREAD, &x) < 0) ? 0 : x);
}