2 * io.c -- dv1394d 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.
25 /* System header files */
33 /* Application header files */
36 char *chomp( char *input
)
40 int length
= strlen( input
);
41 if ( length
&& input
[ length
- 1 ] == '\n' )
42 input
[ length
- 1 ] = '\0';
43 if ( length
> 1 && input
[ length
- 2 ] == '\r' )
44 input
[ length
- 2 ] = '\0';
49 char *trim( char *input
)
53 int length
= strlen( input
);
55 while( first
< length
&& isspace( input
[ first
] ) )
57 memmove( input
, input
+ first
, length
- first
+ 1 );
58 length
= length
- first
;
59 while ( length
> 0 && isspace( input
[ length
- 1 ] ) )
60 input
[ -- length
] = '\0';
65 char *strip_quotes( char *input
)
69 char *ptr
= strrchr( input
, '\"' );
72 if ( input
[ 0 ] == '\"' )
73 strcpy( input
, input
+ 1 );
78 char *get_string( char *output
, int maxlength
, char *use
)
81 strcpy( output
, use
);
82 if ( trim( chomp( fgets( output
, maxlength
, stdin
) ) ) != NULL
)
84 if ( !strcmp( output
, "" ) )
85 strcpy( output
, use
);
91 int *get_int( int *output
, int use
)
96 if ( trim( chomp( fgets( temp
, 132, stdin
) ) ) != NULL
)
98 if ( strcmp( temp
, "" ) )
99 *output
= atoi( temp
);
105 /** This stores the previous settings
108 static struct termios oldtty
;
111 /** This is called automatically on application exit to restore the
112 previous tty settings.
119 tcsetattr( 0, TCSANOW
, &oldtty
);
124 /** Init terminal so that we can grab keys without blocking.
131 tcgetattr( 0, &tty
);
134 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
135 tty
.c_oflag
|= OPOST
;
136 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
137 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
139 tty
.c_cc
[ VMIN
] = 1;
140 tty
.c_cc
[ VTIME
] = 0;
142 tcsetattr( 0, TCSANOW
, &tty
);
149 /** Check for a keypress without blocking infinitely.
150 Returns: ASCII value of keypress or -1 if no keypress detected.
164 n
= select( 1, &rfds
, NULL
, NULL
, &tv
);
167 n
= read( 0, &ch
, 1 );
168 tcflush( 0, TCIFLUSH
);
184 while ( ( pressed
= term_read( ) ) == -1 ) ;
187 value
= (char)pressed
;
192 void wait_for_any_key( char *message
)
194 if ( message
== NULL
)
195 printf( "Press any key to continue: " );
197 printf( "%s", message
);