Added lance entry to drivers.conf.
[minix3-old.git] / commands / de / de_stdin.c
blob5f440d8d6edf315f921476aee50f7514bf80ed01
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 /****************************************************************/
12 #include <sys/types.h>
13 #include <termios.h>
14 #include <signal.h>
15 #include <unistd.h>
16 #include <stdio.h>
18 #include <minix/config.h>
19 #include <minix/const.h>
20 #include "../../servers/mfs/const.h"
21 #include "../../servers/mfs/inode.h"
23 #include "de.h"
25 FORWARD _PROTOTYPE(int Timed_Get_Char , (int time ));
26 FORWARD _PROTOTYPE(void Timed_Out , (int sig));
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 /****************************************************************/
50 static struct termios saved_term;
53 void Save_Term()
56 tcgetattr( 0, &saved_term );
62 void Set_Term()
65 struct termios term;
67 term = saved_term;
70 /* No tab expansion, no echo, don't map ^M to ^J, cbreak mode */
72 term.c_iflag &= ~ICRNL;
73 term.c_oflag &= ~OPOST;
74 term.c_lflag &= ~ICANON & ~ECHO;
77 /* Change the interrupt character to ^C */
79 term.c_cc[VINTR] = '\003';
81 tcsetattr( 0, TCSANOW, &term );
87 void Reset_Term()
90 tcsetattr( 0, TCSANOW, &saved_term );
98 /****************************************************************/
99 /* */
100 /* Get_Char() */
101 /* */
102 /* Return the next input character. Escape */
103 /* sequences are mapped to special codes. */
104 /* */
105 /****************************************************************/
108 int Get_Char()
110 int c;
111 static int unget_char = EOF;
114 /* Flush the output to the screen before waiting */
115 /* for input from the user. */
117 fflush( stdout );
119 if ( unget_char == EOF )
121 while ( (c = Timed_Get_Char( 60 * 60 )) < EOF )
122 printf( "%c", BELL );
124 else
126 c = unget_char;
127 unget_char = EOF;
130 if ( c == EOF )
131 return( EOF );
133 if ( c != ESCAPE )
134 return( c );
136 if ( (c = Timed_Get_Char( 1 )) <= EOF )
137 return( ESCAPE );
139 if ( c != '[' )
141 unget_char = c;
142 return( ESCAPE );
145 if ( (c = Timed_Get_Char( 1 )) <= EOF )
147 unget_char = '[';
148 return( ESCAPE );
151 return( c | 0x80 ); /* Flag ESC [ x */
159 int Timed_Get_Char( time )
160 int time;
163 char c;
164 int count;
166 signal( SIGALRM, Timed_Out );
168 alarm( time );
169 count = read( 0, &c, 1 );
170 alarm( 0 );
172 if ( count <= 0 )
173 return( EOF + count );
175 return( c & 0x7f );
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 /****************************************************************/
195 char *Get_Line()
198 int c;
199 int i;
200 static char line[ MAX_STRING + 1 ];
202 for ( i = 0; i <= MAX_STRING; ++i )
204 c = Get_Char();
206 if ( c == EOF || c == DEL || (c & 0x80) )
207 return( NULL );
209 if ( c == BS )
211 if ( --i >= 0 )
213 printf( "\b \b" );
214 --i;
218 else if ( c == CTRL_U )
220 for ( --i; i >= 0; --i )
221 printf( "\b \b" );
224 else if ( c == '\r' )
226 line[ i ] = '\0';
227 return( line );
230 else if ( i < MAX_STRING )
232 line[ i ] = c;
233 Print_Ascii( c );
236 else /* Line buffer is full, don't add any more to it. */
238 putchar( BELL );
239 --i;
243 Error( "Internal fault (line buffer overflow)" );
245 /* NOTREACHED */
246 return( NULL );
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 /****************************************************************/
267 extern char Kup; /* (ku) - Up arrow key */
268 extern char Kdown; /* (kd) - Down arrow key */
269 extern char Kleft; /* (kl) - Left arrow key */
270 extern char Kright; /* (kr) - Right arrow key */
273 int Arrow_Esc( c )
274 int c;
277 if ( c == Kup )
278 return( ESC_UP );
280 if ( c == Kdown )
281 return( ESC_DOWN );
283 if ( c == Kleft )
284 return( ESC_LEFT );
286 if ( c == Kright )
287 return( ESC_RIGHT );
289 return( c );
292 void Timed_Out(sig)
293 int sig;
297 * $PchHeader: /mount/hd2/minix/sys/cmd/de/RCS/de_stdin.c,v 1.3 1995/02/10 08:01:30 philip Exp $