added the Sed and SedStd classes
This commit is contained in:
parent
4eca91258c
commit
3930bfc8f8
@ -712,7 +712,7 @@ The following sample illustrates the basic steps hightlighed above.
|
||||
|
||||
If you prefer C++, you may use the Hawk/HawkStd wrapper classes to simplify the task. The C++ classes are inferior to the C equivalents in that they don't allow creation of multiple runtime contexts over a single hawk instance. Here is the sample code that prints "hello, world".
|
||||
|
||||
#include <HawkStd.hpp>
|
||||
#include <Hawk.hpp>
|
||||
#include <stdio.h>
|
||||
|
||||
int main ()
|
||||
|
377
hawk/lib/Hawk-Sed.hpp
Normal file
377
hawk/lib/Hawk-Sed.hpp
Normal file
@ -0,0 +1,377 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _HAWK_SED_HPP_
|
||||
#define _HAWK_SED_HPP_
|
||||
|
||||
#include <hawk-sed.h>
|
||||
#include <Hawk.hpp>
|
||||
|
||||
/// \file
|
||||
/// This file defines C++ classes that you can use when you create a stream
|
||||
/// editor. The C++ classes encapsulates the C data types and functions in
|
||||
/// a more object-oriented manner.
|
||||
///
|
||||
/// \todo support sed tracer
|
||||
///
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_BEGIN_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
||||
|
||||
///
|
||||
/// The Sed class implements a stream editor by wrapping around #hawk_hawk_sed_t.
|
||||
///
|
||||
class HAWK_EXPORT Sed: public Uncopyable, public Mmged
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// The Stream class is a abstract class for I/O operation during
|
||||
/// execution.
|
||||
///
|
||||
class HAWK_EXPORT Stream
|
||||
{
|
||||
public:
|
||||
/// The Mode type defines I/O modes.
|
||||
enum Mode
|
||||
{
|
||||
READ, ///< open for read
|
||||
WRITE ///< open for write
|
||||
};
|
||||
|
||||
/// The Data class conveys information need for I/O operations.
|
||||
class HAWK_EXPORT Data
|
||||
{
|
||||
public:
|
||||
friend class Sed;
|
||||
|
||||
protected:
|
||||
Data (Sed* sed, Mode mode, hawk_sed_io_arg_t* arg):
|
||||
sed (sed), mode (mode), arg (arg) {}
|
||||
|
||||
public:
|
||||
/// The getMode() function returns the I/O mode
|
||||
/// requested.
|
||||
Mode getMode() const { return this->mode; }
|
||||
|
||||
/// The getHandle() function returns the I/O handle
|
||||
/// saved by setHandle().
|
||||
void* getHandle () const { return this->arg->handle; }
|
||||
|
||||
/// The setHandle() function sets an I/O handle
|
||||
/// typically in the Stream::open() function.
|
||||
void setHandle (void* handle) { this->arg->handle = handle; }
|
||||
|
||||
/// The getName() function returns an I/O name.
|
||||
/// \return #HAWK_NULL for the main data stream,
|
||||
/// file path for explicit file stream
|
||||
const hawk_ooch_t* getName () const { return this->arg->path; }
|
||||
|
||||
/// The Sed* operator returns the associated Sed class.
|
||||
operator Sed* () const { return this->sed; }
|
||||
|
||||
/// The hawk_sed_t* operator returns a pointer to the
|
||||
/// underlying stream editor.
|
||||
operator hawk_sed_t* () const { return this->sed->getHandle(); }
|
||||
|
||||
operator hawk_gem_t* () const { return hawk_sed_getgem(this->sed->getHandle()); }
|
||||
|
||||
protected:
|
||||
Sed* sed;
|
||||
Mode mode;
|
||||
hawk_sed_io_arg_t* arg;
|
||||
};
|
||||
|
||||
/// The Stream() function constructs a stream.
|
||||
Stream () {}
|
||||
|
||||
/// The Stream() function destructs a stream.
|
||||
virtual ~Stream () {}
|
||||
|
||||
/// The open() function should be implemented by a subclass
|
||||
/// to open a stream. It can get the mode requested by calling
|
||||
/// the Data::getMode() function over the I/O parameter \a io.
|
||||
///
|
||||
/// The return value of 0 may look a bit tricky. Easygoers
|
||||
/// can just return 1 on success and never return 0 from open().
|
||||
/// - If 0 is returned for the #READ mode, Sed::execute()
|
||||
/// returns success after having called close() as it has
|
||||
/// opened a console but has reached EOF.
|
||||
/// - If 0 is returned for the #WRITE mode and there are
|
||||
/// any write() calls, the Sed::execute() function returns
|
||||
/// failure after having called close() as it cannot write
|
||||
/// further on EOF.
|
||||
///
|
||||
/// \return -1 on failure, 1 on success,
|
||||
/// 0 on success but reached EOF.
|
||||
virtual int open (Data& io) = 0;
|
||||
|
||||
/// The close() function should be implemented by a subclass
|
||||
/// to open a stream.
|
||||
virtual int close (Data& io) = 0;
|
||||
|
||||
virtual hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len) = 0;
|
||||
virtual hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len) = 0;
|
||||
|
||||
private:
|
||||
Stream (const Stream&);
|
||||
Stream& operator= (const Stream&);
|
||||
};
|
||||
|
||||
///
|
||||
/// The Sed() function creates an uninitialized stream editor.
|
||||
///
|
||||
Sed (Mmgr* mmgr);
|
||||
|
||||
///
|
||||
/// The ~Sed() function destroys a stream editor.
|
||||
/// \note The close() function is not called by this destructor.
|
||||
/// To avoid resource leaks, You should call close() before
|
||||
/// a stream editor is destroyed if it has been initialized
|
||||
/// with open().
|
||||
///
|
||||
virtual ~Sed () {}
|
||||
|
||||
|
||||
hawk_cmgr_t* getCmgr () const;
|
||||
void setCmgr (hawk_cmgr_t* cmgr);
|
||||
|
||||
///
|
||||
/// The open() function initializes a stream editor and makes it
|
||||
/// ready for subsequent use.
|
||||
/// \return 0 on success, -1 on failure.
|
||||
///
|
||||
int open ();
|
||||
|
||||
///
|
||||
/// The close() function finalizes a stream editor.
|
||||
///
|
||||
void close ();
|
||||
|
||||
///
|
||||
/// The compile() function compiles a script from a stream
|
||||
/// \a iostream.
|
||||
/// \return 0 on success, -1 on failure
|
||||
///
|
||||
int compile (Stream& sstream);
|
||||
|
||||
///
|
||||
/// The execute() function executes compiled commands over the I/O
|
||||
/// streams defined through I/O handlers
|
||||
/// \return 0 on success, -1 on failure
|
||||
///
|
||||
int execute (Stream& iostream);
|
||||
|
||||
///
|
||||
/// The halt() function makes a request to break a running loop
|
||||
/// inside execute(). Note that this does not affect blocking
|
||||
/// operations in user-defined stream handlers.
|
||||
///
|
||||
void halt ();
|
||||
|
||||
///
|
||||
/// The isHalt() function returns true if halt() has been called
|
||||
/// since the last call to execute(), false otherwise.
|
||||
///
|
||||
bool isHalt () const;
|
||||
|
||||
///
|
||||
/// The getTrait() function gets the current traits.
|
||||
/// \return 0 or current options ORed of #trait_t enumerators.
|
||||
///
|
||||
int getTrait () const;
|
||||
|
||||
///
|
||||
/// The setTrait() function sets traits for a stream editor.
|
||||
/// The option code \a opt is 0 or OR'ed of #trait_t enumerators.
|
||||
///
|
||||
void setTrait (
|
||||
int trait ///< option code
|
||||
);
|
||||
|
||||
///
|
||||
/// The getErrorMessage() function gets the description of the last
|
||||
/// error occurred. It returns an empty string if the stream editor
|
||||
/// has not been initialized with the open() function.
|
||||
///
|
||||
const hawk_ooch_t* getErrorMessage() const;
|
||||
const hawk_uch_t* getErrorMessageU () const;
|
||||
const hawk_bch_t* getErrorMessageB () const;
|
||||
|
||||
///
|
||||
/// The getErrorLocation() function gets the location where
|
||||
/// the last error occurred. The line and the column of the #hawk_loc_t
|
||||
/// structure retruend are 0 if the stream editor has not been
|
||||
/// initialized with the open() function.
|
||||
///
|
||||
hawk_loc_t getErrorLocation () const;
|
||||
|
||||
///
|
||||
/// The getErrorNumber() function gets the number of the last
|
||||
/// error occurred. It returns HAWK_SED_ENOERR if the stream editor
|
||||
/// has not been initialized with the open() function.
|
||||
///
|
||||
hawk_errnum_t getErrorNumber () const;
|
||||
|
||||
///
|
||||
/// The setError() function sets information on an error occurred.
|
||||
///
|
||||
void setError (
|
||||
hawk_errnum_t num, ///< error number
|
||||
const hawk_oocs_t* args = HAWK_NULL, ///< string array for formatting
|
||||
/// an error message
|
||||
const hawk_loc_t* loc = HAWK_NULL ///< error location
|
||||
);
|
||||
|
||||
const hawk_ooch_t* getCompileId () const;
|
||||
|
||||
const hawk_ooch_t* setCompileId (
|
||||
const hawk_ooch_t* id
|
||||
);
|
||||
|
||||
///
|
||||
/// The getConsoleLine() function returns the current line
|
||||
/// number from an input console.
|
||||
/// \return current line number
|
||||
///
|
||||
hawk_oow_t getConsoleLine ();
|
||||
|
||||
///
|
||||
/// The setConsoleLine() function changes the current line
|
||||
/// number from an input console.
|
||||
///
|
||||
void setConsoleLine (
|
||||
hawk_oow_t num ///< a line number
|
||||
);
|
||||
|
||||
protected:
|
||||
///
|
||||
/// The getErrorString() function returns an error formatting string
|
||||
/// for the error number \a num. A subclass wishing to customize
|
||||
/// an error formatting string may override this function.
|
||||
///
|
||||
virtual const hawk_ooch_t* getErrorString (
|
||||
hawk_errnum_t num ///< an error number
|
||||
) const;
|
||||
|
||||
public:
|
||||
// use this with care
|
||||
hawk_sed_t* getHandle() const { return this->sed; }
|
||||
|
||||
protected:
|
||||
hawk_cmgr_t* _cmgr;
|
||||
/// handle to a primitive sed object
|
||||
hawk_sed_t* sed;
|
||||
/// default error formatting string getter
|
||||
hawk_errstr_t dflerrstr;
|
||||
/// Stream to read script from
|
||||
Stream* sstream;
|
||||
/// I/O stream to read data from and write output to.
|
||||
Stream* iostream;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
static hawk_ooi_t sin (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
static hawk_ooi_t xin (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
static hawk_ooi_t xout (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* dat, hawk_oow_t len);
|
||||
static const hawk_ooch_t* xerrstr (hawk_sed_t* s, hawk_errnum_t num);
|
||||
};
|
||||
|
||||
|
||||
///
|
||||
/// The StdSed class inherits the Sed class, implements a standard
|
||||
/// I/O stream class, and sets the default memory manager.
|
||||
///
|
||||
class HAWK_EXPORT SedStd: public Sed
|
||||
{
|
||||
public:
|
||||
SedStd (Mmgr* mmgr = MmgrStd::getDFL()): Sed(mmgr) {}
|
||||
|
||||
///
|
||||
/// The FileStream class implements a stream over input
|
||||
/// and output files.
|
||||
///
|
||||
class HAWK_EXPORT FileStream: public Stream
|
||||
{
|
||||
public:
|
||||
FileStream (const hawk_ooch_t* infile = HAWK_NULL,
|
||||
const hawk_ooch_t* outfile = HAWK_NULL,
|
||||
hawk_cmgr_t* cmgr = HAWK_NULL):
|
||||
infile(infile), outfile(outfile), cmgr(cmgr)
|
||||
{
|
||||
}
|
||||
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
|
||||
protected:
|
||||
const hawk_ooch_t* infile;
|
||||
const hawk_ooch_t* outfile;
|
||||
hawk_cmgr_t* cmgr;
|
||||
};
|
||||
|
||||
///
|
||||
/// The StringStream class implements a stream over a string
|
||||
///
|
||||
class HAWK_EXPORT StringStream: public Stream
|
||||
{
|
||||
public:
|
||||
StringStream (const hawk_ooch_t* in);
|
||||
StringStream (const hawk_ooch_t* in, hawk_oow_t len);
|
||||
~StringStream ();
|
||||
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
|
||||
const hawk_ooch_t* getInput (hawk_oow_t* len = HAWK_NULL) const;
|
||||
const hawk_ooch_t* getOutput (hawk_oow_t* len = HAWK_NULL) const;
|
||||
|
||||
protected:
|
||||
struct
|
||||
{
|
||||
const hawk_ooch_t* ptr;
|
||||
const hawk_ooch_t* end;
|
||||
const hawk_ooch_t* cur;
|
||||
} in;
|
||||
|
||||
struct
|
||||
{
|
||||
bool inited;
|
||||
hawk_ooecs_t buf;
|
||||
} out;
|
||||
};
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_END_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
@ -25,7 +25,6 @@
|
||||
*/
|
||||
|
||||
#include <Hawk.hpp>
|
||||
#include <HawkStd.hpp> // for MmgrStd only. i don't like this being mutually dependent
|
||||
#include "hawk-prv.h"
|
||||
|
||||
/////////////////////////////////
|
||||
|
@ -410,7 +410,7 @@ protected:
|
||||
/// const hawk_ooch_t* MyHawk::getErrorString (hawk_errnum_t num) const
|
||||
/// {
|
||||
/// if (num == HAWK_ENOENT) return HAWK_T("cannot find '${0}'");
|
||||
/// return Hawk::getErrorString (num);
|
||||
/// return Hawk::getErrorString(num);
|
||||
/// }
|
||||
/// \endcode
|
||||
///
|
||||
@ -1819,6 +1819,238 @@ private:
|
||||
static const hawk_ooch_t* xerrstr (hawk_t* a, hawk_errnum_t num);
|
||||
};
|
||||
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
class HAWK_EXPORT MmgrStd: public Mmgr
|
||||
{
|
||||
public:
|
||||
MmgrStd () HAWK_CXX_NOEXCEPT: Mmgr () {}
|
||||
|
||||
void* allocMem (hawk_oow_t n) HAWK_CXX_NOEXCEPT;
|
||||
void* reallocMem (void* ptr, hawk_oow_t n) HAWK_CXX_NOEXCEPT;
|
||||
void freeMem (void* ptr) HAWK_CXX_NOEXCEPT;
|
||||
|
||||
#if 0
|
||||
/// The getInstance() function returns the stock instance of the MmgrStd class.
|
||||
static MmgrStd* getInstance () HAWK_CXX_NOEXCEPT;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------------------
|
||||
|
||||
///
|
||||
/// The HawkStd class provides an easier-to-use interface by overriding
|
||||
/// primitive methods, and implementing the file handler, the pipe handler,
|
||||
/// and common intrinsic functions.
|
||||
///
|
||||
class HAWK_EXPORT HawkStd: public Hawk
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// The SourceFile class implements script I/O from and to a file.
|
||||
///
|
||||
class HAWK_EXPORT SourceFile: public Source
|
||||
{
|
||||
public:
|
||||
SourceFile (const hawk_uch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_UCH), _name(name), cmgr(cmgr)
|
||||
{
|
||||
dir.ptr = HAWK_NULL; dir.len = 0;
|
||||
}
|
||||
|
||||
SourceFile (const hawk_bch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_BCH), _name(name), cmgr(cmgr)
|
||||
{
|
||||
dir.ptr = HAWK_NULL; dir.len = 0;
|
||||
}
|
||||
|
||||
~SourceFile () {};
|
||||
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
|
||||
protected:
|
||||
enum
|
||||
{
|
||||
NAME_UCH,
|
||||
NAME_BCH
|
||||
} _type;
|
||||
const void* _name;
|
||||
hawk_oocs_t dir;
|
||||
hawk_cmgr_t* cmgr;
|
||||
};
|
||||
|
||||
///
|
||||
/// The SourceString class implements script input from a string.
|
||||
/// Deparsing is not supported.
|
||||
///
|
||||
class HAWK_EXPORT SourceString: public Source
|
||||
{
|
||||
public:
|
||||
SourceString (const hawk_uch_t* str, hawk_cmgr_t* cmgr = HAWK_NULL): _type(STR_UCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL), cmgr(cmgr) {}
|
||||
SourceString (const hawk_bch_t* str, hawk_cmgr_t* cmgr = HAWK_NULL): _type(STR_BCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL), cmgr(cmgr) {}
|
||||
~SourceString ();
|
||||
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
|
||||
protected:
|
||||
enum
|
||||
{
|
||||
STR_UCH,
|
||||
STR_BCH
|
||||
} _type;
|
||||
const void* _str;
|
||||
hawk_t* _hawk;
|
||||
|
||||
hawk_ooch_t* str;
|
||||
const hawk_ooch_t* ptr;
|
||||
hawk_cmgr_t* cmgr; // for reading included files
|
||||
};
|
||||
|
||||
HawkStd (Mmgr* mmgr = HAWK_NULL): Hawk(mmgr), cmgrtab_inited(false), stdmod_up(false), console_cmgr(HAWK_NULL)
|
||||
{
|
||||
}
|
||||
|
||||
int open ();
|
||||
void close ();
|
||||
|
||||
void uponClosing ();
|
||||
|
||||
Run* parse (Source& in, Source& out);
|
||||
|
||||
/// The setConsoleCmgr() function sets the encoding type of
|
||||
/// the console streams. They include both the input and the output
|
||||
/// streams. It provides no way to specify a different encoding
|
||||
/// type for the input and the output stream.
|
||||
void setConsoleCmgr (const hawk_cmgr_t* cmgr);
|
||||
|
||||
/// The getConsoleCmgr() function returns the current encoding
|
||||
/// type set for the console streams.
|
||||
const hawk_cmgr_t* getConsoleCmgr () const;
|
||||
|
||||
/// The addConsoleOutput() function adds a file to form an
|
||||
/// output console stream.
|
||||
int addConsoleOutput (const hawk_uch_t* arg, hawk_oow_t len);
|
||||
int addConsoleOutput (const hawk_uch_t* arg);
|
||||
int addConsoleOutput (const hawk_bch_t* arg, hawk_oow_t len);
|
||||
int addConsoleOutput (const hawk_bch_t* arg);
|
||||
|
||||
void clearConsoleOutputs ();
|
||||
|
||||
protected:
|
||||
#define HAWK_STD_ENV_CHAR_IS_BCH
|
||||
typedef hawk_bch_t env_char_t;
|
||||
|
||||
int make_additional_globals (Run* run);
|
||||
int build_argcv (Run* run);
|
||||
int build_environ (Run* run, env_char_t* envarr[]);
|
||||
|
||||
// intrinsic functions
|
||||
hawk_cmgr_t* getiocmgr (const hawk_ooch_t* ioname);
|
||||
|
||||
int setioattr (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len);
|
||||
int getioattr (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len);
|
||||
|
||||
// pipe io handlers
|
||||
int openPipe (Pipe& io);
|
||||
int closePipe (Pipe& io);
|
||||
hawk_ooi_t readPipe (Pipe& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t readPipeBytes (Pipe& io, hawk_bch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writePipe (Pipe& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writePipeBytes (Pipe& io, const hawk_bch_t* buf, hawk_oow_t len);
|
||||
int flushPipe (Pipe& io);
|
||||
|
||||
// file io handlers
|
||||
int openFile (File& io);
|
||||
int closeFile (File& io);
|
||||
hawk_ooi_t readFile (File& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t readFileBytes (File& io, hawk_bch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeFile (File& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeFileBytes (File& io, const hawk_bch_t* buf, hawk_oow_t len);
|
||||
int flushFile (File& io);
|
||||
|
||||
// console io handlers
|
||||
int openConsole (Console& io);
|
||||
int closeConsole (Console& io);
|
||||
hawk_ooi_t readConsole (Console& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t readConsoleBytes (Console& io, hawk_bch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeConsole (Console& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeConsoleBytes (Console& io, const hawk_bch_t* buf, hawk_oow_t len);
|
||||
int flushConsole (Console& io);
|
||||
int nextConsole (Console& io);
|
||||
|
||||
// primitive handlers
|
||||
void* allocMem (hawk_oow_t n);
|
||||
void* reallocMem (void* ptr, hawk_oow_t n);
|
||||
void freeMem (void* ptr);
|
||||
|
||||
hawk_flt_t pow (hawk_flt_t x, hawk_flt_t y);
|
||||
hawk_flt_t mod (hawk_flt_t x, hawk_flt_t y);
|
||||
|
||||
void* modopen (const hawk_mod_spec_t* spec);
|
||||
void modclose (void* handle);
|
||||
void* modgetsym (void* handle, const hawk_ooch_t* name);
|
||||
|
||||
protected:
|
||||
hawk_htb_t cmgrtab;
|
||||
bool cmgrtab_inited;
|
||||
bool stdmod_up;
|
||||
|
||||
hawk_cmgr_t* console_cmgr;
|
||||
|
||||
// global variables
|
||||
int gbl_argc;
|
||||
int gbl_argv;
|
||||
int gbl_environ;
|
||||
|
||||
// standard input console - reuse runarg
|
||||
hawk_oow_t runarg_index;
|
||||
hawk_oow_t runarg_count;
|
||||
|
||||
// standard output console
|
||||
xstrs_t ofile;
|
||||
hawk_oow_t ofile_index;
|
||||
hawk_oow_t ofile_count;
|
||||
|
||||
public:
|
||||
struct ioattr_t
|
||||
{
|
||||
hawk_cmgr_t* cmgr;
|
||||
hawk_ooch_t cmgr_name[64]; // i assume that the cmgr name never exceeds this length.
|
||||
hawk_ntime_t tmout[4];
|
||||
|
||||
ioattr_t (): cmgr (HAWK_NULL)
|
||||
{
|
||||
this->cmgr_name[0] = HAWK_T('\0');
|
||||
for (hawk_oow_t i = 0; i < HAWK_COUNTOF(this->tmout); i++)
|
||||
{
|
||||
this->tmout[i].sec = -999;
|
||||
this->tmout[i].nsec = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static ioattr_t default_ioattr;
|
||||
|
||||
protected:
|
||||
ioattr_t* get_ioattr (const hawk_ooch_t* ptr, hawk_oow_t len);
|
||||
ioattr_t* find_or_make_ioattr (const hawk_ooch_t* ptr, hawk_oow_t len);
|
||||
|
||||
|
||||
private:
|
||||
int open_console_in (Console& io);
|
||||
int open_console_out (Console& io);
|
||||
|
||||
int open_pio (Pipe& io);
|
||||
int open_nwio (Pipe& io, int flags, void* nwad);
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_END_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
||||
|
@ -1,270 +0,0 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _HAWK_HAWK_STD_HPP_
|
||||
#define _HAWK_HAWK_STD_HPP_
|
||||
|
||||
#include <Hawk.hpp>
|
||||
|
||||
/// \file
|
||||
/// Standard Hawk Interpreter
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_BEGIN_NAMESPACE(HAWK)
|
||||
////////////////////////////////
|
||||
|
||||
class HAWK_EXPORT MmgrStd: public Mmgr
|
||||
{
|
||||
public:
|
||||
MmgrStd () HAWK_CXX_NOEXCEPT: Mmgr () {}
|
||||
|
||||
void* allocMem (hawk_oow_t n) HAWK_CXX_NOEXCEPT;
|
||||
void* reallocMem (void* ptr, hawk_oow_t n) HAWK_CXX_NOEXCEPT;
|
||||
void freeMem (void* ptr) HAWK_CXX_NOEXCEPT;
|
||||
|
||||
#if 0
|
||||
/// The getInstance() function returns the stock instance of the MmgrStd class.
|
||||
static MmgrStd* getInstance () HAWK_CXX_NOEXCEPT;
|
||||
#endif
|
||||
};
|
||||
|
||||
///
|
||||
/// The HawkStd class provides an easier-to-use interface by overriding
|
||||
/// primitive methods, and implementing the file handler, the pipe handler,
|
||||
/// and common intrinsic functions.
|
||||
///
|
||||
class HAWK_EXPORT HawkStd: public Hawk
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// The SourceFile class implements script I/O from and to a file.
|
||||
///
|
||||
class HAWK_EXPORT SourceFile: public Source
|
||||
{
|
||||
public:
|
||||
SourceFile (const hawk_uch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_UCH), _name(name), cmgr(cmgr)
|
||||
{
|
||||
dir.ptr = HAWK_NULL; dir.len = 0;
|
||||
}
|
||||
|
||||
SourceFile (const hawk_bch_t* name, hawk_cmgr_t* cmgr = HAWK_NULL): _type(NAME_BCH), _name(name), cmgr(cmgr)
|
||||
{
|
||||
dir.ptr = HAWK_NULL; dir.len = 0;
|
||||
}
|
||||
|
||||
~SourceFile () {};
|
||||
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
|
||||
protected:
|
||||
enum
|
||||
{
|
||||
NAME_UCH,
|
||||
NAME_BCH
|
||||
} _type;
|
||||
const void* _name;
|
||||
hawk_oocs_t dir;
|
||||
hawk_cmgr_t* cmgr;
|
||||
};
|
||||
|
||||
///
|
||||
/// The SourceString class implements script input from a string.
|
||||
/// Deparsing is not supported.
|
||||
///
|
||||
class HAWK_EXPORT SourceString: public Source
|
||||
{
|
||||
public:
|
||||
SourceString (const hawk_uch_t* str, hawk_cmgr_t* cmgr = HAWK_NULL): _type(STR_UCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL), cmgr(cmgr) {}
|
||||
SourceString (const hawk_bch_t* str, hawk_cmgr_t* cmgr = HAWK_NULL): _type(STR_BCH), _str(str), _hawk(HAWK_NULL), str(HAWK_NULL), ptr(HAWK_NULL), cmgr(cmgr) {}
|
||||
~SourceString ();
|
||||
|
||||
int open (Data& io);
|
||||
int close (Data& io);
|
||||
hawk_ooi_t read (Data& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
|
||||
protected:
|
||||
enum
|
||||
{
|
||||
STR_UCH,
|
||||
STR_BCH
|
||||
} _type;
|
||||
const void* _str;
|
||||
hawk_t* _hawk;
|
||||
|
||||
hawk_ooch_t* str;
|
||||
const hawk_ooch_t* ptr;
|
||||
hawk_cmgr_t* cmgr; // for reading included files
|
||||
};
|
||||
|
||||
HawkStd (Mmgr* mmgr = HAWK_NULL): Hawk(mmgr), cmgrtab_inited(false), stdmod_up(false), console_cmgr(HAWK_NULL)
|
||||
{
|
||||
}
|
||||
|
||||
int open ();
|
||||
void close ();
|
||||
|
||||
void uponClosing ();
|
||||
|
||||
Run* parse (Source& in, Source& out);
|
||||
|
||||
/// The setConsoleCmgr() function sets the encoding type of
|
||||
/// the console streams. They include both the input and the output
|
||||
/// streams. It provides no way to specify a different encoding
|
||||
/// type for the input and the output stream.
|
||||
void setConsoleCmgr (const hawk_cmgr_t* cmgr);
|
||||
|
||||
/// The getConsoleCmgr() function returns the current encoding
|
||||
/// type set for the console streams.
|
||||
const hawk_cmgr_t* getConsoleCmgr () const;
|
||||
|
||||
/// The addConsoleOutput() function adds a file to form an
|
||||
/// output console stream.
|
||||
int addConsoleOutput (const hawk_uch_t* arg, hawk_oow_t len);
|
||||
int addConsoleOutput (const hawk_uch_t* arg);
|
||||
int addConsoleOutput (const hawk_bch_t* arg, hawk_oow_t len);
|
||||
int addConsoleOutput (const hawk_bch_t* arg);
|
||||
|
||||
void clearConsoleOutputs ();
|
||||
|
||||
protected:
|
||||
#define HAWK_STD_ENV_CHAR_IS_BCH
|
||||
typedef hawk_bch_t env_char_t;
|
||||
|
||||
int make_additional_globals (Run* run);
|
||||
int build_argcv (Run* run);
|
||||
int build_environ (Run* run, env_char_t* envarr[]);
|
||||
|
||||
// intrinsic functions
|
||||
hawk_cmgr_t* getiocmgr (const hawk_ooch_t* ioname);
|
||||
|
||||
int setioattr (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len);
|
||||
int getioattr (Run& run, Value& ret, Value* args, hawk_oow_t nargs, const hawk_ooch_t* name, hawk_oow_t len);
|
||||
|
||||
// pipe io handlers
|
||||
int openPipe (Pipe& io);
|
||||
int closePipe (Pipe& io);
|
||||
hawk_ooi_t readPipe (Pipe& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t readPipeBytes (Pipe& io, hawk_bch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writePipe (Pipe& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writePipeBytes (Pipe& io, const hawk_bch_t* buf, hawk_oow_t len);
|
||||
int flushPipe (Pipe& io);
|
||||
|
||||
// file io handlers
|
||||
int openFile (File& io);
|
||||
int closeFile (File& io);
|
||||
hawk_ooi_t readFile (File& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t readFileBytes (File& io, hawk_bch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeFile (File& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeFileBytes (File& io, const hawk_bch_t* buf, hawk_oow_t len);
|
||||
int flushFile (File& io);
|
||||
|
||||
// console io handlers
|
||||
int openConsole (Console& io);
|
||||
int closeConsole (Console& io);
|
||||
hawk_ooi_t readConsole (Console& io, hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t readConsoleBytes (Console& io, hawk_bch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeConsole (Console& io, const hawk_ooch_t* buf, hawk_oow_t len);
|
||||
hawk_ooi_t writeConsoleBytes (Console& io, const hawk_bch_t* buf, hawk_oow_t len);
|
||||
int flushConsole (Console& io);
|
||||
int nextConsole (Console& io);
|
||||
|
||||
// primitive handlers
|
||||
void* allocMem (hawk_oow_t n);
|
||||
void* reallocMem (void* ptr, hawk_oow_t n);
|
||||
void freeMem (void* ptr);
|
||||
|
||||
hawk_flt_t pow (hawk_flt_t x, hawk_flt_t y);
|
||||
hawk_flt_t mod (hawk_flt_t x, hawk_flt_t y);
|
||||
|
||||
void* modopen (const hawk_mod_spec_t* spec);
|
||||
void modclose (void* handle);
|
||||
void* modgetsym (void* handle, const hawk_ooch_t* name);
|
||||
|
||||
protected:
|
||||
hawk_htb_t cmgrtab;
|
||||
bool cmgrtab_inited;
|
||||
bool stdmod_up;
|
||||
|
||||
hawk_cmgr_t* console_cmgr;
|
||||
|
||||
// global variables
|
||||
int gbl_argc;
|
||||
int gbl_argv;
|
||||
int gbl_environ;
|
||||
|
||||
// standard input console - reuse runarg
|
||||
hawk_oow_t runarg_index;
|
||||
hawk_oow_t runarg_count;
|
||||
|
||||
// standard output console
|
||||
xstrs_t ofile;
|
||||
hawk_oow_t ofile_index;
|
||||
hawk_oow_t ofile_count;
|
||||
|
||||
public:
|
||||
struct ioattr_t
|
||||
{
|
||||
hawk_cmgr_t* cmgr;
|
||||
hawk_ooch_t cmgr_name[64]; // i assume that the cmgr name never exceeds this length.
|
||||
hawk_ntime_t tmout[4];
|
||||
|
||||
ioattr_t (): cmgr (HAWK_NULL)
|
||||
{
|
||||
this->cmgr_name[0] = HAWK_T('\0');
|
||||
for (hawk_oow_t i = 0; i < HAWK_COUNTOF(this->tmout); i++)
|
||||
{
|
||||
this->tmout[i].sec = -999;
|
||||
this->tmout[i].nsec = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static ioattr_t default_ioattr;
|
||||
|
||||
protected:
|
||||
ioattr_t* get_ioattr (const hawk_ooch_t* ptr, hawk_oow_t len);
|
||||
ioattr_t* find_or_make_ioattr (const hawk_ooch_t* ptr, hawk_oow_t len);
|
||||
|
||||
|
||||
private:
|
||||
int open_console_in (Console& io);
|
||||
int open_console_out (Console& io);
|
||||
|
||||
int open_pio (Pipe& io);
|
||||
int open_nwio (Pipe& io, int flags, void* nwad);
|
||||
};
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_END_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -170,8 +170,8 @@ libhawk_la_LIBADD += $(UNWIND_LIBS)
|
||||
endif
|
||||
|
||||
if ENABLE_CXX
|
||||
pkginclude_HEADERS += Hawk.hpp HawkStd.hpp
|
||||
libhawk_la_SOURCES += Hawk.cpp HawkStd.cpp
|
||||
pkginclude_HEADERS += Hawk.hpp Hawk-Sed.hpp
|
||||
libhawk_la_SOURCES += Hawk.cpp Std.cpp Sed.cpp Std-Sed.cpp
|
||||
|
||||
###libhawk_la_LINK = $(CXXLINK)
|
||||
libhawk_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \
|
||||
|
@ -93,8 +93,8 @@ host_triplet = @host@
|
||||
@ENABLE_LIBLTDL_TRUE@am__append_4 = $(LTDL_LIBS)
|
||||
@ENABLE_LIBLTDL_FALSE@am__append_5 = $(DL_LIBS)
|
||||
@ENABLE_LIBUNWIND_TRUE@am__append_6 = $(UNWIND_LIBS)
|
||||
@ENABLE_CXX_TRUE@am__append_7 = Hawk.hpp HawkStd.hpp
|
||||
@ENABLE_CXX_TRUE@am__append_8 = Hawk.cpp HawkStd.cpp
|
||||
@ENABLE_CXX_TRUE@am__append_7 = Hawk.hpp Hawk-Sed.hpp
|
||||
@ENABLE_CXX_TRUE@am__append_8 = Hawk.cpp Std.cpp Sed.cpp Std-Sed.cpp
|
||||
|
||||
##################################################
|
||||
# STATIC MODULES BUILT INTO MAIN LIBRARY
|
||||
@ -232,7 +232,7 @@ am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \
|
||||
hawk-dir.h hawk-ecs.h hawk-fmt.h hawk-gem.h hawk-htb.h \
|
||||
hawk-map.h hawk-rbt.h hawk-pack1.h hawk-skad.h hawk-utl.h \
|
||||
hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \
|
||||
Hawk.hpp HawkStd.hpp arr.c chr.c dir.c ecs-imp.h ecs.c \
|
||||
Hawk.hpp Hawk-Sed.hpp arr.c chr.c dir.c ecs-imp.h ecs.c \
|
||||
err-prv.h err.c err-sys.c fmt-imp.h fmt.c fnc-prv.h fnc.c \
|
||||
htb.c gem.c gem-nwif.c gem-nwif2.c hawk-prv.h hawk.c \
|
||||
idmap-imp.h mb8.c misc-imp.h misc-prv.h misc.c parse-prv.h \
|
||||
@ -245,13 +245,13 @@ am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \
|
||||
utl-str.c utl-sys.c utl.c val-prv.h val.c xma.c hawk-cli.h \
|
||||
hawk-fio.h hawk-mtx.h hawk-pio.h hawk-sio.h hawk-tio.h \
|
||||
cli-imp.h cli.c fio.c mtx.c pio.c sio.c syscall.h tio.c \
|
||||
std-prv.h std.c std-sed.c Hawk.cpp HawkStd.cpp mod-hawk.c \
|
||||
mod-hawk.h mod-math.c mod-math.h mod-str.c mod-str.h mod-sys.c \
|
||||
mod-sys.h
|
||||
std-prv.h std.c std-sed.c Hawk.cpp Std.cpp Sed.cpp Std-Sed.cpp \
|
||||
mod-hawk.c mod-hawk.h mod-math.c mod-math.h mod-str.c \
|
||||
mod-str.h mod-sys.c mod-sys.h
|
||||
am__objects_1 =
|
||||
am__objects_2 = $(am__objects_1)
|
||||
@ENABLE_CXX_TRUE@am__objects_3 = libhawk_la-Hawk.lo \
|
||||
@ENABLE_CXX_TRUE@ libhawk_la-HawkStd.lo
|
||||
@ENABLE_CXX_TRUE@am__objects_3 = libhawk_la-Hawk.lo libhawk_la-Std.lo \
|
||||
@ENABLE_CXX_TRUE@ libhawk_la-Sed.lo libhawk_la-Std-Sed.lo
|
||||
@ENABLE_STATIC_MODULE_TRUE@am__objects_4 = libhawk_la-mod-hawk.lo \
|
||||
@ENABLE_STATIC_MODULE_TRUE@ libhawk_la-mod-math.lo \
|
||||
@ENABLE_STATIC_MODULE_TRUE@ libhawk_la-mod-str.lo \
|
||||
@ -293,11 +293,11 @@ DEFAULT_INCLUDES =
|
||||
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
|
||||
am__maybe_remake_depfiles = depfiles
|
||||
am__depfiles_remade = ./$(DEPDIR)/libhawk_hawk_la-mod-hawk.Plo \
|
||||
./$(DEPDIR)/libhawk_la-Hawk.Plo \
|
||||
./$(DEPDIR)/libhawk_la-HawkStd.Plo \
|
||||
./$(DEPDIR)/libhawk_la-arr.Plo ./$(DEPDIR)/libhawk_la-chr.Plo \
|
||||
./$(DEPDIR)/libhawk_la-cli.Plo ./$(DEPDIR)/libhawk_la-dir.Plo \
|
||||
./$(DEPDIR)/libhawk_la-ecs.Plo \
|
||||
./$(DEPDIR)/libhawk_la-Hawk.Plo ./$(DEPDIR)/libhawk_la-Sed.Plo \
|
||||
./$(DEPDIR)/libhawk_la-Std-Sed.Plo \
|
||||
./$(DEPDIR)/libhawk_la-Std.Plo ./$(DEPDIR)/libhawk_la-arr.Plo \
|
||||
./$(DEPDIR)/libhawk_la-chr.Plo ./$(DEPDIR)/libhawk_la-cli.Plo \
|
||||
./$(DEPDIR)/libhawk_la-dir.Plo ./$(DEPDIR)/libhawk_la-ecs.Plo \
|
||||
./$(DEPDIR)/libhawk_la-err-sys.Plo \
|
||||
./$(DEPDIR)/libhawk_la-err.Plo ./$(DEPDIR)/libhawk_la-fio.Plo \
|
||||
./$(DEPDIR)/libhawk_la-fmt.Plo ./$(DEPDIR)/libhawk_la-fnc.Plo \
|
||||
@ -392,7 +392,7 @@ am__pkginclude_HEADERS_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \
|
||||
hawk-dir.h hawk-ecs.h hawk-fmt.h hawk-gem.h hawk-htb.h \
|
||||
hawk-map.h hawk-rbt.h hawk-pack1.h hawk-skad.h hawk-utl.h \
|
||||
hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \
|
||||
Hawk.hpp HawkStd.hpp
|
||||
Hawk.hpp Hawk-Sed.hpp
|
||||
HEADERS = $(pkginclude_HEADERS)
|
||||
am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \
|
||||
hawk-cfg.h.in
|
||||
@ -779,7 +779,9 @@ distclean-compile:
|
||||
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_hawk_la-mod-hawk.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-Hawk.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-HawkStd.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-Sed.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-Std-Sed.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-Std.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-arr.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-chr.Plo@am__quote@ # am--include-marker
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libhawk_la-cli.Plo@am__quote@ # am--include-marker
|
||||
@ -1297,12 +1299,26 @@ libhawk_la-Hawk.lo: Hawk.cpp
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -c -o libhawk_la-Hawk.lo `test -f 'Hawk.cpp' || echo '$(srcdir)/'`Hawk.cpp
|
||||
|
||||
libhawk_la-HawkStd.lo: HawkStd.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -MT libhawk_la-HawkStd.lo -MD -MP -MF $(DEPDIR)/libhawk_la-HawkStd.Tpo -c -o libhawk_la-HawkStd.lo `test -f 'HawkStd.cpp' || echo '$(srcdir)/'`HawkStd.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-HawkStd.Tpo $(DEPDIR)/libhawk_la-HawkStd.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='HawkStd.cpp' object='libhawk_la-HawkStd.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
libhawk_la-Std.lo: Std.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -MT libhawk_la-Std.lo -MD -MP -MF $(DEPDIR)/libhawk_la-Std.Tpo -c -o libhawk_la-Std.lo `test -f 'Std.cpp' || echo '$(srcdir)/'`Std.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-Std.Tpo $(DEPDIR)/libhawk_la-Std.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='Std.cpp' object='libhawk_la-Std.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -c -o libhawk_la-HawkStd.lo `test -f 'HawkStd.cpp' || echo '$(srcdir)/'`HawkStd.cpp
|
||||
@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -c -o libhawk_la-Std.lo `test -f 'Std.cpp' || echo '$(srcdir)/'`Std.cpp
|
||||
|
||||
libhawk_la-Sed.lo: Sed.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -MT libhawk_la-Sed.lo -MD -MP -MF $(DEPDIR)/libhawk_la-Sed.Tpo -c -o libhawk_la-Sed.lo `test -f 'Sed.cpp' || echo '$(srcdir)/'`Sed.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-Sed.Tpo $(DEPDIR)/libhawk_la-Sed.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='Sed.cpp' object='libhawk_la-Sed.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -c -o libhawk_la-Sed.lo `test -f 'Sed.cpp' || echo '$(srcdir)/'`Sed.cpp
|
||||
|
||||
libhawk_la-Std-Sed.lo: Std-Sed.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -MT libhawk_la-Std-Sed.lo -MD -MP -MF $(DEPDIR)/libhawk_la-Std-Sed.Tpo -c -o libhawk_la-Std-Sed.lo `test -f 'Std-Sed.cpp' || echo '$(srcdir)/'`Std-Sed.cpp
|
||||
@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libhawk_la-Std-Sed.Tpo $(DEPDIR)/libhawk_la-Std-Sed.Plo
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='Std-Sed.cpp' object='libhawk_la-Std-Sed.lo' libtool=yes @AMDEPBACKSLASH@
|
||||
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
|
||||
@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libhawk_la_CPPFLAGS) $(CPPFLAGS) $(libhawk_la_CXXFLAGS) $(CXXFLAGS) -c -o libhawk_la-Std-Sed.lo `test -f 'Std-Sed.cpp' || echo '$(srcdir)/'`Std-Sed.cpp
|
||||
|
||||
mostlyclean-libtool:
|
||||
-rm -f *.lo
|
||||
@ -1461,7 +1477,9 @@ clean-am: clean-generic clean-libtool clean-pkglibLTLIBRARIES \
|
||||
distclean: distclean-am
|
||||
-rm -f ./$(DEPDIR)/libhawk_hawk_la-mod-hawk.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Hawk.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-HawkStd.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Sed.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Std-Sed.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Std.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-arr.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-chr.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-cli.Plo
|
||||
@ -1566,7 +1584,9 @@ installcheck-am:
|
||||
maintainer-clean: maintainer-clean-am
|
||||
-rm -f ./$(DEPDIR)/libhawk_hawk_la-mod-hawk.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Hawk.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-HawkStd.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Sed.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Std-Sed.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-Std.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-arr.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-chr.Plo
|
||||
-rm -f ./$(DEPDIR)/libhawk_la-cli.Plo
|
||||
|
287
hawk/lib/Sed.cpp
Normal file
287
hawk/lib/Sed.cpp
Normal file
@ -0,0 +1,287 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <Hawk-Sed.hpp>
|
||||
#include "sed-prv.h"
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_BEGIN_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
||||
|
||||
struct xtn_t
|
||||
{
|
||||
Sed* sed;
|
||||
};
|
||||
|
||||
#if defined(HAWK_HAVE_INLINE)
|
||||
static HAWK_INLINE xtn_t* GET_XTN(hawk_sed_t* sed) { return (xtn_t*)((hawk_uint8_t*)hawk_sed_getxtn(sed) - HAWK_SIZEOF(xtn_t)); }
|
||||
#else
|
||||
#define GET_XTN(sed) ((xtn_t*)((hawk_uint8_t*)hawk_sed_getxtn(sed) - HAWK_SIZEOF(xtn_t)))
|
||||
#endif
|
||||
|
||||
Sed::Sed (Mmgr* mmgr): Mmged(mmgr), sed(HAWK_NULL), dflerrstr(HAWK_NULL)
|
||||
{
|
||||
this->_cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8);
|
||||
}
|
||||
|
||||
hawk_cmgr_t* Sed::getCmgr () const
|
||||
{
|
||||
return this->sed? hawk_sed_getcmgr(this->sed): this->_cmgr;
|
||||
}
|
||||
|
||||
void Sed::setCmgr (hawk_cmgr_t* cmgr)
|
||||
{
|
||||
if (this->sed) hawk_sed_setcmgr(this->sed, cmgr);
|
||||
this->_cmgr = cmgr;
|
||||
}
|
||||
|
||||
int Sed::open ()
|
||||
{
|
||||
hawk_errnum_t errnum;
|
||||
this->sed = hawk_sed_open(this->getMmgr(), HAWK_SIZEOF(xtn_t), this->getCmgr(), &errnum);
|
||||
if (!this->sed)
|
||||
{
|
||||
this->setError (errnum);
|
||||
return -1;
|
||||
}
|
||||
|
||||
this->sed->_instsize += HAWK_SIZEOF(xtn_t);
|
||||
|
||||
xtn_t* xtn = GET_XTN(this->sed);
|
||||
xtn->sed = this;
|
||||
|
||||
this->dflerrstr = hawk_sed_geterrstr(this->sed);
|
||||
// TODO: revive this too when hawk_sed_seterrstr is revived()
|
||||
// hawk_sed_seterrstr (this->sed, Sed::xerrstr);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Sed::close ()
|
||||
{
|
||||
if (this->sed)
|
||||
{
|
||||
hawk_sed_close (this->sed);
|
||||
this->sed = HAWK_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int Sed::compile (Stream& sstream)
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
|
||||
this->sstream = &sstream;
|
||||
return hawk_sed_comp(this->sed, Sed::sin);
|
||||
}
|
||||
|
||||
int Sed::execute (Stream& iostream)
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
|
||||
this->iostream = &iostream;
|
||||
return hawk_sed_exec(this->sed, Sed::xin, Sed::xout);
|
||||
}
|
||||
|
||||
void Sed::halt ()
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
hawk_sed_halt (this->sed);
|
||||
}
|
||||
|
||||
bool Sed::isHalt () const
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
return hawk_sed_ishalt(this->sed);
|
||||
}
|
||||
|
||||
int Sed::getTrait () const
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
int val;
|
||||
hawk_sed_getopt (this->sed, HAWK_SED_TRAIT, &val);
|
||||
return val;
|
||||
}
|
||||
|
||||
void Sed::setTrait (int trait)
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
hawk_sed_setopt (this->sed, HAWK_SED_TRAIT, &trait);
|
||||
}
|
||||
|
||||
const hawk_ooch_t* Sed::getErrorMessage () const
|
||||
{
|
||||
return (this->sed == HAWK_NULL)? HAWK_T(""): hawk_sed_geterrmsg(this->sed);
|
||||
}
|
||||
|
||||
const hawk_uch_t* Sed::getErrorMessageU () const
|
||||
{
|
||||
return (this->sed == HAWK_NULL)? HAWK_UT(""): hawk_sed_geterrumsg(this->sed);
|
||||
}
|
||||
|
||||
const hawk_bch_t* Sed::getErrorMessageB () const
|
||||
{
|
||||
return (this->sed == HAWK_NULL)? HAWK_BT(""): hawk_sed_geterrbmsg(this->sed);
|
||||
}
|
||||
|
||||
hawk_loc_t Sed::getErrorLocation () const
|
||||
{
|
||||
if (this->sed == HAWK_NULL)
|
||||
{
|
||||
hawk_loc_t loc;
|
||||
loc.line = 0;
|
||||
loc.colm = 0;
|
||||
return loc;
|
||||
}
|
||||
return *hawk_sed_geterrloc(this->sed);
|
||||
}
|
||||
|
||||
hawk_errnum_t Sed::getErrorNumber () const
|
||||
{
|
||||
return (this->sed == HAWK_NULL)? HAWK_ENOERR: hawk_sed_geterrnum(this->sed);
|
||||
}
|
||||
|
||||
void Sed::setError (hawk_errnum_t err, const hawk_oocs_t* args, const hawk_loc_t* loc)
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
hawk_sed_seterror (this->sed, loc, err, args);
|
||||
}
|
||||
|
||||
const hawk_ooch_t* Sed::getCompileId () const
|
||||
{
|
||||
return hawk_sed_getcompid(this->sed);
|
||||
}
|
||||
|
||||
const hawk_ooch_t* Sed::setCompileId (const hawk_ooch_t* id)
|
||||
{
|
||||
return hawk_sed_setcompid(this->sed, id);
|
||||
}
|
||||
|
||||
hawk_oow_t Sed::getConsoleLine ()
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
return hawk_sed_getlinenum(this->sed);
|
||||
}
|
||||
|
||||
void Sed::setConsoleLine (hawk_oow_t num)
|
||||
{
|
||||
HAWK_ASSERT (this->sed != HAWK_NULL);
|
||||
hawk_sed_setlinenum (this->sed, num);
|
||||
}
|
||||
|
||||
hawk_ooi_t Sed::sin (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len)
|
||||
{
|
||||
xtn_t* xtn = GET_XTN(s);
|
||||
|
||||
Stream::Data iodata (xtn->sed, Stream::READ, arg);
|
||||
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case HAWK_SED_IO_OPEN:
|
||||
return xtn->sed->sstream->open(iodata);
|
||||
case HAWK_SED_IO_CLOSE:
|
||||
return xtn->sed->sstream->close(iodata);
|
||||
case HAWK_SED_IO_READ:
|
||||
return xtn->sed->sstream->read(iodata, buf, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
hawk_ooi_t Sed::xin (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* buf, hawk_oow_t len)
|
||||
{
|
||||
xtn_t* xtn = GET_XTN(s);
|
||||
|
||||
Stream::Data iodata (xtn->sed, Stream::READ, arg);
|
||||
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case HAWK_SED_IO_OPEN:
|
||||
return xtn->sed->iostream->open(iodata);
|
||||
case HAWK_SED_IO_CLOSE:
|
||||
return xtn->sed->iostream->close(iodata);
|
||||
case HAWK_SED_IO_READ:
|
||||
return xtn->sed->iostream->read(iodata, buf, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
hawk_ooi_t Sed::xout (hawk_sed_t* s, hawk_sed_io_cmd_t cmd, hawk_sed_io_arg_t* arg, hawk_ooch_t* dat, hawk_oow_t len)
|
||||
{
|
||||
xtn_t* xtn = GET_XTN(s);
|
||||
|
||||
Stream::Data iodata (xtn->sed, Stream::WRITE, arg);
|
||||
|
||||
try
|
||||
{
|
||||
switch (cmd)
|
||||
{
|
||||
case HAWK_SED_IO_OPEN:
|
||||
return xtn->sed->iostream->open(iodata);
|
||||
case HAWK_SED_IO_CLOSE:
|
||||
return xtn->sed->iostream->close(iodata);
|
||||
case HAWK_SED_IO_WRITE:
|
||||
return xtn->sed->iostream->write(iodata, dat, len);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
const hawk_ooch_t* Sed::getErrorString (hawk_errnum_t num) const
|
||||
{
|
||||
HAWK_ASSERT (this->dflerrstr != HAWK_NULL);
|
||||
return this->dflerrstr(num);
|
||||
}
|
||||
|
||||
const hawk_ooch_t* Sed::xerrstr (hawk_sed_t* s, hawk_errnum_t num)
|
||||
{
|
||||
xtn_t* xtn = GET_XTN(s);
|
||||
return xtn->sed->getErrorString(num);
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_END_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
300
hawk/lib/Std-Sed.cpp
Normal file
300
hawk/lib/Std-Sed.cpp
Normal file
@ -0,0 +1,300 @@
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
Copyright (c) 2006-2020 Chung, Hyung-Hwan. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <Hawk-Sed.hpp>
|
||||
#include <hawk-fio.h>
|
||||
#include <hawk-sio.h>
|
||||
#include "sed-prv.h"
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_BEGIN_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
||||
|
||||
static hawk_sio_t* open_sio (SedStd::Stream::Data& io, const hawk_ooch_t* file, int flags)
|
||||
{
|
||||
hawk_sio_t* sio;
|
||||
|
||||
sio = hawk_sio_open((hawk_gem_t*)io, 0, file, flags);
|
||||
if (sio == HAWK_NULL)
|
||||
{
|
||||
hawk_oocs_t ea;
|
||||
ea.ptr = (hawk_ooch_t*)file;
|
||||
ea.len = hawk_count_oocstr(file);
|
||||
((Sed*)io)->setError (HAWK_SED_EIOFIL, &ea);
|
||||
}
|
||||
return sio;
|
||||
}
|
||||
|
||||
static hawk_sio_t* open_sio_std (SedStd::Stream::Data& io, hawk_sio_std_t std, int flags)
|
||||
{
|
||||
hawk_sio_t* sio;
|
||||
static const hawk_ooch_t* std_names[] =
|
||||
{
|
||||
HAWK_T("stdin"),
|
||||
HAWK_T("stdout"),
|
||||
HAWK_T("stderr"),
|
||||
};
|
||||
|
||||
sio = hawk_sio_openstd((hawk_gem_t*)io, 0, std, flags);
|
||||
if (sio == HAWK_NULL)
|
||||
{
|
||||
hawk_oocs_t ea;
|
||||
ea.ptr = (hawk_ooch_t*)std_names[std];
|
||||
ea.len = hawk_count_oocstr(std_names[std]);
|
||||
((Sed*)io)->setError (HAWK_SED_EIOFIL, &ea);
|
||||
}
|
||||
return sio;
|
||||
}
|
||||
|
||||
int SedStd::FileStream::open (Data& io)
|
||||
{
|
||||
hawk_sio_t* sio;
|
||||
const hawk_ooch_t* ioname = io.getName();
|
||||
int oflags;
|
||||
|
||||
if (io.getMode() == READ)
|
||||
oflags = HAWK_SIO_READ | HAWK_SIO_IGNOREECERR;
|
||||
else
|
||||
oflags = HAWK_SIO_WRITE | HAWK_SIO_CREATE | HAWK_SIO_TRUNCATE | HAWK_SIO_IGNOREECERR | HAWK_SIO_LINEBREAK;
|
||||
|
||||
if (ioname == HAWK_NULL)
|
||||
{
|
||||
//
|
||||
// a normal console is indicated by a null name or a dash
|
||||
//
|
||||
if (io.getMode() == READ)
|
||||
{
|
||||
sio = (infile == HAWK_NULL || (infile[0] == HAWK_T('-') && infile[1] == HAWK_T('\0')))?
|
||||
open_sio_std (io, HAWK_SIO_STDIN, oflags):
|
||||
open_sio (io, infile, oflags);
|
||||
}
|
||||
else
|
||||
{
|
||||
sio = (outfile == HAWK_NULL || (outfile[0] == HAWK_T('-') && outfile[1] == HAWK_T('\0')))?
|
||||
open_sio_std (io, HAWK_SIO_STDOUT, oflags):
|
||||
open_sio (io, outfile, oflags);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// if ioname is not empty, it is a file name
|
||||
//
|
||||
sio = open_sio (io, ioname, oflags);
|
||||
}
|
||||
if (sio == HAWK_NULL) return -1;
|
||||
|
||||
if (this->cmgr) hawk_sio_setcmgr (sio, this->cmgr);
|
||||
io.setHandle (sio);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SedStd::FileStream::close (Data& io)
|
||||
{
|
||||
hawk_sio_close ((hawk_sio_t*)io.getHandle());
|
||||
return 0;
|
||||
}
|
||||
|
||||
hawk_ooi_t SedStd::FileStream::read (Data& io, hawk_ooch_t* buf, hawk_oow_t len)
|
||||
{
|
||||
hawk_ooi_t n = hawk_sio_getoochars((hawk_sio_t*)io.getHandle(), buf, len);
|
||||
|
||||
if (n == -1)
|
||||
{
|
||||
if (io.getName() == HAWK_NULL && infile != HAWK_NULL)
|
||||
{
|
||||
// if writing to outfile, set the error message
|
||||
// explicitly. other cases are handled by
|
||||
// the caller in sed.c.
|
||||
hawk_oocs_t ea;
|
||||
ea.ptr = (hawk_ooch_t*)infile;
|
||||
ea.len = hawk_count_oocstr(infile);
|
||||
((Sed*)io)->setError (HAWK_SED_EIOFIL, &ea);
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
hawk_ooi_t SedStd::FileStream::write (Data& io, const hawk_ooch_t* buf, hawk_oow_t len)
|
||||
{
|
||||
hawk_ooi_t n = hawk_sio_putoochars((hawk_sio_t*)io.getHandle(), buf, len);
|
||||
|
||||
if (n == -1)
|
||||
{
|
||||
if (io.getName() == HAWK_NULL && outfile != HAWK_NULL)
|
||||
{
|
||||
// if writing to outfile, set the error message
|
||||
// explicitly. other cases are handled by
|
||||
// the caller in sed.c.
|
||||
hawk_oocs_t ea;
|
||||
ea.ptr = (hawk_ooch_t*)outfile;
|
||||
ea.len = hawk_count_oocstr(outfile);
|
||||
((Sed*)io)->setError (HAWK_SED_EIOFIL, &ea);
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
SedStd::StringStream::StringStream (const hawk_ooch_t* in)
|
||||
{
|
||||
this->in.ptr = in;
|
||||
this->in.end = in + hawk_count_oocstr(in);
|
||||
this->out.inited = false;
|
||||
}
|
||||
|
||||
SedStd::StringStream::StringStream (const hawk_ooch_t* in, hawk_oow_t len)
|
||||
{
|
||||
this->in.ptr = in;
|
||||
this->in.end = in + len;
|
||||
this->out.inited = false;
|
||||
}
|
||||
|
||||
SedStd::StringStream::~StringStream ()
|
||||
{
|
||||
if (out.inited) hawk_ooecs_fini (&out.buf);
|
||||
}
|
||||
|
||||
int SedStd::StringStream::open (Data& io)
|
||||
{
|
||||
const hawk_ooch_t* ioname = io.getName ();
|
||||
|
||||
if (ioname == HAWK_NULL)
|
||||
{
|
||||
// open a main data stream
|
||||
if (io.getMode() == READ)
|
||||
{
|
||||
in.cur = in.ptr;
|
||||
io.setHandle ((void*)in.ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!out.inited)
|
||||
{
|
||||
if (hawk_ooecs_init(&out.buf, (hawk_gem_t*)io, 256) <= -1)
|
||||
{
|
||||
((Sed*)io)->setError (HAWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
out.inited = true;
|
||||
}
|
||||
|
||||
hawk_ooecs_clear (&out.buf);
|
||||
io.setHandle (&out.buf);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// open files for a r or w command
|
||||
hawk_sio_t* sio;
|
||||
int mode = (io.getMode() == READ)?
|
||||
HAWK_SIO_READ:
|
||||
(HAWK_SIO_WRITE|HAWK_SIO_CREATE|HAWK_SIO_TRUNCATE);
|
||||
|
||||
sio = hawk_sio_open((hawk_gem_t*)io, 0, ioname, mode);
|
||||
if (sio == HAWK_NULL) return -1;
|
||||
|
||||
io.setHandle (sio);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SedStd::StringStream::close (Data& io)
|
||||
{
|
||||
const void* handle = io.getHandle();
|
||||
if (handle != in.ptr && handle != &out.buf)
|
||||
hawk_sio_close ((hawk_sio_t*)handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hawk_ooi_t SedStd::StringStream::read (Data& io, hawk_ooch_t* buf, hawk_oow_t len)
|
||||
{
|
||||
const void* handle = io.getHandle();
|
||||
|
||||
if (len == (hawk_oow_t)-1) len--; // shrink buffer if too long
|
||||
if (handle == in.ptr)
|
||||
{
|
||||
hawk_oow_t n = 0;
|
||||
while (in.cur < in.end && n < len)
|
||||
buf[n++] = *in.cur++;
|
||||
return (hawk_ooi_t)n;
|
||||
}
|
||||
else
|
||||
{
|
||||
HAWK_ASSERT (handle != &out.buf);
|
||||
return hawk_sio_getoochars((hawk_sio_t*)handle, buf, len);
|
||||
}
|
||||
}
|
||||
|
||||
hawk_ooi_t SedStd::StringStream::write (Data& io, const hawk_ooch_t* data, hawk_oow_t len)
|
||||
{
|
||||
const void* handle = io.getHandle();
|
||||
|
||||
if (len == (hawk_oow_t)-1) len--; // shrink data if too long
|
||||
|
||||
if (handle == &out.buf)
|
||||
{
|
||||
if (hawk_ooecs_ncat(&out.buf, data, len) == (hawk_oow_t)-1)
|
||||
{
|
||||
((Sed*)io)->setError (HAWK_ENOMEM);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
else
|
||||
{
|
||||
HAWK_ASSERT (handle != in.ptr);
|
||||
return hawk_sio_putoochars((hawk_sio_t*)handle, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
const hawk_ooch_t* SedStd::StringStream::getInput (hawk_oow_t* len) const
|
||||
{
|
||||
if (len) *len = in.end - in.ptr;
|
||||
return in.ptr;
|
||||
}
|
||||
|
||||
const hawk_ooch_t* SedStd::StringStream::getOutput (hawk_oow_t* len) const
|
||||
{
|
||||
if (out.inited)
|
||||
{
|
||||
if (len) *len = HAWK_OOECS_LEN(&out.buf);
|
||||
return HAWK_OOECS_PTR(&out.buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (len) *len = 0;
|
||||
return HAWK_T("");
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
HAWK_END_NAMESPACE(HAWK)
|
||||
/////////////////////////////////
|
@ -24,7 +24,7 @@
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <HawkStd.hpp>
|
||||
#include <Hawk.hpp>
|
||||
#include <hawk-sio.h>
|
||||
#include <hawk-pio.h>
|
||||
#include <hawk-std.h> // for hawk_stdmodXXX() functions
|
@ -289,7 +289,7 @@ typedef hawk_ooi_t (*hawk_sed_io_impl_t) (
|
||||
hawk_sed_io_cmd_t cmd,
|
||||
hawk_sed_io_arg_t* arg,
|
||||
hawk_ooch_t* data,
|
||||
hawk_oow_t count
|
||||
hawk_oow_t count
|
||||
);
|
||||
|
||||
/**
|
||||
@ -493,11 +493,13 @@ static HAWK_INLINE hawk_gem_t* hawk_sed_getgem (hawk_sed_t* sed) { return &((haw
|
||||
*/
|
||||
static HAWK_INLINE hawk_mmgr_t* hawk_sed_getmmgr (hawk_sed_t* sed) { return ((hawk_sed_alt_t*)sed)->_gem.mmgr; }
|
||||
static HAWK_INLINE hawk_cmgr_t* hawk_sed_getcmgr (hawk_sed_t* sed) { return ((hawk_sed_alt_t*)sed)->_gem.cmgr; }
|
||||
static HAWK_INLINE void hawk_sed_setcmgr (hawk_sed_t* sed, hawk_cmgr_t* cmgr) { ((hawk_sed_alt_t*)sed)->_gem.cmgr = cmgr; }
|
||||
#else
|
||||
#define hawk_sed_getxtn(sed) ((void*)((hawk_uint8_t*)sed + ((hawk_sed_alt_t*)sed)->_instsize))
|
||||
#define hawk_sed_getgem(sed) (&((hawk_sed_alt_t*)(sed))->_gem)
|
||||
#define hawk_sed_getmmgr(sed) (((hawk_sed_alt_t*)(sed))->_gem.mmgr)
|
||||
#define hawk_sed_getcmgr(sed) (((hawk_sed_alt_t*)(sed))->_gem.cmgr)
|
||||
#define hawk_sed_setcmgr(sed,_cmgr) (((hawk_sed_alt_t*)(sed))->_gem.cmgr = (_cmgr))
|
||||
#endif /* HAWK_HAVE_INLINE */
|
||||
|
||||
|
||||
@ -535,6 +537,10 @@ HAWK_EXPORT int hawk_sed_setopt (
|
||||
const void* value
|
||||
);
|
||||
|
||||
HAWK_EXPORT hawk_errstr_t hawk_sed_geterrstr (
|
||||
hawk_sed_t* sed
|
||||
);
|
||||
|
||||
/**
|
||||
* The hawk_sed_geterrnum() function returns the number of the last error
|
||||
* occurred.
|
||||
@ -562,6 +568,12 @@ static HAWK_INLINE void hawk_sed_geterror (hawk_sed_t* sed, hawk_errnum_t* errnu
|
||||
#define hawk_sed_geterror(sed, errnum, errmsg, errloc) (hawk_gem_geterror(hawk_sed_getgem(sed), errnum, errmsg, errloc))
|
||||
#endif
|
||||
|
||||
#if defined(HAWK_OOCH_IS_BCH)
|
||||
# define hawk_sed_geterrmsg hawk_sed_geterrbmsg
|
||||
#else
|
||||
# define hawk_sed_geterrmsg hawk_sed_geterrumsg
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* The hawk_sed_seterrnum() function sets the error information omitting
|
||||
|
@ -145,6 +145,11 @@ void hawk_sed_fini (hawk_sed_t* sed)
|
||||
hawk_ooecs_fini (&sed->tmp.rex);
|
||||
}
|
||||
|
||||
hawk_errstr_t hawk_sed_geterrstr (hawk_sed_t* sed)
|
||||
{
|
||||
return sed->_gem.errstr;
|
||||
}
|
||||
|
||||
void hawk_sed_seterrbfmt (hawk_sed_t* sed, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_bch_t* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
@ -24,7 +24,7 @@
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <HawkStd.hpp>
|
||||
#include <Hawk.hpp>
|
||||
#include <hawk-utl.h>
|
||||
#include <hawk-fmt.h>
|
||||
#include <hawk-cli.h>
|
||||
|
Loading…
Reference in New Issue
Block a user