2006-03-31 16:35:37 +00:00
|
|
|
/*
|
2006-09-27 14:11:31 +00:00
|
|
|
* $Id: awk_i.h,v 1.60 2006-09-27 14:11:31 bacon Exp $
|
2006-03-31 16:35:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _XP_AWK_AWKI_H_
|
|
|
|
#define _XP_AWK_AWKI_H_
|
|
|
|
|
2006-04-21 17:24:31 +00:00
|
|
|
typedef struct xp_awk_chain_t xp_awk_chain_t;
|
|
|
|
typedef struct xp_awk_run_t xp_awk_run_t;
|
|
|
|
typedef struct xp_awk_tree_t xp_awk_tree_t;
|
2006-05-12 09:39:20 +00:00
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
#include <xp/awk/awk.h>
|
|
|
|
|
2006-04-16 04:31:38 +00:00
|
|
|
#ifdef XP_AWK_STAND_ALONE
|
2006-09-01 06:23:58 +00:00
|
|
|
#if !defined(XP_CHAR_IS_MCHAR) && !defined(XP_CHAR_IS_WCHAR)
|
|
|
|
#error Neither XP_CHAR_IS_MCHAR nor XP_CHAR_IS_WCHAR is defined.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#define xp_assert assert
|
2006-09-22 14:05:30 +00:00
|
|
|
#else
|
|
|
|
#include <xp/bas/assert.h>
|
2006-03-31 16:35:37 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-31 16:00:20 +00:00
|
|
|
#include <xp/awk/str.h>
|
2006-07-25 16:41:40 +00:00
|
|
|
#include <xp/awk/rex.h>
|
2006-06-21 11:45:26 +00:00
|
|
|
#include <xp/awk/map.h>
|
2006-09-27 14:11:31 +00:00
|
|
|
#include <xp/awk/tree.h>
|
2006-06-21 11:45:26 +00:00
|
|
|
#include <xp/awk/val.h>
|
|
|
|
#include <xp/awk/func.h>
|
|
|
|
#include <xp/awk/tab.h>
|
2006-08-06 15:03:42 +00:00
|
|
|
#include <xp/awk/parse.h>
|
2006-06-21 11:45:26 +00:00
|
|
|
#include <xp/awk/run.h>
|
|
|
|
#include <xp/awk/extio.h>
|
2006-09-08 15:26:49 +00:00
|
|
|
#include <xp/awk/misc.h>
|
2006-06-21 11:45:26 +00:00
|
|
|
|
2006-08-16 09:35:21 +00:00
|
|
|
#ifdef _MSC_VER
|
2006-06-30 04:18:47 +00:00
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#endif
|
|
|
|
|
2006-05-12 09:39:20 +00:00
|
|
|
#if defined(_WIN32) && defined(XP_AWK_STAND_ALONE) && defined(_DEBUG)
|
|
|
|
#define _CRTDBG_MAP_ALLOC
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
2006-08-06 15:03:42 +00:00
|
|
|
#define XP_AWK_MAX_GLOBALS 9999
|
|
|
|
#define XP_AWK_MAX_LOCALS 9999
|
|
|
|
#define XP_AWK_MAX_PARAMS 9999
|
|
|
|
|
2006-09-01 04:03:28 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
|
|
|
#define XP_AWK_MALLOC(awk,size) malloc (size)
|
|
|
|
#define XP_AWK_REALLOC(awk,ptr,size) realloc (ptr, size)
|
|
|
|
#define XP_AWK_FREE(awk,ptr) free (ptr)
|
|
|
|
#else
|
|
|
|
#define XP_AWK_MALLOC(awk,size) \
|
|
|
|
(awk)->syscas->malloc (size, (awk)->syscas->custom_data)
|
|
|
|
#define XP_AWK_REALLOC(awk,ptr,size) \
|
|
|
|
(awk)->syscas->realloc (ptr, size, (awk)->syscas->custom_data)
|
|
|
|
#define XP_AWK_FREE(awk,ptr) \
|
|
|
|
(awk)->syscas->free (ptr, (awk)->syscas->custom_data)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define XP_AWK_LOCK(awk) \
|
|
|
|
do { \
|
2006-09-01 06:23:58 +00:00
|
|
|
if ((awk)->syscas != XP_NULL && (awk)->syscas->lock != XP_NULL) \
|
|
|
|
(awk)->syscas->lock (awk, (awk)->syscas->custom_data); \
|
2006-09-01 04:03:28 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define XP_AWK_UNLOCK(awk) \
|
|
|
|
do { \
|
2006-09-01 06:23:58 +00:00
|
|
|
if ((awk)->syscas != XP_NULL && (awk)->syscas->unlock != XP_NULL) \
|
|
|
|
(awk)->syscas->unlock (awk, (awk)->syscas->custom_data); \
|
2006-09-01 04:03:28 +00:00
|
|
|
} while (0)
|
2006-08-31 14:52:12 +00:00
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
#define XP_AWK_ISUPPER(awk,c) (awk)->syscas->is_upper(c)
|
|
|
|
#define XP_AWK_ISLOWER(awk,c) (awk)->syscas->is_lower(c)
|
|
|
|
#define XP_AWK_ISALPHA(awk,c) (awk)->syscas->is_alpha(c)
|
|
|
|
#define XP_AWK_ISDIGIT(awk,c) (awk)->syscas->is_digit(c)
|
|
|
|
#define XP_AWK_ISXDIGIT(awk,c) (awk)->syscas->is_xdigit(c)
|
|
|
|
#define XP_AWK_ISALNUM(awk,c) (awk)->syscas->is_alnum(c)
|
|
|
|
#define XP_AWK_ISSPACE(awk,c) (awk)->syscas->is_space(c)
|
|
|
|
#define XP_AWK_ISPRINT(awk,c) (awk)->syscas->is_print(c)
|
|
|
|
#define XP_AWK_ISGRAPH(awk,c) (awk)->syscas->is_graph(c)
|
|
|
|
#define XP_AWK_ISCNTRL(awk,c) (awk)->syscas->is_cntrl(c)
|
|
|
|
#define XP_AWK_ISPUNCT(awk,c) (awk)->syscas->is_punct(c)
|
|
|
|
#define XP_AWK_TOUPPER(awk,c) (awk)->syscas->to_upper(c)
|
|
|
|
#define XP_AWK_TOLOWER(awk,c) (awk)->syscas->to_lower(c)
|
|
|
|
|
2006-09-25 06:17:19 +00:00
|
|
|
#define XP_AWK_MEMCPY(awk,dst,src,len) \
|
|
|
|
do { \
|
|
|
|
if ((awk)->syscas->memcpy == XP_NULL) \
|
|
|
|
xp_awk_memcpy (dst, src, len); \
|
|
|
|
else (awk)->syscas->memcpy (dst, src, len); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define XP_AWK_MEMSET(awk,dst,val,len) \
|
|
|
|
do { \
|
|
|
|
if ((awk)->syscas->memset == XP_NULL) \
|
|
|
|
xp_awk_memset (dst, val, len); \
|
|
|
|
else (awk)->syscas->memset (dst, val, len); \
|
|
|
|
} while (0)
|
|
|
|
|
2006-04-21 17:24:31 +00:00
|
|
|
struct xp_awk_tree_t
|
|
|
|
{
|
2006-08-04 16:31:22 +00:00
|
|
|
xp_size_t nglobals; /* total number of globals */
|
|
|
|
xp_size_t nbglobals; /* number of builtin globals */
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_map_t afns; /* awk function map */
|
2006-04-21 17:24:31 +00:00
|
|
|
xp_awk_nde_t* begin;
|
|
|
|
xp_awk_nde_t* end;
|
|
|
|
xp_awk_chain_t* chain;
|
|
|
|
xp_awk_chain_t* chain_tail;
|
2006-08-13 05:55:02 +00:00
|
|
|
xp_size_t chain_size; /* number of nodes in the chain */
|
2006-04-21 17:24:31 +00:00
|
|
|
};
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
struct xp_awk_t
|
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
xp_awk_syscas_t* syscas;
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
/* options */
|
2006-08-04 17:36:40 +00:00
|
|
|
int option;
|
2006-03-31 16:35:37 +00:00
|
|
|
|
|
|
|
/* parse tree */
|
2006-04-21 17:24:31 +00:00
|
|
|
xp_awk_tree_t tree;
|
2006-08-01 15:57:43 +00:00
|
|
|
int state;
|
2006-03-31 16:35:37 +00:00
|
|
|
|
|
|
|
/* temporary information that the parser needs */
|
|
|
|
struct
|
|
|
|
{
|
2006-08-01 15:57:43 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
int block;
|
|
|
|
int loop;
|
|
|
|
} id;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_size_t loop;
|
|
|
|
} depth;
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_tab_t globals;
|
|
|
|
xp_awk_tab_t locals;
|
|
|
|
xp_awk_tab_t params;
|
|
|
|
xp_size_t nlocals_max;
|
|
|
|
} parse;
|
|
|
|
|
2006-08-06 08:16:03 +00:00
|
|
|
/* source code management */
|
|
|
|
struct
|
2006-03-31 16:35:37 +00:00
|
|
|
{
|
2006-08-06 08:16:03 +00:00
|
|
|
xp_awk_srcios_t* ios;
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_cint_t curc;
|
|
|
|
xp_cint_t ungotc[5];
|
|
|
|
xp_size_t ungotc_count;
|
2006-05-10 16:02:39 +00:00
|
|
|
|
2006-08-06 08:16:03 +00:00
|
|
|
xp_size_t line;
|
|
|
|
xp_size_t column;
|
|
|
|
} lex;
|
2006-05-13 16:33:07 +00:00
|
|
|
|
2006-08-06 08:16:03 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_char_t buf[512];
|
|
|
|
xp_size_t buf_pos;
|
|
|
|
xp_size_t buf_len;
|
|
|
|
} shared;
|
|
|
|
} src;
|
2006-03-31 16:35:37 +00:00
|
|
|
|
|
|
|
/* token */
|
|
|
|
struct
|
|
|
|
{
|
2006-08-31 16:00:20 +00:00
|
|
|
int prev;
|
|
|
|
int type;
|
|
|
|
xp_awk_str_t name;
|
|
|
|
xp_size_t line;
|
|
|
|
xp_size_t column;
|
2006-03-31 16:35:37 +00:00
|
|
|
} token;
|
|
|
|
|
2006-07-13 15:43:39 +00:00
|
|
|
/* builtin functions */
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_awk_bfn_t* sys;
|
|
|
|
xp_awk_bfn_t* user;
|
|
|
|
} bfn;
|
|
|
|
|
2006-08-10 16:02:15 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_size_t count;
|
|
|
|
xp_awk_run_t* ptr;
|
|
|
|
} run;
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
/* housekeeping */
|
2006-08-10 16:02:15 +00:00
|
|
|
int errnum;
|
2006-03-31 16:35:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct xp_awk_chain_t
|
|
|
|
{
|
|
|
|
xp_awk_nde_t* pattern;
|
|
|
|
xp_awk_nde_t* action;
|
|
|
|
xp_awk_chain_t* next;
|
|
|
|
};
|
|
|
|
|
2006-04-21 16:21:27 +00:00
|
|
|
struct xp_awk_run_t
|
|
|
|
{
|
2006-08-10 16:02:15 +00:00
|
|
|
int id;
|
2006-04-21 16:21:27 +00:00
|
|
|
xp_awk_map_t named;
|
|
|
|
|
|
|
|
void** stack;
|
|
|
|
xp_size_t stack_top;
|
|
|
|
xp_size_t stack_base;
|
|
|
|
xp_size_t stack_limit;
|
|
|
|
int exit_level;
|
|
|
|
|
2006-07-01 16:07:06 +00:00
|
|
|
xp_awk_val_int_t* icache[100]; /* TODO: choose the optimal size */
|
|
|
|
xp_awk_val_real_t* rcache[100]; /* TODO: choose the optimal size */
|
2006-08-21 02:53:42 +00:00
|
|
|
xp_awk_val_ref_t* fcache[100]; /* TODO: choose the optimal size */
|
2006-04-21 16:21:27 +00:00
|
|
|
xp_size_t icache_count;
|
|
|
|
xp_size_t rcache_count;
|
2006-08-21 02:53:42 +00:00
|
|
|
xp_size_t fcache_count;
|
2006-04-21 16:21:27 +00:00
|
|
|
|
2006-08-03 15:50:04 +00:00
|
|
|
xp_awk_nde_blk_t* active_block;
|
2006-08-13 05:55:02 +00:00
|
|
|
xp_byte_t* pattern_range_state;
|
2006-08-03 15:50:04 +00:00
|
|
|
|
2006-04-22 13:54:53 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_char_t buf[1024];
|
|
|
|
xp_size_t buf_pos;
|
|
|
|
xp_size_t buf_len;
|
|
|
|
xp_bool_t eof;
|
2006-07-05 16:20:23 +00:00
|
|
|
|
2006-08-31 16:00:20 +00:00
|
|
|
xp_awk_str_t line;
|
2006-07-06 15:54:41 +00:00
|
|
|
xp_awk_val_t* d0; /* $0 */
|
|
|
|
|
2006-07-07 09:48:23 +00:00
|
|
|
xp_size_t maxflds;
|
2006-07-06 15:54:41 +00:00
|
|
|
xp_size_t nflds; /* NF */
|
2006-07-05 16:20:23 +00:00
|
|
|
struct
|
|
|
|
{
|
2006-07-06 15:54:41 +00:00
|
|
|
xp_char_t* ptr;
|
|
|
|
xp_size_t len;
|
|
|
|
xp_awk_val_t* val; /* $1 .. $NF */
|
2006-07-05 16:20:23 +00:00
|
|
|
}* flds;
|
2006-09-05 04:11:11 +00:00
|
|
|
|
2006-07-03 04:09:56 +00:00
|
|
|
} inrec;
|
2006-04-22 13:54:53 +00:00
|
|
|
|
2006-09-05 04:11:11 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
void* rs;
|
|
|
|
void* fs;
|
2006-09-08 15:26:49 +00:00
|
|
|
int ignorecase;
|
2006-09-22 14:05:30 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_char_t* ptr;
|
|
|
|
xp_size_t len;
|
|
|
|
} ofs;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_char_t* ptr;
|
|
|
|
xp_size_t len;
|
|
|
|
} subsep;
|
|
|
|
} global;
|
2006-09-05 04:11:11 +00:00
|
|
|
|
2006-06-21 13:52:15 +00:00
|
|
|
/* extio chain */
|
2006-08-10 16:02:15 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
xp_awk_io_t handler[XP_AWK_EXTIO_NUM];
|
|
|
|
xp_awk_extio_t* chain;
|
|
|
|
} extio;
|
2006-06-16 07:37:27 +00:00
|
|
|
|
2006-08-10 16:02:15 +00:00
|
|
|
int errnum;
|
2006-04-21 16:21:27 +00:00
|
|
|
|
2006-06-19 04:38:51 +00:00
|
|
|
xp_awk_t* awk;
|
2006-08-10 16:02:15 +00:00
|
|
|
xp_awk_run_t* prev;
|
|
|
|
xp_awk_run_t* next;
|
2006-06-16 07:37:27 +00:00
|
|
|
};
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
#endif
|