de_stdin.c

Go to the documentation of this file.
00001 /****************************************************************/
00002 /*                                                              */
00003 /*      de_stdin.c                                              */
00004 /*                                                              */
00005 /*              Processing input from the "de" user.            */
00006 /*                                                              */
00007 /****************************************************************/
00008 /*  origination         1989-Jan-15        Terrence W. Holm     */
00009 /****************************************************************/
00010 
00011 
00012 #include <sys/types.h>
00013 #include <termios.h>
00014 #include <signal.h>
00015 #include <unistd.h>
00016 #include <stdio.h>
00017 
00018 #include <minix/config.h>
00019 #include <minix/const.h>
00020 #include "../../servers/fs/const.h"
00021 #include "../../servers/fs/inode.h"
00022 
00023 #include "de.h"
00024 
00025 FORWARD _PROTOTYPE(int Timed_Get_Char , (int time ));
00026 FORWARD _PROTOTYPE(void Timed_Out , (int sig));
00027 
00028 
00029 
00030 
00031 /****************************************************************/
00032 /*                                                              */
00033 /*      Save_Term()                                             */
00034 /*                                                              */
00035 /*              Save the current terminal characteristics.      */
00036 /*                                                              */
00037 /*                                                              */
00038 /*      Set_Term()                                              */
00039 /*                                                              */
00040 /*              Set up the terminal characteristics.            */
00041 /*                                                              */
00042 /*                                                              */
00043 /*      Reset_Term()                                            */
00044 /*                                                              */
00045 /*              Restore the terminal characteristics.           */
00046 /*                                                              */
00047 /****************************************************************/
00048 
00049 
00050 static struct termios saved_term;
00051 
00052 
00053 void Save_Term()
00054 
00055   {
00056   tcgetattr( 0, &saved_term );
00057   }
00058 
00059 
00060 
00061 
00062 void Set_Term()
00063 
00064   {
00065   struct termios term;
00066 
00067   term = saved_term;
00068 
00069 
00070   /*  No tab expansion, no echo, don't map ^M to ^J, cbreak mode  */
00071 
00072   term.c_iflag &= ~ICRNL;
00073   term.c_oflag &= ~OPOST;
00074   term.c_lflag &= ~ICANON & ~ECHO;
00075 
00076 
00077   /*  Change the interrupt character to ^C  */
00078 
00079   term.c_cc[VINTR] = '\003';
00080 
00081   tcsetattr( 0, TCSANOW, &term );
00082   }
00083 
00084 
00085 
00086 
00087 void Reset_Term()
00088 
00089   {
00090   tcsetattr( 0, TCSANOW, &saved_term );
00091   }
00092 
00093 
00094 
00095 
00096 
00097 
00098 /****************************************************************/
00099 /*                                                              */
00100 /*      Get_Char()                                              */
00101 /*                                                              */
00102 /*              Return the next input character. Escape         */
00103 /*              sequences are mapped to special codes.          */
00104 /*                                                              */
00105 /****************************************************************/
00106 
00107 
00108 int Get_Char()
00109   {
00110   int c;
00111   static int unget_char = EOF;
00112 
00113 
00114   /*  Flush the output to the screen before waiting  */
00115   /*  for input from the user.                       */
00116 
00117   fflush( stdout );
00118 
00119   if ( unget_char == EOF )
00120     {
00121     while ( (c = Timed_Get_Char( 60 * 60 )) < EOF )
00122       printf( "%c", BELL );
00123     }
00124   else
00125     {
00126     c = unget_char;
00127     unget_char = EOF;
00128     }
00129 
00130   if ( c == EOF )
00131     return( EOF );
00132 
00133   if ( c != ESCAPE )
00134     return( c );
00135 
00136   if ( (c = Timed_Get_Char( 1 )) <= EOF )
00137     return( ESCAPE );
00138 
00139   if ( c != '[' )
00140     {
00141     unget_char = c;
00142     return( ESCAPE );
00143     }
00144 
00145   if ( (c = Timed_Get_Char( 1 )) <= EOF )
00146     {
00147     unget_char = '[';
00148     return( ESCAPE );
00149     }
00150 
00151   return( c | 0x80 );   /* Flag ESC [ x  */
00152   }
00153 
00154 
00155 
00156 
00157 
00158 
00159 int Timed_Get_Char( time )
00160   int time;
00161 
00162   {
00163   char c;
00164   int  count;
00165 
00166   signal( SIGALRM, Timed_Out );
00167 
00168   alarm( time );
00169   count = read( 0, &c, 1 );
00170   alarm( 0 );
00171 
00172   if ( count <= 0 )
00173     return( EOF + count );
00174 
00175   return( c & 0x7f );
00176   }
00177 
00178 
00179 
00180 
00181 
00182 
00183 /****************************************************************/
00184 /*                                                              */
00185 /*      Get_Line()                                              */
00186 /*                                                              */
00187 /*              Read a line from the user. Returns a pointer    */
00188 /*              to a local buffer, or NULL if DEL or a non-     */
00189 /*              ASCII character was typed. Processes ^H and     */
00190 /*              ^U. ^M terminates the input.                    */
00191 /*                                                              */
00192 /****************************************************************/
00193 
00194 
00195 char *Get_Line()
00196 
00197   {
00198   int c;
00199   int i;
00200   static char line[ MAX_STRING + 1 ];
00201 
00202   for ( i = 0;  i <= MAX_STRING;  ++i )
00203     {
00204     c = Get_Char();
00205 
00206     if ( c == EOF  ||  c == DEL  ||  (c & 0x80) )
00207         return( NULL );
00208 
00209     if ( c == BS )
00210         {
00211         if ( --i >= 0 )
00212           {
00213           printf( "\b \b" );
00214           --i;
00215           }
00216         }
00217 
00218     else if ( c == CTRL_U )
00219         {
00220         for ( --i;  i >= 0;  --i )
00221           printf( "\b \b" );
00222         }
00223 
00224     else if ( c == '\r' )
00225         {
00226         line[ i ] = '\0';
00227         return( line );
00228         }
00229 
00230     else if ( i < MAX_STRING )
00231         {
00232         line[ i ] = c;
00233         Print_Ascii( c );
00234         }
00235 
00236     else  /*  Line buffer is full, don't add any more to it.  */
00237         {
00238         putchar( BELL );
00239         --i;
00240         }
00241     }
00242 
00243   Error( "Internal fault (line buffer overflow)" );
00244 
00245   /* NOTREACHED */
00246   return( NULL );
00247   }
00248 
00249 
00250 
00251 
00252 
00253 
00254 /****************************************************************/
00255 /*                                                              */
00256 /*      Arrow_Esc( char )                                       */
00257 /*                                                              */
00258 /*              If the keyboard does not generate Ansi escape   */
00259 /*              codes for the arrow keys, but does generate     */
00260 /*              single byte control codes, then map these       */
00261 /*              codes to the special characters we are using    */
00262 /*              to denote the Ansi escape codes.                */
00263 /*                                                              */
00264 /****************************************************************/
00265 
00266 
00267 extern  char   Kup;             /* (ku) - Up arrow key          */
00268 extern  char   Kdown;           /* (kd) - Down arrow key        */
00269 extern  char   Kleft;           /* (kl) - Left arrow key        */
00270 extern  char   Kright;          /* (kr) - Right arrow key       */
00271 
00272 
00273 int Arrow_Esc( c )
00274   int c;
00275 
00276   {
00277   if ( c == Kup )
00278     return( ESC_UP );
00279 
00280   if ( c == Kdown )
00281     return( ESC_DOWN );
00282 
00283   if ( c == Kleft )
00284     return( ESC_LEFT );
00285 
00286   if ( c == Kright )
00287     return( ESC_RIGHT );
00288 
00289   return( c );
00290   }
00291 
00292 void Timed_Out(sig)
00293 int sig;
00294   {}
00295 
00296 /*
00297  * $PchHeader: /mount/hd2/minix/sys/cmd/de/RCS/de_stdin.c,v 1.3 1995/02/10 08:01:30 philip Exp $
00298  */

Generated on Fri Apr 14 22:56:47 2006 for minix by  doxygen 1.4.6