qse/ase/awk/extio.c

798 lines
17 KiB
C
Raw Normal View History

2006-06-16 07:37:27 +00:00
/*
2006-10-09 14:37:42 +00:00
* $Id: extio.c,v 1.50 2006-10-09 14:37:14 bacon Exp $
2006-06-16 07:37:27 +00:00
*/
#include <xp/awk/awk_i.h>
2006-06-29 14:38:01 +00:00
enum
{
2006-07-14 04:19:22 +00:00
__MASK_READ = 0x0100,
__MASK_WRITE = 0x0200,
__MASK_RDWR = 0x0400,
2006-08-23 15:42:16 +00:00
__MASK_CLEAR = 0x00FF
2006-06-29 14:38:01 +00:00
};
2006-07-28 10:34:22 +00:00
static int __in_type_map[] =
{
/* the order should match the order of the
* XP_AWK_IN_XXX values in tree.h */
XP_AWK_EXTIO_PIPE,
XP_AWK_EXTIO_COPROC,
XP_AWK_EXTIO_FILE,
XP_AWK_EXTIO_CONSOLE
};
static int __in_mode_map[] =
{
/* the order should match the order of the
* XP_AWK_IN_XXX values in tree.h */
XP_AWK_IO_PIPE_READ,
0,
XP_AWK_IO_FILE_READ,
XP_AWK_IO_CONSOLE_READ
};
static int __in_mask_map[] =
{
__MASK_READ,
__MASK_RDWR,
__MASK_READ,
__MASK_READ
};
static int __out_type_map[] =
{
/* the order should match the order of the
* XP_AWK_OUT_XXX values in tree.h */
XP_AWK_EXTIO_PIPE,
XP_AWK_EXTIO_COPROC,
XP_AWK_EXTIO_FILE,
XP_AWK_EXTIO_FILE,
XP_AWK_EXTIO_CONSOLE
};
static int __out_mode_map[] =
{
/* the order should match the order of the
* XP_AWK_OUT_XXX values in tree.h */
XP_AWK_IO_PIPE_WRITE,
0,
XP_AWK_IO_FILE_WRITE,
XP_AWK_IO_FILE_APPEND,
XP_AWK_IO_CONSOLE_WRITE
};
static int __out_mask_map[] =
{
__MASK_WRITE,
__MASK_RDWR,
__MASK_WRITE,
__MASK_WRITE,
__MASK_WRITE
};
2006-07-14 04:19:22 +00:00
2006-06-21 13:52:15 +00:00
int xp_awk_readextio (
2006-06-28 14:19:01 +00:00
xp_awk_run_t* run, int in_type,
2006-08-31 16:00:20 +00:00
const xp_char_t* name, xp_awk_str_t* buf)
2006-06-16 07:37:27 +00:00
{
2006-08-10 16:02:15 +00:00
xp_awk_extio_t* p = run->extio.chain;
2006-06-28 10:40:24 +00:00
xp_awk_io_t handler;
2006-08-24 03:30:32 +00:00
int extio_type, extio_mode, extio_mask, n, ret;
xp_awk_val_t* rs;
xp_char_t* rs_ptr;
xp_size_t rs_len;
2006-08-25 03:31:09 +00:00
xp_size_t line_len = 0;
2006-06-29 14:38:01 +00:00
2006-06-28 10:40:24 +00:00
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_type_map));
2006-07-28 10:34:22 +00:00
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mode_map));
2006-06-29 14:38:01 +00:00
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mask_map));
2006-06-28 10:40:24 +00:00
2006-07-28 10:34:22 +00:00
/* translate the in_type into the relevant extio type and mode */
2006-06-28 10:40:24 +00:00
extio_type = __in_type_map[in_type];
2006-07-28 10:34:22 +00:00
extio_mode = __in_mode_map[in_type];
2006-06-29 14:38:01 +00:00
extio_mask = __in_mask_map[in_type];
2006-06-28 10:40:24 +00:00
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-06-21 15:37:51 +00:00
if (handler == XP_NULL)
{
/* no io handler provided */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOIMPL; /* TODO: change the error code */
2006-06-21 15:37:51 +00:00
return -1;
}
2006-06-16 07:37:27 +00:00
while (p != XP_NULL)
{
2006-06-29 14:38:01 +00:00
if (p->type == (extio_type | extio_mask) &&
2006-10-09 14:37:42 +00:00
xp_awk_strcmp (p->name,name) == 0) break;
2006-06-16 07:37:27 +00:00
p = p->next;
}
if (p == XP_NULL)
{
2006-08-31 14:52:12 +00:00
p = (xp_awk_extio_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_extio_t));
2006-06-16 07:37:27 +00:00
if (p == XP_NULL)
{
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-06-16 07:37:27 +00:00
return -1;
}
2006-09-01 03:44:51 +00:00
p->name = xp_awk_strdup (run->awk, name);
2006-06-16 07:37:27 +00:00
if (p->name == XP_NULL)
{
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-06-16 07:37:27 +00:00
return -1;
}
2006-06-29 14:38:01 +00:00
p->type = (extio_type | extio_mask);
2006-07-28 10:34:22 +00:00
p->mode = extio_mode;
2006-06-16 07:37:27 +00:00
p->handle = XP_NULL;
2006-06-28 14:19:01 +00:00
2006-08-25 03:31:09 +00:00
p->in.buf[0] = XP_T('\0');
2006-06-28 14:19:01 +00:00
p->in.pos = 0;
p->in.len = 0;
p->in.eof = xp_false;
2006-06-19 09:10:57 +00:00
p->next = XP_NULL;
2006-06-16 07:37:27 +00:00
2006-07-28 10:34:22 +00:00
n = handler (XP_AWK_IO_OPEN, p, XP_NULL, 0);
if (n == -1)
2006-06-16 07:37:27 +00:00
{
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p->name);
XP_AWK_FREE (run->awk, p);
2006-06-19 09:10:57 +00:00
2006-07-30 15:53:42 +00:00
/* TODO: use meaningful error code */
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_ERRNO,
xp_awk_val_one) == -1) return -1;
2006-07-30 15:53:42 +00:00
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-06-16 07:37:27 +00:00
return -1;
}
2006-06-19 04:38:51 +00:00
2006-06-21 15:37:51 +00:00
/* chain it */
2006-08-10 16:02:15 +00:00
p->next = run->extio.chain;
run->extio.chain = p;
2006-07-28 10:34:22 +00:00
/* n == 0 indicates that it has reached the end of input.
* the user io handler can return 0 for the open request
* if it doesn't have any files to open. One advantage
* of doing this would be that you can skip the entire
* pattern-block matching and exeuction. */
if (n == 0) return 0;
2006-06-19 09:10:57 +00:00
}
2006-08-24 03:30:32 +00:00
/* ready to read a line */
2006-08-31 16:00:20 +00:00
xp_awk_str_clear (buf);
2006-06-19 04:38:51 +00:00
2006-08-24 03:30:32 +00:00
/* get the record separator */
rs = xp_awk_getglobal (run, XP_AWK_GLOBAL_RS);
xp_awk_refupval (rs);
if (rs->type == XP_AWK_VAL_NIL)
{
rs_ptr = XP_NULL;
rs_len = 0;
}
else if (rs->type == XP_AWK_VAL_STR)
{
rs_ptr = ((xp_awk_val_str_t*)rs)->buf;
rs_len = ((xp_awk_val_str_t*)rs)->len;
}
else
{
2006-08-31 15:39:14 +00:00
rs_ptr = xp_awk_valtostr (run, rs, xp_true, XP_NULL, &rs_len);
2006-08-24 03:30:32 +00:00
if (rs_ptr == XP_NULL)
{
xp_awk_refdownval (run, rs);
return -1;
}
}
ret = 1;
/* call the io handler */
2006-06-28 14:19:01 +00:00
while (1)
2006-06-19 04:38:51 +00:00
{
2006-06-28 14:19:01 +00:00
xp_char_t c;
if (p->in.pos >= p->in.len)
{
xp_ssize_t n;
if (p->in.eof)
{
2006-08-31 16:00:20 +00:00
if (XP_AWK_STR_LEN(buf) == 0) ret = 0;
2006-06-28 14:19:01 +00:00
break;
}
2006-07-28 10:34:22 +00:00
n = handler (XP_AWK_IO_READ, p,
p->in.buf, xp_countof(p->in.buf));
2006-06-28 14:19:01 +00:00
if (n == -1)
{
/* handler error. getline should return -1 */
2006-07-30 15:53:42 +00:00
/* TODO: use meaningful error code */
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_ERRNO,
xp_awk_val_one) == -1)
{
ret = -1;
}
else
{
run->errnum = XP_AWK_EIOHANDLER;
ret = -1;
}
2006-08-24 03:30:32 +00:00
break;
2006-06-28 14:19:01 +00:00
}
if (n == 0)
{
p->in.eof = xp_true;
2006-08-31 16:00:20 +00:00
if (XP_AWK_STR_LEN(buf) == 0) ret = 0;
2006-06-28 14:19:01 +00:00
break;
}
p->in.len = n;
p->in.pos = 0;
}
c = p->in.buf[p->in.pos++];
2006-08-24 03:30:32 +00:00
if (rs_ptr == XP_NULL)
{
/* separate by a new line */
2006-08-30 07:15:14 +00:00
/* TODO: handle different line terminator like \r\n */
if (c == XP_T('\n')) break;
2006-08-24 03:30:32 +00:00
}
else if (rs_len == 0)
{
/* separate by a blank line */
2006-08-25 03:31:09 +00:00
/* TODO: handle different line terminator like \r\n */
if (line_len == 0 && c == XP_T('\n'))
{
2006-08-31 16:00:20 +00:00
if (XP_AWK_STR_LEN(buf) <= 0)
2006-08-25 03:31:09 +00:00
{
/* if the record is empty when a blank
* line is encountered, the line
* terminator should not be added to
* the record */
continue;
}
/* when a blank line is encountered,
* it needs to snip off the line
* terminator of the previous line */
/* TODO: handle different line terminator like \r\n */
2006-08-31 16:00:20 +00:00
XP_AWK_STR_LEN(buf) -= 1;
2006-08-25 03:31:09 +00:00
break;
}
2006-08-24 03:30:32 +00:00
}
else if (rs_len == 1)
{
if (c == rs_ptr[0]) break;
}
else
{
2006-08-30 14:48:09 +00:00
const xp_char_t* match_ptr;
2006-08-25 15:52:47 +00:00
xp_size_t match_len;
2006-09-22 14:05:30 +00:00
xp_assert (run->global.rs != XP_NULL);
2006-08-29 15:01:45 +00:00
/* TODO: safematchrex */
2006-09-01 03:44:51 +00:00
n = xp_awk_matchrex (
2006-09-22 14:05:30 +00:00
run->awk, run->global.rs,
((run->global.ignorecase)? XP_AWK_REX_IGNORECASE: 0),
2006-08-31 16:00:20 +00:00
XP_AWK_STR_BUF(buf), XP_AWK_STR_LEN(buf),
2006-08-29 15:01:45 +00:00
&match_ptr, &match_len, &run->errnum);
2006-08-25 15:52:47 +00:00
if (n == -1)
{
ret = -1;
break;
}
if (n == 1)
{
2006-08-29 15:01:45 +00:00
/* the match should be found at the end of
* the current buffer */
xp_assert (
2006-08-31 16:00:20 +00:00
XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf) ==
2006-08-29 15:01:45 +00:00
match_ptr + match_len);
2006-08-31 16:00:20 +00:00
XP_AWK_STR_LEN(buf) -= match_len;
2006-08-29 15:01:45 +00:00
break;
2006-08-25 15:52:47 +00:00
}
2006-08-24 03:30:32 +00:00
}
2006-06-19 04:38:51 +00:00
2006-08-31 16:00:20 +00:00
if (xp_awk_str_ccat (buf, c) == (xp_size_t)-1)
2006-06-28 14:19:01 +00:00
{
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-08-24 03:30:32 +00:00
ret = -1;
break;
2006-06-28 14:19:01 +00:00
}
2006-08-25 03:31:09 +00:00
2006-08-30 07:15:14 +00:00
/* TODO: handle different line terminator like \r\n */
2006-08-25 03:31:09 +00:00
if (c == XP_T('\n')) line_len = 0;
else line_len = line_len + 1;
2006-06-16 07:37:27 +00:00
}
2006-08-31 14:52:12 +00:00
if (rs_ptr != XP_NULL && rs->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, rs_ptr);
2006-08-24 03:30:32 +00:00
xp_awk_refdownval (run, rs);
2006-08-30 07:15:14 +00:00
/* increment NR */
if (ret != -1)
{
xp_awk_val_t* nr;
xp_long_t lv;
xp_real_t rv;
nr = xp_awk_getglobal (run, XP_AWK_GLOBAL_NR);
xp_awk_refupval (nr);
2006-09-01 06:23:58 +00:00
n = xp_awk_valtonum (run, nr, &lv, &rv);
2006-08-30 07:15:14 +00:00
xp_awk_refdownval (run, nr);
2006-09-01 06:23:58 +00:00
if (n == -1) ret = -1;
2006-08-30 07:15:14 +00:00
else
{
if (n == 1) lv = (xp_long_t)rv;
nr = xp_awk_makeintval (run, lv + 1);
if (nr == XP_NULL)
{
run->errnum = XP_AWK_ENOMEM;
ret = -1;
}
else
{
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_NR, nr) == -1) ret = -1;
}
}
}
2006-08-24 03:30:32 +00:00
return ret;
2006-06-16 07:37:27 +00:00
}
2006-09-29 11:18:13 +00:00
int xp_awk_writeextio_val (
2006-06-28 03:44:40 +00:00
xp_awk_run_t* run, int out_type,
2006-08-27 15:29:21 +00:00
const xp_char_t* name, xp_awk_val_t* v)
2006-08-03 09:54:16 +00:00
{
2006-09-29 11:18:13 +00:00
xp_char_t* str;
xp_size_t len;
int n;
2006-08-03 09:54:16 +00:00
2006-09-29 11:18:13 +00:00
if (v->type == XP_AWK_VAL_STR)
{
str = ((xp_awk_val_str_t*)v)->buf;
len = ((xp_awk_val_str_t*)v)->len;
}
else
{
str = xp_awk_valtostr (run, v, xp_true, XP_NULL, &len);
if (str == XP_NULL) return -1;
}
n = xp_awk_writeextio_str (run, out_type, name, str, len);
if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str);
return n;
2006-08-03 09:54:16 +00:00
}
2006-09-29 11:18:13 +00:00
int xp_awk_writeextio_str (
2006-08-03 09:54:16 +00:00
xp_awk_run_t* run, int out_type,
2006-09-29 11:18:13 +00:00
const xp_char_t* name, xp_char_t* str, xp_size_t len)
2006-06-22 14:15:02 +00:00
{
2006-08-10 16:02:15 +00:00
xp_awk_extio_t* p = run->extio.chain;
2006-06-28 03:44:40 +00:00
xp_awk_io_t handler;
2006-07-28 10:34:22 +00:00
int extio_type, extio_mode, extio_mask, n;
2006-06-29 14:38:01 +00:00
2006-06-28 03:44:40 +00:00
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_type_map));
2006-07-28 10:34:22 +00:00
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mode_map));
2006-06-29 14:38:01 +00:00
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mask_map));
2006-06-28 03:44:40 +00:00
2006-07-28 10:34:22 +00:00
/* translate the out_type into the relevant extio type and mode */
2006-06-28 03:44:40 +00:00
extio_type = __out_type_map[out_type];
2006-07-28 10:34:22 +00:00
extio_mode = __out_mode_map[out_type];
2006-06-29 14:38:01 +00:00
extio_mask = __out_mask_map[out_type];
2006-06-28 03:44:40 +00:00
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-06-22 14:15:02 +00:00
if (handler == XP_NULL)
{
/* no io handler provided */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOIMPL; /* TODO: change the error code */
2006-06-22 14:15:02 +00:00
return -1;
}
2006-06-26 15:09:28 +00:00
/* look for the corresponding extio for name */
2006-06-22 14:15:02 +00:00
while (p != XP_NULL)
{
2006-06-28 08:56:59 +00:00
/* the file "1.tmp", in the following code snippets,
* would be opened by the first print statement, but not by
* the second print statement. this is because
* both XP_AWK_OUT_FILE and XP_AWK_OUT_FILE_APPEND are
* translated to XP_AWK_EXTIO_FILE and it is used to
* keep track of file handles..
*
* print "1111" >> "1.tmp"
* print "1111" > "1.tmp"
*/
2006-06-29 14:38:01 +00:00
if (p->type == (extio_type | extio_mask) &&
2006-09-01 06:23:58 +00:00
xp_awk_strcmp (p->name, name) == 0) break;
2006-06-22 14:15:02 +00:00
p = p->next;
}
2006-06-26 15:09:28 +00:00
/* if there is not corresponding extio for name, create one */
2006-06-22 14:15:02 +00:00
if (p == XP_NULL)
{
2006-08-31 14:52:12 +00:00
p = (xp_awk_extio_t*) XP_AWK_MALLOC (
run->awk, xp_sizeof(xp_awk_extio_t));
2006-06-22 14:15:02 +00:00
if (p == XP_NULL)
{
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-06-22 14:15:02 +00:00
return -1;
}
2006-09-01 03:44:51 +00:00
p->name = xp_awk_strdup (run->awk, name);
2006-06-22 14:15:02 +00:00
if (p->name == XP_NULL)
{
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p);
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_ENOMEM;
2006-06-22 14:15:02 +00:00
return -1;
}
2006-06-29 14:38:01 +00:00
p->type = (extio_type | extio_mask);
2006-07-28 10:34:22 +00:00
p->mode = extio_mode;
2006-06-22 14:15:02 +00:00
p->handle = XP_NULL;
p->next = XP_NULL;
2006-07-28 10:34:22 +00:00
n = handler (XP_AWK_IO_OPEN, p, XP_NULL, 0);
if (n == -1)
2006-06-22 14:15:02 +00:00
{
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p->name);
XP_AWK_FREE (run->awk, p);
2006-06-22 14:15:02 +00:00
2006-07-30 15:53:42 +00:00
/* TODO: use meaningful error code */
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_ERRNO,
xp_awk_val_one) == -1) return -1;
2006-07-30 15:53:42 +00:00
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-06-22 14:15:02 +00:00
return -1;
}
/* chain it */
2006-08-10 16:02:15 +00:00
p->next = run->extio.chain;
run->extio.chain = p;
2006-07-28 10:34:22 +00:00
/* read the comment in xp_awk_readextio */
if (n == 0) return 0;
2006-06-22 14:15:02 +00:00
}
2006-07-28 10:34:22 +00:00
/* TODO: if write handler returns less than the request, loop */
2006-08-03 09:54:16 +00:00
if (len > 0)
2006-07-28 10:34:22 +00:00
{
2006-08-03 09:54:16 +00:00
n = handler (XP_AWK_IO_WRITE, p, str, len);
2006-07-30 15:53:42 +00:00
2006-08-03 09:54:16 +00:00
if (n == -1)
{
/* TODO: use meaningful error code */
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_ERRNO,
xp_awk_val_one) == -1) return -1;
run->errnum = XP_AWK_EIOHANDLER;
2006-08-03 09:54:16 +00:00
return -1;
}
if (n == 0) return 0;
2006-06-22 14:15:02 +00:00
}
return 1;
}
2006-09-29 11:18:13 +00:00
2006-08-22 15:11:13 +00:00
int xp_awk_flushextio (
2006-08-27 15:29:21 +00:00
xp_awk_run_t* run, int out_type, const xp_char_t* name)
2006-08-22 15:11:13 +00:00
{
xp_awk_extio_t* p = run->extio.chain;
xp_awk_io_t handler;
int extio_type, extio_mode, extio_mask, n;
2006-08-23 15:42:16 +00:00
xp_bool_t ok = xp_false;
2006-08-22 15:11:13 +00:00
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_type_map));
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mode_map));
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mask_map));
/* translate the out_type into the relevant extio type and mode */
extio_type = __out_type_map[out_type];
extio_mode = __out_mode_map[out_type];
extio_mask = __out_mask_map[out_type];
handler = run->extio.handler[extio_type];
if (handler == XP_NULL)
{
/* no io handler provided */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOIMPL; /* TODO: change the error code */
2006-08-22 15:11:13 +00:00
return -1;
}
/* look for the corresponding extio for name */
while (p != XP_NULL)
{
if (p->type == (extio_type | extio_mask) &&
2006-09-01 06:23:58 +00:00
(name == XP_NULL || xp_awk_strcmp (p->name, name) == 0))
2006-08-23 15:42:16 +00:00
{
n = handler (XP_AWK_IO_FLUSH, p, XP_NULL, 0);
2006-08-22 15:11:13 +00:00
2006-08-23 15:42:16 +00:00
if (n == -1)
{
/* TODO: use meaningful error code */
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_ERRNO,
xp_awk_val_one) == -1) return -1;
run->errnum = XP_AWK_EIOHANDLER;
2006-08-23 15:42:16 +00:00
return -1;
}
2006-08-22 15:11:13 +00:00
2006-08-23 15:42:16 +00:00
ok = xp_true;
}
2006-08-22 15:11:13 +00:00
2006-08-23 15:42:16 +00:00
p = p->next;
2006-08-22 15:11:13 +00:00
}
2006-08-23 15:42:16 +00:00
if (ok) return 0;
/* there is no corresponding extio for name */
/* TODO: use meaningful error code. but is this needed? */
2006-08-27 15:29:21 +00:00
if (xp_awk_setglobal (
run, XP_AWK_GLOBAL_ERRNO, xp_awk_val_one) == -1) return -1;
run->errnum = XP_AWK_ENOSUCHIO;
2006-08-23 15:42:16 +00:00
return -1;
2006-08-22 15:11:13 +00:00
}
2006-07-28 10:34:22 +00:00
int xp_awk_nextextio_read (
2006-08-27 15:29:21 +00:00
xp_awk_run_t* run, int in_type, const xp_char_t* name)
2006-07-28 10:34:22 +00:00
{
2006-08-10 16:02:15 +00:00
xp_awk_extio_t* p = run->extio.chain;
2006-07-28 10:34:22 +00:00
xp_awk_io_t handler;
int extio_type, extio_mode, extio_mask, n;
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_type_map));
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mode_map));
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mask_map));
/* translate the in_type into the relevant extio type and mode */
extio_type = __in_type_map[in_type];
extio_mode = __in_mode_map[in_type];
extio_mask = __in_mask_map[in_type];
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-07-28 10:34:22 +00:00
if (handler == XP_NULL)
{
/* no io handler provided */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOIMPL; /* TODO: change the error code */
2006-07-28 10:34:22 +00:00
return -1;
}
while (p != XP_NULL)
{
if (p->type == (extio_type | extio_mask) &&
2006-10-09 14:37:42 +00:00
xp_awk_strcmp (p->name,name) == 0) break;
2006-07-28 10:34:22 +00:00
p = p->next;
}
if (p == XP_NULL)
{
/* something is totally wrong */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EINTERNAL;
2006-07-28 10:34:22 +00:00
return -1;
}
n = handler (XP_AWK_IO_NEXT, p, XP_NULL, 0);
if (n == -1)
{
/* TODO: is this errnum correct? */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-07-28 10:34:22 +00:00
return -1;
}
return n;
}
2006-08-02 11:26:11 +00:00
int xp_awk_closeextio_read (
2006-08-27 15:29:21 +00:00
xp_awk_run_t* run, int in_type, const xp_char_t* name)
2006-08-02 11:26:11 +00:00
{
2006-08-10 16:02:15 +00:00
xp_awk_extio_t* p = run->extio.chain, * px = XP_NULL;
2006-08-02 11:26:11 +00:00
xp_awk_io_t handler;
int extio_type, extio_mode, extio_mask;
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_type_map));
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mode_map));
xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mask_map));
/* translate the in_type into the relevant extio type and mode */
extio_type = __in_type_map[in_type];
extio_mode = __in_mode_map[in_type];
extio_mask = __in_mask_map[in_type];
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-08-02 11:26:11 +00:00
if (handler == XP_NULL)
{
/* no io handler provided */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOIMPL; /* TODO: change the error code */
2006-08-02 11:26:11 +00:00
return -1;
}
while (p != XP_NULL)
{
if (p->type == (extio_type | extio_mask) &&
2006-09-01 06:23:58 +00:00
xp_awk_strcmp (p->name, name) == 0)
2006-08-02 11:26:11 +00:00
{
xp_awk_io_t handler;
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[p->type & __MASK_CLEAR];
2006-08-02 11:26:11 +00:00
if (handler != XP_NULL)
{
if (handler (XP_AWK_IO_CLOSE, p, XP_NULL, 0) == -1)
{
/* this is not a run-time error.*/
2006-08-23 15:42:16 +00:00
/* TODO: set ERRNO */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-08-02 11:26:11 +00:00
return -1;
}
}
if (px != XP_NULL) px->next = p->next;
2006-08-10 16:02:15 +00:00
else run->extio.chain = p->next;
2006-08-02 11:26:11 +00:00
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p->name);
XP_AWK_FREE (run->awk, p);
2006-08-02 11:26:11 +00:00
return 0;
}
px = p;
p = p->next;
}
/* this is not a run-time error */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-08-02 11:26:11 +00:00
return -1;
}
int xp_awk_closeextio_write (
2006-08-27 15:29:21 +00:00
xp_awk_run_t* run, int out_type, const xp_char_t* name)
2006-08-02 11:26:11 +00:00
{
2006-08-10 16:02:15 +00:00
xp_awk_extio_t* p = run->extio.chain, * px = XP_NULL;
2006-08-02 11:26:11 +00:00
xp_awk_io_t handler;
int extio_type, extio_mode, extio_mask;
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_type_map));
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mode_map));
xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mask_map));
/* translate the out_type into the relevant extio type and mode */
extio_type = __out_type_map[out_type];
extio_mode = __out_mode_map[out_type];
extio_mask = __out_mask_map[out_type];
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-08-02 11:26:11 +00:00
if (handler == XP_NULL)
{
/* no io handler provided */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOIMPL; /* TODO: change the error code */
2006-08-02 11:26:11 +00:00
return -1;
}
while (p != XP_NULL)
{
if (p->type == (extio_type | extio_mask) &&
2006-09-01 06:23:58 +00:00
xp_awk_strcmp (p->name, name) == 0)
2006-08-02 11:26:11 +00:00
{
xp_awk_io_t handler;
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[p->type & __MASK_CLEAR];
2006-08-02 11:26:11 +00:00
if (handler != XP_NULL)
{
if (handler (XP_AWK_IO_CLOSE, p, XP_NULL, 0) == -1)
{
/* this is not a run-time error.*/
2006-08-23 15:42:16 +00:00
/* TODO: set ERRNO */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-08-02 11:26:11 +00:00
return -1;
}
}
if (px != XP_NULL) px->next = p->next;
2006-08-10 16:02:15 +00:00
else run->extio.chain = p->next;
2006-08-02 11:26:11 +00:00
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p->name);
XP_AWK_FREE (run->awk, p);
2006-08-02 11:26:11 +00:00
return 0;
}
px = p;
p = p->next;
}
/* this is not a run-time error */
2006-08-23 15:42:16 +00:00
/* TODO: set ERRNO */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-08-02 11:26:11 +00:00
return -1;
}
2006-08-27 15:29:21 +00:00
int xp_awk_closeextio (xp_awk_run_t* run, const xp_char_t* name)
2006-06-16 07:37:27 +00:00
{
2006-08-10 16:02:15 +00:00
xp_awk_extio_t* p = run->extio.chain, * px = XP_NULL;
2006-06-16 07:37:27 +00:00
while (p != XP_NULL)
{
2006-06-21 15:37:51 +00:00
/* it handles the first that matches the given name
2006-06-21 13:52:15 +00:00
* regardless of the extio type */
2006-09-01 06:23:58 +00:00
if (xp_awk_strcmp (p->name, name) == 0)
2006-06-16 07:37:27 +00:00
{
2006-06-29 14:38:01 +00:00
xp_awk_io_t handler;
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[p->type & __MASK_CLEAR];
2006-07-14 04:19:22 +00:00
if (handler != XP_NULL)
2006-06-19 04:38:51 +00:00
{
2006-07-28 10:34:22 +00:00
if (handler (XP_AWK_IO_CLOSE, p, XP_NULL, 0) == -1)
2006-06-21 15:37:51 +00:00
{
/* this is not a run-time error.*/
2006-08-23 15:42:16 +00:00
/* TODO: set ERRNO */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-06-21 15:37:51 +00:00
return -1;
}
2006-06-19 04:38:51 +00:00
}
2006-06-16 07:37:27 +00:00
2006-06-21 15:37:51 +00:00
if (px != XP_NULL) px->next = p->next;
2006-08-10 16:02:15 +00:00
else run->extio.chain = p->next;
2006-06-16 07:37:27 +00:00
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, p->name);
XP_AWK_FREE (run->awk, p);
2006-06-19 04:38:51 +00:00
return 0;
2006-06-16 07:37:27 +00:00
}
px = p;
p = p->next;
}
2006-06-19 09:10:57 +00:00
/* this is not a run-time error */
2006-08-23 15:42:16 +00:00
/* TODO: set ERRNO */
2006-08-27 15:29:21 +00:00
run->errnum = XP_AWK_EIOHANDLER;
2006-06-16 07:37:27 +00:00
return -1;
}
2006-06-21 13:52:15 +00:00
2006-06-21 15:37:51 +00:00
void xp_awk_clearextio (xp_awk_run_t* run)
2006-06-21 13:52:15 +00:00
{
xp_awk_extio_t* next;
xp_awk_io_t handler;
2006-06-21 15:37:51 +00:00
int n;
2006-06-21 13:52:15 +00:00
2006-08-10 16:02:15 +00:00
while (run->extio.chain != XP_NULL)
2006-06-21 13:52:15 +00:00
{
2006-08-31 14:52:12 +00:00
handler = run->extio.handler[
run->extio.chain->type & __MASK_CLEAR];
2006-08-10 16:02:15 +00:00
next = run->extio.chain->next;
2006-06-21 13:52:15 +00:00
2006-06-21 15:37:51 +00:00
if (handler != XP_NULL)
2006-06-21 13:52:15 +00:00
{
2006-08-10 16:02:15 +00:00
n = handler (XP_AWK_IO_CLOSE, run->extio.chain, XP_NULL, 0);
2006-06-21 15:37:51 +00:00
if (n == -1)
{
/* TODO:
* some warning actions need to be taken */
}
2006-06-21 13:52:15 +00:00
}
2006-08-31 14:52:12 +00:00
XP_AWK_FREE (run->awk, run->extio.chain->name);
XP_AWK_FREE (run->awk, run->extio.chain);
2006-06-21 13:52:15 +00:00
2006-08-10 16:02:15 +00:00
run->extio.chain = next;
2006-06-21 13:52:15 +00:00
}
}