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 */
33 // MinGW defines struct timespec in pthread.h
39 /* Application header files */
42 char *chomp( char *input )
46 int length = strlen( input );
47 if ( length && input[ length - 1 ] == '\n' )
48 input[ length - 1 ] = '\0';
49 if ( length > 1 && input[ length - 2 ] == '\r' )
50 input[ length - 2 ] = '\0';
55 char *trim( char *input )
59 int length = strlen( input );
61 while( first < length && isspace( input[ first ] ) )
63 memmove( input, input + first, length - first + 1 );
64 length = length - first;
65 while ( length > 0 && isspace( input[ length - 1 ] ) )
66 input[ -- length ] = '\0';
71 char *strip_quotes( char *input )
75 char *ptr = strrchr( input, '\"' );
78 if ( input[ 0 ] == '\"' )
79 strcpy( input, input + 1 );
84 int *get_int( int *output, int use )
89 if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
91 if ( strcmp( temp, "" ) )
92 *output = atoi( temp );
98 /** This stores the previous settings
101 static struct termios oldtty;
104 /** This is called automatically on application exit to restore the
105 previous tty settings.
113 tcsetattr( 0, TCSANOW, &oldtty );
119 /** Init terminal so that we can grab keys without blocking.
127 tcgetattr( 0, &tty );
130 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
131 tty.c_oflag |= OPOST;
132 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
133 tty.c_cflag &= ~(CSIZE|PARENB);
135 tty.c_cc[ VMIN ] = 1;
136 tty.c_cc[ VTIME ] = 0;
138 tcsetattr( 0, TCSANOW, &tty );
146 /** Check for a keypress without blocking infinitely.
147 Returns: ASCII value of keypress or -1 if no keypress detected.
162 n = select( 1, &rfds, NULL, NULL, &tv );
165 n = read( 0, &ch, 1 );
166 tcflush( 0, TCIFLUSH );
172 struct timespec tm = { 0, 40000 };
173 nanosleep( &tm, NULL );
186 while ( ( pressed = term_read( ) ) == -1 ) ;
189 value = (char)pressed;
194 void wait_for_any_key( char *message )
196 if ( message == NULL )
197 printf( "Press any key to continue: " );
199 printf( "%s", message );