2009-01-29 08:50:30 +00:00
# include <qse/cmn/pio.h>
2009-06-04 15:50:32 +00:00
# include <qse/cmn/stdio.h>
2009-01-15 03:58:27 +00:00
# include <string.h>
# include <locale.h>
2009-07-23 06:47:13 +00:00
# ifdef _WIN32
# include <windows.h>
2009-07-25 06:22:00 +00:00
# else
# include <unistd.h>
# include <sys / wait.h>
2009-07-23 06:47:13 +00:00
# endif
2009-01-15 03:58:27 +00:00
# define R(f) \
do { \
qse_printf ( QSE_T ( " == %s == \n " ) , QSE_T ( # f ) ) ; \
if ( f ( ) = = - 1 ) return - 1 ; \
} while ( 0 )
2009-01-29 08:50:30 +00:00
static int pio1 ( const qse_char_t * cmd , int oflags , qse_pio_hid_t rhid )
2009-01-15 03:58:27 +00:00
{
2009-01-29 08:50:30 +00:00
qse_pio_t * pio ;
2009-01-15 03:58:27 +00:00
int x ;
2009-01-29 08:50:30 +00:00
pio = qse_pio_open (
2009-01-15 03:58:27 +00:00
QSE_NULL ,
0 ,
cmd ,
oflags
) ;
2009-01-29 08:50:30 +00:00
if ( pio = = QSE_NULL )
2009-01-15 03:58:27 +00:00
{
qse_printf ( QSE_T ( " cannot open program through pipe \n " ) ) ;
return - 1 ;
}
while ( 1 )
{
qse_byte_t buf [ 128 ] ;
2009-07-24 07:01:52 +00:00
qse_ssize_t i ;
2009-01-15 03:58:27 +00:00
2009-01-29 08:50:30 +00:00
/*qse_pio_canread (pio, QSE_PIO_ERR, 1000)*/
qse_ssize_t n = qse_pio_read ( pio , buf , sizeof ( buf ) , rhid ) ;
2009-01-15 03:58:27 +00:00
if ( n = = 0 ) break ;
2009-07-24 07:01:52 +00:00
if ( n < = - 1 )
2009-01-15 03:58:27 +00:00
{
2009-07-25 06:22:00 +00:00
qse_printf (
QSE_T ( " qse_pio_read() returned error - %s \n " ) ,
qse_pio_geterrmsg ( pio )
) ;
2009-01-15 03:58:27 +00:00
break ;
}
2009-07-24 07:01:52 +00:00
qse_printf ( QSE_T ( " N===> %d buf => [ " ) , ( int ) n ) ;
for ( i = 0 ; i < n ; i + + )
{
2009-01-15 03:58:27 +00:00
# ifdef QSE_CHAR_IS_MCHAR
2009-07-24 07:01:52 +00:00
qse_printf ( QSE_T ( " %c " ) , buf [ i ] ) ;
2009-01-15 03:58:27 +00:00
# else
2009-07-24 07:01:52 +00:00
qse_printf ( QSE_T ( " %C " ) , buf [ i ] ) ;
2009-01-15 03:58:27 +00:00
# endif
2009-07-24 07:01:52 +00:00
}
qse_printf ( QSE_T ( " ] \n " ) ) ;
2009-01-15 03:58:27 +00:00
}
2009-01-29 08:50:30 +00:00
x = qse_pio_wait ( pio ) ;
qse_printf ( QSE_T ( " qse_pio_wait returns %d \n " ) , x ) ;
2009-07-24 07:01:52 +00:00
if ( x < = - 1 )
2009-01-15 03:58:27 +00:00
{
2009-07-25 06:22:00 +00:00
qse_printf ( QSE_T ( " error code : %d, error string: %s \n " ) ,
( int ) qse_pio_geterrnum ( pio ) , qse_pio_geterrmsg ( pio ) ) ;
2009-01-15 03:58:27 +00:00
}
2009-01-29 08:50:30 +00:00
qse_pio_close ( pio ) ;
2009-01-15 03:58:27 +00:00
return 0 ;
}
2009-01-29 08:50:30 +00:00
static int pio2 ( const qse_char_t * cmd , int oflags , qse_pio_hid_t rhid )
2009-01-15 03:58:27 +00:00
{
2009-01-29 08:50:30 +00:00
qse_pio_t * pio ;
2009-01-15 03:58:27 +00:00
int x ;
2009-01-29 08:50:30 +00:00
pio = qse_pio_open (
2009-01-15 03:58:27 +00:00
QSE_NULL ,
0 ,
cmd ,
2009-01-29 08:50:30 +00:00
oflags | QSE_PIO_TEXT
2009-01-15 03:58:27 +00:00
) ;
2009-01-29 08:50:30 +00:00
if ( pio = = QSE_NULL )
2009-01-15 03:58:27 +00:00
{
qse_printf ( QSE_T ( " cannot open program through pipe \n " ) ) ;
return - 1 ;
}
while ( 1 )
{
qse_char_t buf [ 128 ] ;
2009-07-24 07:01:52 +00:00
qse_ssize_t i ;
2009-01-15 03:58:27 +00:00
2009-01-29 08:50:30 +00:00
qse_ssize_t n = qse_pio_read ( pio , buf , QSE_COUNTOF ( buf ) , rhid ) ;
2009-01-15 03:58:27 +00:00
if ( n = = 0 ) break ;
if ( n < 0 )
{
2009-07-25 06:22:00 +00:00
qse_printf (
QSE_T ( " qse_pio_read() returned error - %s \n " ) ,
qse_pio_geterrmsg ( pio )
) ;
2009-01-15 03:58:27 +00:00
break ;
}
2009-07-24 07:01:52 +00:00
qse_printf ( QSE_T ( " N===> %d buf => [ " ) , ( int ) n ) ;
for ( i = 0 ; i < n ; i + + )
{
qse_printf ( QSE_T ( " %c " ) , buf [ i ] ) ;
}
qse_printf ( QSE_T ( " ] \n " ) ) ;
2009-01-15 03:58:27 +00:00
}
2009-01-29 08:50:30 +00:00
x = qse_pio_wait ( pio ) ;
qse_printf ( QSE_T ( " qse_pio_wait returns %d \n " ) , x ) ;
2009-07-24 07:01:52 +00:00
if ( x < = - 1 )
2009-01-15 03:58:27 +00:00
{
2009-07-25 06:22:00 +00:00
qse_printf ( QSE_T ( " error code : %d, error string: %s \n " ) ,
( int ) qse_pio_geterrnum ( pio ) , qse_pio_geterrmsg ( pio ) ) ;
2009-01-15 03:58:27 +00:00
}
2009-01-29 08:50:30 +00:00
qse_pio_close ( pio ) ;
2009-01-15 03:58:27 +00:00
return 0 ;
}
static int test1 ( void )
{
2009-07-23 06:47:13 +00:00
return pio1 (
# ifdef _WIN32
2009-07-24 22:11:07 +00:00
QSE_T ( " dir /a " ) ,
2009-07-23 06:47:13 +00:00
# else
QSE_T ( " ls -laF " ) ,
# endif
QSE_PIO_READOUT | QSE_PIO_WRITEIN | QSE_PIO_SHELL ,
QSE_PIO_OUT
) ;
2009-01-15 03:58:27 +00:00
}
static int test2 ( void )
{
2009-07-23 06:47:13 +00:00
return pio1 (
# ifdef _WIN32
2009-07-24 22:11:07 +00:00
QSE_T ( " dir /a " ) ,
2009-07-23 06:47:13 +00:00
# else
QSE_T ( " ls -laF " ) ,
# endif
QSE_PIO_READERR | QSE_PIO_OUTTOERR | QSE_PIO_WRITEIN | QSE_PIO_SHELL ,
QSE_PIO_ERR
) ;
2009-01-15 03:58:27 +00:00
}
static int test3 ( void )
{
2009-07-24 07:01:52 +00:00
return pio1 (
# ifdef _WIN32
QSE_T ( " . \\ sll.exe " ) ,
# else
QSE_T ( " /bin/ls -laF " ) ,
# endif
QSE_PIO_READERR | QSE_PIO_OUTTOERR | QSE_PIO_WRITEIN ,
QSE_PIO_ERR
) ;
2009-01-15 03:58:27 +00:00
}
static int test4 ( void )
{
2009-07-24 07:01:52 +00:00
return pio2 (
# ifdef _WIN32
2009-07-24 22:11:07 +00:00
QSE_T ( " dir /a " ) ,
2009-07-24 07:01:52 +00:00
# else
QSE_T ( " ls -laF " ) ,
# endif
QSE_PIO_READOUT | QSE_PIO_WRITEIN | QSE_PIO_SHELL ,
QSE_PIO_OUT
) ;
2009-01-15 03:58:27 +00:00
}
static int test5 ( void )
{
2009-07-24 07:01:52 +00:00
return pio2 (
# ifdef _WIN32
2009-07-24 22:11:07 +00:00
QSE_T ( " dir /a " ) ,
2009-07-24 07:01:52 +00:00
# else
QSE_T ( " ls -laF " ) ,
# endif
QSE_PIO_READERR | QSE_PIO_OUTTOERR | QSE_PIO_WRITEIN | QSE_PIO_SHELL ,
QSE_PIO_ERR
) ;
2009-01-15 03:58:27 +00:00
}
static int test6 ( void )
{
2009-07-24 07:01:52 +00:00
return pio2 (
# ifdef _WIN32
QSE_T ( " . \\ sll.exe " ) ,
# else
QSE_T ( " /bin/ls -laF " ) ,
# endif
QSE_PIO_READERR | QSE_PIO_OUTTOERR | QSE_PIO_WRITEIN ,
QSE_PIO_ERR
) ;
2009-01-15 03:58:27 +00:00
}
static int test7 ( void )
{
2009-07-24 07:01:52 +00:00
return pio1 (
# ifdef _WIN32
QSE_T ( " . \\ sll.exe " ) ,
# else
QSE_T ( " /bin/ls -laF " ) ,
# endif
QSE_PIO_READOUT | QSE_PIO_ERRTOOUT | QSE_PIO_WRITEIN ,
QSE_PIO_OUT
2009-01-15 03:58:27 +00:00
) ;
}
static int test8 ( void )
2009-07-24 07:01:52 +00:00
{
return pio1 (
# ifdef _WIN32
2009-07-25 06:22:00 +00:00
QSE_T ( " . \\ sll.exe " ) ,
2009-07-24 07:01:52 +00:00
# else
2009-07-25 06:22:00 +00:00
QSE_T ( " /bin/ls -laF " ) ,
2009-07-24 07:01:52 +00:00
# endif
QSE_PIO_READOUT | QSE_PIO_WRITEIN |
QSE_PIO_OUTTONUL | QSE_PIO_ERRTONUL | QSE_PIO_INTONUL ,
QSE_PIO_OUT
) ;
}
static int test9 ( void )
2009-01-15 03:58:27 +00:00
{
2009-01-29 08:50:30 +00:00
qse_pio_t * pio ;
2009-01-15 03:58:27 +00:00
int x ;
2009-01-29 08:50:30 +00:00
pio = qse_pio_open (
2009-01-15 03:58:27 +00:00
QSE_NULL ,
0 ,
2009-07-24 07:01:52 +00:00
# ifdef _WIN32
QSE_T ( " . \\ sll.exe " ) ,
# else
2009-01-15 03:58:27 +00:00
QSE_T ( " /bin/ls -laF " ) ,
2009-07-24 07:01:52 +00:00
# endif
2009-01-29 08:50:30 +00:00
QSE_PIO_READOUT | QSE_PIO_READERR | QSE_PIO_WRITEIN
2009-01-15 03:58:27 +00:00
) ;
2009-01-29 08:50:30 +00:00
if ( pio = = QSE_NULL )
2009-01-15 03:58:27 +00:00
{
qse_printf ( QSE_T ( " cannot open program through pipe \n " ) ) ;
return - 1 ;
}
2009-07-23 06:47:13 +00:00
# ifdef _WIN32
{
int n = 5 ;
qse_printf ( QSE_T ( " sleeping for %d seconds \n " ) , n ) ;
Sleep ( n * 1000 ) ;
qse_printf ( QSE_T ( " WaitForSingleObject....%d \n " ) ,
WaitForSingleObject ( pio - > child , 0 ) ) ;
}
# else
2009-01-15 03:58:27 +00:00
{
int status ;
int n = 5 ;
qse_printf ( QSE_T ( " sleeping for %d seconds \n " ) , n ) ;
sleep ( n ) ;
qse_printf ( QSE_T ( " waitpid...%d \n " ) , ( int ) waitpid ( - 1 , & status , 0 ) ) ;
}
2009-07-23 06:47:13 +00:00
# endif
2009-01-15 03:58:27 +00:00
2009-01-29 08:50:30 +00:00
x = qse_pio_wait ( pio ) ;
qse_printf ( QSE_T ( " qse_pio_wait returns %d \n " ) , x ) ;
2009-01-15 03:58:27 +00:00
if ( x = = - 1 )
{
2009-07-23 06:47:13 +00:00
qse_printf ( QSE_T ( " error code : %d, error string: %s \n " ) , ( int ) QSE_PIO_ERRNUM ( pio ) , qse_pio_geterrmsg ( pio ) ) ;
2009-01-15 03:58:27 +00:00
}
2009-01-29 08:50:30 +00:00
qse_pio_close ( pio ) ;
2009-01-15 03:58:27 +00:00
return 0 ;
}
int main ( )
{
setlocale ( LC_ALL , " " ) ;
qse_printf ( QSE_T ( " -------------------------------------------------------------------------------- \n " ) ) ;
qse_printf ( QSE_T ( " Set the environment LANG to a Unicode locale such as UTF-8 if you see the illegal XXXXX errors. If you see such errors in Unicode locales, this program might be buggy. It is normal to see such messages in non-Unicode locales as it uses Unicode data \n " ) ) ;
qse_printf ( QSE_T ( " -------------------------------------------------------------------------------- \n " ) ) ;
R ( test1 ) ;
R ( test2 ) ;
R ( test3 ) ;
R ( test4 ) ;
R ( test5 ) ;
R ( test6 ) ;
R ( test7 ) ;
R ( test8 ) ;
2009-07-24 07:01:52 +00:00
R ( test9 ) ;
2009-01-15 03:58:27 +00:00
return 0 ;
}