hawk/hawk/lib/HawkStd.cpp

1408 lines
34 KiB
C++
Raw Normal View History

2019-12-13 04:29:58 +00:00
/*
* $Id$
*
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
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.
*/
#include <HawkStd.hpp>
2020-01-05 15:12:24 +00:00
#include <hawk-sio.h>
#include <hawk-pio.h>
#include <hawk-std.h> // for hawk_stdmodXXX() functions
2019-12-13 04:29:58 +00:00
#include "hawk-prv.h"
#include <stdlib.h>
#if !defined(HAWK_HAVE_CONFIG_H)
# if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
# define HAVE_POW
# define HAVE_FMOD
# endif
#endif
// TODO: remove the following definitions and find a way to share the similar definitions in std.c
#if defined(HAWK_ENABLE_LIBLTDL)
# define USE_LTDL
#elif defined(HAVE_DLFCN_H)
# define USE_DLFCN
#else
# error UNSUPPORTED DYNAMIC LINKER
#endif
/////////////////////////////////
2019-12-16 15:09:58 +00:00
HAWK_BEGIN_NAMESPACE(HAWK)
2019-12-13 04:29:58 +00:00
/////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// MmgrStd
//////////////////////////////////////////////////////////////////////////////
void* MmgrStd::allocMem (hawk_oow_t n) HAWK_CPP_NOEXCEPT
{
return ::malloc(n);
}
void* MmgrStd::reallocMem (void* ptr, hawk_oow_t n) HAWK_CPP_NOEXCEPT
{
return ::realloc(ptr, n);
}
void MmgrStd::freeMem (void* ptr) HAWK_CPP_NOEXCEPT
{
::free (ptr);
}
//////////////////////////////////////////////////////////////////////////////
// HawkStd
//////////////////////////////////////////////////////////////////////////////
HawkStd::ioattr_t HawkStd::default_ioattr;
2019-12-13 04:29:58 +00:00
static hawk_sio_t* open_sio (Hawk* awk, HawkStd::Run* run, const hawk_ooch_t* file, int flags)
2019-12-13 04:29:58 +00:00
{
hawk_sio_t* sio;
//sio = hawk_sio_open ((run? ((Hawk::awk_t*)*(Hawk*)*run)->mmgr: awk->getMmgr()), 0, file, flags);
2020-01-05 15:12:24 +00:00
sio = hawk_sio_open((run? (hawk_gem_t*)*run: (hawk_gem_t*)*awk), 0, file, flags);
2019-12-13 04:29:58 +00:00
if (sio == HAWK_NULL)
{
2020-01-05 15:12:24 +00:00
if (run)
{
const hawk_ooch_t* bem = hawk_rtx_backuperrmsg(*run);
run->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), file, bem);
}
else
{
const hawk_ooch_t* bem = hawk_backuperrmsg(*awk);
awk->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), file, bem);
}
2019-12-13 04:29:58 +00:00
}
return sio;
}
static hawk_sio_t* open_sio_std (Hawk* awk, HawkStd::Run* run, hawk_sio_std_t std, int flags)
2019-12-13 04:29:58 +00:00
{
hawk_sio_t* sio;
static const hawk_ooch_t* std_names[] =
2019-12-13 04:29:58 +00:00
{
HAWK_T("stdin"),
HAWK_T("stdout"),
HAWK_T("stderr"),
};
//sio = hawk_sio_openstd ((run? ((Hawk::awk_t*)*(Hawk*)*run)->mmgr: awk->getMmgr()), 0, std, flags);
2020-01-05 15:12:24 +00:00
sio = hawk_sio_openstd((run? (hawk_gem_t*)*run: (hawk_gem_t*)*awk), 0, std, flags);
2019-12-13 04:29:58 +00:00
if (sio == HAWK_NULL)
{
2020-01-05 15:12:24 +00:00
if (run)
{
const hawk_ooch_t* bem = hawk_rtx_backuperrmsg(*run);
run->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), std_names[std], bem);
}
else
{
const hawk_ooch_t* bem = hawk_backuperrmsg(*awk);
awk->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), std_names[std], bem);
}
2019-12-13 04:29:58 +00:00
}
return sio;
}
int HawkStd::open ()
2019-12-13 04:29:58 +00:00
{
int n = Hawk::open ();
if (n == -1) return n;
2020-01-05 15:12:24 +00:00
this->gbl_argc = this->addGlobal(HAWK_T("ARGC"));
this->gbl_argv = this->addGlobal(HAWK_T("ARGV"));
this->gbl_environ = this->addGlobal(HAWK_T("ENVIRON"));
if (this->gbl_argc <= -1 || this->gbl_argv <= -1 || this->gbl_environ <= -1) goto oops;
2019-12-13 04:29:58 +00:00
if (this->addFunction(HAWK_T("rand"), 1, 0, HAWK_T("math"), HAWK_NULL, 0) <= -1 ||
this->addFunction(HAWK_T("srand"), 1, 0, HAWK_T("math"), HAWK_NULL, 0) <= -1 ||
this->addFunction(HAWK_T("system"), 1, 0, HAWK_T("sys"), HAWK_NULL, 0) <= -1 ||
this->addFunction(HAWK_T("setioattr"), 3, 3, HAWK_NULL, (FunctionHandler)&HawkStd::setioattr, HAWK_RIO) <= -1 ||
this->addFunction(HAWK_T("getioattr"), 3, 3, HAWK_T("vvr"), (FunctionHandler)&HawkStd::getioattr, HAWK_RIO) <= -1)
2019-12-13 04:29:58 +00:00
{
goto oops;
}
if (!this->stdmod_up)
{
#if defined(USE_DLFCN)
if (hawk_setopt(awk, HAWK_MODPOSTFIX, HAWK_T(".so")) <= -1) goto oops;
#endif
2020-01-05 15:12:24 +00:00
if (hawk_stdmodstartup(this->awk) <= -1) goto oops;
2019-12-13 04:29:58 +00:00
this->stdmod_up = true;
}
this->cmgrtab_inited = false;
return 0;
oops:
Hawk::close ();
return -1;
}
void HawkStd::close ()
2019-12-13 04:29:58 +00:00
{
if (this->cmgrtab_inited)
{
hawk_htb_fini (&this->cmgrtab);
this->cmgrtab_inited = false;
}
clearConsoleOutputs ();
//
// HawkStd called hawk_stdmodstartup() after Hawk::open().
2019-12-13 04:29:58 +00:00
// It's logical to call hawk_stdmodshutdown() Hawk::close().
// but Hawk::close() still needs to call some module's fini and
// unload functions. So it must be done in HawkStd::uponClosing()
2019-12-13 04:29:58 +00:00
// which is called after modules have been unloaded but while
// the underlying awk object is still alive.
//
// See HawkStd::uponClosing() below.
2019-12-13 04:29:58 +00:00
//
//if (this->stdmod_up)
//{
// hawk_stdmodshutdown (this->awk);
// this->stdmod_up = false;
//}
//
Hawk::close ();
}
void HawkStd::uponClosing ()
2019-12-13 04:29:58 +00:00
{
if (this->stdmod_up)
{
hawk_stdmodshutdown (this->awk);
this->stdmod_up = false;
}
// chain up
Hawk::uponClosing ();
}
HawkStd::Run* HawkStd::parse (Source& in, Source& out)
2019-12-13 04:29:58 +00:00
{
Run* run = Hawk::parse(in, out);
if (this->cmgrtab_inited)
{
// if cmgrtab has already been initialized,
// just clear the contents regardless of
// parse() result.
hawk_htb_clear (&this->cmgrtab);
}
else if (run && (this->getTrait() & HAWK_RIO))
{
// it initialized cmgrtab only if HAWK_RIO is set.
// but if you call parse() multiple times while
// setting and unsetting HAWK_RIO in-between,
// cmgrtab can still be initialized when HAWK_RIO is not set.
if (hawk_htb_init(&this->cmgrtab, *this, 256, 70, HAWK_SIZEOF(hawk_ooch_t), 1) <= -1)
2019-12-13 04:29:58 +00:00
{
this->setError (HAWK_ENOMEM);
return HAWK_NULL;
}
hawk_htb_setstyle (&this->cmgrtab, hawk_get_htb_style(HAWK_HTB_STYLE_INLINE_KEY_COPIER));
this->cmgrtab_inited = true;
}
if (run && make_additional_globals(run) <= -1) return HAWK_NULL;
return run;
}
int HawkStd::build_argcv (Run* run)
2019-12-13 04:29:58 +00:00
{
Value argv (run);
for (hawk_oow_t i = 0; i < this->runarg.len; i++)
2019-12-13 04:29:58 +00:00
{
if (argv.setIndexedStr (
Value::IntIndex(i),
this->runarg.ptr[i].ptr,
this->runarg.ptr[i].len, true) <= -1) return -1;
}
run->setGlobal (this->gbl_argc, (int_t)this->runarg.len);
run->setGlobal (this->gbl_argv, argv);
return 0;
}
2020-01-05 15:12:24 +00:00
int HawkStd::build_environ (Run* run, env_char_t* envarr[])
2019-12-13 04:29:58 +00:00
{
Value v_env (run);
if (envarr)
{
2020-01-05 15:12:24 +00:00
env_char_t* eq;
2019-12-13 04:29:58 +00:00
hawk_ooch_t* kptr, * vptr;
hawk_oow_t klen, count;
hawk_rtx_t* rtx = *run;
2019-12-13 04:29:58 +00:00
for (count = 0; envarr[count]; count++)
{
#if ((defined(HAWK_STD_ENV_CHAR_IS_BCH) && defined(HAWK_OOCH_IS_BCH)) || \
(defined(HAWK_STD_ENV_CHAR_IS_UCH) && defined(HAWK_OOCH_IS_UCH)))
eq = hawk_find_oochar_in_oocstr(envarr[count], HAWK_T('='));
2019-12-13 04:29:58 +00:00
if (eq == HAWK_NULL || eq == envarr[count]) continue;
kptr = envarr[count];
klen = eq - envarr[count];
vptr = eq + 1;
#elif defined(HAWK_STD_ENV_CHAR_IS_BCH)
eq = hawk_find_bchar_in_bcstr(envarr[count], HAWK_BT('='));
2019-12-13 04:29:58 +00:00
if (eq == HAWK_NULL || eq == envarr[count]) continue;
*eq = HAWK_BT('\0');
kptr = hawk_rtx_dupbtoucstr(rtx, envarr[count], &klen, 1);
vptr = hawk_rtx_dupbtoucstr(rtx, eq + 1, HAWK_NULL, 1);
2019-12-13 04:29:58 +00:00
if (kptr == HAWK_NULL || vptr == HAWK_NULL)
{
if (kptr) hawk_rtx_freemem (rtx, kptr);
if (vptr) hawk_rtx_freemem (rtx, kptr);
2019-12-13 04:29:58 +00:00
/* mbstowcsdup() may fail for invalid encoding.
* so setting the error code to ENOMEM may not
* be really accurate */
this->setError (HAWK_ENOMEM);
return -1;
}
*eq = HAWK_BT('=');
#else
eq = hawk_find_uchar_in_ucstr(envarr[count], HAWK_UT('='));
2019-12-13 04:29:58 +00:00
if (eq == HAWK_NULL || eq == envarr[count]) continue;
*eq = HAWK_UT('\0');
kptr = hawk_rtx_duputobcstr(rtx, envarr[count], &klen);
vptr = hawk_rtx_duputobcstr(rtx, eq + 1, HAWK_NULL);
2019-12-13 04:29:58 +00:00
if (kptr == HAWK_NULL || vptr == HAWK_NULL)
{
#if ((defined(HAWK_STD_ENV_CHAR_IS_BCH) && defined(HAWK_OOCH_IS_BCH)) || \
(defined(HAWK_STD_ENV_CHAR_IS_UCH) && defined(HAWK_OOCH_IS_UCH)))
/* nothing to do */
#else
if (vptr) hawk_rtx_freemem (rtx, vptr);
if (kptr) hawk_rtx_freemem (rtx, kptr);
#endif
2019-12-13 04:29:58 +00:00
this->setError (HAWK_ENOMEM);
return -1;
}
*eq = HAWK_UT('=');
#endif
// numeric string
v_env.setIndexedStr (Value::Index(kptr, klen), vptr, true);
2019-12-13 04:29:58 +00:00
#if ((defined(HAWK_STD_ENV_CHAR_IS_BCH) && defined(HAWK_OOCH_IS_BCH)) || \
(defined(HAWK_STD_ENV_CHAR_IS_UCH) && defined(HAWK_OOCH_IS_UCH)))
2019-12-13 04:29:58 +00:00
/* nothing to do */
#else
if (vptr) hawk_rtx_freemem (rtx, vptr);
if (kptr) hawk_rtx_freemem (rtx, kptr);
2019-12-13 04:29:58 +00:00
#endif
}
}
return run->setGlobal (this->gbl_environ, v_env);
}
int HawkStd::make_additional_globals (Run* run)
2019-12-13 04:29:58 +00:00
{
/* TODO: use wenviron where it's available */
extern char **environ;
2020-01-05 15:12:24 +00:00
if (build_argcv(run) <= -1 ||
build_environ(run, environ) <= -1) return -1;
2019-12-13 04:29:58 +00:00
return 0;
}
hawk_cmgr_t* HawkStd::getiocmgr (const hawk_ooch_t* ioname)
2019-12-13 04:29:58 +00:00
{
HAWK_ASSERT (this->cmgrtab_inited == true);
#if defined(HAWK_OOCH_IS_UCH)
ioattr_t* ioattr = get_ioattr(ioname, hawk_count_oocstr(ioname));
2019-12-13 04:29:58 +00:00
if (ioattr) return ioattr->cmgr;
#endif
return HAWK_NULL;
}
HawkStd::ioattr_t* HawkStd::get_ioattr (const hawk_ooch_t* ptr, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
hawk_htb_pair_t* pair;
pair = hawk_htb_search (&this->cmgrtab, ptr, len);
if (pair == HAWK_NULL) return HAWK_NULL;
return (ioattr_t*)HAWK_HTB_VPTR(pair);
}
HawkStd::ioattr_t* HawkStd::find_or_make_ioattr (const hawk_ooch_t* ptr, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
hawk_htb_pair_t* pair;
pair = hawk_htb_search (&this->cmgrtab, ptr, len);
if (pair == HAWK_NULL)
{
pair = hawk_htb_insert (
&this->cmgrtab, (void*)ptr, len,
(void*)&HawkStd::default_ioattr,
HAWK_SIZEOF(HawkStd::default_ioattr));
2019-12-13 04:29:58 +00:00
if (pair == HAWK_NULL)
{
this->setError (HAWK_ENOMEM);
return HAWK_NULL;
}
}
return (ioattr_t*)HAWK_HTB_VPTR(pair);
}
static int timeout_code (const hawk_ooch_t* name)
{
if (hawk_comp_oocstr(name, HAWK_T("rtimeout"), 1) == 0) return 0;
if (hawk_comp_oocstr(name, HAWK_T("wtimeout"), 1) == 0) return 1;
if (hawk_comp_oocstr(name, HAWK_T("ctimeout"), 1) == 0) return 2;
if (hawk_comp_oocstr(name, HAWK_T("atimeout"), 1) == 0) return 3;
2019-12-13 04:29:58 +00:00
return -1;
}
int HawkStd::setioattr (
Run& run, Value& ret, Value* args, hawk_oow_t nargs,
const hawk_ooch_t* name, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
HAWK_ASSERT (this->cmgrtab_inited == true);
hawk_oow_t l[3];
const hawk_ooch_t* ptr[3];
2019-12-13 04:29:58 +00:00
ptr[0] = args[0].toStr(&l[0]);
ptr[1] = args[1].toStr(&l[1]);
ptr[2] = args[2].toStr(&l[2]);
if (hawk_find_oochar(ptr[0], l[0], HAWK_T('\0')) ||
hawk_find_oochar(ptr[1], l[1], HAWK_T('\0')) ||
hawk_find_oochar(ptr[2], l[2], HAWK_T('\0')))
2019-12-13 04:29:58 +00:00
{
return ret.setInt ((int_t)-1);
}
int tmout;
if ((tmout = timeout_code (ptr[1])) >= 0)
{
int_t lv;
flt_t fv;
int n;
n = args[2].getNum(&lv, &fv);
if (n <= -1) return -1;
ioattr_t* ioattr = find_or_make_ioattr (ptr[0], l[0]);
if (ioattr == HAWK_NULL) return -1;
if (n == 0)
{
ioattr->tmout[tmout].sec = lv;
ioattr->tmout[tmout].nsec = 0;
}
else
{
hawk_flt_t nsec;
ioattr->tmout[tmout].sec = (hawk_int_t)fv;
2019-12-13 04:29:58 +00:00
nsec = fv - ioattr->tmout[tmout].sec;
ioattr->tmout[tmout].nsec = HAWK_SEC_TO_NSEC(nsec);
}
return ret.setInt ((int_t)0);
}
#if defined(HAWK_OOCH_IS_UCH)
else if (hawk_comp_oocstr(ptr[1], HAWK_T("codepage"), 1) == 0 ||
hawk_comp_oocstr(ptr[1], HAWK_T("encoding"), 1) == 0)
2019-12-13 04:29:58 +00:00
{
ioattr_t* ioattr;
hawk_cmgr_t* cmgr;
if (ptr[2][0] == HAWK_T('\0')) cmgr = HAWK_NULL;
else
{
cmgr = hawk_get_cmgr_by_name(ptr[2]);
2019-12-13 04:29:58 +00:00
if (cmgr == HAWK_NULL) return ret.setInt ((int_t)-1);
}
ioattr = find_or_make_ioattr(ptr[0], l[0]);
2019-12-13 04:29:58 +00:00
if (ioattr == HAWK_NULL) return -1;
ioattr->cmgr = cmgr;
2020-01-04 05:21:00 +00:00
hawk_copy_oocstr (ioattr->cmgr_name, HAWK_COUNTOF(ioattr->cmgr_name), ptr[2]);
2019-12-13 04:29:58 +00:00
return 0;
}
#endif
else
{
// unknown attribute name
return ret.setInt ((int_t)-1);
}
}
int HawkStd::getioattr (
Run& run, Value& ret, Value* args, hawk_oow_t nargs,
const hawk_ooch_t* name, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
HAWK_ASSERT (this->cmgrtab_inited == true);
hawk_oow_t l[2];
const hawk_ooch_t* ptr[2];
2019-12-13 04:29:58 +00:00
ptr[0] = args[0].toStr(&l[0]);
ptr[1] = args[1].toStr(&l[1]);
int xx = -1;
/* ionames must not contains a null character */
if (hawk_find_oochar(ptr[0], l[0], HAWK_T('\0')) == HAWK_NULL &&
hawk_find_oochar(ptr[1], l[1], HAWK_T('\0')) == HAWK_NULL)
2019-12-13 04:29:58 +00:00
{
ioattr_t* ioattr = get_ioattr(ptr[0], l[0]);
if (ioattr == HAWK_NULL) ioattr = &HawkStd::default_ioattr;
2019-12-13 04:29:58 +00:00
int tmout;
if ((tmout = timeout_code(ptr[1])) >= 0)
{
if (ioattr->tmout[tmout].nsec == 0)
xx = args[2].setInt ((int_t)ioattr->tmout[tmout].sec);
else
xx = args[2].setFlt ((hawk_flt_t)ioattr->tmout[tmout].sec + HAWK_NSEC_TO_SEC((hawk_flt_t)ioattr->tmout[tmout].nsec));
}
#if defined(HAWK_OOCH_IS_UCH)
else if (hawk_comp_oocstr(ptr[1], HAWK_T("codepage"), 1) == 0 ||
hawk_comp_oocstr(ptr[1], HAWK_T("encoding"), 1) == 0)
2019-12-13 04:29:58 +00:00
{
xx = args[2].setStr (ioattr->cmgr_name);
}
#endif
}
// unknown attribute name or errors
return ret.setInt ((int_t)xx);
}
#if defined(ENABLE_NWIO)
int HawkStd::open_nwio (Pipe& io, int flags, void* nwad)
2019-12-13 04:29:58 +00:00
{
hawk_nwio_tmout_t tmout_buf;
hawk_nwio_tmout_t* tmout = HAWK_NULL;
const hawk_ooch_t* name = io.getName();
2020-01-05 15:12:24 +00:00
ioattr_t* ioattr = get_ioattr(name, hawk_count_oocstr(name));
2019-12-13 04:29:58 +00:00
if (ioattr)
{
tmout = &tmout_buf;
tmout->r = ioattr->tmout[0];
tmout->w = ioattr->tmout[1];
tmout->c = ioattr->tmout[2];
tmout->a = ioattr->tmout[3];
}
hawk_nwio_t* handle = hawk_nwio_open (
this->getMmgr(), 0, (hawk_nwad_t*)nwad,
flags | HAWK_NWIO_TEXT | HAWK_NWIO_IGNOREECERR |
HAWK_NWIO_REUSEADDR | HAWK_NWIO_READNORETRY | HAWK_NWIO_WRITENORETRY,
tmout
);
if (handle == HAWK_NULL) return -1;
#if defined(HAWK_OOCH_IS_UCH)
hawk_cmgr_t* cmgr = this->getiocmgr(io.getName());
if (cmgr) hawk_nwio_setcmgr (handle, cmgr);
#endif
io.setHandle ((void*)handle);
io.setUflags (1);
return 1;
}
#endif
2019-12-13 04:29:58 +00:00
int HawkStd::open_pio (Pipe& io)
2019-12-13 04:29:58 +00:00
{
Hawk::Pipe::Mode mode = io.getMode();
hawk_pio_t* pio = HAWK_NULL;
int flags = HAWK_PIO_TEXT | HAWK_PIO_SHELL | HAWK_PIO_IGNOREECERR;
switch (mode)
{
case Hawk::Pipe::READ:
/* TODO: should we specify ERRTOOUT? */
flags |= HAWK_PIO_READOUT |
HAWK_PIO_ERRTOOUT;
break;
case Hawk::Pipe::WRITE:
flags |= HAWK_PIO_WRITEIN;
break;
case Hawk::Pipe::RW:
flags |= HAWK_PIO_READOUT |
HAWK_PIO_ERRTOOUT |
HAWK_PIO_WRITEIN;
break;
}
pio = hawk_pio_open (
2020-01-06 16:18:33 +00:00
*this,
2019-12-13 04:29:58 +00:00
0,
io.getName(),
flags
);
if (pio == HAWK_NULL) return -1;
#if defined(HAWK_OOCH_IS_UCH)
hawk_cmgr_t* cmgr = this->getiocmgr(io.getName());
if (cmgr)
{
hawk_pio_setcmgr (pio, HAWK_PIO_IN, cmgr);
hawk_pio_setcmgr (pio, HAWK_PIO_OUT, cmgr);
hawk_pio_setcmgr (pio, HAWK_PIO_ERR, cmgr);
}
#endif
io.setHandle (pio);
io.setUflags (0);
return 1;
}
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
static int parse_rwpipe_uri (const hawk_ooch_t* uri, int* flags, hawk_nwad_t* nwad)
{
static struct
{
const hawk_ooch_t* prefix;
hawk_oow_t len;
int flags;
} x[] =
{
{ HAWK_T("tcp://"), 6, HAWK_NWIO_TCP },
{ HAWK_T("udp://"), 6, HAWK_NWIO_UDP },
{ HAWK_T("tcpd://"), 7, HAWK_NWIO_TCP | HAWK_NWIO_PASSIVE },
{ HAWK_T("udpd://"), 7, HAWK_NWIO_UDP | HAWK_NWIO_PASSIVE }
};
hawk_oow_t i;
2019-12-13 04:29:58 +00:00
for (i = 0; i < HAWK_COUNTOF(x); i++)
{
if (hawk_strzcmp (uri, x[i].prefix, x[i].len) == 0)
{
if (hawk_strtonwad (uri + x[i].len, nwad) <= -1) return -1;
*flags = x[i].flags;
return 0;
}
}
return -1;
}
#endif
2019-12-13 04:29:58 +00:00
int HawkStd::openPipe (Pipe& io)
2019-12-13 04:29:58 +00:00
{
2020-01-05 15:12:24 +00:00
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
int flags;
hawk_nwad_t nwad;
if (io.getMode() != Hawk::Pipe::RW ||
parse_rwpipe_uri(io.getName(), &flags, &nwad) <= -1)
2019-12-13 04:29:58 +00:00
{
2020-01-05 15:12:24 +00:00
return open_pio(io);
2019-12-13 04:29:58 +00:00
}
else
{
return open_nwio(io, flags, &nwad);
2019-12-13 04:29:58 +00:00
}
2020-01-05 15:12:24 +00:00
#else
return this->open_pio(io);
#endif
2019-12-13 04:29:58 +00:00
}
int HawkStd::closePipe (Pipe& io)
2019-12-13 04:29:58 +00:00
{
2020-01-05 15:12:24 +00:00
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
if (io.getUflags() > 0)
{
/* nwio can't honor partical close */
hawk_nwio_close ((hawk_nwio_t*)io.getHandle());
}
else
{
2020-01-05 15:12:24 +00:00
#endif
2019-12-13 04:29:58 +00:00
hawk_pio_t* pio = (hawk_pio_t*)io.getHandle();
if (io.getMode() == Hawk::Pipe::RW)
{
Pipe::CloseMode rwcopt = io.getCloseMode();
if (rwcopt == Hawk::Pipe::CLOSE_READ)
{
hawk_pio_end (pio, HAWK_PIO_IN);
return 0;
}
else if (rwcopt == Hawk::Pipe::CLOSE_WRITE)
{
hawk_pio_end (pio, HAWK_PIO_OUT);
return 0;
}
}
hawk_pio_close (pio);
2020-01-05 15:12:24 +00:00
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
}
2020-01-05 15:12:24 +00:00
#endif
2019-12-13 04:29:58 +00:00
return 0;
}
hawk_ooi_t HawkStd::readPipe (Pipe& io, hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
return (io.getUflags() > 0)?
hawk_nwio_read ((hawk_nwio_t*)io.getHandle(), buf, len):
hawk_pio_read ((hawk_pio_t*)io.getHandle(), HAWK_PIO_OUT, buf, len);
#else
return hawk_pio_read ((hawk_pio_t*)io.getHandle(), HAWK_PIO_OUT, buf, len);
#endif
2019-12-13 04:29:58 +00:00
}
hawk_ooi_t HawkStd::writePipe (Pipe& io, const hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
return (io.getUflags() > 0)?
hawk_nwio_write((hawk_nwio_t*)io.getHandle(), buf, len):
hawk_pio_write((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN, buf, len);
#else
return hawk_pio_write((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN, buf, len);
#endif
2019-12-13 04:29:58 +00:00
}
hawk_ooi_t HawkStd::writePipeBytes (Pipe& io, const hawk_bch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
return (io.getUflags() > 0)?
hawk_nwio_writebytes((hawk_nwio_t*)io.getHandle(), buf, len):
hawk_pio_writebytes((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN, buf, len);
#else
return hawk_pio_writebytes((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN, buf, len);
#endif
2019-12-13 04:29:58 +00:00
}
int HawkStd::flushPipe (Pipe& io)
2019-12-13 04:29:58 +00:00
{
#if defined(ENABLE_NWIO)
2019-12-13 04:29:58 +00:00
return (io.getUflags() > 0)?
hawk_nwio_flush((hawk_nwio_t*)io.getHandle()):
hawk_pio_flush((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN);
#else
return hawk_pio_flush((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN);
#endif
2019-12-13 04:29:58 +00:00
}
int HawkStd::openFile (File& io)
2019-12-13 04:29:58 +00:00
{
Hawk::File::Mode mode = io.getMode();
hawk_sio_t* sio = HAWK_NULL;
int flags = HAWK_SIO_IGNOREECERR;
switch (mode)
{
case Hawk::File::READ:
flags |= HAWK_SIO_READ;
break;
case Hawk::File::WRITE:
flags |= HAWK_SIO_WRITE |
HAWK_SIO_CREATE |
HAWK_SIO_TRUNCATE;
break;
case Hawk::File::APPEND:
flags |= HAWK_SIO_APPEND |
HAWK_SIO_CREATE;
break;
}
2020-01-06 16:18:33 +00:00
sio = hawk_sio_open(*this, 0, io.getName(), flags);
2019-12-13 04:29:58 +00:00
if (!sio) return -1;
#if defined(HAWK_OOCH_IS_UCH)
hawk_cmgr_t* cmgr = this->getiocmgr(io.getName());
if (cmgr) hawk_sio_setcmgr (sio, cmgr);
#endif
io.setHandle (sio);
return 1;
}
int HawkStd::closeFile (File& io)
2019-12-13 04:29:58 +00:00
{
hawk_sio_close ((hawk_sio_t*)io.getHandle());
return 0;
}
hawk_ooi_t HawkStd::readFile (File& io, hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_getoochars((hawk_sio_t*)io.getHandle(), buf, len);
}
hawk_ooi_t HawkStd::writeFile (File& io, const hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_putoochars((hawk_sio_t*)io.getHandle(), buf, len);
}
hawk_ooi_t HawkStd::writeFileBytes (File& io, const hawk_bch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_putbchars((hawk_sio_t*)io.getHandle(), buf, len);
}
int HawkStd::flushFile (File& io)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_flush((hawk_sio_t*)io.getHandle());
}
void HawkStd::setConsoleCmgr (const hawk_cmgr_t* cmgr)
2019-12-13 04:29:58 +00:00
{
this->console_cmgr = (hawk_cmgr_t*)cmgr;
}
const hawk_cmgr_t* HawkStd::getConsoleCmgr () const
2019-12-13 04:29:58 +00:00
{
return this->console_cmgr;
}
int HawkStd::addConsoleOutput (const hawk_ooch_t* arg, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
HAWK_ASSERT (awk != HAWK_NULL);
int n = this->ofile.add (awk, arg, len);
if (n <= -1) setError (HAWK_ENOMEM);
return n;
}
int HawkStd::addConsoleOutput (const hawk_ooch_t* arg)
2019-12-13 04:29:58 +00:00
{
return addConsoleOutput (arg, hawk_count_oocstr(arg));
2019-12-13 04:29:58 +00:00
}
void HawkStd::clearConsoleOutputs ()
2019-12-13 04:29:58 +00:00
{
this->ofile.clear (awk);
}
int HawkStd::open_console_in (Console& io)
2019-12-13 04:29:58 +00:00
{
2020-01-05 15:12:24 +00:00
hawk_rtx_t* rtx = (hawk_rtx_t*)io;
2019-12-13 04:29:58 +00:00
if (this->runarg.ptr == HAWK_NULL)
{
HAWK_ASSERT (this->runarg.len == 0 && this->runarg.capa == 0);
if (this->runarg_count == 0)
{
hawk_sio_t* sio;
sio = open_sio_std(
2019-12-13 04:29:58 +00:00
HAWK_NULL, io, HAWK_SIO_STDIN,
HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
if (sio == HAWK_NULL) return -1;
if (this->console_cmgr)
hawk_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
this->runarg_count++;
return 1;
}
return 0;
}
else
{
hawk_sio_t* sio;
const hawk_ooch_t* file;
hawk_val_t* argv;
hawk_htb_t* map;
hawk_htb_pair_t* pair;
hawk_ooch_t ibuf[128];
hawk_oow_t ibuflen;
hawk_val_t* v;
hawk_oocs_t as;
nextfile:
file = this->runarg.ptr[this->runarg_index].ptr;
if (file == HAWK_NULL)
{
/* no more input file */
if (this->runarg_count == 0)
{
/* all ARGVs are empty strings.
* so no console files were opened.
* open the standard input here.
*
* 'BEGIN { ARGV[1]=""; ARGV[2]=""; }
* { print $0; }' file1 file2
*/
sio = open_sio_std(
2019-12-13 04:29:58 +00:00
HAWK_NULL, io, HAWK_SIO_STDIN,
HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
if (sio == HAWK_NULL) return -1;
if (this->console_cmgr)
hawk_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
this->runarg_count++;
return 1;
}
return 0;
}
if (hawk_count_oocstr(file) != this->runarg.ptr[this->runarg_index].len)
2019-12-13 04:29:58 +00:00
{
2020-01-06 16:18:33 +00:00
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), this->runarg.ptr[this->runarg_index].len);
2019-12-13 04:29:58 +00:00
return -1;
}
/* handle special case when ARGV[x] has been altered.
* so from here down, the file name gotten from
* rxtn->c.in.files is not important and is overridden
* from ARGV.
* 'BEGIN { ARGV[1]="file3"; }
* { print $0; }' file1 file2
*/
argv = hawk_rtx_getgbl (rtx, this->gbl_argv);
HAWK_ASSERT (argv != HAWK_NULL);
HAWK_ASSERT (HAWK_RTX_GETVALTYPE (rtx, argv) == HAWK_VAL_MAP);
map = ((hawk_val_map_t*)argv)->map;
HAWK_ASSERT (map != HAWK_NULL);
// ok to find ARGV[this->runarg_index] as ARGV[0]
// has been skipped.
ibuflen = hawk_int_to_oocstr (this->runarg_index, 10, HAWK_NULL, ibuf, HAWK_COUNTOF(ibuf));
pair = hawk_htb_search (map, ibuf, ibuflen);
HAWK_ASSERT (pair != HAWK_NULL);
v = (hawk_val_t*)HAWK_HTB_VPTR(pair);
HAWK_ASSERT (v != HAWK_NULL);
2020-01-06 16:18:33 +00:00
as.ptr = hawk_rtx_getvaloocstr(rtx, v, &as.len);
2019-12-13 04:29:58 +00:00
if (as.ptr == HAWK_NULL) return -1;
if (as.len == 0)
{
/* the name is empty */
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
this->runarg_index++;
goto nextfile;
}
if (hawk_count_oocstr(as.ptr) < as.len)
2019-12-13 04:29:58 +00:00
{
/* the name contains one or more '\0' */
2020-01-06 16:18:33 +00:00
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), as.len);
2019-12-13 04:29:58 +00:00
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
return -1;
}
file = as.ptr;
if (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))
sio = open_sio_std(HAWK_NULL, io, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
2019-12-13 04:29:58 +00:00
else
sio = open_sio(HAWK_NULL, io, file, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
2019-12-13 04:29:58 +00:00
if (sio == HAWK_NULL)
{
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
return -1;
}
2020-01-06 16:18:33 +00:00
if (hawk_rtx_setfilename(rtx, file, hawk_count_oocstr(file)) <= -1)
2019-12-13 04:29:58 +00:00
{
hawk_sio_close (sio);
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
return -1;
}
hawk_rtx_freevaloocstr (rtx, v, as.ptr);
if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr);
io.setHandle (sio);
/* increment the counter of files successfully opened */
this->runarg_count++;
this->runarg_index++;
return 1;
}
}
int HawkStd::open_console_out (Console& io)
2019-12-13 04:29:58 +00:00
{
2020-01-05 15:12:24 +00:00
hawk_rtx_t* rtx = (hawk_rtx_t*)io;
2019-12-13 04:29:58 +00:00
if (this->ofile.ptr == HAWK_NULL)
{
HAWK_ASSERT (this->ofile.len == 0 && this->ofile.capa == 0);
if (this->ofile_count == 0)
{
hawk_sio_t* sio;
sio = open_sio_std(
2019-12-13 04:29:58 +00:00
HAWK_NULL, io, HAWK_SIO_STDOUT,
HAWK_SIO_WRITE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK);
if (sio == HAWK_NULL) return -1;
2020-01-05 15:12:24 +00:00
if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr);
2019-12-13 04:29:58 +00:00
io.setHandle (sio);
this->ofile_count++;
return 1;
}
return 0;
}
else
{
/* a temporary variable sio is used here not to change
* any fields of riod when the open operation fails */
hawk_sio_t* sio;
const hawk_ooch_t* file;
file = this->ofile.ptr[this->ofile_index].ptr;
if (file == HAWK_NULL)
{
/* no more input file */
return 0;
}
if (hawk_count_oocstr(file) != this->ofile.ptr[this->ofile_index].len)
2019-12-13 04:29:58 +00:00
{
2020-01-06 16:18:33 +00:00
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), this->ofile.ptr[this->ofile_index].len);
2019-12-13 04:29:58 +00:00
return -1;
}
if (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))
sio = open_sio_std(HAWK_NULL, io, HAWK_SIO_STDOUT, HAWK_SIO_WRITE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK);
2019-12-13 04:29:58 +00:00
else
sio = open_sio(HAWK_NULL, io, file, HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR);
2019-12-13 04:29:58 +00:00
if (sio == HAWK_NULL) return -1;
2020-01-05 15:12:24 +00:00
if (hawk_rtx_setofilename(rtx, file, hawk_count_oocstr(file)) == -1)
2019-12-13 04:29:58 +00:00
{
hawk_sio_close (sio);
return -1;
}
2020-01-05 15:12:24 +00:00
if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr);
2019-12-13 04:29:58 +00:00
io.setHandle (sio);
this->ofile_index++;
this->ofile_count++;
return 1;
}
}
int HawkStd::openConsole (Console& io)
2019-12-13 04:29:58 +00:00
{
Console::Mode mode = io.getMode();
if (mode == Console::READ)
{
this->runarg_count = 0;
this->runarg_index = 0;
if (this->runarg.len > 0)
{
// skip ARGV[0]
this->runarg_index++;
}
return open_console_in (io);
}
else
{
HAWK_ASSERT (mode == Console::WRITE);
this->ofile_count = 0;
this->ofile_index = 0;
return open_console_out (io);
}
}
int HawkStd::closeConsole (Console& io)
2019-12-13 04:29:58 +00:00
{
hawk_sio_close ((hawk_sio_t*)io.getHandle());
return 0;
}
hawk_ooi_t HawkStd::readConsole (Console& io, hawk_ooch_t* data, hawk_oow_t size)
2019-12-13 04:29:58 +00:00
{
hawk_ooi_t nn;
2019-12-13 04:29:58 +00:00
while ((nn = hawk_sio_getoochars((hawk_sio_t*)io.getHandle(),data,size)) == 0)
{
int n;
hawk_sio_t* sio = (hawk_sio_t*)io.getHandle();
n = open_console_in (io);
if (n == -1) return -1;
if (n == 0)
{
/* no more input console */
return 0;
}
if (sio) hawk_sio_close (sio);
}
return nn;
}
hawk_ooi_t HawkStd::writeConsole (Console& io, const hawk_ooch_t* data, hawk_oow_t size)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_putoochars((hawk_sio_t*)io.getHandle(), data, size);
}
hawk_ooi_t HawkStd::writeConsoleBytes (Console& io, const hawk_bch_t* data, hawk_oow_t size)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_putbchars((hawk_sio_t*)io.getHandle(), data, size);
}
int HawkStd::flushConsole (Console& io)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_flush ((hawk_sio_t*)io.getHandle());
}
int HawkStd::nextConsole (Console& io)
2019-12-13 04:29:58 +00:00
{
int n;
hawk_sio_t* sio = (hawk_sio_t*)io.getHandle();
n = (io.getMode() == Console::READ)?
open_console_in(io): open_console_out(io);
if (n == -1) return -1;
if (n == 0)
{
/* if there is no more file, keep the previous handle */
return 0;
}
if (sio) hawk_sio_close (sio);
return n;
}
// memory allocation primitives
void* HawkStd::allocMem (hawk_oow_t n)
2019-12-13 04:29:58 +00:00
{
return ::malloc (n);
}
void* HawkStd::reallocMem (void* ptr, hawk_oow_t n)
2019-12-13 04:29:58 +00:00
{
return ::realloc (ptr, n);
}
void HawkStd::freeMem (void* ptr)
2019-12-13 04:29:58 +00:00
{
::free (ptr);
}
// miscellaneous primitive
HawkStd::flt_t HawkStd::pow (flt_t x, flt_t y)
2019-12-13 04:29:58 +00:00
{
return hawk_stdmathpow (this->awk, x, y);
}
HawkStd::flt_t HawkStd::mod (flt_t x, flt_t y)
2019-12-13 04:29:58 +00:00
{
return hawk_stdmathmod (this->awk, x, y);
}
void* HawkStd::modopen (const mod_spec_t* spec)
2019-12-13 04:29:58 +00:00
{
void* h;
h = hawk_stdmodopen (this->awk, spec);
if (!h) this->retrieveError ();
return h;
}
void HawkStd::modclose (void* handle)
2019-12-13 04:29:58 +00:00
{
hawk_stdmodclose (this->awk, handle);
}
void* HawkStd::modgetsym (void* handle, const hawk_ooch_t* name)
2019-12-13 04:29:58 +00:00
{
void* s;
2020-01-06 16:18:33 +00:00
s = hawk_stdmodgetsym(this->awk, handle, name);
2019-12-13 04:29:58 +00:00
if (!s) this->retrieveError ();
return s;
}
int HawkStd::SourceFile::open (Data& io)
2019-12-13 04:29:58 +00:00
{
hawk_sio_t* sio;
if (io.isMaster())
{
// open the main source file.
if (this->name[0] == HAWK_T('-') && this->name[1] == HAWK_T('\0'))
{
if (io.getMode() == READ)
sio = open_sio_std(
2019-12-13 04:29:58 +00:00
io, HAWK_NULL, HAWK_SIO_STDIN,
HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
else
sio = open_sio_std(
2019-12-13 04:29:58 +00:00
io, HAWK_NULL, HAWK_SIO_STDOUT,
HAWK_SIO_WRITE | HAWK_SIO_CREATE |
HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK);
if (sio == HAWK_NULL) return -1;
}
else
{
sio = open_sio (
2019-12-13 04:29:58 +00:00
io, HAWK_NULL, this->name,
(io.getMode() == READ?
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
);
if (sio == HAWK_NULL) return -1;
}
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
io.setName (this->name);
}
else
{
// open an included file
const hawk_ooch_t* ioname, * file;
hawk_ooch_t fbuf[64];
hawk_ooch_t* dbuf = HAWK_NULL;
2019-12-13 04:29:58 +00:00
ioname = io.getName();
HAWK_ASSERT (ioname != HAWK_NULL);
file = ioname;
if (io.getPrevHandle())
{
const hawk_ooch_t* outer;
2020-01-06 16:18:33 +00:00
outer = hawk_sio_getpath((hawk_sio_t*)io.getPrevHandle());
2019-12-13 04:29:58 +00:00
if (outer)
{
const hawk_ooch_t* base;
2020-01-06 16:18:33 +00:00
base = hawk_get_base_name_oocstr(outer);
2019-12-13 04:29:58 +00:00
if (base != outer && ioname[0] != HAWK_T('/'))
{
hawk_oow_t tmplen, totlen, dirlen;
2019-12-13 04:29:58 +00:00
dirlen = base - outer;
totlen = hawk_count_oocstr(ioname) + dirlen;
2019-12-13 04:29:58 +00:00
if (totlen >= HAWK_COUNTOF(fbuf))
{
2020-01-05 15:12:24 +00:00
dbuf = (hawk_ooch_t*)HAWK_MMGR_ALLOC(
2019-12-13 04:29:58 +00:00
((Hawk*)io)->getMmgr(),
HAWK_SIZEOF(hawk_ooch_t) * (totlen + 1)
);
if (dbuf == HAWK_NULL)
{
((Hawk*)io)->setError (HAWK_ENOMEM);
return -1;
}
file = dbuf;
}
else file = fbuf;
tmplen = hawk_copy_oochars_to_oocstr_unlimited ((hawk_ooch_t*)file, outer, dirlen);
hawk_copy_oocstr_unlimited ((hawk_ooch_t*)file + tmplen, ioname);
2019-12-13 04:29:58 +00:00
}
}
}
sio = open_sio(
2019-12-13 04:29:58 +00:00
io, HAWK_NULL, file,
(io.getMode() == READ?
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
);
if (dbuf) HAWK_MMGR_FREE (((Hawk*)io)->getMmgr(), dbuf);
if (sio == HAWK_NULL) return -1;
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
}
io.setHandle (sio);
return 1;
}
int HawkStd::SourceFile::close (Data& io)
2019-12-13 04:29:58 +00:00
{
hawk_sio_t* sio = (hawk_sio_t*)io.getHandle();
hawk_sio_flush (sio);
hawk_sio_close (sio);
return 0;
}
hawk_ooi_t HawkStd::SourceFile::read (Data& io, hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_getoochars ((hawk_sio_t*)io.getHandle(), buf, len);
}
hawk_ooi_t HawkStd::SourceFile::write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
return hawk_sio_putoochars ((hawk_sio_t*)io.getHandle(), buf, len);
}
int HawkStd::SourceString::open (Data& io)
2019-12-13 04:29:58 +00:00
{
hawk_sio_t* sio;
if (io.getName() == HAWK_NULL)
{
// open the main source file.
// SourceString does not support writing.
if (io.getMode() == WRITE) return -1;
ptr = str;
}
else
{
// open an included file
const hawk_ooch_t* ioname, * file;
hawk_ooch_t fbuf[64];
hawk_ooch_t* dbuf = HAWK_NULL;
2019-12-13 04:29:58 +00:00
ioname = io.getName();
HAWK_ASSERT (ioname != HAWK_NULL);
file = ioname;
if (io.getPrevHandle())
{
const hawk_ooch_t* outer;
2020-01-05 15:12:24 +00:00
outer = hawk_sio_getpath((hawk_sio_t*)io.getPrevHandle());
2019-12-13 04:29:58 +00:00
if (outer)
{
const hawk_ooch_t* base;
2020-01-06 16:18:33 +00:00
base = hawk_get_base_name_oocstr(outer);
2019-12-13 04:29:58 +00:00
if (base != outer && ioname[0] != HAWK_T('/'))
{
hawk_oow_t tmplen, totlen, dirlen;
2019-12-13 04:29:58 +00:00
dirlen = base - outer;
totlen = hawk_count_oocstr(ioname) + dirlen;
2019-12-13 04:29:58 +00:00
if (totlen >= HAWK_COUNTOF(fbuf))
{
dbuf = (hawk_ooch_t*)HAWK_MMGR_ALLOC(
((Hawk*)io)->getMmgr(),
HAWK_SIZEOF(hawk_ooch_t) * (totlen + 1)
);
if (dbuf == HAWK_NULL)
{
((Hawk*)io)->setError (HAWK_ENOMEM);
return -1;
}
file = dbuf;
}
else file = fbuf;
2020-01-05 15:12:24 +00:00
tmplen = hawk_copy_oochars_to_oocstr_unlimited((hawk_ooch_t*)file, outer, dirlen);
hawk_copy_oocstr_unlimited ((hawk_ooch_t*)file + tmplen, ioname);
2019-12-13 04:29:58 +00:00
}
}
}
sio = open_sio(
2019-12-13 04:29:58 +00:00
io, HAWK_NULL, file,
(io.getMode() == READ?
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
);
if (dbuf) HAWK_MMGR_FREE (((Hawk*)io)->getMmgr(), dbuf);
if (sio == HAWK_NULL) return -1;
io.setHandle (sio);
}
return 1;
}
int HawkStd::SourceString::close (Data& io)
2019-12-13 04:29:58 +00:00
{
if (!io.isMaster()) hawk_sio_close ((hawk_sio_t*)io.getHandle());
return 0;
}
hawk_ooi_t HawkStd::SourceString::read (Data& io, hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
if (io.isMaster())
{
hawk_oow_t n = 0;
while (*ptr != HAWK_T('\0') && n < len) buf[n++] = *ptr++;
return n;
}
else
{
return hawk_sio_getoochars ((hawk_sio_t*)io.getHandle(), buf, len);
}
}
hawk_ooi_t HawkStd::SourceString::write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len)
2019-12-13 04:29:58 +00:00
{
if (io.isMaster())
{
return -1;
}
else
{
// in fact, this block will never be reached as
// there is no included file concept for deparsing
return hawk_sio_putoochars ((hawk_sio_t*)io.getHandle(), buf, len);
}
}
/////////////////////////////////
2019-12-16 15:09:58 +00:00
HAWK_END_NAMESPACE(HAWK)
2019-12-13 04:29:58 +00:00
/////////////////////////////////