Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 145 lines (123 sloc) 3.264 kb
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
1 /* tcaposix.c */
2
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
3 #ifdef FEATURE_RCSID
4 char id_tcaposix[] = "$Id: tcaposix.h,v 2.13 2003/01/26 19:41:36 steve Exp $";
5 #endif
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
6
7 #include <termios.h>
8 #include <signal.h>
9 #include <unistd.h>
10
11 /* HPUX does a "#define ttysize winsize". Elvis doesn't like that. */
12 #undef ttysize
13
14
15 #if USE_PROTOTYPES
16 static void catchsig(int signo);
17 #endif
18
19
20 static struct termios oldtermio; /* original tty mode */
21 static struct termios newtermio; /* cbreak/noecho tty mode */
22
23
24 /* this function is used to catch signals */
25 static void catchsig(signo)
26 int signo;
27 {
28 caught = (1 << signo);
29 }
30
31 /* get the original tty state */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
32 static void ttyinit2()
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
33 {
34 /* get the old tty state */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
35 tcgetattr(ttykbd, &oldtermio);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
36 }
37
38 /* switch to the tty state that elvis runs in */
39 void ttyraw(erasekey)
40 char *erasekey; /* where to store the ERASE char */
41 {
42 #ifdef SA_NOMASK
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
43 struct sigaction newsa;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
44
45 /* arrange for signals to be caught */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
46 newsa.sa_handler = catchsig;
47 newsa.sa_flags = (SA_INTERRUPT|SA_NOMASK); /* not one-shot */
48 #if 0
49 newsa.sa_restorer = NULL;
50 #endif
51 sigaction(SIGINT, &newsa, NULL);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
52 # ifdef SIGWINCH
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
53 sigaction(SIGWINCH, &newsa, NULL);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
54 # endif
55 #else
56 signal(SIGINT, catchsig);
57 # ifdef SIGWINCH
58 signal(SIGWINCH, catchsig);
59 # endif
60 #endif
61 signal(SIGQUIT, SIG_IGN);
62
63 /* switch to raw mode */
64 ospeed = cfgetospeed(&oldtermio);
65 *erasekey = oldtermio.c_cc[VERASE];
66 newtermio = oldtermio;
67 newtermio.c_iflag &= (IXON|IXOFF|ISTRIP|IGNBRK);
68 newtermio.c_oflag &= ~OPOST;
69 newtermio.c_lflag &= ISIG;
70 newtermio.c_cc[VINTR] = ELVCTRL('C'); /* always use ^C for interrupts */
71 #ifdef NDEBUG
72 newtermio.c_cc[VQUIT] = 0;
73 #endif
74 newtermio.c_cc[VMIN] = 1;
75 newtermio.c_cc[VTIME] = 0;
76 # ifdef VSWTCH
77 newtermio.c_cc[VSWTCH] = 0;
78 # endif
79 # ifdef VSWTC /* is this a bug in Linux's headers? */
80 newtermio.c_cc[VSWTC] = 0;
81 # endif
82 # ifdef VSUSP
83 newtermio.c_cc[VSUSP] = 0;
84 # endif
85 # ifdef VDSUSP
86 newtermio.c_cc[VDSUSP] = 0;
87 # endif
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
88 tcsetattr(ttykbd, TCSADRAIN, &newtermio);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
89 }
90
91 /* switch back to the original tty state */
92 void ttynormal()
93 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
94 tcsetattr(ttykbd, TCSADRAIN, &oldtermio);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
95 }
96
97 /* Read from keyboard with timeout. For POSIX, we use VMIN/VTIME to implement
98 * the timeout. For no timeout, VMIN should be 1 and VTIME should be 0; for
99 * timeout, VMIN should be 0 and VTIME should be the timeout value.
100 */
101 int ttyread(buf, len, timeout)
102 char *buf; /* where to place the read characters */
103 int len; /* maximum number of characters to read */
104 int timeout;/* timeout (0 for none) */
105 {
106 struct termios t, oldt;
107 int bytes; /* number of bytes actually read */
108
109 /* clear the "caught" variable */
110 caught = 0;
111
112 #ifndef SA_NOMASK
113 /* make sure the signal catcher hasn't been reset */
114 signal(SIGINT, catchsig);
115 # ifdef SIGWINCH
116 signal(SIGWINCH, catchsig);
117 # endif
118 #endif
119
120 /* arrange for timeout, and disable control chars */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
121 tcgetattr(ttykbd, &t);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
122 oldt = t;
123 if (timeout)
124 {
125 t.c_cc[VMIN] = 0;
126 t.c_cc[VTIME] = timeout;
127 }
128 else
129 {
130 t.c_cc[VMIN] = 1;
131 t.c_cc[VTIME] = 0;
132 }
133 t.c_cc[VINTR] = t.c_cc[VQUIT] = t.c_cc[VSTART] = t.c_cc[VSTOP] = 0;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
134 tcsetattr(ttykbd, TCSANOW, &t);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
135
136 /* Perform the read. */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
137 bytes = read(ttykbd, buf, (unsigned)len);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
138
139 /* revert to previous mode */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
140 tcsetattr(ttykbd, TCSANOW, &oldt);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
141
142 /* return the number of bytes read */
143 return bytes;
144 }
Something went wrong with that request. Please try again.