diff --git a/ase/awk/Awk.cpp b/ase/awk/Awk.cpp index c8e6bfac..d29c00c4 100644 --- a/ase/awk/Awk.cpp +++ b/ase/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp,v 1.31 2007/05/17 15:02:54 bacon Exp $ + * $Id: Awk.cpp,v 1.35 2007/05/19 16:45:27 bacon Exp $ */ #include @@ -56,16 +56,6 @@ namespace ASE extio->handle = handle; } - Awk::run_t* Awk::Extio::getRun () const - { - return extio->run; - } - - Awk::awk_t* Awk::Extio::getAwk () const - { - return ase_awk_getrunawk(extio->run); - } - ////////////////////////////////////////////////////////////////// // Awk::Pipe ////////////////////////////////////////////////////////////////// @@ -281,18 +271,6 @@ namespace ASE } } - Awk::run_t* Awk::Argument::getRun () const - { - ASE_ASSERT (this->run != ASE_NULL); - return this->run; - } - - Awk::awk_t* Awk::Argument::getAwk () const - { - ASE_ASSERT (this->run != ASE_NULL); - return ase_awk_getrunawk (this->run); - } - ////////////////////////////////////////////////////////////////// // Awk::Return ////////////////////////////////////////////////////////////////// @@ -327,16 +305,6 @@ namespace ASE return ASE_NULL; } - Awk::run_t* Awk::Return::getRun () const - { - return this->run; - } - - Awk::awk_t* Awk::Return::getAwk () const - { - return ase_awk_getrunawk (this->run); - } - int Awk::Return::set (long_t v) { clear (); @@ -391,45 +359,107 @@ namespace ASE // Awk::Run ////////////////////////////////////////////////////////////////// - Awk::Run::Run (run_t* run): run (run) + Awk::Run::Run (Awk* awk): + awk (awk), run (ASE_NULL), callbackFailed (false) { } + int Awk::Run::stop () + { + 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 (E_NOERR), errlin (0) + errnum (E_NOERR), errlin (0), runCallback (false) { + this->errmsg[0] = ASE_T('\0'); } Awk::~Awk () { } - Awk::ErrorCode Awk::getErrorCode () + Awk::ErrorCode Awk::getErrorCode () const { return this->errnum; } - Awk::size_t Awk::getErrorLine () + Awk::size_t Awk::getErrorLine () const { return this->errlin; } - void Awk::setError (ErrorCode code, size_t line) + const Awk::char_t* Awk::getErrorMessage () const { - this->errnum = code; - this->errlin = line; + 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::clearError () { this->errnum = E_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 () @@ -481,6 +511,22 @@ namespace ASE return -1; } + int opt = + OPT_IMPLICIT | + OPT_EXPLICIT | + OPT_UNIQUEFN | + OPT_IDIV | + OPT_SHADING | + OPT_SHIFT | + OPT_EXTIO | + OPT_BLOCKLESS | + OPT_STRBASEONE | + OPT_STRIPSPACES | + OPT_NEXTOFILE | + OPT_ARGSTOMAIN; + ase_awk_setoption (awk, opt); + + runCallback = false; return 0; } @@ -499,6 +545,19 @@ namespace ASE } 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); } int Awk::parse () @@ -511,7 +570,9 @@ namespace ASE srcios.out = sourceWriter; srcios.custom_data = this; - return ase_awk_parse (awk, &srcios); + 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) @@ -522,6 +583,7 @@ namespace ASE 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; @@ -529,11 +591,14 @@ namespace ASE runios.console = consoleHandler; runios.custom_data = this; - runcbs.on_start = onRunStart; - runcbs.on_end = onRunEnd; - runcbs.on_return = ASE_NULL; - runcbs.on_statement = ASE_NULL; - runcbs.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) { @@ -542,7 +607,7 @@ namespace ASE if (runarg == ASE_NULL) { - // TODO: SET ERROR INFO + setError (E_NOMEM); return -1; } @@ -560,7 +625,7 @@ namespace ASE } } - // TODO: SET ERROR INFO + setError (E_NOMEM); return -1; } } @@ -570,7 +635,10 @@ namespace ASE } int n = ase_awk_run ( - awk, main, &runios, &runcbs, runarg, this); + awk, main, &runios, + (runCallback? &runcbs: ASE_NULL), + runarg, this); + if (n == -1) retrieveError (); if (runarg != ASE_NULL) { @@ -593,11 +661,7 @@ namespace ASE awk = ase_awk_getrunawk (run); pair = ase_awk_map_get (functionMap, name, len); - if (pair == ASE_NULL) - { - // TODO: SET ERROR INFO - return -1; - } + if (pair == ASE_NULL) return -1; FunctionHandler handler; handler = *(FunctionHandler*)ASE_AWK_PAIR_VAL(pair); @@ -607,11 +671,7 @@ namespace ASE //Argument* args = ASE_NULL; //try { args = new Argument [nargs]; } catch (...) {} Argument* args = new(awk) Argument[nargs]; - if (args == ASE_NULL) - { - // TODO: SET ERROR INFO - return -1; - } + if (args == ASE_NULL) return -1; for (i = 0; i < nargs; i++) { @@ -619,29 +679,19 @@ namespace ASE if (args[i].init (run, v) == -1) { delete[] args; - // TODO: SET ERROR INFO return -1; } } - Return ret (run); int n = (this->*handler) (&ret, args, nargs); delete[] args; - if (n <= -1) - { - // TODO: SET ERROR INFO - return -1; - } + if (n <= -1) return -1; val_t* r = ret.toVal (); - if (r == ASE_NULL) - { - // TODO: SET ERROR INFO - return -1; - } + if (r == ASE_NULL) return -1; ase_awk_setretval (run, r); return 0; @@ -657,7 +707,7 @@ namespace ASE ase_awk_malloc (awk, ASE_SIZEOF(handler)); if (tmp == ASE_NULL) { - // TODO: SET ERROR INFO -> ENOMEM + setError (E_NOMEM); return -1; } @@ -672,15 +722,17 @@ namespace ASE 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) { - // TODO: SET ERROR INFO ase_awk_delbfn (awk, name, nameLen); ase_awk_free (awk, tmp); + + setError (E_NOMEM); return -1; } @@ -695,15 +747,34 @@ namespace ASE 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, int errnum) + void Awk::onRunEnd (const Run& run) + { + } + + void Awk::onRunReturn (const Run& run, const Argument& ret) + { + } + + void Awk::onRunStatement (const Run& run, size_t line) { } @@ -853,14 +924,48 @@ namespace ASE void Awk::onRunStart (run_t* run, void* custom) { - Awk* awk = (Awk*)custom; - awk->onRunStart (Run(run)); + Run* r = (Run*)custom; + r->run = run; + + r->callbackFailed = false; + r->awk->onRunStart (*r); } void Awk::onRunEnd (run_t* run, int errnum, void* custom) { - Awk* awk = (Awk*)custom; - awk->onRunEnd (Run(run), errnum); + Run* r = (Run*)custom; + + if (errnum == E_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) diff --git a/ase/awk/Awk.hpp b/ase/awk/Awk.hpp index 10fec148..41b79588 100644 --- a/ase/awk/Awk.hpp +++ b/ase/awk/Awk.hpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.hpp,v 1.29 2007/05/17 15:02:54 bacon Exp $ + * $Id: Awk.hpp,v 1.33 2007/05/19 16:45:27 bacon Exp $ */ #ifndef _ASE_AWK_AWK_HPP_ @@ -60,9 +60,6 @@ namespace ASE const void* getHandle () const; void setHandle (void* handle); - run_t* getRun () const; - awk_t* getAwk () const; - protected: extio_t* extio; }; @@ -159,9 +156,6 @@ namespace ASE real_t toReal () const; const char_t* toStr (size_t* len) const; - run_t* getRun () const; - awk_t* getAwk () const; - protected: run_t* run; val_t* val; @@ -187,9 +181,6 @@ namespace ASE val_t* toVal () const; public: - run_t* getRun () const; - awk_t* getAwk () const; - int set (long_t v); int set (real_t v); int set (char_t* ptr, size_t len); @@ -212,36 +203,199 @@ namespace ASE } v; }; + // generated by generrcode.awk + enum ErrorCode + { + E_NOERR = ASE_AWK_ENOERR, + E_INVAL = ASE_AWK_EINVAL, + E_NOMEM = ASE_AWK_ENOMEM, + E_NOSUP = ASE_AWK_ENOSUP, + E_NOPER = ASE_AWK_ENOPER, + E_NODEV = ASE_AWK_ENODEV, + E_NOSPC = ASE_AWK_ENOSPC, + E_MFILE = ASE_AWK_EMFILE, + E_MLINK = ASE_AWK_EMLINK, + E_AGAIN = ASE_AWK_EAGAIN, + E_NOENT = ASE_AWK_ENOENT, + E_EXIST = ASE_AWK_EEXIST, + E_FTBIG = ASE_AWK_EFTBIG, + E_TBUSY = ASE_AWK_ETBUSY, + E_ISDIR = ASE_AWK_EISDIR, + E_IOERR = ASE_AWK_EIOERR, + E_OPEN = ASE_AWK_EOPEN, + E_READ = ASE_AWK_EREAD, + E_WRITE = ASE_AWK_EWRITE, + E_CLOSE = ASE_AWK_ECLOSE, + E_INTERN = ASE_AWK_EINTERN, + E_RUNTIME = ASE_AWK_ERUNTIME, + E_BLKNST = ASE_AWK_EBLKNST, + E_EXPRNST = ASE_AWK_EEXPRNST, + E_SINOP = ASE_AWK_ESINOP, + E_SINCL = ASE_AWK_ESINCL, + E_SINRD = ASE_AWK_ESINRD, + E_SOUTOP = ASE_AWK_ESOUTOP, + E_SOUTCL = ASE_AWK_ESOUTCL, + E_SOUTWR = ASE_AWK_ESOUTWR, + E_LXCHR = ASE_AWK_ELXCHR, + E_LXUNG = ASE_AWK_ELXUNG, + E_ENDSRC = ASE_AWK_EENDSRC, + E_ENDCMT = ASE_AWK_EENDCMT, + E_ENDSTR = ASE_AWK_EENDSTR, + E_ENDREX = ASE_AWK_EENDREX, + E_LBRACE = ASE_AWK_ELBRACE, + E_LPAREN = ASE_AWK_ELPAREN, + E_RPAREN = ASE_AWK_ERPAREN, + E_RBRACK = ASE_AWK_ERBRACK, + E_COMMA = ASE_AWK_ECOMMA, + E_SCOLON = ASE_AWK_ESCOLON, + E_COLON = ASE_AWK_ECOLON, + E_STMEND = ASE_AWK_ESTMEND, + E_IN = ASE_AWK_EIN, + E_NOTVAR = ASE_AWK_ENOTVAR, + E_EXPRES = ASE_AWK_EEXPRES, + E_WHILE = ASE_AWK_EWHILE, + E_ASSIGN = ASE_AWK_EASSIGN, + E_IDENT = ASE_AWK_EIDENT, + E_FNNAME = ASE_AWK_EFNNAME, + E_BLKBEG = ASE_AWK_EBLKBEG, + E_BLKEND = ASE_AWK_EBLKEND, + E_DUPBEG = ASE_AWK_EDUPBEG, + E_DUPEND = ASE_AWK_EDUPEND, + E_BFNRED = ASE_AWK_EBFNRED, + E_AFNRED = ASE_AWK_EAFNRED, + E_GBLRED = ASE_AWK_EGBLRED, + E_PARRED = ASE_AWK_EPARRED, + E_DUPPAR = ASE_AWK_EDUPPAR, + E_DUPGBL = ASE_AWK_EDUPGBL, + E_DUPLCL = ASE_AWK_EDUPLCL, + E_BADPAR = ASE_AWK_EBADPAR, + E_BADVAR = ASE_AWK_EBADVAR, + E_UNDEF = ASE_AWK_EUNDEF, + E_LVALUE = ASE_AWK_ELVALUE, + E_GBLTM = ASE_AWK_EGBLTM, + E_LCLTM = ASE_AWK_ELCLTM, + E_PARTM = ASE_AWK_EPARTM, + E_DELETE = ASE_AWK_EDELETE, + E_BREAK = ASE_AWK_EBREAK, + E_CONTINUE = ASE_AWK_ECONTINUE, + E_NEXTBEG = ASE_AWK_ENEXTBEG, + E_NEXTEND = ASE_AWK_ENEXTEND, + E_NEXTFBEG = ASE_AWK_ENEXTFBEG, + E_NEXTFEND = ASE_AWK_ENEXTFEND, + E_PRINTFARG = ASE_AWK_EPRINTFARG, + E_PREPST = ASE_AWK_EPREPST, + E_GLNCPS = ASE_AWK_EGLNCPS, + E_DIVBY0 = ASE_AWK_EDIVBY0, + E_OPERAND = ASE_AWK_EOPERAND, + E_POSIDX = ASE_AWK_EPOSIDX, + E_ARGTF = ASE_AWK_EARGTF, + E_ARGTM = ASE_AWK_EARGTM, + E_FNNONE = ASE_AWK_EFNNONE, + E_NOTIDX = ASE_AWK_ENOTIDX, + E_NOTDEL = ASE_AWK_ENOTDEL, + E_NOTMAP = ASE_AWK_ENOTMAP, + E_NOTMAPIN = ASE_AWK_ENOTMAPIN, + E_NOTMAPNILIN = ASE_AWK_ENOTMAPNILIN, + E_NOTREF = ASE_AWK_ENOTREF, + E_NOTASS = ASE_AWK_ENOTASS, + E_IDXVALASSMAP = ASE_AWK_EIDXVALASSMAP, + E_POSVALASSMAP = ASE_AWK_EPOSVALASSMAP, + E_MAPTOSCALAR = ASE_AWK_EMAPTOSCALAR, + E_SCALARTOMAP = ASE_AWK_ESCALARTOMAP, + E_MAPNOTALLOWED = ASE_AWK_EMAPNOTALLOWED, + E_VALTYPE = ASE_AWK_EVALTYPE, + E_RDELETE = ASE_AWK_ERDELETE, + E_RNEXTBEG = ASE_AWK_ERNEXTBEG, + E_RNEXTEND = ASE_AWK_ERNEXTEND, + E_RNEXTFBEG = ASE_AWK_ERNEXTFBEG, + E_RNEXTFEND = ASE_AWK_ERNEXTFEND, + E_BFNUSER = ASE_AWK_EBFNUSER, + E_BFNIMPL = ASE_AWK_EBFNIMPL, + E_IOUSER = ASE_AWK_EIOUSER, + E_IONONE = ASE_AWK_EIONONE, + E_IOIMPL = ASE_AWK_EIOIMPL, + E_IONMEM = ASE_AWK_EIONMEM, + E_IONMNL = ASE_AWK_EIONMNL, + E_FMTARG = ASE_AWK_EFMTARG, + E_FMTCNV = ASE_AWK_EFMTCNV, + E_CONVFMTCHR = ASE_AWK_ECONVFMTCHR, + E_OFMTCHR = ASE_AWK_EOFMTCHR, + E_REXRECUR = ASE_AWK_EREXRECUR, + E_REXRPAREN = ASE_AWK_EREXRPAREN, + E_REXRBRACKET = ASE_AWK_EREXRBRACKET, + E_REXRBRACE = ASE_AWK_EREXRBRACE, + E_REXUNBALPAR = ASE_AWK_EREXUNBALPAR, + E_REXCOLON = ASE_AWK_EREXCOLON, + E_REXCRANGE = ASE_AWK_EREXCRANGE, + E_REXCCLASS = ASE_AWK_EREXCCLASS, + E_REXBRANGE = ASE_AWK_EREXBRANGE, + E_REXEND = ASE_AWK_EREXEND, + E_REXGARBAGE = ASE_AWK_EREXGARBAGE + }; + // end of enum ErrorCode + + // 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_STRBASEONE = ASE_AWK_STRBASEONE, + OPT_STRIPSPACES = ASE_AWK_STRIPSPACES, + OPT_NEXTOFILE = ASE_AWK_NEXTOFILE, + OPT_CRLF = ASE_AWK_CRLF, + OPT_ARGSTOMAIN = ASE_AWK_ARGSTOMAIN + }; + // end of enum Option + class Run { protected: friend class Awk; - Run (run_t* run); + + Run (Awk* awk); + + public: + int stop (); + + ErrorCode getErrorCode () const; + size_t getErrorLine () const; + const char_t* getErrorMessage () const; protected: + Awk* awk; run_t* run; + bool callbackFailed; }; - enum ErrorCode - { - E_NOERR = ASE_AWK_ENOERR, - E_NOMEM = ASE_AWK_ENOMEM - }; - + Awk (); virtual ~Awk (); - ErrorCode getErrorCode (); - size_t getErrorLine (); + ErrorCode getErrorCode () const; + size_t getErrorLine () const ; + const char_t* getErrorMessage () const; protected: - void setError (ErrorCode code, size_t line = 0); + void setError (ErrorCode code, size_t line = 0, + const char_t* arg = ASE_NULL, size_t len = 0); void clearError (); + void retrieveError (); public: virtual int open (); virtual void close (); + virtual void setOption (int opt); + virtual int getOption () const; + virtual int parse (); virtual int run (const char_t* main = ASE_NULL, const char_t** args = ASE_NULL, size_t nargs = 0); @@ -254,8 +408,10 @@ namespace ASE FunctionHandler handler); virtual int deleteFunction (const char_t* main); - protected: + virtual void enableRunCallback (); + virtual void disableRunCallback (); + protected: virtual int dispatchFunction ( run_t* run, const char_t* name, size_t len); @@ -289,7 +445,9 @@ namespace ASE // run-time callbacks virtual void onRunStart (const Run& run); - virtual void onRunEnd (const Run& run, int errnum); + 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; @@ -334,6 +492,8 @@ namespace ASE 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); @@ -366,7 +526,10 @@ namespace ASE Source sourceOut; ErrorCode errnum; - size_t errlin; + size_t errlin; + char_t errmsg[256]; + + bool runCallback; private: Awk (const Awk&); diff --git a/ase/awk/StdAwk.cpp b/ase/awk/StdAwk.cpp index 4d163b6a..b17902bc 100644 --- a/ase/awk/StdAwk.cpp +++ b/ase/awk/StdAwk.cpp @@ -1,5 +1,5 @@ /* - * $Id: StdAwk.cpp,v 1.15 2007/05/18 15:36:02 bacon Exp $ + * $Id: StdAwk.cpp,v 1.17 2007/05/19 16:45:27 bacon Exp $ */ #include @@ -37,22 +37,6 @@ namespace ASE int n = Awk::open (); if (n == -1) return n; - int opt = - ASE_AWK_IMPLICIT | - ASE_AWK_EXPLICIT | - ASE_AWK_UNIQUEFN | - ASE_AWK_IDIV | - ASE_AWK_SHADING | - ASE_AWK_SHIFT | - ASE_AWK_EXTIO | - ASE_AWK_BLOCKLESS | - ASE_AWK_STRBASEONE | - ASE_AWK_STRIPSPACES | - ASE_AWK_NEXTOFILE | - ASE_AWK_ARGSTOMAIN; - ase_awk_setoption (awk, opt); - - 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); @@ -236,7 +220,7 @@ namespace ASE FILE* fp = (FILE*)io.getHandle(); ssize_t n = 0; - while (n < len) + while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); if (c == ASE_CHAR_EOF) break; @@ -246,17 +230,7 @@ namespace ASE } return n; - /* - if (ase_fgets (buf, len, fp) == ASE_NULL) - { - if (ferror(fp)) return -1; - return 0; - } - - return ase_strlen(buf); - */ } - StdAwk::ssize_t StdAwk::writePipe (Pipe& io, char_t* buf, size_t len) { @@ -298,7 +272,7 @@ namespace ASE int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf); #endif - if (n < 0 || n > left) return -1; + if (n < 0 || n > (ssize_t)left) return -1; left -= n; buf += n; } } @@ -347,7 +321,7 @@ namespace ASE FILE* fp = (FILE*)io.getHandle(); ssize_t n = 0; - while (n < len) + while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); if (c == ASE_CHAR_EOF) break; @@ -357,16 +331,6 @@ namespace ASE } return n; - - /* - if (ase_fgets (buf, len, fp) == ASE_NULL) - { - if (ferror(fp)) return -1; - return 0; - } - - return ase_strlen(buf); - */ } StdAwk::ssize_t StdAwk::writeFile (File& io, char_t* buf, size_t len) @@ -384,7 +348,7 @@ namespace ASE else { int n = ase_fprintf (fp, ASE_T("%.*s"), left, buf); - if (n < 0 || n > left) return -1; + if (n < 0 || n > (ssize_t)left) return -1; left -= n; buf += n; } } diff --git a/ase/awk/awk.h b/ase/awk/awk.h index cc19f80e..6dca0551 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.4 2007/05/09 16:07:44 bacon Exp $ + * $Id: awk.h,v 1.5 2007/05/19 12:13:41 bacon Exp $ * * {License} */ @@ -117,7 +117,7 @@ struct ase_awk_runarg_t }; /* io function commands */ -enum +enum ase_awk_iocmd_t { ASE_AWK_IO_OPEN = 0, ASE_AWK_IO_CLOSE = 1, @@ -128,7 +128,7 @@ enum }; /* various options */ -enum +enum ase_awk_option_t { /* allow undeclared variables and implicit concatenation */ ASE_AWK_IMPLICIT = (1 << 0), @@ -194,7 +194,7 @@ enum }; /* error code */ -enum +enum ase_awk_errnum_t { ASE_AWK_ENOERR, /* no error */ diff --git a/ase/awk/awk.txt b/ase/awk/awk.txt index 6e2ea48a..44474c37 100644 --- a/ase/awk/awk.txt +++ b/ase/awk/awk.txt @@ -32,12 +32,12 @@ Actions Variables -global variables (enabled when awk->opt & XP_AWK_OPT_VARDCL) +global variables (enabled when awk->opt & ASE_AWK_OPT_VARDCL) global x; global x, y; -local variables (enabled when awk->opt & XP_AWK_OPT_VARDCL) +local variables (enabled when awk->opt & ASE_AWK_OPT_VARDCL) local x; local x, y; @@ -47,12 +47,12 @@ function arguments (enabled always) function funca (x, y) -local variables in function declaration (enabled when awk->opt & XP_AWK_OPT_FUNCLOCAL) +local variables in function declaration (enabled when awk->opt & ASE_AWK_OPT_FUNCLOCAL) function funca (x, y, v1, v2) -variables without any declarations (enabled when awk->opt & XP_AWK_OPT_NAMEDVAR) +variables without any declarations (enabled when awk->opt & ASE_AWK_OPT_NAMEDVAR) x = 10; // x is put into the global hash table. diff --git a/ase/awk/err.c b/ase/awk/err.c index 91df8c14..0bfcf7c3 100644 --- a/ase/awk/err.c +++ b/ase/awk/err.c @@ -1,5 +1,5 @@ /* - * $Id: err.c,v 1.3 2007/04/30 05:47:33 bacon Exp $ + * $Id: err.c,v 1.4 2007/05/19 12:13:41 bacon Exp $ * * {License} */ @@ -175,7 +175,6 @@ int ase_awk_seterrstr (ase_awk_t* awk, int num, const ase_char_t* str) if (awk->errstr[num] != ASE_NULL) ASE_AWK_FREE (awk, awk->errstr[num]); - else awk->errstr[num] = dup; return 0; } diff --git a/ase/awk/generrcode.awk b/ase/awk/generrcode.awk new file mode 100644 index 00000000..75dbc51d --- /dev/null +++ b/ase/awk/generrcode.awk @@ -0,0 +1,33 @@ +# +# generrcode.awk +# +# aseawk -f generror.awk awk.h +# + +BEGIN { + collect=0; + tab3="\t\t"; + tab4="\t\t\t"; +} + +/^[[:space:]]*enum[[:space:]]+ase_awk_errnum_t[[:space:]]*$/ { + collect=1; + print tab3 "// generated by generrcode.awk"; + print tab3 "enum ErrorCode"; + print tab3 "{"; +} + +collect && /^[[:space:]]*};[[:space:]]*$/ { + print tab3 "};"; + print tab3 "// end of enum ErrorCode"; + print ""; + collect=0; +} + +collect && /^[[:space:]]*ASE_AWK_E[[:alnum:]]+/ { + split ($1, flds, ","); + name=flds[1]; + + print tab4 "E_" substr (name,10,length(name)-9) " = " name ","; +} + diff --git a/ase/awk/genoptcode.awk b/ase/awk/genoptcode.awk new file mode 100644 index 00000000..0c1149bc --- /dev/null +++ b/ase/awk/genoptcode.awk @@ -0,0 +1,33 @@ +# +# genoptcode.awk +# +# aseawk -f generror.awk awk.h +# + +BEGIN { + collect=0; + tab3="\t\t"; + tab4="\t\t\t"; +} + +/^[[:space:]]*enum[[:space:]]+ase_awk_option_t[[:space:]]*$/ { + collect=1; + print tab3 "// generated by genoptcode.awk"; + print tab3 "enum Option"; + print tab3 "{"; +} + +collect && /^[[:space:]]*};[[:space:]]*$/ { + print tab3 "};"; + print tab3 "// end of enum Option"; + print ""; + collect=0; +} + +collect && /^[[:space:]]*ASE_AWK_[[:alnum:]]+/ { + split ($1, flds, ","); + name=flds[1]; + + print tab4 "OPT_" substr (name,9,length(name)-8) " = " name ","; +} + diff --git a/ase/awk/run.c b/ase/awk/run.c index 4979121b..130680fb 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.5 2007/05/13 14:43:58 bacon Exp $ + * $Id: run.c,v 1.7 2007/05/19 16:45:27 bacon Exp $ * * {License} */ @@ -662,6 +662,12 @@ int ase_awk_run (ase_awk_t* awk, /* the run loop ended. execute the end callback if it exists */ if (runcbs != ASE_NULL && runcbs->on_end != ASE_NULL) { + if (n == 0) + { + /* clear error if run is successful just in case */ + ase_awk_setrunerror (run, ASE_AWK_ENOERR, 0, ASE_NULL, 0); + } + runcbs->on_end (run, ((n == -1)? run->errnum: ASE_AWK_ENOERR), runcbs->custom_data); diff --git a/ase/test/awk/Awk.cpp b/ase/test/awk/Awk.cpp index 3429c173..b0ccd0e5 100644 --- a/ase/test/awk/Awk.cpp +++ b/ase/test/awk/Awk.cpp @@ -1,5 +1,5 @@ /* - * $Id: Awk.cpp,v 1.15 2007/05/16 14:44:13 bacon Exp $ + * $Id: Awk.cpp,v 1.17 2007/05/19 16:45:27 bacon Exp $ */ #include @@ -87,12 +87,27 @@ protected: void onRunStart (const Run& run) { - wprintf (L"*** awk run started ***\n"); + ase_printf (ASE_T("*** awk run started ***\n")); } - void onRunEnd (const Run& run, int errnum) + void onRunEnd (const Run& run) { - wprintf (L"*** awk run ended ***\n"); + ErrorCode err = run.getErrorCode(); + + if (err != E_NOERR) + { + ase_fprintf (stderr, ASE_T("cannot run: LINE[%d] %s\n"), + run.getErrorLine(), run.getErrorMessage()); + } + + ase_printf (ASE_T("*** awk run ended ***\n")); + } + + void onRunReturn (const Run& run, const Argument& ret) + { + size_t len; + const char_t* ptr = ret.toStr (&len); + ase_printf (ASE_T("*** return [%.*s] ***\n"), len, ptr); } int openSource (Source& io) @@ -142,7 +157,7 @@ protected: FILE* fp = (FILE*)io.getHandle(); ssize_t n = 0; - while (n < len) + while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); if (c == ASE_CHAR_EOF) break; @@ -248,7 +263,7 @@ protected: FILE* fp = t->handle; ssize_t n = 0; - while (n < len) + while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); if (c == ASE_CHAR_EOF) break; @@ -546,13 +561,17 @@ int awk_main (int argc, ase_char_t* argv[]) if (awk.parse (srcin, srcout) == -1) { - ase_fprintf (stderr, ASE_T("cannot parse\n")); + ase_fprintf (stderr, ASE_T("cannot parse: LINE[%d] %s\n"), + awk.getErrorLine(), awk.getErrorMessage()); return -1; } + awk.enableRunCallback (); + if (awk.run (mainfn, args, nargs) == -1) { - ase_fprintf (stderr, ASE_T("cannot run\n")); + ase_fprintf (stderr, ASE_T("cannot run: LINE[%d] %s\n"), + awk.getErrorLine(), awk.getErrorMessage()); return -1; }