2009-12-22 06:29:52 +00:00
|
|
|
/*
|
2012-01-06 14:38:11 +00:00
|
|
|
* $Id$
|
2009-12-22 06:29:52 +00:00
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2009-12-22 06:29:52 +00:00
|
|
|
|
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.
|
2009-12-22 06:29:52 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
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.
|
2009-12-22 06:29:52 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "sed-prv.h"
|
2013-02-22 03:18:42 +00:00
|
|
|
#include <qse/sed/stdsed.h>
|
2009-12-22 06:29:52 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2016-04-28 15:29:28 +00:00
|
|
|
#include <qse/si/sio.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include "../cmn/mem-prv.h"
|
2009-12-22 06:29:52 +00:00
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
typedef struct xtn_in_t xtn_in_t;
|
|
|
|
struct xtn_in_t
|
|
|
|
{
|
|
|
|
qse_sed_iostd_t* ptr;
|
|
|
|
qse_sed_iostd_t* cur;
|
|
|
|
qse_size_t mempos;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct xtn_out_t xtn_out_t;
|
|
|
|
struct xtn_out_t
|
|
|
|
{
|
|
|
|
qse_sed_iostd_t* ptr;
|
|
|
|
qse_str_t* memstr;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct xtn_t xtn_t;
|
2009-12-22 06:29:52 +00:00
|
|
|
struct xtn_t
|
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
struct
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
xtn_in_t in;
|
2011-10-07 02:01:35 +00:00
|
|
|
qse_char_t last;
|
|
|
|
int newline_squeezed;
|
2011-10-03 01:25:23 +00:00
|
|
|
} s;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
xtn_in_t in;
|
|
|
|
xtn_out_t out;
|
2011-09-18 09:41:26 +00:00
|
|
|
} e;
|
2009-12-22 06:29:52 +00:00
|
|
|
};
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
#if defined(QSE_HAVE_INLINE)
|
|
|
|
static QSE_INLINE xtn_t* GET_XTN(qse_sed_t* sed) { return (xtn_t*)((qse_uint8_t*)qse_sed_getxtn(sed) - QSE_SIZEOF(xtn_t)); }
|
|
|
|
#else
|
|
|
|
#define GET_XTN(sed) ((xtn_t*)((qse_uint8_t*)qse_sed_getxtn(sed) - QSE_SIZEOF(xtn_t)))
|
|
|
|
#endif
|
2009-12-22 06:29:52 +00:00
|
|
|
|
2011-10-07 02:01:35 +00:00
|
|
|
static int int_to_str (qse_size_t val, qse_char_t* buf, qse_size_t buflen)
|
|
|
|
{
|
|
|
|
qse_size_t t;
|
|
|
|
qse_size_t rlen = 0;
|
|
|
|
|
|
|
|
t = val;
|
|
|
|
if (t == 0) rlen++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* non-zero values */
|
|
|
|
if (t < 0) { t = -t; rlen++; }
|
|
|
|
while (t > 0) { rlen++; t /= 10; }
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rlen >= buflen) return -1; /* buffer too small */
|
|
|
|
|
|
|
|
buf[rlen] = QSE_T('\0');
|
|
|
|
|
|
|
|
t = val;
|
|
|
|
if (t == 0) buf[0] = QSE_T('0');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (t < 0) t = -t;
|
|
|
|
|
|
|
|
/* fill in the buffer with digits */
|
|
|
|
while (t > 0)
|
|
|
|
{
|
|
|
|
buf[--rlen] = (qse_char_t)(t % 10) + QSE_T('0');
|
|
|
|
t /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert the negative sign if necessary */
|
|
|
|
if (val < 0) buf[--rlen] = QSE_T('-');
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:11:50 +00:00
|
|
|
qse_sed_t* qse_sed_openstd (qse_size_t xtnsize, qse_sed_errnum_t* errnum)
|
2010-07-25 06:43:26 +00:00
|
|
|
{
|
2015-06-12 13:11:50 +00:00
|
|
|
return qse_sed_openstdwithmmgr (QSE_MMGR_GETDFL(), xtnsize, errnum);
|
2010-07-25 06:43:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:11:50 +00:00
|
|
|
qse_sed_t* qse_sed_openstdwithmmgr (qse_mmgr_t* mmgr, qse_size_t xtnsize, qse_sed_errnum_t* errnum)
|
2009-12-22 06:29:52 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
qse_sed_t* sed;
|
2009-12-22 06:29:52 +00:00
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
sed = qse_sed_open (mmgr, QSE_SIZEOF(xtn_t) + xtnsize, errnum);
|
|
|
|
if (!sed) return QSE_NULL;
|
|
|
|
|
|
|
|
sed->_instsize += QSE_SIZEOF(xtn_t);
|
|
|
|
|
|
|
|
return sed;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
static int verify_iostd_in (qse_sed_t* sed, qse_sed_iostd_t in[])
|
2009-12-22 06:29:52 +00:00
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_size_t i;
|
|
|
|
|
|
|
|
if (in[0].type == QSE_SED_IOSTD_NULL)
|
|
|
|
{
|
|
|
|
/* if 'in' is specified, it must contains at least one
|
|
|
|
* valid entry */
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; in[i].type != QSE_SED_IOSTD_NULL; i++)
|
|
|
|
{
|
2013-01-12 16:46:12 +00:00
|
|
|
if (in[i].type != QSE_SED_IOSTD_FILE &&
|
2013-01-12 16:51:49 +00:00
|
|
|
in[i].type != QSE_SED_IOSTD_STR &&
|
|
|
|
in[i].type != QSE_SED_IOSTD_SIO)
|
2011-10-03 01:25:23 +00:00
|
|
|
{
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
static qse_sio_t* open_sio_file (qse_sed_t* sed, const qse_char_t* file, int flags)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
qse_sio_t* sio;
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
sio = qse_sio_open(qse_sed_getmmgr(sed), 0, file, flags);
|
2011-09-18 09:41:26 +00:00
|
|
|
if (sio == QSE_NULL)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2011-09-18 09:41:26 +00:00
|
|
|
ea.ptr = file;
|
|
|
|
ea.len = qse_strlen (file);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
return sio;
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
struct sio_std_name_t
|
2011-10-08 22:41:52 +00:00
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
const qse_char_t* ptr;
|
|
|
|
qse_size_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct sio_std_name_t sio_std_names[] =
|
|
|
|
{
|
|
|
|
{ QSE_T("stdin"), 5 },
|
|
|
|
{ QSE_T("stdout"), 6 },
|
|
|
|
{ QSE_T("stderr"), 6 }
|
2011-10-08 22:41:52 +00:00
|
|
|
};
|
|
|
|
|
2011-09-20 00:51:02 +00:00
|
|
|
static qse_sio_t* open_sio_std (qse_sed_t* sed, qse_sio_std_t std, int flags)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sio_t* sio;
|
|
|
|
|
2019-06-25 01:21:00 +00:00
|
|
|
sio = qse_sio_openstd(qse_sed_getmmgr(sed), 0, std, flags);
|
2011-09-20 00:51:02 +00:00
|
|
|
if (sio == QSE_NULL)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2012-01-06 14:38:11 +00:00
|
|
|
ea.ptr = sio_std_names[std].ptr;
|
|
|
|
ea.len = sio_std_names[std].len;
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
2011-09-20 00:51:02 +00:00
|
|
|
return sio;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_main_stream (
|
|
|
|
qse_sed_t* sed, qse_sed_io_arg_t* arg, qse_sed_iostd_t* io)
|
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
switch (io->type)
|
|
|
|
{
|
|
|
|
case QSE_SED_IOSTD_FILE:
|
|
|
|
qse_sio_close (arg->handle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_SED_IOSTD_STR:
|
|
|
|
/* nothing to do for input.
|
|
|
|
* i don't close xtn->e.out.memstr intentionally.
|
|
|
|
* i close this in qse_awk_execstd()
|
|
|
|
*/
|
|
|
|
break;
|
2012-02-25 14:52:26 +00:00
|
|
|
|
2013-01-12 16:51:49 +00:00
|
|
|
case QSE_SED_IOSTD_SIO:
|
|
|
|
/* nothing to do */
|
|
|
|
break;
|
|
|
|
|
2012-02-25 14:52:26 +00:00
|
|
|
default:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
2012-01-06 14:38:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
static int open_input_stream (
|
|
|
|
qse_sed_t* sed, qse_sed_io_arg_t* arg, qse_sed_iostd_t* io, xtn_in_t* base)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-09-18 09:41:26 +00:00
|
|
|
|
|
|
|
QSE_ASSERT (io != QSE_NULL);
|
|
|
|
switch (io->type)
|
|
|
|
{
|
|
|
|
case QSE_SED_IOSTD_FILE:
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
|
|
|
qse_sio_t* sio;
|
2013-01-13 09:04:54 +00:00
|
|
|
|
|
|
|
if (io->u.file.path == QSE_NULL ||
|
|
|
|
(io->u.file.path[0] == QSE_T('-') &&
|
|
|
|
io->u.file.path[1] == QSE_T('\0')))
|
|
|
|
{
|
|
|
|
sio = open_sio_std (
|
|
|
|
sed, QSE_SIO_STDIN,
|
|
|
|
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sio = open_sio_file (
|
|
|
|
sed, io->u.file.path,
|
|
|
|
QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
|
|
|
|
}
|
2011-09-20 00:51:02 +00:00
|
|
|
if (sio == QSE_NULL) return -1;
|
2012-01-06 14:38:11 +00:00
|
|
|
if (io->u.file.cmgr) qse_sio_setcmgr (sio, io->u.file.cmgr);
|
2011-09-20 00:51:02 +00:00
|
|
|
arg->handle = sio;
|
2011-09-18 09:41:26 +00:00
|
|
|
break;
|
2011-09-20 00:51:02 +00:00
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
case QSE_SED_IOSTD_STR:
|
2011-09-18 09:41:26 +00:00
|
|
|
/* don't store anything to arg->handle */
|
2011-10-03 01:25:23 +00:00
|
|
|
base->mempos = 0;
|
2011-09-18 09:41:26 +00:00
|
|
|
break;
|
|
|
|
|
2013-01-12 16:51:49 +00:00
|
|
|
case QSE_SED_IOSTD_SIO:
|
|
|
|
arg->handle = io->u.sio;
|
|
|
|
break;
|
|
|
|
|
2011-10-06 00:41:04 +00:00
|
|
|
default:
|
|
|
|
QSE_ASSERTX (
|
|
|
|
!"should never happen",
|
2012-01-06 14:38:11 +00:00
|
|
|
"io-type must be one of SIO,FILE,STR"
|
2011-10-06 00:41:04 +00:00
|
|
|
);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINTERN, QSE_NULL);
|
|
|
|
return -1;
|
2011-10-03 01:25:23 +00:00
|
|
|
}
|
|
|
|
|
2011-10-07 02:01:35 +00:00
|
|
|
|
|
|
|
if (base == &xtn->s.in)
|
|
|
|
{
|
|
|
|
/* reset script location */
|
|
|
|
if (io->type == QSE_SED_IOSTD_FILE)
|
|
|
|
{
|
2011-10-08 22:41:52 +00:00
|
|
|
qse_sed_setcompid (
|
|
|
|
sed,
|
2012-01-06 14:38:11 +00:00
|
|
|
((io->u.file.path == QSE_NULL)?
|
|
|
|
sio_std_names[QSE_SIO_STDIN].ptr: io->u.file.path)
|
2011-10-08 22:41:52 +00:00
|
|
|
);
|
2011-10-07 02:01:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qse_char_t buf[64];
|
2011-10-08 22:41:52 +00:00
|
|
|
|
|
|
|
/* format an identifier to be something like M#1, S#5 */
|
2012-01-06 14:38:11 +00:00
|
|
|
buf[0] = (io->type == QSE_SED_IOSTD_STR)? QSE_T('M'): QSE_T('S');
|
2011-10-07 02:01:35 +00:00
|
|
|
buf[1] = QSE_T('#');
|
2011-10-08 22:41:52 +00:00
|
|
|
int_to_str (io - xtn->s.in.ptr, &buf[2], QSE_COUNTOF(buf) - 2);
|
|
|
|
|
|
|
|
/* don't care about failure int_to_str() though it's not
|
|
|
|
* likely to happen */
|
|
|
|
qse_sed_setcompid (sed, buf);
|
2011-10-07 02:01:35 +00:00
|
|
|
}
|
|
|
|
sed->src.loc.line = 1;
|
|
|
|
sed->src.loc.colm = 1;
|
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
static int open_output_stream (qse_sed_t* sed, qse_sed_io_arg_t* arg, qse_sed_iostd_t* io)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-09-18 09:41:26 +00:00
|
|
|
|
|
|
|
QSE_ASSERT (io != QSE_NULL);
|
|
|
|
switch (io->type)
|
|
|
|
{
|
|
|
|
case QSE_SED_IOSTD_FILE:
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
|
|
|
qse_sio_t* sio;
|
2013-01-13 09:04:54 +00:00
|
|
|
if (io->u.file.path == QSE_NULL ||
|
|
|
|
(io->u.file.path[0] == QSE_T('-') &&
|
|
|
|
io->u.file.path[1] == QSE_T('\0')))
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2011-09-20 00:51:02 +00:00
|
|
|
sio = open_sio_std (
|
|
|
|
sed, QSE_SIO_STDOUT,
|
|
|
|
QSE_SIO_WRITE |
|
|
|
|
QSE_SIO_CREATE |
|
|
|
|
QSE_SIO_TRUNCATE |
|
2013-10-21 14:53:53 +00:00
|
|
|
QSE_SIO_IGNOREMBWCERR |
|
|
|
|
QSE_SIO_LINEBREAK
|
2011-09-20 00:51:02 +00:00
|
|
|
);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
sio = open_sio_file (
|
|
|
|
sed, io->u.file.path,
|
2011-09-18 09:41:26 +00:00
|
|
|
QSE_SIO_WRITE |
|
|
|
|
QSE_SIO_CREATE |
|
|
|
|
QSE_SIO_TRUNCATE |
|
|
|
|
QSE_SIO_IGNOREMBWCERR
|
|
|
|
);
|
|
|
|
}
|
2011-09-20 00:51:02 +00:00
|
|
|
if (sio == QSE_NULL) return -1;
|
2012-01-06 14:38:11 +00:00
|
|
|
if (io->u.file.cmgr) qse_sio_setcmgr (sio, io->u.file.cmgr);
|
2011-09-20 00:51:02 +00:00
|
|
|
arg->handle = sio;
|
2011-09-18 09:41:26 +00:00
|
|
|
break;
|
2011-09-20 00:51:02 +00:00
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
case QSE_SED_IOSTD_STR:
|
2011-09-18 09:41:26 +00:00
|
|
|
/* don't store anything to arg->handle */
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn->e.out.memstr = qse_str_open(qse_sed_getmmgr(sed), 0, 512);
|
2011-10-03 01:25:23 +00:00
|
|
|
if (xtn->e.out.memstr == QSE_NULL)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_ENOMEM, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2011-10-06 00:41:04 +00:00
|
|
|
|
2013-01-12 16:51:49 +00:00
|
|
|
case QSE_SED_IOSTD_SIO:
|
|
|
|
arg->handle = io->u.sio;
|
|
|
|
break;
|
|
|
|
|
2011-10-06 00:41:04 +00:00
|
|
|
default:
|
|
|
|
QSE_ASSERTX (
|
|
|
|
!"should never happen",
|
2012-01-06 14:38:11 +00:00
|
|
|
"io-type must be one of SIO,FILE,STR"
|
2011-10-06 00:41:04 +00:00
|
|
|
);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINTERN, QSE_NULL);
|
|
|
|
return -1;
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
static qse_ssize_t read_input_stream (
|
|
|
|
qse_sed_t* sed, qse_sed_io_arg_t* arg,
|
|
|
|
qse_char_t* buf, qse_size_t len, xtn_in_t* base)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-09-18 09:41:26 +00:00
|
|
|
qse_sed_iostd_t* io, * next;
|
|
|
|
void* old, * new;
|
|
|
|
qse_ssize_t n = 0;
|
|
|
|
|
|
|
|
if (len > QSE_TYPE_MAX(qse_ssize_t)) len = QSE_TYPE_MAX(qse_ssize_t);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
io = base->cur;
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2011-10-07 02:01:35 +00:00
|
|
|
if (base == &xtn->s.in && xtn->s.newline_squeezed)
|
|
|
|
{
|
|
|
|
xtn->s.newline_squeezed = 0;
|
|
|
|
goto open_next;
|
|
|
|
}
|
|
|
|
|
2011-09-18 09:41:26 +00:00
|
|
|
QSE_ASSERT (io != QSE_NULL);
|
2011-10-07 02:01:35 +00:00
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
if (io->type == QSE_SED_IOSTD_STR)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
n = 0;
|
2012-01-06 14:38:11 +00:00
|
|
|
while (base->mempos < io->u.str.len && n < len)
|
|
|
|
buf[n++] = io->u.str.ptr[base->mempos++];
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
2011-12-18 17:40:37 +00:00
|
|
|
else n = qse_sio_getstrn (arg->handle, buf, len);
|
2012-01-06 14:38:11 +00:00
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
if (n != 0)
|
|
|
|
{
|
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
if (io->type == QSE_SED_IOSTD_FILE)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2012-01-06 14:38:11 +00:00
|
|
|
if (io->u.file.path)
|
|
|
|
{
|
|
|
|
ea.ptr = io->u.file.path;
|
|
|
|
ea.len = qse_strlen (io->u.file.path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
|
|
|
|
ea.len = sio_std_names[QSE_SIO_STDIN].len;
|
|
|
|
}
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
}
|
2011-10-07 02:01:35 +00:00
|
|
|
else if (base == &xtn->s.in)
|
2011-10-05 00:08:12 +00:00
|
|
|
{
|
2011-10-07 02:01:35 +00:00
|
|
|
xtn->s.last = buf[n-1];
|
2011-10-05 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
/* ============================================= */
|
|
|
|
/* == end of file on the current input stream == */
|
|
|
|
/* ============================================= */
|
|
|
|
|
2011-10-07 02:01:35 +00:00
|
|
|
if (base == &xtn->s.in && xtn->s.last != QSE_T('\n'))
|
|
|
|
{
|
|
|
|
/* TODO: different line termination convension */
|
|
|
|
buf[0] = QSE_T('\n');
|
|
|
|
n = 1;
|
|
|
|
xtn->s.newline_squeezed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_next:
|
2011-10-03 01:25:23 +00:00
|
|
|
next = base->cur + 1;
|
2011-09-18 09:41:26 +00:00
|
|
|
if (next->type == QSE_SED_IOSTD_NULL)
|
|
|
|
{
|
|
|
|
/* no next stream available - return 0 */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = arg->handle;
|
|
|
|
|
|
|
|
/* try to open the next input stream */
|
2011-10-03 01:25:23 +00:00
|
|
|
if (open_input_stream (sed, arg, next, base) <= -1)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
/* failed to open the next input stream */
|
2011-10-03 01:25:23 +00:00
|
|
|
if (next->type == QSE_SED_IOSTD_FILE)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2012-01-06 14:38:11 +00:00
|
|
|
if (next->u.file.path)
|
|
|
|
{
|
|
|
|
ea.ptr = next->u.file.path;
|
|
|
|
ea.len = qse_strlen (next->u.file.path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
|
|
|
|
ea.len = sio_std_names[QSE_SIO_STDIN].len;
|
|
|
|
}
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
|
2011-09-18 09:41:26 +00:00
|
|
|
n = -1;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-07 02:01:35 +00:00
|
|
|
|
|
|
|
/* successfuly opened the next input stream */
|
2011-09-18 09:41:26 +00:00
|
|
|
new = arg->handle;
|
|
|
|
|
|
|
|
arg->handle = old;
|
2011-10-07 02:01:35 +00:00
|
|
|
|
|
|
|
/* close the previous stream */
|
2011-09-18 09:41:26 +00:00
|
|
|
close_main_stream (sed, arg, io);
|
|
|
|
|
|
|
|
arg->handle = new;
|
2011-10-03 01:30:44 +00:00
|
|
|
|
2011-10-07 02:01:35 +00:00
|
|
|
base->cur++;
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
static qse_ssize_t s_in (
|
|
|
|
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg,
|
|
|
|
qse_char_t* buf, qse_size_t len)
|
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-10-03 01:25:23 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case QSE_SED_IO_OPEN:
|
|
|
|
{
|
|
|
|
if (open_input_stream (sed, arg, xtn->s.in.cur, &xtn->s.in) <= -1) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_SED_IO_CLOSE:
|
|
|
|
{
|
|
|
|
close_main_stream (sed, arg, xtn->s.in.cur);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_SED_IO_READ:
|
|
|
|
{
|
|
|
|
return read_input_stream (sed, arg, buf, len, &xtn->s.in);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
QSE_ASSERTX (
|
|
|
|
!"should never happen",
|
|
|
|
"cmd must be one of OPEN,CLOSE,READ"
|
|
|
|
);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINTERN, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static qse_ssize_t x_in (
|
2009-12-22 06:29:52 +00:00
|
|
|
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg,
|
|
|
|
qse_char_t* buf, qse_size_t len)
|
|
|
|
{
|
|
|
|
qse_sio_t* sio;
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2009-12-22 06:29:52 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case QSE_SED_IO_OPEN:
|
|
|
|
{
|
|
|
|
if (arg->path == QSE_NULL)
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
/* no file specified. console stream */
|
|
|
|
if (xtn->e.in.ptr == QSE_NULL)
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
2013-01-13 09:04:54 +00:00
|
|
|
/* QSE_NULL passed into qse_sed_exec() for input */
|
2011-09-20 00:51:02 +00:00
|
|
|
sio = open_sio_std (
|
|
|
|
sed, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
|
|
|
|
if (sio == QSE_NULL) return -1;
|
|
|
|
arg->handle = sio;
|
|
|
|
}
|
2009-12-22 06:29:52 +00:00
|
|
|
else
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
if (open_input_stream (sed, arg, xtn->e.in.cur, &xtn->e.in) <= -1) return -1;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
sio = open_sio_file (sed, arg->path, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR);
|
2011-09-18 09:41:26 +00:00
|
|
|
if (sio == QSE_NULL) return -1;
|
|
|
|
arg->handle = sio;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_SED_IO_CLOSE:
|
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
if (arg->path == QSE_NULL)
|
|
|
|
{
|
|
|
|
/* main data stream */
|
2011-10-03 01:25:23 +00:00
|
|
|
if (xtn->e.in.ptr == QSE_NULL)
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sio_close (arg->handle);
|
|
|
|
else
|
2011-10-03 01:25:23 +00:00
|
|
|
close_main_stream (sed, arg, xtn->e.in.cur);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sio_close (arg->handle);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-22 06:29:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_SED_IO_READ:
|
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
if (arg->path == QSE_NULL)
|
2009-12-22 06:29:52 +00:00
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
/* main data stream */
|
2011-10-03 01:25:23 +00:00
|
|
|
if (xtn->e.in.ptr == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_ssize_t n;
|
2011-12-18 17:40:37 +00:00
|
|
|
n = qse_sio_getstrn (arg->handle, buf, len);
|
2011-10-03 01:25:23 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2012-01-06 14:38:11 +00:00
|
|
|
ea.ptr = sio_std_names[QSE_SIO_STDIN].ptr;
|
|
|
|
ea.len = sio_std_names[QSE_SIO_STDIN].len;
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
else
|
2011-10-03 01:25:23 +00:00
|
|
|
return read_input_stream (sed, arg, buf, len, &xtn->e.in);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_ssize_t n;
|
2011-12-18 17:40:37 +00:00
|
|
|
n = qse_sio_getstrn (arg->handle, buf, len);
|
2011-10-03 01:25:23 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2011-10-03 01:25:23 +00:00
|
|
|
ea.ptr = arg->path;
|
|
|
|
ea.len = qse_strlen (arg->path);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
return n;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2011-10-03 01:25:23 +00:00
|
|
|
QSE_ASSERTX (
|
|
|
|
!"should never happen",
|
|
|
|
"cmd must be one of OPEN,CLOSE,READ"
|
|
|
|
);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINTERN, QSE_NULL);
|
2009-12-22 06:29:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
static qse_ssize_t x_out (
|
2009-12-22 06:29:52 +00:00
|
|
|
qse_sed_t* sed, qse_sed_io_cmd_t cmd, qse_sed_io_arg_t* arg,
|
|
|
|
qse_char_t* dat, qse_size_t len)
|
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-09-18 09:41:26 +00:00
|
|
|
qse_sio_t* sio;
|
2009-12-22 06:29:52 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case QSE_SED_IO_OPEN:
|
|
|
|
{
|
|
|
|
if (arg->path == QSE_NULL)
|
|
|
|
{
|
2013-01-13 09:04:54 +00:00
|
|
|
/* main data stream */
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
if (xtn->e.out.ptr == QSE_NULL)
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
2013-01-13 09:04:54 +00:00
|
|
|
/* QSE_NULL passed into qse_sed_execstd() for output */
|
2011-09-20 00:51:02 +00:00
|
|
|
sio = open_sio_std (
|
|
|
|
sed, QSE_SIO_STDOUT,
|
|
|
|
QSE_SIO_WRITE |
|
|
|
|
QSE_SIO_CREATE |
|
|
|
|
QSE_SIO_TRUNCATE |
|
2013-10-21 14:53:53 +00:00
|
|
|
QSE_SIO_IGNOREMBWCERR |
|
|
|
|
QSE_SIO_LINEBREAK
|
2011-09-20 00:51:02 +00:00
|
|
|
);
|
|
|
|
if (sio == QSE_NULL) return -1;
|
|
|
|
arg->handle = sio;
|
|
|
|
}
|
2009-12-22 06:29:52 +00:00
|
|
|
else
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
if (open_output_stream (sed, arg, xtn->e.out.ptr) <= -1) return -1;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-13 09:04:54 +00:00
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
sio = open_sio_file (
|
2011-09-18 09:41:26 +00:00
|
|
|
sed, arg->path,
|
2009-12-22 06:29:52 +00:00
|
|
|
QSE_SIO_WRITE |
|
|
|
|
QSE_SIO_CREATE |
|
2011-09-12 06:44:56 +00:00
|
|
|
QSE_SIO_TRUNCATE |
|
|
|
|
QSE_SIO_IGNOREMBWCERR
|
2009-12-22 06:29:52 +00:00
|
|
|
);
|
2011-09-18 09:41:26 +00:00
|
|
|
if (sio == QSE_NULL) return -1;
|
|
|
|
arg->handle = sio;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_SED_IO_CLOSE:
|
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
if (arg->path == QSE_NULL)
|
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
if (xtn->e.out.ptr == QSE_NULL)
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sio_close (arg->handle);
|
|
|
|
else
|
2011-10-03 01:25:23 +00:00
|
|
|
close_main_stream (sed, arg, xtn->e.out.ptr);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-20 00:51:02 +00:00
|
|
|
qse_sio_close (arg->handle);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
2009-12-22 06:29:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_SED_IO_WRITE:
|
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
if (arg->path == QSE_NULL)
|
2009-12-22 06:29:52 +00:00
|
|
|
{
|
2011-09-18 09:41:26 +00:00
|
|
|
/* main data stream */
|
2012-01-06 14:38:11 +00:00
|
|
|
if (xtn->e.out.ptr == QSE_NULL)
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_ssize_t n;
|
2011-12-18 17:40:37 +00:00
|
|
|
n = qse_sio_putstrn (arg->handle, dat, len);
|
2011-10-03 01:25:23 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2012-01-06 14:38:11 +00:00
|
|
|
ea.ptr = sio_std_names[QSE_SIO_STDOUT].ptr;
|
|
|
|
ea.len = sio_std_names[QSE_SIO_STDOUT].len;
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
return n;
|
2011-09-20 00:51:02 +00:00
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
else
|
2009-12-22 06:29:52 +00:00
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_sed_iostd_t* io = xtn->e.out.ptr;
|
2012-01-06 14:38:11 +00:00
|
|
|
if (io->type == QSE_SED_IOSTD_STR)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
if (len > QSE_TYPE_MAX(qse_ssize_t)) len = QSE_TYPE_MAX(qse_ssize_t);
|
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
if (qse_str_ncat (xtn->e.out.memstr, dat, len) == (qse_size_t)-1)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_ENOMEM, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_ssize_t n;
|
2011-12-18 17:40:37 +00:00
|
|
|
n = qse_sio_putstrn (arg->handle, dat, len);
|
2011-10-03 01:25:23 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2012-01-06 14:38:11 +00:00
|
|
|
if (io->u.file.path)
|
|
|
|
{
|
|
|
|
ea.ptr = io->u.file.path;
|
|
|
|
ea.len = qse_strlen(io->u.file.path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ea.ptr = sio_std_names[QSE_SIO_STDOUT].ptr;
|
|
|
|
ea.len = sio_std_names[QSE_SIO_STDOUT].len;
|
|
|
|
}
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
return n;
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
else
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
qse_ssize_t n;
|
2011-12-18 17:40:37 +00:00
|
|
|
n = qse_sio_putstrn (arg->handle, dat, len);
|
2011-10-03 01:25:23 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2011-10-03 01:25:23 +00:00
|
|
|
ea.ptr = arg->path;
|
|
|
|
ea.len = qse_strlen(arg->path);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EIOFIL, &ea);
|
|
|
|
}
|
|
|
|
return n;
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2011-10-03 01:25:23 +00:00
|
|
|
QSE_ASSERTX (
|
|
|
|
!"should never happen",
|
|
|
|
"cmd must be one of OPEN,CLOSE,WRITE"
|
|
|
|
);
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINTERN, QSE_NULL);
|
2009-12-22 06:29:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 00:41:04 +00:00
|
|
|
int qse_sed_compstd (qse_sed_t* sed, qse_sed_iostd_t in[], qse_size_t* count)
|
2009-12-22 06:29:52 +00:00
|
|
|
{
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-10-06 00:41:04 +00:00
|
|
|
int ret;
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
if (in == QSE_NULL)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
/* it requires a valid array unlike qse_sed_execstd(). */
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (verify_iostd_in (sed, in) <= -1) return -1;
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
QSE_MEMSET (&xtn->s, 0, QSE_SIZEOF(xtn->s));
|
|
|
|
xtn->s.in.ptr = in;
|
|
|
|
xtn->s.in.cur = in;
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2011-10-06 00:41:04 +00:00
|
|
|
ret = qse_sed_comp (sed, s_in);
|
|
|
|
|
|
|
|
if (count) *count = xtn->s.in.cur - xtn->s.in.ptr;
|
|
|
|
|
|
|
|
return ret;
|
2011-10-03 01:25:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qse_sed_execstd (
|
|
|
|
qse_sed_t* sed, qse_sed_iostd_t in[], qse_sed_iostd_t* out)
|
|
|
|
{
|
|
|
|
int n;
|
2019-06-25 01:21:00 +00:00
|
|
|
xtn_t* xtn = GET_XTN(sed);
|
2011-10-03 01:25:23 +00:00
|
|
|
|
|
|
|
if (in && verify_iostd_in (sed, in) <= -1) return -1;
|
2011-09-18 09:41:26 +00:00
|
|
|
|
|
|
|
if (out)
|
|
|
|
{
|
2013-01-12 16:46:12 +00:00
|
|
|
if (out->type != QSE_SED_IOSTD_FILE &&
|
2013-01-12 16:51:49 +00:00
|
|
|
out->type != QSE_SED_IOSTD_STR &&
|
|
|
|
out->type != QSE_SED_IOSTD_SIO)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
|
|
|
qse_sed_seterrnum (sed, QSE_SED_EINVAL, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSE_MEMSET (&xtn->e, 0, QSE_SIZEOF(xtn->e));
|
2011-10-03 01:25:23 +00:00
|
|
|
xtn->e.in.ptr = in;
|
|
|
|
xtn->e.in.cur = in;
|
2013-01-13 09:04:54 +00:00
|
|
|
xtn->e.out.ptr = out;
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2011-10-03 01:25:23 +00:00
|
|
|
n = qse_sed_exec (sed, x_in, x_out);
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
if (out && out->type == QSE_SED_IOSTD_STR)
|
2011-09-18 09:41:26 +00:00
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
if (n >= 0)
|
|
|
|
{
|
|
|
|
QSE_ASSERT (xtn->e.out.memstr != QSE_NULL);
|
|
|
|
qse_str_yield (xtn->e.out.memstr, &out->u.str, 0);
|
|
|
|
}
|
|
|
|
if (xtn->e.out.memstr) qse_str_close (xtn->e.out.memstr);
|
2011-09-18 09:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2009-12-22 06:29:52 +00:00
|
|
|
}
|
2011-09-18 09:41:26 +00:00
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
int qse_sed_compstdfile (
|
|
|
|
qse_sed_t* sed, const qse_char_t* file, qse_cmgr_t* cmgr)
|
2011-10-14 22:57:41 +00:00
|
|
|
{
|
|
|
|
qse_sed_iostd_t in[2];
|
|
|
|
|
|
|
|
in[0].type = QSE_SED_IOSTD_FILE;
|
2012-01-06 14:38:11 +00:00
|
|
|
in[0].u.file.path = file;
|
|
|
|
in[0].u.file.cmgr = cmgr;
|
2011-10-14 22:57:41 +00:00
|
|
|
in[1].type = QSE_SED_IOSTD_NULL;
|
|
|
|
|
|
|
|
return qse_sed_compstd (sed, in, QSE_NULL);
|
|
|
|
}
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
int qse_sed_compstdstr (qse_sed_t* sed, const qse_char_t* script)
|
2011-10-14 22:57:41 +00:00
|
|
|
{
|
|
|
|
qse_sed_iostd_t in[2];
|
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
in[0].type = QSE_SED_IOSTD_STR;
|
2012-02-25 14:52:26 +00:00
|
|
|
in[0].u.str.ptr = (qse_char_t*)script;
|
2012-01-06 14:38:11 +00:00
|
|
|
in[0].u.str.len = qse_strlen(script);
|
2011-10-14 22:57:41 +00:00
|
|
|
in[1].type = QSE_SED_IOSTD_NULL;
|
|
|
|
|
|
|
|
return qse_sed_compstd (sed, in, QSE_NULL);
|
|
|
|
}
|
|
|
|
|
2014-07-08 14:30:42 +00:00
|
|
|
int qse_sed_compstdxstr (qse_sed_t* sed, const qse_cstr_t* script)
|
2014-02-19 15:24:33 +00:00
|
|
|
{
|
|
|
|
qse_sed_iostd_t in[2];
|
|
|
|
|
|
|
|
in[0].type = QSE_SED_IOSTD_STR;
|
2014-02-20 05:29:32 +00:00
|
|
|
in[0].u.str = *script;
|
2014-02-19 15:24:33 +00:00
|
|
|
in[1].type = QSE_SED_IOSTD_NULL;
|
|
|
|
|
|
|
|
return qse_sed_compstd (sed, in, QSE_NULL);
|
|
|
|
}
|
|
|
|
|
2011-09-20 00:51:02 +00:00
|
|
|
int qse_sed_execstdfile (
|
2012-01-06 14:38:11 +00:00
|
|
|
qse_sed_t* sed, const qse_char_t* infile,
|
|
|
|
const qse_char_t* outfile, qse_cmgr_t* cmgr)
|
2011-09-20 00:51:02 +00:00
|
|
|
{
|
|
|
|
qse_sed_iostd_t in[2];
|
|
|
|
qse_sed_iostd_t out;
|
|
|
|
qse_sed_iostd_t* pin = QSE_NULL, * pout = QSE_NULL;
|
|
|
|
|
|
|
|
if (infile)
|
|
|
|
{
|
|
|
|
in[0].type = QSE_SED_IOSTD_FILE;
|
2012-01-06 14:38:11 +00:00
|
|
|
in[0].u.file.path = infile;
|
|
|
|
in[0].u.file.cmgr = cmgr;
|
2011-09-20 00:51:02 +00:00
|
|
|
in[1].type = QSE_SED_IOSTD_NULL;
|
|
|
|
pin = in;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outfile)
|
|
|
|
{
|
|
|
|
out.type = QSE_SED_IOSTD_FILE;
|
2012-01-06 14:38:11 +00:00
|
|
|
out.u.file.path = outfile;
|
|
|
|
out.u.file.cmgr = cmgr;
|
2011-09-20 00:51:02 +00:00
|
|
|
pout = &out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return qse_sed_execstd (sed, pin, pout);
|
|
|
|
}
|
2014-02-20 05:29:32 +00:00
|
|
|
|
|
|
|
int qse_sed_execstdxstr (
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_sed_t* sed, const qse_cstr_t* instr,
|
|
|
|
qse_cstr_t* outstr, qse_cmgr_t* cmgr)
|
2014-02-20 05:29:32 +00:00
|
|
|
{
|
|
|
|
qse_sed_iostd_t in[2];
|
|
|
|
qse_sed_iostd_t out;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
in[0].type = QSE_SED_IOSTD_STR;
|
|
|
|
in[0].u.str = *instr;
|
|
|
|
in[1].type = QSE_SED_IOSTD_NULL;
|
|
|
|
|
|
|
|
out.type = QSE_SED_IOSTD_STR;
|
|
|
|
|
|
|
|
n = qse_sed_execstd (sed, in, &out);
|
|
|
|
|
|
|
|
if (n >= 0) *outstr = out.u.str;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|