implemented the pipe to the child process and integrated it into awk

- renamed pio to pcp
- finished the first version of pcp for unix
- integrated pcp into awk
- yet to finish pcp for win32
This commit is contained in:
2009-01-15 03:58:27 +00:00
parent b1897b3b4d
commit 46e4ed5087
16 changed files with 1331 additions and 1145 deletions

View File

@ -1,5 +1,5 @@
pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h pio.h tio.h sio.h time.h
pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h pcp.h tio.h sio.h time.h
pkgincludedir= $(includedir)/qse/cmn

View File

@ -172,7 +172,7 @@ sysconfdir = @sysconfdir@
target_alias = @target_alias@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h pio.h tio.h sio.h time.h
pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h pcp.h tio.h sio.h time.h
CLEANFILES = *dist
all: all-am

321
qse/include/qse/cmn/pcp.h Normal file
View File

@ -0,0 +1,321 @@
/*
* $Id$
*
Copyright 2006-2008 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitapcpns under the License.
*/
#ifndef _QSE_CMN_PCP_H_
#define _QSE_CMN_PCP_H_
/* (P)ipe to a (C)hild (P)rocess */
#include <qse/types.h>
#include <qse/macros.h>
#include <qse/cmn/tio.h>
enum qse_pcp_open_flag_t
{
QSE_PCP_WRITEIN = (1 << 0),
QSE_PCP_READOUT = (1 << 1),
QSE_PCP_READERR = (1 << 2),
QSE_PCP_ERRTOOUT = (1 << 3),
QSE_PCP_OUTTOERR = (1 << 4),
QSE_PCP_INTONUL = (1 << 5),
QSE_PCP_ERRTONUL = (1 << 6),
QSE_PCP_OUTTONUL = (1 << 7),
QSE_PCP_DROPIN = (1 << 8),
QSE_PCP_DROPOUT = (1 << 9),
QSE_PCP_DROPERR = (1 << 10),
/* invoke the command through a default system shell */
QSE_PCP_SHELL = (1 << 11),
/* enable ase_char_t based IO */
QSE_PCP_TEXT = (1 << 12)
};
enum qse_pcp_wait_flag_t
{
QSE_PCP_NOHANG = (1 << 0),
QSE_PCP_IGNINTR = (1 << 1)
};
enum qse_pcp_hid_t
{
QSE_PCP_IN = 0,
QSE_PCP_OUT = 1,
QSE_PCP_ERR = 2
};
enum qse_pcp_err_t
{
QSE_PCP_ENOERR = 0,
QSE_PCP_ENOMEM, /* out of memory */
QSE_PCP_ENOHND, /* no handle available */
QSE_PCP_ECHILD, /* the child is not valid */
QSE_PCP_EINTR, /* interrupted */
QSE_PCP_ESYSCALL /* system call error */
};
typedef enum qse_pcp_hid_t qse_pcp_hid_t;
typedef enum qse_pcp_err_t qse_pcp_err_t;
#ifdef _WIN32
/* <winnt.h> => typedef PVOID HANDLE; */
typedef void* qse_pcp_hnd_t;
typedef void* qse_pcp_pid_t;
# define QSE_PCP_HND_NIL ((qse_pcp_hnd_t)QSE_NULL)
# define QSE_PCP_PID_NIL ((qse_pcp_pid_t)QSE_NULL)
#else
typedef int qse_pcp_hnd_t;
typedef int qse_pcp_pid_t;
# define QSE_PCP_HND_NIL ((qse_pcp_hnd_t)-1)
# define QSE_PCP_PID_NIL ((qse_pcp_hnd_t)-1)
#endif
typedef struct qse_pcp_t qse_pcp_t;
typedef struct qse_pcp_pip_t qse_pcp_pip_t;
struct qse_pcp_pip_t
{
qse_pcp_hnd_t handle;
qse_tio_t* tio;
qse_pcp_t* self;
};
struct qse_pcp_t
{
QSE_DEFINE_STD_FIELDS(pcp)
qse_pcp_err_t errnum;
qse_pcp_pid_t child;
qse_pcp_pip_t pip[3];
};
#define QSE_PCP_MMGR(pcp) ((pcp)->mmgr)
#define QSE_PCP_XTN(pcp) ((void*)(((qse_pcp_t*)pcp) + 1))
#define QSE_PCP_HANDLE(pcp,hid) ((pcp)->pip[3].handle)
#define QSE_PCP_CHILD(pcp) ((pcp)->child)
#define QSE_PCP_ERRNUM(pcp) ((pcp)->errnum)
#ifdef __cplusplus
extern "C" {
#endif
QSE_DEFINE_STD_FUNCTIONS (pcp)
/****f* qse.cmn.pcp/qse_pcp_open
* NAME
* qse_pcp_open - open pipes to a child process
*
* DESCRIPTION
* QSE_PCP_SHELL drives the funcpcpn to execute the command via /bin/sh.
* If flags is clear of QSE_PCP_SHELL, you should pass the full program path.
*
* SYNOPSIS
*/
qse_pcp_t* qse_pcp_open (
qse_mmgr_t* mmgr,
qse_size_t ext,
const qse_char_t* cmd,
int flags
);
/******/
/****f* qse.cmn.pcp/qse_pcp_close
* NAME
* qse_pcp_close - close pipes to a child process
*
* SYNOPSIS
*/
void qse_pcp_close (
qse_pcp_t* pcp
);
/******/
/****f* qse.cmn/pcp/qse_pcp_init
* NAME
* qse_pcp_init - initialize pipes to a child process
*
* SYNOPSIS
*/
qse_pcp_t* qse_pcp_init (
qse_pcp_t* pcp,
qse_mmgr_t* mmgr,
const qse_char_t* path,
int flags
);
/******/
/****f* qse.cmn/pcp/qse_pcp_fini
* NAME
* qse_pcp_fini - finalize pipes to a child process
*
* SYNOPSIS
*/
void qse_pcp_fini (
qse_pcp_t* pcp
);
/******/
/****f* qse.cmn.pcp/qse_pcp_geterrnum
* NAME
* qse_pcp_geterrnum - get an error code
*
* SYNOPSIS
*/
qse_pcp_err_t qse_pcp_geterrnum (qse_pcp_t* pcp);
/******/
/****f* qse.cmn.pcp/qse_pcp_geterrstr
* NAME
* qse_pcp_geterrstr - transllate an error code to a string
*
* DESCRIPTION
* The qse_pcp_geterrstr() funcpcpn returns the pointer to a constant string
* describing the last error occurred.
*
* SYNOPSIS
*/
const qse_char_t* qse_pcp_geterrstr (qse_pcp_t* pcp);
/******/
/****f* qse.cmn.pcp/qse_pcp_gethandle
* NAME
* qse_pcp_gethandle - get native handle
*
* SYNOPSIS
*/
qse_pcp_hnd_t qse_pcp_gethandle (
qse_pcp_t* pcp,
qse_pcp_hid_t hid
);
/******/
/****f* qse.cmn.pcp/qse_pcp_getchild
* NAME
* qse_pcp_getchild - get the PID of a child process
*
* SYNOPSIS
*/
qse_pcp_pid_t qse_pcp_getchild (
qse_pcp_t* pcp
);
/******/
/****f* qse.cmn.pcp/qse_pcp_read
* NAME
* qse_pcp_read - read data
*
* SYNOPSIS
*/
qse_ssize_t qse_pcp_read (
qse_pcp_t* pcp,
void* buf,
qse_size_t size,
qse_pcp_hid_t hid
);
/******/
/****f* qse.cmn.pcp/qse_pcp_write
* NAME
* qse_pcp_write - write data
*
* DESCRIPTION
* If the parameter 'size' is zero, qse_pcp_write() closes the the writing
* stream causing the child process reach the end of the stream.
*
* SYNOPSIS
*/
qse_ssize_t qse_pcp_write (
qse_pcp_t* pcp,
const void* data,
qse_size_t size,
qse_pcp_hid_t hid
);
/******/
/****f* qse.cmn.pcp/qse_pcp_flush
* NAME
* qse_pcp_flush - flush data
*
* SYNOPSIS
*/
qse_ssize_t qse_pcp_flush (
qse_pcp_t* pcp,
qse_pcp_hid_t hid
);
/*****/
/****f* qse.cmn.pcp/qse_pcp_end
* NAME
* qse_pcp_end - close native handle
*
* SYNOPSIS
*/
void qse_pcp_end (
qse_pcp_t* pcp,
qse_pcp_hid_t hid
);
/******/
/****f* qse.cmn.pcp/qse_pcp_wait
* NAME
* qse_pcp_wait - wait for a child process
*
* DESCRIPTION
* QSE_PCP_IGNINTR causes the function to retry when the underlying system
* call is interrupted.
* When you specify QSE_PCP_NOHANG, the return value of 256 indicates that the
* child process has not terminated. If the flag is not specified, 256 will
* never be returned.
*
* RETURN
* -1 on error, 256 if the child is alive and QSE_PCP_NOHANG is specified,
* a number between 0 and 255 inclusive if the child process ends normally,
* 256 + signal number if the child process is terminated by a signal.
*
* SYNOPSIS
*/
int qse_pcp_wait (
qse_pcp_t* pcp,
int flags
);
/******/
/****f* qse.cmn.pcp/qse_pcp_kill
* NAME
* qse_pcp_kill - terminate the child process
*
* NOTES
* You should know the danger of calling this function as the function can
* kill a process that is not your child process if it has terminated but
* there is a new process with the same process handle.
*
* SYNOPSIS
*/
int qse_pcp_kill (
qse_pcp_t* pcp
);
/******/
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,323 +0,0 @@
/*
* $Id$
*
Copyright 2006-2008 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitapions under the License.
*/
#ifndef _QSE_CMN_PIO_H_
#define _QSE_CMN_PIO_H_
#include <qse/types.h>
#include <qse/macros.h>
#include <qse/cmn/tio.h>
enum qse_pio_open_flag_t
{
QSE_PIO_WRITEIN = (1 << 0),
QSE_PIO_READOUT = (1 << 1),
QSE_PIO_READERR = (1 << 2),
QSE_PIO_ERRTOOUT = (1 << 3),
QSE_PIO_OUTTOERR = (1 << 4),
QSE_PIO_INTONUL = (1 << 5),
QSE_PIO_ERRTONUL = (1 << 6),
QSE_PIO_OUTTONUL = (1 << 7),
QSE_PIO_DROPIN = (1 << 8),
QSE_PIO_DROPOUT = (1 << 9),
QSE_PIO_DROPERR = (1 << 10),
/* invoke the command through a default system shell */
QSE_PIO_SHELL = (1 << 11),
/* enable ase_char_t based IO */
QSE_PIO_TEXT = (1 << 12)
};
enum qse_pio_wait_flag_t
{
QSE_PIO_NOHANG = (1 << 0),
QSE_PIO_IGNINTR = (1 << 1)
};
enum qse_pio_hid_t
{
QSE_PIO_IN = 0,
QSE_PIO_OUT = 1,
QSE_PIO_ERR = 2
};
enum qse_pio_err_t
{
QSE_PIO_ENOERR = 0,
QSE_PIO_ENOMEM, /* out of memory */
QSE_PIO_ENOHND, /* no handle available */
QSE_PIO_ECHILD, /* the child is not valid */
QSE_PIO_EINTR, /* interrupted */
QSE_PIO_ESYSCALL /* system call error */
};
typedef enum qse_pio_hid_t qse_pio_hid_t;
typedef enum qse_pio_err_t qse_pio_err_t;
#ifdef _WIN32
/* <winnt.h> => typedef PVOID HANDLE; */
typedef void* qse_pio_hnd_t;
typedef void* qse_pio_pid_t;
# define QSE_PIO_HND_NIL ((qse_pio_hnd_t)QSE_NULL)
# define QSE_PIO_PID_NIL ((qse_pio_pid_t)QSE_NULL)
#else
typedef int qse_pio_hnd_t;
typedef int qse_pio_pid_t;
# define QSE_PIO_HND_NIL ((qse_pio_hnd_t)-1)
# define QSE_PIO_PID_NIL ((qse_pio_hnd_t)-1)
#endif
typedef struct qse_pio_t qse_pio_t;
struct qse_pio_t
{
qse_mmgr_t* mmgr;
qse_pio_err_t errnum;
qse_pio_pid_t child;
qse_pio_hnd_t handle[3];
qse_tio_t* tio[3];
};
#define QSE_PIO_MMGR(pio) ((pio)->mmgr)
#define QSE_PIO_XTN(pio) ((void*)(((qse_pio_t*)pio) + 1))
#define QSE_PIO_HANDLE(pio,hid) ((pio)->handle[hid])
#define QSE_PIO_CHILD(pio) ((pio)->child)
#ifdef __cplusplus
extern "C" {
#endif
/****f* qse.cmn.pio/qse_pio_open
* NAME
* qse_pio_open - open pipes to a child process
*
* DESCRIPTION
* QSE_PIO_SHELL drives the funcpion to execute the command via /bin/sh.
* If flags is clear of QSE_PIO_SHELL, you should pass the full program path.
*
* SYNOPSIS
*/
qse_pio_t* qse_pio_open (
qse_mmgr_t* mmgr,
qse_size_t ext,
const qse_char_t* cmd,
int flags
);
/******/
/****f* qse.cmn.pio/qse_pio_close
* NAME
* qse_pio_close - close pipes to a child process
*
* SYNOPSIS
*/
void qse_pio_close (
qse_pio_t* pio
);
/******/
/****f* qse.cmn/pio/qse_pio_init
* NAME
* qse_pio_init - initialize pipes to a child process
*
* SYNOPSIS
*/
qse_pio_t* qse_pio_init (
qse_pio_t* pio,
qse_mmgr_t* mmgr,
const qse_char_t* path,
int flags
);
/******/
/****f* qse.cmn/pio/qse_pio_fini
* NAME
* qse_pio_fini - finalize pipes to a child process
*
* SYNOPSIS
*/
void qse_pio_fini (
qse_pio_t* pio
);
/******/
void* qse_pio_getxtn (
qse_pio_t* pio
);
qse_mmgr_t* qse_pio_getmmgr (
qse_pio_t* pio
);
void qse_pio_setmmgr (
qse_pio_t* pio,
qse_mmgr_t* mmgr
);
/* TODO:
QSE_OBJECT_DEFINE_BASIC_MACROS(pio)
QSE_OBJEcT_DEFINE_BASIC_FIELDS(pio)
QSE_OBJECT_DEFINE_BASIC_FUNCTION(pio)
QSE_OBJECT_IMPLEMENT_BASIC_FUNCTION(pio)
*/
/****f* qse.cmn.pio/qse_pio_geterrnum
* NAME
* qse_pio_geterrnum - get an error code
*
* SYNOPSIS
*/
qse_pio_err_t qse_pio_geterrnum (qse_pio_t* pio);
/******/
/****f* qse.cmn.pio/qse_pio_geterrstr
* NAME
* qse_pio_geterrstr - transllate an error code to a string
*
* DESCRIPTION
* The qse_pio_geterrstr() funcpion returns the pointer to a constant string
* describing the last error occurred.
*
* SYNOPSIS
*/
const qse_char_t* qse_pio_geterrstr (qse_pio_t* pio);
/******/
/****f* qse.cmn.pio/qse_pio_gethandle
* NAME
* qse_pio_gethandle - get native handle
*
* SYNOPSIS
*/
qse_pio_hnd_t qse_pio_gethandle (
qse_pio_t* pio,
qse_pio_hid_t hid
);
/******/
/****f* qse.cmn.pio/qse_pio_getchild
* NAME
* qse_pio_getchild - get the PID of a child process
*
* SYNOPSIS
*/
qse_pio_pid_t qse_pio_getchild (
qse_pio_t* pio
);
/******/
/****f* qse.cmn.pio/qse_pio_read
* NAME
* qse_pio_read - read data
*
* SYNOPSIS
*/
qse_ssize_t qse_pio_read (
qse_pio_t* pio,
void* buf,
qse_size_t size,
qse_pio_hid_t hid
);
/******/
/****f* qse.cmn.pio/qse_pio_write
* NAME
* qse_pio_write - write data
*
* DESCRIPTION
* If the parameter 'size' is zero, qse_pio_write() closes the the writing
* stream causing the child process reach the end of the stream.
*
* SYNOPSIS
*/
qse_ssize_t qse_pio_write (
qse_pio_t* pio,
const void* data,
qse_size_t size,
qse_pio_hid_t hid
);
/******/
void qse_pio_flush (
qse_pio_t* pio,
qse_pio_hid_t hid
);
/****f* qse.cmn.pio/qse_pio_end
* NAME
* qse_pio_end
*
* SYNOPSIS
*/
void qse_pio_end (
qse_pio_t* pio,
qse_pio_hid_t hid
);
/******/
/****f* qse.cmn.pio/qse_pio_wait
* NAME
* qse_pio_wait - wait for a child process
*
* DESCRIPTION
* QSE_PIO_IGNINTR causes the function to retry when the underlying system
* call is interrupted.
* When you specify QSE_PIO_NOHANG, the return value of 256 indicates that the
* child process has not terminated. If the flag is not specified, 256 will
* never be returned.
*
* RETURN
* -1 on error, 256 if the child is still alive and QSE_PIO_NOHANG is specified,
* a number between 0 and 255 inclusive if the child process terminates normally,
* 256 + signal number if the child process is terminated by a signal.
*
* SYNOPSIS
*/
int qse_pio_wait (
qse_pio_t* pio,
int flags
);
/******/
/****f* qse.cmn.pio/qse_pio_kill
* NAME
* qse_pio_kill - terminate the child process
*
* NOTES
* You should know the danger of calling this function as the function can
* kill a process that is not your child process if it has terminated but
* there is a new process with the same process handle.
*
* SYNOPSIS
*/
int qse_pio_kill (
qse_pio_t* pio
);
/******/
#ifdef __cplusplus
}
#endif
#endif

View File

@ -85,7 +85,7 @@ typedef qse_ssize_t (*qse_tio_io_t) (
struct qse_tio_t
{
qse_mmgr_t* mmgr;
QSE_DEFINE_STD_FIELDS (tio)
qse_tio_err_t errnum;
/* io functions */
@ -108,6 +108,8 @@ struct qse_tio_t
extern "C" {
#endif
QSE_DEFINE_STD_FUNCTIONS (tio)
/*
* FUNCTION: qse_tio_open
*/
@ -132,26 +134,15 @@ int qse_tio_fini (
qse_tio_t* tio
);
void* qse_tio_getxtn (
qse_tio_t* tio
);
qse_mmgr_t* qse_tio_getmmgr (
qse_tio_t* tio
);
void qse_tio_setmmgr (
qse_tio_t* tio,
qse_mmgr_t* mmgr
);
/****f* qse.cmn.tio/qse_tio_geterrnum
* NAME
* qse_tio_geterrnum - get an error code
*
* SYNOPSIS
*/
qse_tio_err_t qse_tio_geterrnum (qse_tio_t* tio);
qse_tio_err_t qse_tio_geterrnum (
qse_tio_t* tio
);
/******/
/*
@ -164,7 +155,9 @@ qse_tio_err_t qse_tio_geterrnum (qse_tio_t* tio);
* RETURNS:
* A pointer to a constant string describing the last error occurred
*/
const qse_char_t* qse_tio_geterrstr (qse_tio_t* tio);
const qse_char_t* qse_tio_geterrstr (
qse_tio_t* tio
);
/*
* FUNCTION: qse_tio_attachin

View File

@ -154,4 +154,26 @@
# define QSE_END_NAMESPACE2(y,x) }}
#endif
#define QSE_DEFINE_STD_FIELDS(name) \
qse_mmgr_t* mmgr;
#define QSE_DEFINE_STD_FUNCTIONS(name) \
qse_##name##_t qse_##name##_setmmgr (qse_##name##_t* name, qse_mmgr_t* mmgr); \
qse_mmgr_t* qse_##name##_getmmgr (qse_##name##_t* name); \
void* qse_##name##_getxtn (qse_##name##_t* name);
#define QSE_IMPLEMENT_STD_FUNCTIONS(name) \
qse_##name##_t qse_##name##_setmmgr (qse_##name##_t* name, qse_mmgr_t* mmgr) \
{ \
name->mmgr = mmgr; \
} \
qse_mmgr_t* qse_##name##_getmmgr (qse_##name##_t* name) \
{ \
return name->mmgr; \
} \
void* qse_##name##_getxtn (qse_##name##_t* name) \
{ \
return (void*)(name + 1); \
}
#endif