2 * io.c -- Valerie client demo 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.
21 /* System header files */
30 /* Application header files */
33 char *chomp( char *input
)
37 int length
= strlen( input
);
38 if ( length
&& input
[ length
- 1 ] == '\n' )
39 input
[ length
- 1 ] = '\0';
40 if ( length
> 1 && input
[ length
- 2 ] == '\r' )
41 input
[ length
- 2 ] = '\0';
46 char *trim( char *input
)
50 int length
= strlen( input
);
52 while( first
< length
&& isspace( input
[ first
] ) )
54 memmove( input
, input
+ first
, length
- first
+ 1 );
55 length
= length
- first
;
56 while ( length
> 0 && isspace( input
[ length
- 1 ] ) )
57 input
[ -- length
] = '\0';
62 char *strip_quotes( char *input
)
66 char *ptr
= strrchr( input
, '\"' );
69 if ( input
[ 0 ] == '\"' )
70 strcpy( input
, input
+ 1 );
75 char *io_get_string( char *output
, int maxlength
, char *use
)
78 strcpy( output
, use
);
79 if ( trim( chomp( fgets( output
, maxlength
, stdin
) ) ) != NULL
)
81 if ( !strcmp( output
, "" ) )
82 strcpy( output
, use
);
88 int *get_int( int *output
, int use
)
93 if ( trim( chomp( fgets( temp
, 132, stdin
) ) ) != NULL
)
95 if ( strcmp( temp
, "" ) )
96 *output
= atoi( temp
);
102 /** This stores the previous settings
105 static struct termios oldtty
;
108 /** This is called automatically on application exit to restore the
109 previous tty settings.
116 tcsetattr( 0, TCSANOW
, &oldtty
);
121 /** Init terminal so that we can grab keys without blocking.
128 tcgetattr( 0, &tty
);
131 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
132 tty
.c_oflag
|= OPOST
;
133 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
134 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
136 tty
.c_cc
[ VMIN
] = 1;
137 tty
.c_cc
[ VTIME
] = 0;
139 tcsetattr( 0, TCSANOW
, &tty
);
146 /** Check for a keypress without blocking infinitely.
147 Returns: ASCII value of keypress or -1 if no keypress detected.
161 n
= select( 1, &rfds
, NULL
, NULL
, &tv
);
164 n
= read( 0, &ch
, 1 );
165 tcflush( 0, TCIFLUSH
);
181 while ( ( pressed
= term_read( ) ) == -1 ) ;
184 value
= (char)pressed
;
189 void wait_for_any_key( char *message
)
191 if ( message
== NULL
)
192 printf( "Press any key to continue: " );
194 printf( "%s", message
);