42718df0249700958c5fd48ebb75d0c2a218efdc
[melted] / src / mvcp-client / io.c
1 /*
2 * io.c -- Valerie client demo input/output
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 /* System header files */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <termios.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29
30 /* Application header files */
31 #include "io.h"
32
33 char *chomp( char *input )
34 {
35 if ( input != NULL )
36 {
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';
42 }
43 return input;
44 }
45
46 char *trim( char *input )
47 {
48 if ( input != NULL )
49 {
50 int length = strlen( input );
51 int first = 0;
52 while( first < length && isspace( input[ first ] ) )
53 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';
58 }
59 return input;
60 }
61
62 char *strip_quotes( char *input )
63 {
64 if ( input != NULL )
65 {
66 char *ptr = strrchr( input, '\"' );
67 if ( ptr != NULL )
68 *ptr = '\0';
69 if ( input[ 0 ] == '\"' )
70 strcpy( input, input + 1 );
71 }
72 return input;
73 }
74
75 char *io_get_string( char *output, int maxlength, const char *use )
76 {
77 char *value = NULL;
78 strcpy( output, use );
79 if ( trim( chomp( fgets( output, maxlength, stdin ) ) ) != NULL )
80 {
81 if ( !strcmp( output, "" ) )
82 strcpy( output, use );
83 value = output;
84 }
85 return value;
86 }
87
88 int *get_int( int *output, int use )
89 {
90 int *value = NULL;
91 char temp[ 132 ];
92 *output = use;
93 if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
94 {
95 if ( strcmp( temp, "" ) )
96 *output = atoi( temp );
97 value = output;
98 }
99 return value;
100 }
101
102 /** This stores the previous settings
103 */
104
105 static struct termios oldtty;
106 static int mode = 0;
107
108 /** This is called automatically on application exit to restore the
109 previous tty settings.
110 */
111
112 void term_exit(void)
113 {
114 if ( mode == 1 )
115 {
116 tcsetattr( 0, TCSANOW, &oldtty );
117 mode = 0;
118 }
119 }
120
121 /** Init terminal so that we can grab keys without blocking.
122 */
123
124 void term_init( )
125 {
126 struct termios tty;
127
128 tcgetattr( 0, &tty );
129 oldtty = tty;
130
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);
135 tty.c_cflag |= CS8;
136 tty.c_cc[ VMIN ] = 1;
137 tty.c_cc[ VTIME ] = 0;
138
139 tcsetattr( 0, TCSANOW, &tty );
140
141 mode = 1;
142
143 atexit( term_exit );
144 }
145
146 /** Check for a keypress without blocking infinitely.
147 Returns: ASCII value of keypress or -1 if no keypress detected.
148 */
149
150 int term_read( )
151 {
152 int n = 1;
153 unsigned char ch;
154 struct timeval tv;
155 fd_set rfds;
156
157 FD_ZERO( &rfds );
158 FD_SET( 0, &rfds );
159 tv.tv_sec = 1;
160 tv.tv_usec = 0;
161 n = select( 1, &rfds, NULL, NULL, &tv );
162 if (n > 0)
163 {
164 n = read( 0, &ch, 1 );
165 tcflush( 0, TCIFLUSH );
166 if (n == 1)
167 return ch;
168 return n;
169 }
170 return -1;
171 }
172
173 char get_keypress( )
174 {
175 char value = '\0';
176 int pressed = 0;
177
178 fflush( stdout );
179
180 term_init( );
181 while ( ( pressed = term_read( ) ) == -1 ) ;
182 term_exit( );
183
184 value = (char)pressed;
185
186 return value;
187 }
188
189 void wait_for_any_key( const char *message )
190 {
191 if ( message == NULL )
192 printf( "Press any key to continue: " );
193 else
194 printf( "%s", message );
195
196 get_keypress( );
197
198 printf( "\n\n" );
199 }
200
201 void beep( )
202 {
203 printf( "%c", 7 );
204 fflush( stdout );
205 }