2019-12-13 04:29:58 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2020-04-16 03:42:30 +00:00
|
|
|
Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved.
|
2019-12-13 04:29:58 +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.
|
|
|
|
*/
|
|
|
|
|
2022-06-04 15:00:13 +00:00
|
|
|
#include <Hawk.hpp>
|
2020-01-05 15:12:24 +00:00
|
|
|
#include <hawk-sio.h>
|
2020-01-06 08:53:31 +00:00
|
|
|
#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>
|
2022-06-10 15:23:30 +00:00
|
|
|
#include <sys/stat.h>
|
2019-12-13 04:29:58 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
#if defined(HAVE_CRT_EXTERNS_H)
|
|
|
|
# include <crt_externs.h> /* MacOSX/darwin. _NSGetEnviron() */
|
2020-12-04 05:57:23 +00:00
|
|
|
# define SYSTEM_ENVIRON (*(_NSGetEnviron()))
|
2020-01-08 03:14:41 +00:00
|
|
|
#else
|
|
|
|
extern char** environ;
|
2020-12-04 05:57:23 +00:00
|
|
|
# define SYSTEM_ENVIRON ::environ
|
2020-01-08 03:14:41 +00:00
|
|
|
#endif
|
2020-01-07 16:34:26 +00:00
|
|
|
|
2019-12-13 04:29:58 +00:00
|
|
|
/////////////////////////////////
|
2019-12-16 15:09:58 +00:00
|
|
|
HAWK_BEGIN_NAMESPACE(HAWK)
|
2019-12-13 04:29:58 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MmgrStd
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2020-04-16 06:11:07 +00:00
|
|
|
void* MmgrStd::allocMem (hawk_oow_t n) HAWK_CXX_NOEXCEPT
|
2020-01-05 04:32:14 +00:00
|
|
|
{
|
|
|
|
return ::malloc(n);
|
|
|
|
}
|
|
|
|
|
2020-04-16 06:11:07 +00:00
|
|
|
void* MmgrStd::reallocMem (void* ptr, hawk_oow_t n) HAWK_CXX_NOEXCEPT
|
2020-01-05 04:32:14 +00:00
|
|
|
{
|
|
|
|
return ::realloc(ptr, n);
|
|
|
|
}
|
|
|
|
|
2020-04-16 06:11:07 +00:00
|
|
|
void MmgrStd::freeMem (void* ptr) HAWK_CXX_NOEXCEPT
|
2020-01-05 04:32:14 +00:00
|
|
|
{
|
|
|
|
::free (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// HawkStd
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HawkStd::ioattr_t HawkStd::default_ioattr;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-07 16:34:26 +00:00
|
|
|
static hawk_sio_t* open_sio (Hawk* hawk, HawkStd::Run* run, const hawk_ooch_t* file, int flags)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
hawk_sio_t* sio;
|
|
|
|
|
2020-04-16 03:42:30 +00:00
|
|
|
//sio = hawk_sio_open ((run? ((Hawk::hawk_t*)*(Hawk*)*run)->mmgr: hawk->getMmgr()), 0, file, flags);
|
2020-01-07 16:34:26 +00:00
|
|
|
sio = hawk_sio_open((run? (hawk_gem_t*)*run: (hawk_gem_t*)*hawk), 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)
|
|
|
|
{
|
2020-01-07 16:34:26 +00:00
|
|
|
const hawk_ooch_t* bem = hawk_rtx_backuperrmsg((hawk_rtx_t*)*run);
|
|
|
|
//run->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), file, bem);
|
|
|
|
hawk_rtx_seterrfmt ((hawk_rtx_t*)*run, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), file, bem);
|
2020-01-05 15:12:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-07 16:34:26 +00:00
|
|
|
const hawk_ooch_t* bem = hawk_backuperrmsg((hawk_t*)*hawk);
|
|
|
|
//hawk->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), file, bem);
|
|
|
|
hawk_seterrfmt ((hawk_t*)*hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), file, bem);
|
2020-01-05 15:12:24 +00:00
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
return sio;
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:34:26 +00:00
|
|
|
static hawk_sio_t* open_sio_std (Hawk* hawk, HawkStd::Run* run, hawk_sio_std_t std, int flags)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
hawk_sio_t* sio;
|
2020-01-04 16:19:38 +00:00
|
|
|
static const hawk_ooch_t* std_names[] =
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
HAWK_T("stdin"),
|
|
|
|
HAWK_T("stdout"),
|
|
|
|
HAWK_T("stderr"),
|
|
|
|
};
|
|
|
|
|
2020-04-16 03:42:30 +00:00
|
|
|
//sio = hawk_sio_openstd ((run? ((Hawk::hawk_t*)*(Hawk*)*run)->mmgr: hawk->getMmgr()), 0, std, flags);
|
2020-01-07 16:34:26 +00:00
|
|
|
sio = hawk_sio_openstd((run? (hawk_gem_t*)*run: (hawk_gem_t*)*hawk), 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)
|
|
|
|
{
|
2020-01-07 16:34:26 +00:00
|
|
|
const hawk_ooch_t* bem = hawk_rtx_backuperrmsg((hawk_rtx_t*)*run);
|
|
|
|
//run->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), std_names[std], bem);
|
|
|
|
hawk_rtx_seterrfmt ((hawk_rtx_t*)*run, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), std_names[std], bem);
|
2020-01-05 15:12:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-07 16:34:26 +00:00
|
|
|
const hawk_ooch_t* bem = hawk_backuperrmsg((hawk_t*)*hawk);
|
2020-04-16 03:42:30 +00:00
|
|
|
//hawk->formatError (HAWK_EOPEN, HAWK_NULL, HAWK_T("unable to open %js - %js"), std_names[std], bem);
|
2020-01-07 16:34:26 +00:00
|
|
|
hawk_seterrfmt ((hawk_t*)*hawk, HAWK_NULL, HAWK_EOPEN, HAWK_T("unable to open %js - %js"), std_names[std], bem);
|
2020-01-05 15:12:24 +00:00
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
return sio;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::open ()
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-04-17 17:19:34 +00:00
|
|
|
int n = Hawk::open();
|
|
|
|
if (HAWK_UNLIKELY(n <= -1)) return n;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
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
|
|
|
|
2020-01-06 08:53:31 +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)
|
|
|
|
{
|
2020-04-15 15:12:28 +00:00
|
|
|
/*
|
2019-12-13 04:29:58 +00:00
|
|
|
#if defined(USE_DLFCN)
|
2020-04-15 08:38:41 +00:00
|
|
|
if (hawk_setopt(this->hawk, HAWK_OPT_MODPOSTFIX, HAWK_T(".so")) <= -1) goto oops;
|
2019-12-13 04:29:58 +00:00
|
|
|
#endif
|
2020-04-15 15:12:28 +00:00
|
|
|
*/
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-08 15:37:42 +00:00
|
|
|
if (hawk_stdmodstartup(this->hawk) <= -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;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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 ();
|
|
|
|
|
|
|
|
//
|
2020-01-05 04:32:14 +00:00
|
|
|
// 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
|
2020-01-05 04:32:14 +00:00
|
|
|
// 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
|
2020-04-16 03:42:30 +00:00
|
|
|
// the underlying hawk object is still alive.
|
2019-12-13 04:29:58 +00:00
|
|
|
//
|
2020-01-05 04:32:14 +00:00
|
|
|
// See HawkStd::uponClosing() below.
|
2019-12-13 04:29:58 +00:00
|
|
|
//
|
|
|
|
//if (this->stdmod_up)
|
|
|
|
//{
|
2020-01-08 15:37:42 +00:00
|
|
|
// hawk_stdmodshutdown (this->hawk);
|
2019-12-13 04:29:58 +00:00
|
|
|
// this->stdmod_up = false;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
|
|
|
|
Hawk::close ();
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void HawkStd::uponClosing ()
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
if (this->stdmod_up)
|
|
|
|
{
|
2020-01-08 15:37:42 +00:00
|
|
|
hawk_stdmodshutdown (this->hawk);
|
2019-12-13 04:29:58 +00:00
|
|
|
this->stdmod_up = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// chain up
|
|
|
|
Hawk::uponClosing ();
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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.
|
2020-01-06 08:53:31 +00:00
|
|
|
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;
|
2020-03-03 08:03:04 +00:00
|
|
|
|
2019-12-13 04:29:58 +00:00
|
|
|
return run;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::build_argcv (Run* run)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
Value argv (run);
|
|
|
|
|
2020-01-04 16:19:38 +00:00
|
|
|
for (hawk_oow_t i = 0; i < this->runarg.len; i++)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-03-03 08:03:04 +00:00
|
|
|
if (argv.setIndexedStr(Value::IntIndex(i), this->runarg.ptr[i].ptr, this->runarg.ptr[i].len, true) <= -1) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 03:21:49 +00:00
|
|
|
run->setGlobal (this->gbl_argc, (hawk_int_t)this->runarg.len);
|
2019-12-13 04:29:58 +00:00
|
|
|
run->setGlobal (this->gbl_argv, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:12:24 +00:00
|
|
|
|
2020-01-06 08:53:31 +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;
|
2020-01-06 08:53:31 +00:00
|
|
|
hawk_rtx_t* rtx = *run;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
|
|
|
for (count = 0; envarr[count]; count++)
|
|
|
|
{
|
2020-01-06 08:53:31 +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)))
|
|
|
|
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;
|
2020-01-06 08:53:31 +00:00
|
|
|
#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');
|
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
2020-01-06 08:53:31 +00:00
|
|
|
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');
|
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-01-06 08:53:31 +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)))
|
|
|
|
/* 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
|
2020-01-06 08:53:31 +00:00
|
|
|
v_env.setIndexedStr (Value::Index(kptr, klen), vptr, true);
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-06 08:53:31 +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
|
2020-01-06 08:53:31 +00:00
|
|
|
if (vptr) hawk_rtx_freemem (rtx, vptr);
|
|
|
|
if (kptr) hawk_rtx_freemem (rtx, kptr);
|
2019-12-13 04:29:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 15:04:47 +00:00
|
|
|
return run->setGlobal(this->gbl_environ, v_env);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::make_additional_globals (Run* run)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
/* TODO: use wenviron where it's available */
|
|
|
|
|
2020-12-04 05:57:23 +00:00
|
|
|
if (this->build_argcv(run) <= -1 || this->build_environ(run, SYSTEM_ENVIRON) <= -1) return -1;
|
2020-03-03 08:03:04 +00:00
|
|
|
|
2019-12-13 04:29:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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)
|
2020-01-04 16:19:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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,
|
2020-01-05 04:32:14 +00:00
|
|
|
(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)
|
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::setioattr (
|
2020-01-04 16:19:38 +00:00
|
|
|
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);
|
2020-01-04 16:19:38 +00:00
|
|
|
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]);
|
|
|
|
|
2020-12-16 15:13:29 +00:00
|
|
|
if (hawk_find_oochar_in_oochars(ptr[0], l[0], '\0') ||
|
|
|
|
hawk_find_oochar_in_oochars(ptr[1], l[1], '\0') ||
|
|
|
|
hawk_find_oochar_in_oochars(ptr[2], l[2], '\0'))
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-08 03:21:49 +00:00
|
|
|
return ret.setInt ((hawk_int_t)-1);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tmout;
|
|
|
|
if ((tmout = timeout_code (ptr[1])) >= 0)
|
|
|
|
{
|
2020-01-08 03:21:49 +00:00
|
|
|
hawk_int_t lv;
|
|
|
|
hawk_flt_t fv;
|
2019-12-13 04:29:58 +00:00
|
|
|
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;
|
2020-01-06 08:53:31 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-01-08 03:21:49 +00:00
|
|
|
return ret.setInt ((hawk_int_t)0);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
cmgr = hawk_get_cmgr_by_name(ptr[2]);
|
2020-01-08 03:21:49 +00:00
|
|
|
if (cmgr == HAWK_NULL) return ret.setInt ((hawk_int_t)-1);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
2020-01-08 03:21:49 +00:00
|
|
|
return ret.setInt ((hawk_int_t)-1);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::getioattr (
|
2020-01-04 16:19:38 +00:00
|
|
|
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);
|
2020-01-04 16:19:38 +00:00
|
|
|
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 */
|
2020-12-16 15:13:29 +00:00
|
|
|
if (hawk_find_oochar_in_oochars(ptr[0], l[0], '\0') == HAWK_NULL &&
|
|
|
|
hawk_find_oochar_in_oochars(ptr[1], l[1], '\0') == HAWK_NULL)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
ioattr_t* ioattr = get_ioattr(ptr[0], l[0]);
|
2020-01-05 04:32:14 +00:00
|
|
|
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)
|
2020-01-08 03:21:49 +00:00
|
|
|
xx = args[2].setInt ((hawk_int_t)ioattr->tmout[tmout].sec);
|
2019-12-13 04:29:58 +00:00
|
|
|
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)
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
2020-01-08 03:21:49 +00:00
|
|
|
return ret.setInt ((hawk_int_t)xx);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
#if defined(ENABLE_NWIO)
|
2020-01-05 04:32:14 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-01-06 08:53:31 +00:00
|
|
|
#endif
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-05 04:32:14 +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? */
|
2020-01-07 16:34:26 +00:00
|
|
|
flags |= HAWK_PIO_READOUT | HAWK_PIO_ERRTOOUT;
|
2019-12-13 04:29:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Hawk::Pipe::WRITE:
|
|
|
|
flags |= HAWK_PIO_WRITEIN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Hawk::Pipe::RW:
|
2020-01-07 16:34:26 +00:00
|
|
|
flags |= HAWK_PIO_READOUT | HAWK_PIO_ERRTOOUT | HAWK_PIO_WRITEIN;
|
2019-12-13 04:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:34:26 +00:00
|
|
|
pio = hawk_pio_open((hawk_gem_t*)*this, 0, io.getName(), flags);
|
|
|
|
if (!pio) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
#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 }
|
|
|
|
};
|
2020-01-04 16:19:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-01-06 08:53:31 +00:00
|
|
|
#endif
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-05 04:32:14 +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 ||
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +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;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
hawk_ooi_t HawkStd::readPipe (Pipe& io, hawk_ooch_t* buf, hawk_oow_t len)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
#if defined(ENABLE_NWIO)
|
2019-12-13 04:29:58 +00:00
|
|
|
return (io.getUflags() > 0)?
|
2020-01-17 06:25:51 +00:00
|
|
|
hawk_nwio_read((hawk_nwio_t*)io.getHandle(), buf, len):
|
|
|
|
hawk_pio_read((hawk_pio_t*)io.getHandle(), HAWK_PIO_OUT, buf, len);
|
2020-01-06 08:53:31 +00:00
|
|
|
#else
|
2020-01-17 06:25:51 +00:00
|
|
|
return hawk_pio_read((hawk_pio_t*)io.getHandle(), HAWK_PIO_OUT, buf, len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_ooi_t HawkStd::readPipeBytes (Pipe& io, hawk_bch_t* buf, hawk_oow_t len)
|
|
|
|
{
|
|
|
|
#if defined(ENABLE_NWIO)
|
|
|
|
return (io.getUflags() > 0)?
|
|
|
|
hawk_nwio_readbytes((hawk_nwio_t*)io.getHandle(), buf, len):
|
|
|
|
hawk_pio_readbytes((hawk_pio_t*)io.getHandle(), HAWK_PIO_OUT, buf, len);
|
|
|
|
#else
|
|
|
|
return hawk_pio_readbytes((hawk_pio_t*)io.getHandle(), HAWK_PIO_OUT, buf, len);
|
2020-01-06 08:53:31 +00:00
|
|
|
#endif
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +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
|
|
|
{
|
2020-01-06 08:53:31 +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);
|
2020-01-06 08:53:31 +00:00
|
|
|
#else
|
|
|
|
return hawk_pio_write((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN, buf, len);
|
|
|
|
#endif
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +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
|
|
|
{
|
2020-01-06 08:53:31 +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);
|
2020-01-06 08:53:31 +00:00
|
|
|
#else
|
|
|
|
return hawk_pio_writebytes((hawk_pio_t*)io.getHandle(), HAWK_PIO_IN, buf, len);
|
|
|
|
#endif
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::flushPipe (Pipe& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-06 08:53:31 +00:00
|
|
|
#if defined(ENABLE_NWIO)
|
2019-12-13 04:29:58 +00:00
|
|
|
return (io.getUflags() > 0)?
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +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:
|
2020-01-07 16:34:26 +00:00
|
|
|
flags |= HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE;
|
2019-12-13 04:29:58 +00:00
|
|
|
break;
|
|
|
|
case Hawk::File::APPEND:
|
2020-01-07 16:34:26 +00:00
|
|
|
flags |= HAWK_SIO_APPEND | HAWK_SIO_CREATE;
|
2019-12-13 04:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
const hawk_ooch_t* ioname = io.getName();
|
|
|
|
if (ioname[0] == '-' && ioname[1] == '\0')
|
|
|
|
{
|
|
|
|
if (mode == Hawk::File::READ)
|
|
|
|
sio = open_sio_std(HAWK_NULL, io, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
|
|
|
|
else
|
|
|
|
sio = open_sio_std(HAWK_NULL, io, HAWK_SIO_STDOUT, HAWK_SIO_WRITE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sio = hawk_sio_open((hawk_gem_t*)*this, 0, ioname, flags);
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
if (!sio) return -1;
|
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
2020-03-05 15:11:55 +00:00
|
|
|
hawk_cmgr_t* cmgr = this->getiocmgr(ioname);
|
2019-12-13 04:29:58 +00:00
|
|
|
if (cmgr) hawk_sio_setcmgr (sio, cmgr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
io.setHandle (sio);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::closeFile (File& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
hawk_sio_close ((hawk_sio_t*)io.getHandle());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-17 06:25:51 +00:00
|
|
|
hawk_ooi_t HawkStd::readFileBytes (File& io, hawk_bch_t* buf, hawk_oow_t len)
|
|
|
|
{
|
|
|
|
return hawk_sio_getbchars((hawk_sio_t*)io.getHandle(), buf, len);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::flushFile (File& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
return hawk_sio_flush((hawk_sio_t*)io.getHandle());
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void HawkStd::setConsoleCmgr (const hawk_cmgr_t* cmgr)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
this->console_cmgr = (hawk_cmgr_t*)cmgr;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
const hawk_cmgr_t* HawkStd::getConsoleCmgr () const
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
return this->console_cmgr;
|
|
|
|
}
|
|
|
|
|
2020-01-07 09:02:56 +00:00
|
|
|
int HawkStd::addConsoleOutput (const hawk_uch_t* arg, hawk_oow_t len)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-08 15:37:42 +00:00
|
|
|
HAWK_ASSERT (this->hawk != HAWK_NULL);
|
|
|
|
int n = this->ofile.add(this->hawk, arg, len);
|
2020-01-07 09:02:56 +00:00
|
|
|
if (n <= -1) this->setError (HAWK_ENOMEM);
|
2019-12-13 04:29:58 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-01-07 09:02:56 +00:00
|
|
|
int HawkStd::addConsoleOutput (const hawk_uch_t* arg)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-07 09:02:56 +00:00
|
|
|
return this->addConsoleOutput(arg, hawk_count_ucstr(arg));
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 09:02:56 +00:00
|
|
|
int HawkStd::addConsoleOutput (const hawk_bch_t* arg, hawk_oow_t len)
|
|
|
|
{
|
2020-03-05 15:22:55 +00:00
|
|
|
HAWK_ASSERT (this->hawk != HAWK_NULL);
|
2020-01-08 15:37:42 +00:00
|
|
|
int n = this->ofile.add(this->hawk, arg, len);
|
2020-01-07 09:02:56 +00:00
|
|
|
if (n <= -1) this->setError (HAWK_ENOMEM);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HawkStd::addConsoleOutput (const hawk_bch_t* arg)
|
|
|
|
{
|
|
|
|
return this->addConsoleOutput(arg, hawk_count_bcstr(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void HawkStd::clearConsoleOutputs ()
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-08 15:37:42 +00:00
|
|
|
this->ofile.clear (this->hawk);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
static int check_var_assign (hawk_rtx_t* rtx, const hawk_ooch_t* str)
|
|
|
|
{
|
2020-03-13 16:00:21 +00:00
|
|
|
hawk_ooch_t* eq, * dstr;
|
2020-03-05 15:11:55 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
eq = hawk_find_oochar_in_oocstr(str, '=');
|
|
|
|
if (!eq || eq <= str) return 0; /* not assignment */
|
|
|
|
|
2020-03-13 16:00:21 +00:00
|
|
|
dstr = hawk_rtx_dupoocstr(rtx, str, HAWK_NULL);
|
|
|
|
if (HAWK_UNLIKELY(!dstr)) return -1;
|
2020-03-05 15:11:55 +00:00
|
|
|
|
2020-03-13 16:00:21 +00:00
|
|
|
eq = dstr + (eq - str);
|
|
|
|
*eq = '\0';
|
|
|
|
|
|
|
|
if (hawk_isvalidident(hawk_rtx_gethawk(rtx), dstr))
|
|
|
|
{
|
|
|
|
hawk_unescape_oocstr (eq + 1);
|
|
|
|
n = (hawk_rtx_setgbltostrbyname(rtx, dstr, eq + 1) <= -1)? -1: 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_rtx_freemem (rtx, dstr);
|
2020-03-05 15:11:55 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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;
|
2020-03-05 15:11:55 +00:00
|
|
|
hawk_sio_t* sio;
|
|
|
|
hawk_val_t* v_argc, * v_argv, * v_pair;
|
|
|
|
hawk_int_t i_argc;
|
|
|
|
const hawk_ooch_t* file;
|
2020-03-15 17:39:15 +00:00
|
|
|
hawk_map_t* map;
|
|
|
|
hawk_map_pair_t* pair;
|
2020-03-05 15:11:55 +00:00
|
|
|
hawk_ooch_t ibuf[128];
|
|
|
|
hawk_oow_t ibuflen;
|
|
|
|
hawk_oocs_t as;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
v_argc = hawk_rtx_getgbl(rtx, this->gbl_argc);
|
|
|
|
HAWK_ASSERT (v_argc != HAWK_NULL);
|
|
|
|
if (hawk_rtx_valtoint(rtx, v_argc, &i_argc) <= -1) 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
|
|
|
|
*/
|
|
|
|
v_argv = hawk_rtx_getgbl(rtx, this->gbl_argv);
|
2020-03-05 15:22:55 +00:00
|
|
|
HAWK_ASSERT (v_argv != HAWK_NULL);
|
2020-03-05 15:11:55 +00:00
|
|
|
if (HAWK_RTX_GETVALTYPE(rtx, v_argv) != HAWK_VAL_MAP)
|
|
|
|
{
|
|
|
|
/* with flexmap on, you can change ARGV to a scalar.
|
|
|
|
* BEGIN { ARGV="xxx"; }
|
|
|
|
* you must not do this. */
|
|
|
|
hawk_rtx_seterrfmt (rtx, HAWK_NULL, HAWK_EINVAL, HAWK_T("phony value in ARGV"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
map = ((hawk_val_map_t*)v_argv)->map;
|
|
|
|
HAWK_ASSERT (map != HAWK_NULL);
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
nextfile:
|
|
|
|
if ((hawk_int_t)this->runarg_index >= (i_argc - 1)) /* ARGV is a kind of 0-based array unlike other normal arrays or substring indexing scheme */
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-03-05 15:11:55 +00:00
|
|
|
/* reached the last ARGV */
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
if (this->runarg_count <= 0) /* but no file has been ever opened */
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-03-05 15:11:55 +00:00
|
|
|
console_open_stdin:
|
|
|
|
/* open stdin */
|
2020-01-07 16:34:26 +00:00
|
|
|
sio = open_sio_std(HAWK_NULL, io, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
|
2019-12-13 04:29:58 +00:00
|
|
|
if (sio == HAWK_NULL) return -1;
|
|
|
|
|
2020-01-07 16:34:26 +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->runarg_count++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
return 0;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
ibuflen = hawk_int_to_oocstr(this->runarg_index + 1, 10, HAWK_NULL, ibuf, HAWK_COUNTOF(ibuf));
|
2020-03-15 17:39:15 +00:00
|
|
|
pair = hawk_map_search(map, ibuf, ibuflen);
|
2020-03-05 15:11:55 +00:00
|
|
|
if (!pair)
|
|
|
|
{
|
|
|
|
if (this->runarg_count <= 0) goto console_open_stdin;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
v_pair = (hawk_val_t*)HAWK_HTB_VPTR(pair);
|
|
|
|
HAWK_ASSERT (v_pair != HAWK_NULL);
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
as.ptr = hawk_rtx_getvaloocstr(rtx, v_pair, &as.len);
|
|
|
|
if (HAWK_UNLIKELY(!as.ptr)) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
if (as.len == 0)
|
|
|
|
{
|
|
|
|
/* the name is empty */
|
|
|
|
hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr);
|
|
|
|
this->runarg_index++;
|
|
|
|
goto nextfile;
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
if (hawk_count_oocstr(as.ptr) < as.len)
|
|
|
|
{
|
|
|
|
/* the name contains one or more '\0' */
|
|
|
|
((Run*)io)->formatError (HAWK_EIONMNL, HAWK_NULL, HAWK_T("invalid I/O name of length %zu containing '\\0'"), as.len);
|
|
|
|
hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
file = as.ptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is different from the -v option.
|
|
|
|
* if an argument has a special form of var=val, it is treated specially
|
|
|
|
*
|
|
|
|
* on the command-line
|
2020-04-16 03:42:30 +00:00
|
|
|
* hawk -f a.hawk a=20 /etc/passwd
|
2020-03-05 15:11:55 +00:00
|
|
|
* or via ARGV
|
|
|
|
* hawk 'BEGIN { ARGV[1] = "a=20"; }
|
|
|
|
* { print a $1; }' dummy /etc/hosts
|
|
|
|
*/
|
|
|
|
if ((x = check_var_assign(rtx, file)) != 0)
|
|
|
|
{
|
|
|
|
hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr);
|
|
|
|
if (HAWK_UNLIKELY(x <= -1)) return -1;
|
|
|
|
this->runarg_index++;
|
|
|
|
goto nextfile;
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
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);
|
|
|
|
else
|
|
|
|
sio = open_sio(HAWK_NULL, io, file, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR);
|
|
|
|
if (sio == HAWK_NULL)
|
|
|
|
{
|
|
|
|
hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hawk_rtx_setfilenamewithoochars(rtx, file, hawk_count_oocstr(file)) <= -1)
|
|
|
|
{
|
|
|
|
hawk_sio_close (sio);
|
|
|
|
hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
hawk_rtx_freevaloocstr (rtx, v_pair, as.ptr);
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
if (this->console_cmgr) hawk_sio_setcmgr (sio, this->console_cmgr);
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
io.setHandle (sio);
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-03-05 15:11:55 +00:00
|
|
|
/* increment the counter of files successfully opened */
|
|
|
|
this->runarg_count++;
|
|
|
|
this->runarg_index++;
|
|
|
|
return 1;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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;
|
2020-01-06 08:53:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-04 16:19:38 +00:00
|
|
|
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'))
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
2020-01-06 08:53:31 +00:00
|
|
|
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-03-03 12:00:13 +00:00
|
|
|
if (hawk_rtx_setofilenamewithoochars(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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;
|
|
|
|
return open_console_in (io);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HAWK_ASSERT (mode == Console::WRITE);
|
|
|
|
|
|
|
|
this->ofile_count = 0;
|
|
|
|
this->ofile_index = 0;
|
|
|
|
return open_console_out (io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::closeConsole (Console& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
hawk_sio_close ((hawk_sio_t*)io.getHandle());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
hawk_ooi_t HawkStd::readConsole (Console& io, hawk_ooch_t* data, hawk_oow_t size)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-04 16:19:38 +00:00
|
|
|
hawk_ooi_t nn;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-17 06:25:51 +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);
|
2020-03-15 17:39:15 +00:00
|
|
|
((Run*)io)->setGlobal (HAWK_GBL_FNR, (hawk_int_t)0);
|
2020-01-17 06:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nn;
|
|
|
|
}
|
|
|
|
|
|
|
|
hawk_ooi_t HawkStd::readConsoleBytes (Console& io, hawk_bch_t* data, hawk_oow_t size)
|
|
|
|
{
|
|
|
|
hawk_ooi_t nn;
|
|
|
|
|
|
|
|
while ((nn = hawk_sio_getbchars((hawk_sio_t*)io.getHandle(), data, size)) == 0)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
hawk_sio_t* sio = (hawk_sio_t*)io.getHandle();
|
|
|
|
|
2020-01-17 06:25:51 +00:00
|
|
|
n = open_console_in(io);
|
2019-12-13 04:29:58 +00:00
|
|
|
if (n == -1) return -1;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* no more input console */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sio) hawk_sio_close (sio);
|
2020-03-15 17:39:15 +00:00
|
|
|
((Run*)io)->setGlobal (HAWK_GBL_FNR, (hawk_int_t)0);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nn;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::flushConsole (Console& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
return hawk_sio_flush ((hawk_sio_t*)io.getHandle());
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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
|
2020-01-05 04:32:14 +00:00
|
|
|
void* HawkStd::allocMem (hawk_oow_t n)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
return ::malloc (n);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void* HawkStd::reallocMem (void* ptr, hawk_oow_t n)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
return ::realloc (ptr, n);
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void HawkStd::freeMem (void* ptr)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
::free (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// miscellaneous primitive
|
|
|
|
|
2020-01-08 03:21:49 +00:00
|
|
|
hawk_flt_t HawkStd::pow (hawk_flt_t x, hawk_flt_t y)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-09 04:34:21 +00:00
|
|
|
return hawk_stdmathpow(this->hawk, x, y);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 03:21:49 +00:00
|
|
|
hawk_flt_t HawkStd::mod (hawk_flt_t x, hawk_flt_t y)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-09 04:34:21 +00:00
|
|
|
return hawk_stdmathmod(this->hawk, x, y);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
void* HawkStd::modopen (const hawk_mod_spec_t* spec)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
void* h;
|
2020-12-18 14:43:30 +00:00
|
|
|
h = hawk_stdmodopen(this->hawk, spec);
|
2019-12-13 04:29:58 +00:00
|
|
|
if (!h) this->retrieveError ();
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void HawkStd::modclose (void* handle)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-08 15:37:42 +00:00
|
|
|
hawk_stdmodclose (this->hawk, handle);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
void* HawkStd::modgetsym (void* handle, const hawk_ooch_t* name)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
void* s;
|
2020-01-08 15:37:42 +00:00
|
|
|
s = hawk_stdmodgetsym(this->hawk, handle, name);
|
2019-12-13 04:29:58 +00:00
|
|
|
if (!s) this->retrieveError ();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::SourceFile::open (Data& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
hawk_sio_t* sio;
|
|
|
|
|
|
|
|
if (io.isMaster())
|
|
|
|
{
|
2021-08-17 19:26:41 +00:00
|
|
|
hawk_ooch_t* xpath;
|
2021-08-17 17:38:16 +00:00
|
|
|
|
2021-08-17 19:26:41 +00:00
|
|
|
xpath = (this->_type == NAME_UCH)?
|
|
|
|
hawk_addsionamewithuchars((hawk_t*)io, (const hawk_uch_t*)this->_name, hawk_count_ucstr((const hawk_uch_t*)this->_name)):
|
|
|
|
hawk_addsionamewithbchars((hawk_t*)io, (const hawk_bch_t*)this->_name, hawk_count_bcstr((const hawk_bch_t*)this->_name));
|
|
|
|
if (HAWK_UNLIKELY(!xpath)) return -1;
|
2021-08-17 17:38:16 +00:00
|
|
|
|
2021-08-17 19:26:41 +00:00
|
|
|
if (xpath[0] == HAWK_T('-') && xpath[1] == HAWK_T('\0'))
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
|
|
|
if (io.getMode() == READ)
|
2020-01-06 08:53:31 +00:00
|
|
|
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
|
2020-01-06 08:53:31 +00:00
|
|
|
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);
|
2021-08-17 19:26:41 +00:00
|
|
|
if (sio == HAWK_NULL) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-17 19:26:41 +00:00
|
|
|
sio = open_sio(
|
|
|
|
io, HAWK_NULL, xpath,
|
2019-12-13 04:29:58 +00:00
|
|
|
(io.getMode() == READ?
|
|
|
|
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
|
|
|
|
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
|
|
|
|
);
|
2021-08-17 19:26:41 +00:00
|
|
|
if (sio == HAWK_NULL) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
|
2021-08-17 19:26:41 +00:00
|
|
|
io.setName (xpath);
|
2021-08-18 05:11:04 +00:00
|
|
|
io.setPath (xpath); // let the parser use this path, especially upon an error
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-08 03:14:41 +00:00
|
|
|
// open an included file
|
|
|
|
const hawk_ooch_t* ioname;
|
|
|
|
hawk_ooch_t* xpath;
|
|
|
|
|
2019-12-13 04:29:58 +00:00
|
|
|
ioname = io.getName();
|
|
|
|
HAWK_ASSERT (ioname != HAWK_NULL);
|
|
|
|
|
|
|
|
if (io.getPrevHandle())
|
|
|
|
{
|
2020-01-08 03:14:41 +00:00
|
|
|
const hawk_ooch_t* outer, * path;
|
|
|
|
hawk_ooch_t fbuf[64];
|
|
|
|
hawk_ooch_t* dbuf = HAWK_NULL;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
path = ioname;
|
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('/'))
|
|
|
|
{
|
2020-01-04 16:19:38 +00:00
|
|
|
hawk_oow_t tmplen, totlen, dirlen;
|
2020-01-08 03:14:41 +00:00
|
|
|
|
2019-12-13 04:29:58 +00:00
|
|
|
dirlen = base - outer;
|
2020-01-04 16:19:38 +00:00
|
|
|
totlen = hawk_count_oocstr(ioname) + dirlen;
|
2019-12-13 04:29:58 +00:00
|
|
|
if (totlen >= HAWK_COUNTOF(fbuf))
|
|
|
|
{
|
2020-01-08 03:14:41 +00:00
|
|
|
dbuf = (hawk_ooch_t*)hawk_allocmem((hawk_t*)io, HAWK_SIZEOF(hawk_ooch_t) * (totlen + 1));
|
|
|
|
if (!dbuf) return -1;
|
|
|
|
path = dbuf;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
2020-01-08 03:14:41 +00:00
|
|
|
else path = fbuf;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
tmplen = hawk_copy_oochars_to_oocstr_unlimited((hawk_ooch_t*)path, outer, dirlen);
|
|
|
|
hawk_copy_oocstr_unlimited ((hawk_ooch_t*)path + tmplen, ioname);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-10 15:23:30 +00:00
|
|
|
|
|
|
|
if (!hawk_stdplainfileexists((hawk_t*)io, path) && ((hawk_t*)io)->opt.includedirs.len > 0 && ioname[0] != '/')
|
|
|
|
{
|
|
|
|
const hawk_ooch_t* tmp;
|
|
|
|
tmp = hawk_stdgetfileindirs((hawk_t*)io, &((hawk_t*)io)->opt.includedirs, ioname);
|
|
|
|
if (tmp) path = tmp;
|
|
|
|
}
|
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
xpath = hawk_addsionamewithoochars((hawk_t*)io, path, hawk_count_oocstr(path));
|
|
|
|
if (dbuf) hawk_freemem ((hawk_t*)io, dbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-10 15:23:30 +00:00
|
|
|
const hawk_ooch_t* path;
|
|
|
|
|
|
|
|
path = ioname;
|
|
|
|
if (!hawk_stdplainfileexists((hawk_t*)io, path) && ((hawk_t*)io)->opt.includedirs.len > 0 && ioname[0] != '/')
|
|
|
|
{
|
|
|
|
const hawk_ooch_t* tmp;
|
|
|
|
tmp = hawk_stdgetfileindirs((hawk_t*)io, &((hawk_t*)io)->opt.includedirs, ioname);
|
|
|
|
if (tmp) path = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
xpath = hawk_addsionamewithoochars((hawk_t*)io, path, hawk_count_oocstr(path));
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
2020-01-08 03:14:41 +00:00
|
|
|
if (!xpath) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
sio = open_sio(
|
2020-01-08 03:14:41 +00:00
|
|
|
io, HAWK_NULL, xpath,
|
2019-12-13 04:29:58 +00:00
|
|
|
(io.getMode() == READ?
|
|
|
|
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
|
|
|
|
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
|
|
|
|
);
|
2020-01-08 03:14:41 +00:00
|
|
|
if (!sio) return -1;
|
|
|
|
|
|
|
|
io.setPath (xpath);
|
|
|
|
io.setHandle (sio);
|
2019-12-13 04:29:58 +00:00
|
|
|
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
io.setHandle (sio);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
hawk_ooi_t HawkStd::SourceFile::read (Data& io, hawk_ooch_t* buf, hawk_oow_t len)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2020-01-07 09:02:56 +00:00
|
|
|
return hawk_sio_getoochars((hawk_sio_t*)io.getHandle(), buf, len);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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
|
|
|
{
|
2020-01-07 09:02:56 +00:00
|
|
|
return hawk_sio_putoochars((hawk_sio_t*)io.getHandle(), buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
HawkStd::SourceString::~SourceString ()
|
|
|
|
{
|
2021-08-18 05:11:04 +00:00
|
|
|
HAWK_ASSERT (this->_hawk == HAWK_NULL);
|
|
|
|
HAWK_ASSERT (this->str == HAWK_NULL);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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.
|
2020-01-07 09:02:56 +00:00
|
|
|
if (io.getMode() == WRITE)
|
|
|
|
{
|
|
|
|
// SourceString does not support writing.
|
|
|
|
((Hawk*)io)->setError (HAWK_ENOIMPL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this->str)
|
|
|
|
{
|
|
|
|
this->_hawk = (hawk_t*)io;
|
|
|
|
if (this->_type == STR_UCH)
|
|
|
|
{
|
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
2021-08-18 05:11:04 +00:00
|
|
|
this->str = hawk_dupucstr(this->_hawk, (const hawk_uch_t*)this->_str, HAWK_NULL);
|
2020-01-07 09:02:56 +00:00
|
|
|
#else
|
2021-08-18 05:11:04 +00:00
|
|
|
this->str = hawk_duputobcstr(this->_hawk, (const hawk_uch_t*)this->_str, HAWK_NULL);
|
2020-01-07 09:02:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-07 16:34:26 +00:00
|
|
|
HAWK_ASSERT (this->_type == STR_BCH);
|
2020-01-07 09:02:56 +00:00
|
|
|
#if defined(HAWK_OOCH_IS_UCH)
|
2021-08-18 05:11:04 +00:00
|
|
|
this->str = hawk_dupbtoucstr(this->_hawk, (const hawk_bch_t*)this->_str, HAWK_NULL, 0);
|
2020-01-07 09:02:56 +00:00
|
|
|
#else
|
2021-08-18 05:11:04 +00:00
|
|
|
this->str = hawk_dupbcstr(this->_hawk, (const hawk_bch_t*)this->_str, HAWK_NULL);
|
2020-01-07 09:02:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-08-18 05:11:04 +00:00
|
|
|
if (HAWK_UNLIKELY(!this->str))
|
2021-08-17 17:38:16 +00:00
|
|
|
{
|
|
|
|
this->_hawk = HAWK_NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
2020-01-07 09:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->ptr = this->str;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// open an included file
|
2020-01-08 03:14:41 +00:00
|
|
|
const hawk_ooch_t* ioname;
|
|
|
|
hawk_ooch_t* xpath;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
|
|
|
ioname = io.getName();
|
|
|
|
HAWK_ASSERT (ioname != HAWK_NULL);
|
|
|
|
|
|
|
|
if (io.getPrevHandle())
|
|
|
|
{
|
2020-01-08 03:14:41 +00:00
|
|
|
const hawk_ooch_t* outer, * path;
|
|
|
|
hawk_ooch_t fbuf[64];
|
|
|
|
hawk_ooch_t* dbuf = HAWK_NULL;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
path = ioname;
|
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-07 09:02:56 +00:00
|
|
|
|
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('/'))
|
|
|
|
{
|
2020-01-04 16:19:38 +00:00
|
|
|
hawk_oow_t tmplen, totlen, dirlen;
|
2020-01-08 03:14:41 +00:00
|
|
|
|
2019-12-13 04:29:58 +00:00
|
|
|
dirlen = base - outer;
|
2020-01-04 16:19:38 +00:00
|
|
|
totlen = hawk_count_oocstr(ioname) + dirlen;
|
2019-12-13 04:29:58 +00:00
|
|
|
if (totlen >= HAWK_COUNTOF(fbuf))
|
|
|
|
{
|
2020-01-07 09:02:56 +00:00
|
|
|
dbuf = (hawk_ooch_t*)hawk_allocmem((hawk_t*)io, HAWK_SIZEOF(hawk_ooch_t) * (totlen + 1));
|
2021-08-18 05:11:04 +00:00
|
|
|
if (HAWK_UNLIKELY(!dbuf)) return -1;
|
2020-01-08 03:14:41 +00:00
|
|
|
path = dbuf;
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
2020-01-08 03:14:41 +00:00
|
|
|
else path = fbuf;
|
2020-01-07 09:02:56 +00:00
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
tmplen = hawk_copy_oochars_to_oocstr_unlimited((hawk_ooch_t*)path, outer, dirlen);
|
|
|
|
hawk_copy_oocstr_unlimited ((hawk_ooch_t*)path + tmplen, ioname);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 03:14:41 +00:00
|
|
|
xpath = hawk_addsionamewithoochars((hawk_t*)io, path, hawk_count_oocstr(path));
|
|
|
|
if (dbuf) hawk_freemem ((hawk_t*)io, dbuf);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
2020-01-08 03:14:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
xpath = hawk_addsionamewithoochars((hawk_t*)io, ioname, hawk_count_oocstr(ioname));
|
|
|
|
}
|
2021-08-18 05:11:04 +00:00
|
|
|
if (HAWK_UNLIKELY(!xpath)) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-06 08:53:31 +00:00
|
|
|
sio = open_sio(
|
2020-01-08 03:14:41 +00:00
|
|
|
io, HAWK_NULL, xpath,
|
2019-12-13 04:29:58 +00:00
|
|
|
(io.getMode() == READ?
|
|
|
|
(HAWK_SIO_READ | HAWK_SIO_IGNOREECERR | HAWK_SIO_KEEPPATH):
|
|
|
|
(HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR))
|
|
|
|
);
|
2020-01-08 03:14:41 +00:00
|
|
|
if (!sio) return -1;
|
2019-12-13 04:29:58 +00:00
|
|
|
|
2020-01-08 03:14:41 +00:00
|
|
|
io.setPath (xpath);
|
2019-12-13 04:29:58 +00:00
|
|
|
io.setHandle (sio);
|
2021-08-17 19:26:41 +00:00
|
|
|
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
int HawkStd::SourceString::close (Data& io)
|
2019-12-13 04:29:58 +00:00
|
|
|
{
|
2021-08-17 19:26:41 +00:00
|
|
|
if (io.isMaster())
|
|
|
|
{
|
2021-08-19 20:17:17 +00:00
|
|
|
// free the resources and nullify this->_hawk in particular
|
|
|
|
// to prevent this object from outliving the hawk instance pointed to
|
|
|
|
// by this->_hawk.
|
2021-08-17 19:26:41 +00:00
|
|
|
HAWK_ASSERT (this->_hawk != HAWK_NULL);
|
|
|
|
HAWK_ASSERT (this->str != HAWK_NULL);
|
|
|
|
hawk_freemem (this->_hawk, this->str);
|
|
|
|
this->str = HAWK_NULL;
|
|
|
|
this->ptr = HAWK_NULL;
|
|
|
|
this->_hawk = HAWK_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hawk_sio_close ((hawk_sio_t*)io.getHandle());
|
|
|
|
}
|
2019-12-13 04:29:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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;
|
2021-08-17 19:26:41 +00:00
|
|
|
while (*this->ptr != '\0' && n < len) buf[n++] = *this->ptr++;
|
2019-12-13 04:29:58 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-17 19:26:41 +00:00
|
|
|
return hawk_sio_getoochars((hawk_sio_t*)io.getHandle(), buf, len);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:32:14 +00:00
|
|
|
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
|
2021-08-17 19:26:41 +00:00
|
|
|
return hawk_sio_putoochars((hawk_sio_t*)io.getHandle(), buf, len);
|
2019-12-13 04:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////
|
2019-12-16 15:09:58 +00:00
|
|
|
HAWK_END_NAMESPACE(HAWK)
|
2019-12-13 04:29:58 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
|