qse/ase/awk/extio.c

952 lines
21 KiB
C
Raw Normal View History

2006-06-16 07:37:27 +00:00
/*
2007-03-20 10:46:27 +00:00
* $Id: extio.c,v 1.78 2007-03-20 10:44:44 bacon Exp $
2007-02-03 10:47:41 +00:00
*
* {License}
2006-06-16 07:37:27 +00:00
*/
2006-10-24 04:10:12 +00:00
#include <ase/awk/awk_i.h>
2006-06-16 07:37:27 +00:00
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
2006-10-24 04:10:12 +00:00
* ASE_AWK_IN_XXX values in tree.h */
ASE_AWK_EXTIO_PIPE,
ASE_AWK_EXTIO_COPROC,
ASE_AWK_EXTIO_FILE,
ASE_AWK_EXTIO_CONSOLE
2006-07-28 10:34:22 +00:00
};
static int __in_mode_map[] =
{
/* the order should match the order of the
2006-10-24 04:10:12 +00:00
* ASE_AWK_IN_XXX values in tree.h */
2006-11-21 15:06:51 +00:00
ASE_AWK_EXTIO_PIPE_READ,
2006-07-28 10:34:22 +00:00
0,
2006-11-21 15:06:51 +00:00
ASE_AWK_EXTIO_FILE_READ,
ASE_AWK_EXTIO_CONSOLE_READ
2006-07-28 10:34:22 +00:00
};
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
2006-10-24 04:10:12 +00:00
* ASE_AWK_OUT_XXX values in tree.h */
ASE_AWK_EXTIO_PIPE,
ASE_AWK_EXTIO_COPROC,
ASE_AWK_EXTIO_FILE,
ASE_AWK_EXTIO_FILE,
ASE_AWK_EXTIO_CONSOLE
2006-07-28 10:34:22 +00:00
};
static int __out_mode_map[] =
{
/* the order should match the order of the
2006-10-24 04:10:12 +00:00
* ASE_AWK_OUT_XXX values in tree.h */
2006-11-21 15:06:51 +00:00
ASE_AWK_EXTIO_PIPE_WRITE,
2006-07-28 10:34:22 +00:00
0,
2006-11-21 15:06:51 +00:00
ASE_AWK_EXTIO_FILE_WRITE,
ASE_AWK_EXTIO_FILE_APPEND,
ASE_AWK_EXTIO_CONSOLE_WRITE
2006-07-28 10:34:22 +00:00
};
static int __out_mask_map[] =
{
__MASK_WRITE,
__MASK_RDWR,
__MASK_WRITE,
__MASK_WRITE,
__MASK_WRITE
};
2006-07-14 04:19:22 +00:00
2006-10-24 04:10:12 +00:00
int ase_awk_readextio (
ase_awk_run_t* run, int in_type,
2007-02-23 08:17:51 +00:00
const ase_char_t* name, ase_str_t* buf)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain;
ase_awk_io_t handler;
2006-08-24 03:30:32 +00:00
int extio_type, extio_mode, extio_mask, n, ret;
2006-10-24 04:10:12 +00:00
ase_awk_val_t* rs;
ase_char_t* rs_ptr;
ase_size_t rs_len;
ase_size_t line_len = 0;
2007-01-03 04:16:15 +00:00
ase_char_t c = ASE_T('\0'), pc;
2006-10-24 04:10:12 +00:00
2007-03-06 14:58:00 +00:00
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_type_map));
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_mode_map));
ASE_ASSERT (in_type >= 0 && in_type <= ASE_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-10-24 04:10:12 +00:00
if (handler == ASE_NULL)
2006-06-21 15:37:51 +00:00
{
/* no io handler provided */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIOUSER);
2006-06-21 15:37:51 +00:00
return -1;
}
2006-06-16 07:37:27 +00:00
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-06-16 07:37:27 +00:00
{
2006-06-29 14:38:01 +00:00
if (p->type == (extio_type | extio_mask) &&
2007-02-23 08:17:51 +00:00
ase_strcmp (p->name,name) == 0) break;
2006-06-16 07:37:27 +00:00
p = p->next;
}
2006-10-24 04:10:12 +00:00
if (p == ASE_NULL)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
p = (ase_awk_extio_t*) ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
run->awk, ASE_SIZEOF(ase_awk_extio_t));
2006-10-24 04:10:12 +00:00
if (p == ASE_NULL)
2006-06-16 07:37:27 +00:00
{
2007-03-10 11:59:04 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
2006-06-16 07:37:27 +00:00
return -1;
}
2007-02-23 08:17:51 +00:00
p->name = ase_strdup (name, &run->awk->prmfns.mmgr);
2006-10-24 04:10:12 +00:00
if (p->name == ASE_NULL)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p);
2007-03-10 11:59:04 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
2006-06-16 07:37:27 +00:00
return -1;
}
2006-10-16 14:39:21 +00:00
p->run = run;
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-10-24 04:10:12 +00:00
p->handle = ASE_NULL;
p->next = ASE_NULL;
2006-10-13 10:18:39 +00:00
p->custom_data = run->extio.custom_data;
2006-06-28 14:19:01 +00:00
2006-10-24 04:10:12 +00:00
p->in.buf[0] = ASE_T('\0');
2006-06-28 14:19:01 +00:00
p->in.pos = 0;
p->in.len = 0;
2006-10-24 04:10:12 +00:00
p->in.eof = ase_false;
2006-11-21 15:06:51 +00:00
p->in.eos = ase_false;
2006-06-28 14:19:01 +00:00
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2007-03-19 15:25:51 +00:00
2006-10-24 04:10:12 +00:00
n = handler (ASE_AWK_IO_OPEN, p, ASE_NULL, 0);
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p->name);
ASE_AWK_FREE (run->awk, p);
2007-03-19 15:25:51 +00:00
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
{
/* if the error number has not been
* set by the user handler */
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
}
2007-03-19 15:25:51 +00:00
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
2006-10-13 10:18:39 +00:00
/* usually, n == 0 indicates that it has reached the end
* of the 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. */
2006-11-21 15:06:51 +00:00
if (n == 0)
{
p->in.eos = ase_true;
return 0;
}
}
if (p->in.eos)
{
/* no more streams. */
return 0;
2006-06-19 09:10:57 +00:00
}
2006-08-24 03:30:32 +00:00
/* ready to read a line */
2007-02-23 08:17:51 +00:00
ase_str_clear (buf);
2006-06-19 04:38:51 +00:00
2006-08-24 03:30:32 +00:00
/* get the record separator */
2006-10-24 04:10:12 +00:00
rs = ase_awk_getglobal (run, ASE_AWK_GLOBAL_RS);
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, rs);
2006-08-24 03:30:32 +00:00
2006-10-24 04:10:12 +00:00
if (rs->type == ASE_AWK_VAL_NIL)
2006-08-24 03:30:32 +00:00
{
2006-10-24 04:10:12 +00:00
rs_ptr = ASE_NULL;
2006-08-24 03:30:32 +00:00
rs_len = 0;
}
2006-10-24 04:10:12 +00:00
else if (rs->type == ASE_AWK_VAL_STR)
2006-08-24 03:30:32 +00:00
{
2006-10-24 04:10:12 +00:00
rs_ptr = ((ase_awk_val_str_t*)rs)->buf;
rs_len = ((ase_awk_val_str_t*)rs)->len;
2006-08-24 03:30:32 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
rs_ptr = ase_awk_valtostr (
run, rs, ASE_AWK_VALTOSTR_CLEAR, ASE_NULL, &rs_len);
if (rs_ptr == ASE_NULL)
2006-08-24 03:30:32 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, rs);
2006-08-24 03:30:32 +00:00
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
if (p->in.pos >= p->in.len)
{
2006-10-24 04:10:12 +00:00
ase_ssize_t n;
2006-06-28 14:19:01 +00:00
if (p->in.eof)
{
2007-02-23 08:17:51 +00:00
if (ASE_STR_LEN(buf) == 0) ret = 0;
2006-06-28 14:19:01 +00:00
break;
}
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
n = handler (ASE_AWK_IO_READ,
p, p->in.buf, ASE_COUNTOF(p->in.buf));
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-06-28 14:19:01 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
{
/* if the error number has not been
* set by the user handler */
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
}
2006-12-17 14:56:07 +00:00
ret = -1;
2006-08-24 03:30:32 +00:00
break;
2006-06-28 14:19:01 +00:00
}
if (n == 0)
{
2006-10-24 04:10:12 +00:00
p->in.eof = ase_true;
2007-02-23 08:17:51 +00:00
if (ASE_STR_LEN(buf) == 0) ret = 0;
2006-06-28 14:19:01 +00:00
break;
}
p->in.len = n;
p->in.pos = 0;
}
2007-01-03 04:16:15 +00:00
pc = c;
2006-06-28 14:19:01 +00:00
c = p->in.buf[p->in.pos++];
2006-08-24 03:30:32 +00:00
2006-10-24 04:10:12 +00:00
if (rs_ptr == ASE_NULL)
2006-08-24 03:30:32 +00:00
{
/* separate by a new line */
2007-01-03 04:16:15 +00:00
if (c == ASE_T('\n'))
{
if (pc == ASE_T('\r') &&
2007-02-23 08:17:51 +00:00
ASE_STR_LEN(buf) > 0)
2007-01-03 04:16:15 +00:00
{
2007-02-23 08:17:51 +00:00
ASE_STR_LEN(buf) -= 1;
2007-01-03 04:16:15 +00:00
}
break;
}
2006-08-24 03:30:32 +00:00
}
else if (rs_len == 0)
{
/* separate by a blank line */
2007-01-03 04:16:15 +00:00
if (c == ASE_T('\n'))
{
if (pc == ASE_T('\r') &&
2007-02-23 08:17:51 +00:00
ASE_STR_LEN(buf) > 0)
2007-01-03 04:16:15 +00:00
{
2007-02-23 08:17:51 +00:00
ASE_STR_LEN(buf) -= 1;
2007-01-03 04:16:15 +00:00
}
}
2006-10-24 04:10:12 +00:00
if (line_len == 0 && c == ASE_T('\n'))
2006-08-25 03:31:09 +00:00
{
2007-02-23 08:17:51 +00:00
if (ASE_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 */
2007-02-23 08:17:51 +00:00
ASE_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-10-24 04:10:12 +00:00
const ase_char_t* match_ptr;
ase_size_t match_len;
2006-08-25 15:52:47 +00:00
2007-03-06 14:58:00 +00:00
ASE_ASSERT (run->global.rs != ASE_NULL);
2006-08-29 15:01:45 +00:00
2006-10-24 04:10:12 +00:00
n = ase_awk_matchrex (
2006-09-22 14:05:30 +00:00
run->awk, run->global.rs,
2006-10-24 04:10:12 +00:00
((run->global.ignorecase)? ASE_AWK_REX_IGNORECASE: 0),
2007-02-23 08:17:51 +00:00
ASE_STR_BUF(buf), ASE_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 */
2007-03-06 14:58:00 +00:00
ASE_ASSERT (
2007-02-23 08:17:51 +00:00
ASE_STR_BUF(buf) + ASE_STR_LEN(buf) ==
2006-08-29 15:01:45 +00:00
match_ptr + match_len);
2007-02-23 08:17:51 +00:00
ASE_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
2007-02-23 08:17:51 +00:00
if (ase_str_ccat (buf, c) == (ase_size_t)-1)
2006-06-28 14:19:01 +00:00
{
2007-03-10 11:59:04 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
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-10-24 04:10:12 +00:00
if (c == ASE_T('\n')) line_len = 0;
2006-08-25 03:31:09 +00:00
else line_len = line_len + 1;
2006-06-16 07:37:27 +00:00
}
2006-11-21 15:06:51 +00:00
if (rs_ptr != ASE_NULL &&
rs->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, rs_ptr);
2006-10-24 04:10:12 +00:00
ase_awk_refdownval (run, rs);
2006-08-30 07:15:14 +00:00
/* increment NR */
2006-11-17 07:26:15 +00:00
if (ret != -1 && ret != 0)
2006-08-30 07:15:14 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_val_t* nr;
ase_long_t lv;
ase_real_t rv;
2006-08-30 07:15:14 +00:00
2006-10-24 04:10:12 +00:00
nr = ase_awk_getglobal (run, ASE_AWK_GLOBAL_NR);
2006-11-16 11:53:16 +00:00
ase_awk_refupval (run, nr);
2006-10-24 04:10:12 +00:00
n = ase_awk_valtonum (run, nr, &lv, &rv);
ase_awk_refdownval (run, nr);
2006-08-30 07:15:14 +00:00
2006-09-01 06:23:58 +00:00
if (n == -1) ret = -1;
2006-08-30 07:15:14 +00:00
else
{
2006-10-24 04:10:12 +00:00
if (n == 1) lv = (ase_long_t)rv;
2006-08-30 07:15:14 +00:00
2006-10-24 04:10:12 +00:00
nr = ase_awk_makeintval (run, lv + 1);
if (nr == ASE_NULL)
2006-08-30 07:15:14 +00:00
{
2007-03-10 11:59:04 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
2006-08-30 07:15:14 +00:00
ret = -1;
}
else
{
2006-10-24 04:10:12 +00:00
if (ase_awk_setglobal (
run, ASE_AWK_GLOBAL_NR, nr) == -1) ret = -1;
2006-08-30 07:15:14 +00:00
}
}
}
2006-08-24 03:30:32 +00:00
return ret;
2006-06-16 07:37:27 +00:00
}
2006-10-24 04:10:12 +00:00
int ase_awk_writeextio_val (
ase_awk_run_t* run, int out_type,
const ase_char_t* name, ase_awk_val_t* v)
2006-08-03 09:54:16 +00:00
{
2006-10-24 04:10:12 +00:00
ase_char_t* str;
ase_size_t len;
2006-09-29 11:18:13 +00:00
int n;
2006-08-03 09:54:16 +00:00
2006-10-24 04:10:12 +00:00
if (v->type == ASE_AWK_VAL_STR)
2006-09-29 11:18:13 +00:00
{
2006-10-24 04:10:12 +00:00
str = ((ase_awk_val_str_t*)v)->buf;
len = ((ase_awk_val_str_t*)v)->len;
2006-09-29 11:18:13 +00:00
}
else
{
2006-10-24 04:10:12 +00:00
str = ase_awk_valtostr (
2006-10-12 04:17:58 +00:00
run, v,
2006-10-24 04:10:12 +00:00
ASE_AWK_VALTOSTR_CLEAR | ASE_AWK_VALTOSTR_PRINT,
ASE_NULL, &len);
if (str == ASE_NULL) return -1;
2006-09-29 11:18:13 +00:00
}
2006-10-24 04:10:12 +00:00
n = ase_awk_writeextio_str (run, out_type, name, str, len);
2006-09-29 11:18:13 +00:00
2006-10-24 04:10:12 +00:00
if (v->type != ASE_AWK_VAL_STR) ASE_AWK_FREE (run->awk, str);
2006-09-29 11:18:13 +00:00
return n;
2006-08-03 09:54:16 +00:00
}
2006-10-24 04:10:12 +00:00
int ase_awk_writeextio_str (
ase_awk_run_t* run, int out_type,
const ase_char_t* name, ase_char_t* str, ase_size_t len)
2006-06-22 14:15:02 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain;
ase_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
2007-03-06 14:58:00 +00:00
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_type_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mode_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_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-10-24 04:10:12 +00:00
if (handler == ASE_NULL)
2006-06-22 14:15:02 +00:00
{
/* no io handler provided */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIOUSER);
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-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-06-22 14:15:02 +00:00
{
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
2006-10-24 04:10:12 +00:00
* both ASE_AWK_OUT_FILE and ASE_AWK_OUT_FILE_APPEND are
* translated to ASE_AWK_EXTIO_FILE and it is used to
2006-06-28 08:56:59 +00:00
* 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) &&
2007-02-23 08:17:51 +00:00
ase_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-10-24 04:10:12 +00:00
if (p == ASE_NULL)
2006-06-22 14:15:02 +00:00
{
2006-10-24 04:10:12 +00:00
p = (ase_awk_extio_t*) ASE_AWK_MALLOC (
2006-11-29 02:54:17 +00:00
run->awk, ASE_SIZEOF(ase_awk_extio_t));
2006-10-24 04:10:12 +00:00
if (p == ASE_NULL)
2006-06-22 14:15:02 +00:00
{
2007-03-10 11:59:04 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
2006-06-22 14:15:02 +00:00
return -1;
}
2007-02-23 08:17:51 +00:00
p->name = ase_strdup (name, &run->awk->prmfns.mmgr);
2006-10-24 04:10:12 +00:00
if (p->name == ASE_NULL)
2006-06-22 14:15:02 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p);
2007-03-10 11:59:04 +00:00
ase_awk_setrunerror (
run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0);
2006-06-22 14:15:02 +00:00
return -1;
}
2006-10-16 14:39:21 +00:00
p->run = run;
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-10-24 04:10:12 +00:00
p->handle = ASE_NULL;
p->next = ASE_NULL;
2006-10-13 10:18:39 +00:00
p->custom_data = run->extio.custom_data;
2006-06-22 14:15:02 +00:00
2006-11-21 15:06:51 +00:00
p->out.eof = ase_false;
p->out.eos = ase_false;
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-10-24 04:10:12 +00:00
n = handler (ASE_AWK_IO_OPEN, p, ASE_NULL, 0);
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-06-22 14:15:02 +00:00
{
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p->name);
ASE_AWK_FREE (run->awk, p);
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
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
2006-10-13 10:18:39 +00:00
/* usually, n == 0 indicates that it has reached the end
* of the 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. */
2006-11-21 15:06:51 +00:00
if (n == 0)
{
p->out.eos = ase_true;
return 0;
}
}
if (p->out.eos)
{
/* no more streams */
return 0;
}
if (p->out.eof)
{
/* it has reached the end of the stream but this function
* has been recalled */
return 0;
2006-06-22 14:15:02 +00:00
}
2006-11-21 15:06:51 +00:00
while (len > 0)
2006-07-28 10:34:22 +00:00
{
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-10-24 04:10:12 +00:00
n = handler (ASE_AWK_IO_WRITE, p, str, len);
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-08-03 09:54:16 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
2006-08-03 09:54:16 +00:00
return -1;
}
2006-11-21 15:06:51 +00:00
if (n == 0)
{
p->out.eof = ase_true;
return 0;
}
len -= n;
str += n;
2006-06-22 14:15:02 +00:00
}
return 1;
}
2006-10-24 04:10:12 +00:00
int ase_awk_flushextio (
ase_awk_run_t* run, int out_type, const ase_char_t* name)
2006-08-22 15:11:13 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain;
ase_awk_io_t handler;
2006-10-27 13:49:43 +00:00
int extio_type, /*extio_mode,*/ extio_mask, n;
2006-10-24 04:10:12 +00:00
ase_bool_t ok = ase_false;
2006-08-22 15:11:13 +00:00
2007-03-06 14:58:00 +00:00
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_type_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mode_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mask_map));
2006-08-22 15:11:13 +00:00
/* translate the out_type into the relevant extio type and mode */
extio_type = __out_type_map[out_type];
2006-10-27 13:49:43 +00:00
/*extio_mode = __out_mode_map[out_type];*/
2006-08-22 15:11:13 +00:00
extio_mask = __out_mask_map[out_type];
handler = run->extio.handler[extio_type];
2006-10-24 04:10:12 +00:00
if (handler == ASE_NULL)
2006-08-22 15:11:13 +00:00
{
/* no io handler provided */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIOUSER);
2006-08-22 15:11:13 +00:00
return -1;
}
/* look for the corresponding extio for name */
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-08-22 15:11:13 +00:00
{
if (p->type == (extio_type | extio_mask) &&
2007-03-20 10:46:27 +00:00
(name == ASE_NULL || ase_strcmp(p->name,name) == 0))
2006-08-23 15:42:16 +00:00
{
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-10-24 04:10:12 +00:00
n = handler (ASE_AWK_IO_FLUSH, p, ASE_NULL, 0);
2006-08-22 15:11:13 +00:00
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-08-23 15:42:16 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
2006-08-23 15:42:16 +00:00
return -1;
}
2006-08-22 15:11:13 +00:00
2006-10-24 04:10:12 +00:00
ok = ase_true;
2006-08-23 15:42:16 +00:00
}
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 */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIONONE);
2006-08-23 15:42:16 +00:00
return -1;
2006-08-22 15:11:13 +00:00
}
2006-10-24 04:10:12 +00:00
int ase_awk_nextextio_read (
ase_awk_run_t* run, int in_type, const ase_char_t* name)
2006-07-28 10:34:22 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain;
ase_awk_io_t handler;
2006-10-27 13:49:43 +00:00
int extio_type, /*extio_mode,*/ extio_mask, n;
2006-07-28 10:34:22 +00:00
2007-03-06 14:58:00 +00:00
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_type_map));
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_mode_map));
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_mask_map));
2006-07-28 10:34:22 +00:00
/* translate the in_type into the relevant extio type and mode */
extio_type = __in_type_map[in_type];
2006-10-27 13:49:43 +00:00
/*extio_mode = __in_mode_map[in_type];*/
2006-07-28 10:34:22 +00:00
extio_mask = __in_mask_map[in_type];
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-10-24 04:10:12 +00:00
if (handler == ASE_NULL)
2006-07-28 10:34:22 +00:00
{
/* no io handler provided */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIOUSER);
2006-07-28 10:34:22 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-07-28 10:34:22 +00:00
{
if (p->type == (extio_type | extio_mask) &&
2007-02-23 08:17:51 +00:00
ase_strcmp (p->name,name) == 0) break;
2006-07-28 10:34:22 +00:00
p = p->next;
}
2006-10-24 04:10:12 +00:00
if (p == ASE_NULL)
2006-07-28 10:34:22 +00:00
{
/* something is totally wrong */
2007-03-06 14:58:00 +00:00
ASE_ASSERT (
2006-11-23 03:31:58 +00:00
!"should never happen - cannot find the relevant extio entry");
2007-03-10 15:02:31 +00:00
ase_awk_setrunerror (run, ASE_AWK_EINTERN, 0, ASE_NULL, 0);
2006-07-28 10:34:22 +00:00
return -1;
}
2006-11-21 15:06:51 +00:00
if (p->in.eos)
{
/* no more streams. */
return 0;
}
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-10-24 04:10:12 +00:00
n = handler (ASE_AWK_IO_NEXT, p, ASE_NULL, 0);
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-07-28 10:34:22 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
2006-07-28 10:34:22 +00:00
return -1;
}
2006-11-21 15:06:51 +00:00
if (n == 0)
{
/* the next stream cannot be opened.
* set the eos flags so that the next call to nextextio_read
* will return 0 without executing the handler */
p->in.eos = ase_true;
}
else
{
/* as the next stream has been opened successfully,
* the eof flag should be cleared if set */
p->in.eof = ase_false;
2006-11-22 15:12:04 +00:00
/* also the previous input buffer must be reset */
p->in.pos = 0;
p->in.len = 0;
2006-11-21 15:06:51 +00:00
}
2006-07-28 10:34:22 +00:00
return n;
}
2006-11-23 03:31:58 +00:00
int ase_awk_nextextio_write (
ase_awk_run_t* run, int out_type, const ase_char_t* name)
{
ase_awk_extio_t* p = run->extio.chain;
ase_awk_io_t handler;
int extio_type, /*extio_mode,*/ extio_mask, n;
2007-03-06 14:58:00 +00:00
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_type_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mode_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mask_map));
2006-11-23 03:31:58 +00:00
/* 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 == ASE_NULL)
{
/* no io handler provided */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIOUSER);
2006-11-23 03:31:58 +00:00
return -1;
}
while (p != ASE_NULL)
{
if (p->type == (extio_type | extio_mask) &&
2007-02-23 08:17:51 +00:00
ase_strcmp (p->name,name) == 0) break;
2006-11-23 03:31:58 +00:00
p = p->next;
}
if (p == ASE_NULL)
{
/* something is totally wrong */
2007-03-06 14:58:00 +00:00
ASE_ASSERT (!"should never happen - cannot find the relevant extio entry");
2006-11-23 03:31:58 +00:00
2007-03-10 15:02:31 +00:00
ase_awk_setrunerror (run, ASE_AWK_EINTERN, 0, ASE_NULL, 0);
2006-11-23 03:31:58 +00:00
return -1;
}
if (p->out.eos)
{
/* no more streams. */
return 0;
}
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-11-23 03:31:58 +00:00
n = handler (ASE_AWK_IO_NEXT, p, ASE_NULL, 0);
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-11-23 03:31:58 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
2006-11-23 03:31:58 +00:00
return -1;
}
if (n == 0)
{
/* the next stream cannot be opened.
* set the eos flags so that the next call to nextextio_write
* will return 0 without executing the handler */
p->out.eos = ase_true;
}
else
{
/* as the next stream has been opened successfully,
* the eof flag should be cleared if set */
p->out.eof = ase_false;
}
return n;
}
2006-10-24 04:10:12 +00:00
int ase_awk_closeextio_read (
ase_awk_run_t* run, int in_type, const ase_char_t* name)
2006-08-02 11:26:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain, * px = ASE_NULL;
ase_awk_io_t handler;
2006-10-27 13:49:43 +00:00
int extio_type, /*extio_mode,*/ extio_mask;
2006-08-02 11:26:11 +00:00
2007-03-06 14:58:00 +00:00
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_type_map));
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_mode_map));
ASE_ASSERT (in_type >= 0 && in_type <= ASE_COUNTOF(__in_mask_map));
2006-08-02 11:26:11 +00:00
/* translate the in_type into the relevant extio type and mode */
extio_type = __in_type_map[in_type];
2006-10-27 13:49:43 +00:00
/*extio_mode = __in_mode_map[in_type];*/
2006-08-02 11:26:11 +00:00
extio_mask = __in_mask_map[in_type];
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-10-24 04:10:12 +00:00
if (handler == ASE_NULL)
2006-08-02 11:26:11 +00:00
{
/* no io handler provided */
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIOUSER);
2006-08-02 11:26:11 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-08-02 11:26:11 +00:00
{
if (p->type == (extio_type | extio_mask) &&
2007-02-23 08:17:51 +00:00
ase_strcmp (p->name, name) == 0)
2006-08-02 11:26:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_io_t handler;
2006-08-02 11:26:11 +00:00
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[p->type & __MASK_CLEAR];
2006-10-24 04:10:12 +00:00
if (handler != ASE_NULL)
2006-08-02 11:26:11 +00:00
{
2006-12-09 11:50:08 +00:00
if (handler (ASE_AWK_IO_CLOSE, p, ASE_NULL, 0) <= -1)
2006-08-02 11:26:11 +00:00
{
/* this is not a run-time error.*/
2007-03-10 15:02:31 +00:00
ase_awk_setrunerror (run, ASE_AWK_EIOIMPL, 0, ASE_NULL, 0);
2006-08-02 11:26:11 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
if (px != ASE_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-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p->name);
ASE_AWK_FREE (run->awk, p);
2006-08-02 11:26:11 +00:00
return 0;
}
px = p;
p = p->next;
}
2007-03-20 10:46:27 +00:00
/* the name given is not found */
ase_awk_setrunerrnum (run, ASE_AWK_EIONONE);
2006-08-02 11:26:11 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
int ase_awk_closeextio_write (
ase_awk_run_t* run, int out_type, const ase_char_t* name)
2006-08-02 11:26:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain, * px = ASE_NULL;
ase_awk_io_t handler;
2006-10-27 13:49:43 +00:00
int extio_type, /*extio_mode,*/ extio_mask;
2006-08-02 11:26:11 +00:00
2007-03-06 14:58:00 +00:00
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_type_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mode_map));
ASE_ASSERT (out_type >= 0 && out_type <= ASE_COUNTOF(__out_mask_map));
2006-08-02 11:26:11 +00:00
/* translate the out_type into the relevant extio type and mode */
extio_type = __out_type_map[out_type];
2006-10-27 13:49:43 +00:00
/*extio_mode = __out_mode_map[out_type];*/
2006-08-02 11:26:11 +00:00
extio_mask = __out_mask_map[out_type];
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[extio_type];
2006-10-24 04:10:12 +00:00
if (handler == ASE_NULL)
2006-08-02 11:26:11 +00:00
{
/* no io handler provided */
2007-03-10 15:02:31 +00:00
ase_awk_setrunerror (run, ASE_AWK_EIOUSER, 0, ASE_NULL, 0);
2006-08-02 11:26:11 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-08-02 11:26:11 +00:00
{
if (p->type == (extio_type | extio_mask) &&
2007-02-23 08:17:51 +00:00
ase_strcmp (p->name, name) == 0)
2006-08-02 11:26:11 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_io_t handler;
2006-08-02 11:26:11 +00:00
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[p->type & __MASK_CLEAR];
2006-10-24 04:10:12 +00:00
if (handler != ASE_NULL)
2006-08-02 11:26:11 +00:00
{
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-12-09 11:50:08 +00:00
if (handler (ASE_AWK_IO_CLOSE, p, ASE_NULL, 0) <= -1)
2006-08-02 11:26:11 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
2006-08-02 11:26:11 +00:00
return -1;
}
}
2006-10-24 04:10:12 +00:00
if (px != ASE_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-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p->name);
ASE_AWK_FREE (run->awk, p);
2006-08-02 11:26:11 +00:00
return 0;
}
px = p;
p = p->next;
}
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIONONE);
2006-08-02 11:26:11 +00:00
return -1;
}
2006-10-24 04:10:12 +00:00
int ase_awk_closeextio (ase_awk_run_t* run, const ase_char_t* name)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* p = run->extio.chain, * px = ASE_NULL;
2006-06-16 07:37:27 +00:00
2006-10-24 04:10:12 +00:00
while (p != ASE_NULL)
2006-06-16 07:37:27 +00:00
{
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 */
2007-02-23 08:17:51 +00:00
if (ase_strcmp (p->name, name) == 0)
2006-06-16 07:37:27 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_io_t handler;
2006-06-29 14:38:01 +00:00
2006-08-10 16:02:15 +00:00
handler = run->extio.handler[p->type & __MASK_CLEAR];
2006-10-24 04:10:12 +00:00
if (handler != ASE_NULL)
2006-06-19 04:38:51 +00:00
{
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-12-09 11:50:08 +00:00
if (handler (ASE_AWK_IO_CLOSE, p, ASE_NULL, 0) <= -1)
2006-06-21 15:37:51 +00:00
{
/* this is not a run-time error.*/
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
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-10-24 04:10:12 +00:00
if (px != ASE_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-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, p->name);
ASE_AWK_FREE (run->awk, p);
2007-03-20 10:46:27 +00:00
2006-06-19 04:38:51 +00:00
return 0;
2006-06-16 07:37:27 +00:00
}
px = p;
p = p->next;
}
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_EIONONE);
2006-06-16 07:37:27 +00:00
return -1;
}
2006-06-21 13:52:15 +00:00
2006-10-24 04:10:12 +00:00
void ase_awk_clearextio (ase_awk_run_t* run)
2006-06-21 13:52:15 +00:00
{
2006-10-24 04:10:12 +00:00
ase_awk_extio_t* next;
ase_awk_io_t handler;
2006-06-21 15:37:51 +00:00
int n;
2006-06-21 13:52:15 +00:00
2006-10-24 04:10:12 +00:00
while (run->extio.chain != ASE_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-10-24 04:10:12 +00:00
if (handler != ASE_NULL)
2006-06-21 13:52:15 +00:00
{
2007-03-20 10:46:27 +00:00
ase_awk_setrunerrnum (run, ASE_AWK_ENOERR);
2006-10-24 04:10:12 +00:00
n = handler (ASE_AWK_IO_CLOSE, run->extio.chain, ASE_NULL, 0);
2006-12-09 11:50:08 +00:00
if (n <= -1)
2006-06-21 15:37:51 +00:00
{
2007-03-20 10:46:27 +00:00
if (run->errnum == ASE_AWK_ENOERR)
ase_awk_setrunerrnum (run, ASE_AWK_EIOIMPL);
/* TODO: some warnings need to be shown??? */
2006-06-21 15:37:51 +00:00
}
2006-06-21 13:52:15 +00:00
}
2006-10-24 04:10:12 +00:00
ASE_AWK_FREE (run->awk, run->extio.chain->name);
ASE_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
}
}