This commit is contained in:
2008-06-20 03:54:58 +00:00
parent 8f15f888d9
commit 77d6c882d7
34 changed files with 6930 additions and 11 deletions

1109
ase/include/ase/awk/Awk.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,109 @@
/*
* $Id: StdAwk.hpp 195 2008-06-06 13:01:55Z baconevi $
*
* {License}
*/
#ifndef _ASE_AWK_STDAWK_HPP_
#define _ASE_AWK_STDAWK_HPP_
#include <ase/awk/Awk.hpp>
/////////////////////////////////
ASE_BEGIN_NAMESPACE(ASE)
/////////////////////////////////
/**
* Provides a more useful AWK interpreter by overriding primitive methods,
* the file handler, the pipe handler and implementing common AWK intrinsic
* functions.
*/
class StdAwk: public Awk
{
public:
StdAwk ();
int open ();
protected:
// intrinsic functions
int sin (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int cos (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int tan (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int atan (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int atan2 (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int log (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int exp (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int sqrt (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int fnint (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int rand (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int srand (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int systime (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int strftime (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int strfgmtime (Run& run, Return& ret, const Argument* args, size_t nargs,
const char_t* name, size_t len);
int system (Run& run, 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);
// 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);
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);
protected:
unsigned int seed;
};
/////////////////////////////////
ASE_END_NAMESPACE(ASE)
/////////////////////////////////
#endif

674
ase/include/ase/awk/awk.h Normal file
View File

@ -0,0 +1,674 @@
/*
* $Id: awk.h 196 2008-06-08 14:04:16Z baconevi $
*
* {License}
*/
#ifndef _ASE_AWK_AWK_H_
#define _ASE_AWK_AWK_H_
/**
* @file awk.h
* @brief Define an AWK interpreter
*
* This file defines most of the data types and functions required to embed
* a AWK interpreter engine.
*/
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
#include <ase/cmn/map.h>
typedef struct ase_awk_t ase_awk_t;
typedef struct ase_awk_run_t ase_awk_run_t;
typedef struct ase_awk_val_t ase_awk_val_t;
typedef struct ase_awk_extio_t ase_awk_extio_t;
typedef struct ase_awk_prmfns_t ase_awk_prmfns_t;
typedef struct ase_awk_srcios_t ase_awk_srcios_t;
typedef struct ase_awk_runios_t ase_awk_runios_t;
typedef struct ase_awk_runcbs_t ase_awk_runcbs_t;
typedef struct ase_awk_runarg_t ase_awk_runarg_t;
typedef struct ase_awk_rexfns_t ase_awk_rexfns_t;
typedef ase_real_t (*ase_awk_pow_t) (void* custom, ase_real_t x, ase_real_t y);
typedef int (*ase_awk_sprintf_t) (
void* custom, ase_char_t* buf, ase_size_t size,
const ase_char_t* fmt, ...);
typedef void (*ase_awk_dprintf_t) (void* custom, const ase_char_t* fmt, ...);
typedef ase_ssize_t (*ase_awk_io_t) (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
struct ase_awk_extio_t
{
ase_awk_run_t* run; /* [IN] */
int type; /* [IN] console, file, coproc, pipe */
int mode; /* [IN] read, write, etc */
ase_char_t* name; /* [IN] */
void* custom_data; /* [IN] */
void* handle; /* [OUT] */
/* input */
struct
{
ase_char_t buf[2048];
ase_size_t pos;
ase_size_t len;
ase_bool_t eof;
ase_bool_t eos;
} in;
/* output */
struct
{
ase_bool_t eof;
ase_bool_t eos;
} out;
ase_awk_extio_t* next;
};
struct ase_awk_prmfns_t
{
ase_mmgr_t mmgr;
ase_ccls_t ccls;
struct
{
/* utilities */
ase_awk_pow_t pow; /* required */
ase_awk_sprintf_t sprintf; /* required */
ase_awk_dprintf_t dprintf; /* required in the debug mode */
/* user-defined data passed to the functions above */
void* custom_data; /* optional */
} misc;
};
struct ase_awk_srcios_t
{
ase_awk_io_t in;
ase_awk_io_t out;
void* custom_data;
};
struct ase_awk_runios_t
{
ase_awk_io_t pipe;
ase_awk_io_t coproc;
ase_awk_io_t file;
ase_awk_io_t console;
void* custom_data;
};
struct ase_awk_runcbs_t
{
void (*on_start) (
ase_awk_run_t* run, void* custom_data);
void (*on_statement) (
ase_awk_run_t* run, ase_size_t line, void* custom_data);
void (*on_return) (
ase_awk_run_t* run, ase_awk_val_t* ret, void* custom_data);
void (*on_end) (
ase_awk_run_t* run, int errnum, void* custom_data);
void* custom_data;
};
struct ase_awk_runarg_t
{
ase_char_t* ptr;
ase_size_t len;
};
struct ase_awk_rexfns_t
{
void* (*build) (
ase_awk_t* awk, const ase_char_t* ptn,
ase_size_t len, int* errnum);
int (*match) (
ase_awk_t* awk, void* code, int option,
const ase_char_t* str, ase_size_t len,
const ase_char_t** mptr, ase_size_t* mlen,
int* errnum);
void (*free) (ase_awk_t* awk, void* code);
ase_bool_t (*isempty) (ase_awk_t* awk, void* code);
};
/* io function commands */
enum ase_awk_iocmd_t
{
ASE_AWK_IO_OPEN = 0,
ASE_AWK_IO_CLOSE = 1,
ASE_AWK_IO_READ = 2,
ASE_AWK_IO_WRITE = 3,
ASE_AWK_IO_FLUSH = 4,
ASE_AWK_IO_NEXT = 5
};
/* various options */
enum ase_awk_option_t
{
/* allow undeclared variables and implicit concatenation */
ASE_AWK_IMPLICIT = (1 << 0),
/* allow explicit variable declaration, the concatenation
* operator(.), and a parse-time function check. */
ASE_AWK_EXPLICIT = (1 << 1),
/* a function name should not coincide to be a variable name */
/*ASE_AWK_UNIQUEFN = (1 << 2),*/
/* allow variable shading */
/*ASE_AWK_SHADING = (1 << 3),*/
/* support shift operators */
ASE_AWK_SHIFT = (1 << 4),
/* enable the idiv operator (double slashes) */
ASE_AWK_IDIV = (1 << 5),
/* support string concatenation in tokenization.
* this option can change the behavior of a certain construct.
* getline < "abc" ".def" is treated as if it is getline < "abc.def"
* when this option is on. If this option is off, the same expression
* is treated as if it is (getline < "abc") ".def". */
ASE_AWK_STRCONCAT = (1 << 6),
/* support getline and print */
ASE_AWK_EXTIO = (1 << 7),
/* support co-process - NOT IMPLEMENTED YET */
ASE_AWK_COPROC = (1 << 8),
/* support blockless patterns */
ASE_AWK_BLOCKLESS = (1 << 9),
/* use 1 as the start index for string operations and ARGV */
ASE_AWK_BASEONE = (1 << 10),
/* strip off leading and trailing spaces when splitting a record
* into fields with a regular expression.
*
* Consider the following program.
* BEGIN { FS="[:[:space:]]+"; }
* {
* print "NF=" NF;
* for (i = 0; i < NF; i++) print i " [" $(i+1) "]";
* }
*
* The program splits " a b c " into [a], [b], [c] when this
* option is on while into [], [a], [b], [c], [] when it is off.
*/
ASE_AWK_STRIPSPACES = (1 << 11),
/* enable the nextoutfile keyword */
ASE_AWK_NEXTOFILE = (1 << 12),
/* cr + lf by default */
ASE_AWK_CRLF = (1 << 13),
/* pass the arguments to the main function */
ASE_AWK_ARGSTOMAIN = (1 << 14),
/* enable the non-standard keyworkd reset */
ASE_AWK_RESET = (1 << 15),
/* allows the assignment of a map value to a variable */
ASE_AWK_MAPTOVAR = (1 << 16),
/* allows BEGIN, END, pattern-action blocks */
ASE_AWK_PABLOCK = (1 << 17)
};
/* error code */
enum ase_awk_errnum_t
{
ASE_AWK_ENOERR, /* no error */
ASE_AWK_ECUSTOM, /* custom error */
ASE_AWK_EINVAL, /* invalid parameter or data */
ASE_AWK_ENOMEM, /* out of memory */
ASE_AWK_ENOSUP, /* not supported */
ASE_AWK_ENOPER, /* operation not allowed */
ASE_AWK_ENODEV, /* no such device */
ASE_AWK_ENOSPC, /* no space left on device */
ASE_AWK_EMFILE, /* too many open files */
ASE_AWK_EMLINK, /* too many links */
ASE_AWK_EAGAIN, /* resource temporarily unavailable */
ASE_AWK_ENOENT, /* "'%.*s' not existing */
ASE_AWK_EEXIST, /* file or data exists */
ASE_AWK_EFTBIG, /* file or data too big */
ASE_AWK_ETBUSY, /* system too busy */
ASE_AWK_EISDIR, /* is a directory */
ASE_AWK_EIOERR, /* i/o error */
ASE_AWK_EOPEN, /* cannot open */
ASE_AWK_EREAD, /* cannot read */
ASE_AWK_EWRITE, /* cannot write */
ASE_AWK_ECLOSE, /* cannot close */
ASE_AWK_EINTERN, /* internal error */
ASE_AWK_ERUNTIME, /* run-time error */
ASE_AWK_EBLKNST, /* blocke nested too deeply */
ASE_AWK_EEXPRNST, /* expression nested too deeply */
ASE_AWK_ESINOP,
ASE_AWK_ESINCL,
ASE_AWK_ESINRD,
ASE_AWK_ESOUTOP,
ASE_AWK_ESOUTCL,
ASE_AWK_ESOUTWR,
ASE_AWK_ELXCHR, /* lexer came accross an wrong character */
ASE_AWK_ELXDIG, /* invalid digit */
ASE_AWK_ELXUNG, /* lexer failed to unget a character */
ASE_AWK_EENDSRC, /* unexpected end of source */
ASE_AWK_EENDCMT, /* a comment not closed properly */
ASE_AWK_EENDSTR, /* a string not closed with a quote */
ASE_AWK_EENDREX, /* unexpected end of a regular expression */
ASE_AWK_ELBRACE, /* left brace expected */
ASE_AWK_ELPAREN, /* left parenthesis expected */
ASE_AWK_ERPAREN, /* right parenthesis expected */
ASE_AWK_ERBRACK, /* right bracket expected */
ASE_AWK_ECOMMA, /* comma expected */
ASE_AWK_ESCOLON, /* semicolon expected */
ASE_AWK_ECOLON, /* colon expected */
ASE_AWK_ESTMEND, /* statement not ending with a semicolon */
ASE_AWK_EIN, /* keyword 'in' is expected */
ASE_AWK_ENOTVAR, /* not a variable name after 'in' */
ASE_AWK_EEXPRES, /* expression expected */
ASE_AWK_EFUNC, /* keyword 'func' is expected */
ASE_AWK_EWHILE, /* keyword 'while' is expected */
ASE_AWK_EASSIGN, /* assignment statement expected */
ASE_AWK_EIDENT, /* identifier expected */
ASE_AWK_EFNNAME, /* not a valid function name */
ASE_AWK_EBLKBEG, /* BEGIN requires an action block */
ASE_AWK_EBLKEND, /* END requires an action block */
ASE_AWK_EDUPBEG, /* duplicate BEGIN */
ASE_AWK_EDUPEND, /* duplicate END */
ASE_AWK_EBFNRED, /* intrinsic function redefined */
ASE_AWK_EAFNRED, /* function redefined */
ASE_AWK_EGBLRED, /* global variable redefined */
ASE_AWK_EPARRED, /* parameter redefined */
ASE_AWK_EVARRED, /* named variable redefined */
ASE_AWK_EDUPPAR, /* duplicate parameter name */
ASE_AWK_EDUPGBL, /* duplicate global variable name */
ASE_AWK_EDUPLCL, /* duplicate local variable name */
ASE_AWK_EBADPAR, /* not a valid parameter name */
ASE_AWK_EBADVAR, /* not a valid variable name */
ASE_AWK_EUNDEF, /* undefined identifier */
ASE_AWK_ELVALUE, /* l-value required */
ASE_AWK_EGBLTM, /* too many global variables */
ASE_AWK_ELCLTM, /* too many local variables */
ASE_AWK_EPARTM, /* too many parameters */
ASE_AWK_EDELETE, /* delete not followed by a variable */
ASE_AWK_ERESET, /* reset not followed by a variable */
ASE_AWK_EBREAK, /* break outside a loop */
ASE_AWK_ECONTINUE, /* continue outside a loop */
ASE_AWK_ENEXTBEG, /* next illegal in BEGIN block */
ASE_AWK_ENEXTEND, /* next illegal in END block */
ASE_AWK_ENEXTFBEG, /* nextfile illegal in BEGIN block */
ASE_AWK_ENEXTFEND, /* nextfile illegal in END block */
ASE_AWK_EPRINTFARG, /* printf not followed by any arguments */
ASE_AWK_EPREPST, /* both prefix and postfix increment/decrement
operator present */
ASE_AWK_EGLNCPS, /* coprocess not supported by getline */
/* run time error */
ASE_AWK_EDIVBY0, /* divide by zero */
ASE_AWK_EOPERAND, /* invalid operand */
ASE_AWK_EPOSIDX, /* wrong position index */
ASE_AWK_EARGTF, /* too few arguments */
ASE_AWK_EARGTM, /* too many arguments */
ASE_AWK_EFNNONE, /* "function '%.*s' not found" */
ASE_AWK_ENOTIDX, /* variable not indexable */
ASE_AWK_ENOTDEL, /* variable not deletable */
ASE_AWK_ENOTMAP, /* value not a map */
ASE_AWK_ENOTMAPIN, /* right-hand side of 'in' not a map */
ASE_AWK_ENOTMAPNILIN, /* right-hand side of 'in' not a map nor nil */
ASE_AWK_ENOTREF, /* value not referenceable */
ASE_AWK_ENOTASS, /* value not assignable */
ASE_AWK_EIDXVALASSMAP, /* indexed value cannot be assigned a map */
ASE_AWK_EPOSVALASSMAP, /* a positional cannot be assigned a map */
ASE_AWK_EMAPTOSCALAR, /* cannot change a map to a scalar value */
ASE_AWK_ESCALARTOMAP, /* cannot change a scalar value to a map */
ASE_AWK_EMAPNOTALLOWED, /* a map is not allowed */
ASE_AWK_EVALTYPE, /* wrong value type */
ASE_AWK_ERDELETE, /* delete called with a wrong target */
ASE_AWK_ERRESET, /* reset called with a wrong target */
ASE_AWK_ERNEXTBEG, /* next called from BEGIN */
ASE_AWK_ERNEXTEND, /* next called from END */
ASE_AWK_ERNEXTFBEG, /* nextfile called from BEGIN */
ASE_AWK_ERNEXTFEND, /* nextfile called from END */
ASE_AWK_EBFNUSER, /* wrong intrinsic function implementation */
ASE_AWK_EBFNIMPL, /* intrinsic function handler failed */
ASE_AWK_EIOUSER, /* wrong user io handler implementation */
ASE_AWK_EIONONE, /* no such io name found */
ASE_AWK_EIOIMPL, /* i/o callback returned an error */
ASE_AWK_EIONMEM, /* i/o name empty */
ASE_AWK_EIONMNL, /* i/o name contains '\0' */
ASE_AWK_EFMTARG, /* arguments to format string not sufficient */
ASE_AWK_EFMTCNV, /* recursion detected in format conversion */
ASE_AWK_ECONVFMTCHR, /* an invalid character found in CONVFMT */
ASE_AWK_EOFMTCHR, /* an invalid character found in OFMT */
/* regular expression error */
ASE_AWK_EREXRECUR, /* recursion too deep */
ASE_AWK_EREXRPAREN, /* a right parenthesis is expected */
ASE_AWK_EREXRBRACKET, /* a right bracket is expected */
ASE_AWK_EREXRBRACE, /* a right brace is expected */
ASE_AWK_EREXUNBALPAR, /* unbalanced parenthesis */
ASE_AWK_EREXCOLON, /* a colon is expected */
ASE_AWK_EREXCRANGE, /* invalid character range */
ASE_AWK_EREXCCLASS, /* invalid character class */
ASE_AWK_EREXBRANGE, /* invalid boundary range */
ASE_AWK_EREXEND, /* unexpected end of the pattern */
ASE_AWK_EREXGARBAGE, /* garbage after the pattern */
/* the number of error numbers, internal use only */
ASE_AWK_NUMERRNUM
};
/* depth types */
enum ase_awk_depth_t
{
ASE_AWK_DEPTH_BLOCK_PARSE = (1 << 0),
ASE_AWK_DEPTH_BLOCK_RUN = (1 << 1),
ASE_AWK_DEPTH_EXPR_PARSE = (1 << 2),
ASE_AWK_DEPTH_EXPR_RUN = (1 << 3),
ASE_AWK_DEPTH_REX_BUILD = (1 << 4),
ASE_AWK_DEPTH_REX_MATCH = (1 << 5)
};
/* extio types */
enum ase_awk_extio_type_t
{
/* extio types available */
ASE_AWK_EXTIO_PIPE,
ASE_AWK_EXTIO_COPROC,
ASE_AWK_EXTIO_FILE,
ASE_AWK_EXTIO_CONSOLE,
/* reserved for internal use only */
ASE_AWK_EXTIO_NUM
};
enum ase_awk_extio_mode_t
{
ASE_AWK_EXTIO_PIPE_READ = 0,
ASE_AWK_EXTIO_PIPE_WRITE = 1,
/*
ASE_AWK_EXTIO_COPROC_READ = 0,
ASE_AWK_EXTIO_COPROC_WRITE = 1,
ASE_AWK_EXTIO_COPROC_RDWR = 2,
*/
ASE_AWK_EXTIO_FILE_READ = 0,
ASE_AWK_EXTIO_FILE_WRITE = 1,
ASE_AWK_EXTIO_FILE_APPEND = 2,
ASE_AWK_EXTIO_CONSOLE_READ = 0,
ASE_AWK_EXTIO_CONSOLE_WRITE = 1
};
enum ase_awk_global_id_t
{
/* this table should match gtab in parse.c.
* in addition, ase_awk_setglobal also counts
* on the order of these values */
ASE_AWK_GLOBAL_ARGC,
ASE_AWK_GLOBAL_ARGV,
ASE_AWK_GLOBAL_CONVFMT,
ASE_AWK_GLOBAL_FILENAME,
ASE_AWK_GLOBAL_FNR,
ASE_AWK_GLOBAL_FS,
ASE_AWK_GLOBAL_IGNORECASE,
ASE_AWK_GLOBAL_NF,
ASE_AWK_GLOBAL_NR,
ASE_AWK_GLOBAL_OFILENAME,
ASE_AWK_GLOBAL_OFMT,
ASE_AWK_GLOBAL_OFS,
ASE_AWK_GLOBAL_ORS,
ASE_AWK_GLOBAL_RLENGTH,
ASE_AWK_GLOBAL_RS,
ASE_AWK_GLOBAL_RSTART,
ASE_AWK_GLOBAL_SUBSEP,
/* these are not not the actual IDs and are used internally only
* Make sure you update these values properly if you add more
* ID definitions, however */
ASE_AWK_MIN_GLOBAL_ID = ASE_AWK_GLOBAL_ARGC,
ASE_AWK_MAX_GLOBAL_ID = ASE_AWK_GLOBAL_SUBSEP
};
#ifdef __cplusplus
extern "C" {
#endif
ase_awk_t* ase_awk_open (const ase_awk_prmfns_t* prmfns, void* custom_data);
int ase_awk_close (ase_awk_t* awk);
int ase_awk_clear (ase_awk_t* awk);
ase_mmgr_t* ase_awk_getmmgr (ase_awk_t* awk);
void* ase_awk_getcustomdata (ase_awk_t* awk);
const ase_char_t* ase_awk_geterrstr (ase_awk_t* awk, int num);
int ase_awk_seterrstr (ase_awk_t* awk, int num, const ase_char_t* str);
int ase_awk_geterrnum (ase_awk_t* awk);
ase_size_t ase_awk_geterrlin (ase_awk_t* awk);
const ase_char_t* ase_awk_geterrmsg (ase_awk_t* awk);
void ase_awk_seterrnum (ase_awk_t* awk, int errnum);
void ase_awk_seterrmsg (ase_awk_t* awk,
int errnum, ase_size_t errlin, const ase_char_t* errmsg);
void ase_awk_geterror (
ase_awk_t* awk, int* errnum,
ase_size_t* errlin, const ase_char_t** errmsg);
void ase_awk_seterror (
ase_awk_t* awk, int errnum, ase_size_t errlin,
const ase_cstr_t* errarg, ase_size_t argcnt);
int ase_awk_getoption (ase_awk_t* awk);
void ase_awk_setoption (ase_awk_t* awk, int opt);
ase_size_t ase_awk_getmaxdepth (ase_awk_t* awk, int type);
void ase_awk_setmaxdepth (ase_awk_t* awk, int types, ase_size_t depth);
int ase_awk_getword (ase_awk_t* awk,
const ase_char_t* okw, ase_size_t olen,
const ase_char_t** nkw, ase_size_t* nlen);
/**
* Enables replacement of a name of a keyword, intrinsic global variables,
* and intrinsic functions.
*
* If nkw is ASE_NULL or nlen is zero and okw is ASE_NULL or olen is zero,
* it unsets all word replacements. If nkw is ASE_NULL or nlen is zero,
* it unsets the replacement for okw and olen. If all of them are valid,
* it sets the word replace for okw and olen to nkw and nlen.
*
* @return
* On success, 0 is returned.
* On failure, -1 is returned.
*/
int ase_awk_setword (ase_awk_t* awk,
const ase_char_t* okw, ase_size_t olen,
const ase_char_t* nkw, ase_size_t nlen);
/**
* Sets the customized regular processing routine.
*
* @return
* On success, 0 is returned.
* On failure, -1 is returned.
*/
int ase_awk_setrexfns (ase_awk_t* awk, ase_awk_rexfns_t* rexfns);
/**
* Adds an intrinsic global variable.
*
* @return
* On success, the ID of the global variable added is returned.
* On failure, -1 is returned.
*/
int ase_awk_addglobal (ase_awk_t* awk, const ase_char_t* name, ase_size_t len);
/**
* Deletes a instrinsic global variable.
*
* @return
* On success, 0 is returned.
* On failure, -1 is returned.
*/
int ase_awk_delglobal (ase_awk_t* awk, const ase_char_t* name, ase_size_t len);
/**
* Parses the source code
*
* @return
* On success, 0 is returned.
* On failure, -1 is returned.
*/
int ase_awk_parse (ase_awk_t* awk, ase_awk_srcios_t* srcios);
/**
* Executes a parsed program.
*
* ase_awk_run returns 0 on success and -1 on failure, generally speaking.
* A runtime context is required for it to start running the program.
* Once the runtime context is created, the program starts to run.
* The context creation failure is reported by the return value -1 of
* this function. however, the runtime error after the context creation
* is reported differently depending on the use of the callback.
* When no callback is specified (i.e. runcbs is ASE_NULL), ase_awk_run
* returns -1 on an error and awk->errnum is set accordingly.
* However, if a callback is specified (i.e. runcbs is not ASE_NULL),
* ase_awk_run returns 0 on both success and failure. Instead, the
* on_end handler of the callback is triggered with the relevant
* error number. The third parameter to on_end denotes this error number.
*/
int ase_awk_run (
ase_awk_t* awk, const ase_char_t* main,
ase_awk_runios_t* runios, ase_awk_runcbs_t* runcbs,
ase_awk_runarg_t* runarg, void* custom_data);
void ase_awk_stop (ase_awk_run_t* run);
void ase_awk_stopall (ase_awk_t* awk);
ase_bool_t ase_awk_isstop (ase_awk_run_t* run);
/**
* Gets the number of arguments passed to ase_awk_run
*/
ase_size_t ase_awk_getnargs (ase_awk_run_t* run);
/**
* Gets an argument passed to ase_awk_run
*/
ase_awk_val_t* ase_awk_getarg (ase_awk_run_t* run, ase_size_t idx);
/**
* Gets the value of a global variable.
*
* @param run A run-time context
* @param id The ID to a global variable.
* This value correspondsto the predefined global variable IDs or
* the value returned by ase_awk_addglobal.
* @return
* The pointer to a value is returned. This function never fails
* so long as id is valid. Otherwise, you may fall into trouble.
*/
ase_awk_val_t* ase_awk_getglobal (ase_awk_run_t* run, int id);
int ase_awk_setglobal (ase_awk_run_t* run, int id, ase_awk_val_t* val);
/**
* Sets the return value of a function from within a function handler.
*
* @param run A run-time context
* @param val A pointer to the value to set.
* ase_awk_refupval and ase_awk_refdownval are not needed because
* ase_awk_setretval never fails and it updates the reference count
* of the value properly.
*/
void ase_awk_setretval (ase_awk_run_t* run, ase_awk_val_t* val);
int ase_awk_setfilename (
ase_awk_run_t* run, const ase_char_t* name, ase_size_t len);
int ase_awk_setofilename (
ase_awk_run_t* run, const ase_char_t* name, ase_size_t len);
ase_awk_t* ase_awk_getrunawk (ase_awk_run_t* awk);
void* ase_awk_getruncustomdata (ase_awk_run_t* awk);
ase_map_t* ase_awk_getrunnamedvarmap (ase_awk_run_t* awk);
/* functions to manipulate the run-time error */
int ase_awk_getrunerrnum (ase_awk_run_t* run);
ase_size_t ase_awk_getrunerrlin (ase_awk_run_t* run);
const ase_char_t* ase_awk_getrunerrmsg (ase_awk_run_t* run);
void ase_awk_setrunerrnum (ase_awk_run_t* run, int errnum);
void ase_awk_setrunerrmsg (ase_awk_run_t* run,
int errnum, ase_size_t errlin, const ase_char_t* errmsg);
void ase_awk_getrunerror (
ase_awk_run_t* run, int* errnum,
ase_size_t* errlin, const ase_char_t** errmsg);
void ase_awk_setrunerror (
ase_awk_run_t* run, int errnum, ase_size_t errlin,
const ase_cstr_t* errarg, ase_size_t argcnt);
/* functions to manipulate intrinsic functions */
void* ase_awk_addfunc (
ase_awk_t* awk, const ase_char_t* name, ase_size_t name_len,
int when_valid, ase_size_t min_args, ase_size_t max_args,
const ase_char_t* arg_spec,
int (*handler)(ase_awk_run_t*,const ase_char_t*,ase_size_t));
int ase_awk_delfunc (
ase_awk_t* awk, const ase_char_t* name, ase_size_t name_len);
void ase_awk_clrbfn (ase_awk_t* awk);
/* record and field functions */
int ase_awk_clrrec (ase_awk_run_t* run, ase_bool_t skip_inrec_line);
int ase_awk_setrec (ase_awk_run_t* run, ase_size_t idx, const ase_char_t* str, ase_size_t len);
/* utility functions exported by awk.h */
void* ase_awk_malloc (ase_awk_t* awk, ase_size_t size);
void ase_awk_free (ase_awk_t* awk, void* ptr);
ase_char_t* ase_awk_strxdup (
ase_awk_t* awk, const ase_char_t* ptr, ase_size_t len);
ase_long_t ase_awk_strxtolong (
ase_awk_t* awk, const ase_char_t* str, ase_size_t len,
int base, const ase_char_t** endptr);
ase_real_t ase_awk_strxtoreal (
ase_awk_t* awk, const ase_char_t* str, ase_size_t len,
const ase_char_t** endptr);
ase_size_t ase_awk_longtostr (
ase_long_t value, int radix, const ase_char_t* prefix,
ase_char_t* buf, ase_size_t size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,5 @@
pkginclude_HEADERS = awk.h val.h
pkgincludedir= $(includedir)/ase/awk
CLEANFILES = *dist

213
ase/include/ase/awk/val.h Normal file
View File

@ -0,0 +1,213 @@
/*
* $Id: val.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_AWK_VAL_H_
#define _ASE_AWK_VAL_H_
#ifndef _ASE_AWK_AWK_H_
#error Include <ase/awk/awk.h> first
#endif
#include <ase/cmn/str.h>
enum ase_awk_val_type_t
{
/* the values between ASE_AWK_VAL_NIL and ASE_AWK_VAL_STR inclusive
* must be synchronized with an internal table of the __cmp_val
* function in run.c */
ASE_AWK_VAL_NIL = 0,
ASE_AWK_VAL_INT = 1,
ASE_AWK_VAL_REAL = 2,
ASE_AWK_VAL_STR = 3,
ASE_AWK_VAL_REX = 4,
ASE_AWK_VAL_MAP = 5,
ASE_AWK_VAL_REF = 6
};
enum ase_awk_val_ref_id_t
{
/* keep these items in the same order as corresponding items
* in tree.h */
ASE_AWK_VAL_REF_NAMED,
ASE_AWK_VAL_REF_GLOBAL,
ASE_AWK_VAL_REF_LOCAL,
ASE_AWK_VAL_REF_ARG,
ASE_AWK_VAL_REF_NAMEDIDX,
ASE_AWK_VAL_REF_GLOBALIDX,
ASE_AWK_VAL_REF_LOCALIDX,
ASE_AWK_VAL_REF_ARGIDX,
ASE_AWK_VAL_REF_POS
};
enum ase_awk_valtostr_opt_t
{
ASE_AWK_VALTOSTR_CLEAR = (1 << 0),
ASE_AWK_VALTOSTR_FIXED = (1 << 1),/* this overrides CLEAR */
ASE_AWK_VALTOSTR_PRINT = (1 << 2)
};
typedef struct ase_awk_val_nil_t ase_awk_val_nil_t;
typedef struct ase_awk_val_int_t ase_awk_val_int_t;
typedef struct ase_awk_val_real_t ase_awk_val_real_t;
typedef struct ase_awk_val_str_t ase_awk_val_str_t;
typedef struct ase_awk_val_rex_t ase_awk_val_rex_t;
typedef struct ase_awk_val_map_t ase_awk_val_map_t;
typedef struct ase_awk_val_ref_t ase_awk_val_ref_t;
/* this is not a value. it is just a value holder */
typedef struct ase_awk_val_chunk_t ase_awk_val_chunk_t;
#if ASE_SIZEOF_INT == 2
#define ASE_AWK_VAL_HDR \
unsigned int type: 3; \
unsigned int ref: 13
#else
#define ASE_AWK_VAL_HDR \
unsigned int type: 3; \
unsigned int ref: 29
#endif
#ifndef ASE_AWK_NDE_INT_DEFINED
#define ASE_AWK_NDE_INT_DEFINED
typedef struct ase_awk_nde_int_t ase_awk_nde_int_t;
#endif
#ifndef ASE_AWK_NDE_REAL_DEFINED
#define ASE_AWK_NDE_REAL_DEFINED
typedef struct ase_awk_nde_real_t ase_awk_nde_real_t;
#endif
#define ASE_AWK_VAL_TYPE(x) ((x)->type)
struct ase_awk_val_t
{
ASE_AWK_VAL_HDR;
};
/* ASE_AWK_VAL_NIL */
struct ase_awk_val_nil_t
{
ASE_AWK_VAL_HDR;
};
/* ASE_AWK_VAL_INT */
struct ase_awk_val_int_t
{
ASE_AWK_VAL_HDR;
ase_long_t val;
ase_awk_nde_int_t* nde;
};
/* ASE_AWK_VAL_REAL */
struct ase_awk_val_real_t
{
ASE_AWK_VAL_HDR;
ase_real_t val;
ase_awk_nde_real_t* nde;
};
/* ASE_AWK_VAL_STR */
struct ase_awk_val_str_t
{
ASE_AWK_VAL_HDR;
ase_char_t* buf;
ase_size_t len;
};
/* ASE_AWK_VAL_REX */
struct ase_awk_val_rex_t
{
ASE_AWK_VAL_HDR;
ase_char_t* buf;
ase_size_t len;
void* code;
};
/* ASE_AWK_VAL_MAP */
struct ase_awk_val_map_t
{
ASE_AWK_VAL_HDR;
/* TODO: make val_map to array if the indices used are all
* integers switch to map dynamically once the
* non-integral index is seen.
*/
ase_map_t* map;
};
/* ASE_AWK_VAL_REF */
struct ase_awk_val_ref_t
{
ASE_AWK_VAL_HDR;
int id;
/* if id is ASE_AWK_VAL_REF_POS, adr holds an index of the
* positional variable. Otherwise, adr points to the value
* directly. */
ase_awk_val_t** adr;
};
#ifdef __cplusplus
extern "C" {
#endif
extern ase_awk_val_t* ase_awk_val_nil;
extern ase_awk_val_t* ase_awk_val_zls;
extern ase_awk_val_t* ase_awk_val_negone;
extern ase_awk_val_t* ase_awk_val_zero;
extern ase_awk_val_t* ase_awk_val_one;
ase_awk_val_t* ase_awk_makeintval (ase_awk_run_t* run, ase_long_t v);
ase_awk_val_t* ase_awk_makerealval (ase_awk_run_t* run, ase_real_t v);
ase_awk_val_t* ase_awk_makestrval0 (
ase_awk_run_t* run, const ase_char_t* str);
ase_awk_val_t* ase_awk_makestrval (
ase_awk_run_t* run, const ase_char_t* str, ase_size_t len);
ase_awk_val_t* ase_awk_makestrval_nodup (
ase_awk_run_t* run, ase_char_t* str, ase_size_t len);
ase_awk_val_t* ase_awk_makestrval2 (
ase_awk_run_t* run,
const ase_char_t* str1, ase_size_t len1,
const ase_char_t* str2, ase_size_t len2);
ase_awk_val_t* ase_awk_makerexval (
ase_awk_run_t* run, const ase_char_t* buf, ase_size_t len, void* code);
ase_awk_val_t* ase_awk_makemapval (ase_awk_run_t* run);
ase_awk_val_t* ase_awk_makerefval (
ase_awk_run_t* run, int id, ase_awk_val_t** adr);
ase_bool_t ase_awk_isstaticval (ase_awk_val_t* val);
void ase_awk_freeval (ase_awk_run_t* run, ase_awk_val_t* val, ase_bool_t cache);
void ase_awk_refupval (ase_awk_run_t* run, ase_awk_val_t* val);
void ase_awk_refdownval (ase_awk_run_t* run, ase_awk_val_t* val);
void ase_awk_refdownval_nofree (ase_awk_run_t* run, ase_awk_val_t* val);
void ase_awk_freevalchunk (ase_awk_run_t* run, ase_awk_val_chunk_t* chunk);
ase_bool_t ase_awk_valtobool (
ase_awk_run_t* run, ase_awk_val_t* val);
ase_char_t* ase_awk_valtostr (
ase_awk_run_t* run, ase_awk_val_t* val,
int opt, ase_str_t* buf, ase_size_t* len);
int ase_awk_valtonum (
ase_awk_run_t* run, ase_awk_val_t* v, ase_long_t* l, ase_real_t* r);
int ase_awk_strtonum (
ase_awk_run_t* run, const ase_char_t* ptr, ase_size_t len,
ase_long_t* l, ase_real_t* r);
void ase_awk_dprintval (ase_awk_run_t* run, ase_awk_val_t* val);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,64 @@
/*
* $Id: conf_msw.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
/*
Macro Meaning
_WIN64 A 64-bit platform.
_WIN32 A 32-bit platform. This value is also defined by the 64-bit
compiler for backward compatibility.
_WIN16 A 16-bit platform
The following macros are specific to the architecture.
Macro Meaning
_M_IA64 Intel Itanium Processor Family
_M_IX86 x86 platform
_M_X64 x64 platform
*/
#define ASE_ENDIAN_LITTLE
#define ASE_SIZEOF_CHAR 1
#define ASE_SIZEOF_SHORT 2
#define ASE_SIZEOF_INT 4
/*#ifdef _WIN64
#define ASE_SIZEOF_LONG 8
#else*/
#define ASE_SIZEOF_LONG 4
/*#endif*/
#if defined(__POCC__) || defined(__DMC__)
/* pelles c with no microsoft extension */
#define ASE_SIZEOF_LONG_LONG 8
#define ASE_SIZEOF___INT8 0
#define ASE_SIZEOF___INT16 0
#define ASE_SIZEOF___INT32 0
#define ASE_SIZEOF___INT64 0
#define ASE_SIZEOF___INT96 0
#define ASE_SIZEOF___INT128 0
#else
#define ASE_SIZEOF_LONG_LONG 0
#define ASE_SIZEOF___INT8 1
#define ASE_SIZEOF___INT16 2
#define ASE_SIZEOF___INT32 4
#define ASE_SIZEOF___INT64 8
#define ASE_SIZEOF___INT96 0
#define ASE_SIZEOF___INT128 0
#endif
#ifdef _WIN64
#define ASE_SIZEOF_VOID_P 8
#else
#define ASE_SIZEOF_VOID_P 4
#endif
#define ASE_SIZEOF_FLOAT 4
#define ASE_SIZEOF_DOUBLE 8
#define ASE_SIZEOF_LONG_DOUBLE 16
#define ASE_SIZEOF_WCHAR_T 2

View File

@ -0,0 +1,166 @@
/* inc/ase/cmn/conf_unx.h.in. Generated from configure.ac by autoheader. */
/* char is mchar */
#undef ASE_CHAR_IS_MCHAR
/* char is wchar */
#undef ASE_CHAR_IS_WCHAR
/* Big Endian */
#undef ASE_ENDIAN_BIG
/* Little Endian */
#undef ASE_ENDIAN_LITTLE
/* Unknown Endian */
#undef ASE_ENDIAN_UNKNOWN
/* long double */
#undef ASE_HAVE_LONG_DOUBLE
/* long long */
#undef ASE_HAVE_LONG_LONG
/* Description */
#undef ASE_SIZEOF_CHAR
/* Description */
#undef ASE_SIZEOF_DOUBLE
/* Description */
#undef ASE_SIZEOF_FLOAT
/* Description */
#undef ASE_SIZEOF_INT
/* Description */
#undef ASE_SIZEOF_LONG
/* Description */
#undef ASE_SIZEOF_LONG_DOUBLE
/* Description */
#undef ASE_SIZEOF_LONG_LONG
/* Description */
#undef ASE_SIZEOF_SHORT
/* Description */
#undef ASE_SIZEOF_VOID_P
/* Description */
#undef ASE_SIZEOF_WCHAR_T
/* Description */
#undef ASE_SIZEOF___INT128
/* Description */
#undef ASE_SIZEOF___INT16
/* Description */
#undef ASE_SIZEOF___INT32
/* Description */
#undef ASE_SIZEOF___INT64
/* Description */
#undef ASE_SIZEOF___INT8
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Name of package */
#undef PACKAGE
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* The size of `char', as computed by sizeof. */
#undef SIZEOF_CHAR
/* The size of `double', as computed by sizeof. */
#undef SIZEOF_DOUBLE
/* The size of `float', as computed by sizeof. */
#undef SIZEOF_FLOAT
/* The size of `int', as computed by sizeof. */
#undef SIZEOF_INT
/* The size of `long', as computed by sizeof. */
#undef SIZEOF_LONG
/* The size of `long double', as computed by sizeof. */
#undef SIZEOF_LONG_DOUBLE
/* The size of `long long', as computed by sizeof. */
#undef SIZEOF_LONG_LONG
/* The size of `short', as computed by sizeof. */
#undef SIZEOF_SHORT
/* The size of `void *', as computed by sizeof. */
#undef SIZEOF_VOID_P
/* The size of `wchar_t', as computed by sizeof. */
#undef SIZEOF_WCHAR_T
/* The size of `__int128', as computed by sizeof. */
#undef SIZEOF___INT128
/* The size of `__int16', as computed by sizeof. */
#undef SIZEOF___INT16
/* The size of `__int32', as computed by sizeof. */
#undef SIZEOF___INT32
/* The size of `__int64', as computed by sizeof. */
#undef SIZEOF___INT64
/* The size of `__int8', as computed by sizeof. */
#undef SIZEOF___INT8
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Version number of package */
#undef VERSION

View File

@ -0,0 +1,90 @@
/*
* $Id: conf_vms.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
/* all of vax, alpha, ia64 are in the little endian. */
#define ASE_ENDIAN_LITTLE
/*
* Refer to the chapter 3 of the following URL for the data sizes.
* http://h71000.www7.hp.com/commercial/c/docs/6180profile.html
*/
#define ASE_SIZEOF_CHAR 1
#define ASE_SIZEOF_SHORT 2
#define ASE_SIZEOF_INT 4
#define ASE_SIZEOF_LONG 4
#if defined(vax) || defined(__vax)
#define ASE_SIZEOF_LONG_LONG 0
#elif defined(alpha) || defined(__alpha)
#define ASE_SIZEOF_LONG_LONG 8
#elif defined(ia64) || defined(__ia64)
#define ASE_SIZEOF_LONG_LONG 8
#else
#define ASE_SIZEOF_LONG_LONG 0
#endif
#define ASE_SIZEOF___INT8 1
#define ASE_SIZEOF___INT16 2
#define ASE_SIZEOF___INT32 4
#if defined(vax) || defined(__vax)
#define ASE_SIZEOF___INT64 0
#elif defined(alpha) || defined(__alpha)
#define ASE_SIZEOF___INT64 8
#elif defined(ia64) || defined(__ia64)
#define ASE_SIZEOF___INT64 8
#else
#define ASE_SIZEOF___INT64 0
#endif
#define ASE_SIZEOF___INT96 0
#define ASE_SIZEOF___INT128 0
#if defined(vax) || defined(__vax)
#define ASE_SIZEOF_VOID_P 4
#elif defined(alpha) || defined(__alpha)
#if __INITIAL_POINTER_SIZE==64
#pragma pointer_size 64
#define ASE_SIZEOF_VOID_P 8
#elif __INITIAL_POINTER_SIZE==32
#pragma pointer_size 32
#define ASE_SIZEOF_VOID_P 4
#elif __INITIAL_POINTER_SIZE==0
#define ASE_SIZEOF_VOID_P 4
#else
#error "unsupported initial pointer size"
#endif
#elif defined(ia64) || defined(__ia64)
#if __INITIAL_POINTER_SIZE==64
#pragma pointer_size 64
#define ASE_SIZEOF_VOID_P 8
#elif __INITIAL_POINTER_SIZE==32
#pragma pointer_size 32
#define ASE_SIZEOF_VOID_P 4
#elif __INITIAL_POINTER_SIZE==0
#define ASE_SIZEOF_VOID_P 4
#else
#error "unsupported initial pointer size"
#endif
#else
#error "unsupported architecture"
#endif
#define ASE_SIZEOF_FLOAT 4
#define ASE_SIZEOF_DOUBLE 8
#if defined(vax) || defined(__vax)
#define ASE_SIZEOF_LONG_DOUBLE 8
#elif defined(alpha) || defined(__alpha)
#define ASE_SIZEOF_LONG_DOUBLE 16
#elif defined(ia64) || defined(__ia64)
#define ASE_SIZEOF_LONG_DOUBLE 16
#else
#define ASE_SIZEOF_LONG_DOUBLE 0
#endif
#define ASE_SIZEOF_WCHAR_T 4

View File

@ -0,0 +1,173 @@
/*
* $Id: macros.h 196 2008-06-08 14:04:16Z baconevi $
*
* {License}
*/
#ifndef _ASE_CMN_MACROS_H_
#define _ASE_CMN_MACROS_H_
/**
* @file macros.h
* @brief Define common macros
*
* This file defines various macros for assertion, memory allocation,
* data type manipulation, etc.
*/
#include <ase/cmn/types.h>
#ifdef __cplusplus
/*#define ASE_NULL ((ase_uint_t)0)*/
#define ASE_NULL (0)
#else
#define ASE_NULL ((void*)0)
#endif
#define ASE_CHAR_EOF ((ase_cint_t)-1)
#define ASE_SIZEOF(n) (sizeof(n))
#define ASE_COUNTOF(n) (sizeof(n)/sizeof(n[0]))
#define ASE_OFFSETOF(type,member) ((ase_size_t)&((type*)0)->member)
#define ASE_TYPE_IS_SIGNED(type) (((type)0) > ((type)-1))
#define ASE_TYPE_IS_UNSIGNED(type) (((type)0) < ((type)-1))
#define ASE_TYPE_SIGNED_MAX(type) \
((type)~((type)1 << (ASE_SIZEOF(type) * 8 - 1)))
#define ASE_TYPE_UNSIGNED_MAX(type) ((type)(~(type)0))
#define ASE_TYPE_SIGNED_MIN(type) \
((type)((type)1 << (ASE_SIZEOF(type) * 8 - 1)))
#define ASE_TYPE_UNSIGNED_MIN(type) ((type)0)
/**
* @define ASE_TYPE_MAX(type)
* @brief Get the maximum value \a type can hold
*/
#define ASE_TYPE_MAX(type) \
((ASE_TYPE_IS_SIGNED(type)? ASE_TYPE_SIGNED_MAX(type): ASE_TYPE_UNSIGNED_MAX(type)))
#define ASE_TYPE_MIN(type) \
((ASE_TYPE_IS_SIGNED(type)? ASE_TYPE_SIGNED_MIN(type): ASE_TYPE_UNSIGNED_MIN(type)))
#define ASE_IS_POWOF2(x) (((x) & ((x) - 1)) == 0)
#define ASE_SWAP(x,y,original_type,casting_type) \
do { \
x = (original_type)((casting_type)(x) ^ (casting_type)(y)); \
y = (original_type)((casting_type)(y) ^ (casting_type)(x)); \
x = (original_type)((casting_type)(x) ^ (casting_type)(y)); \
} while (0)
#define ASE_ABS(x) ((x) < 0? -(x): (x))
#define ASE_LOOP_CONTINUE(id) goto __loop_ ## id ## _begin__;
#define ASE_LOOP_BREAK(id) goto __loop_ ## id ## _end__;
#define ASE_LOOP_BEGIN(id) __loop_ ## id ## _begin__: {
#define ASE_LOOP_END(id) ASE_LOOP_CONTINUE(id) } __loop_ ## id ## _end__:;
#define ASE_REPEAT(n,blk) \
do { \
ase_size_t __ase_repeat_x1__ = (ase_size_t)(n); \
ase_size_t __ase_repeat_x2__ = __ase_repeat_x1__ >> 4; \
__ase_repeat_x1__ &= 15; \
while (__ase_repeat_x1__-- > 0) { blk; } \
while (__ase_repeat_x2__-- > 0) { \
blk; blk; blk; blk; blk; blk; blk; blk; \
blk; blk; blk; blk; blk; blk; blk; blk; \
} \
} while (0);
#define ASE_MQ_I(val) #val
#define ASE_MQ(val) ASE_MQ_I(val)
#define ASE_MC(ch) ((ase_mchar_t)ch)
#define ASE_MS(str) ((const ase_mchar_t*)str)
#define ASE_MT(txt) (txt)
#define ASE_WQ_I(val) (L ## #val)
#define ASE_WQ(val) ASE_WQ_I(val)
#define ASE_WC(ch) ((ase_wchar_t)L ## ch)
#define ASE_WS(str) ((const ase_wchar_t*)L ## str)
#define ASE_WT(txt) (L ## txt)
#if defined(ASE_CHAR_IS_MCHAR)
#define ASE_Q(val) ASE_MQ(val)
#define ASE_C(ch) ASE_MC(ch)
#define ASE_S(str) ASE_MS(str)
#define ASE_T(txt) ASE_MT(txt)
#else
#define ASE_Q(val) ASE_WQ(val)
#define ASE_C(ch) ASE_WC(ch)
#define ASE_S(str) ASE_WS(str)
#define ASE_T(txt) ASE_WT(txt)
#endif
#if defined(__GNUC__)
#define ASE_BEGIN_PACKED_STRUCT(x) struct x {
#define ASE_END_PACKED_STRUCT() } __attribute__((packed));
#else
#define ASE_BEGIN_PACKED_STRUCT(x) struct x {
#define ASE_END_PACKED_STRUCT() };
#endif
#ifdef NDEBUG
#define ASE_ASSERT(expr) ((void)0)
#define ASE_ASSERTX(expr,desc) ((void)0)
#else
#ifdef __cplusplus
extern "C" {
#endif
void ase_assert_failed (
const ase_char_t* expr, const ase_char_t* desc,
const ase_char_t* file, ase_size_t line);
#ifdef __cplusplus
}
#endif
#define ASE_ASSERT(expr) (void)((expr) || \
(ase_assert_failed (ASE_T(#expr), ASE_NULL, ASE_T(__FILE__), __LINE__), 0))
#define ASE_ASSERTX(expr,desc) (void)((expr) || \
(ase_assert_failed (ASE_T(#expr), ASE_T(desc), ASE_T(__FILE__), __LINE__), 0))
#endif
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
#include <stdlib.h>
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define ASE_MALLOC(mmgr,size) malloc (size)
#define ASE_REALLOC(mmgr,ptr,size) realloc (ptr, size)
#define ASE_FREE(mmgr,ptr) free (ptr)
#else
#define ASE_MALLOC(mmgr,size) \
(mmgr)->malloc((mmgr)->custom_data, size)
#define ASE_REALLOC(mmgr,ptr,size) \
(mmgr)->realloc((mmgr)->custom_data, ptr, size)
#define ASE_FREE(mmgr,ptr) \
(mmgr)->free((mmgr)->custom_data, ptr)
#endif
#define ASE_ISUPPER(ccls,c) (ccls)->is_upper((ccls)->custom_data,c)
#define ASE_ISLOWER(ccls,c) (ccls)->is_lower((ccls)->custom_data,c)
#define ASE_ISALPHA(ccls,c) (ccls)->is_alpha((ccls)->custom_data,c)
#define ASE_ISDIGIT(ccls,c) (ccls)->is_digit((ccls)->custom_data,c)
#define ASE_ISXDIGIT(ccls,c) (ccls)->is_xdigit((ccls)->custom_data,c)
#define ASE_ISALNUM(ccls,c) (ccls)->is_alnum((ccls)->custom_data,c)
#define ASE_ISSPACE(ccls,c) (ccls)->is_space((ccls)->custom_data,c)
#define ASE_ISPRINT(ccls,c) (ccls)->is_print((ccls)->custom_data,c)
#define ASE_ISGRAPH(ccls,c) (ccls)->is_graph((ccls)->custom_data,c)
#define ASE_ISCNTRL(ccls,c) (ccls)->is_cntrl((ccls)->custom_data,c)
#define ASE_ISPUNCT(ccls,c) (ccls)->is_punct((ccls)->custom_data,c)
#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

View File

@ -0,0 +1,7 @@
pkginclude_HEADERS = conf_msw.h conf_unx.h conf_vms.h \
macros.h map.h mem.h pack.h rex.h str.h types.h unpack.h
pkgincludedir= $(includedir)/ase/cmn
CLEANFILES = *dist

134
ase/include/ase/cmn/map.h Normal file
View File

@ -0,0 +1,134 @@
/*
* $Id: map.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_CMN_MAP_H_
#define _ASE_CMN_MAP_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
typedef struct ase_map_t ase_map_t;
typedef struct ase_pair_t ase_pair_t;
struct ase_pair_t
{
struct
{
ase_char_t* ptr;
ase_size_t len;
} key;
void* val;
/* used internally */
ase_pair_t* next;
};
struct ase_map_t
{
/* map owner. passed to freeval and sameval as the first argument */
void* owner;
ase_size_t size;
ase_size_t capa;
unsigned int factor;
ase_size_t threshold;
ase_pair_t** buck;
void (*freeval) (void* owner,void* val);
void (*sameval) (void* owner,void* val);
/* the mmgr pointed at by mmgr should be valid
* while the map is alive as the contents are
* not replicated */
ase_mmgr_t* mmgr;
/* list of free pairs */
/*ase_pair_t* fp;*/
};
#define ASE_PAIR_KEYPTR(p) ((p)->key.ptr)
#define ASE_PAIR_KEYLEN(p) ((p)->key.len)
#define ASE_PAIR_VAL(p) ((p)->val)
#define ASE_PAIR_LNK(p) ((p)->next)
#ifdef __cplusplus
extern "C" {
#endif
/**
* Creates a hashed map with a dynamic array bucket and a list of values linked
*
* @param owner [in]
* @param capa [in]
* @param factor [in]
* @param freeval [in]
* @param sameval [in]
* @param mmgr [in]
*/
ase_map_t* ase_map_open (
void* owner, ase_size_t capa, unsigned int factor,
void(*freeval)(void*,void*), void(*sameval)(void*,void*),
ase_mmgr_t* mmgr);
/**
* Destroys a hashed map
*/
void ase_map_close (ase_map_t* map);
void ase_map_clear (ase_map_t* map);
ase_size_t ase_map_getsize (ase_map_t* map);
ase_pair_t* ase_map_get (
ase_map_t* map, const ase_char_t* keyptr, ase_size_t keylen);
ase_pair_t* ase_map_put (
ase_map_t* map, const ase_char_t* keyptr, ase_size_t keylen,
void* val);
int ase_map_putx (
ase_map_t* map, const ase_char_t* keyptr, ase_size_t keylen,
void* val, ase_pair_t** px);
ase_pair_t* ase_map_set (
ase_map_t* map, const ase_char_t* keyptr, ase_size_t keylen,
void* val);
ase_pair_t* ase_map_getpair (
ase_map_t* map, const ase_char_t* keyptr, ase_size_t keylen,
void** val);
ase_pair_t* ase_map_setpair (ase_map_t* map, ase_pair_t* pair, void* val);
int ase_map_remove (
ase_map_t* map, const ase_char_t* keyptr, ase_size_t keylen);
int ase_map_walk (ase_map_t* map, int (*walker)(ase_pair_t*,void*), void* arg);
/**
* Gets the pointer to the first pair in the map.
* @param map [in]
* @param buckno [out]
*/
ase_pair_t* ase_map_getfirstpair (ase_map_t* map, ase_size_t* buckno);
/**
* Gets the pointer to the next pair in the map.
* @param map [in]
* @param pair [in]
* @param buckno [in out]
*/
ase_pair_t* ase_map_getnextpair (
ase_map_t* map, ase_pair_t* pair, ase_size_t* buckno);
#ifdef __cplusplus
}
#endif
#endif

30
ase/include/ase/cmn/mem.h Normal file
View File

@ -0,0 +1,30 @@
/*
* $Id: mem.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_CMN_MEM_H_
#define _ASE_CMN_MEM_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
#ifdef __cplusplus
extern "C" {
#endif
void* ase_memcpy (void* dst, const void* src, ase_size_t n);
void* ase_memmove (void* dst, const void* src, ase_size_t n);
void* ase_memset (void* dst, int val, ase_size_t n);
int ase_memcmp (const void* s1, const void* s2, ase_size_t n);
void* ase_memchr (const void* s, int val, ase_size_t n);
void* ase_memrchr (const void* s, int val, ase_size_t n);
void* ase_memmem (const void* hs, ase_size_t hl, const void* nd, ase_size_t nl);
void* ase_memrmem (const void* hs, ase_size_t hl, const void* nd, ase_size_t nl);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,17 @@
/*
* $Id: pack.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#if defined(__GNUC__)
#pragma pack(1)
#elif defined(__HP_aCC) || defined(__HP_cc)
#pragma PACK 1
#elif defined(_MSC_VER) || defined(__BORLANDC__)
#pragma pack(push,1)
#elif defined(__DECC)
#pragma pack(push,1)
#else
#pragma pack(1)
#endif

94
ase/include/ase/cmn/rex.h Normal file
View File

@ -0,0 +1,94 @@
/*
* $Id: rex.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_CMN_REX_H_
#define _ASE_CMN_REX_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
/*
* Regular Esseression Syntax
* A regular expression is zero or more branches, separated by '|'.
* ......
* ......
*
* Compiled form of a regular expression:
*
* | expression |
* | header | branch | branch | branch |
* | nb | el | na | bl | cmd | arg | cmd | arg | na | bl | cmd | arg | na | bl | cmd |
*
* nb: the number of branches
* el: the length of a expression including the length of nb and el
* na: the number of atoms
* bl: the length of a branch including the length of na and bl
* cmd: The command and repetition info encoded together.
* Some commands require an argument to follow them but some other don't.
* It is encoded as follows:
*
* Subexpressions can be nested by having the command "GROUP"
* and a subexpression as its argument.
*
* Examples:
* a.c -> |1|6|5|ORD_CHAR(no bound)|a|ANY_CHAR(no bound)|ORD_CHAR(no bound)|c|
* ab|xy -> |2|10|4|ORD_CHAR(no bound)|a|ORD_CHAR(no bound)|b|4|ORD_CHAR(no bound)|x|ORD_CHAR(no bound)|y|
*/
#define ASE_REX_NA(code) (*(ase_size_t*)(code))
#define ASE_REX_LEN(code) \
(*(ase_size_t*)((ase_byte_t*)(code)+ASE_SIZEOF(ase_size_t)))
enum ase_rex_option_t
{
ASE_REX_IGNORECASE = (1 << 0)
};
enum ase_rex_errnum_t
{
ASE_REX_ENOERR = 0,
ASE_REX_ENOMEM,
ASE_REX_ERECUR, /* recursion too deep */
ASE_REX_ERPAREN, /* a right parenthesis is expected */
ASE_REX_ERBRACKET, /* a right bracket is expected */
ASE_REX_ERBRACE, /* a right brace is expected */
ASE_REX_EUNBALPAR, /* unbalanced parenthesis */
ASE_REX_ECOLON, /* a colon is expected */
ASE_REX_ECRANGE, /* invalid character range */
ASE_REX_ECCLASS, /* invalid character class */
ASE_REX_EBRANGE, /* invalid boundary range */
ASE_REX_EEND, /* unexpected end of the pattern */
ASE_REX_EGARBAGE /* garbage after the pattern */
};
#ifdef __cplusplus
extern "C" {
#endif
void* ase_buildrex (
ase_mmgr_t* mmgr, ase_size_t depth,
const ase_char_t* ptn, ase_size_t len, int* errnum);
int ase_matchrex (
ase_mmgr_t* mmgr, ase_ccls_t* ccls, ase_size_t depth,
void* code, int option,
const ase_char_t* str, ase_size_t len,
const ase_char_t** match_ptr, ase_size_t* match_len, int* errnum);
void ase_freerex (ase_mmgr_t* mmgr, void* code);
ase_bool_t ase_isemptyrex (void* code);
#if 0
void ase_dprintrex (ase_rex_t* rex, void* rex);
#endif
#ifdef __cplusplus
}
#endif
#endif

200
ase/include/ase/cmn/str.h Normal file
View File

@ -0,0 +1,200 @@
/*
* $Id: str.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_CMN_STR_H_
#define _ASE_CMN_STR_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
#define ASE_STR_LEN(x) ((x)->size)
#define ASE_STR_SIZE(x) ((x)->size + 1)
#define ASE_STR_CAPA(x) ((x)->capa)
#define ASE_STR_BUF(x) ((x)->buf)
#define ASE_STR_CHAR(x,idx) ((x)->buf[idx])
typedef struct ase_str_t ase_str_t;
struct ase_str_t
{
ase_char_t* buf;
ase_size_t size;
ase_size_t capa;
ase_mmgr_t* mmgr;
ase_bool_t __dynamic;
};
/* int ase_chartonum (ase_char_t c, int base) */
#define ASE_CHARTONUM(c,base) \
((c>=ASE_T('0') && c<=ASE_T('9'))? ((c-ASE_T('0')<base)? (c-ASE_T('0')): base): \
(c>=ASE_T('A') && c<=ASE_T('Z'))? ((c-ASE_T('A')+10<base)? (c-ASE_T('A')+10): base): \
(c>=ASE_T('a') && c<=ASE_T('z'))? ((c-ASE_T('a')+10<base)? (c-ASE_T('a')+10): base): base)
/* ase_strtonum (const ase_char_t* nptr, ase_char_t** endptr, int base) */
#define ASE_STRTONUM(value,nptr,endptr,base) \
{ \
int __ston_f = 0, __ston_v; \
const ase_char_t* __ston_ptr = nptr; \
for (;;) { \
ase_char_t __ston_c = *__ston_ptr; \
if (__ston_c == ASE_T(' ') || \
__ston_c == ASE_T('\t')) { __ston_ptr++; continue; } \
if (__ston_c == ASE_T('-')) { __ston_f++; __ston_ptr++; } \
if (__ston_c == ASE_T('+')) { __ston_ptr++; } \
break; \
} \
for (value = 0; (__ston_v = ASE_CHARTONUM(*__ston_ptr, base)) < base; __ston_ptr++) { \
value = value * base + __ston_v; \
} \
if (endptr != ASE_NULL) *((const ase_char_t**)endptr) = __ston_ptr; \
if (__ston_f > 0) value *= -1; \
}
/* ase_strxtonum (const ase_char_t* nptr, ase_size_t len, ase_char_char** endptr, int base) */
#define ASE_STRXTONUM(value,nptr,len,endptr,base) \
{ \
int __ston_f = 0, __ston_v; \
const ase_char_t* __ston_ptr = nptr; \
const ase_char_t* __ston_end = __ston_ptr + len; \
value = 0; \
while (__ston_ptr < __ston_end) { \
ase_char_t __ston_c = *__ston_ptr; \
if (__ston_c == ASE_T(' ') || __ston_c == ASE_T('\t')) { \
__ston_ptr++; continue; \
} \
if (__ston_c == ASE_T('-')) { __ston_f++; __ston_ptr++; } \
if (__ston_c == ASE_T('+')) { __ston_ptr++; } \
break; \
} \
for (value = 0; __ston_ptr < __ston_end && \
(__ston_v = ASE_CHARTONUM(*__ston_ptr, base)) != base; __ston_ptr++) { \
value = value * base + __ston_v; \
} \
if (endptr != ASE_NULL) *((const ase_char_t**)endptr) = __ston_ptr; \
if (__ston_f > 0) value *= -1; \
}
#ifdef __cplusplus
extern "C" {
#endif
/*
* basic string functions
*/
ase_size_t ase_strlen (const ase_char_t* str);
ase_size_t ase_strcpy (
ase_char_t* buf, const ase_char_t* str);
ase_size_t ase_strxcpy (
ase_char_t* buf, ase_size_t bsz, const ase_char_t* str);
ase_size_t ase_strncpy (
ase_char_t* buf, const ase_char_t* str, ase_size_t len);
ase_size_t ase_strxncpy (
ase_char_t* buf, ase_size_t bsz, const ase_char_t* str, ase_size_t len);
ase_size_t ase_strxcat (
ase_char_t* buf, ase_size_t bsz, const ase_char_t* str);
ase_size_t ase_strxncat (
ase_char_t* buf, ase_size_t bsz, const ase_char_t* str, ase_size_t len);
int ase_strcmp (const ase_char_t* s1, const ase_char_t* s2);
int ase_strxcmp (const ase_char_t* s1, ase_size_t len1, const ase_char_t* s2);
int ase_strxncmp (
const ase_char_t* s1, ase_size_t len1,
const ase_char_t* s2, ase_size_t len2);
int ase_strcasecmp (
const ase_char_t* s1, const ase_char_t* s2, ase_ccls_t* ccls);
int ase_strxncasecmp (
const ase_char_t* s1, ase_size_t len1,
const ase_char_t* s2, ase_size_t len2, ase_ccls_t* ccls);
ase_char_t* ase_strdup (const ase_char_t* str, ase_mmgr_t* mmgr);
ase_char_t* ase_strxdup (
const ase_char_t* str, ase_size_t len, ase_mmgr_t* mmgr);
ase_char_t* ase_strxdup2 (
const ase_char_t* str1, ase_size_t len1,
const ase_char_t* str2, ase_size_t len2, ase_mmgr_t* mmgr);
ase_char_t* ase_strstr (const ase_char_t* str, const ase_char_t* sub);
ase_char_t* ase_strxstr (
const ase_char_t* str, ase_size_t size, const ase_char_t* sub);
ase_char_t* ase_strxnstr (
const ase_char_t* str, ase_size_t strsz,
const ase_char_t* sub, ase_size_t subsz);
ase_char_t* ase_strchr (const ase_char_t* str, ase_cint_t c);
ase_char_t* ase_strxchr (const ase_char_t* str, ase_size_t len, ase_cint_t c);
ase_char_t* ase_strrchr (const ase_char_t* str, ase_cint_t c);
ase_char_t* ase_strxrchr (const ase_char_t* str, ase_size_t len, ase_cint_t c);
/* Checks if a string begins with a substring */
ase_char_t* ase_strbeg (const ase_char_t* str, const ase_char_t* sub);
ase_char_t* ase_strxbeg (
const ase_char_t* str, ase_size_t len, const ase_char_t* sub);
ase_char_t* ase_strnbeg (
const ase_char_t* str, const ase_char_t* sub, ase_size_t len);
ase_char_t* ase_strxnbeg (
const ase_char_t* str, ase_size_t len1,
const ase_char_t* sub, ase_size_t len2);
/* Checks if a string ends with a substring */
ase_char_t* ase_strend (const ase_char_t* str, const ase_char_t* sub);
ase_char_t* ase_strxend (
const ase_char_t* str, ase_size_t len, const ase_char_t* sub);
ase_char_t* ase_strnend (
const ase_char_t* str, const ase_char_t* sub, ase_size_t len);
ase_char_t* ase_strxnend (
const ase_char_t* str, ase_size_t len1,
const ase_char_t* sub, ase_size_t len2);
/*
* string conversion
*/
int ase_strtoi (const ase_char_t* str);
long ase_strtol (const ase_char_t* str);
unsigned int ase_strtoui (const ase_char_t* str);
unsigned long ase_strtoul (const ase_char_t* str);
int ase_strxtoi (const ase_char_t* str, ase_size_t len);
long ase_strxtol (const ase_char_t* str, ase_size_t len);
unsigned int ase_strxtoui (const ase_char_t* str, ase_size_t len);
unsigned long ase_strxtoul (const ase_char_t* str, ase_size_t len);
ase_int_t ase_strtoint (const ase_char_t* str);
ase_long_t ase_strtolong (const ase_char_t* str);
ase_uint_t ase_strtouint (const ase_char_t* str);
ase_ulong_t ase_strtoulong (const ase_char_t* str);
ase_int_t ase_strxtoint (const ase_char_t* str, ase_size_t len);
ase_long_t ase_strxtolong (const ase_char_t* str, ase_size_t len);
ase_uint_t ase_strxtouint (const ase_char_t* str, ase_size_t len);
ase_ulong_t ase_strxtoulong (const ase_char_t* str, ase_size_t len);
/*
* dynamic string
*/
ase_str_t* ase_str_open (ase_str_t* str, ase_size_t capa, ase_mmgr_t* mmgr);
void ase_str_close (ase_str_t* str);
void ase_str_clear (ase_str_t* str);
void ase_str_forfeit (ase_str_t* str);
void ase_str_swap (ase_str_t* str, ase_str_t* str2);
ase_size_t ase_str_cpy (ase_str_t* str, const ase_char_t* s);
ase_size_t ase_str_ncpy (ase_str_t* str, const ase_char_t* s, ase_size_t len);
ase_size_t ase_str_cat (ase_str_t* str, const ase_char_t* s);
ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len);
ase_size_t ase_str_ccat (ase_str_t* str, ase_char_t c);
ase_size_t ase_str_nccat (ase_str_t* str, ase_char_t c, ase_size_t len);
#ifdef __cplusplus
}
#endif
#endif

360
ase/include/ase/cmn/types.h Normal file
View File

@ -0,0 +1,360 @@
/*
* $Id: types.h 196 2008-06-08 14:04:16Z baconevi $
*
* {License}
*/
#ifndef _ASE_CMN_TYPES_H_
#define _ASE_CMN_TYPES_H_
/**
* @file types.h
* @brief Define common data types
*
* This file defines various data types to enhance portability across
* different platforms.
*/
#if defined(_WIN32)
#include <ase/cmn/conf_msw.h>
#elif defined(vms) || defined(__vms)
#include <ase/cmn/conf_vms.h>
#elif defined(__unix__) || defined(__unix) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__SPU__)
#if !defined(__unix__)
#define __unix__
#endif
#if !defined(__unix)
#define __unix
#endif
#include <ase/cmn/conf_unx.h>
#else
#error unsupported operating system
#endif
/**
* @brief a boolean type
*
* This type defines a boolean type as represented
* by #ASE_TRUE and #ASE_FALSE.
*/
typedef int ase_bool_t;
/** Represents a boolean true value */
#define ASE_TRUE (0 == 0)
/** Represents a boolean false value */
#define ASE_FALSE (0 != 0)
/**
* @brief a tri-state type
*
* This type defines a tri-state type as represented
* by ASE_ALIVE, ASE_ZOMBIE, and ASE_DEAD.
*/
typedef int ase_tri_t;
#define ASE_ALIVE 1
#define ASE_ZOMBIE 0
#define ASE_DEAD -1
/**
* @typedef ase_int_t
* @brief a signed integer type that can hold a pointer
*/
/**
* @typedef ase_uint_t
* @brief a unsigned integer type that can hold a pointer
*/
#if (defined(hpux) || defined(__hpux) || defined(__hpux__)) && (ASE_SIZEOF_VOID_P == ASE_SIZEOF_LONG)
typedef long ase_int_t;
typedef unsigned long ase_uint_t;
#elif defined(__SPU__) && (ASE_SIZEOF_VOID_P == ASE_SIZEOF_LONG)
typedef long ase_int_t;
typedef unsigned long ase_uint_t;
#elif ASE_SIZEOF_VOID_P == ASE_SIZEOF_INT
typedef int ase_int_t;
typedef unsigned int ase_uint_t;
#elif ASE_SIZEOF_VOID_P == ASE_SIZEOF_LONG
typedef long ase_int_t;
typedef unsigned long ase_uint_t;
#elif ASE_SIZEOF_VOID_P == ASE_SIZEOF_LONG_LONG
typedef long long ase_int_t;
typedef unsigned long long ase_uint_t;
#elif ASE_SIZEOF_VOID_P == ASE_SIZEOF___INT32
typedef __int32 ase_int_t;
typedef unsigned __int32 ase_uint_t;
#elif ASE_SIZEOF_VOID_P == ASE_SIZEOF___INT64
typedef __int64 ase_int_t;
typedef unsigned __int64 ase_uint_t;
#else
#error unsupported pointer size
#endif
/**
* @typedef ase_long_t
* @brief the largest signed integer type supported by the system
*/
/**
* @typedef ase_ulong_t
* @brief the largest unsigned integer type supported by the system
*/
#if ASE_SIZEOF_LONG_LONG != 0
typedef long long ase_long_t;
typedef unsigned long long ase_ulong_t;
#elif ASE_SIZEOF___INT64 != 0
typedef __int64 ase_long_t;
typedef unsigned __int64 ase_ulong_t;
#else
typedef long ase_long_t;
typedef unsigned long ase_ulong_t;
#endif
/**
* @typedef ase_int8_t
* @brief an 8-bit signed integer type
*/
/**
* @typedef ase_uint8_t
* @brief an 8-bit unsigned integer type
*/
#if ASE_SIZEOF_CHAR == 1
typedef char ase_int8_t;
typedef unsigned char ase_uint8_t;
#elif ASE_SIZEOF___INT8 == 1
typedef __int8 ase_int8_t;
typedef unsigned __int8 ase_uint8_t;
#endif
/**
* @typedef ase_int16_t
* @brief an 16-bit signed integer type
*/
/**
* @typedef ase_uint16_t
* @brief an 16-bit unsigned integer type
*/
#if ASE_SIZEOF_SHORT == 2
typedef short ase_int16_t;
typedef unsigned short ase_uint16_t;
#elif ASE_SIZEOF___INT16 == 2
typedef __int16 ase_int16_t;
typedef unsigned __int16 ase_uint16_t;
#endif
/**
* @typedef ase_int32_t
* @brief an 32-bit signed integer type
*/
/**
* @typedef ase_uint32_t
* @brief an 32-bit unsigned integer type
*/
#if ASE_SIZEOF_INT == 4
typedef int ase_int32_t;
typedef unsigned int ase_uint32_t;
#elif ASE_SIZEOF_LONG == 4
typedef long ase_int32_t;
typedef unsigned long ase_uint32_t;
#elif ASE_SIZEOF___INT32 == 4
typedef __int32 ase_int32_t;
typedef unsigned __int32 ase_uint32_t;
#endif
/**
* @typedef ase_int64_t
* @brief an 64-bit signed integer type
*/
/**
* @typedef ase_uint64_t
* @brief an 64-bit unsigned integer type
*/
#if ASE_SIZEOF_INT == 8
#define ASE_HAVE_INT64_T
#define ASE_HAVE_UINT64_T
typedef int ase_int64_t;
typedef unsigned int ase_uint64_t;
#elif ASE_SIZEOF_LONG == 8
#define ASE_HAVE_INT64_T
#define ASE_HAVE_UINT64_T
typedef long ase_int64_t;
typedef unsigned long ase_uint64_t;
#elif ASE_SIZEOF_LONG_LONG == 8
#define ASE_HAVE_INT64_T
#define ASE_HAVE_UINT64_T
typedef long long ase_int64_t;
typedef unsigned long long ase_uint64_t;
#elif ASE_SIZEOF___INT64 == 8
#define ASE_HAVE_INT64_T
#define ASE_HAVE_UINT64_T
typedef __int64 ase_int64_t;
typedef unsigned __int64 ase_uint64_t;
#endif
#if ASE_SIZEOF_INT == 16
#define ASE_HAVE_INT128_T
#define ASE_HAVE_UINT128_T
typedef int ase_int128_t;
typedef unsigned int ase_uint128_t;
#elif ASE_SIZEOF_LONG == 16
#define ASE_HAVE_INT128_T
#define ASE_HAVE_UINT128_T
typedef long ase_int128_t;
typedef unsigned long ase_uint128_t;
#elif ASE_SIZEOF_LONG_LONG == 16
#define ASE_HAVE_INT128_T
#define ASE_HAVE_UINT128_T
typedef long long ase_int128_t;
typedef unsigned long long ase_uint128_t;
#elif ASE_SIZEOF___INT128 == 16
#define ASE_HAVE_INT128_T
#define ASE_HAVE_UINT128_T
typedef __int128 ase_int128_t;
typedef unsigned __int128 ase_uint128_t;
#endif
/** an 8-bit unsigned integer type */
typedef ase_uint8_t ase_byte_t;
/** an unsigned integer type that can hold a pointer value */
typedef ase_uint_t ase_size_t;
/** an signed integer type that can hold a pointer value */
typedef ase_int_t ase_ssize_t;
/** an integer type identical to ase_uint_t */
typedef ase_uint_t ase_word_t;
/* floating-point number */
#if defined(__FreeBSD__)
/* TODO: check if the support for long double is complete.
* if so, use long double for ase_real_t */
#define ASE_SIZEOF_REAL ASE_SIZEOF_DOUBLE
typedef double ase_real_t;
#elif ASE_SIZEOF_LONG_DOUBLE > ASE_SIZEOF_DOUBLE
#define ASE_SIZEOF_REAL ASE_SIZEOF_LONG_DOUBLE
typedef long double ase_real_t;
#else
#define ASE_SIZEOF_REAL ASE_SIZEOF_DOUBLE
typedef double ase_real_t;
#endif
/* character types */
typedef char ase_mchar_t;
typedef int ase_mcint_t;
#if defined(__cplusplus) && (!defined(_MSC_VER) || (defined(_MSC_VER)&&defined(_NATIVE_WCHAR_T_DEFINED)))
/* C++ */
typedef wchar_t ase_wchar_t;
typedef wchar_t ase_wcint_t;
/* all the way down from here for C */
#elif (ASE_SIZEOF_WCHAR_T == 2) || (ASE_SIZEOF_WCHAR_T == 0)
typedef unsigned short ase_wchar_t;
typedef unsigned short ase_wcint_t;
#elif (ASE_SIZEOF_WCHAR_T == 4)
#if defined(vms) || defined(__vms)
typedef unsigned int ase_wchar_t;
typedef int ase_wcint_t;
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
typedef int ase_wchar_t;
typedef int ase_wcint_t;
#elif (defined(sun) || defined(__sun)) && defined(_LP64)
typedef int ase_wchar_t;
typedef int ase_wcint_t;
#elif defined(__APPLE__) && defined(__MACH__)
typedef int ase_wchar_t;
typedef int ase_wcint_t;
#elif defined(hpux) || defined(__hpux) || defined(__hpux__)
#if defined(__HP_cc) || defined(__HP_aCC)
typedef unsigned int ase_wchar_t;
#else
typedef int ase_wchar_t;
#endif
typedef int ase_wcint_t;
#elif ASE_SIZEOF_LONG == 4
typedef long ase_wchar_t;
typedef long ase_wcint_t;
#elif ASE_SIZEOF_INT == 4
typedef int ase_wchar_t;
typedef int ase_wcint_t;
#else
#error no supported data type for wchar_t
#endif
#else
#error unsupported size of wchar_t
#endif
#if defined(_WIN32) && (defined(UNICODE)||defined(_UNICODE))
#define ASE_CHAR_IS_WCHAR
typedef ase_wchar_t ase_char_t;
typedef ase_wcint_t ase_cint_t;
#else
#if defined(ASE_CHAR_IS_MCHAR)
typedef ase_mchar_t ase_char_t;
typedef ase_mcint_t ase_cint_t;
#elif defined(ASE_CHAR_IS_WCHAR)
typedef ase_wchar_t ase_char_t;
typedef ase_wcint_t ase_cint_t;
#elif defined(_MBCS)
#define ASE_CHAR_IS_MCHAR
typedef ase_mchar_t ase_char_t;
typedef ase_mcint_t ase_cint_t;
#else
#define ASE_CHAR_IS_WCHAR
typedef ase_wchar_t ase_char_t;
typedef ase_wcint_t ase_cint_t;
#endif
#endif
#if defined(ASE_CHAR_IS_WCHAR) && defined(_WIN32)
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#endif
typedef struct ase_cstr_t ase_cstr_t;
typedef struct ase_mmgr_t ase_mmgr_t;
typedef struct ase_ccls_t ase_ccls_t;
typedef void* (*ase_malloc_t) (void* custom, ase_size_t n);
typedef void* (*ase_realloc_t) (void* custom, void* ptr, ase_size_t n);
typedef void (*ase_free_t) (void* custom, void* ptr);
typedef ase_bool_t (*ase_isccls_t) (void* custom, ase_cint_t c);
typedef ase_cint_t (*ase_toccls_t) (void* custom, ase_cint_t c);
struct ase_cstr_t
{
const ase_char_t* ptr;
ase_size_t len;
};
struct ase_mmgr_t
{
ase_malloc_t malloc;
ase_realloc_t realloc;
ase_free_t free;
void* custom_data;
};
struct ase_ccls_t
{
ase_isccls_t is_upper;
ase_isccls_t is_lower;
ase_isccls_t is_alpha;
ase_isccls_t is_digit;
ase_isccls_t is_xdigit;
ase_isccls_t is_alnum;
ase_isccls_t is_space;
ase_isccls_t is_print;
ase_isccls_t is_graph;
ase_isccls_t is_cntrl;
ase_isccls_t is_punct;
ase_toccls_t to_upper;
ase_toccls_t to_lower;
void* custom_data;
};
#endif

View File

@ -0,0 +1,17 @@
/*
* $Id: unpack.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#if defined(__GNUC__)
#pragma pack()
#elif defined(__HP_aCC) || defined(__HP_cc)
#pragma PACK
#elif defined(_MSC_VER) || defined(__BORLANDC__)
#pragma pack(pop)
#elif defined(__DECC)
#pragma pack(pop)
#else
#pragma pack()
#endif

123
ase/include/ase/lsp/lsp.h Normal file
View File

@ -0,0 +1,123 @@
/*
* $Id: lsp.h 183 2008-06-03 08:18:55Z baconevi $
*
* {License}
*/
#ifndef _ASE_LSP_LSP_H_
#define _ASE_LSP_LSP_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
typedef struct ase_lsp_t ase_lsp_t;
typedef struct ase_lsp_obj_t ase_lsp_obj_t;
typedef struct ase_lsp_prmfns_t ase_lsp_prmfns_t;
typedef ase_ssize_t (*ase_lsp_io_t) (
int cmd, void* arg, ase_char_t* data, ase_size_t count);
typedef ase_real_t (*ase_lsp_pow_t) (
void* custom, ase_real_t x, ase_real_t y);
typedef int (*ase_lsp_sprintf_t) (
void* custom, ase_char_t* buf, ase_size_t size,
const ase_char_t* fmt, ...);
typedef void (*ase_lsp_dprintf_t) (void* custom, const ase_char_t* fmt, ...);
struct ase_lsp_prmfns_t
{
ase_mmgr_t mmgr;
ase_ccls_t ccls;
/* utilities */
struct
{
ase_lsp_sprintf_t sprintf;
ase_lsp_dprintf_t dprintf;
void* custom_data;
} misc;
};
/* io function commands */
enum
{
ASE_LSP_IO_OPEN = 0,
ASE_LSP_IO_CLOSE = 1,
ASE_LSP_IO_READ = 2,
ASE_LSP_IO_WRITE = 3
};
/* option code */
enum
{
ASE_LSP_UNDEFSYMBOL = (1 << 0)
};
/* error code */
enum
{
ASE_LSP_ENOERR,
ASE_LSP_ENOMEM,
ASE_LSP_EEXIT,
ASE_LSP_EEND,
ASE_LSP_EENDSTR,
ASE_LSP_ENOINP,
ASE_LSP_EINPUT,
ASE_LSP_ENOOUTP,
ASE_LSP_EOUTPUT,
ASE_LSP_ESYNTAX,
ASE_LSP_ERPAREN,
ASE_LSP_EARGBAD,
ASE_LSP_EARGFEW,
ASE_LSP_EARGMANY,
ASE_LSP_EUNDEFFN,
ASE_LSP_EBADFN,
ASE_LSP_EDUPFML,
ASE_LSP_EBADSYM,
ASE_LSP_EUNDEFSYM,
ASE_LSP_EEMPBDY,
ASE_LSP_EVALBAD,
ASE_LSP_EDIVBY0
};
typedef ase_lsp_obj_t* (*ase_lsp_prim_t) (ase_lsp_t* lsp, ase_lsp_obj_t* obj);
#ifdef __cplusplus
extern "C" {
#endif
ase_lsp_t* ase_lsp_open (
const ase_lsp_prmfns_t* prmfns,
ase_size_t mem_ubound, ase_size_t mem_ubound_inc);
void ase_lsp_close (ase_lsp_t* lsp);
void ase_lsp_geterror (
ase_lsp_t* lsp, int* errnum, const ase_char_t** errmsg);
void ase_lsp_seterror (
ase_lsp_t* lsp, int errnum,
const ase_char_t** errarg, ase_size_t argcnt);
int ase_lsp_attinput (ase_lsp_t* lsp, ase_lsp_io_t input, void* arg);
int ase_lsp_detinput (ase_lsp_t* lsp);
int ase_lsp_attoutput (ase_lsp_t* lsp, ase_lsp_io_t output, void* arg);
int ase_lsp_detoutput (ase_lsp_t* lsp);
ase_lsp_obj_t* ase_lsp_read (ase_lsp_t* lsp);
ase_lsp_obj_t* ase_lsp_eval (ase_lsp_t* lsp, ase_lsp_obj_t* obj);
int ase_lsp_print (ase_lsp_t* lsp, const ase_lsp_obj_t* obj);
int ase_lsp_addprim (
ase_lsp_t* lsp, const ase_char_t* name, ase_size_t name_len,
ase_lsp_prim_t prim, ase_size_t min_args, ase_size_t max_args);
int ase_lsp_removeprim (ase_lsp_t* lsp, const ase_char_t* name);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,5 @@
AUTOMAKE_OPTIONS = no-dependencies
# EXTRA_DIST = README
SUBDIRS = cmn awk utl

View File

@ -0,0 +1,34 @@
/*
* $Id: ctype.h 194 2008-06-06 13:00:39Z baconevi $
*/
#ifndef _ASE_UTL_CTYPE_H_
#define _ASE_UTL_CTYPE_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
#ifdef __cplusplus
extern "C" {
#endif
ase_bool_t ase_isupper (ase_cint_t c);
ase_bool_t ase_islower (ase_cint_t c);
ase_bool_t ase_isalpha (ase_cint_t c);
ase_bool_t ase_isdigit (ase_cint_t c);
ase_bool_t ase_isxdigit (ase_cint_t c);
ase_bool_t ase_isalnum (ase_cint_t c);
ase_bool_t ase_isspace (ase_cint_t c);
ase_bool_t ase_isprint (ase_cint_t c);
ase_bool_t ase_isgraph (ase_cint_t c);
ase_bool_t ase_iscntrl (ase_cint_t c);
ase_bool_t ase_ispunct (ase_cint_t c);
ase_cint_t ase_toupper (ase_cint_t c);
ase_cint_t ase_tolower (ase_cint_t c);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,42 @@
/*
* $Id: getopt.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_UTL_GETOPT_H_
#define _ASE_UTL_GETOPT_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
typedef struct ase_opt_t ase_opt_t;
struct ase_opt_t
{
/* input */
const ase_char_t* str;
/* output */
ase_cint_t opt; /* character checked for validity */
ase_char_t* arg; /* argument associated with an option */
int err;
/* input + output */
int ind; /* index into parent argv vector */
/* internal */
ase_char_t* cur;
};
#ifdef __cplusplus
extern "C" {
#endif
ase_cint_t ase_getopt (int argc, ase_char_t* const* argv, ase_opt_t* opt);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,66 @@
/*
* $Id: http.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_UTL_HTTP_H_
#define _ASE_UTL_HTTP_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
/* returns the type of http method */
typedef struct ase_http_req_t ase_http_req_t;
typedef struct ase_http_hdr_t ase_http_hdr_t;
struct ase_http_req_t
{
ase_char_t* method;
struct
{
ase_char_t* ptr;
ase_size_t len;
} path;
struct
{
ase_char_t* ptr;
ase_size_t len;
} args;
struct
{
char major;
char minor;
} vers;
};
struct ase_http_hdr_t
{
struct
{
ase_char_t* ptr;
ase_size_t len;
} name;
struct
{
ase_char_t* ptr;
ase_size_t len;
} value;
};
#ifdef __cplusplus
extern "C" {
#endif
ase_char_t* ase_parsehttpreq (ase_char_t* buf, ase_http_req_t* req);
ase_char_t* ase_parsehttphdr (ase_char_t* buf, ase_http_hdr_t* hdr);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,33 @@
/*
* $Id: main.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_UTL_MAIN_H_
#define _ASE_UTL_MAIN_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
#if defined(_WIN32)
#include <tchar.h>
#define ase_main _tmain
typedef ase_char_t ase_achar_t;
#else
#define ase_main main
typedef ase_mchar_t ase_achar_t;
#endif
#ifdef __cplusplus
extern "C" {
#endif
int ase_runmain (int argc, ase_achar_t* argv[], int(*mf) (int,ase_char_t*[]));
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,6 @@
pkginclude_HEADERS = ctype.h getopt.h http.h main.h stdio.h
pkgincludedir= $(includedir)/ase/utl
CLEANFILES = *dist

View File

@ -0,0 +1,88 @@
/*
* $Id: stdio.h 194 2008-06-06 13:00:39Z baconevi $
*
* {License}
*/
#ifndef _ASE_UTL_STDIO_H_
#define _ASE_UTL_STDIO_H_
#include <ase/cmn/types.h>
#include <ase/cmn/macros.h>
#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>
#if defined(_WIN32)
#include <tchar.h>
#define ase_printf _tprintf
#define ase_vprintf _vtprintf
#define ase_fprintf _ftprintf
#define ase_vfprintf _vftprintf
#define ase_fgets(x,y,s) _fgetts(x,y,s)
#define ase_fgetc(x) _fgettc(x)
#define ase_fputs(x,s) _fputts(x,s)
#define ase_fputc(x,s) _fputtc(x,s)
#elif defined(ASE_CHAR_IS_MCHAR)
#define ase_fgets(x,y,s) fgets(x,y,s)
#define ase_fgetc(x) fgetc(x)
#define ase_fputs(x,s) fputs(x,s)
#define ase_fputc(x,s) fputc(x,s)
#else
#define ase_fgets(x,y,s) fgetws(x,y,s)
#define ase_fgetc(x) fgetwc(x)
#define ase_fputs(x,s) fputws(x,s)
#define ase_fputc(x,s) fputwc(x,s)
#endif
#define ase_feof(s) feof(s)
#define ase_ferror(s) ferror(s)
#define ase_clearerr(s) clearerr(s)
#define ase_fflush(s) fflush(s)
#define ase_fclose(s) fclose(s)
#define ASE_FILE FILE
#define ASE_STDIN stdin
#define ASE_STDOUT stdout
#define ASE_STDERR stderr
typedef int (*ase_getdelim_t) (const ase_char_t* ptr,ase_size_t len, void* arg);
#ifdef __cplusplus
extern "C" {
#endif
int ase_vsprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, va_list ap);
int ase_sprintf (ase_char_t* buf, size_t size, const ase_char_t* fmt, ...);
#if !defined(_WIN32)
int ase_vfprintf (ASE_FILE *stream, const ase_char_t* fmt, va_list ap);
int ase_vprintf (const ase_char_t* fmt, va_list ap);
int ase_fprintf (ASE_FILE* file, const ase_char_t* fmt, ...);
int ase_printf (const ase_char_t* fmt, ...);
#endif
int ase_dprintf (const ase_char_t* fmt, ...);
ASE_FILE* ase_fopen (const ase_char_t* path, const ase_char_t* mode);
ASE_FILE* ase_popen (const ase_char_t* cmd, const ase_char_t* mode);
/**
* returns -2 on error, -1 on eof, length of data read on success
*/
ase_ssize_t ase_getline (ase_char_t **buf, ase_size_t *n, ASE_FILE *fp);
/**
* returns -3 on line breaker error, -2 on error, -1 on eof,
* length of data read on success
*/
ase_ssize_t ase_getdelim (
ase_char_t **buf, ase_size_t *n,
ase_getdelim_t fn, void* fnarg, ASE_FILE *fp);
#ifdef __cplusplus
}
#endif
#endif

5
ase/include/makefile.am Normal file
View File

@ -0,0 +1,5 @@
AUTOMAKE_OPTIONS = no-dependencies
# EXTRA_DIST = README
SUBDIRS = ase