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 */
34 /* Application header files */
37 char *chomp( char *input
)
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';
50 char *trim( char *input
)
54 int length
= strlen( input
);
56 while( first
< length
&& isspace( input
[ 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';
66 char *strip_quotes( char *input
)
70 char *ptr
= strrchr( input
, '\"' );
73 if ( input
[ 0 ] == '\"' )
74 strcpy( input
, input
+ 1 );
79 char *get_string( char *output
, int maxlength
, char *use
)
82 strcpy( output
, use
);
83 if ( trim( chomp( fgets( output
, maxlength
, stdin
) ) ) != NULL
)
85 if ( !strcmp( output
, "" ) )
86 strcpy( output
, use
);
92 int *get_int( int *output
, int use
)
97 if ( trim( chomp( fgets( temp
, 132, stdin
) ) ) != NULL
)
99 if ( strcmp( temp
, "" ) )
100 *output
= atoi( temp
);
106 /** This stores the previous settings
109 static struct termios oldtty
;
112 /** This is called automatically on application exit to restore the
113 previous tty settings.
120 tcsetattr( 0, TCSANOW
, &oldtty
);
125 /** Init terminal so that we can grab keys without blocking.
132 tcgetattr( 0, &tty
);
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
);
140 tty
.c_cc
[ VMIN
] = 1;
141 tty
.c_cc
[ VTIME
] = 0;
143 tcsetattr( 0, TCSANOW
, &tty
);
150 /** Check for a keypress without blocking infinitely.
151 Returns: ASCII value of keypress or -1 if no keypress detected.
165 n
= select( 1, &rfds
, NULL
, NULL
, &tv
);
168 n
= read( 0, &ch
, 1 );
169 tcflush( 0, TCIFLUSH
);
185 while ( ( pressed
= term_read( ) ) == -1 ) ;
188 value
= (char)pressed
;
193 void wait_for_any_key( char *message
)
195 if ( message
== NULL
)
196 printf( "Press any key to continue: " );
198 printf( "%s", message
);