--- l2_ch_pipe.c 2001/09/12 09:42:34 1.8
+++ l2_ch_pipe.c 2001/09/14 11:13:32 1.9
@@ -24,53 +24,99 @@
** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
**
-** l2_ch_pipe.c: program pipe channel implementation
+** l2_ch_pipe.c: pipe channel implementation
*/
#include "l2.h"
+#include <unistd.h>
+
+/* declare private channel configuration */
+typedef struct {
+ int iPipe; /* pipe filedescriptor used for writing only */
+} l2_ch_pipe_t;
+
+/* create channel */
static l2_result_t hook_create(l2_context_t *ctx, l2_channel_t *ch)
{
+ l2_ch_pipe_t *cfg;
+
+ /* allocate private channel configuration */
+ if ((cfg = (l2_ch_pipe_t *)malloc(sizeof(l2_ch_pipe_t))) == NULL)
+ return L2_ERR_ARG;
+
+ /* initialize configuration with reasonable defaults */
+ cfg->iPipe = -1;
+
+ /* link private channel configuration into channel context */
+ ctx->vp = cfg;
+
return L2_OK;
}
+/* configure channel */
static l2_result_t hook_configure(l2_context_t *ctx, l2_channel_t *ch, const char *fmt, va_list ap)
{
- return L2_OK;
-}
+ l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
+ l2_param_t pa[2];
+ l2_result_t rv;
-static l2_result_t hook_open(l2_context_t *ctx, l2_channel_t *ch)
-{
- return L2_OK;
-}
+ /* feed and call generic parameter parsing engine */
+ L2_PARAM_SET(pa[0], pipe, INT, &cfg->iPipe);
+ L2_PARAM_END(pa[1]);
+ rv = l2_util_setparams(pa, fmt, ap);
-static l2_result_t hook_write(l2_context_t *ctx, l2_channel_t *ch, l2_level_t level, const char *buf, size_t buf_size)
-{
- return L2_OK;
+ /* validate the pipe fd */
+ if (cfg->iPipe == -1)
+ return L2_ERR_ARG;
+
+ return rv;
}
-static l2_result_t hook_flush(l2_context_t *ctx, l2_channel_t *ch)
+/* write to channel */
+static l2_result_t hook_write(l2_context_t *ctx, l2_channel_t *ch,
+ l2_level_t level, const char *buf, size_t buf_size)
{
+ l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
+
+ /* write message to channel pipe */
+ if (write(cfg->iPipe, buf, buf_size) == -1)
+ return L2_ERR_SYS;
+
return L2_OK;
}
+/* close channel */
static l2_result_t hook_close(l2_context_t *ctx, l2_channel_t *ch)
{
+ l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
+
+ /* close channel pipe */
+ close(cfg->iPipe);
+ cfg->iPipe = -1;
+
return L2_OK;
}
+/* destroy channel */
static l2_result_t hook_destroy(l2_context_t *ctx, l2_channel_t *ch)
{
+ l2_ch_pipe_t *cfg = (l2_ch_pipe_t *)ctx->vp;
+
+ /* destroy channel configuration */
+ free(cfg);
+
return L2_OK;
}
+/* exported channel handler structure */
l2_handler_t l2_handler_pipe = {
L2_CHANNEL_OUTPUT,
hook_create,
hook_configure,
- hook_open,
+ NULL,
hook_write,
- hook_flush,
+ NULL,
hook_close,
hook_destroy
};
|