2 * io.c -- inigo input/output
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 /* System header files */
34 /* Application header files */
37 char *chomp( char *input )
41 int length = strlen( input );
42 if ( length && input[ length - 1 ] == '\n' )
43 input[ length - 1 ] = '\0';
44 if ( length > 1 && input[ length - 2 ] == '\r' )
45 input[ length - 2 ] = '\0';
50 char *trim( char *input )
54 int length = strlen( input );
56 while( first < length && isspace( input[ first ] ) )
58 memmove( input, input + first, length - first + 1 );
59 length = length - first;
60 while ( length > 0 && isspace( input[ length - 1 ] ) )
61 input[ -- length ] = '\0';
66 char *strip_quotes( char *input )
70 char *ptr = strrchr( input, '\"' );
73 if ( input[ 0 ] == '\"' )
74 strcpy( input, input + 1 );
79 int *get_int( int *output, int use )
84 if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
86 if ( strcmp( temp, "" ) )
87 *output = atoi( temp );
93 /** This stores the previous settings
96 static struct termios oldtty;
99 /** This is called automatically on application exit to restore the
100 previous tty settings.
107 tcsetattr( 0, TCSANOW, &oldtty );
112 /** Init terminal so that we can grab keys without blocking.
119 tcgetattr( 0, &tty );
122 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
123 tty.c_oflag |= OPOST;
124 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
125 tty.c_cflag &= ~(CSIZE|PARENB);
127 tty.c_cc[ VMIN ] = 1;
128 tty.c_cc[ VTIME ] = 0;
130 tcsetattr( 0, TCSANOW, &tty );
137 /** Check for a keypress without blocking infinitely.
138 Returns: ASCII value of keypress or -1 if no keypress detected.
152 n = select( 1, &rfds, NULL, NULL, &tv );
155 n = read( 0, &ch, 1 );
156 tcflush( 0, TCIFLUSH );
172 while ( ( pressed = term_read( ) ) == -1 ) ;
175 value = (char)pressed;
180 void wait_for_any_key( char *message )
182 if ( message == NULL )
183 printf( "Press any key to continue: " );
185 printf( "%s", message );