271f771d1368f7315ffbbb412a22642432562baf
[melted] / src / inigo / io.c
1 /*
2 * io.c -- dv1394d 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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 /* System header files */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <sys/time.h>
33
34 /* Application header files */
35 #include "io.h"
36
37 char *chomp( char *input )
38 {
39 if ( input != NULL )
40 {
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';
46 }
47 return input;
48 }
49
50 char *trim( char *input )
51 {
52 if ( input != NULL )
53 {
54 int length = strlen( input );
55 int first = 0;
56 while( first < length && isspace( input[ first ] ) )
57 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';
62 }
63 return input;
64 }
65
66 char *strip_quotes( char *input )
67 {
68 if ( input != NULL )
69 {
70 char *ptr = strrchr( input, '\"' );
71 if ( ptr != NULL )
72 *ptr = '\0';
73 if ( input[ 0 ] == '\"' )
74 strcpy( input, input + 1 );
75 }
76 return input;
77 }
78
79 char *get_string( char *output, int maxlength, char *use )
80 {
81 char *value = NULL;
82 strcpy( output, use );
83 if ( trim( chomp( fgets( output, maxlength, stdin ) ) ) != NULL )
84 {
85 if ( !strcmp( output, "" ) )
86 strcpy( output, use );
87 value = output;
88 }
89 return value;
90 }
91
92 int *get_int( int *output, int use )
93 {
94 int *value = NULL;
95 char temp[ 132 ];
96 *output = use;
97 if ( trim( chomp( fgets( temp, 132, stdin ) ) ) != NULL )
98 {
99 if ( strcmp( temp, "" ) )
100 *output = atoi( temp );
101 value = output;
102 }
103 return value;
104 }
105
106 /** This stores the previous settings
107 */
108
109 static struct termios oldtty;
110 static int mode = 0;
111
112 /** This is called automatically on application exit to restore the
113 previous tty settings.
114 */
115
116 void term_exit(void)
117 {
118 if ( mode == 1 )
119 {
120 tcsetattr( 0, TCSANOW, &oldtty );
121 mode = 0;
122 }
123 }
124
125 /** Init terminal so that we can grab keys without blocking.
126 */
127
128 void term_init( )
129 {
130 struct termios tty;
131
132 tcgetattr( 0, &tty );
133 oldtty = tty;
134
135 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
136 tty.c_oflag |= OPOST;
137 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
138 tty.c_cflag &= ~(CSIZE|PARENB);
139 tty.c_cflag |= CS8;
140 tty.c_cc[ VMIN ] = 1;
141 tty.c_cc[ VTIME ] = 0;
142
143 tcsetattr( 0, TCSANOW, &tty );
144
145 mode = 1;
146
147 atexit( term_exit );
148 }
149
150 /** Check for a keypress without blocking infinitely.
151 Returns: ASCII value of keypress or -1 if no keypress detected.
152 */
153
154 int term_read( )
155 {
156 int n = 1;
157 unsigned char ch;
158 struct timeval tv;
159 fd_set rfds;
160
161 FD_ZERO( &rfds );
162 FD_SET( 0, &rfds );
163 tv.tv_sec = 0;
164 tv.tv_usec = 40000;
165 n = select( 1, &rfds, NULL, NULL, &tv );
166 if (n > 0)
167 {
168 n = read( 0, &ch, 1 );
169 tcflush( 0, TCIFLUSH );
170 if (n == 1)
171 return ch;
172 return n;
173 }
174 return -1;
175 }
176
177 char get_keypress( )
178 {
179 char value = '\0';
180 int pressed = 0;
181
182 fflush( stdout );
183
184 term_init( );
185 while ( ( pressed = term_read( ) ) == -1 ) ;
186 term_exit( );
187
188 value = (char)pressed;
189
190 return value;
191 }
192
193 void wait_for_any_key( char *message )
194 {
195 if ( message == NULL )
196 printf( "Press any key to continue: " );
197 else
198 printf( "%s", message );
199
200 get_keypress( );
201
202 printf( "\n\n" );
203 }
204
205 void beep( )
206 {
207 printf( "%c", 7 );
208 fflush( stdout );
209 }