2 * io.c -- melt 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 */
36 /* Application header files */
39 char *chomp( char *input )
43 int length = strlen( input );
44 if ( length && input[ length - 1 ] == '\n' )
45 input[ length - 1 ] = '\0';
46 if ( length > 1 && input[ length - 2 ] == '\r' )
47 input[ length - 2 ] = '\0';
52 char *trim( char *input )
56 int length = strlen( input );
58 while( first < length && isspace( input[ first ] ) )
60 memmove( input, input + first, length - first + 1 );
61 length = length - first;
62 while ( length > 0 && isspace( input[ length - 1 ] ) )
63 input[ -- length ] = '\0';
68 char *strip_quotes( char *input )
72 char *ptr = strrchr( input, '\"' );
75 if ( input[ 0 ] == '\"' )
76 strcpy( input, input + 1 );
81 int *get_int( int *output, int use )
86 if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
88 if ( strcmp( temp, "" ) )
89 *output = atoi( temp );
95 /** This stores the previous settings
98 static struct termios oldtty;
101 /** This is called automatically on application exit to restore the
102 previous tty settings.
110 tcsetattr( 0, TCSANOW, &oldtty );
116 /** Init terminal so that we can grab keys without blocking.
124 tcgetattr( 0, &tty );
127 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
128 tty.c_oflag |= OPOST;
129 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
130 tty.c_cflag &= ~(CSIZE|PARENB);
132 tty.c_cc[ VMIN ] = 1;
133 tty.c_cc[ VTIME ] = 0;
135 tcsetattr( 0, TCSANOW, &tty );
143 /** Check for a keypress without blocking infinitely.
144 Returns: ASCII value of keypress or -1 if no keypress detected.
159 n = select( 1, &rfds, NULL, NULL, &tv );
162 n = read( 0, &ch, 1 );
163 tcflush( 0, TCIFLUSH );
180 while ( ( pressed = term_read( ) ) == -1 ) ;
183 value = (char)pressed;
188 void wait_for_any_key( char *message )
190 if ( message == NULL )
191 printf( "Press any key to continue: " );
193 printf( "%s", message );