2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2007-10-30 00:20:00 +00:00
|
|
|
* $Id: Awk.cpp,v 1.81 2007/10/28 06:12:37 bacon Exp $
|
2007-09-25 20:25:00 +00:00
|
|
|
*
|
|
|
|
* {License}
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2007-08-22 22:56:00 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
#include <ase/awk/Awk.hpp>
|
2007-05-09 00:09:00 +00:00
|
|
|
#include <ase/awk/val.h>
|
2007-05-06 19:44:00 +00:00
|
|
|
#include <ase/cmn/str.h>
|
|
|
|
#include <ase/cmn/mem.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
/////////////////////////////////
|
|
|
|
ASE_BEGIN_NAMESPACE(ASE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Source
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-09-23 00:46:00 +00:00
|
|
|
Awk::Source::Source (Mode mode): mode (mode), handle (ASE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Source::Mode Awk::Source::getMode () const
|
|
|
|
{
|
|
|
|
return this->mode;
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const void* Awk::Source::getHandle () const
|
|
|
|
{
|
|
|
|
return this->handle;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::Source::setHandle (void* handle)
|
|
|
|
{
|
|
|
|
this->handle = handle;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Extio
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Extio::Extio (extio_t* extio): extio (extio)
|
|
|
|
{
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const Awk::char_t* Awk::Extio::getName () const
|
|
|
|
{
|
|
|
|
return extio->name;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const void* Awk::Extio::getHandle () const
|
|
|
|
{
|
|
|
|
return extio->handle;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::Extio::setHandle (void* handle)
|
|
|
|
{
|
|
|
|
extio->handle = handle;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const Awk::extio_t* Awk::Extio::getRawExtio () const
|
|
|
|
{
|
|
|
|
return extio;
|
|
|
|
}
|
2007-05-15 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const Awk::run_t* Awk::Extio::getRawRun () const
|
|
|
|
{
|
|
|
|
return extio->run;
|
|
|
|
}
|
2007-09-07 23:14:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Pipe
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2007-09-07 23:14:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Pipe::Pipe (extio_t* extio): Extio(extio)
|
|
|
|
{
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Pipe::Mode Awk::Pipe::getMode () const
|
|
|
|
{
|
|
|
|
return (Mode)extio->mode;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::File
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::File::File (extio_t* extio): Extio(extio)
|
|
|
|
{
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::File::Mode Awk::File::getMode () const
|
|
|
|
{
|
|
|
|
return (Mode)extio->mode;
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Console
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Awk::Console::Console (extio_t* extio): Extio(extio), filename(ASE_NULL)
|
|
|
|
{
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Console::~Console ()
|
|
|
|
{
|
|
|
|
if (filename != ASE_NULL)
|
2007-05-15 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_free (ase_awk_getrunawk(extio->run), filename);
|
2007-05-15 01:21:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-15 01:21:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::Console::setFileName (const char_t* name)
|
|
|
|
{
|
|
|
|
if (extio->mode == READ)
|
2007-05-15 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
return ase_awk_setfilename (
|
|
|
|
extio->run, name, ase_strlen(name));
|
2007-05-15 01:21:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else
|
2007-05-06 19:44:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
return ase_awk_setofilename (
|
|
|
|
extio->run, name, ase_strlen(name));
|
2007-05-06 19:44:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 00:20:00 +00:00
|
|
|
int Awk::Console::setFNR (long_t fnr)
|
|
|
|
{
|
|
|
|
ase_awk_val_t* tmp;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
tmp = ase_awk_makeintval (extio->run, fnr);
|
|
|
|
if (tmp == ASE_NULL) return -1;
|
|
|
|
|
|
|
|
ase_awk_refupval (extio->run, tmp);
|
|
|
|
n = ase_awk_setglobal (extio->run, ASE_AWK_GLOBAL_FNR, tmp);
|
|
|
|
ase_awk_refdownval (extio->run, tmp);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Console::Mode Awk::Console::getMode () const
|
|
|
|
{
|
|
|
|
return (Mode)extio->mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Argument
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
Awk::Argument::Argument (Run& run): run (&run), val (ASE_NULL)
|
|
|
|
{
|
|
|
|
this->inum = 0;
|
|
|
|
this->rnum = 0.0;
|
|
|
|
|
|
|
|
this->str.ptr = ASE_NULL;
|
|
|
|
this->str.len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::Argument::Argument (Run* run): run (run), val (ASE_NULL)
|
|
|
|
{
|
|
|
|
this->inum = 0;
|
|
|
|
this->rnum = 0.0;
|
|
|
|
|
|
|
|
this->str.ptr = ASE_NULL;
|
|
|
|
this->str.len = 0;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Argument::Argument (): run (ASE_NULL), val (ASE_NULL)
|
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
this->inum = 0;
|
|
|
|
this->rnum = 0.0;
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
this->str.ptr = ASE_NULL;
|
|
|
|
this->str.len = 0;
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Argument::~Argument ()
|
2007-09-25 00:12:00 +00:00
|
|
|
{
|
|
|
|
clear ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::Argument::clear ()
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
if (this->val == ASE_NULL)
|
2007-05-11 10:07:00 +00:00
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
/* case 1. not initialized.
|
|
|
|
* case 2. initialized with the second init.
|
|
|
|
* none of the cases creates a new string so the sttring
|
|
|
|
* that str.ptr is pointing at doesn't have to be freed */
|
2007-05-11 10:07:00 +00:00
|
|
|
this->str.ptr = ASE_NULL;
|
2007-05-12 02:21:00 +00:00
|
|
|
this->str.len = 0;
|
2007-05-11 10:07:00 +00:00
|
|
|
}
|
2007-10-22 00:05:00 +00:00
|
|
|
else if (ASE_AWK_VAL_TYPE(this->val) == ASE_AWK_VAL_MAP)
|
2007-05-11 10:07:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
/* when the value is a map, str.ptr and str.len are
|
|
|
|
* used for index iteration in getFirstIndex & getNextIndex */
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-09-25 00:12:00 +00:00
|
|
|
this->val = ASE_NULL;
|
2007-05-11 10:07:00 +00:00
|
|
|
}
|
2007-10-02 00:22:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
|
|
|
if (this->str.ptr != ASE_NULL)
|
|
|
|
{
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_STR)
|
2007-10-02 00:22:00 +00:00
|
|
|
{
|
2007-10-14 02:25:00 +00:00
|
|
|
awk_t* awk = this->run->awk->awk;
|
2007-10-13 23:34:00 +00:00
|
|
|
ase_awk_free (awk, this->str.ptr);
|
2007-10-02 00:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->str.ptr = ASE_NULL;
|
|
|
|
this->str.len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->val != ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-02 00:22:00 +00:00
|
|
|
this->val = ASE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this->rnum = 0.0;
|
|
|
|
this->inum = 0;
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void* Awk::Argument::operator new (size_t n, awk_t* awk) throw ()
|
|
|
|
{
|
|
|
|
void* ptr = ase_awk_malloc (awk, ASE_SIZEOF(awk) + n);
|
|
|
|
if (ptr == ASE_NULL) return ASE_NULL;
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
*(awk_t**)ptr = awk;
|
|
|
|
return (char*)ptr+ASE_SIZEOF(awk);
|
|
|
|
}
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void* Awk::Argument::operator new[] (size_t n, awk_t* awk) throw ()
|
|
|
|
{
|
|
|
|
void* ptr = ase_awk_malloc (awk, ASE_SIZEOF(awk) + n);
|
|
|
|
if (ptr == ASE_NULL) return ASE_NULL;
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
*(awk_t**)ptr = awk;
|
|
|
|
return (char*)ptr+ASE_SIZEOF(awk);
|
|
|
|
}
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-06-18 00:31:00 +00:00
|
|
|
#if !defined(__BORLANDC__)
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::Argument::operator delete (void* ptr, awk_t* awk)
|
|
|
|
{
|
|
|
|
ase_awk_free (awk, (char*)ptr-ASE_SIZEOF(awk));
|
|
|
|
}
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::Argument::operator delete[] (void* ptr, awk_t* awk)
|
|
|
|
{
|
|
|
|
ase_awk_free (awk, (char*)ptr-ASE_SIZEOF(awk));
|
|
|
|
}
|
2007-06-18 00:31:00 +00:00
|
|
|
#endif
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::Argument::operator delete (void* ptr)
|
|
|
|
{
|
|
|
|
void* p = (char*)ptr-ASE_SIZEOF(awk_t*);
|
|
|
|
ase_awk_free (*(awk_t**)p, p);
|
|
|
|
}
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::Argument::operator delete[] (void* ptr)
|
|
|
|
{
|
|
|
|
void* p = (char*)ptr-ASE_SIZEOF(awk_t*);
|
|
|
|
ase_awk_free (*(awk_t**)p, p);
|
|
|
|
}
|
2007-05-17 00:53:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
int Awk::Argument::init (val_t* v)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
// this method is used internally only
|
|
|
|
// and should never be called more than once
|
2007-10-08 18:50:00 +00:00
|
|
|
ASE_ASSERT (this->val == ASE_NULL);
|
|
|
|
ASE_ASSERT (v != ASE_NULL);
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, v);
|
2007-09-11 22:42:00 +00:00
|
|
|
this->val = v;
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(v) == ASE_AWK_VAL_STR)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
int n = ase_awk_valtonum (
|
2007-10-08 18:50:00 +00:00
|
|
|
this->run->run, v, &this->inum, &this->rnum);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (n == 0)
|
2007-05-12 19:14:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
this->rnum = (ase_real_t)this->inum;
|
2007-10-21 00:06:00 +00:00
|
|
|
this->str.ptr = ((ase_awk_val_str_t*)this->val)->buf;
|
|
|
|
this->str.len = ((ase_awk_val_str_t*)this->val)->len;
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
2007-05-12 19:14:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else if (n == 1)
|
2007-05-12 19:14:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
this->inum = (ase_long_t)this->rnum;
|
2007-10-21 00:06:00 +00:00
|
|
|
this->str.ptr = ((ase_awk_val_str_t*)this->val)->buf;
|
|
|
|
this->str.len = ((ase_awk_val_str_t*)this->val)->len;
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
2007-05-12 19:14:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-10-22 00:05:00 +00:00
|
|
|
else if (ASE_AWK_VAL_TYPE(v) == ASE_AWK_VAL_INT)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
this->inum = ((ase_awk_val_int_t*)v)->val;
|
|
|
|
this->rnum = (ase_real_t)((ase_awk_val_int_t*)v)->val;
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
this->str.ptr = ase_awk_valtostr (
|
2007-10-08 18:50:00 +00:00
|
|
|
this->run->run, v, 0, ASE_NULL, &this->str.len);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (this->str.ptr != ASE_NULL) return 0;
|
|
|
|
}
|
2007-10-22 00:05:00 +00:00
|
|
|
else if (ASE_AWK_VAL_TYPE(v) == ASE_AWK_VAL_REAL)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
this->inum = (ase_long_t)((ase_awk_val_real_t*)v)->val;
|
|
|
|
this->rnum = ((ase_awk_val_real_t*)v)->val;
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
this->str.ptr = ase_awk_valtostr (
|
2007-10-08 18:50:00 +00:00
|
|
|
this->run->run, v, 0, ASE_NULL, &this->str.len);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (this->str.ptr != ASE_NULL) return 0;
|
|
|
|
}
|
2007-10-22 00:05:00 +00:00
|
|
|
else if (ASE_AWK_VAL_TYPE(v) == ASE_AWK_VAL_NIL)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
this->inum = 0;
|
|
|
|
this->rnum = 0.0;
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
this->str.ptr = ase_awk_valtostr (
|
2007-10-08 18:50:00 +00:00
|
|
|
this->run->run, v, 0, ASE_NULL, &this->str.len);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (this->str.ptr != ASE_NULL) return 0;
|
2007-05-12 02:21:00 +00:00
|
|
|
}
|
2007-10-22 00:05:00 +00:00
|
|
|
else if (ASE_AWK_VAL_TYPE(v) == ASE_AWK_VAL_MAP)
|
2007-09-27 20:33:00 +00:00
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
this->inum = 0;
|
|
|
|
this->rnum = 0.0;
|
|
|
|
this->str.ptr = ASE_NULL;
|
|
|
|
this->str.len = 0;
|
|
|
|
return 0;
|
2007-09-27 20:33:00 +00:00
|
|
|
}
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
// an error has occurred
|
2007-10-08 18:50:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, v);
|
2007-09-11 22:42:00 +00:00
|
|
|
this->val = ASE_NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
int Awk::Argument::init (const char_t* str, size_t len)
|
2007-10-02 00:22:00 +00:00
|
|
|
{
|
2007-10-08 18:50:00 +00:00
|
|
|
ASE_ASSERT (this->val == ASE_NULL);
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
this->str.ptr = (char_t*)str;
|
|
|
|
this->str.len = len;
|
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
if (ase_awk_strtonum (this->run->run,
|
|
|
|
str, len, &this->inum, &this->rnum) == 0)
|
2007-10-02 00:22:00 +00:00
|
|
|
{
|
|
|
|
this->rnum = (real_t)this->inum;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->inum = (long_t)this->rnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::long_t Awk::Argument::toInt () const
|
|
|
|
{
|
|
|
|
return this->inum;
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::real_t Awk::Argument::toReal () const
|
|
|
|
{
|
|
|
|
return this->rnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Awk::char_t* Awk::Argument::toStr (size_t* len) const
|
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
|
2007-10-22 00:05:00 +00:00
|
|
|
if (this->val != ASE_NULL &&
|
|
|
|
ASE_AWK_VAL_TYPE(this->val) == ASE_AWK_VAL_MAP)
|
2007-09-25 20:25:00 +00:00
|
|
|
{
|
|
|
|
*len = 0;
|
2007-10-02 00:22:00 +00:00
|
|
|
return ASE_T("");
|
2007-09-25 20:25:00 +00:00
|
|
|
}
|
2007-10-02 00:22:00 +00:00
|
|
|
else if (this->str.ptr == ASE_NULL)
|
2007-05-11 10:07:00 +00:00
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
*len = 0;
|
|
|
|
return ASE_T("");
|
2007-05-11 10:07:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else
|
2007-05-11 10:07:00 +00:00
|
|
|
{
|
2007-10-02 00:22:00 +00:00
|
|
|
*len = this->str.len;
|
|
|
|
return this->str.ptr;
|
2007-05-11 10:07:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
bool Awk::Argument::isIndexed () const
|
|
|
|
{
|
|
|
|
if (this->val == ASE_NULL) return false;
|
2007-10-22 00:05:00 +00:00
|
|
|
return ASE_AWK_VAL_TYPE(this->val) == ASE_AWK_VAL_MAP;
|
2007-10-02 00:22:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 18:47:00 +00:00
|
|
|
int Awk::Argument::getIndexed (const char_t* idxptr, Awk::Argument& val) const
|
2007-10-02 00:22:00 +00:00
|
|
|
{
|
2007-10-03 18:47:00 +00:00
|
|
|
return getIndexed (idxptr, ase_strlen(idxptr), val);
|
2007-10-02 00:22:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-04 23:26:00 +00:00
|
|
|
int Awk::Argument::getIndexed (
|
|
|
|
const char_t* idxptr, size_t idxlen, Awk::Argument& val) const
|
2007-10-02 00:22:00 +00:00
|
|
|
{
|
|
|
|
val.clear ();
|
|
|
|
|
|
|
|
// not initialized yet. val is just nil. not an error
|
|
|
|
if (this->val == ASE_NULL) return 0;
|
|
|
|
// not a map. val is just nil. not an error
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP) return 0;
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
// get the value from the map.
|
|
|
|
ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val;
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_get (m->map, idxptr, idxlen);
|
|
|
|
|
|
|
|
// the key is not found. it is not an error. val is just nil
|
|
|
|
if (pair == ASE_NULL) return 0;
|
|
|
|
|
|
|
|
// if val.init fails, it should return an error
|
2007-10-08 18:50:00 +00:00
|
|
|
return val.init ((val_t*)pair->val);
|
2007-10-02 00:22:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-04 23:26:00 +00:00
|
|
|
int Awk::Argument::getIndexed (long_t idx, Argument& val) const
|
|
|
|
{
|
|
|
|
val.clear ();
|
|
|
|
|
|
|
|
// not initialized yet. val is just nil. not an error
|
|
|
|
if (this->val == ASE_NULL) return 0;
|
|
|
|
|
|
|
|
// not a map. val is just nil. not an error
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP) return 0;
|
2007-10-04 23:26:00 +00:00
|
|
|
|
|
|
|
char_t ri[128];
|
|
|
|
|
|
|
|
int rl = Awk::sprintf (
|
2007-10-14 21:36:00 +00:00
|
|
|
this->run->awk, ri, ASE_COUNTOF(ri),
|
2007-10-04 23:26:00 +00:00
|
|
|
#if ASE_SIZEOF_LONG_LONG > 0
|
|
|
|
ASE_T("%lld"), (long long)idx
|
|
|
|
#elif ASE_SIZEOF___INT64 > 0
|
|
|
|
ASE_T("%I64d"), (__int64)idx
|
|
|
|
#elif ASE_SIZEOF_LONG > 0
|
|
|
|
ASE_T("%ld"), (long)idx
|
|
|
|
#elif ASE_SIZEOF_INT > 0
|
|
|
|
ASE_T("%d"), (int)idx
|
|
|
|
#else
|
|
|
|
#error unsupported size
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rl < 0)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
run->setError (ERR_INTERN, 0, ASE_NULL, 0);
|
2007-10-04 23:26:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the value from the map.
|
|
|
|
ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val;
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_get (m->map, ri, rl);
|
|
|
|
|
|
|
|
// the key is not found. it is not an error. val is just nil
|
|
|
|
if (pair == ASE_NULL) return 0;
|
|
|
|
|
|
|
|
// if val.init fails, it should return an error
|
2007-10-08 18:50:00 +00:00
|
|
|
return val.init ((val_t*)pair->val);
|
2007-10-04 23:26:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
int Awk::Argument::getFirstIndex (Awk::Argument& val) const
|
|
|
|
{
|
|
|
|
val.clear ();
|
|
|
|
|
|
|
|
if (this->val == ASE_NULL) return -1;
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP) return -1;
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
ase_size_t buckno;
|
|
|
|
ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val;
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_getfirstpair (m->map, &buckno);
|
|
|
|
if (pair == ASE_NULL) return 0; // no more key
|
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
if (val.init (pair->key.ptr, pair->key.len) == -1) return -1;
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
// reuse the string field as an interator.
|
|
|
|
this->str.ptr = (char_t*)pair;
|
|
|
|
this->str.len = buckno;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Argument::getNextIndex (Awk::Argument& val) const
|
|
|
|
{
|
|
|
|
val.clear ();
|
|
|
|
|
|
|
|
if (this->val == ASE_NULL) return -1;
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP) return -1;
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
ase_awk_val_map_t* m = (ase_awk_val_map_t*)this->val;
|
|
|
|
|
|
|
|
ase_awk_pair_t* pair = (ase_awk_pair_t*)this->str.ptr;
|
|
|
|
ase_size_t buckno = this->str.len;
|
|
|
|
|
|
|
|
pair = ase_awk_map_getnextpair (m->map, pair, &buckno);
|
|
|
|
if (pair == ASE_NULL) return 0;
|
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
if (val.init (pair->key.ptr, pair->key.len) == -1) return -1;
|
2007-10-02 00:22:00 +00:00
|
|
|
|
|
|
|
// reuse the string field as an interator.
|
|
|
|
this->str.ptr = (char_t*)pair;
|
|
|
|
this->str.len = buckno;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Return
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
Awk::Return::Return (Run& run): run(&run), val(ase_awk_val_nil)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
Awk::Return::Return (Run* run): run(run), val(ase_awk_val_nil)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Return::~Return ()
|
|
|
|
{
|
|
|
|
clear ();
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-10-03 00:21:00 +00:00
|
|
|
Awk::val_t* Awk::Return::toVal () const
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2007-10-03 00:21:00 +00:00
|
|
|
return this->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::Return::operator Awk::val_t* () const
|
|
|
|
{
|
|
|
|
return this->val;
|
|
|
|
}
|
2007-09-25 00:12:00 +00:00
|
|
|
|
2007-10-03 00:21:00 +00:00
|
|
|
int Awk::Return::set (long_t v)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x = ase_awk_makeintval (this->run->run, v);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x == ASE_NULL) return -1;
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = x;
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, this->val);
|
2007-09-11 22:42:00 +00:00
|
|
|
|
2007-10-03 00:21:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Return::set (real_t v)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-09-11 22:42:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x = ase_awk_makerealval (this->run->run, v);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x == ASE_NULL) return -1;
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = x;
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
return 0;
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-03 00:21:00 +00:00
|
|
|
int Awk::Return::set (const char_t* ptr, size_t len)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2007-10-03 00:21:00 +00:00
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x = ase_awk_makestrval (this->run->run, ptr, len);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x == ASE_NULL) return -1;
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = x;
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, this->val);
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-03 18:47:00 +00:00
|
|
|
bool Awk::Return::isIndexed () const
|
|
|
|
{
|
|
|
|
if (this->val == ASE_NULL) return false;
|
2007-10-22 00:05:00 +00:00
|
|
|
return ASE_AWK_VAL_TYPE(this->val) == ASE_AWK_VAL_MAP;
|
2007-10-03 18:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Return::setIndexed (const char_t* idx, size_t iln, long_t v)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2007-10-03 00:21:00 +00:00
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
int opt = this->run->awk->getOption();
|
2007-10-03 00:21:00 +00:00
|
|
|
if ((opt & OPT_MAPTOVAR) == 0)
|
|
|
|
{
|
|
|
|
/* refer to run_return in run.c */
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->setError (ERR_MAPNOTALLOWED, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP)
|
2007-10-03 00:21:00 +00:00
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x = ase_awk_makemapval (this->run->run);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x == ASE_NULL) return -1;
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x2 = ase_awk_makeintval (this->run->run, v);
|
2007-10-03 00:21:00 +00:00
|
|
|
if (x2 == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-12 02:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x2);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_put (
|
|
|
|
((ase_awk_val_map_t*)x)->map, idx, iln, x2);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x2);
|
|
|
|
ase_awk_refdownval (this->run->run, x);
|
|
|
|
this->run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x2 = ase_awk_makeintval (this->run->run, v);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x2 == ASE_NULL) return -1;
|
2007-10-03 00:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x2);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_put (
|
|
|
|
((ase_awk_val_map_t*)this->val)->map, idx, iln, x2);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x2);
|
|
|
|
this->run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-10-03 18:47:00 +00:00
|
|
|
int Awk::Return::setIndexed (const char_t* idx, size_t iln, real_t v)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2007-10-03 00:21:00 +00:00
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
int opt = this->run->awk->getOption();
|
2007-10-03 00:21:00 +00:00
|
|
|
if ((opt & OPT_MAPTOVAR) == 0)
|
|
|
|
{
|
|
|
|
/* refer to run_return in run.c */
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->setError (ERR_MAPNOTALLOWED, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP)
|
2007-10-03 00:21:00 +00:00
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x = ase_awk_makemapval (this->run->run);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x == ASE_NULL) return -1;
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x2 = ase_awk_makerealval (this->run->run, v);
|
2007-10-03 00:21:00 +00:00
|
|
|
if (x2 == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x2);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_put (
|
|
|
|
((ase_awk_val_map_t*)x)->map, idx, iln, x2);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x2);
|
|
|
|
ase_awk_refdownval (this->run->run, x);
|
|
|
|
this->run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x2 = ase_awk_makerealval (this->run->run, v);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x2 == ASE_NULL) return -1;
|
2007-10-03 00:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x2);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_put (
|
|
|
|
((ase_awk_val_map_t*)this->val)->map, idx, iln, x2);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x2);
|
|
|
|
this->run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-10-03 18:47:00 +00:00
|
|
|
int Awk::Return::setIndexed (const char_t* idx, size_t iln, const char_t* str, size_t sln)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
2007-10-03 00:21:00 +00:00
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
int opt = this->run->awk->getOption();
|
2007-10-03 00:21:00 +00:00
|
|
|
if ((opt & OPT_MAPTOVAR) == 0)
|
2007-05-11 10:07:00 +00:00
|
|
|
{
|
2007-10-03 00:21:00 +00:00
|
|
|
/* refer to run_return in run.c */
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->setError (ERR_MAPNOTALLOWED, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-10-22 00:05:00 +00:00
|
|
|
if (ASE_AWK_VAL_TYPE(this->val) != ASE_AWK_VAL_MAP)
|
2007-10-03 00:21:00 +00:00
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x = ase_awk_makemapval (this->run->run);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x == ASE_NULL) return -1;
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x2 = ase_awk_makestrval (this->run->run, str, sln);
|
2007-10-03 00:21:00 +00:00
|
|
|
if (x2 == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x2);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_put (
|
|
|
|
((ase_awk_val_map_t*)x)->map, idx, iln, x2);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x2);
|
|
|
|
ase_awk_refdownval (this->run->run, x);
|
|
|
|
this->run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_val_t* x2 = ase_awk_makestrval (this->run->run, str, sln);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (x2 == ASE_NULL) return -1;
|
2007-10-03 00:21:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refupval (this->run->run, x2);
|
2007-10-03 00:21:00 +00:00
|
|
|
|
|
|
|
ase_awk_pair_t* pair = ase_awk_map_put (
|
|
|
|
((ase_awk_val_map_t*)this->val)->map, idx, iln, x2);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, x2);
|
|
|
|
this->run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-10-03 00:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-03 18:47:00 +00:00
|
|
|
int Awk::Return::setIndexed (long_t idx, long_t v)
|
|
|
|
{
|
|
|
|
char_t ri[128];
|
|
|
|
|
|
|
|
int rl = Awk::sprintf (
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->awk, ri, ASE_COUNTOF(ri),
|
2007-10-03 18:47:00 +00:00
|
|
|
#if ASE_SIZEOF_LONG_LONG > 0
|
|
|
|
ASE_T("%lld"), (long long)idx
|
|
|
|
#elif ASE_SIZEOF___INT64 > 0
|
|
|
|
ASE_T("%I64d"), (__int64)idx
|
|
|
|
#elif ASE_SIZEOF_LONG > 0
|
|
|
|
ASE_T("%ld"), (long)idx
|
|
|
|
#elif ASE_SIZEOF_INT > 0
|
|
|
|
ASE_T("%d"), (int)idx
|
|
|
|
#else
|
|
|
|
#error unsupported size
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rl < 0)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->setError (ERR_INTERN, 0, ASE_NULL, 0);
|
2007-10-03 18:47:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return setIndexed (ri, rl, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Return::setIndexed (long_t idx, real_t v)
|
|
|
|
{
|
|
|
|
char_t ri[128];
|
|
|
|
|
|
|
|
int rl = Awk::sprintf (
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->awk, ri, ASE_COUNTOF(ri),
|
2007-10-03 18:47:00 +00:00
|
|
|
#if ASE_SIZEOF_LONG_LONG > 0
|
|
|
|
ASE_T("%lld"), (long long)idx
|
|
|
|
#elif ASE_SIZEOF___INT64 > 0
|
|
|
|
ASE_T("%I64d"), (__int64)idx
|
|
|
|
#elif ASE_SIZEOF_LONG > 0
|
|
|
|
ASE_T("%ld"), (long)idx
|
|
|
|
#elif ASE_SIZEOF_INT > 0
|
|
|
|
ASE_T("%d"), (int)idx
|
|
|
|
#else
|
|
|
|
#error unsupported size
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rl < 0)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->setError (ERR_INTERN, 0, ASE_NULL, 0);
|
2007-10-03 18:47:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return setIndexed (ri, rl, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Return::setIndexed (long_t idx, const char_t* str, size_t sln)
|
|
|
|
{
|
|
|
|
char_t ri[128];
|
|
|
|
|
|
|
|
int rl = Awk::sprintf (
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->awk, ri, ASE_COUNTOF(ri),
|
2007-10-03 18:47:00 +00:00
|
|
|
#if ASE_SIZEOF_LONG_LONG > 0
|
|
|
|
ASE_T("%lld"), (long long)idx
|
|
|
|
#elif ASE_SIZEOF___INT64 > 0
|
|
|
|
ASE_T("%I64d"), (__int64)idx
|
|
|
|
#elif ASE_SIZEOF_LONG > 0
|
|
|
|
ASE_T("%ld"), (long)idx
|
|
|
|
#elif ASE_SIZEOF_INT > 0
|
|
|
|
ASE_T("%d"), (int)idx
|
|
|
|
#else
|
|
|
|
#error unsupported size
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rl < 0)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
this->run->setError (ERR_INTERN, 0, ASE_NULL, 0);
|
2007-10-03 18:47:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return setIndexed (ri, rl, str, sln);
|
|
|
|
}
|
|
|
|
|
2007-10-03 00:21:00 +00:00
|
|
|
void Awk::Return::clear ()
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_refdownval (this->run->run, this->val);
|
2007-10-03 00:21:00 +00:00
|
|
|
this->val = ase_awk_val_nil;
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk::Run
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::Run::Run (Awk* awk):
|
|
|
|
awk (awk), run (ASE_NULL), callbackFailed (false)
|
|
|
|
{
|
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
Awk::Run::Run (Awk* awk, run_t* run):
|
2007-10-08 18:50:00 +00:00
|
|
|
awk (awk), run (run), callbackFailed (false), custom (ASE_NULL)
|
2007-09-25 00:12:00 +00:00
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::Run::~Run ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-10-10 16:03:00 +00:00
|
|
|
Awk::Run::operator Awk* () const
|
|
|
|
{
|
|
|
|
return this->awk;
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::Run::operator Awk::run_t* () const
|
|
|
|
{
|
|
|
|
return this->run;
|
|
|
|
}
|
|
|
|
|
2007-10-12 00:13:00 +00:00
|
|
|
void Awk::Run::stop () const
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-10-12 00:13:00 +00:00
|
|
|
ase_awk_stop (this->run);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Awk::Run::isStop () const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-10-16 01:15:00 +00:00
|
|
|
return ase_awk_isstop (this->run)? true: false;
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-11 10:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ErrorCode Awk::Run::getErrorCode () const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
return (ErrorCode)ase_awk_getrunerrnum (this->run);
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::size_t Awk::Run::getErrorLine () const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
return ase_awk_getrunerrlin (this->run);
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const Awk::char_t* Awk::Run::getErrorMessage () const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
return ase_awk_getrunerrmsg (this->run);
|
|
|
|
}
|
|
|
|
|
2007-10-10 22:33:00 +00:00
|
|
|
void Awk::Run::setError (ErrorCode code)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
ase_awk_setrunerror (this->run, code, 0, ASE_NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::Run::setError (ErrorCode code, size_t line)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
ase_awk_setrunerror (this->run, code, line, ASE_NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::Run::setError (ErrorCode code, size_t line, const char_t* arg)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
ase_cstr_t x = { arg, ase_strlen(arg) };
|
|
|
|
ase_awk_setrunerror (this->run, code, line, &x, 1);
|
|
|
|
}
|
|
|
|
|
2007-10-02 00:22:00 +00:00
|
|
|
void Awk::Run::setError (
|
|
|
|
ErrorCode code, size_t line, const char_t* arg, size_t len)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
ase_cstr_t x = { arg, len };
|
|
|
|
ase_awk_setrunerror (this->run, code, line, &x, 1);
|
|
|
|
}
|
|
|
|
|
2007-10-10 22:33:00 +00:00
|
|
|
void Awk::Run::setErrorWithMessage (
|
2007-10-02 00:22:00 +00:00
|
|
|
ErrorCode code, size_t line, const char_t* msg)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
ase_awk_setrunerrmsg (this->run, code, line, msg);
|
|
|
|
}
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int Awk::Run::setGlobal (int id, long_t v)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
|
|
|
ase_awk_val_t* tmp = ase_awk_makeintval (run, v);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (tmp == ASE_NULL) return -1;
|
2007-09-25 00:12:00 +00:00
|
|
|
|
|
|
|
ase_awk_refupval (run, tmp);
|
|
|
|
int n = ase_awk_setglobal (this->run, id, tmp);
|
|
|
|
ase_awk_refdownval (run, tmp);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Run::setGlobal (int id, real_t v)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
|
|
|
ase_awk_val_t* tmp = ase_awk_makerealval (run, v);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (tmp == ASE_NULL) return -1;
|
2007-09-25 00:12:00 +00:00
|
|
|
|
|
|
|
ase_awk_refupval (run, tmp);
|
|
|
|
int n = ase_awk_setglobal (this->run, id, tmp);
|
|
|
|
ase_awk_refdownval (run, tmp);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::Run::setGlobal (int id, const char_t* ptr, size_t len)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (run != ASE_NULL);
|
|
|
|
|
|
|
|
ase_awk_val_t* tmp = ase_awk_makestrval (run, ptr, len);
|
2007-10-24 00:18:00 +00:00
|
|
|
if (tmp == ASE_NULL) return -1;
|
2007-09-25 00:12:00 +00:00
|
|
|
|
|
|
|
ase_awk_refupval (run, tmp);
|
|
|
|
int n = ase_awk_setglobal (this->run, id, tmp);
|
|
|
|
ase_awk_refdownval (run, tmp);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
int Awk::Run::setGlobal (int id, const Return& global)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
|
|
|
|
|
|
|
return ase_awk_setglobal (this->run, id, global.toVal());
|
|
|
|
}
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int Awk::Run::getGlobal (int id, Argument& global) const
|
|
|
|
{
|
2007-10-08 18:50:00 +00:00
|
|
|
ASE_ASSERT (this->run != ASE_NULL);
|
2007-09-25 00:12:00 +00:00
|
|
|
|
|
|
|
global.clear ();
|
2007-10-08 18:50:00 +00:00
|
|
|
return global.init (ase_awk_getglobal(this->run,id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::Run::setCustom (void* custom)
|
|
|
|
{
|
|
|
|
this->custom = custom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Awk::Run::getCustom () const
|
|
|
|
{
|
|
|
|
return this->custom;
|
2007-09-25 00:12:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Awk
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Awk::Awk (): awk (ASE_NULL), functionMap (ASE_NULL),
|
|
|
|
sourceIn (Source::READ), sourceOut (Source::WRITE),
|
|
|
|
errnum (ERR_NOERR), errlin (0), runCallback (false)
|
|
|
|
|
|
|
|
{
|
|
|
|
this->errmsg[0] = ASE_T('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::~Awk ()
|
|
|
|
{
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-10-10 16:03:00 +00:00
|
|
|
Awk::operator Awk::awk_t* () const
|
|
|
|
{
|
|
|
|
return this->awk;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ErrorCode Awk::getErrorCode () const
|
|
|
|
{
|
|
|
|
return this->errnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::size_t Awk::getErrorLine () const
|
|
|
|
{
|
|
|
|
return this->errlin;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Awk::char_t* Awk::getErrorMessage () const
|
|
|
|
{
|
|
|
|
return this->errmsg;
|
|
|
|
}
|
|
|
|
|
2007-10-10 22:33:00 +00:00
|
|
|
void Awk::setError (ErrorCode code)
|
|
|
|
{
|
|
|
|
setError (code, 0, ASE_NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::setError (ErrorCode code, size_t line)
|
|
|
|
{
|
|
|
|
setError (code, line, ASE_NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::setError (ErrorCode code, size_t line, const char_t* arg)
|
|
|
|
{
|
|
|
|
setError (code, line, arg, ase_strlen(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::setError (ErrorCode code, size_t line, const char_t* arg, size_t len)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
if (awk != ASE_NULL)
|
2007-05-21 01:22:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_cstr_t x = { arg, len };
|
|
|
|
ase_awk_seterror (awk, code, line, &x, 1);
|
|
|
|
retrieveError ();
|
2007-05-21 01:22:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else
|
2007-05-21 01:22:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
this->errnum = code;
|
|
|
|
this->errlin = line;
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const char_t* es = ase_awk_geterrstr (ASE_NULL, code);
|
|
|
|
ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), es);
|
2007-05-16 01:03:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-10-10 22:33:00 +00:00
|
|
|
void Awk::setErrorWithMessage (ErrorCode code, size_t line, const char_t* msg)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
if (awk != ASE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_seterrmsg (awk, code, line, msg);
|
|
|
|
retrieveError ();
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
this->errnum = code;
|
|
|
|
this->errlin = line;
|
|
|
|
ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), msg);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::clearError ()
|
|
|
|
{
|
|
|
|
this->errnum = ERR_NOERR;
|
|
|
|
this->errlin = 0;
|
|
|
|
this->errmsg[0] = ASE_T('\0');
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::retrieveError ()
|
|
|
|
{
|
|
|
|
if (this->awk == ASE_NULL)
|
2007-05-19 01:30:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
clearError ();
|
2007-05-19 01:30:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else
|
2007-05-19 01:30:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
int num;
|
|
|
|
const char_t* msg;
|
|
|
|
|
|
|
|
ase_awk_geterror (this->awk, &num, &this->errlin, &msg);
|
|
|
|
this->errnum = (ErrorCode)num;
|
|
|
|
ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), msg);
|
2007-05-19 01:30:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::open ()
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk == ASE_NULL && functionMap == ASE_NULL);
|
|
|
|
|
|
|
|
ase_awk_prmfns_t prmfns;
|
|
|
|
|
|
|
|
prmfns.mmgr.malloc = allocMem;
|
|
|
|
prmfns.mmgr.realloc = reallocMem;
|
|
|
|
prmfns.mmgr.free = freeMem;
|
|
|
|
prmfns.mmgr.custom_data = this;
|
|
|
|
|
|
|
|
prmfns.ccls.is_upper = isUpper;
|
|
|
|
prmfns.ccls.is_lower = isLower;
|
|
|
|
prmfns.ccls.is_alpha = isAlpha;
|
|
|
|
prmfns.ccls.is_digit = isDigit;
|
|
|
|
prmfns.ccls.is_xdigit = isXdigit;
|
|
|
|
prmfns.ccls.is_alnum = isAlnum;
|
|
|
|
prmfns.ccls.is_space = isSpace;
|
|
|
|
prmfns.ccls.is_print = isPrint;
|
|
|
|
prmfns.ccls.is_graph = isGraph;
|
|
|
|
prmfns.ccls.is_cntrl = isCntrl;
|
|
|
|
prmfns.ccls.is_punct = isPunct;
|
|
|
|
prmfns.ccls.to_upper = toUpper;
|
|
|
|
prmfns.ccls.to_lower = toLower;
|
|
|
|
prmfns.ccls.custom_data = this;
|
|
|
|
|
|
|
|
prmfns.misc.pow = pow;
|
|
|
|
prmfns.misc.sprintf = sprintf;
|
|
|
|
prmfns.misc.dprintf = dprintf;
|
|
|
|
prmfns.misc.custom_data = this;
|
|
|
|
|
|
|
|
awk = ase_awk_open (&prmfns, this);
|
|
|
|
if (awk == ASE_NULL)
|
|
|
|
{
|
|
|
|
setError (ERR_NOMEM);
|
|
|
|
return -1;
|
2007-05-21 01:22:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
functionMap = ase_awk_map_open (
|
|
|
|
this, 512, 70, freeFunctionMapValue, awk);
|
|
|
|
if (functionMap == ASE_NULL)
|
2007-05-19 01:30:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_close (awk);
|
|
|
|
awk = ASE_NULL;
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
setError (ERR_NOMEM);
|
|
|
|
return -1;
|
2007-05-19 01:30:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int opt =
|
|
|
|
OPT_IMPLICIT |
|
|
|
|
OPT_UNIQUEFN |
|
|
|
|
OPT_SHADING |
|
|
|
|
OPT_EXTIO |
|
|
|
|
OPT_BLOCKLESS |
|
2007-10-12 00:13:00 +00:00
|
|
|
OPT_BASEONE |
|
|
|
|
OPT_PABLOCK;
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_setoption (awk, opt);
|
|
|
|
|
|
|
|
runCallback = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::close ()
|
|
|
|
{
|
|
|
|
if (functionMap != ASE_NULL)
|
2007-08-26 23:33:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_map_close (functionMap);
|
|
|
|
functionMap = ASE_NULL;
|
2007-08-26 23:33:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (awk != ASE_NULL)
|
2007-05-19 01:30:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_close (awk);
|
|
|
|
awk = ASE_NULL;
|
2007-05-21 01:22:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
clearError ();
|
|
|
|
runCallback = false;
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::setOption (int opt)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
ase_awk_setoption (awk, opt);
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::getOption () const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_getoption (awk);
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::setMaxDepth (int ids, size_t depth)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
ase_awk_setmaxdepth (awk, ids, depth);
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::size_t Awk::getMaxDepth (int id) const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_getmaxdepth (awk, id);
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
const Awk::char_t* Awk::getErrorString (ErrorCode num) const
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_geterrstr (awk, (int)num);
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::setErrorString (ErrorCode num, const char_t* str)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_seterrstr (awk, (int)num, str);
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::setWord (const char_t* ow, const char_t* nw)
|
|
|
|
{
|
|
|
|
return setWord (ow, ase_strlen(ow), nw, ase_strlen(nw));
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::setWord (
|
|
|
|
const char_t* ow, ase_size_t owl,
|
|
|
|
const char_t* nw, ase_size_t nwl)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_setword (awk, ow, owl, nw, nwl);
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::unsetWord (const char_t* ow)
|
|
|
|
{
|
|
|
|
return unsetWord (ow, ase_strlen(ow));
|
|
|
|
}
|
2007-05-19 01:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::unsetWord (const char_t* ow, ase_size_t owl)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_setword (awk, ow, owl, ASE_NULL, 0);
|
|
|
|
}
|
2007-05-24 15:56:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::unsetAllWords ()
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
return ase_awk_setword (awk, ASE_NULL, 0, ASE_NULL, 0);
|
|
|
|
}
|
2007-05-24 15:56:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::parse ()
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
2007-08-26 23:33:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_srcios_t srcios;
|
2007-06-24 20:14:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
srcios.in = sourceReader;
|
|
|
|
srcios.out = sourceWriter;
|
|
|
|
srcios.custom_data = this;
|
2007-06-19 23:59:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int n = ase_awk_parse (awk, &srcios);
|
|
|
|
if (n == -1) retrieveError ();
|
|
|
|
return n;
|
|
|
|
}
|
2007-06-19 23:59:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::run (const char_t* main, const char_t** args, size_t nargs)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
2007-06-29 20:40:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
size_t i;
|
|
|
|
ase_awk_runios_t runios;
|
|
|
|
ase_awk_runcbs_t runcbs;
|
|
|
|
ase_awk_runarg_t* runarg = ASE_NULL;
|
2007-10-08 00:27:00 +00:00
|
|
|
|
|
|
|
// make sure that the run field is set in Awk::onRunStart.
|
|
|
|
Run runctx (this);
|
2007-06-29 20:40:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
runios.pipe = pipeHandler;
|
|
|
|
runios.coproc = ASE_NULL;
|
|
|
|
runios.file = fileHandler;
|
|
|
|
runios.console = consoleHandler;
|
|
|
|
runios.custom_data = this;
|
2007-06-29 20:40:00 +00:00
|
|
|
|
2007-10-16 01:15:00 +00:00
|
|
|
ase_memset (&runcbs, 0, ASE_SIZEOF(runcbs));
|
|
|
|
runcbs.on_start = onRunStart;
|
2007-09-11 22:42:00 +00:00
|
|
|
if (runCallback)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
runcbs.on_end = onRunEnd;
|
|
|
|
runcbs.on_return = onRunReturn;
|
|
|
|
runcbs.on_statement = onRunStatement;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2007-10-16 01:15:00 +00:00
|
|
|
runcbs.custom_data = &runctx;
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (nargs > 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
runarg = (ase_awk_runarg_t*) ase_awk_malloc (
|
|
|
|
awk, ASE_SIZEOF(ase_awk_runarg_t)*(nargs+1));
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (runarg == ASE_NULL)
|
2007-05-21 01:22:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
setError (ERR_NOMEM);
|
|
|
|
return -1;
|
2007-05-21 01:22:00 +00:00
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
for (i = 0; i < nargs; i++)
|
2007-05-15 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
runarg[i].len = ase_strlen (args[i]);
|
|
|
|
runarg[i].ptr = ase_awk_strxdup (awk, args[i], runarg[i].len);
|
|
|
|
if (runarg[i].ptr == ASE_NULL)
|
2007-05-15 01:21:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
while (i > 0) ase_awk_free (awk, runarg[--i].ptr);
|
|
|
|
ase_awk_free (awk, runarg);
|
2007-06-28 00:27:00 +00:00
|
|
|
setError (ERR_NOMEM);
|
2007-05-15 01:21:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
runarg[i].ptr = ASE_NULL;
|
|
|
|
runarg[i].len = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
|
2007-10-16 01:15:00 +00:00
|
|
|
int n = ase_awk_run (awk, main, &runios, &runcbs, runarg, &runctx);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (n == -1) retrieveError ();
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (runarg != ASE_NULL)
|
2007-05-06 19:44:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
while (i > 0) ase_awk_free (awk, runarg[--i].ptr);
|
|
|
|
ase_awk_free (awk, runarg);
|
|
|
|
}
|
2007-05-12 19:14:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return n;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-10-12 00:13:00 +00:00
|
|
|
void Awk::stop ()
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
ase_awk_stopall (awk);
|
|
|
|
}
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
int Awk::dispatchFunction (Run* run, const char_t* name, size_t len)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
pair_t* pair;
|
2007-10-08 00:27:00 +00:00
|
|
|
awk_t* awk = run->awk->awk;
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
//awk = ase_awk_getrunawk (run);
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
pair = ase_awk_map_get (functionMap, name, len);
|
2007-09-25 00:12:00 +00:00
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
run->setError (ERR_FNNONE, 0, name, len);
|
2007-09-25 00:12:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-09 00:09:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
FunctionHandler handler;
|
|
|
|
handler = *(FunctionHandler*)ASE_AWK_PAIR_VAL(pair);
|
2007-05-09 00:09:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
size_t i, nargs = ase_awk_getnargs(run->run);
|
2007-09-11 22:42:00 +00:00
|
|
|
|
|
|
|
//Argument* args = ASE_NULL;
|
|
|
|
//try { args = new Argument [nargs]; } catch (...) {}
|
|
|
|
Argument* args = new(awk) Argument[nargs];
|
2007-09-25 00:12:00 +00:00
|
|
|
if (args == ASE_NULL)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-09-25 00:12:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2007-10-08 18:50:00 +00:00
|
|
|
args[i].run = run; // dirty late initialization
|
|
|
|
// due to c++ array creation limitation.
|
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
val_t* v = ase_awk_getarg (run->run, i);
|
2007-10-08 18:50:00 +00:00
|
|
|
if (args[i].init (v) == -1)
|
2007-05-09 00:09:00 +00:00
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
run->setError (ERR_NOMEM, 0, ASE_NULL, 0);
|
2007-09-11 22:42:00 +00:00
|
|
|
delete[] args;
|
|
|
|
return -1;
|
2007-05-09 00:09:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 00:21:00 +00:00
|
|
|
Return ret (run);
|
2007-09-25 00:12:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
int n = (this->*handler) (*run, ret, args, nargs, name, len);
|
2007-09-11 22:42:00 +00:00
|
|
|
|
|
|
|
delete[] args;
|
2007-05-09 00:09:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
|
|
|
/* this is really the handler error. the underlying engine
|
|
|
|
* will take care of the error code. */
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-09 00:09:00 +00:00
|
|
|
|
2007-10-08 00:27:00 +00:00
|
|
|
ase_awk_setretval (run->run, ret);
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int Awk::addGlobal (const char_t* name)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
|
|
|
|
int n = ase_awk_addglobal (awk, name, ase_strlen(name));
|
|
|
|
if (n == -1) retrieveError ();
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::deleteGlobal (const char_t* name)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
|
|
|
int n = ase_awk_delglobal (awk, name, ase_strlen(name));
|
|
|
|
if (n == -1) retrieveError ();
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::addFunction (
|
|
|
|
const char_t* name, size_t minArgs, size_t maxArgs,
|
|
|
|
FunctionHandler handler)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
2007-05-09 00:09:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
FunctionHandler* tmp = (FunctionHandler*)
|
|
|
|
ase_awk_malloc (awk, ASE_SIZEOF(handler));
|
|
|
|
if (tmp == ASE_NULL)
|
|
|
|
{
|
|
|
|
setError (ERR_NOMEM);
|
|
|
|
return -1;
|
2007-05-06 19:44:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
//ase_memcpy (tmp, &handler, ASE_SIZEOF(handler));
|
|
|
|
*tmp = handler;
|
|
|
|
|
|
|
|
size_t nameLen = ase_strlen(name);
|
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
void* p = ase_awk_addfunc (awk, name, nameLen,
|
2007-09-11 22:42:00 +00:00
|
|
|
0, minArgs, maxArgs, ASE_NULL,
|
|
|
|
functionHandler);
|
|
|
|
if (p == ASE_NULL)
|
2007-05-06 19:44:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_free (awk, tmp);
|
|
|
|
retrieveError ();
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
pair_t* pair = ase_awk_map_put (functionMap, name, nameLen, tmp);
|
|
|
|
if (pair == ASE_NULL)
|
|
|
|
{
|
2007-09-25 00:12:00 +00:00
|
|
|
ase_awk_delfunc (awk, name, nameLen);
|
2007-09-11 22:42:00 +00:00
|
|
|
ase_awk_free (awk, tmp);
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
setError (ERR_NOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::deleteFunction (const char_t* name)
|
|
|
|
{
|
|
|
|
ASE_ASSERT (awk != ASE_NULL);
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
size_t nameLen = ase_strlen(name);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-25 00:12:00 +00:00
|
|
|
int n = ase_awk_delfunc (awk, name, nameLen);
|
2007-09-11 22:42:00 +00:00
|
|
|
if (n == 0) ase_awk_map_remove (functionMap, name, nameLen);
|
|
|
|
else retrieveError ();
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return n;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::enableRunCallback ()
|
|
|
|
{
|
|
|
|
runCallback = true;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::disableRunCallback ()
|
|
|
|
{
|
|
|
|
runCallback = false;
|
|
|
|
}
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-10-16 01:15:00 +00:00
|
|
|
void Awk::triggerOnRunStart (Run& run)
|
|
|
|
{
|
|
|
|
if (runCallback) onRunStart (run);
|
|
|
|
}
|
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
void Awk::onRunStart (Run& run)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
void Awk::onRunEnd (Run& run)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
void Awk::onRunReturn (Run& run, const Argument& ret)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
void Awk::onRunStatement (Run& run, size_t line)
|
2007-09-11 22:42:00 +00:00
|
|
|
{
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ssize_t Awk::sourceReader (
|
|
|
|
int cmd, void* arg, char_t* data, size_t count)
|
|
|
|
{
|
|
|
|
Awk* awk = (Awk*)arg;
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
switch (cmd)
|
2007-05-21 01:22:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
return awk->openSource (awk->sourceIn);
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
return awk->closeSource (awk->sourceIn);
|
|
|
|
case ASE_AWK_IO_READ:
|
|
|
|
return awk->readSource (awk->sourceIn, data, count);
|
2007-05-21 01:22:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ssize_t Awk::sourceWriter (
|
|
|
|
int cmd, void* arg, char_t* data, size_t count)
|
|
|
|
{
|
|
|
|
Awk* awk = (Awk*)arg;
|
2007-05-04 20:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
switch (cmd)
|
2007-05-04 00:14:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
return awk->openSource (awk->sourceOut);
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
return awk->closeSource (awk->sourceOut);
|
|
|
|
case ASE_AWK_IO_WRITE:
|
|
|
|
return awk->writeSource (awk->sourceOut, data, count);
|
2007-05-06 19:44:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ssize_t Awk::pipeHandler (
|
|
|
|
int cmd, void* arg, char_t* data, size_t count)
|
|
|
|
{
|
|
|
|
extio_t* extio = (extio_t*)arg;
|
|
|
|
Awk* awk = (Awk*)extio->custom_data;
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_PIPE);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Pipe pipe (extio);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
return awk->openPipe (pipe);
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
return awk->closePipe (pipe);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_READ:
|
|
|
|
return awk->readPipe (pipe, data, count);
|
|
|
|
case ASE_AWK_IO_WRITE:
|
|
|
|
return awk->writePipe (pipe, data, count);
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_FLUSH:
|
|
|
|
return awk->flushPipe (pipe);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_NEXT:
|
|
|
|
return -1;
|
2007-05-06 19:44:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ssize_t Awk::fileHandler (
|
|
|
|
int cmd, void* arg, char_t* data, size_t count)
|
|
|
|
{
|
|
|
|
extio_t* extio = (extio_t*)arg;
|
|
|
|
Awk* awk = (Awk*)extio->custom_data;
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_FILE);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
File file (extio);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
return awk->openFile (file);
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
return awk->closeFile (file);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_READ:
|
|
|
|
return awk->readFile (file, data, count);
|
|
|
|
case ASE_AWK_IO_WRITE:
|
|
|
|
return awk->writeFile (file, data, count);
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_FLUSH:
|
|
|
|
return awk->flushFile (file);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_NEXT:
|
|
|
|
return -1;
|
2007-05-06 19:44:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::ssize_t Awk::consoleHandler (
|
|
|
|
int cmd, void* arg, char_t* data, size_t count)
|
|
|
|
{
|
|
|
|
extio_t* extio = (extio_t*)arg;
|
|
|
|
Awk* awk = (Awk*)extio->custom_data;
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_CONSOLE);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Console console (extio);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
switch (cmd)
|
2007-05-07 01:05:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
return awk->openConsole (console);
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
return awk->closeConsole (console);
|
2007-05-07 01:05:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_READ:
|
|
|
|
return awk->readConsole (console, data, count);
|
|
|
|
case ASE_AWK_IO_WRITE:
|
|
|
|
return awk->writeConsole (console, data, count);
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
case ASE_AWK_IO_FLUSH:
|
|
|
|
return awk->flushConsole (console);
|
|
|
|
case ASE_AWK_IO_NEXT:
|
|
|
|
return awk->nextConsole (console);
|
2007-05-16 01:03:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
int Awk::functionHandler (
|
|
|
|
run_t* run, const char_t* name, size_t len)
|
|
|
|
{
|
2007-10-08 00:27:00 +00:00
|
|
|
Run* ctx = (Run*) ase_awk_getruncustomdata (run);
|
|
|
|
Awk* awk = ctx->awk;
|
|
|
|
return awk->dispatchFunction (ctx, name, len);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::freeFunctionMapValue (void* owner, void* value)
|
|
|
|
{
|
|
|
|
Awk* awk = (Awk*)owner;
|
|
|
|
ase_awk_free (awk->awk, value);
|
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::onRunStart (run_t* run, void* custom)
|
|
|
|
{
|
|
|
|
Run* r = (Run*)custom;
|
2007-09-25 00:12:00 +00:00
|
|
|
|
|
|
|
// the actual run_t value for the run-time callback is set here.
|
2007-10-08 00:27:00 +00:00
|
|
|
// r here refers to runctx declared in Awk::run. As onRunStart
|
|
|
|
// is executed immediately after the run method is invoked,
|
|
|
|
// the run field can be set safely here. This seems to be the
|
|
|
|
// only place to acquire the run_t value safely as Awk::run
|
|
|
|
// is blocking.
|
2007-09-25 00:12:00 +00:00
|
|
|
r->run = run;
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
r->callbackFailed = false;
|
2007-10-16 01:15:00 +00:00
|
|
|
r->awk->triggerOnRunStart (*r);
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-21 01:22:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::onRunEnd (run_t* run, int errnum, void* custom)
|
|
|
|
{
|
|
|
|
Run* r = (Run*)custom;
|
2007-05-16 01:03:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
if (errnum == ERR_NOERR && r->callbackFailed)
|
2007-05-04 20:30:00 +00:00
|
|
|
{
|
2007-10-30 00:20:00 +00:00
|
|
|
ase_awk_setrunerrnum (r->run, ERR_NOMEM);
|
2007-05-04 20:30:00 +00:00
|
|
|
}
|
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
r->awk->onRunEnd (*r);
|
|
|
|
}
|
2007-05-04 20:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::onRunReturn (run_t* run, val_t* ret, void* custom)
|
|
|
|
{
|
|
|
|
Run* r = (Run*)custom;
|
|
|
|
if (r->callbackFailed) return;
|
2007-05-04 20:30:00 +00:00
|
|
|
|
2007-10-08 18:50:00 +00:00
|
|
|
Argument x (r);
|
|
|
|
if (x.init (ret) == -1)
|
2007-05-04 20:30:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
r->callbackFailed = true;
|
2007-05-04 20:30:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
else
|
2007-05-04 20:30:00 +00:00
|
|
|
{
|
2007-09-11 22:42:00 +00:00
|
|
|
r->awk->onRunReturn (*r, x);
|
2007-05-04 20:30:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
}
|
2007-05-04 20:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void Awk::onRunStatement (run_t* run, size_t line, void* custom)
|
|
|
|
{
|
|
|
|
Run* r = (Run*)custom;
|
|
|
|
if (r->callbackFailed) return;
|
|
|
|
r->awk->onRunStatement (*r, line);
|
|
|
|
}
|
2007-05-04 20:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
void* Awk::allocMem (void* custom, size_t n)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->allocMem (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Awk::reallocMem (void* custom, void* ptr, size_t n)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->reallocMem (ptr, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::freeMem (void* custom, void* ptr)
|
|
|
|
{
|
|
|
|
((Awk*)custom)->freeMem (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isUpper (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isUpper (c);
|
|
|
|
}
|
2007-05-04 20:30:00 +00:00
|
|
|
|
2007-09-11 22:42:00 +00:00
|
|
|
Awk::bool_t Awk::isLower (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isLower (c);
|
2007-05-04 00:14:00 +00:00
|
|
|
}
|
2007-09-11 22:42:00 +00:00
|
|
|
|
|
|
|
Awk::bool_t Awk::isAlpha (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isAlpha (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isDigit (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isDigit (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isXdigit (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isXdigit (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isAlnum (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isAlnum (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isSpace (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isSpace (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isPrint (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isPrint (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isGraph (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isGraph (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isCntrl (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isCntrl (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::bool_t Awk::isPunct (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->isPunct (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::cint_t Awk::toUpper (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->toUpper (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::cint_t Awk::toLower (void* custom, cint_t c)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->toLower (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Awk::real_t Awk::pow (void* custom, real_t x, real_t y)
|
|
|
|
{
|
|
|
|
return ((Awk*)custom)->pow (x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Awk::sprintf (void* custom, char_t* buf, size_t size,
|
|
|
|
const char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
int n = ((Awk*)custom)->vsprintf (buf, size, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Awk::dprintf (void* custom, const char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
((Awk*)custom)->vdprintf (fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
ASE_END_NAMESPACE(ASE)
|
|
|
|
/////////////////////////////////
|
|
|
|
|