source: trunk/minix/commands/de/de_stdin.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 5.0 KB
Line 
1/****************************************************************/
2/* */
3/* de_stdin.c */
4/* */
5/* Processing input from the "de" user. */
6/* */
7/****************************************************************/
8/* origination 1989-Jan-15 Terrence W. Holm */
9/****************************************************************/
10
11
12#include <sys/types.h>
13#include <termios.h>
14#include <signal.h>
15#include <unistd.h>
16#include <stdio.h>
17
18#include <minix/config.h>
19#include <minix/const.h>
20#include "../../servers/fs/const.h"
21#include "../../servers/fs/inode.h"
22
23#include "de.h"
24
25FORWARD _PROTOTYPE(int Timed_Get_Char , (int time ));
26FORWARD _PROTOTYPE(void Timed_Out , (int sig));
27
28
29
30
31/****************************************************************/
32/* */
33/* Save_Term() */
34/* */
35/* Save the current terminal characteristics. */
36/* */
37/* */
38/* Set_Term() */
39/* */
40/* Set up the terminal characteristics. */
41/* */
42/* */
43/* Reset_Term() */
44/* */
45/* Restore the terminal characteristics. */
46/* */
47/****************************************************************/
48
49
50static struct termios saved_term;
51
52
53void Save_Term()
54
55 {
56 tcgetattr( 0, &saved_term );
57 }
58
59
60
61
62void Set_Term()
63
64 {
65 struct termios term;
66
67 term = saved_term;
68
69
70 /* No tab expansion, no echo, don't map ^M to ^J, cbreak mode */
71
72 term.c_iflag &= ~ICRNL;
73 term.c_oflag &= ~OPOST;
74 term.c_lflag &= ~ICANON & ~ECHO;
75
76
77 /* Change the interrupt character to ^C */
78
79 term.c_cc[VINTR] = '\003';
80
81 tcsetattr( 0, TCSANOW, &term );
82 }
83
84
85
86
87void Reset_Term()
88
89 {
90 tcsetattr( 0, TCSANOW, &saved_term );
91 }
92
93
94
95
96
97
98/****************************************************************/
99/* */
100/* Get_Char() */
101/* */
102/* Return the next input character. Escape */
103/* sequences are mapped to special codes. */
104/* */
105/****************************************************************/
106
107
108int Get_Char()
109 {
110 int c;
111 static int unget_char = EOF;
112
113
114 /* Flush the output to the screen before waiting */
115 /* for input from the user. */
116
117 fflush( stdout );
118
119 if ( unget_char == EOF )
120 {
121 while ( (c = Timed_Get_Char( 60 * 60 )) < EOF )
122 printf( "%c", BELL );
123 }
124 else
125 {
126 c = unget_char;
127 unget_char = EOF;
128 }
129
130 if ( c == EOF )
131 return( EOF );
132
133 if ( c != ESCAPE )
134 return( c );
135
136 if ( (c = Timed_Get_Char( 1 )) <= EOF )
137 return( ESCAPE );
138
139 if ( c != '[' )
140 {
141 unget_char = c;
142 return( ESCAPE );
143 }
144
145 if ( (c = Timed_Get_Char( 1 )) <= EOF )
146 {
147 unget_char = '[';
148 return( ESCAPE );
149 }
150
151 return( c | 0x80 ); /* Flag ESC [ x */
152 }
153
154
155
156
157
158
159int Timed_Get_Char( time )
160 int time;
161
162 {
163 char c;
164 int count;
165
166 signal( SIGALRM, Timed_Out );
167
168 alarm( time );
169 count = read( 0, &c, 1 );
170 alarm( 0 );
171
172 if ( count <= 0 )
173 return( EOF + count );
174
175 return( c & 0x7f );
176 }
177
178
179
180
181
182
183/****************************************************************/
184/* */
185/* Get_Line() */
186/* */
187/* Read a line from the user. Returns a pointer */
188/* to a local buffer, or NULL if DEL or a non- */
189/* ASCII character was typed. Processes ^H and */
190/* ^U. ^M terminates the input. */
191/* */
192/****************************************************************/
193
194
195char *Get_Line()
196
197 {
198 int c;
199 int i;
200 static char line[ MAX_STRING + 1 ];
201
202 for ( i = 0; i <= MAX_STRING; ++i )
203 {
204 c = Get_Char();
205
206 if ( c == EOF || c == DEL || (c & 0x80) )
207 return( NULL );
208
209 if ( c == BS )
210 {
211 if ( --i >= 0 )
212 {
213 printf( "\b \b" );
214 --i;
215 }
216 }
217
218 else if ( c == CTRL_U )
219 {
220 for ( --i; i >= 0; --i )
221 printf( "\b \b" );
222 }
223
224 else if ( c == '\r' )
225 {
226 line[ i ] = '\0';
227 return( line );
228 }
229
230 else if ( i < MAX_STRING )
231 {
232 line[ i ] = c;
233 Print_Ascii( c );
234 }
235
236 else /* Line buffer is full, don't add any more to it. */
237 {
238 putchar( BELL );
239 --i;
240 }
241 }
242
243 Error( "Internal fault (line buffer overflow)" );
244
245 /* NOTREACHED */
246 return( NULL );
247 }
248
249
250
251
252
253
254/****************************************************************/
255/* */
256/* Arrow_Esc( char ) */
257/* */
258/* If the keyboard does not generate Ansi escape */
259/* codes for the arrow keys, but does generate */
260/* single byte control codes, then map these */
261/* codes to the special characters we are using */
262/* to denote the Ansi escape codes. */
263/* */
264/****************************************************************/
265
266
267extern char Kup; /* (ku) - Up arrow key */
268extern char Kdown; /* (kd) - Down arrow key */
269extern char Kleft; /* (kl) - Left arrow key */
270extern char Kright; /* (kr) - Right arrow key */
271
272
273int Arrow_Esc( c )
274 int c;
275
276 {
277 if ( c == Kup )
278 return( ESC_UP );
279
280 if ( c == Kdown )
281 return( ESC_DOWN );
282
283 if ( c == Kleft )
284 return( ESC_LEFT );
285
286 if ( c == Kright )
287 return( ESC_RIGHT );
288
289 return( c );
290 }
291
292void Timed_Out(sig)
293int sig;
294 {}
295
296/*
297 * $PchHeader: /mount/hd2/minix/sys/cmd/de/RCS/de_stdin.c,v 1.3 1995/02/10 08:01:30 philip Exp $
298 */
Note: See TracBrowser for help on using the repository browser.