2016-04-29 03:55:42 +00:00
# include <qse/si/pio.h>
2011-12-28 14:26:02 +00:00
# include <qse/cmn/mem.h>
2011-08-13 10:35:34 +00:00
# include <qse/cmn/env.h>
2016-04-29 03:55:42 +00:00
# include <qse/si/sio.h>
2009-01-15 03:58:27 +00:00
# include <locale.h>
2011-03-19 09:07:21 +00:00
# if defined(_WIN32)
2009-07-23 06:47:13 +00:00
# include <windows.h>
2011-03-19 09:07:21 +00:00
# elif defined(__OS2__)
# define INCL_DOSPROCESS
# define INCL_DOSERRORS
2011-08-13 10:35:34 +00:00
# define INCL_DOSDATETIME
2011-03-19 09:07:21 +00:00
# include <os2.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 )
2011-08-13 10:35:34 +00:00
static int pio1 ( const qse_char_t * cmd , qse_env_t * env , int oflags , qse_pio_hid_t rhid )
2009-01-15 03:58:27 +00:00
{
2012-02-19 14:38:22 +00:00
qse_pio_t pio ;
2009-01-15 03:58:27 +00:00
int x ;
2012-02-19 14:38:22 +00:00
if ( qse_pio_init ( & pio , QSE_MMGR_GETDFL ( ) , cmd , env , oflags ) < = - 1 )
2009-01-15 03:58:27 +00:00
{
2012-02-19 14:38:22 +00:00
qse_printf ( QSE_T ( " cannot open program through pipe - %d \n " ) ,
( int ) qse_pio_geterrnum ( & pio ) ) ;
2009-01-15 03:58:27 +00:00
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
2012-02-19 14:38:22 +00:00
/*qse_pio_canread (&pio, QSE_PIO_ERR, 1000)*/
qse_ssize_t n = qse_pio_read ( & pio , rhid , buf , QSE_SIZEOF ( buf ) ) ;
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 (
2012-02-12 13:20:39 +00:00
QSE_T ( " qse_pio_read() returned error - %d \n " ) ,
2012-02-19 14:38:22 +00:00
( int ) qse_pio_geterrnum ( & pio )
2009-07-25 06:22:00 +00:00
) ;
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
}
2012-02-19 14:38:22 +00:00
x = qse_pio_wait ( & pio ) ;
2009-01-29 08:50:30 +00:00
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
{
2012-02-19 14:38:22 +00:00
qse_printf ( QSE_T ( " error code : %d \n " ) , ( int ) qse_pio_geterrnum ( & pio ) ) ;
2009-01-15 03:58:27 +00:00
}
2012-02-19 14:38:22 +00:00
qse_pio_fini ( & pio ) ;
2009-01-15 03:58:27 +00:00
return 0 ;
}
2011-08-13 10:35:34 +00:00
static int pio2 ( const qse_char_t * cmd , qse_env_t * env , 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 (
2011-12-28 14:26:02 +00:00
QSE_MMGR_GETDFL ( ) ,
2009-01-15 03:58:27 +00:00
0 ,
cmd ,
2011-08-13 10:35:34 +00:00
env ,
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
2011-12-30 14:44:21 +00:00
qse_ssize_t n = qse_pio_read ( pio , rhid , buf , QSE_COUNTOF ( buf ) ) ;
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 (
2012-02-12 13:20:39 +00:00
QSE_T ( " qse_pio_read() returned error - %d \n " ) ,
( int ) qse_pio_geterrnum ( pio )
2009-07-25 06:22:00 +00:00
) ;
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
{
2012-02-12 13:20:39 +00:00
qse_printf ( QSE_T ( " error code : %d \n " ) , ( int ) qse_pio_geterrnum ( 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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
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
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-23 06:47:13 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
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
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-23 06:47:13 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
QSE_T ( " tree.com " ) ,
2009-07-24 07:01:52 +00:00
# else
QSE_T ( " /bin/ls -laF " ) ,
# endif
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-24 07:01:52 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
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
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-24 07:01:52 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
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
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-24 07:01:52 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
QSE_T ( " tree.com " ) ,
2009-07-24 07:01:52 +00:00
# else
QSE_T ( " /bin/ls -laF " ) ,
# endif
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-24 07:01:52 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
QSE_T ( " tree.com " ) ,
2009-07-24 07:01:52 +00:00
# else
QSE_T ( " /bin/ls -laF " ) ,
# endif
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-24 07:01:52 +00:00
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 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
QSE_T ( " tree.com " ) ,
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
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2009-07-24 07:01:52 +00:00
QSE_PIO_READOUT | QSE_PIO_WRITEIN |
QSE_PIO_OUTTONUL | QSE_PIO_ERRTONUL | QSE_PIO_INTONUL ,
QSE_PIO_OUT
) ;
}
static int test9 ( void )
2011-08-12 09:11:02 +00:00
{
return pio1 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
( const qse_char_t * ) " tree.com " ,
2011-08-12 09:11:02 +00:00
# else
( const qse_char_t * ) " /bin/ls -laF " ,
# endif
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2011-08-12 09:11:02 +00:00
QSE_PIO_MBSCMD | QSE_PIO_READOUT | QSE_PIO_WRITEIN ,
QSE_PIO_OUT
) ;
}
static int test10 ( void )
{
return pio1 (
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
2011-08-12 09:11:02 +00:00
( const qse_char_t * ) " dir /a " ,
# else
2011-08-13 10:35:34 +00:00
( const qse_char_t * ) " ls -laF " ,
2011-08-12 09:11:02 +00:00
# endif
2011-08-13 10:35:34 +00:00
QSE_NULL ,
2011-08-12 09:11:02 +00:00
QSE_PIO_MBSCMD | QSE_PIO_READOUT | QSE_PIO_WRITEIN | QSE_PIO_SHELL ,
QSE_PIO_OUT
) ;
}
static int test11 ( void )
2012-02-02 14:48:06 +00:00
{
return pio1 (
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
( const qse_char_t * ) " dir /a " ,
# else
( const qse_char_t * ) " ls -laF " ,
# endif
QSE_NULL ,
QSE_PIO_MBSCMD | QSE_PIO_READOUT | QSE_PIO_WRITEIN | QSE_PIO_DROPERR | QSE_PIO_INTONUL | QSE_PIO_SHELL ,
QSE_PIO_OUT
) ;
}
static int test12 ( void )
2011-08-13 10:35:34 +00:00
{
qse_env_t * env ;
int n ;
2011-12-28 14:26:02 +00:00
env = qse_env_open ( QSE_MMGR_GETDFL ( ) , 0 , 0 ) ;
2011-08-13 10:35:34 +00:00
if ( env = = QSE_NULL ) return - 1 ;
2012-02-08 12:59:59 +00:00
qse_env_insert ( env , QSE_T ( " PATH " ) , QSE_NULL ) ;
2011-08-13 10:35:34 +00:00
qse_env_insert ( env , QSE_T ( " HELLO " ) , QSE_T ( " WORLD " ) ) ;
n = pio1 (
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
QSE_T ( " set " ) ,
# else
QSE_T ( " printenv " ) ,
# endif
env ,
QSE_PIO_READOUT | QSE_PIO_WRITEIN | QSE_PIO_SHELL ,
QSE_PIO_OUT
) ;
qse_env_close ( env ) ;
return n ;
}
2012-02-02 14:48:06 +00:00
static int test13 ( 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 (
2011-12-28 14:26:02 +00:00
QSE_MMGR_GETDFL ( ) ,
2009-01-15 03:58:27 +00:00
0 ,
2011-08-13 10:35:34 +00:00
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
QSE_T ( " tree.com " ) ,
2009-07-24 07:01:52 +00:00
# else
2009-01-15 03:58:27 +00:00
QSE_T ( " /bin/ls -laF " ) ,
2009-07-24 07:01:52 +00:00
# endif
2011-08-10 10:08:26 +00:00
QSE_NULL ,
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 ;
}
2011-03-19 09:07:21 +00:00
# if defined(_WIN32)
2009-07-23 06:47:13 +00:00
{
int n = 5 ;
qse_printf ( QSE_T ( " sleeping for %d seconds \n " ) , n ) ;
Sleep ( n * 1000 ) ;
qse_printf ( QSE_T ( " WaitForSingleObject....%d \n " ) ,
2011-08-13 22:44:43 +00:00
( int ) WaitForSingleObject ( pio - > child , INFINITE ) ) ;
2009-07-23 06:47:13 +00:00
}
2011-03-19 09:07:21 +00:00
# elif defined(__OS2__)
{
int n = 5 ;
2011-08-13 10:35:34 +00:00
RESULTCODES result ;
PID pid ;
2011-03-19 09:07:21 +00:00
qse_printf ( QSE_T ( " sleeping for %d seconds \n " ) , n ) ;
DosSleep ( n * 1000 ) ;
2011-08-13 10:35:34 +00:00
/* it doesn't seem to proceed if the pipe is not read out.
* maybe the OS2 pipe buffer is too smally ? ? */
while ( 1 )
{
qse_mchar_t buf [ 100 ] ;
2011-12-30 14:44:21 +00:00
qse_ssize_t x = qse_pio_read ( pio , QSE_PIO_OUT , buf , QSE_SIZEOF ( buf ) ) ;
2011-08-13 10:35:34 +00:00
if ( x < = 0 ) break ;
}
qse_printf ( QSE_T ( " DosWaitChild....%d \n " ) ,
( int ) DosWaitChild ( DCWA_PROCESS , DCWW_WAIT , & result , & pid , pio - > child ) ) ;
2011-03-19 09:07:21 +00:00
}
2011-08-13 10:35:34 +00:00
# elif defined(__DOS__)
# error NOT SUPPORTED
2009-07-23 06:47:13 +00:00
# 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 )
{
2012-02-12 13:20:39 +00:00
qse_printf ( QSE_T ( " error code : %d \n " ) , ( int ) QSE_PIO_ERRNUM ( 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 ( )
{
2017-09-16 08:54:25 +00:00
qse_open_stdsios ( ) ;
2009-01-15 03:58:27 +00:00
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 ) ;
2011-08-12 09:11:02 +00:00
R ( test10 ) ;
R ( test11 ) ;
2011-08-13 10:35:34 +00:00
R ( test12 ) ;
2012-02-02 14:48:06 +00:00
R ( test13 ) ;
2017-09-16 08:54:25 +00:00
qse_close_stdsios ( ) ;
2009-01-15 03:58:27 +00:00
return 0 ;
}