Skip to content
Newer
Older
100644 224 lines (184 sloc) 4.78 KB
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
1 /* osos2/tcaphelp.c */
2
3 /* This file contains code from the osunix/tcaphelp.c and osunix/tcaposix.h
4 * files necessary if Termcap support (emx ony) is desired. Else nothing
5 * usable is implemented yet. A minimal Termcap support could be of interest
6 * for the Open interface that is currently only available in the emx version.
7 */
8
9 #include "elvis.h"
10 #if defined(GUI_TERMCAP) || defined(GUI_OPEN)
11
12 # ifdef __EMX__
13
14 #include <termios.h>
15 #include <signal.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18
19 /* This variable is used by all versions to indicate which signals have
20 * been caught.
21 */
22 static long caught;
23
24
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
25 static struct termios oldtermio; /* original tty mode */
26 static struct termios newtermio; /* cbreak/noecho tty mode */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
27
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
28 static int ttykbd = 0;
29 static int ttyscr = 1;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
30
31 /* this function is used to catch signals */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
32 static void
33 catchsig(int signo)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
34 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
35 caught = (1 << signo);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
36 }
37
38 /* get the original tty state */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
39 void
40 ttyinit(void)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
41 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
42 /* get the old tty state */
43 tcgetattr(0, &oldtermio);
44
45 #ifdef FEATURE_STDIN
46 /* maybe use an alternate input */
47 if (stdin_not_kbd)
48 ttykbd = 2;
49 #endif
50
51 /* maybe use an alternate output */
52 if (!isatty(1))
53 ttyscr = 2;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
54 }
55
56 /* switch to the tty state that elvis runs in */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
57 void ttyraw(char *erasekey) /* where to store the ERASE char */
58 {
59 signal(SIGINT, catchsig);
60 signal(SIGQUIT, SIG_IGN);
61
62 /* switch to raw mode */
63 ospeed = cfgetospeed(&oldtermio);
64 *erasekey = oldtermio.c_cc[VERASE];
65 newtermio = oldtermio;
66 newtermio.c_iflag &= (IXON|IXOFF|ISTRIP|IGNBRK);
67 newtermio.c_oflag &= ~OPOST;
68 newtermio.c_lflag &= ISIG;
69 newtermio.c_cc[VINTR] = ELVCTRL('C'); /* always use ^C for interrupts */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
70 #ifdef NDEBUG
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
71 newtermio.c_cc[VQUIT] = 0;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
72 #endif
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
73 newtermio.c_cc[VMIN] = 1;
74 newtermio.c_cc[VTIME] = 0;
75 newtermio.c_cc[VSUSP] = 0;
76 tcsetattr(ttykbd, TCSADRAIN, &newtermio);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
77 }
78
79 /* switch back to the original tty state */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
80 void
81 ttynormal(void)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
82 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
83 tcsetattr(ttykbd, TCSADRAIN, &oldtermio);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
84 }
85
86 /* Read from keyboard with timeout. For POSIX, we use VMIN/VTIME to implement
87 * the timeout. For no timeout, VMIN should be 1 and VTIME should be 0; for
88 * timeout, VMIN should be 0 and VTIME should be the timeout value.
89 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
90 int ttyread(char *buf, /* where to place the read characters */
91 int len, /* maximum number of characters to read */
92 int timeout) /* timeout (0 for none) */
93 {
94 struct termios t, oldt;
95 int bytes; /* number of bytes actually read */
96
97 /* clear the "caught" variable */
98 caught = 0;
99
100 /* make sure the signal catcher hasn't been reset */
101 signal(SIGINT, catchsig);
102
103 /* arrange for timeout, and disable control chars */
104 tcgetattr(0, &t);
105 oldt = t;
106 if (timeout)
107 {
108 t.c_cc[VMIN] = 0;
109 t.c_cc[VTIME] = timeout;
110 }
111 else
112 {
113 t.c_cc[VMIN] = 1;
114 t.c_cc[VTIME] = 0;
115 }
116 t.c_cc[VINTR] = t.c_cc[VQUIT] = t.c_cc[VSTART] = t.c_cc[VSTOP] = 0;
117 tcsetattr(ttykbd, TCSANOW, &t);
118
119 /* Perform the read. */
120 bytes = read(ttykbd, buf, (unsigned)len);
121
122 /* revert to previous mode */
123 tcsetattr(ttykbd, TCSANOW, &oldt);
124
125 /* return the number of bytes read */
126 return bytes;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
127 }
128
129 /* write characters out to the screen */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
130 void
131 ttywrite (char *buf, /* buffer, holds characters to be written */
132 int len) /* number of characters in buf */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
133 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
134 write(ttyscr, buf, (size_t)len);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
135 }
136
137 /* determine the terminal type */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
138 char *
139 ttytermtype (void)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
140 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
141 char *type;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
142
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
143 type = getenv("TERM");
144 if (!type)
145 {
146 type = TTY_DEFAULT;
147 }
148 return type;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
149 }
150
151
152 /* This function gets the window size. */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
153 ELVBOOL
154 ttysize(int *linesptr, /* where to store the number of rows */
155 int *colsptr) /* where to store the number of columns */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
156 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
157 int coli[2];
158 _scrsize (coli);
159 *colsptr = coli[0];
160 *linesptr = coli[1];
161 return ElvTrue;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
162 }
163
164 /* Check for signs of boredom from user, so we can abort a time-consuming
165 * operation. Here we check to see if SIGINT has been caught recently.
166 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
167 ELVBOOL
168 ttypoll(ELVBOOL reset)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
169 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
170 return (caught & (1L << SIGINT)) ? ElvTrue : ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
171 }
172
173
174 #else /* not emx */
175
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
176 void
177 ttyinit (void)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
178 {
179 }
180
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
181 void
182 ttyraw (char *erasekey)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
183 {
184 }
185
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
186 void
187 ttynormal (void)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
188 {
189 }
190
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
191 int
192 ttyread (char *buf, int len, int timeout)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
193 {
194 }
195
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
196 void
197 ttywrite (char *buf, int len)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
198 {
199 }
200
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
201 char *
202 ttytermtype (void)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
203 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
204 char *type;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
205
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
206 type = getenv ("TERM");
207 if (!type)
208 {
209 type = TTY_DEFAULT;
210 }
211 return type;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
212 }
213
214
215 /* This function gets the window size. */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
216 ELVBOOL
217 ttysize (int *linesptr, int *colsptr)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
218 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
219 /* no special way to detect screen size */
220 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
221 }
222 #endif /* emx */
223 #endif /* GUI_TERMCAP || GUI_OPEN */
Something went wrong with that request. Please try again.