From dc2af2c9adb600c122b38a608ea84b6b58dd9a57 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 11 Sep 2007 22:42:00 +0000 Subject: [PATCH] Recovered from cvs revision 2007-09-08 13:08:00 --- ase/awk/Awk.cpp | 2128 ++++++++++++++++++++++---------------------- ase/awk/Awk.hpp | 1306 +++++++++++++-------------- ase/awk/StdAwk.cpp | 913 +++++++++---------- ase/awk/StdAwk.hpp | 157 ++-- ase/cmn/macros.h | 10 +- 5 files changed, 2269 insertions(+), 2245 deletions(-) diff --git a/ase/awk/Awk.cpp b/ase/awk/Awk.cpp index 011ce335..da0e6d31 100644 --- a/ase/awk/Awk.cpp +++ b/ase/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp,v 1.53 2007/09/06 08:44:42 bacon Exp $ + * $Id: Awk.cpp,v 1.54 2007/09/07 05:40:16 bacon Exp $ */ @@ -8,1140 +8,1144 @@ #include #include -namespace ASE +///////////////////////////////// +ASE_BEGIN_NAMESPACE(ASE) +///////////////////////////////// + +////////////////////////////////////////////////////////////////// +// Awk::Source +////////////////////////////////////////////////////////////////// + +Awk::Source::Source (Mode mode): mode (mode) { +} - ////////////////////////////////////////////////////////////////// - // Awk::Source - ////////////////////////////////////////////////////////////////// +Awk::Source::Mode Awk::Source::getMode () const +{ + return this->mode; +} - Awk::Source::Source (Mode mode): mode (mode) +const void* Awk::Source::getHandle () const +{ + return this->handle; +} + +void Awk::Source::setHandle (void* handle) +{ + this->handle = handle; +} + +////////////////////////////////////////////////////////////////// +// Awk::Extio +////////////////////////////////////////////////////////////////// + +Awk::Extio::Extio (extio_t* extio): extio (extio) +{ +} + +const Awk::char_t* Awk::Extio::getName () const +{ + return extio->name; +} + +const void* Awk::Extio::getHandle () const +{ + return extio->handle; +} + +void Awk::Extio::setHandle (void* handle) +{ + extio->handle = handle; +} + +const Awk::extio_t* Awk::Extio::getRawExtio () const +{ + return extio; +} + +const Awk::run_t* Awk::Extio::getRawRun () const +{ + return extio->run; +} + +////////////////////////////////////////////////////////////////// +// Awk::Pipe +////////////////////////////////////////////////////////////////// + +Awk::Pipe::Pipe (extio_t* extio): Extio(extio) +{ +} + +Awk::Pipe::Mode Awk::Pipe::getMode () const +{ + return (Mode)extio->mode; +} + +////////////////////////////////////////////////////////////////// +// Awk::File +////////////////////////////////////////////////////////////////// + +Awk::File::File (extio_t* extio): Extio(extio) +{ +} + +Awk::File::Mode Awk::File::getMode () const +{ + return (Mode)extio->mode; +} + +////////////////////////////////////////////////////////////////// +// Awk::Console +////////////////////////////////////////////////////////////////// + +Awk::Console::Console (extio_t* extio): Extio(extio), filename(ASE_NULL) +{ +} + +Awk::Console::~Console () +{ + if (filename != ASE_NULL) { + ase_awk_free (ase_awk_getrunawk(extio->run), filename); } +} - Awk::Source::Mode Awk::Source::getMode () const +int Awk::Console::setFileName (const char_t* name) +{ + if (extio->mode == READ) { - return this->mode; + return ase_awk_setfilename ( + extio->run, name, ase_strlen(name)); } - - const void* Awk::Source::getHandle () const + else { - return this->handle; + return ase_awk_setofilename ( + extio->run, name, ase_strlen(name)); } +} - void Awk::Source::setHandle (void* handle) - { - this->handle = handle; - } - - ////////////////////////////////////////////////////////////////// - // Awk::Extio - ////////////////////////////////////////////////////////////////// - - Awk::Extio::Extio (extio_t* extio): extio (extio) - { - } - - const Awk::char_t* Awk::Extio::getName () const - { - return extio->name; - } - - const void* Awk::Extio::getHandle () const - { - return extio->handle; - } - - void Awk::Extio::setHandle (void* handle) - { - extio->handle = handle; - } - - const Awk::extio_t* Awk::Extio::getRawExtio () const - { - return extio; - } - - const Awk::run_t* Awk::Extio::getRawRun () const - { - return extio->run; - } - - ////////////////////////////////////////////////////////////////// - // Awk::Pipe - ////////////////////////////////////////////////////////////////// - - Awk::Pipe::Pipe (extio_t* extio): Extio(extio) - { - } - - Awk::Pipe::Mode Awk::Pipe::getMode () const - { - return (Mode)extio->mode; - } - - ////////////////////////////////////////////////////////////////// - // Awk::File - ////////////////////////////////////////////////////////////////// - - Awk::File::File (extio_t* extio): Extio(extio) - { - } - - Awk::File::Mode Awk::File::getMode () const - { - return (Mode)extio->mode; - } - - ////////////////////////////////////////////////////////////////// - // Awk::Console - ////////////////////////////////////////////////////////////////// - - Awk::Console::Console (extio_t* extio): Extio(extio), filename(ASE_NULL) - { - } - - Awk::Console::~Console () - { - if (filename != ASE_NULL) - { - ase_awk_free (ase_awk_getrunawk(extio->run), filename); - } - } - - int Awk::Console::setFileName (const char_t* name) - { - if (extio->mode == READ) - { - return ase_awk_setfilename ( - extio->run, name, ase_strlen(name)); - } - else - { - return ase_awk_setofilename ( - extio->run, name, ase_strlen(name)); - } - } - - Awk::Console::Mode Awk::Console::getMode () const - { - return (Mode)extio->mode; - } - - ////////////////////////////////////////////////////////////////// - // Awk::Argument - ////////////////////////////////////////////////////////////////// - - Awk::Argument::Argument (): run (ASE_NULL), val (ASE_NULL) +Awk::Console::Mode Awk::Console::getMode () const +{ + return (Mode)extio->mode; +} + +////////////////////////////////////////////////////////////////// +// Awk::Argument +////////////////////////////////////////////////////////////////// + +Awk::Argument::Argument (): run (ASE_NULL), val (ASE_NULL) +{ + this->str.ptr = ASE_NULL; + this->str.len = 0; +} + +Awk::Argument::~Argument () +{ + if (this->str.ptr != ASE_NULL) { + ASE_ASSERT (this->run != ASE_NULL); + ase_awk_free ( + ase_awk_getrunawk(this->run), this->str.ptr); this->str.ptr = ASE_NULL; this->str.len = 0; } - Awk::Argument::~Argument () + if (this->val != ASE_NULL) { - if (this->str.ptr != ASE_NULL) - { - ASE_ASSERT (this->run != ASE_NULL); - ase_awk_free ( - ase_awk_getrunawk(this->run), this->str.ptr); - this->str.ptr = ASE_NULL; - this->str.len = 0; - } - - if (this->val != ASE_NULL) - { - ASE_ASSERT (this->run != ASE_NULL); - ase_awk_refdownval (this->run, this->val); - } + ASE_ASSERT (this->run != ASE_NULL); + ase_awk_refdownval (this->run, this->val); } +} - 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; +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; - *(awk_t**)ptr = awk; - return (char*)ptr+ASE_SIZEOF(awk); - } + *(awk_t**)ptr = awk; + return (char*)ptr+ASE_SIZEOF(awk); +} - 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; +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; - *(awk_t**)ptr = awk; - return (char*)ptr+ASE_SIZEOF(awk); - } + *(awk_t**)ptr = awk; + return (char*)ptr+ASE_SIZEOF(awk); +} #if !defined(__BORLANDC__) - void Awk::Argument::operator delete (void* ptr, awk_t* awk) - { - ase_awk_free (awk, (char*)ptr-ASE_SIZEOF(awk)); - } +void Awk::Argument::operator delete (void* ptr, awk_t* awk) +{ + ase_awk_free (awk, (char*)ptr-ASE_SIZEOF(awk)); +} - void Awk::Argument::operator delete[] (void* ptr, awk_t* awk) - { - ase_awk_free (awk, (char*)ptr-ASE_SIZEOF(awk)); - } +void Awk::Argument::operator delete[] (void* ptr, awk_t* awk) +{ + ase_awk_free (awk, (char*)ptr-ASE_SIZEOF(awk)); +} #endif - void Awk::Argument::operator delete (void* ptr) +void Awk::Argument::operator delete (void* ptr) +{ + void* p = (char*)ptr-ASE_SIZEOF(awk_t*); + ase_awk_free (*(awk_t**)p, p); +} + +void Awk::Argument::operator delete[] (void* ptr) +{ + void* p = (char*)ptr-ASE_SIZEOF(awk_t*); + ase_awk_free (*(awk_t**)p, p); +} + +int Awk::Argument::init (run_t* run, val_t* v) +{ + // this method is used internally only + // and should never be called more than once + ASE_ASSERT (this->run == ASE_NULL && this->val == ASE_NULL); + ASE_ASSERT (run != ASE_NULL && v != ASE_NULL); + + ase_awk_refupval (run, v); + this->run = run; + this->val = v; + + if (v->type == ASE_AWK_VAL_STR) { - void* p = (char*)ptr-ASE_SIZEOF(awk_t*); - ase_awk_free (*(awk_t**)p, p); + int n = ase_awk_valtonum ( + run, v, &this->inum, &this->rnum); + if (n == 0) + { + this->rnum = (ase_real_t)this->inum; + return 0; + } + else if (n == 1) + { + this->inum = (ase_long_t)this->rnum; + return 0; + } + } + else if (v->type == ASE_AWK_VAL_INT) + { + this->inum = ((ase_awk_val_int_t*)v)->val; + this->rnum = (ase_real_t)((ase_awk_val_int_t*)v)->val; + + this->str.ptr = ase_awk_valtostr (run, v, 0, ASE_NULL, &this->str.len); + if (this->str.ptr != ASE_NULL) return 0; + } + else if (v->type == ASE_AWK_VAL_REAL) + { + this->inum = (ase_long_t)((ase_awk_val_real_t*)v)->val; + this->rnum = ((ase_awk_val_real_t*)v)->val; + + this->str.ptr = ase_awk_valtostr (run, v, 0, ASE_NULL, &this->str.len); + if (this->str.ptr != ASE_NULL) return 0; + } + else if (v->type == ASE_AWK_VAL_NIL) + { + this->inum = 0; + this->rnum = 0.0; + + this->str.ptr = ase_awk_valtostr (run, v, 0, ASE_NULL, &this->str.len); + if (this->str.ptr != ASE_NULL) return 0; } - void Awk::Argument::operator delete[] (void* ptr) + ase_awk_refdownval (run, v); + this->run = ASE_NULL; + this->val = ASE_NULL; + return -1; +} + +Awk::long_t Awk::Argument::toInt () const +{ + ASE_ASSERT (this->run != ASE_NULL && this->val != ASE_NULL); + return this->inum; +} + +Awk::real_t Awk::Argument::toReal () const +{ + ASE_ASSERT (this->run != ASE_NULL && this->val != ASE_NULL); + return this->rnum; +} + +const Awk::char_t* Awk::Argument::toStr (size_t* len) const +{ + ASE_ASSERT (this->run != ASE_NULL && this->val != ASE_NULL); + + if (this->str.ptr != ASE_NULL) { - void* p = (char*)ptr-ASE_SIZEOF(awk_t*); - ase_awk_free (*(awk_t**)p, p); + *len = this->str.len; + return this->str.ptr; + } + else + { + ASE_ASSERT (val->type == ASE_AWK_VAL_STR); + *len = ((ase_awk_val_str_t*)this->val)->len; + return ((ase_awk_val_str_t*)this->val)->buf; + } +} + +////////////////////////////////////////////////////////////////// +// Awk::Return +////////////////////////////////////////////////////////////////// + +Awk::Return::Return (run_t* run): run (run), type (ASE_AWK_VAL_NIL) +{ +} + +Awk::Return::~Return () +{ + clear (); +} + +Awk::val_t* Awk::Return::toVal () const +{ + switch (this->type) + { + case ASE_AWK_VAL_NIL: + return ase_awk_val_nil; + + case ASE_AWK_VAL_INT: + return ase_awk_makeintval (this->run, this->v.inum); + + case ASE_AWK_VAL_REAL: + return ase_awk_makerealval (this->run, this->v.rnum); + + case ASE_AWK_VAL_STR: + return ase_awk_makestrval ( + this->run, this->v.str.ptr, this->v.str.len); } - int Awk::Argument::init (run_t* run, val_t* v) + return ASE_NULL; +} + +int Awk::Return::set (long_t v) +{ + clear (); + + this->type = ASE_AWK_VAL_INT; + this->v.inum = v; + + return 0; +} + +int Awk::Return::set (real_t v) +{ + clear (); + + this->type = ASE_AWK_VAL_REAL; + this->v.rnum = v; + + return 0; +} + +int Awk::Return::set (const char_t* ptr, size_t len) +{ + awk_t* awk = ase_awk_getrunawk(this->run); + char_t* tmp = ase_awk_strxdup (awk, ptr, len); + if (tmp == ASE_NULL) return -1; + + clear (); + + this->type = ASE_AWK_VAL_STR; + this->v.str.ptr = tmp; + this->v.str.len = len; + + return 0; +} + +void Awk::Return::clear () +{ + if (this->type == ASE_AWK_VAL_STR) { - // this method is used internally only - // and should never be called more than once - ASE_ASSERT (this->run == ASE_NULL && this->val == ASE_NULL); - ASE_ASSERT (run != ASE_NULL && v != ASE_NULL); + ASE_ASSERT (this->v.str.ptr != ASE_NULL); + awk_t* awk = ase_awk_getrunawk(this->run); + ase_awk_free (awk, this->v.str.ptr); + this->v.str.ptr = ASE_NULL; + this->v.str.len = 0; + } - ase_awk_refupval (run, v); - this->run = run; - this->val = v; + this->type = ASE_AWK_VAL_NIL; +} - if (v->type == ASE_AWK_VAL_STR) - { - int n = ase_awk_valtonum ( - run, v, &this->inum, &this->rnum); - if (n == 0) - { - this->rnum = (ase_real_t)this->inum; - return 0; - } - else if (n == 1) - { - this->inum = (ase_long_t)this->rnum; - return 0; - } - } - else if (v->type == ASE_AWK_VAL_INT) - { - this->inum = ((ase_awk_val_int_t*)v)->val; - this->rnum = (ase_real_t)((ase_awk_val_int_t*)v)->val; - this->str.ptr = ase_awk_valtostr (run, v, 0, ASE_NULL, &this->str.len); - if (this->str.ptr != ASE_NULL) return 0; - } - else if (v->type == ASE_AWK_VAL_REAL) - { - this->inum = (ase_long_t)((ase_awk_val_real_t*)v)->val; - this->rnum = ((ase_awk_val_real_t*)v)->val; +////////////////////////////////////////////////////////////////// +// Awk::Run +////////////////////////////////////////////////////////////////// - this->str.ptr = ase_awk_valtostr (run, v, 0, ASE_NULL, &this->str.len); - if (this->str.ptr != ASE_NULL) return 0; - } - else if (v->type == ASE_AWK_VAL_NIL) - { - this->inum = 0; - this->rnum = 0.0; +Awk::Run::Run (Awk* awk): + awk (awk), run (ASE_NULL), callbackFailed (false) +{ +} - this->str.ptr = ase_awk_valtostr (run, v, 0, ASE_NULL, &this->str.len); - if (this->str.ptr != ASE_NULL) return 0; - } +int Awk::Run::stop () const +{ + ASE_ASSERT (this->run != ASE_NULL); + return ase_awk_stop (this->run); +} - ase_awk_refdownval (run, v); - this->run = ASE_NULL; - this->val = ASE_NULL; +Awk::ErrorCode Awk::Run::getErrorCode () const +{ + ASE_ASSERT (this->run != ASE_NULL); + return (ErrorCode)ase_awk_getrunerrnum (this->run); +} + +Awk::size_t Awk::Run::getErrorLine () const +{ + ASE_ASSERT (this->run != ASE_NULL); + return ase_awk_getrunerrlin (this->run); +} + +const Awk::char_t* Awk::Run::getErrorMessage () const +{ + ASE_ASSERT (this->run != ASE_NULL); + return ase_awk_getrunerrmsg (this->run); +} + +////////////////////////////////////////////////////////////////// +// 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 () +{ +} + +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; +} + +void Awk::setError ( + ErrorCode code, size_t line, const char_t* arg, size_t len) +{ + if (awk != ASE_NULL) + { + ase_cstr_t x = { arg, len }; + ase_awk_seterror (awk, code, line, &x, 1); + retrieveError (); + } + else + { + this->errnum = code; + this->errlin = line; + + const char_t* es = ase_awk_geterrstr (ASE_NULL, code); + ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), es); + } +} + +void Awk::setError ( + ErrorCode code, size_t line, const char_t* msg) +{ + if (awk != ASE_NULL) + { + ase_awk_seterrmsg (awk, code, line, msg); + retrieveError (); + } + else + { + this->errnum = code; + this->errlin = line; + ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), msg); + } +} + +void Awk::clearError () +{ + this->errnum = ERR_NOERR; + this->errlin = 0; + this->errmsg[0] = ASE_T('\0'); +} + +void Awk::retrieveError () +{ + if (this->awk == ASE_NULL) + { + clearError (); + } + else + { + 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); + } +} + +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; } - Awk::long_t Awk::Argument::toInt () const + functionMap = ase_awk_map_open ( + this, 512, 70, freeFunctionMapValue, awk); + if (functionMap == ASE_NULL) { - ASE_ASSERT (this->run != ASE_NULL && this->val != ASE_NULL); - return this->inum; + ase_awk_close (awk); + awk = ASE_NULL; + + setError (ERR_NOMEM); + return -1; } - Awk::real_t Awk::Argument::toReal () const + int opt = + OPT_IMPLICIT | + OPT_EXPLICIT | + OPT_UNIQUEFN | + OPT_IDIV | + OPT_SHADING | + OPT_SHIFT | + OPT_EXTIO | + OPT_BLOCKLESS | + OPT_BASEONE | + OPT_STRIPSPACES | + OPT_NEXTOFILE | + OPT_ARGSTOMAIN; + ase_awk_setoption (awk, opt); + + runCallback = false; + return 0; +} + +void Awk::close () +{ + if (functionMap != ASE_NULL) { - ASE_ASSERT (this->run != ASE_NULL && this->val != ASE_NULL); - return this->rnum; + ase_awk_map_close (functionMap); + functionMap = ASE_NULL; } - const Awk::char_t* Awk::Argument::toStr (size_t* len) const + if (awk != ASE_NULL) { - ASE_ASSERT (this->run != ASE_NULL && this->val != ASE_NULL); - - if (this->str.ptr != ASE_NULL) - { - *len = this->str.len; - return this->str.ptr; - } - else - { - ASE_ASSERT (val->type == ASE_AWK_VAL_STR); - *len = ((ase_awk_val_str_t*)this->val)->len; - return ((ase_awk_val_str_t*)this->val)->buf; - } + ase_awk_close (awk); + awk = ASE_NULL; } - ////////////////////////////////////////////////////////////////// - // Awk::Return - ////////////////////////////////////////////////////////////////// + clearError (); + runCallback = false; +} - Awk::Return::Return (run_t* run): run (run), type (ASE_AWK_VAL_NIL) +void Awk::setOption (int opt) +{ + ASE_ASSERT (awk != ASE_NULL); + ase_awk_setoption (awk, opt); +} + +int Awk::getOption () const +{ + ASE_ASSERT (awk != ASE_NULL); + return ase_awk_getoption (awk); +} + +void Awk::setMaxDepth (int ids, size_t depth) +{ + ASE_ASSERT (awk != ASE_NULL); + ase_awk_setmaxdepth (awk, ids, depth); +} + +Awk::size_t Awk::getMaxDepth (int id) const +{ + ASE_ASSERT (awk != ASE_NULL); + return ase_awk_getmaxdepth (awk, id); +} + +const Awk::char_t* Awk::getErrorString (ErrorCode num) const +{ + ASE_ASSERT (awk != ASE_NULL); + return ase_awk_geterrstr (awk, (int)num); +} + +int Awk::setErrorString (ErrorCode num, const char_t* str) +{ + ASE_ASSERT (awk != ASE_NULL); + return ase_awk_seterrstr (awk, (int)num, str); +} + +int Awk::setWord (const char_t* ow, const char_t* nw) +{ + return setWord (ow, ase_strlen(ow), nw, ase_strlen(nw)); +} + +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); +} + +int Awk::unsetWord (const char_t* ow) +{ + return unsetWord (ow, ase_strlen(ow)); +} + +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); +} + +int Awk::unsetAllWords () +{ + ASE_ASSERT (awk != ASE_NULL); + return ase_awk_setword (awk, ASE_NULL, 0, ASE_NULL, 0); +} + +int Awk::parse () +{ + ASE_ASSERT (awk != ASE_NULL); + + ase_awk_srcios_t srcios; + + srcios.in = sourceReader; + srcios.out = sourceWriter; + srcios.custom_data = this; + + int n = ase_awk_parse (awk, &srcios); + if (n == -1) retrieveError (); + return n; +} + +int Awk::run (const char_t* main, const char_t** args, size_t nargs) +{ + ASE_ASSERT (awk != ASE_NULL); + + size_t i; + ase_awk_runios_t runios; + ase_awk_runcbs_t runcbs; + ase_awk_runarg_t* runarg = ASE_NULL; + Run emptyRun (this); + + runios.pipe = pipeHandler; + runios.coproc = ASE_NULL; + runios.file = fileHandler; + runios.console = consoleHandler; + runios.custom_data = this; + + if (runCallback) { + runcbs.on_start = onRunStart; + runcbs.on_end = onRunEnd; + runcbs.on_return = onRunReturn; + runcbs.on_statement = onRunStatement; + runcbs.custom_data = &emptyRun; } - Awk::Return::~Return () + if (nargs > 0) { - clear (); - } + runarg = (ase_awk_runarg_t*) ase_awk_malloc ( + awk, ASE_SIZEOF(ase_awk_runarg_t)*(nargs+1)); - Awk::val_t* Awk::Return::toVal () const - { - switch (this->type) - { - case ASE_AWK_VAL_NIL: - return ase_awk_val_nil; - - case ASE_AWK_VAL_INT: - return ase_awk_makeintval (this->run, this->v.inum); - - case ASE_AWK_VAL_REAL: - return ase_awk_makerealval (this->run, this->v.rnum); - - case ASE_AWK_VAL_STR: - return ase_awk_makestrval ( - this->run, this->v.str.ptr, this->v.str.len); - } - - return ASE_NULL; - } - - int Awk::Return::set (long_t v) - { - clear (); - - this->type = ASE_AWK_VAL_INT; - this->v.inum = v; - - return 0; - } - - int Awk::Return::set (real_t v) - { - clear (); - - this->type = ASE_AWK_VAL_REAL; - this->v.rnum = v; - - return 0; - } - - int Awk::Return::set (const char_t* ptr, size_t len) - { - awk_t* awk = ase_awk_getrunawk(this->run); - char_t* tmp = ase_awk_strxdup (awk, ptr, len); - if (tmp == ASE_NULL) return -1; - - clear (); - - this->type = ASE_AWK_VAL_STR; - this->v.str.ptr = tmp; - this->v.str.len = len; - - return 0; - } - - void Awk::Return::clear () - { - if (this->type == ASE_AWK_VAL_STR) - { - ASE_ASSERT (this->v.str.ptr != ASE_NULL); - awk_t* awk = ase_awk_getrunawk(this->run); - ase_awk_free (awk, this->v.str.ptr); - this->v.str.ptr = ASE_NULL; - this->v.str.len = 0; - } - - this->type = ASE_AWK_VAL_NIL; - } - - - ////////////////////////////////////////////////////////////////// - // Awk::Run - ////////////////////////////////////////////////////////////////// - - Awk::Run::Run (Awk* awk): - awk (awk), run (ASE_NULL), callbackFailed (false) - { - } - - int Awk::Run::stop () const - { - ASE_ASSERT (this->run != ASE_NULL); - return ase_awk_stop (this->run); - } - - Awk::ErrorCode Awk::Run::getErrorCode () const - { - ASE_ASSERT (this->run != ASE_NULL); - return (ErrorCode)ase_awk_getrunerrnum (this->run); - } - - Awk::size_t Awk::Run::getErrorLine () const - { - ASE_ASSERT (this->run != ASE_NULL); - return ase_awk_getrunerrlin (this->run); - } - - const Awk::char_t* Awk::Run::getErrorMessage () const - { - ASE_ASSERT (this->run != ASE_NULL); - return ase_awk_getrunerrmsg (this->run); - } - - ////////////////////////////////////////////////////////////////// - // 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 () - { - } - - 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; - } - - void Awk::setError ( - ErrorCode code, size_t line, const char_t* arg, size_t len) - { - if (awk != ASE_NULL) - { - ase_cstr_t x = { arg, len }; - ase_awk_seterror (awk, code, line, &x, 1); - retrieveError (); - } - else - { - this->errnum = code; - this->errlin = line; - - const char_t* es = ase_awk_geterrstr (ASE_NULL, code); - ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), es); - } - } - - void Awk::setError ( - ErrorCode code, size_t line, const char_t* msg) - { - if (awk != ASE_NULL) - { - ase_awk_seterrmsg (awk, code, line, msg); - retrieveError (); - } - else - { - this->errnum = code; - this->errlin = line; - ase_strxcpy (this->errmsg, ASE_COUNTOF(this->errmsg), msg); - } - } - - void Awk::clearError () - { - this->errnum = ERR_NOERR; - this->errlin = 0; - this->errmsg[0] = ASE_T('\0'); - } - - void Awk::retrieveError () - { - if (this->awk == ASE_NULL) - { - clearError (); - } - else - { - 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); - } - } - - 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) + if (runarg == ASE_NULL) { setError (ERR_NOMEM); return -1; } - functionMap = ase_awk_map_open ( - this, 512, 70, freeFunctionMapValue, awk); - if (functionMap == ASE_NULL) - { - ase_awk_close (awk); - awk = ASE_NULL; - - setError (ERR_NOMEM); - return -1; - } - - int opt = - OPT_IMPLICIT | - OPT_EXPLICIT | - OPT_UNIQUEFN | - OPT_IDIV | - OPT_SHADING | - OPT_SHIFT | - OPT_EXTIO | - OPT_BLOCKLESS | - OPT_BASEONE | - OPT_STRIPSPACES | - OPT_NEXTOFILE | - OPT_ARGSTOMAIN; - ase_awk_setoption (awk, opt); - - runCallback = false; - return 0; - } - - void Awk::close () - { - if (functionMap != ASE_NULL) - { - ase_awk_map_close (functionMap); - functionMap = ASE_NULL; - } - - if (awk != ASE_NULL) - { - ase_awk_close (awk); - awk = ASE_NULL; - } - - clearError (); - runCallback = false; - } - - void Awk::setOption (int opt) - { - ASE_ASSERT (awk != ASE_NULL); - ase_awk_setoption (awk, opt); - } - - int Awk::getOption () const - { - ASE_ASSERT (awk != ASE_NULL); - return ase_awk_getoption (awk); - } - - void Awk::setMaxDepth (int ids, size_t depth) - { - ASE_ASSERT (awk != ASE_NULL); - ase_awk_setmaxdepth (awk, ids, depth); - } - - Awk::size_t Awk::getMaxDepth (int id) const - { - ASE_ASSERT (awk != ASE_NULL); - return ase_awk_getmaxdepth (awk, id); - } - - const Awk::char_t* Awk::getErrorString (ErrorCode num) const - { - ASE_ASSERT (awk != ASE_NULL); - return ase_awk_geterrstr (awk, (int)num); - } - - int Awk::setErrorString (ErrorCode num, const char_t* str) - { - ASE_ASSERT (awk != ASE_NULL); - return ase_awk_seterrstr (awk, (int)num, str); - } - - int Awk::setWord (const char_t* ow, const char_t* nw) - { - return setWord (ow, ase_strlen(ow), nw, ase_strlen(nw)); - } - - 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); - } - - int Awk::unsetWord (const char_t* ow) - { - return unsetWord (ow, ase_strlen(ow)); - } - - 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); - } - - int Awk::unsetAllWords () - { - ASE_ASSERT (awk != ASE_NULL); - return ase_awk_setword (awk, ASE_NULL, 0, ASE_NULL, 0); - } - - int Awk::parse () - { - ASE_ASSERT (awk != ASE_NULL); - - ase_awk_srcios_t srcios; - - srcios.in = sourceReader; - srcios.out = sourceWriter; - srcios.custom_data = this; - - int n = ase_awk_parse (awk, &srcios); - if (n == -1) retrieveError (); - return n; - } - - int Awk::run (const char_t* main, const char_t** args, size_t nargs) - { - ASE_ASSERT (awk != ASE_NULL); - - size_t i; - ase_awk_runios_t runios; - ase_awk_runcbs_t runcbs; - ase_awk_runarg_t* runarg = ASE_NULL; - Run emptyRun (this); - - runios.pipe = pipeHandler; - runios.coproc = ASE_NULL; - runios.file = fileHandler; - runios.console = consoleHandler; - runios.custom_data = this; - - if (runCallback) - { - runcbs.on_start = onRunStart; - runcbs.on_end = onRunEnd; - runcbs.on_return = onRunReturn; - runcbs.on_statement = onRunStatement; - runcbs.custom_data = &emptyRun; - } - - if (nargs > 0) - { - runarg = (ase_awk_runarg_t*) ase_awk_malloc ( - awk, ASE_SIZEOF(ase_awk_runarg_t)*(nargs+1)); - - if (runarg == ASE_NULL) - { - setError (ERR_NOMEM); - return -1; - } - - for (i = 0; i < nargs; i++) - { - 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) - { - while (i > 0) ase_awk_free (awk, runarg[--i].ptr); - ase_awk_free (awk, runarg); - setError (ERR_NOMEM); - return -1; - } - } - - runarg[i].ptr = ASE_NULL; - runarg[i].len = 0; - } - - int n = ase_awk_run ( - awk, main, &runios, - (runCallback? &runcbs: ASE_NULL), - runarg, this); - if (n == -1) retrieveError (); - - if (runarg != ASE_NULL) - { - while (i > 0) ase_awk_free (awk, runarg[--i].ptr); - ase_awk_free (awk, runarg); - } - - return n; - } - - int Awk::dispatchFunction ( - run_t* run, const char_t* name, size_t len) - { - pair_t* pair; - awk_t* awk; - - awk = ase_awk_getrunawk (run); - - pair = ase_awk_map_get (functionMap, name, len); - if (pair == ASE_NULL) return -1; - - FunctionHandler handler; - handler = *(FunctionHandler*)ASE_AWK_PAIR_VAL(pair); - - size_t i, nargs = ase_awk_getnargs(run); - - //Argument* args = ASE_NULL; - //try { args = new Argument [nargs]; } catch (...) {} - Argument* args = new(awk) Argument[nargs]; - if (args == ASE_NULL) return -1; - for (i = 0; i < nargs; i++) { - val_t* v = ase_awk_getarg (run, i); - if (args[i].init (run, v) == -1) + 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) { - delete[] args; + while (i > 0) ase_awk_free (awk, runarg[--i].ptr); + ase_awk_free (awk, runarg); + setError (ERR_NOMEM); return -1; } } - - Return ret (run); - int n = (this->*handler) (&ret, args, nargs, name, len); - delete[] args; - - if (n <= -1) return -1; - - val_t* r = ret.toVal (); - if (r == ASE_NULL) return -1; - - ase_awk_setretval (run, r); - return 0; - } - - int Awk::addFunction ( - const char_t* name, size_t minArgs, size_t maxArgs, - FunctionHandler handler) - { - ASE_ASSERT (awk != ASE_NULL); - - FunctionHandler* tmp = (FunctionHandler*) - ase_awk_malloc (awk, ASE_SIZEOF(handler)); - if (tmp == ASE_NULL) - { - setError (ERR_NOMEM); - return -1; - } - - //ase_memcpy (tmp, &handler, ASE_SIZEOF(handler)); - *tmp = handler; - - size_t nameLen = ase_strlen(name); - - void* p = ase_awk_addbfn (awk, name, nameLen, - 0, minArgs, maxArgs, ASE_NULL, - functionHandler); - if (p == ASE_NULL) - { - ase_awk_free (awk, tmp); - retrieveError (); - return -1; - } - - pair_t* pair = ase_awk_map_put (functionMap, name, nameLen, tmp); - if (pair == ASE_NULL) - { - ase_awk_delbfn (awk, name, nameLen); - ase_awk_free (awk, tmp); - - setError (ERR_NOMEM); - return -1; - } - - return 0; - } - - int Awk::deleteFunction (const char_t* name) - { - ASE_ASSERT (awk != ASE_NULL); - - size_t nameLen = ase_strlen(name); - - int n = ase_awk_delbfn (awk, name, nameLen); - if (n == 0) ase_awk_map_remove (functionMap, name, nameLen); - else retrieveError (); - - return n; - } - - void Awk::enableRunCallback () - { - runCallback = true; - } - - void Awk::disableRunCallback () - { - runCallback = false; - } - - void Awk::onRunStart (const Run& run) - { - } - - void Awk::onRunEnd (const Run& run) - { - } - - void Awk::onRunReturn (const Run& run, const Argument& ret) - { - } - - void Awk::onRunStatement (const Run& run, size_t line) - { - } - - Awk::ssize_t Awk::sourceReader ( - int cmd, void* arg, char_t* data, size_t count) - { - Awk* awk = (Awk*)arg; - - switch (cmd) - { - 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); - } - - return -1; - } - - Awk::ssize_t Awk::sourceWriter ( - int cmd, void* arg, char_t* data, size_t count) - { - Awk* awk = (Awk*)arg; - - switch (cmd) - { - 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); - } - - return -1; - } - - 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; - - ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_PIPE); - - Pipe pipe (extio); - - switch (cmd) - { - case ASE_AWK_IO_OPEN: - return awk->openPipe (pipe); - case ASE_AWK_IO_CLOSE: - return awk->closePipe (pipe); - - case ASE_AWK_IO_READ: - return awk->readPipe (pipe, data, count); - case ASE_AWK_IO_WRITE: - return awk->writePipe (pipe, data, count); - - case ASE_AWK_IO_FLUSH: - return awk->flushPipe (pipe); - - case ASE_AWK_IO_NEXT: - return -1; - } - - return -1; - } - - 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; - - ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_FILE); - - File file (extio); - - switch (cmd) - { - case ASE_AWK_IO_OPEN: - return awk->openFile (file); - case ASE_AWK_IO_CLOSE: - return awk->closeFile (file); - - case ASE_AWK_IO_READ: - return awk->readFile (file, data, count); - case ASE_AWK_IO_WRITE: - return awk->writeFile (file, data, count); - - case ASE_AWK_IO_FLUSH: - return awk->flushFile (file); - - case ASE_AWK_IO_NEXT: - return -1; - } - - return -1; - } - - 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; - - ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_CONSOLE); - - Console console (extio); - - switch (cmd) - { - case ASE_AWK_IO_OPEN: - return awk->openConsole (console); - case ASE_AWK_IO_CLOSE: - return awk->closeConsole (console); - - case ASE_AWK_IO_READ: - return awk->readConsole (console, data, count); - case ASE_AWK_IO_WRITE: - return awk->writeConsole (console, data, count); - - case ASE_AWK_IO_FLUSH: - return awk->flushConsole (console); - case ASE_AWK_IO_NEXT: - return awk->nextConsole (console); - } - - return -1; - } - - int Awk::functionHandler ( - run_t* run, const char_t* name, size_t len) - { - Awk* awk = (Awk*) ase_awk_getruncustomdata (run); - return awk->dispatchFunction (run, name, len); - } - - void Awk::freeFunctionMapValue (void* owner, void* value) - { - Awk* awk = (Awk*)owner; - ase_awk_free (awk->awk, value); - } - - void Awk::onRunStart (run_t* run, void* custom) - { - Run* r = (Run*)custom; - r->run = run; - - r->callbackFailed = false; - r->awk->onRunStart (*r); - } - - void Awk::onRunEnd (run_t* run, int errnum, void* custom) - { - Run* r = (Run*)custom; - - if (errnum == ERR_NOERR && r->callbackFailed) - { - ase_awk_setrunerror ( - r->run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); - } - - r->awk->onRunEnd (*r); - } - - void Awk::onRunReturn (run_t* run, val_t* ret, void* custom) - { - Run* r = (Run*)custom; - if (r->callbackFailed) return; - - Argument x; - if (x.init (run, ret) == -1) - { - r->callbackFailed = true; - } - else - { - r->awk->onRunReturn (*r, x); - } - } - - void Awk::onRunStatement (run_t* run, size_t line, void* custom) - { - Run* r = (Run*)custom; - if (r->callbackFailed) return; - r->awk->onRunStatement (*r, line); - } - - 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); + runarg[i].ptr = ASE_NULL; + runarg[i].len = 0; } - Awk::bool_t Awk::isLower (void* custom, cint_t c) - { - return ((Awk*)custom)->isLower (c); - } - - 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) + int n = ase_awk_run ( + awk, main, &runios, + (runCallback? &runcbs: ASE_NULL), + runarg, this); + if (n == -1) retrieveError (); + + if (runarg != ASE_NULL) { - 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); + while (i > 0) ase_awk_free (awk, runarg[--i].ptr); + ase_awk_free (awk, runarg); } + return n; } + +int Awk::dispatchFunction ( + run_t* run, const char_t* name, size_t len) +{ + pair_t* pair; + awk_t* awk; + + awk = ase_awk_getrunawk (run); + + pair = ase_awk_map_get (functionMap, name, len); + if (pair == ASE_NULL) return -1; + + FunctionHandler handler; + handler = *(FunctionHandler*)ASE_AWK_PAIR_VAL(pair); + + size_t i, nargs = ase_awk_getnargs(run); + + //Argument* args = ASE_NULL; + //try { args = new Argument [nargs]; } catch (...) {} + Argument* args = new(awk) Argument[nargs]; + if (args == ASE_NULL) return -1; + + for (i = 0; i < nargs; i++) + { + val_t* v = ase_awk_getarg (run, i); + if (args[i].init (run, v) == -1) + { + delete[] args; + return -1; + } + } + + Return ret (run); + int n = (this->*handler) (&ret, args, nargs, name, len); + + delete[] args; + + if (n <= -1) return -1; + + val_t* r = ret.toVal (); + if (r == ASE_NULL) return -1; + + ase_awk_setretval (run, r); + return 0; +} + +int Awk::addFunction ( + const char_t* name, size_t minArgs, size_t maxArgs, + FunctionHandler handler) +{ + ASE_ASSERT (awk != ASE_NULL); + + FunctionHandler* tmp = (FunctionHandler*) + ase_awk_malloc (awk, ASE_SIZEOF(handler)); + if (tmp == ASE_NULL) + { + setError (ERR_NOMEM); + return -1; + } + + //ase_memcpy (tmp, &handler, ASE_SIZEOF(handler)); + *tmp = handler; + + size_t nameLen = ase_strlen(name); + + void* p = ase_awk_addbfn (awk, name, nameLen, + 0, minArgs, maxArgs, ASE_NULL, + functionHandler); + if (p == ASE_NULL) + { + ase_awk_free (awk, tmp); + retrieveError (); + return -1; + } + + pair_t* pair = ase_awk_map_put (functionMap, name, nameLen, tmp); + if (pair == ASE_NULL) + { + ase_awk_delbfn (awk, name, nameLen); + ase_awk_free (awk, tmp); + + setError (ERR_NOMEM); + return -1; + } + + return 0; +} + +int Awk::deleteFunction (const char_t* name) +{ + ASE_ASSERT (awk != ASE_NULL); + + size_t nameLen = ase_strlen(name); + + int n = ase_awk_delbfn (awk, name, nameLen); + if (n == 0) ase_awk_map_remove (functionMap, name, nameLen); + else retrieveError (); + + return n; +} + +void Awk::enableRunCallback () +{ + runCallback = true; +} + +void Awk::disableRunCallback () +{ + runCallback = false; +} + +void Awk::onRunStart (const Run& run) +{ +} + +void Awk::onRunEnd (const Run& run) +{ +} + +void Awk::onRunReturn (const Run& run, const Argument& ret) +{ +} + +void Awk::onRunStatement (const Run& run, size_t line) +{ +} + +Awk::ssize_t Awk::sourceReader ( + int cmd, void* arg, char_t* data, size_t count) +{ + Awk* awk = (Awk*)arg; + + switch (cmd) + { + 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); + } + + return -1; +} + +Awk::ssize_t Awk::sourceWriter ( + int cmd, void* arg, char_t* data, size_t count) +{ + Awk* awk = (Awk*)arg; + + switch (cmd) + { + 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); + } + + return -1; +} + +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; + + ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_PIPE); + + Pipe pipe (extio); + + switch (cmd) + { + case ASE_AWK_IO_OPEN: + return awk->openPipe (pipe); + case ASE_AWK_IO_CLOSE: + return awk->closePipe (pipe); + + case ASE_AWK_IO_READ: + return awk->readPipe (pipe, data, count); + case ASE_AWK_IO_WRITE: + return awk->writePipe (pipe, data, count); + + case ASE_AWK_IO_FLUSH: + return awk->flushPipe (pipe); + + case ASE_AWK_IO_NEXT: + return -1; + } + + return -1; +} + +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; + + ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_FILE); + + File file (extio); + + switch (cmd) + { + case ASE_AWK_IO_OPEN: + return awk->openFile (file); + case ASE_AWK_IO_CLOSE: + return awk->closeFile (file); + + case ASE_AWK_IO_READ: + return awk->readFile (file, data, count); + case ASE_AWK_IO_WRITE: + return awk->writeFile (file, data, count); + + case ASE_AWK_IO_FLUSH: + return awk->flushFile (file); + + case ASE_AWK_IO_NEXT: + return -1; + } + + return -1; +} + +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; + + ASE_ASSERT ((extio->type & 0xFF) == ASE_AWK_EXTIO_CONSOLE); + + Console console (extio); + + switch (cmd) + { + case ASE_AWK_IO_OPEN: + return awk->openConsole (console); + case ASE_AWK_IO_CLOSE: + return awk->closeConsole (console); + + case ASE_AWK_IO_READ: + return awk->readConsole (console, data, count); + case ASE_AWK_IO_WRITE: + return awk->writeConsole (console, data, count); + + case ASE_AWK_IO_FLUSH: + return awk->flushConsole (console); + case ASE_AWK_IO_NEXT: + return awk->nextConsole (console); + } + + return -1; +} + +int Awk::functionHandler ( + run_t* run, const char_t* name, size_t len) +{ + Awk* awk = (Awk*) ase_awk_getruncustomdata (run); + return awk->dispatchFunction (run, name, len); +} + +void Awk::freeFunctionMapValue (void* owner, void* value) +{ + Awk* awk = (Awk*)owner; + ase_awk_free (awk->awk, value); +} + +void Awk::onRunStart (run_t* run, void* custom) +{ + Run* r = (Run*)custom; + r->run = run; + + r->callbackFailed = false; + r->awk->onRunStart (*r); +} + +void Awk::onRunEnd (run_t* run, int errnum, void* custom) +{ + Run* r = (Run*)custom; + + if (errnum == ERR_NOERR && r->callbackFailed) + { + ase_awk_setrunerror ( + r->run, ASE_AWK_ENOMEM, 0, ASE_NULL, 0); + } + + r->awk->onRunEnd (*r); +} + +void Awk::onRunReturn (run_t* run, val_t* ret, void* custom) +{ + Run* r = (Run*)custom; + if (r->callbackFailed) return; + + Argument x; + if (x.init (run, ret) == -1) + { + r->callbackFailed = true; + } + else + { + r->awk->onRunReturn (*r, x); + } +} + +void Awk::onRunStatement (run_t* run, size_t line, void* custom) +{ + Run* r = (Run*)custom; + if (r->callbackFailed) return; + r->awk->onRunStatement (*r, line); +} + +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); +} + +Awk::bool_t Awk::isLower (void* custom, cint_t c) +{ + return ((Awk*)custom)->isLower (c); +} + +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) +///////////////////////////////// + diff --git a/ase/awk/Awk.hpp b/ase/awk/Awk.hpp index 001f9b0f..dfa5ae0b 100644 --- a/ase/awk/Awk.hpp +++ b/ase/awk/Awk.hpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.hpp,v 1.52 2007/09/06 14:38:56 bacon Exp $ + * $Id: Awk.hpp,v 1.53 2007/09/07 05:40:16 bacon Exp $ */ #ifndef _ASE_AWK_AWK_HPP_ @@ -9,232 +9,269 @@ #include #include -namespace ASE +///////////////////////////////// +ASE_BEGIN_NAMESPACE(ASE) +///////////////////////////////// + +/** + * Provides the awk interpreter engine + */ +class Awk { - /** - * Provides the awk interpreter engine +public: + /** + * boolean data type */ - class Awk + typedef ase_bool_t bool_t; + + /** + * data type that can hold any character + */ + + typedef ase_char_t char_t; + + /** + * data type that can hold any character or + * an end-of-file value + */ + typedef ase_cint_t cint_t; + + + typedef ase_size_t size_t; + typedef ase_ssize_t ssize_t; + typedef ase_long_t long_t; + typedef ase_real_t real_t; + + /** + * represents an internal awk value + */ + typedef ase_awk_val_t val_t; + + /** + * represents the internal hash table + */ + typedef ase_awk_map_t map_t; + + /** + * represents a key/value pair + */ + typedef ase_awk_pair_t pair_t; + + /** + * represents the external i/o operation + */ + typedef ase_awk_extio_t extio_t; + + /** + * represents the run-time instance of an awk interpreter + */ + typedef ase_awk_run_t run_t; + + /** + * reprensts the underlying awk interpreter + */ + typedef ase_awk_t awk_t; + + /** + * The source class + * @see openSource + */ + class Source { public: /** - * boolean data type + * Indicates the mode of the source code I/O */ - typedef ase_bool_t bool_t; - - /** - * data type that can hold any character - */ - - typedef ase_char_t char_t; - - /** - * data type that can hold any character or - * an end-of-file value - */ - typedef ase_cint_t cint_t; - - - typedef ase_size_t size_t; - typedef ase_ssize_t ssize_t; - typedef ase_long_t long_t; - typedef ase_real_t real_t; - - /** - * represents an internal awk value - */ - typedef ase_awk_val_t val_t; - - /** - * represents the internal hash table - */ - typedef ase_awk_map_t map_t; - - /** - * represents a key/value pair - */ - typedef ase_awk_pair_t pair_t; - - /** - * represents the external i/o operation - */ - typedef ase_awk_extio_t extio_t; - - /** - * represents the run-time instance of an awk interpreter - */ - typedef ase_awk_run_t run_t; - - /** - * reprensts the underlying awk interpreter - */ - typedef ase_awk_t awk_t; - - /** - * The source class - * @see openSource - */ - class Source - { - public: - /** - * Indicates the mode of the source code I/O - */ - enum Mode - { - READ, /**< readable I/O */ - WRITE /**< writable I/O */ - }; - - Source (Mode mode); - - Mode getMode() const; - const void* getHandle () const; - void setHandle (void* handle); - - private: - Mode mode; - void* handle; + enum Mode + { + READ, /**< readable I/O */ + WRITE /**< writable I/O */ }; - /** - * Extio class + Source (Mode mode); + + Mode getMode() const; + const void* getHandle () const; + void setHandle (void* handle); + + private: + Mode mode; + void* handle; + }; + + /** + * Extio class + */ + class Extio + { + protected: + Extio (extio_t* extio); + + public: + const char_t* getName() const; + const void* getHandle () const; + void setHandle (void* handle); + + /** + * returns the underlying extio_t handle */ - class Extio + const extio_t* getRawExtio () const; + + /** + * returns the underlying run_t handle associated + * with the underlying extio_t handle + */ + const run_t* getRawRun () const; + + protected: + extio_t* extio; + }; + + /** + * Pipe + */ + class Pipe: public Extio + { + public: + friend class Awk; + + enum Mode { - protected: - Extio (extio_t* extio); - - public: - const char_t* getName() const; - const void* getHandle () const; - void setHandle (void* handle); - - /** - * returns the underlying extio_t handle - */ - const extio_t* getRawExtio () const; - - /** - * returns the underlying run_t handle associated - * with the underlying extio_t handle - */ - const run_t* getRawRun () const; - - protected: - extio_t* extio; + READ = ASE_AWK_EXTIO_PIPE_READ, + WRITE = ASE_AWK_EXTIO_PIPE_WRITE }; - /** - * Pipe - */ - class Pipe: public Extio + protected: + Pipe (extio_t* extio); + + public: + Mode getMode () const; + }; + + /** + * File + */ + class File: public Extio + { + public: + friend class Awk; + + enum Mode { - public: - friend class Awk; - - enum Mode - { - READ = ASE_AWK_EXTIO_PIPE_READ, - WRITE = ASE_AWK_EXTIO_PIPE_WRITE - }; - - protected: - Pipe (extio_t* extio); - - public: - Mode getMode () const; + READ = ASE_AWK_EXTIO_FILE_READ, + WRITE = ASE_AWK_EXTIO_FILE_WRITE, + APPEND = ASE_AWK_EXTIO_FILE_APPEND }; - /** - * File - */ - class File: public Extio + protected: + File (extio_t* extio); + + public: + Mode getMode () const; + }; + + /** + * Console + */ + class Console: public Extio + { + public: + friend class Awk; + + enum Mode { - public: - friend class Awk; - - enum Mode - { - READ = ASE_AWK_EXTIO_FILE_READ, - WRITE = ASE_AWK_EXTIO_FILE_WRITE, - APPEND = ASE_AWK_EXTIO_FILE_APPEND - }; - - protected: - File (extio_t* extio); - - public: - Mode getMode () const; + READ = ASE_AWK_EXTIO_CONSOLE_READ, + WRITE = ASE_AWK_EXTIO_CONSOLE_WRITE }; - /** - * Console - */ - class Console: public Extio + protected: + Console (extio_t* extio); + ~Console (); + + public: + Mode getMode () const; + int setFileName (const char_t* name); + + protected: + char_t* filename; + }; + + /** + * Represents an argument to an intrinsic function + */ + class Argument + { + protected: + friend class Awk; + + Argument (); + ~Argument (); + + // initialization + void* operator new (size_t n, awk_t* awk) throw (); + void* operator new[] (size_t n, awk_t* awk) throw (); + + #if !defined(__BORLANDC__) + // deletion when initialization fails + void operator delete (void* p, awk_t* awk); + void operator delete[] (void* p, awk_t* awk); + #endif + + // normal deletion + void operator delete (void* p); + void operator delete[] (void* p); + + private: + Argument (const Argument&); + Argument& operator= (const Argument&); + + protected: + int init (run_t* run, val_t* v); + + public: + long_t toInt () const; + real_t toReal () const; + const char_t* toStr (size_t* len) const; + + protected: + run_t* run; + val_t* val; + + ase_long_t inum; + ase_real_t rnum; + + struct { - public: - friend class Awk; + char_t* ptr; + size_t len; + } str; + }; - enum Mode - { - READ = ASE_AWK_EXTIO_CONSOLE_READ, - WRITE = ASE_AWK_EXTIO_CONSOLE_WRITE - }; + /** + * represents a return value of an intrinsic function + */ + class Return + { + protected: + friend class Awk; - protected: - Console (extio_t* extio); - ~Console (); + Return (run_t* run); + ~Return (); - public: - Mode getMode () const; - int setFileName (const char_t* name); + val_t* toVal () const; - protected: - char_t* filename; - }; + public: + int set (long_t v); + int set (real_t v); + int set (const char_t* ptr, size_t len); + void clear (); - /** - * Represents an argument to an intrinsic function - */ - class Argument + protected: + run_t* run; + int type; + + union { - protected: - friend class Awk; - - Argument (); - ~Argument (); - - // initialization - void* operator new (size_t n, awk_t* awk) throw (); - void* operator new[] (size_t n, awk_t* awk) throw (); - - #if !defined(__BORLANDC__) - // deletion when initialization fails - void operator delete (void* p, awk_t* awk); - void operator delete[] (void* p, awk_t* awk); - #endif - - // normal deletion - void operator delete (void* p); - void operator delete[] (void* p); - - private: - Argument (const Argument&); - Argument& operator= (const Argument&); - - protected: - int init (run_t* run, val_t* v); - - public: - long_t toInt () const; - real_t toReal () const; - const char_t* toStr (size_t* len) const; - - protected: - run_t* run; - val_t* val; - ase_long_t inum; ase_real_t rnum; @@ -243,468 +280,435 @@ namespace ASE char_t* ptr; size_t len; } str; - }; + } v; + }; - /** - * represents a return value of an intrinsic function - */ - class Return - { - protected: - friend class Awk; + /** + * represens the error code + */ - Return (run_t* run); - ~Return (); + // generated by generrcode.awk + enum ErrorCode + { + ERR_NOERR = ASE_AWK_ENOERR, + ERR_INVAL = ASE_AWK_EINVAL, + ERR_NOMEM = ASE_AWK_ENOMEM, + ERR_NOSUP = ASE_AWK_ENOSUP, + ERR_NOPER = ASE_AWK_ENOPER, + ERR_NODEV = ASE_AWK_ENODEV, + ERR_NOSPC = ASE_AWK_ENOSPC, + ERR_MFILE = ASE_AWK_EMFILE, + ERR_MLINK = ASE_AWK_EMLINK, + ERR_AGAIN = ASE_AWK_EAGAIN, + ERR_NOENT = ASE_AWK_ENOENT, + ERR_EXIST = ASE_AWK_EEXIST, + ERR_FTBIG = ASE_AWK_EFTBIG, + ERR_TBUSY = ASE_AWK_ETBUSY, + ERR_ISDIR = ASE_AWK_EISDIR, + ERR_IOERR = ASE_AWK_EIOERR, + ERR_OPEN = ASE_AWK_EOPEN, + ERR_READ = ASE_AWK_EREAD, + ERR_WRITE = ASE_AWK_EWRITE, + ERR_CLOSE = ASE_AWK_ECLOSE, + ERR_INTERN = ASE_AWK_EINTERN, + ERR_RUNTIME = ASE_AWK_ERUNTIME, + ERR_BLKNST = ASE_AWK_EBLKNST, + ERR_EXPRNST = ASE_AWK_EEXPRNST, + ERR_SINOP = ASE_AWK_ESINOP, + ERR_SINCL = ASE_AWK_ESINCL, + ERR_SINRD = ASE_AWK_ESINRD, + ERR_SOUTOP = ASE_AWK_ESOUTOP, + ERR_SOUTCL = ASE_AWK_ESOUTCL, + ERR_SOUTWR = ASE_AWK_ESOUTWR, + ERR_LXCHR = ASE_AWK_ELXCHR, + ERR_LXDIG = ASE_AWK_ELXDIG, + ERR_LXUNG = ASE_AWK_ELXUNG, + ERR_ENDSRC = ASE_AWK_EENDSRC, + ERR_ENDCMT = ASE_AWK_EENDCMT, + ERR_ENDSTR = ASE_AWK_EENDSTR, + ERR_ENDREX = ASE_AWK_EENDREX, + ERR_LBRACE = ASE_AWK_ELBRACE, + ERR_LPAREN = ASE_AWK_ELPAREN, + ERR_RPAREN = ASE_AWK_ERPAREN, + ERR_RBRACK = ASE_AWK_ERBRACK, + ERR_COMMA = ASE_AWK_ECOMMA, + ERR_SCOLON = ASE_AWK_ESCOLON, + ERR_COLON = ASE_AWK_ECOLON, + ERR_STMEND = ASE_AWK_ESTMEND, + ERR_IN = ASE_AWK_EIN, + ERR_NOTVAR = ASE_AWK_ENOTVAR, + ERR_EXPRES = ASE_AWK_EEXPRES, + ERR_WHILE = ASE_AWK_EWHILE, + ERR_ASSIGN = ASE_AWK_EASSIGN, + ERR_IDENT = ASE_AWK_EIDENT, + ERR_FNNAME = ASE_AWK_EFNNAME, + ERR_BLKBEG = ASE_AWK_EBLKBEG, + ERR_BLKEND = ASE_AWK_EBLKEND, + ERR_DUPBEG = ASE_AWK_EDUPBEG, + ERR_DUPEND = ASE_AWK_EDUPEND, + ERR_BFNRED = ASE_AWK_EBFNRED, + ERR_AFNRED = ASE_AWK_EAFNRED, + ERR_GBLRED = ASE_AWK_EGBLRED, + ERR_PARRED = ASE_AWK_EPARRED, + ERR_DUPPAR = ASE_AWK_EDUPPAR, + ERR_DUPGBL = ASE_AWK_EDUPGBL, + ERR_DUPLCL = ASE_AWK_EDUPLCL, + ERR_BADPAR = ASE_AWK_EBADPAR, + ERR_BADVAR = ASE_AWK_EBADVAR, + ERR_UNDEF = ASE_AWK_EUNDEF, + ERR_LVALUE = ASE_AWK_ELVALUE, + ERR_GBLTM = ASE_AWK_EGBLTM, + ERR_LCLTM = ASE_AWK_ELCLTM, + ERR_PARTM = ASE_AWK_EPARTM, + ERR_DELETE = ASE_AWK_EDELETE, + ERR_BREAK = ASE_AWK_EBREAK, + ERR_CONTINUE = ASE_AWK_ECONTINUE, + ERR_NEXTBEG = ASE_AWK_ENEXTBEG, + ERR_NEXTEND = ASE_AWK_ENEXTEND, + ERR_NEXTFBEG = ASE_AWK_ENEXTFBEG, + ERR_NEXTFEND = ASE_AWK_ENEXTFEND, + ERR_PRINTFARG = ASE_AWK_EPRINTFARG, + ERR_PREPST = ASE_AWK_EPREPST, + ERR_GLNCPS = ASE_AWK_EGLNCPS, + ERR_DIVBY0 = ASE_AWK_EDIVBY0, + ERR_OPERAND = ASE_AWK_EOPERAND, + ERR_POSIDX = ASE_AWK_EPOSIDX, + ERR_ARGTF = ASE_AWK_EARGTF, + ERR_ARGTM = ASE_AWK_EARGTM, + ERR_FNNONE = ASE_AWK_EFNNONE, + ERR_NOTIDX = ASE_AWK_ENOTIDX, + ERR_NOTDEL = ASE_AWK_ENOTDEL, + ERR_NOTMAP = ASE_AWK_ENOTMAP, + ERR_NOTMAPIN = ASE_AWK_ENOTMAPIN, + ERR_NOTMAPNILIN = ASE_AWK_ENOTMAPNILIN, + ERR_NOTREF = ASE_AWK_ENOTREF, + ERR_NOTASS = ASE_AWK_ENOTASS, + ERR_IDXVALASSMAP = ASE_AWK_EIDXVALASSMAP, + ERR_POSVALASSMAP = ASE_AWK_EPOSVALASSMAP, + ERR_MAPTOSCALAR = ASE_AWK_EMAPTOSCALAR, + ERR_SCALARTOMAP = ASE_AWK_ESCALARTOMAP, + ERR_MAPNOTALLOWED = ASE_AWK_EMAPNOTALLOWED, + ERR_VALTYPE = ASE_AWK_EVALTYPE, + ERR_RDELETE = ASE_AWK_ERDELETE, + ERR_RNEXTBEG = ASE_AWK_ERNEXTBEG, + ERR_RNEXTEND = ASE_AWK_ERNEXTEND, + ERR_RNEXTFBEG = ASE_AWK_ERNEXTFBEG, + ERR_RNEXTFEND = ASE_AWK_ERNEXTFEND, + ERR_BFNUSER = ASE_AWK_EBFNUSER, + ERR_BFNIMPL = ASE_AWK_EBFNIMPL, + ERR_IOUSER = ASE_AWK_EIOUSER, + ERR_IONONE = ASE_AWK_EIONONE, + ERR_IOIMPL = ASE_AWK_EIOIMPL, + ERR_IONMEM = ASE_AWK_EIONMEM, + ERR_IONMNL = ASE_AWK_EIONMNL, + ERR_FMTARG = ASE_AWK_EFMTARG, + ERR_FMTCNV = ASE_AWK_EFMTCNV, + ERR_CONVFMTCHR = ASE_AWK_ECONVFMTCHR, + ERR_OFMTCHR = ASE_AWK_EOFMTCHR, + ERR_REXRECUR = ASE_AWK_EREXRECUR, + ERR_REXRPAREN = ASE_AWK_EREXRPAREN, + ERR_REXRBRACKET = ASE_AWK_EREXRBRACKET, + ERR_REXRBRACE = ASE_AWK_EREXRBRACE, + ERR_REXUNBALPAR = ASE_AWK_EREXUNBALPAR, + ERR_REXCOLON = ASE_AWK_EREXCOLON, + ERR_REXCRANGE = ASE_AWK_EREXCRANGE, + ERR_REXCCLASS = ASE_AWK_EREXCCLASS, + ERR_REXBRANGE = ASE_AWK_EREXBRANGE, + ERR_REXEND = ASE_AWK_EREXEND, + ERR_REXGARBAGE = ASE_AWK_EREXGARBAGE, + }; + // end of enum ErrorCode - val_t* toVal () const; + /** + * Option + */ - public: - int set (long_t v); - int set (real_t v); - int set (const char_t* ptr, size_t len); - void clear (); + // generated by genoptcode.awk + enum Option + { + OPT_IMPLICIT = ASE_AWK_IMPLICIT, + OPT_EXPLICIT = ASE_AWK_EXPLICIT, + OPT_UNIQUEFN = ASE_AWK_UNIQUEFN, + OPT_SHADING = ASE_AWK_SHADING, + OPT_SHIFT = ASE_AWK_SHIFT, + OPT_IDIV = ASE_AWK_IDIV, + OPT_STRCONCAT = ASE_AWK_STRCONCAT, + OPT_EXTIO = ASE_AWK_EXTIO, + OPT_COPROC = ASE_AWK_COPROC, + OPT_BLOCKLESS = ASE_AWK_BLOCKLESS, + OPT_BASEONE = ASE_AWK_BASEONE, + OPT_STRIPSPACES = ASE_AWK_STRIPSPACES, + OPT_NEXTOFILE = ASE_AWK_NEXTOFILE, + OPT_CRLF = ASE_AWK_CRLF, + OPT_ARGSTOMAIN = ASE_AWK_ARGSTOMAIN + }; + // end of enum Option - protected: - run_t* run; - int type; + /** + * Run + */ + class Run + { + protected: + friend class Awk; - union - { - ase_long_t inum; - ase_real_t rnum; + Run (Awk* awk); - struct - { - char_t* ptr; - size_t len; - } str; - } v; - }; + public: + int stop () const; - /** - * represens the error code - */ - - // generated by generrcode.awk - enum ErrorCode - { - ERR_NOERR = ASE_AWK_ENOERR, - ERR_INVAL = ASE_AWK_EINVAL, - ERR_NOMEM = ASE_AWK_ENOMEM, - ERR_NOSUP = ASE_AWK_ENOSUP, - ERR_NOPER = ASE_AWK_ENOPER, - ERR_NODEV = ASE_AWK_ENODEV, - ERR_NOSPC = ASE_AWK_ENOSPC, - ERR_MFILE = ASE_AWK_EMFILE, - ERR_MLINK = ASE_AWK_EMLINK, - ERR_AGAIN = ASE_AWK_EAGAIN, - ERR_NOENT = ASE_AWK_ENOENT, - ERR_EXIST = ASE_AWK_EEXIST, - ERR_FTBIG = ASE_AWK_EFTBIG, - ERR_TBUSY = ASE_AWK_ETBUSY, - ERR_ISDIR = ASE_AWK_EISDIR, - ERR_IOERR = ASE_AWK_EIOERR, - ERR_OPEN = ASE_AWK_EOPEN, - ERR_READ = ASE_AWK_EREAD, - ERR_WRITE = ASE_AWK_EWRITE, - ERR_CLOSE = ASE_AWK_ECLOSE, - ERR_INTERN = ASE_AWK_EINTERN, - ERR_RUNTIME = ASE_AWK_ERUNTIME, - ERR_BLKNST = ASE_AWK_EBLKNST, - ERR_EXPRNST = ASE_AWK_EEXPRNST, - ERR_SINOP = ASE_AWK_ESINOP, - ERR_SINCL = ASE_AWK_ESINCL, - ERR_SINRD = ASE_AWK_ESINRD, - ERR_SOUTOP = ASE_AWK_ESOUTOP, - ERR_SOUTCL = ASE_AWK_ESOUTCL, - ERR_SOUTWR = ASE_AWK_ESOUTWR, - ERR_LXCHR = ASE_AWK_ELXCHR, - ERR_LXDIG = ASE_AWK_ELXDIG, - ERR_LXUNG = ASE_AWK_ELXUNG, - ERR_ENDSRC = ASE_AWK_EENDSRC, - ERR_ENDCMT = ASE_AWK_EENDCMT, - ERR_ENDSTR = ASE_AWK_EENDSTR, - ERR_ENDREX = ASE_AWK_EENDREX, - ERR_LBRACE = ASE_AWK_ELBRACE, - ERR_LPAREN = ASE_AWK_ELPAREN, - ERR_RPAREN = ASE_AWK_ERPAREN, - ERR_RBRACK = ASE_AWK_ERBRACK, - ERR_COMMA = ASE_AWK_ECOMMA, - ERR_SCOLON = ASE_AWK_ESCOLON, - ERR_COLON = ASE_AWK_ECOLON, - ERR_STMEND = ASE_AWK_ESTMEND, - ERR_IN = ASE_AWK_EIN, - ERR_NOTVAR = ASE_AWK_ENOTVAR, - ERR_EXPRES = ASE_AWK_EEXPRES, - ERR_WHILE = ASE_AWK_EWHILE, - ERR_ASSIGN = ASE_AWK_EASSIGN, - ERR_IDENT = ASE_AWK_EIDENT, - ERR_FNNAME = ASE_AWK_EFNNAME, - ERR_BLKBEG = ASE_AWK_EBLKBEG, - ERR_BLKEND = ASE_AWK_EBLKEND, - ERR_DUPBEG = ASE_AWK_EDUPBEG, - ERR_DUPEND = ASE_AWK_EDUPEND, - ERR_BFNRED = ASE_AWK_EBFNRED, - ERR_AFNRED = ASE_AWK_EAFNRED, - ERR_GBLRED = ASE_AWK_EGBLRED, - ERR_PARRED = ASE_AWK_EPARRED, - ERR_DUPPAR = ASE_AWK_EDUPPAR, - ERR_DUPGBL = ASE_AWK_EDUPGBL, - ERR_DUPLCL = ASE_AWK_EDUPLCL, - ERR_BADPAR = ASE_AWK_EBADPAR, - ERR_BADVAR = ASE_AWK_EBADVAR, - ERR_UNDEF = ASE_AWK_EUNDEF, - ERR_LVALUE = ASE_AWK_ELVALUE, - ERR_GBLTM = ASE_AWK_EGBLTM, - ERR_LCLTM = ASE_AWK_ELCLTM, - ERR_PARTM = ASE_AWK_EPARTM, - ERR_DELETE = ASE_AWK_EDELETE, - ERR_BREAK = ASE_AWK_EBREAK, - ERR_CONTINUE = ASE_AWK_ECONTINUE, - ERR_NEXTBEG = ASE_AWK_ENEXTBEG, - ERR_NEXTEND = ASE_AWK_ENEXTEND, - ERR_NEXTFBEG = ASE_AWK_ENEXTFBEG, - ERR_NEXTFEND = ASE_AWK_ENEXTFEND, - ERR_PRINTFARG = ASE_AWK_EPRINTFARG, - ERR_PREPST = ASE_AWK_EPREPST, - ERR_GLNCPS = ASE_AWK_EGLNCPS, - ERR_DIVBY0 = ASE_AWK_EDIVBY0, - ERR_OPERAND = ASE_AWK_EOPERAND, - ERR_POSIDX = ASE_AWK_EPOSIDX, - ERR_ARGTF = ASE_AWK_EARGTF, - ERR_ARGTM = ASE_AWK_EARGTM, - ERR_FNNONE = ASE_AWK_EFNNONE, - ERR_NOTIDX = ASE_AWK_ENOTIDX, - ERR_NOTDEL = ASE_AWK_ENOTDEL, - ERR_NOTMAP = ASE_AWK_ENOTMAP, - ERR_NOTMAPIN = ASE_AWK_ENOTMAPIN, - ERR_NOTMAPNILIN = ASE_AWK_ENOTMAPNILIN, - ERR_NOTREF = ASE_AWK_ENOTREF, - ERR_NOTASS = ASE_AWK_ENOTASS, - ERR_IDXVALASSMAP = ASE_AWK_EIDXVALASSMAP, - ERR_POSVALASSMAP = ASE_AWK_EPOSVALASSMAP, - ERR_MAPTOSCALAR = ASE_AWK_EMAPTOSCALAR, - ERR_SCALARTOMAP = ASE_AWK_ESCALARTOMAP, - ERR_MAPNOTALLOWED = ASE_AWK_EMAPNOTALLOWED, - ERR_VALTYPE = ASE_AWK_EVALTYPE, - ERR_RDELETE = ASE_AWK_ERDELETE, - ERR_RNEXTBEG = ASE_AWK_ERNEXTBEG, - ERR_RNEXTEND = ASE_AWK_ERNEXTEND, - ERR_RNEXTFBEG = ASE_AWK_ERNEXTFBEG, - ERR_RNEXTFEND = ASE_AWK_ERNEXTFEND, - ERR_BFNUSER = ASE_AWK_EBFNUSER, - ERR_BFNIMPL = ASE_AWK_EBFNIMPL, - ERR_IOUSER = ASE_AWK_EIOUSER, - ERR_IONONE = ASE_AWK_EIONONE, - ERR_IOIMPL = ASE_AWK_EIOIMPL, - ERR_IONMEM = ASE_AWK_EIONMEM, - ERR_IONMNL = ASE_AWK_EIONMNL, - ERR_FMTARG = ASE_AWK_EFMTARG, - ERR_FMTCNV = ASE_AWK_EFMTCNV, - ERR_CONVFMTCHR = ASE_AWK_ECONVFMTCHR, - ERR_OFMTCHR = ASE_AWK_EOFMTCHR, - ERR_REXRECUR = ASE_AWK_EREXRECUR, - ERR_REXRPAREN = ASE_AWK_EREXRPAREN, - ERR_REXRBRACKET = ASE_AWK_EREXRBRACKET, - ERR_REXRBRACE = ASE_AWK_EREXRBRACE, - ERR_REXUNBALPAR = ASE_AWK_EREXUNBALPAR, - ERR_REXCOLON = ASE_AWK_EREXCOLON, - ERR_REXCRANGE = ASE_AWK_EREXCRANGE, - ERR_REXCCLASS = ASE_AWK_EREXCCLASS, - ERR_REXBRANGE = ASE_AWK_EREXBRANGE, - ERR_REXEND = ASE_AWK_EREXEND, - ERR_REXGARBAGE = ASE_AWK_EREXGARBAGE, - }; - // end of enum ErrorCode - - /** - * Option - */ - - // generated by genoptcode.awk - enum Option - { - OPT_IMPLICIT = ASE_AWK_IMPLICIT, - OPT_EXPLICIT = ASE_AWK_EXPLICIT, - OPT_UNIQUEFN = ASE_AWK_UNIQUEFN, - OPT_SHADING = ASE_AWK_SHADING, - OPT_SHIFT = ASE_AWK_SHIFT, - OPT_IDIV = ASE_AWK_IDIV, - OPT_STRCONCAT = ASE_AWK_STRCONCAT, - OPT_EXTIO = ASE_AWK_EXTIO, - OPT_COPROC = ASE_AWK_COPROC, - OPT_BLOCKLESS = ASE_AWK_BLOCKLESS, - OPT_BASEONE = ASE_AWK_BASEONE, - OPT_STRIPSPACES = ASE_AWK_STRIPSPACES, - OPT_NEXTOFILE = ASE_AWK_NEXTOFILE, - OPT_CRLF = ASE_AWK_CRLF, - OPT_ARGSTOMAIN = ASE_AWK_ARGSTOMAIN - }; - // end of enum Option - - /** - * Run - */ - class Run - { - protected: - friend class Awk; - - Run (Awk* awk); - - public: - int stop () const; - - ErrorCode getErrorCode () const; - size_t getErrorLine () const; - const char_t* getErrorMessage () const; - - protected: - Awk* awk; - run_t* run; - bool callbackFailed; - }; - - Awk (); - virtual ~Awk (); - ErrorCode getErrorCode () const; - size_t getErrorLine () const ; + size_t getErrorLine () const; const char_t* getErrorMessage () const; protected: - void setError (ErrorCode code, size_t line = 0, - const char_t* arg = ASE_NULL, size_t len = 0); - void setError (ErrorCode code, size_t line, const char_t* msg); - - void clearError (); - void retrieveError (); - - public: - virtual int open (); - virtual void close (); - - virtual void setOption (int opt); - virtual int getOption () const; - - enum Depth - { - DEPTH_BLOCK_PARSE = ASE_AWK_DEPTH_BLOCK_PARSE, - DEPTH_BLOCK_RUN = ASE_AWK_DEPTH_BLOCK_RUN, - DEPTH_EXPR_PARSE = ASE_AWK_DEPTH_EXPR_PARSE, - DEPTH_EXPR_RUN = ASE_AWK_DEPTH_EXPR_RUN, - DEPTH_REX_BUILD = ASE_AWK_DEPTH_REX_BUILD, - DEPTH_REX_MATCH = ASE_AWK_DEPTH_REX_MATCH - }; - - virtual void setMaxDepth (int ids, size_t depth); - virtual size_t getMaxDepth (int id) const; - - virtual const char_t* getErrorString (ErrorCode num) const; - virtual int setErrorString (ErrorCode num, const char_t* str); - - virtual int setWord ( - const char_t* ow, const char_t* nw); - virtual int setWord ( - const char_t* ow, ase_size_t owl, - const char_t* nw, ase_size_t nwl); - - virtual int unsetWord (const char_t* ow); - virtual int unsetWord (const char_t* ow, ase_size_t owl); - virtual int unsetAllWords (); - - /** - * parses the source code - */ - virtual int parse (); - - /** - * executes the parse tree - */ - virtual int run (const char_t* main = ASE_NULL, - const char_t** args = ASE_NULL, size_t nargs = 0); - - /** - * defines the user-defined function - */ - typedef int (Awk::*FunctionHandler) ( - Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - - /** - * adds a new user-defined function - */ - virtual int addFunction ( - const char_t* name, size_t minArgs, size_t maxArgs, - FunctionHandler handler); - - /** - * deletes a user-defined function - */ - virtual int deleteFunction (const char_t* main); - - /** - * enables the run-time callback - */ - virtual void enableRunCallback (); - - /** - * disables the run-time callback - */ - virtual void disableRunCallback (); - - protected: - virtual int dispatchFunction ( - run_t* run, const char_t* name, size_t len); - - /** - * @name Source code I/O handlers - * A subclass should override the following methods to - * support the source code input and output. - * The awk interpreter calls the following methods when - * the parse method is invoked. - */ - /*@{*/ - /** opens the source stream */ - virtual int openSource (Source& io) = 0; - /** closes the source stream */ - virtual int closeSource (Source& io) = 0; - /** reads from the source stream */ - virtual ssize_t readSource (Source& io, char_t* buf, size_t len) = 0; - /** writes to the source stream */ - virtual ssize_t writeSource (Source& io, char_t* buf, size_t len) = 0; - /*@}*/ - - - /** - * @name Pipe I/O handlers - * Pipe operations are achieved through the following methods. - */ - /*@{*/ - virtual int openPipe (Pipe& io) = 0; - virtual int closePipe (Pipe& io) = 0; - virtual ssize_t readPipe (Pipe& io, char_t* buf, size_t len) = 0; - virtual ssize_t writePipe (Pipe& io, char_t* buf, size_t len) = 0; - virtual int flushPipe (Pipe& io) = 0; - /*@}*/ - - /** - * @name File I/O handlers - * File operations are achieved through the following methods. - */ - /*@{*/ - virtual int openFile (File& io) = 0; - virtual int closeFile (File& io) = 0; - virtual ssize_t readFile (File& io, char_t* buf, size_t len) = 0; - virtual ssize_t writeFile (File& io, char_t* buf, size_t len) = 0; - virtual int flushFile (File& io) = 0; - /*@}*/ - - /** - * @name Console I/O handlers - * Console operations are achieved through the following methods. - */ - virtual int openConsole (Console& io) = 0; - virtual int closeConsole (Console& io) = 0; - virtual ssize_t readConsole (Console& io, char_t* buf, size_t len) = 0; - virtual ssize_t writeConsole (Console& io, char_t* buf, size_t len) = 0; - virtual int flushConsole (Console& io) = 0; - virtual int nextConsole (Console& io) = 0; - /*@}*/ - - // run-time callbacks - virtual void onRunStart (const Run& run); - virtual void onRunEnd (const Run& run); - virtual void onRunReturn (const Run& run, const Argument& ret); - virtual void onRunStatement (const Run& run, size_t line); - - // primitive handlers - virtual void* allocMem (size_t n) = 0; - virtual void* reallocMem (void* ptr, size_t n) = 0; - virtual void freeMem (void* ptr) = 0; - - virtual bool_t isUpper (cint_t c) = 0; - virtual bool_t isLower (cint_t c) = 0; - virtual bool_t isAlpha (cint_t c) = 0; - virtual bool_t isDigit (cint_t c) = 0; - virtual bool_t isXdigit (cint_t c) = 0; - virtual bool_t isAlnum (cint_t c) = 0; - virtual bool_t isSpace (cint_t c) = 0; - virtual bool_t isPrint (cint_t c) = 0; - virtual bool_t isGraph (cint_t c) = 0; - virtual bool_t isCntrl (cint_t c) = 0; - virtual bool_t isPunct (cint_t c) = 0; - virtual cint_t toUpper (cint_t c) = 0; - virtual cint_t toLower (cint_t c) = 0; - - virtual real_t pow (real_t x, real_t y) = 0; - virtual int vsprintf (char_t* buf, size_t size, - const char_t* fmt, va_list arg) = 0; - virtual void vdprintf (const char_t* fmt, va_list arg) = 0; - - // static glue members for various handlers - static ssize_t sourceReader ( - int cmd, void* arg, char_t* data, size_t count); - static ssize_t sourceWriter ( - int cmd, void* arg, char_t* data, size_t count); - - static ssize_t pipeHandler ( - int cmd, void* arg, char_t* data, size_t count); - static ssize_t fileHandler ( - int cmd, void* arg, char_t* data, size_t count); - static ssize_t consoleHandler ( - int cmd, void* arg, char_t* data, size_t count); - - static int functionHandler ( - run_t* run, const char_t* name, size_t len); - static void freeFunctionMapValue (void* owner, void* value); - - static void onRunStart (run_t* run, void* custom); - static void onRunEnd (run_t* run, int errnum, void* custom); - static void onRunReturn (run_t* run, val_t* ret, void* custom); - static void onRunStatement (run_t* run, size_t line, void* custom); - - static void* allocMem (void* custom, size_t n); - static void* reallocMem (void* custom, void* ptr, size_t n); - static void freeMem (void* custom, void* ptr); - - static bool_t isUpper (void* custom, cint_t c); - static bool_t isLower (void* custom, cint_t c); - static bool_t isAlpha (void* custom, cint_t c); - static bool_t isDigit (void* custom, cint_t c); - static bool_t isXdigit (void* custom, cint_t c); - static bool_t isAlnum (void* custom, cint_t c); - static bool_t isSpace (void* custom, cint_t c); - static bool_t isPrint (void* custom, cint_t c); - static bool_t isGraph (void* custom, cint_t c); - static bool_t isCntrl (void* custom, cint_t c); - static bool_t isPunct (void* custom, cint_t c); - static cint_t toUpper (void* custom, cint_t c); - static cint_t toLower (void* custom, cint_t c); - - static real_t pow (void* custom, real_t x, real_t y); - static int sprintf (void* custom, char_t* buf, size_t size, - const char_t* fmt, ...); - static void dprintf (void* custom, const char_t* fmt, ...); - - protected: - awk_t* awk; - map_t* functionMap; - - Source sourceIn; - Source sourceOut; - - ErrorCode errnum; - size_t errlin; - char_t errmsg[256]; - - bool runCallback; - - private: - Awk (const Awk&); - Awk& operator= (const Awk&); + Awk* awk; + run_t* run; + bool callbackFailed; }; -} + Awk (); + virtual ~Awk (); + + ErrorCode getErrorCode () const; + size_t getErrorLine () const ; + const char_t* getErrorMessage () const; + +protected: + void setError (ErrorCode code, size_t line = 0, + const char_t* arg = ASE_NULL, size_t len = 0); + void setError (ErrorCode code, size_t line, const char_t* msg); + + void clearError (); + void retrieveError (); + +public: + virtual int open (); + virtual void close (); + + virtual void setOption (int opt); + virtual int getOption () const; + + enum Depth + { + DEPTH_BLOCK_PARSE = ASE_AWK_DEPTH_BLOCK_PARSE, + DEPTH_BLOCK_RUN = ASE_AWK_DEPTH_BLOCK_RUN, + DEPTH_EXPR_PARSE = ASE_AWK_DEPTH_EXPR_PARSE, + DEPTH_EXPR_RUN = ASE_AWK_DEPTH_EXPR_RUN, + DEPTH_REX_BUILD = ASE_AWK_DEPTH_REX_BUILD, + DEPTH_REX_MATCH = ASE_AWK_DEPTH_REX_MATCH + }; + + virtual void setMaxDepth (int ids, size_t depth); + virtual size_t getMaxDepth (int id) const; + + virtual const char_t* getErrorString (ErrorCode num) const; + virtual int setErrorString (ErrorCode num, const char_t* str); + + virtual int setWord ( + const char_t* ow, const char_t* nw); + virtual int setWord ( + const char_t* ow, ase_size_t owl, + const char_t* nw, ase_size_t nwl); + + virtual int unsetWord (const char_t* ow); + virtual int unsetWord (const char_t* ow, ase_size_t owl); + virtual int unsetAllWords (); + + /** + * parses the source code + */ + virtual int parse (); + + /** + * executes the parse tree + */ + virtual int run (const char_t* main = ASE_NULL, + const char_t** args = ASE_NULL, size_t nargs = 0); + + /** + * defines the user-defined function + */ + typedef int (Awk::*FunctionHandler) ( + Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + + /** + * adds a new user-defined function + */ + virtual int addFunction ( + const char_t* name, size_t minArgs, size_t maxArgs, + FunctionHandler handler); + + /** + * deletes a user-defined function + */ + virtual int deleteFunction (const char_t* main); + + /** + * enables the run-time callback + */ + virtual void enableRunCallback (); + + /** + * disables the run-time callback + */ + virtual void disableRunCallback (); + +protected: + virtual int dispatchFunction ( + run_t* run, const char_t* name, size_t len); + + /** + * @name Source code I/O handlers + * A subclass should override the following methods to + * support the source code input and output. + * The awk interpreter calls the following methods when + * the parse method is invoked. + */ + /*@{*/ + /** opens the source stream */ + virtual int openSource (Source& io) = 0; + /** closes the source stream */ + virtual int closeSource (Source& io) = 0; + /** reads from the source stream */ + virtual ssize_t readSource (Source& io, char_t* buf, size_t len) = 0; + /** writes to the source stream */ + virtual ssize_t writeSource (Source& io, char_t* buf, size_t len) = 0; + /*@}*/ + + + /** + * @name Pipe I/O handlers + * Pipe operations are achieved through the following methods. + */ + /*@{*/ + virtual int openPipe (Pipe& io) = 0; + virtual int closePipe (Pipe& io) = 0; + virtual ssize_t readPipe (Pipe& io, char_t* buf, size_t len) = 0; + virtual ssize_t writePipe (Pipe& io, char_t* buf, size_t len) = 0; + virtual int flushPipe (Pipe& io) = 0; + /*@}*/ + + /** + * @name File I/O handlers + * File operations are achieved through the following methods. + */ + /*@{*/ + virtual int openFile (File& io) = 0; + virtual int closeFile (File& io) = 0; + virtual ssize_t readFile (File& io, char_t* buf, size_t len) = 0; + virtual ssize_t writeFile (File& io, char_t* buf, size_t len) = 0; + virtual int flushFile (File& io) = 0; + /*@}*/ + + /** + * @name Console I/O handlers + * Console operations are achieved through the following methods. + */ + virtual int openConsole (Console& io) = 0; + virtual int closeConsole (Console& io) = 0; + virtual ssize_t readConsole (Console& io, char_t* buf, size_t len) = 0; + virtual ssize_t writeConsole (Console& io, char_t* buf, size_t len) = 0; + virtual int flushConsole (Console& io) = 0; + virtual int nextConsole (Console& io) = 0; + /*@}*/ + + // run-time callbacks + virtual void onRunStart (const Run& run); + virtual void onRunEnd (const Run& run); + virtual void onRunReturn (const Run& run, const Argument& ret); + virtual void onRunStatement (const Run& run, size_t line); + + // primitive handlers + virtual void* allocMem (size_t n) = 0; + virtual void* reallocMem (void* ptr, size_t n) = 0; + virtual void freeMem (void* ptr) = 0; + + virtual bool_t isUpper (cint_t c) = 0; + virtual bool_t isLower (cint_t c) = 0; + virtual bool_t isAlpha (cint_t c) = 0; + virtual bool_t isDigit (cint_t c) = 0; + virtual bool_t isXdigit (cint_t c) = 0; + virtual bool_t isAlnum (cint_t c) = 0; + virtual bool_t isSpace (cint_t c) = 0; + virtual bool_t isPrint (cint_t c) = 0; + virtual bool_t isGraph (cint_t c) = 0; + virtual bool_t isCntrl (cint_t c) = 0; + virtual bool_t isPunct (cint_t c) = 0; + virtual cint_t toUpper (cint_t c) = 0; + virtual cint_t toLower (cint_t c) = 0; + + virtual real_t pow (real_t x, real_t y) = 0; + virtual int vsprintf (char_t* buf, size_t size, + const char_t* fmt, va_list arg) = 0; + virtual void vdprintf (const char_t* fmt, va_list arg) = 0; + + // static glue members for various handlers + static ssize_t sourceReader ( + int cmd, void* arg, char_t* data, size_t count); + static ssize_t sourceWriter ( + int cmd, void* arg, char_t* data, size_t count); + + static ssize_t pipeHandler ( + int cmd, void* arg, char_t* data, size_t count); + static ssize_t fileHandler ( + int cmd, void* arg, char_t* data, size_t count); + static ssize_t consoleHandler ( + int cmd, void* arg, char_t* data, size_t count); + + static int functionHandler ( + run_t* run, const char_t* name, size_t len); + static void freeFunctionMapValue (void* owner, void* value); + + static void onRunStart (run_t* run, void* custom); + static void onRunEnd (run_t* run, int errnum, void* custom); + static void onRunReturn (run_t* run, val_t* ret, void* custom); + static void onRunStatement (run_t* run, size_t line, void* custom); + + static void* allocMem (void* custom, size_t n); + static void* reallocMem (void* custom, void* ptr, size_t n); + static void freeMem (void* custom, void* ptr); + + static bool_t isUpper (void* custom, cint_t c); + static bool_t isLower (void* custom, cint_t c); + static bool_t isAlpha (void* custom, cint_t c); + static bool_t isDigit (void* custom, cint_t c); + static bool_t isXdigit (void* custom, cint_t c); + static bool_t isAlnum (void* custom, cint_t c); + static bool_t isSpace (void* custom, cint_t c); + static bool_t isPrint (void* custom, cint_t c); + static bool_t isGraph (void* custom, cint_t c); + static bool_t isCntrl (void* custom, cint_t c); + static bool_t isPunct (void* custom, cint_t c); + static cint_t toUpper (void* custom, cint_t c); + static cint_t toLower (void* custom, cint_t c); + + static real_t pow (void* custom, real_t x, real_t y); + static int sprintf (void* custom, char_t* buf, size_t size, + const char_t* fmt, ...); + static void dprintf (void* custom, const char_t* fmt, ...); + +protected: + awk_t* awk; + map_t* functionMap; + + Source sourceIn; + Source sourceOut; + + ErrorCode errnum; + size_t errlin; + char_t errmsg[256]; + + bool runCallback; + +private: + Awk (const Awk&); + Awk& operator= (const Awk&); +}; + +///////////////////////////////// +ASE_END_NAMESPACE(ASE) +///////////////////////////////// #endif diff --git a/ase/awk/StdAwk.cpp b/ase/awk/StdAwk.cpp index e85fde92..f4aab16d 100644 --- a/ase/awk/StdAwk.cpp +++ b/ase/awk/StdAwk.cpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.cpp,v 1.25 2007/09/06 09:23:34 bacon Exp $ + * $Id: StdAwk.cpp,v 1.26 2007/09/07 05:40:16 bacon Exp $ */ #include @@ -12,122 +12,124 @@ #include #ifdef _WIN32 -#include + #include #else -#include + #include #endif -namespace ASE +///////////////////////////////// +ASE_BEGIN_NAMESPACE(ASE) +///////////////////////////////// + + +StdAwk::StdAwk () { + seed = (unsigned int)::time(NULL); + ::srand (seed); +} - StdAwk::StdAwk () - { - seed = (unsigned int)::time(NULL); - ::srand (seed); - } +#define ADD_FUNC(name,min,max,impl) \ + do { \ + if (addFunction (name, min, max, \ + (FunctionHandler)impl) == -1) \ + { \ + Awk::close (); \ + return -1; \ + } \ + } while (0) - #define ADD_FUNC(name,min,max,impl) \ - do { \ - if (addFunction (name, min, max, \ - (FunctionHandler)impl) == -1) \ - { \ - Awk::close (); \ - return -1; \ - } \ - } while (0) +int StdAwk::open () +{ + int n = Awk::open (); + if (n == -1) return n; - int StdAwk::open () - { - int n = Awk::open (); - if (n == -1) return n; + ADD_FUNC (ASE_T("sin"), 1, 1, &StdAwk::sin); + ADD_FUNC (ASE_T("cos"), 1, 1, &StdAwk::cos); + ADD_FUNC (ASE_T("tan"), 1, 1, &StdAwk::tan); + ADD_FUNC (ASE_T("atan"), 1, 1, &StdAwk::atan); + ADD_FUNC (ASE_T("atan2"), 2, 2, &StdAwk::atan2); + ADD_FUNC (ASE_T("log"), 1, 1, &StdAwk::log); + ADD_FUNC (ASE_T("exp"), 1, 1, &StdAwk::exp); + ADD_FUNC (ASE_T("sqrt"), 1, 1, &StdAwk::sqrt); + ADD_FUNC (ASE_T("int"), 1, 1, &StdAwk::fnint); + ADD_FUNC (ASE_T("rand"), 0, 0, &StdAwk::rand); + ADD_FUNC (ASE_T("srand"), 1, 1, &StdAwk::srand); + ADD_FUNC (ASE_T("systime"), 0, 0, &StdAwk::systime); + ADD_FUNC (ASE_T("strftime"), 0, 2, &StdAwk::strftime); + ADD_FUNC (ASE_T("strfgmtime"), 0, 2, &StdAwk::strfgmtime); + ADD_FUNC (ASE_T("system"), 1, 1, &StdAwk::system); - ADD_FUNC (ASE_T("sin"), 1, 1, &StdAwk::sin); - ADD_FUNC (ASE_T("cos"), 1, 1, &StdAwk::cos); - ADD_FUNC (ASE_T("tan"), 1, 1, &StdAwk::tan); - ADD_FUNC (ASE_T("atan"), 1, 1, &StdAwk::atan); - ADD_FUNC (ASE_T("atan2"), 2, 2, &StdAwk::atan2); - ADD_FUNC (ASE_T("log"), 1, 1, &StdAwk::log); - ADD_FUNC (ASE_T("exp"), 1, 1, &StdAwk::exp); - ADD_FUNC (ASE_T("sqrt"), 1, 1, &StdAwk::sqrt); - ADD_FUNC (ASE_T("int"), 1, 1, &StdAwk::fnint); - ADD_FUNC (ASE_T("rand"), 0, 0, &StdAwk::rand); - ADD_FUNC (ASE_T("srand"), 1, 1, &StdAwk::srand); - ADD_FUNC (ASE_T("systime"), 0, 0, &StdAwk::systime); - ADD_FUNC (ASE_T("strftime"), 0, 2, &StdAwk::strftime); - ADD_FUNC (ASE_T("strfgmtime"), 0, 2, &StdAwk::strfgmtime); - ADD_FUNC (ASE_T("system"), 1, 1, &StdAwk::system); + return 0; +} - return 0; - } +int StdAwk::sin (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::sin(args[0].toReal())); +} - int StdAwk::sin (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::sin(args[0].toReal())); - } +int StdAwk::cos (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::cos(args[0].toReal())); +} - int StdAwk::cos (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::cos(args[0].toReal())); - } +int StdAwk::tan (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::tan(args[0].toReal())); +} - int StdAwk::tan (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::tan(args[0].toReal())); - } +int StdAwk::atan (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::atan(args[0].toReal())); +} - int StdAwk::atan (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::atan(args[0].toReal())); - } +int StdAwk::atan2 (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::atan2(args[0].toReal(), args[1].toReal())); +} - int StdAwk::atan2 (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::atan2(args[0].toReal(), args[1].toReal())); - } +int StdAwk::log (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::log(args[0].toReal())); +} - int StdAwk::log (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::log(args[0].toReal())); - } +int StdAwk::exp (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::exp(args[0].toReal())); +} - int StdAwk::exp (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::exp(args[0].toReal())); - } +int StdAwk::sqrt (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((real_t)::sqrt(args[0].toReal())); +} - int StdAwk::sqrt (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((real_t)::sqrt(args[0].toReal())); - } +int StdAwk::fnint (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set (args[0].toInt()); +} - int StdAwk::fnint (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set (args[0].toInt()); - } +int StdAwk::rand (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((long_t)::rand()); +} - int StdAwk::rand (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((long_t)::rand()); - } - - int StdAwk::srand (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - unsigned int prevSeed = seed; - seed = (unsigned int)args[0].toInt(); - ::srand (seed); - return ret->set ((long_t)prevSeed); - } +int StdAwk::srand (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + unsigned int prevSeed = seed; + seed = (unsigned int)args[0].toInt(); + ::srand (seed); + return ret->set ((long_t)prevSeed); +} #if defined(_WIN32) && defined(_MSC_VER) && (_MSC_VER>=1400) #define time_t __time64_t @@ -136,360 +138,363 @@ namespace ASE #define gmtime _gmtime64 #endif - int StdAwk::systime (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - return ret->set ((long_t)::time(NULL)); - } - - int StdAwk::strftime (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - const char_t* fmt; - size_t fln; - - fmt = (nargs < 1)? ASE_T("%c"): args[0].toStr(&fln); - time_t t = (nargs < 2)? ::time(NULL): (time_t)args[1].toInt(); - - char_t buf[128]; - struct tm* tm; - #ifdef _WIN32 - tm = ::localtime (&t); - #else - struct tm tmb; - tm = ::localtime_r (&t, &tmb); - #endif - - #ifdef ASE_CHAR_IS_MCHAR - size_t l = ::strftime (buf, ASE_COUNTOF(buf), fmt, tm); - #else - size_t l = ::wcsftime (buf, ASE_COUNTOF(buf), fmt, tm); - #endif - - return ret->set (buf, l); - } - - int StdAwk::strfgmtime (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - const char_t* fmt; - size_t fln; - - fmt = (nargs < 1)? ASE_T("%c"): args[0].toStr(&fln); - time_t t = (nargs < 2)? ::time(NULL): (time_t)args[1].toInt(); - - char_t buf[128]; - struct tm* tm; - #ifdef _WIN32 - tm = ::gmtime (&t); - #else - struct tm tmb; - tm = ::gmtime_r (&t, &tmb); - #endif - - #ifdef ASE_CHAR_IS_MCHAR - size_t l = ::strftime (buf, ASE_COUNTOF(buf), fmt, tm); - #else - size_t l = ::wcsftime (buf, ASE_COUNTOF(buf), fmt, tm); - #endif - - return ret->set (buf, l); - } - - int StdAwk::system (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len) - { - size_t l; - const char_t* ptr = args[0].toStr(&l); - - #ifdef _WIN32 - return ret->set ((long_t)::_tsystem(ptr)); - #elif defined(ASE_CHAR_IS_MCHAR) - return ret->set ((long_t)::system(ptr)); - #else - char* mbs = (char*)ase_awk_malloc (awk, l*5+1); - if (mbs == ASE_NULL) return -1; - - ::size_t mbl = ::wcstombs (mbs, ptr, l*5); - if (mbl == (::size_t)-1) return -1; - mbs[mbl] = '\0'; - int n = ret->set ((long_t)::system(mbs)); - - ase_awk_free (awk, mbs); - return n; - #endif - } - - int StdAwk::openPipe (Pipe& io) - { - Awk::Pipe::Mode mode = io.getMode(); - FILE* fp = NULL; - - switch (mode) - { - case Awk::Pipe::READ: - fp = ase_popen (io.getName(), ASE_T("r")); - break; - case Awk::Pipe::WRITE: - fp = ase_popen (io.getName(), ASE_T("w")); - break; - } - - if (fp == NULL) return -1; - - io.setHandle (fp); - return 1; - } - - int StdAwk::closePipe (Pipe& io) - { - fclose ((FILE*)io.getHandle()); - return 0; - } - - StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len) - { - FILE* fp = (FILE*)io.getHandle(); - ssize_t n = 0; - - while (n < (ssize_t)len) - { - ase_cint_t c = ase_fgetc (fp); - if (c == ASE_CHAR_EOF) break; - - buf[n++] = c; - if (c == ASE_T('\n')) break; - } - - return n; - } - - StdAwk::ssize_t StdAwk::writePipe (Pipe& io, char_t* buf, size_t len) - { - FILE* fp = (FILE*)io.getHandle(); - size_t left = len; - - while (left > 0) - { - if (*buf == ASE_T('\0')) - { - #if defined(ASE_CHAR_IS_WCHAR) && defined(__linux) - if (fputc ('\0', fp) == EOF) - #else - if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) - #endif - { - return -1; - } - left -= 1; buf += 1; - } - else - { - #if defined(ASE_CHAR_IS_WCHAR) && defined(__linux) - // fwprintf seems to return an error with the file - // pointer opened by popen, as of this writing. - // anyway, hopefully the following replacement - // will work all the way. - int n = fprintf (fp, "%.*ls", left, buf); - if (n >= 0) - { - size_t x; - for (x = 0; x < left; x++) - { - if (buf[x] == ASE_T('\0')) break; - } - n = x; - } - #else - int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf); - #endif - - if (n < 0 || n > (ssize_t)left) return -1; - left -= n; buf += n; - } - } - - return len; - } - - int StdAwk::flushPipe (Pipe& io) - { - return ::fflush ((FILE*)io.getHandle()); - } - - // file io handlers - int StdAwk::openFile (File& io) - { - Awk::File::Mode mode = io.getMode(); - FILE* fp = NULL; - - switch (mode) - { - case Awk::File::READ: - fp = ase_fopen (io.getName(), ASE_T("r")); - break; - case Awk::File::WRITE: - fp = ase_fopen (io.getName(), ASE_T("w")); - break; - case Awk::File::APPEND: - fp = ase_fopen (io.getName(), ASE_T("a")); - break; - } - - if (fp == NULL) return -1; - - io.setHandle (fp); - return 1; - } - - int StdAwk::closeFile (File& io) - { - fclose ((FILE*)io.getHandle()); - return 0; - } - - StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len) - { - FILE* fp = (FILE*)io.getHandle(); - ssize_t n = 0; - - while (n < (ssize_t)len) - { - ase_cint_t c = ase_fgetc (fp); - if (c == ASE_CHAR_EOF) break; - - buf[n++] = c; - if (c == ASE_T('\n')) break; - } - - return n; - } - - StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len) - { - FILE* fp = (FILE*)io.getHandle(); - size_t left = len; - - while (left > 0) - { - if (*buf == ASE_T('\0')) - { - if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) return -1; - left -= 1; buf += 1; - } - else - { - int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf); - if (n < 0 || n > (ssize_t)left) return -1; - left -= n; buf += n; - } - } - - return len; - } - - int StdAwk::flushFile (File& io) - { - return ::fflush ((FILE*)io.getHandle()); - } - - // memory allocation primitives - void* StdAwk::allocMem (size_t n) - { - return ::malloc (n); - } - - void* StdAwk::reallocMem (void* ptr, size_t n) - { - return ::realloc (ptr, n); - } - - void StdAwk::freeMem (void* ptr) - { - ::free (ptr); - } - - // character class primitives - StdAwk::bool_t StdAwk::isUpper (cint_t c) - { - return ase_isupper (c); - } - - StdAwk::bool_t StdAwk::isLower (cint_t c) - { - return ase_islower (c); - } - - StdAwk::bool_t StdAwk::isAlpha (cint_t c) - { - return ase_isalpha (c); - } - - StdAwk::bool_t StdAwk::isDigit (cint_t c) - { - return ase_isdigit (c); - } - - StdAwk::bool_t StdAwk::isXdigit (cint_t c) - { - return ase_isxdigit (c); - } - - StdAwk::bool_t StdAwk::isAlnum (cint_t c) - { - return ase_isalnum (c); - } - - StdAwk::bool_t StdAwk::isSpace (cint_t c) - { - return ase_isspace (c); - } - - StdAwk::bool_t StdAwk::isPrint (cint_t c) - { - return ase_isprint (c); - } - - StdAwk::bool_t StdAwk::isGraph (cint_t c) - { - return ase_isgraph (c); - } - - StdAwk::bool_t StdAwk::isCntrl (cint_t c) - { - return ase_iscntrl (c); - } - - StdAwk::bool_t StdAwk::isPunct (cint_t c) - { - return ase_ispunct (c); - } - - StdAwk::cint_t StdAwk::toUpper (cint_t c) - { - return ase_toupper (c); - } - - StdAwk::cint_t StdAwk::toLower (cint_t c) - { - return ase_tolower (c); - } - - // miscellaneous primitive - StdAwk::real_t StdAwk::pow (real_t x, real_t y) - { - return ::pow (x, y); - } - - int StdAwk::vsprintf ( - char_t* buf, size_t size, const char_t* fmt, va_list arg) - { - return ase_vsprintf (buf, size, fmt, arg); - } - - void StdAwk::vdprintf (const char_t* fmt, va_list arg) - { - ase_vfprintf (stderr, fmt, arg); - } - +int StdAwk::systime (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + return ret->set ((long_t)::time(NULL)); } + +int StdAwk::strftime (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + const char_t* fmt; + size_t fln; + + fmt = (nargs < 1)? ASE_T("%c"): args[0].toStr(&fln); + time_t t = (nargs < 2)? ::time(NULL): (time_t)args[1].toInt(); + + char_t buf[128]; + struct tm* tm; +#ifdef _WIN32 + tm = ::localtime (&t); +#else + struct tm tmb; + tm = ::localtime_r (&t, &tmb); +#endif + +#ifdef ASE_CHAR_IS_MCHAR + size_t l = ::strftime (buf, ASE_COUNTOF(buf), fmt, tm); +#else + size_t l = ::wcsftime (buf, ASE_COUNTOF(buf), fmt, tm); +#endif + + return ret->set (buf, l); +} + +int StdAwk::strfgmtime (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + const char_t* fmt; + size_t fln; + + fmt = (nargs < 1)? ASE_T("%c"): args[0].toStr(&fln); + time_t t = (nargs < 2)? ::time(NULL): (time_t)args[1].toInt(); + + char_t buf[128]; + struct tm* tm; +#ifdef _WIN32 + tm = ::gmtime (&t); +#else + struct tm tmb; + tm = ::gmtime_r (&t, &tmb); +#endif + +#ifdef ASE_CHAR_IS_MCHAR + size_t l = ::strftime (buf, ASE_COUNTOF(buf), fmt, tm); +#else + size_t l = ::wcsftime (buf, ASE_COUNTOF(buf), fmt, tm); +#endif + + return ret->set (buf, l); +} + +int StdAwk::system (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len) +{ + size_t l; + const char_t* ptr = args[0].toStr(&l); + +#ifdef _WIN32 + return ret->set ((long_t)::_tsystem(ptr)); +#elif defined(ASE_CHAR_IS_MCHAR) + return ret->set ((long_t)::system(ptr)); +#else + char* mbs = (char*)ase_awk_malloc (awk, l*5+1); + if (mbs == ASE_NULL) return -1; + + ::size_t mbl = ::wcstombs (mbs, ptr, l*5); + if (mbl == (::size_t)-1) return -1; + mbs[mbl] = '\0'; + int n = ret->set ((long_t)::system(mbs)); + + ase_awk_free (awk, mbs); + return n; +#endif +} + +int StdAwk::openPipe (Pipe& io) +{ + Awk::Pipe::Mode mode = io.getMode(); + FILE* fp = NULL; + + switch (mode) + { + case Awk::Pipe::READ: + fp = ase_popen (io.getName(), ASE_T("r")); + break; + case Awk::Pipe::WRITE: + fp = ase_popen (io.getName(), ASE_T("w")); + break; + } + + if (fp == NULL) return -1; + + io.setHandle (fp); + return 1; +} + +int StdAwk::closePipe (Pipe& io) +{ + fclose ((FILE*)io.getHandle()); + return 0; +} + +StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len) +{ + FILE* fp = (FILE*)io.getHandle(); + ssize_t n = 0; + + while (n < (ssize_t)len) + { + ase_cint_t c = ase_fgetc (fp); + if (c == ASE_CHAR_EOF) break; + + buf[n++] = c; + if (c == ASE_T('\n')) break; + } + + return n; +} + +StdAwk::ssize_t StdAwk::writePipe (Pipe& io, char_t* buf, size_t len) +{ + FILE* fp = (FILE*)io.getHandle(); + size_t left = len; + + while (left > 0) + { + if (*buf == ASE_T('\0')) + { + #if defined(ASE_CHAR_IS_WCHAR) && defined(__linux) + if (fputc ('\0', fp) == EOF) + #else + if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) + #endif + { + return -1; + } + left -= 1; buf += 1; + } + else + { + #if defined(ASE_CHAR_IS_WCHAR) && defined(__linux) + // fwprintf seems to return an error with the file + // pointer opened by popen, as of this writing. + // anyway, hopefully the following replacement + // will work all the way. + int n = fprintf (fp, "%.*ls", left, buf); + if (n >= 0) + { + size_t x; + for (x = 0; x < left; x++) + { + if (buf[x] == ASE_T('\0')) break; + } + n = x; + } + #else + int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf); + #endif + + if (n < 0 || n > (ssize_t)left) return -1; + left -= n; buf += n; + } + } + + return len; +} + +int StdAwk::flushPipe (Pipe& io) +{ + return ::fflush ((FILE*)io.getHandle()); +} + +// file io handlers +int StdAwk::openFile (File& io) +{ + Awk::File::Mode mode = io.getMode(); + FILE* fp = NULL; + + switch (mode) + { + case Awk::File::READ: + fp = ase_fopen (io.getName(), ASE_T("r")); + break; + case Awk::File::WRITE: + fp = ase_fopen (io.getName(), ASE_T("w")); + break; + case Awk::File::APPEND: + fp = ase_fopen (io.getName(), ASE_T("a")); + break; + } + + if (fp == NULL) return -1; + + io.setHandle (fp); + return 1; +} + +int StdAwk::closeFile (File& io) +{ + fclose ((FILE*)io.getHandle()); + return 0; +} + +StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len) +{ + FILE* fp = (FILE*)io.getHandle(); + ssize_t n = 0; + + while (n < (ssize_t)len) + { + ase_cint_t c = ase_fgetc (fp); + if (c == ASE_CHAR_EOF) break; + + buf[n++] = c; + if (c == ASE_T('\n')) break; + } + + return n; +} + +StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len) +{ + FILE* fp = (FILE*)io.getHandle(); + size_t left = len; + + while (left > 0) + { + if (*buf == ASE_T('\0')) + { + if (ase_fputc (*buf, fp) == ASE_CHAR_EOF) return -1; + left -= 1; buf += 1; + } + else + { + int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf); + if (n < 0 || n > (ssize_t)left) return -1; + left -= n; buf += n; + } + } + + return len; +} + +int StdAwk::flushFile (File& io) +{ + return ::fflush ((FILE*)io.getHandle()); +} + +// memory allocation primitives +void* StdAwk::allocMem (size_t n) +{ + return ::malloc (n); +} + +void* StdAwk::reallocMem (void* ptr, size_t n) +{ + return ::realloc (ptr, n); +} + +void StdAwk::freeMem (void* ptr) +{ + ::free (ptr); +} + +// character class primitives +StdAwk::bool_t StdAwk::isUpper (cint_t c) +{ + return ase_isupper (c); +} + +StdAwk::bool_t StdAwk::isLower (cint_t c) +{ + return ase_islower (c); +} + +StdAwk::bool_t StdAwk::isAlpha (cint_t c) +{ + return ase_isalpha (c); +} + +StdAwk::bool_t StdAwk::isDigit (cint_t c) +{ + return ase_isdigit (c); +} + +StdAwk::bool_t StdAwk::isXdigit (cint_t c) +{ + return ase_isxdigit (c); +} + +StdAwk::bool_t StdAwk::isAlnum (cint_t c) +{ + return ase_isalnum (c); +} + +StdAwk::bool_t StdAwk::isSpace (cint_t c) +{ + return ase_isspace (c); +} + +StdAwk::bool_t StdAwk::isPrint (cint_t c) +{ + return ase_isprint (c); +} + +StdAwk::bool_t StdAwk::isGraph (cint_t c) +{ + return ase_isgraph (c); +} + +StdAwk::bool_t StdAwk::isCntrl (cint_t c) +{ + return ase_iscntrl (c); +} + +StdAwk::bool_t StdAwk::isPunct (cint_t c) +{ + return ase_ispunct (c); +} + +StdAwk::cint_t StdAwk::toUpper (cint_t c) +{ + return ase_toupper (c); +} + +StdAwk::cint_t StdAwk::toLower (cint_t c) +{ + return ase_tolower (c); +} + +// miscellaneous primitive +StdAwk::real_t StdAwk::pow (real_t x, real_t y) +{ + return ::pow (x, y); +} + +int StdAwk::vsprintf ( + char_t* buf, size_t size, const char_t* fmt, va_list arg) +{ + return ase_vsprintf (buf, size, fmt, arg); +} + +void StdAwk::vdprintf (const char_t* fmt, va_list arg) +{ + ase_vfprintf (stderr, fmt, arg); +} + +///////////////////////////////// +ASE_END_NAMESPACE(ASE) +///////////////////////////////// + diff --git a/ase/awk/StdAwk.hpp b/ase/awk/StdAwk.hpp index 0809cb20..c8156991 100644 --- a/ase/awk/StdAwk.hpp +++ b/ase/awk/StdAwk.hpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.hpp,v 1.13 2007/08/26 14:33:38 bacon Exp $ + * $Id: StdAwk.hpp,v 1.14 2007/09/07 05:40:16 bacon Exp $ */ #ifndef _ASE_AWK_STDAWK_HPP_ @@ -7,92 +7,95 @@ #include -namespace ASE +///////////////////////////////// +ASE_BEGIN_NAMESPACE(ASE) +///////////////////////////////// + +class StdAwk: public Awk { - class StdAwk: public Awk - { - public: - StdAwk (); +public: + StdAwk (); + int open (); - int open (); +protected: - protected: + // builtin functions + int sin (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int cos (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int tan (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int atan (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int atan2 (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int log (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int exp (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int sqrt (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int fnint (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int rand (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int srand (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int systime (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int strftime (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int strfgmtime (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); + int system (Return* ret, const Argument* args, size_t nargs, + const char_t* name, size_t len); - // builtin functions - int sin (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int cos (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int tan (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int atan (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int atan2 (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int log (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int exp (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int sqrt (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int fnint (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int rand (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int srand (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int systime (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int strftime (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int strfgmtime (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); - int system (Return* ret, const Argument* args, size_t nargs, - const char_t* name, size_t len); + // pipe io handlers + int openPipe (Pipe& io); + int closePipe (Pipe& io); + ssize_t readPipe (Pipe& io, char_t* buf, size_t len); + ssize_t writePipe (Pipe& io, char_t* buf, size_t len); + int flushPipe (Pipe& io); - // pipe io handlers - int openPipe (Pipe& io); - int closePipe (Pipe& io); - ssize_t readPipe (Pipe& io, char_t* buf, size_t len); - ssize_t writePipe (Pipe& io, char_t* buf, size_t len); - int flushPipe (Pipe& io); + // file io handlers + int openFile (File& io); + int closeFile (File& io); + ssize_t readFile (File& io, char_t* buf, size_t len); + ssize_t writeFile (File& io, char_t* buf, size_t len); + int flushFile (File& io); - // file io handlers - int openFile (File& io); - int closeFile (File& io); - ssize_t readFile (File& io, char_t* buf, size_t len); - ssize_t writeFile (File& io, char_t* buf, size_t len); - int flushFile (File& io); + // primitive handlers + void* allocMem (size_t n); + void* reallocMem (void* ptr, size_t n); + void freeMem (void* ptr); - // primitive handlers - void* allocMem (size_t n); - void* reallocMem (void* ptr, size_t n); - void freeMem (void* ptr); + bool_t isUpper (cint_t c); + bool_t isLower (cint_t c); + bool_t isAlpha (cint_t c); + bool_t isDigit (cint_t c); + bool_t isXdigit (cint_t c); + bool_t isAlnum (cint_t c); + bool_t isSpace (cint_t c); + bool_t isPrint (cint_t c); + bool_t isGraph (cint_t c); + bool_t isCntrl (cint_t c); + bool_t isPunct (cint_t c); + cint_t toUpper (cint_t c); + cint_t toLower (cint_t c); - bool_t isUpper (cint_t c); - bool_t isLower (cint_t c); - bool_t isAlpha (cint_t c); - bool_t isDigit (cint_t c); - bool_t isXdigit (cint_t c); - bool_t isAlnum (cint_t c); - bool_t isSpace (cint_t c); - bool_t isPrint (cint_t c); - bool_t isGraph (cint_t c); - bool_t isCntrl (cint_t c); - bool_t isPunct (cint_t c); - cint_t toUpper (cint_t c); - cint_t toLower (cint_t c); + real_t pow (real_t x, real_t y); + int vsprintf (char_t* buf, size_t size, + const char_t* fmt, va_list arg); + void vdprintf (const char_t* fmt, va_list arg); - real_t pow (real_t x, real_t y); - int vsprintf (char_t* buf, size_t size, - const char_t* fmt, va_list arg); - void vdprintf (const char_t* fmt, va_list arg); +protected: + unsigned int seed; +}; - protected: - unsigned int seed; - }; - -} +///////////////////////////////// +ASE_END_NAMESPACE(ASE) +///////////////////////////////// #endif diff --git a/ase/cmn/macros.h b/ase/cmn/macros.h index c97b4044..541dc3d6 100644 --- a/ase/cmn/macros.h +++ b/ase/cmn/macros.h @@ -1,5 +1,5 @@ /* - * $Id: macros.h,v 1.5 2007/05/06 10:08:36 bacon Exp $ + * $Id: macros.h,v 1.6 2007/09/07 05:40:16 bacon Exp $ * * {License} */ @@ -150,4 +150,12 @@ #define ASE_TOUPPER(ccls,c) (ccls)->to_upper((ccls)->custom_data,c) #define ASE_TOLOWER(ccls,c) (ccls)->to_lower((ccls)->custom_data,c) + +#ifdef __cplusplus + #define ASE_BEGIN_NAMESPACE(x) namespace x { + #define ASE_END_NAMESPACE(x) } + #define ASE_BEGIN_NAMESPACE2(x,y) namespace x { namespace y { + #define ASE_END_NAMESPACE2(y,x) }} +#endif + #endif