2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2007-05-02 01:07:00 +00:00
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2014-11-19 14:42:24 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "awk-prv.h"
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-04-28 08:04:13 +00:00
|
|
|
enum io_mask_t
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
IO_MASK_READ = 0x0100,
|
|
|
|
IO_MASK_WRITE = 0x0200,
|
|
|
|
IO_MASK_RDWR = 0x0400,
|
|
|
|
IO_MASK_CLEAR = 0x00FF
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int in_type_map[] =
|
|
|
|
{
|
|
|
|
/* the order should match the order of the
|
2008-12-21 21:35:07 +00:00
|
|
|
* QSE_AWK_IN_XXX values in tree.h */
|
2009-02-16 08:31:34 +00:00
|
|
|
QSE_AWK_RIO_PIPE,
|
|
|
|
QSE_AWK_RIO_PIPE,
|
|
|
|
QSE_AWK_RIO_FILE,
|
|
|
|
QSE_AWK_RIO_CONSOLE
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int in_mode_map[] =
|
|
|
|
{
|
|
|
|
/* the order should match the order of the
|
2008-12-21 21:35:07 +00:00
|
|
|
* QSE_AWK_IN_XXX values in tree.h */
|
2009-02-16 08:31:34 +00:00
|
|
|
QSE_AWK_RIO_PIPE_READ,
|
|
|
|
QSE_AWK_RIO_PIPE_RW,
|
|
|
|
QSE_AWK_RIO_FILE_READ,
|
|
|
|
QSE_AWK_RIO_CONSOLE_READ
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int in_mask_map[] =
|
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
IO_MASK_READ,
|
|
|
|
IO_MASK_RDWR,
|
|
|
|
IO_MASK_READ,
|
|
|
|
IO_MASK_READ
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int out_type_map[] =
|
|
|
|
{
|
|
|
|
/* the order should match the order of the
|
2008-12-21 21:35:07 +00:00
|
|
|
* QSE_AWK_OUT_XXX values in tree.h */
|
2009-02-16 08:31:34 +00:00
|
|
|
QSE_AWK_RIO_PIPE,
|
|
|
|
QSE_AWK_RIO_PIPE,
|
|
|
|
QSE_AWK_RIO_FILE,
|
|
|
|
QSE_AWK_RIO_FILE,
|
|
|
|
QSE_AWK_RIO_CONSOLE
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int out_mode_map[] =
|
|
|
|
{
|
|
|
|
/* the order should match the order of the
|
2008-12-21 21:35:07 +00:00
|
|
|
* QSE_AWK_OUT_XXX values in tree.h */
|
2009-02-16 08:31:34 +00:00
|
|
|
QSE_AWK_RIO_PIPE_WRITE,
|
|
|
|
QSE_AWK_RIO_PIPE_RW,
|
|
|
|
QSE_AWK_RIO_FILE_WRITE,
|
|
|
|
QSE_AWK_RIO_FILE_APPEND,
|
|
|
|
QSE_AWK_RIO_CONSOLE_WRITE
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int out_mask_map[] =
|
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
IO_MASK_WRITE,
|
|
|
|
IO_MASK_RDWR,
|
|
|
|
IO_MASK_WRITE,
|
|
|
|
IO_MASK_WRITE,
|
|
|
|
IO_MASK_WRITE
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
static int find_rio_in (
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_t* rtx, int in_type, const qse_char_t* name,
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_arg_t** rio, qse_awk_rio_impl_t* fun)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2011-05-01 09:24:38 +00:00
|
|
|
int io_type, io_mode, io_mask;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_type_map));
|
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_mode_map));
|
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* translate the in_type into the relevant io type and mode */
|
|
|
|
io_type = in_type_map[in_type];
|
|
|
|
io_mode = in_mode_map[in_type];
|
|
|
|
io_mask = in_mask_map[in_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
/* get the I/O handler provided by a user */
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
2008-12-21 21:35:07 +00:00
|
|
|
if (handler == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
/* no I/O handler provided */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-17 04:20:22 +00:00
|
|
|
/* search the chain for exiting an existing io name */
|
2013-01-16 05:04:56 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-16 08:31:34 +00:00
|
|
|
if (p->type == (io_type | io_mask) &&
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_strcmp (p->name,name) == 0) break;
|
2007-05-02 01:07:00 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
qse_ssize_t x;
|
|
|
|
|
2009-01-17 04:20:22 +00:00
|
|
|
/* if the name doesn't exist in the chain, create an entry
|
|
|
|
* to the chain */
|
2019-06-30 09:26:33 +00:00
|
|
|
p = (qse_awk_rio_arg_t*)qse_awk_rtx_allocmem(rtx, QSE_SIZEOF(qse_awk_rio_arg_t));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-27 14:33:14 +00:00
|
|
|
QSE_MEMSET (p, 0, QSE_SIZEOF(*p));
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
p->name = QSE_AWK_STRDUP (rtx->awk, name);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p->name == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
p->type = (io_type | io_mask);
|
|
|
|
p->mode = io_mode;
|
2009-08-29 03:48:02 +00:00
|
|
|
p->rwcmode = QSE_AWK_RIO_CLOSE_FULL;
|
2012-04-27 14:33:14 +00:00
|
|
|
/*
|
2008-12-21 21:35:07 +00:00
|
|
|
p->handle = QSE_NULL;
|
|
|
|
p->next = QSE_NULL;
|
2009-08-28 06:52:20 +00:00
|
|
|
p->rwcstate = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
p->in.buf[0] = QSE_T('\0');
|
2007-05-02 01:07:00 +00:00
|
|
|
p->in.pos = 0;
|
|
|
|
p->in.len = 0;
|
2009-08-28 06:52:20 +00:00
|
|
|
p->in.eof = 0;
|
|
|
|
p->in.eos = 0;
|
2012-04-27 14:33:14 +00:00
|
|
|
*/
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-17 04:20:22 +00:00
|
|
|
/* request to open the stream */
|
2019-06-30 09:26:33 +00:00
|
|
|
x = handler (rtx, QSE_AWK_RIO_OPEN, p, QSE_NULL, 0);
|
2007-11-11 00:30:00 +00:00
|
|
|
if (x <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p->name);
|
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
/* if the error number has not been
|
2007-05-02 01:07:00 +00:00
|
|
|
* set by the user handler */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chain it */
|
2019-06-30 09:26:33 +00:00
|
|
|
p->next = rtx->rio.chain;
|
|
|
|
rtx->rio.chain = p;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
*rio = p;
|
|
|
|
*fun = handler;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static QSE_INLINE int resolve_rs (qse_awk_rtx_t* rtx, qse_awk_val_t* rs, qse_cstr_t* rrs)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2014-10-22 17:05:45 +00:00
|
|
|
qse_awk_val_type_t rs_vtype;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-10-22 17:05:45 +00:00
|
|
|
|
2014-10-23 09:30:22 +00:00
|
|
|
rs_vtype = QSE_AWK_RTX_GETVALTYPE (rtx, rs);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
|
|
|
switch (rs_vtype)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-04-28 08:04:13 +00:00
|
|
|
case QSE_AWK_VAL_NIL:
|
2011-05-01 09:24:38 +00:00
|
|
|
rrs->ptr = QSE_NULL;
|
|
|
|
rrs->len = 0;
|
2011-04-28 08:04:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_AWK_VAL_STR:
|
2011-05-19 08:36:40 +00:00
|
|
|
rrs->ptr = ((qse_awk_val_str_t*)rs)->val.ptr;
|
|
|
|
rrs->len = ((qse_awk_val_str_t*)rs)->val.len;
|
2011-04-28 08:04:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-10-31 08:31:58 +00:00
|
|
|
rrs->ptr = qse_awk_rtx_valtostrdup (rtx, rs, &rrs->len);
|
2011-05-01 09:24:38 +00:00
|
|
|
if (rrs->ptr == QSE_NULL) ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
static QSE_INLINE int match_long_rs (qse_awk_rtx_t* run, qse_str_t* buf, qse_awk_rio_arg_t* p)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t match;
|
2011-05-01 09:24:38 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
|
|
|
int ret;
|
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
QSE_ASSERT (run->gbl.rs[0] != QSE_NULL);
|
|
|
|
QSE_ASSERT (run->gbl.rs[1] != QSE_NULL);
|
2011-05-01 09:24:38 +00:00
|
|
|
|
2013-04-06 13:39:56 +00:00
|
|
|
ret = qse_awk_matchrex (
|
2013-08-23 15:19:29 +00:00
|
|
|
run->awk, run->gbl.rs[run->gbl.ignorecase],
|
2014-07-02 12:28:58 +00:00
|
|
|
run->gbl.ignorecase, QSE_STR_XSTR(buf), QSE_STR_XSTR(buf),
|
2016-11-09 14:12:22 +00:00
|
|
|
&match, QSE_NULL, &errnum);
|
2011-05-01 09:24:38 +00:00
|
|
|
if (ret <= -1)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
|
|
|
}
|
|
|
|
else if (ret >= 1)
|
|
|
|
{
|
|
|
|
if (p->in.eof)
|
|
|
|
{
|
|
|
|
/* when EOF is reached, the record buffer
|
|
|
|
* is not added with a new character. It's
|
|
|
|
* just called again with the same record buffer
|
|
|
|
* as the previous call to this function.
|
|
|
|
* A match in this case must end at the end of
|
|
|
|
* the current record buffer */
|
|
|
|
QSE_ASSERT (
|
2011-05-02 07:28:51 +00:00
|
|
|
QSE_STR_PTR(buf) + QSE_STR_LEN(buf) ==
|
|
|
|
match.ptr + match.len
|
2011-05-01 09:24:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* drop the RS part. no extra character after RS to drop
|
|
|
|
* because we're at EOF and the EOF condition didn't
|
|
|
|
* add a new character to the buffer before the call
|
|
|
|
* to this function.
|
|
|
|
*/
|
|
|
|
QSE_STR_LEN(buf) -= match.len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the match is found before the end of the current buffer,
|
|
|
|
* I see it as the longest match. A match ending at the end
|
|
|
|
* of the buffer is not indeterministic as we don't have the
|
|
|
|
* full input yet.
|
|
|
|
*/
|
|
|
|
const qse_char_t* be = QSE_STR_PTR(buf) + QSE_STR_LEN(buf);
|
|
|
|
const qse_char_t* me = match.ptr + match.len;
|
|
|
|
|
|
|
|
if (me < be)
|
2011-04-28 08:04:13 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
/* the match ends before the ending boundary.
|
|
|
|
* it must be the longest match. drop the RS part
|
|
|
|
* and the characters after RS. */
|
|
|
|
QSE_STR_LEN(buf) -= match.len + (be - me);
|
|
|
|
p->in.pos -= (be - me);
|
2011-04-28 08:04:13 +00:00
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the match is at the ending boundary. switch to no match */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_readio (qse_awk_rtx_t* rtx, int in_type, const qse_char_t* name, qse_str_t* buf)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
|
|
|
qse_awk_rio_arg_t* p;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2011-05-01 09:24:38 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
qse_awk_val_t* rs;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t rrs;
|
2011-05-01 09:24:38 +00:00
|
|
|
|
|
|
|
qse_size_t line_len = 0;
|
|
|
|
qse_char_t c = QSE_T('\0'), pc;
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (find_rio_in(rtx, in_type, name, &p, &handler) <= -1) return -1;
|
2011-05-01 09:24:38 +00:00
|
|
|
if (p->in.eos) return 0; /* no more streams left */
|
|
|
|
|
|
|
|
/* ready to read a record(typically a line). clear the buffer. */
|
|
|
|
qse_str_clear (buf);
|
|
|
|
|
|
|
|
/* get the record separator */
|
2019-06-30 09:26:33 +00:00
|
|
|
rs = qse_awk_rtx_getgbl(rtx, QSE_AWK_GBL_RS);
|
|
|
|
qse_awk_rtx_refupval (rtx, rs);
|
2011-05-01 09:24:38 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (resolve_rs(rtx, rs, &rrs) <= -1)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, rs);
|
2011-05-01 09:24:38 +00:00
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
/* call the I/O handler */
|
2007-05-02 01:07:00 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (p->in.pos >= p->in.len)
|
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
qse_ssize_t x;
|
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
/* no more data in the read buffer.
|
|
|
|
* let the I/O handler read more */
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (p->in.eof)
|
|
|
|
{
|
2011-05-04 01:48:42 +00:00
|
|
|
/* it has reached EOF at the previous call. */
|
|
|
|
if (QSE_STR_LEN(buf) == 0)
|
|
|
|
{
|
|
|
|
/* we return EOF if the record buffer is empty */
|
|
|
|
ret = 0;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
x = handler(rtx, QSE_AWK_RIO_READ, p, p->in.buf, QSE_COUNTOF(p->in.buf));
|
2011-05-01 09:24:38 +00:00
|
|
|
if (x <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* if the error number has not been
|
2011-05-04 01:48:42 +00:00
|
|
|
* set by the user handler, we set
|
|
|
|
* it here to QSE_AWK_EIOIMPL. */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
if (x == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-04-28 08:04:13 +00:00
|
|
|
/* EOF reached */
|
2009-08-28 06:52:20 +00:00
|
|
|
p->in.eof = 1;
|
2007-05-20 01:45:00 +00:00
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
if (QSE_STR_LEN(buf) == 0)
|
|
|
|
{
|
|
|
|
/* We can return EOF now if the record buffer
|
|
|
|
* is empty */
|
|
|
|
ret = 0;
|
|
|
|
}
|
2012-09-10 09:55:22 +00:00
|
|
|
else if (rrs.ptr && rrs.len == 0)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
2011-05-02 08:37:17 +00:00
|
|
|
/* TODO: handle different line terminator */
|
2011-05-01 09:24:38 +00:00
|
|
|
/* drop the line terminator from the record
|
|
|
|
* if RS is a blank line and EOF is reached. */
|
|
|
|
if (QSE_STR_LASTCHAR(buf) == QSE_T('\n'))
|
2011-05-02 08:37:17 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
QSE_STR_LEN(buf) -= 1;
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->awk->opt.trait & QSE_AWK_CRLF)
|
2011-05-02 08:37:17 +00:00
|
|
|
{
|
|
|
|
/* drop preceding CR */
|
2019-06-30 09:26:33 +00:00
|
|
|
if (QSE_STR_LEN(buf) > 0 && QSE_STR_LASTCHAR(buf) == QSE_T('\r')) QSE_STR_LEN(buf) -= 1;
|
2011-05-02 08:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
}
|
|
|
|
else if (rrs.len >= 2)
|
2007-05-20 01:45:00 +00:00
|
|
|
{
|
2011-04-28 08:04:13 +00:00
|
|
|
/* When RS is multiple characters, it should
|
|
|
|
* check for the match at the end of the
|
|
|
|
* input stream also because the previous
|
|
|
|
* match could fail as it didn't end at the
|
|
|
|
* desired position to be the longest match.
|
|
|
|
* At EOF, the match at the end is considered
|
|
|
|
* the longest as there are no more characters
|
|
|
|
* left */
|
2019-06-30 09:26:33 +00:00
|
|
|
int n = match_long_rs(rtx, buf, p);
|
2011-04-28 08:04:13 +00:00
|
|
|
if (n != 0)
|
2007-05-20 01:45:00 +00:00
|
|
|
{
|
2011-04-28 08:04:13 +00:00
|
|
|
if (n <= -1) ret = -1;
|
2007-05-20 01:45:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
p->in.len = x;
|
2007-05-02 01:07:00 +00:00
|
|
|
p->in.pos = 0;
|
|
|
|
}
|
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
if (rrs.ptr == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
qse_size_t start_pos = p->in.pos;
|
|
|
|
qse_size_t end_pos, tmp;
|
|
|
|
|
|
|
|
do
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
pc = c;
|
|
|
|
c = p->in.buf[p->in.pos++];
|
|
|
|
end_pos = p->in.pos;
|
|
|
|
|
2011-05-02 08:37:17 +00:00
|
|
|
/* TODO: handle different line terminator */
|
2011-05-01 09:24:38 +00:00
|
|
|
/* separate by a new line */
|
|
|
|
if (c == QSE_T('\n'))
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
end_pos--;
|
|
|
|
if (pc == QSE_T('\r'))
|
|
|
|
{
|
|
|
|
if (end_pos > start_pos)
|
|
|
|
{
|
2011-05-02 08:37:17 +00:00
|
|
|
/* CR is the part of the read buffer.
|
2011-05-01 09:24:38 +00:00
|
|
|
* decrementing the end_pos variable can
|
|
|
|
* simply drop it */
|
|
|
|
end_pos--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-02 08:37:17 +00:00
|
|
|
/* CR must have come from the previous
|
|
|
|
* read. drop CR that must be found at
|
|
|
|
* the end of the record buffer. */
|
2011-05-01 09:24:38 +00:00
|
|
|
QSE_ASSERT (end_pos == start_pos);
|
|
|
|
QSE_ASSERT (QSE_STR_LEN(buf) > 0);
|
|
|
|
QSE_ASSERT (QSE_STR_LASTCHAR(buf) == QSE_T('\r'));
|
|
|
|
QSE_STR_LEN(buf)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
}
|
|
|
|
while (p->in.pos < p->in.len);
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
tmp = qse_str_ncat(buf, &p->in.buf[start_pos], end_pos - start_pos);
|
2011-05-01 09:24:38 +00:00
|
|
|
if (tmp == (qse_size_t)-1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-01 09:24:38 +00:00
|
|
|
ret = -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
|
|
|
|
if (end_pos < p->in.len) break; /* RS found */
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
else if (rrs.len == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
do
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
pc = c;
|
|
|
|
c = p->in.buf[p->in.pos++];
|
|
|
|
|
2011-05-02 08:37:17 +00:00
|
|
|
/* TODO: handle different line terminator */
|
2011-05-01 09:24:38 +00:00
|
|
|
/* separate by a blank line */
|
|
|
|
if (c == QSE_T('\n'))
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (pc == QSE_T('\r') && QSE_STR_LEN(buf) > 0)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
/* shrink the line length and the record
|
|
|
|
* by dropping of CR before NL */
|
|
|
|
QSE_ASSERT (line_len > 0);
|
|
|
|
line_len--;
|
2011-05-02 08:37:17 +00:00
|
|
|
|
|
|
|
/* we don't drop CR from the record buffer
|
|
|
|
* if we're in CRLF mode. POINT-X */
|
2019-06-30 09:26:33 +00:00
|
|
|
if (!(rtx->awk->opt.trait & QSE_AWK_CRLF))
|
2011-05-02 08:37:17 +00:00
|
|
|
QSE_STR_LEN(buf) -= 1;
|
2011-05-01 09:24:38 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 07:28:51 +00:00
|
|
|
if (line_len == 0)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
/* we got a blank line */
|
2011-05-02 08:37:17 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->awk->opt.trait & QSE_AWK_CRLF)
|
2011-05-02 07:28:51 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (QSE_STR_LEN(buf) > 0 && QSE_STR_LASTCHAR(buf) == QSE_T('\r'))
|
2011-05-02 08:37:17 +00:00
|
|
|
{
|
|
|
|
/* drop CR not dropped in POINT-X above */
|
|
|
|
QSE_STR_LEN(buf) -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (QSE_STR_LEN(buf) <= 0)
|
|
|
|
{
|
|
|
|
/* if the record is empty when a blank
|
|
|
|
* line is encountered, the line
|
|
|
|
* terminator should not be added to
|
|
|
|
* the record */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drop NL */
|
|
|
|
QSE_STR_LEN(buf) -= 1;
|
|
|
|
|
|
|
|
/* drop preceding CR */
|
2019-06-30 09:26:33 +00:00
|
|
|
if (QSE_STR_LEN(buf) > 0 && QSE_STR_LASTCHAR(buf) == QSE_T('\r')) QSE_STR_LEN(buf) -= 1;
|
2011-05-02 08:37:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (QSE_STR_LEN(buf) <= 0)
|
|
|
|
{
|
|
|
|
/* if the record is empty when a blank
|
|
|
|
* line is encountered, the line
|
|
|
|
* terminator should not be added to
|
|
|
|
* the record */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drop NL of the previous line */
|
|
|
|
QSE_STR_LEN(buf) -= 1; /* simply drop NL */
|
2011-05-02 07:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done = 1;
|
|
|
|
break;
|
2011-05-01 09:24:38 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 07:28:51 +00:00
|
|
|
line_len = 0;
|
2011-05-01 09:24:38 +00:00
|
|
|
}
|
2011-05-02 07:28:51 +00:00
|
|
|
else line_len++;
|
2011-05-01 09:24:38 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
if (qse_str_ccat(buf, c) == (qse_size_t)-1)
|
2011-05-01 09:24:38 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-01 09:24:38 +00:00
|
|
|
ret = -1;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
while (p->in.pos < p->in.len);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-01 09:24:38 +00:00
|
|
|
if (done) break;
|
|
|
|
}
|
|
|
|
else if (rrs.len == 1)
|
|
|
|
{
|
|
|
|
qse_size_t start_pos = p->in.pos;
|
|
|
|
qse_size_t end_pos, tmp;
|
|
|
|
|
|
|
|
do
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
c = p->in.buf[p->in.pos++];
|
|
|
|
end_pos = p->in.pos;
|
|
|
|
if (c == rrs.ptr[0])
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
end_pos--;
|
|
|
|
break;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
}
|
|
|
|
while (p->in.pos < p->in.len);
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
tmp = qse_str_ncat(buf, &p->in.buf[start_pos], end_pos - start_pos);
|
2011-05-01 09:24:38 +00:00
|
|
|
if (tmp == (qse_size_t)-1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-01 09:24:38 +00:00
|
|
|
ret = -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-01 09:24:38 +00:00
|
|
|
|
|
|
|
if (end_pos < p->in.len) break; /* RS found */
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-01 09:24:38 +00:00
|
|
|
qse_size_t tmp;
|
|
|
|
int n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-04-28 08:04:13 +00:00
|
|
|
/* if RS is composed of multiple characters,
|
|
|
|
* I perform the matching after having added the
|
|
|
|
* current character 'c' to the record buffer 'buf'
|
|
|
|
* to find the longest match. If a match found ends
|
|
|
|
* one character before this character just added
|
|
|
|
* to the buffer, it is the longest match.
|
|
|
|
*/
|
2011-05-01 09:24:38 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
tmp = qse_str_ncat(buf, &p->in.buf[p->in.pos], p->in.len - p->in.pos);
|
2011-05-01 09:24:38 +00:00
|
|
|
if (tmp == (qse_size_t)-1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-01 09:24:38 +00:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->in.pos = p->in.len;
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
n = match_long_rs(rtx, buf, p);
|
2011-04-28 08:04:13 +00:00
|
|
|
if (n != 0)
|
|
|
|
{
|
|
|
|
if (n <= -1) ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rrs.ptr && QSE_AWK_RTX_GETVALTYPE (rtx, rs) != QSE_AWK_VAL_STR) qse_awk_rtx_freemem (rtx, rrs.ptr);
|
|
|
|
qse_awk_rtx_refdownval (rtx, rs);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_writeioval (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, qse_awk_val_t* v)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-10-22 17:05:45 +00:00
|
|
|
qse_awk_val_type_t vtype;
|
2019-06-30 09:26:33 +00:00
|
|
|
vtype = QSE_AWK_RTX_GETVALTYPE (rtx, v);
|
2014-10-22 17:05:45 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
switch (vtype)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
case QSE_AWK_VAL_STR:
|
2019-06-30 09:26:33 +00:00
|
|
|
return qse_awk_rtx_writeiostr(rtx, out_type, name, ((qse_awk_val_str_t*)v)->val.ptr, ((qse_awk_val_str_t*)v)->val.len);
|
2009-03-01 09:27:03 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
case QSE_AWK_VAL_MBS:
|
2019-06-30 09:26:33 +00:00
|
|
|
return qse_awk_rtx_writeiobytes(rtx, out_type, name, ((qse_awk_val_mbs_t*)v)->val.ptr, ((qse_awk_val_mbs_t*)v)->val.len);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
qse_awk_rtx_valtostr_out_t out;
|
|
|
|
int n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
out.type = QSE_AWK_RTX_VALTOSTR_CPLDUP | QSE_AWK_RTX_VALTOSTR_PRINT;
|
2019-06-30 09:26:33 +00:00
|
|
|
if (qse_awk_rtx_valtostr(rtx, v, &out) <= -1) return -1;
|
|
|
|
n = qse_awk_rtx_writeiostr(rtx, out_type, name, out.u.cpldup.ptr, out.u.cpldup.len);
|
|
|
|
qse_awk_rtx_freemem (rtx, out.u.cpldup.ptr);
|
2019-04-21 05:44:53 +00:00
|
|
|
return n;
|
|
|
|
}
|
2014-10-22 17:05:45 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
struct write_io_data_t
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_awk_rio_arg_t* p;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2019-04-21 05:44:53 +00:00
|
|
|
};
|
|
|
|
typedef struct write_io_data_t write_io_data_t;
|
|
|
|
|
|
|
|
static int prepare_for_write_io_data (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, write_io_data_t* wid)
|
|
|
|
{
|
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain;
|
|
|
|
qse_awk_rio_impl_t handler;
|
|
|
|
int io_type, io_mode, io_mask, n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_type_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mode_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* translate the out_type into the relevant io type and mode */
|
|
|
|
io_type = out_type_map[out_type];
|
|
|
|
io_mode = out_mode_map[out_type];
|
|
|
|
io_mask = out_mask_map[out_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
2008-12-21 21:35:07 +00:00
|
|
|
if (handler == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-04 01:48:42 +00:00
|
|
|
/* no I/O handler provided */
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* look for the corresponding rio for name */
|
2013-01-16 05:04:56 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +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
|
2009-01-17 04:20:22 +00:00
|
|
|
* both QSE_AWK_OUT_FILE and QSE_AWK_OUT_APFILE are
|
2009-02-16 08:31:34 +00:00
|
|
|
* translated to QSE_AWK_RIO_FILE and it is used to
|
2007-05-02 01:07:00 +00:00
|
|
|
* keep track of file handles..
|
|
|
|
*
|
|
|
|
* print "1111" >> "1.tmp"
|
|
|
|
* print "1111" > "1.tmp"
|
|
|
|
*/
|
2019-04-21 05:44:53 +00:00
|
|
|
if (p->type == (io_type | io_mask) && qse_strcmp(p->name, name) == 0) break;
|
2007-05-02 01:07:00 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* if there is not corresponding rio for name, create one */
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
p = (qse_awk_rio_arg_t*)qse_awk_rtx_allocmem(rtx, QSE_SIZEOF(qse_awk_rio_arg_t));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-04-27 14:33:14 +00:00
|
|
|
|
|
|
|
QSE_MEMSET (p, 0, QSE_SIZEOF(*p));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
p->name = QSE_AWK_STRDUP(rtx->awk, name);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p->name == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
p->type = (io_type | io_mask);
|
|
|
|
p->mode = io_mode;
|
2009-08-29 03:48:02 +00:00
|
|
|
p->rwcmode = QSE_AWK_RIO_CLOSE_FULL;
|
2012-04-27 14:33:14 +00:00
|
|
|
/*
|
2008-12-21 21:35:07 +00:00
|
|
|
p->handle = QSE_NULL;
|
|
|
|
p->next = QSE_NULL;
|
2009-08-28 06:52:20 +00:00
|
|
|
p->rwcstate = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-28 06:52:20 +00:00
|
|
|
p->out.eof = 0;
|
|
|
|
p->out.eos = 0;
|
2012-04-27 14:33:14 +00:00
|
|
|
*/
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
n = handler(rtx, QSE_AWK_RIO_OPEN, p, QSE_NULL, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p->name);
|
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chain it */
|
2019-04-21 05:44:53 +00:00
|
|
|
p->next = rtx->rio.chain;
|
|
|
|
rtx->rio.chain = p;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
if (p->out.eos) return 0; /* no more streams */
|
|
|
|
if (p->out.eof) return 0; /* it has reached the end of the stream but this function has been recalled */
|
|
|
|
|
|
|
|
wid->handler = handler;
|
|
|
|
wid->p = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_awk_rtx_writeiostr (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, qse_char_t* str, qse_size_t len)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
write_io_data_t wid;
|
|
|
|
|
|
|
|
if ((x = prepare_for_write_io_data(rtx, out_type, name, &wid)) <= 0) return x;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
while (len > 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_ssize_t n;
|
|
|
|
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
n = wid.handler(rtx, QSE_AWK_RIO_WRITE, wid.p, str, len);
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
wid.p->out.eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= n;
|
|
|
|
str += n;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 05:44:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_awk_rtx_writeiobytes (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name, qse_mchar_t* str, qse_size_t len)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
write_io_data_t wid;
|
|
|
|
|
|
|
|
if ((x = prepare_for_write_io_data(rtx, out_type, name, &wid)) <= 0) return x;
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
while (len > 0)
|
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
qse_ssize_t n;
|
|
|
|
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
n = wid.handler(rtx, QSE_AWK_RIO_WRITE_BYTES, wid.p, str, len);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
2019-04-21 05:44:53 +00:00
|
|
|
wid.p->out.eof = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= n;
|
|
|
|
str += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_flushio (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2013-01-16 05:04:56 +00:00
|
|
|
int io_type, io_mode, io_mask;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_ssize_t n;
|
2009-08-28 06:52:20 +00:00
|
|
|
int ok = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_type_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mode_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
/* translate the out_type into the relevant I/O type and mode */
|
2009-02-16 08:31:34 +00:00
|
|
|
io_type = out_type_map[out_type];
|
2013-01-16 05:04:56 +00:00
|
|
|
io_mode = out_mode_map[out_type];
|
2009-02-16 08:31:34 +00:00
|
|
|
io_mask = out_mask_map[out_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
|
|
|
if (!handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-04 01:48:42 +00:00
|
|
|
/* no I/O handler provided */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* look for the corresponding rio for name */
|
2013-01-16 05:04:56 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2013-01-16 05:04:56 +00:00
|
|
|
/* without the check for io_mode and p->mode,
|
|
|
|
* QSE_AWK_OUT_FILE and QSE_AWK_OUT_APFILE matches the
|
|
|
|
* same entry since (io_type | io_mask) has the same value
|
|
|
|
* for both. */
|
|
|
|
if (p->type == (io_type | io_mask) && p->mode == io_mode &&
|
2008-12-21 21:35:07 +00:00
|
|
|
(name == QSE_NULL || qse_strcmp(p->name,name) == 0))
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
n = handler(rtx, QSE_AWK_RIO_FLUSH, p, QSE_NULL, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-28 06:52:20 +00:00
|
|
|
ok = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok) return 0;
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* there is no corresponding rio for name */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIONMNF, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_nextio_read (qse_awk_rtx_t* rtx, int in_type, const qse_char_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2009-02-16 08:31:34 +00:00
|
|
|
int io_type, /*io_mode,*/ io_mask;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_ssize_t n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_type_map));
|
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_mode_map));
|
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
/* translate the in_type into the relevant I/O type and mode */
|
2009-02-16 08:31:34 +00:00
|
|
|
io_type = in_type_map[in_type];
|
|
|
|
/*io_mode = in_mode_map[in_type];*/
|
|
|
|
io_mask = in_mask_map[in_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
|
|
|
if (!handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-04 01:48:42 +00:00
|
|
|
/* no I/O handler provided */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (p->type == (io_type | io_mask) && qse_strcmp(p->name,name) == 0) break;
|
2007-05-02 01:07:00 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (!p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* something is totally wrong */
|
2019-06-30 09:26:33 +00:00
|
|
|
QSE_ASSERT (!"should never happen - cannot find the relevant rio entry");
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINTERN, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->in.eos)
|
|
|
|
{
|
|
|
|
/* no more streams. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
n = handler(rtx, QSE_AWK_RIO_NEXT, p, QSE_NULL, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* the next stream cannot be opened.
|
2011-05-01 09:24:38 +00:00
|
|
|
* set the EOS flags so that the next call to nextio_read
|
2007-05-02 01:07:00 +00:00
|
|
|
* will return 0 without executing the handler */
|
2009-08-28 06:52:20 +00:00
|
|
|
p->in.eos = 1;
|
2007-11-11 00:30:00 +00:00
|
|
|
return 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* as the next stream has been opened successfully,
|
2011-05-01 09:24:38 +00:00
|
|
|
* the EOF flag should be cleared if set */
|
2009-08-28 06:52:20 +00:00
|
|
|
p->in.eof = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
/* also the previous input buffer must be reset */
|
|
|
|
p->in.pos = 0;
|
|
|
|
p->in.len = 0;
|
|
|
|
|
2007-11-11 00:30:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_nextio_write (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2009-02-16 08:31:34 +00:00
|
|
|
int io_type, /*io_mode,*/ io_mask;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_ssize_t n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_type_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mode_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
/* translate the out_type into the relevant I/O type and mode */
|
2009-02-16 08:31:34 +00:00
|
|
|
io_type = out_type_map[out_type];
|
|
|
|
/*io_mode = out_mode_map[out_type];*/
|
|
|
|
io_mask = out_mask_map[out_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
|
|
|
if (!handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-04 01:48:42 +00:00
|
|
|
/* no I/O handler provided */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (p->type == (io_type | io_mask) && qse_strcmp(p->name,name) == 0) break;
|
2007-05-02 01:07:00 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (!p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* something is totally wrong */
|
2009-02-16 08:31:34 +00:00
|
|
|
QSE_ASSERT (!"should never happen - cannot find the relevant rio entry");
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EINTERN, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->out.eos)
|
|
|
|
{
|
|
|
|
/* no more streams. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
n = handler(rtx, QSE_AWK_RIO_NEXT, p, QSE_NULL, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* the next stream cannot be opened.
|
2011-05-04 01:48:42 +00:00
|
|
|
* set the EOS flags so that the next call to nextio_write
|
2007-05-02 01:07:00 +00:00
|
|
|
* will return 0 without executing the handler */
|
2009-08-28 06:52:20 +00:00
|
|
|
p->out.eos = 1;
|
2007-11-11 00:30:00 +00:00
|
|
|
return 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* as the next stream has been opened successfully,
|
2011-05-04 01:48:42 +00:00
|
|
|
* the EOF flag should be cleared if set */
|
2009-08-28 06:52:20 +00:00
|
|
|
p->out.eof = 0;
|
2007-11-11 00:30:00 +00:00
|
|
|
return 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_closio_read (qse_awk_rtx_t* rtx, int in_type, const qse_char_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain, * px = QSE_NULL;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2009-02-16 08:31:34 +00:00
|
|
|
int io_type, /*io_mode,*/ io_mask;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_type_map));
|
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_mode_map));
|
|
|
|
QSE_ASSERT (in_type >= 0 && in_type <= QSE_COUNTOF(in_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-04 01:48:42 +00:00
|
|
|
/* translate the in_type into the relevant I/O type and mode */
|
2009-02-16 08:31:34 +00:00
|
|
|
io_type = in_type_map[in_type];
|
|
|
|
/*io_mode = in_mode_map[in_type];*/
|
|
|
|
io_mask = in_mask_map[in_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
|
|
|
if (!handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-04 01:48:42 +00:00
|
|
|
/* no I/O handler provided */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (p->type == (io_type | io_mask) && qse_strcmp(p->name, name) == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2019-06-30 09:26:33 +00:00
|
|
|
|
|
|
|
handler = rtx->rio.handler[p->type & IO_MASK_CLEAR];
|
|
|
|
if (handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (handler (rtx, QSE_AWK_RIO_CLOSE, p, QSE_NULL, 0) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
/* this is not a rtx-time error.*/
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (px) px->next = p->next;
|
|
|
|
else rtx->rio.chain = p->next;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p->name);
|
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
px = p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the name given is not found */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIONMNF, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_closio_write (qse_awk_rtx_t* rtx, int out_type, const qse_char_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain, * px = QSE_NULL;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2009-02-16 08:31:34 +00:00
|
|
|
int io_type, /*io_mode,*/ io_mask;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_type_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mode_map));
|
|
|
|
QSE_ASSERT (out_type >= 0 && out_type <= QSE_COUNTOF(out_mask_map));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* translate the out_type into the relevant io type and mode */
|
|
|
|
io_type = out_type_map[out_type];
|
|
|
|
/*io_mode = out_mode_map[out_type];*/
|
|
|
|
io_mask = out_mask_map[out_type];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[io_type];
|
|
|
|
if (!handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* no io handler provided */
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOUSER, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (p->type == (io_type | io_mask) && qse_strcmp(p->name, name) == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[p->type & IO_MASK_CLEAR];
|
|
|
|
if (handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
if (handler (rtx, QSE_AWK_RIO_CLOSE, p, QSE_NULL, 0) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (px) px->next = p->next;
|
|
|
|
else rtx->rio.chain = p->next;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p->name);
|
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
px = p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIONMNF, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
int qse_awk_rtx_closeio (qse_awk_rtx_t* rtx, const qse_char_t* name, const qse_char_t* opt)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-27 06:59:08 +00:00
|
|
|
qse_awk_rio_arg_t* p = rtx->rio.chain, * px = QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-01-16 05:04:56 +00:00
|
|
|
while (p)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* it handles the first that matches the given name
|
2009-02-16 08:31:34 +00:00
|
|
|
* regardless of the io type */
|
2008-12-21 21:35:07 +00:00
|
|
|
if (qse_strcmp (p->name, name) == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2009-08-29 03:48:02 +00:00
|
|
|
qse_awk_rio_rwcmode_t rwcmode = QSE_AWK_RIO_CLOSE_FULL;
|
2009-08-27 06:59:08 +00:00
|
|
|
|
2012-12-30 06:20:08 +00:00
|
|
|
if (opt)
|
2009-08-27 06:59:08 +00:00
|
|
|
{
|
|
|
|
if (opt[0] == QSE_T('r'))
|
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
if (p->type & IO_MASK_RDWR)
|
2009-08-28 06:52:20 +00:00
|
|
|
{
|
2009-08-29 03:48:02 +00:00
|
|
|
if (p->rwcstate != QSE_AWK_RIO_CLOSE_WRITE)
|
2009-08-28 06:52:20 +00:00
|
|
|
{
|
|
|
|
/* if the write end is not
|
|
|
|
* closed, let io handler close
|
|
|
|
* the read end only. */
|
2009-08-29 03:48:02 +00:00
|
|
|
rwcmode = QSE_AWK_RIO_CLOSE_READ;
|
2009-08-28 06:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-02 07:28:51 +00:00
|
|
|
else if (!(p->type & IO_MASK_READ)) goto skip;
|
2009-08-27 06:59:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QSE_ASSERT (opt[0] == QSE_T('w'));
|
2011-05-02 07:28:51 +00:00
|
|
|
if (p->type & IO_MASK_RDWR)
|
2009-08-28 06:52:20 +00:00
|
|
|
{
|
2009-08-29 03:48:02 +00:00
|
|
|
if (p->rwcstate != QSE_AWK_RIO_CLOSE_READ)
|
2009-08-28 06:52:20 +00:00
|
|
|
{
|
|
|
|
/* if the read end is not
|
|
|
|
* closed, let io handler close
|
|
|
|
* the write end only. */
|
2009-08-29 03:48:02 +00:00
|
|
|
rwcmode = QSE_AWK_RIO_CLOSE_WRITE;
|
2009-08-28 06:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-02 07:28:51 +00:00
|
|
|
else if (!(p->type & IO_MASK_WRITE)) goto skip;
|
2009-08-27 06:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 07:28:51 +00:00
|
|
|
handler = rtx->rio.handler[p->type & IO_MASK_CLEAR];
|
2019-06-30 09:26:33 +00:00
|
|
|
if (handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-27 06:59:08 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
2009-08-29 03:48:02 +00:00
|
|
|
p->rwcmode = rwcmode;
|
2019-06-30 09:26:33 +00:00
|
|
|
if (handler(rtx, QSE_AWK_RIO_CLOSE, p, QSE_NULL, 0) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* this is not a run-time error.*/
|
2009-08-27 06:59:08 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2009-08-28 06:52:20 +00:00
|
|
|
|
2011-05-02 07:28:51 +00:00
|
|
|
if (p->type & IO_MASK_RDWR)
|
2009-08-28 06:52:20 +00:00
|
|
|
{
|
2009-08-29 03:48:02 +00:00
|
|
|
p->rwcmode = rwcmode;
|
|
|
|
if (p->rwcstate == 0 && rwcmode != 0)
|
2009-08-28 06:52:20 +00:00
|
|
|
{
|
|
|
|
/* if either end has not been closed.
|
|
|
|
* return success without destroying
|
|
|
|
* the internal node. rwcstate keeps
|
|
|
|
* what has been successfully closed */
|
2009-08-29 03:48:02 +00:00
|
|
|
p->rwcstate = rwcmode;
|
2009-08-28 06:52:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (px != QSE_NULL) px->next = p->next;
|
2009-08-27 06:59:08 +00:00
|
|
|
else rtx->rio.chain = p->next;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, p->name);
|
|
|
|
qse_awk_rtx_freemem (rtx, p);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
skip:
|
2007-05-02 01:07:00 +00:00
|
|
|
px = p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIONMNF, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
void qse_awk_rtx_cleario (qse_awk_rtx_t* rtx)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-06-17 00:05:40 +00:00
|
|
|
qse_awk_rio_arg_t* next;
|
2012-10-20 15:58:20 +00:00
|
|
|
qse_awk_rio_impl_t handler;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_ssize_t n;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
while (rtx->rio.chain)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
handler = rtx->rio.handler[rtx->rio.chain->type & IO_MASK_CLEAR];
|
|
|
|
next = rtx->rio.chain->next;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
if (handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
|
|
|
rtx->rio.chain->rwcmode = 0;
|
|
|
|
n = handler(rtx, QSE_AWK_RIO_CLOSE, rtx->rio.chain, QSE_NULL, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2019-06-30 09:26:33 +00:00
|
|
|
if (rtx->errinf.num == QSE_AWK_ENOERR)
|
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_EIOIMPL, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
/* TODO: some warnings need to be shown??? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
qse_awk_rtx_freemem (rtx, rtx->rio.chain->name);
|
|
|
|
qse_awk_rtx_freemem (rtx, rtx->rio.chain);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2019-06-30 09:26:33 +00:00
|
|
|
rtx->rio.chain = next;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|