qse/qse/lib/awk/awk.h

392 lines
7.2 KiB
C
Raw Normal View History

/*
* $Id: awk.h 312 2009-12-10 13:03:54Z hyunghwan.chung $
*
2009-09-16 04:01:02 +00:00
Copyright 2006-2009 Chung, Hyung-Hwan.
This file is part of QSE.
2009-09-16 04:01:02 +00:00
QSE is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
2009-09-16 04:01:02 +00:00
QSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
2009-09-16 04:01:02 +00:00
You should have received a copy of the GNU Lesser General Public
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
*/
2008-12-21 21:35:07 +00:00
#ifndef _QSE_LIB_AWK_AWK_H_
#define _QSE_LIB_AWK_AWK_H_
2009-09-22 07:28:18 +00:00
#define USE_STDC
2008-08-19 05:21:48 +00:00
#include "../cmn/mem.h"
#include <qse/cmn/chr.h>
2008-12-21 21:35:07 +00:00
#include <qse/cmn/str.h>
#include <qse/cmn/map.h>
#include <qse/cmn/lda.h>
#include <qse/cmn/rex.h>
2008-12-21 21:35:07 +00:00
typedef struct qse_awk_chain_t qse_awk_chain_t;
typedef struct qse_awk_tree_t qse_awk_tree_t;
2008-12-21 21:35:07 +00:00
#include <qse/awk/awk.h>
#include <qse/cmn/chr.h>
2008-06-07 04:33:44 +00:00
#include "tree.h"
#include "fnc.h"
2008-06-07 04:33:44 +00:00
#include "parse.h"
#include "run.h"
2009-02-16 08:31:34 +00:00
#include "rio.h"
#include "val.h"
#include "err.h"
2008-06-07 04:33:44 +00:00
#include "misc.h"
2009-09-19 22:28:49 +00:00
#define ENABLE_FEATURE_SCACHE
#define FEATURE_SCACHE_NUM_BLOCKS 16
#define FEATURE_SCACHE_BLOCK_UNIT 16
#define FEATURE_SCACHE_BLOCK_SIZE 128
#define QSE_AWK_MAX_GBLS 9999
#define QSE_AWK_MAX_LCLS 9999
2008-12-21 21:35:07 +00:00
#define QSE_AWK_MAX_PARAMS 9999
#define QSE_AWK_ALLOC(awk,size) QSE_MMGR_ALLOC((awk)->mmgr,size)
#define QSE_AWK_REALLOC(awk,ptr,size) QSE_MMGR_REALLOC((awk)->mmgr,ptr,size)
#define QSE_AWK_FREE(awk,ptr) QSE_MMGR_FREE((awk)->mmgr,ptr)
#define QSE_AWK_ISUPPER(awk,c) QSE_ISUPPER(c)
#define QSE_AWK_ISLOWER(awk,c) QSE_ISLOWER(c)
#define QSE_AWK_ISALPHA(awk,c) QSE_ISALPHA(c)
#define QSE_AWK_ISDIGIT(awk,c) QSE_ISDIGIT(c)
#define QSE_AWK_ISXDIGIT(awk,c) QSE_ISXDIGIT(c)
#define QSE_AWK_ISALNUM(awk,c) QSE_ISALNUM(c)
#define QSE_AWK_ISSPACE(awk,c) QSE_ISSPACE(c)
#define QSE_AWK_ISPRINT(awk,c) QSE_ISPRINT(c)
#define QSE_AWK_ISGRAPH(awk,c) QSE_ISGRAPH(c)
#define QSE_AWK_ISCNTRL(awk,c) QSE_ISCNTRL(c)
#define QSE_AWK_ISPUNCT(awk,c) QSE_ISPUNCT(c)
#define QSE_AWK_TOUPPER(awk,c) QSE_TOUPPER(c)
#define QSE_AWK_TOLOWER(awk,c) QSE_TOLOWER(c)
2008-12-21 21:35:07 +00:00
#define QSE_AWK_STRDUP(awk,str) (qse_strdup(str,(awk)->mmgr))
#define QSE_AWK_STRXDUP(awk,str,len) (qse_strxdup(str,len,(awk)->mmgr))
enum qse_awk_rio_type_t
{
/* rio types available */
QSE_AWK_RIO_PIPE,
QSE_AWK_RIO_FILE,
QSE_AWK_RIO_CONSOLE,
/* reserved for internal use only */
QSE_AWK_RIO_NUM
};
2008-12-21 21:35:07 +00:00
struct qse_awk_tree_t
{
2009-02-03 03:54:32 +00:00
qse_size_t ngbls; /* total number of globals */
qse_size_t ngbls_base; /* number of intrinsic globals */
qse_cstr_t cur_fun;
qse_map_t* funs; /* awk function map */
2007-12-05 08:13:38 +00:00
2008-12-21 21:35:07 +00:00
qse_awk_nde_t* begin;
qse_awk_nde_t* begin_tail;
2007-12-05 08:13:38 +00:00
2008-12-21 21:35:07 +00:00
qse_awk_nde_t* end;
qse_awk_nde_t* end_tail;
2007-12-05 08:13:38 +00:00
2008-12-21 21:35:07 +00:00
qse_awk_chain_t* chain;
qse_awk_chain_t* chain_tail;
qse_size_t chain_size; /* number of nodes in the chain */
2007-12-05 08:13:38 +00:00
int ok;
};
2009-08-24 06:56:45 +00:00
typedef struct qse_awk_tok_t qse_awk_tok_t;
struct qse_awk_tok_t
{
2009-08-24 19:50:38 +00:00
int type;
qse_str_t* name;
qse_awk_loc_t loc;
2009-08-24 06:56:45 +00:00
};
2008-12-21 21:35:07 +00:00
struct qse_awk_t
{
2009-05-22 00:50:02 +00:00
QSE_DEFINE_COMMON_FIELDS (sed)
2009-02-17 02:11:31 +00:00
/* primitive functions */
qse_awk_prm_t prm;
2008-07-20 23:53:29 +00:00
/* options */
int option;
/* word table */
2008-12-21 21:35:07 +00:00
qse_map_t* wtab;
/* reverse word table */
2008-12-21 21:35:07 +00:00
qse_map_t* rwtab;
/* parse tree */
2008-12-21 21:35:07 +00:00
qse_awk_tree_t tree;
/* temporary information that the parser needs */
struct
{
struct
{
int block;
int loop;
int stmnt; /* statement */
} id;
struct
{
struct
{
2008-12-21 21:35:07 +00:00
qse_size_t block;
qse_size_t loop;
qse_size_t expr; /* expression */
2009-07-27 20:31:58 +00:00
qse_size_t incl;
} cur;
struct
{
2008-12-21 21:35:07 +00:00
qse_size_t block;
qse_size_t expr;
2009-07-27 20:31:58 +00:00
qse_size_t incl;
} max;
} depth;
2007-12-12 08:05:58 +00:00
/* function calls */
qse_map_t* funs;
/* named variables */
2008-12-21 21:35:07 +00:00
qse_map_t* named;
2007-12-12 08:05:58 +00:00
/* global variables */
qse_lda_t* gbls;
2007-12-12 08:05:58 +00:00
/* local variables */
qse_lda_t* lcls;
2007-12-12 08:05:58 +00:00
/* parameters to a function */
2008-12-21 21:35:07 +00:00
qse_lda_t* params;
2007-12-12 08:05:58 +00:00
/* maximum number of local variables */
qse_size_t nlcls_max;
} parse;
/* source code management */
struct
{
2009-07-27 20:31:58 +00:00
qse_awk_sio_fun_t inf;
qse_awk_sio_fun_t outf;
2009-08-10 21:29:59 +00:00
qse_awk_sio_lxc_t last;
2008-12-21 21:35:07 +00:00
2009-08-07 02:27:14 +00:00
qse_size_t nungots;
2009-08-10 21:29:59 +00:00
qse_awk_sio_lxc_t ungot[5];
2009-08-07 02:27:14 +00:00
qse_awk_sio_arg_t arg; /* for the top level source */
2009-07-27 20:31:58 +00:00
qse_awk_sio_arg_t* inp; /* current input */
2009-08-07 02:27:14 +00:00
qse_map_t* names;
2009-07-27 20:31:58 +00:00
} sio;
2009-08-01 07:01:04 +00:00
/* previous token */
2009-08-24 06:56:45 +00:00
qse_awk_tok_t ptok;
/* current token */
2009-08-24 06:56:45 +00:00
qse_awk_tok_t tok;
2009-08-01 07:01:04 +00:00
/* look-ahead token */
2009-08-24 06:56:45 +00:00
qse_awk_tok_t ntok;
/* intrinsic functions */
struct
{
qse_awk_fnc_t* sys;
2008-12-21 21:35:07 +00:00
qse_map_t* user;
} fnc;
struct
{
struct
{
struct
{
2008-12-21 21:35:07 +00:00
qse_size_t block;
qse_size_t expr;
} max;
} depth;
} run;
struct
{
struct
{
struct
{
2008-12-21 21:35:07 +00:00
qse_size_t build;
qse_size_t match;
} max;
} depth;
} rex;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t fmt[1024];
} tmp;
/* housekeeping */
qse_awk_errstr_t errstr;
qse_awk_errinf_t errinf;
2008-12-21 21:35:07 +00:00
qse_bool_t stopall;
};
2008-12-21 21:35:07 +00:00
struct qse_awk_chain_t
{
2008-12-21 21:35:07 +00:00
qse_awk_nde_t* pattern;
qse_awk_nde_t* action;
qse_awk_chain_t* next;
};
struct qse_awk_rtx_t
{
int id;
2008-12-21 21:35:07 +00:00
qse_map_t* named;
void** stack;
2008-12-21 21:35:07 +00:00
qse_size_t stack_top;
qse_size_t stack_base;
qse_size_t stack_limit;
int exit_level;
2008-12-21 21:35:07 +00:00
qse_awk_val_ref_t* fcache[128];
qse_size_t fcache_count;
2009-09-19 22:28:49 +00:00
#ifdef ENABLE_FEATURE_SCACHE
qse_awk_val_str_t* scache
[FEATURE_SCACHE_NUM_BLOCKS][FEATURE_SCACHE_BLOCK_SIZE];
qse_size_t scache_count[FEATURE_SCACHE_NUM_BLOCKS];
#endif
2008-01-16 21:46:08 +00:00
struct
{
2008-12-21 21:35:07 +00:00
qse_awk_val_int_t* ifree;
qse_awk_val_chunk_t* ichunk;
qse_awk_val_real_t* rfree;
qse_awk_val_chunk_t* rchunk;
2008-01-16 21:46:08 +00:00
} vmgr;
2008-01-16 08:03:41 +00:00
2008-12-21 21:35:07 +00:00
qse_awk_nde_blk_t* active_block;
qse_byte_t* pattern_range_state;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t buf[1024];
qse_size_t buf_pos;
qse_size_t buf_len;
qse_bool_t eof;
2009-09-22 07:28:18 +00:00
qse_str_t line; /* entire line */
qse_str_t linew; /* line for manipulation, if necessary */
2008-12-21 21:35:07 +00:00
qse_awk_val_t* d0; /* $0 */
2008-12-21 21:35:07 +00:00
qse_size_t maxflds;
qse_size_t nflds; /* NF */
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len;
qse_awk_val_t* val; /* $1 .. $NF */
}* flds;
} inrec;
struct
{
void* rs;
void* fs;
int ignorecase;
2008-12-21 21:35:07 +00:00
qse_long_t nr;
qse_long_t fnr;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len;
} convfmt;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len;
} ofmt;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len;
} ofs;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len;
} ors;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len;
} subsep;
} gbl;
2009-02-16 08:31:34 +00:00
/* rio chain */
struct
{
qse_awk_rio_fun_t handler[QSE_AWK_RIO_NUM];
qse_awk_rio_arg_t* chain;
2009-02-16 08:31:34 +00:00
} rio;
struct
{
2008-12-21 21:35:07 +00:00
qse_str_t fmt;
qse_str_t out;
struct
{
2008-12-21 21:35:07 +00:00
qse_char_t* ptr;
qse_size_t len; /* length */
qse_size_t inc; /* increment */
} tmp;
} format;
struct
{
struct
{
2008-12-21 21:35:07 +00:00
qse_size_t block;
qse_size_t expr; /* expression */
} cur;
struct
{
2008-12-21 21:35:07 +00:00
qse_size_t block;
qse_size_t expr;
} max;
} depth;
qse_awk_errinf_t errinf;
2008-12-21 21:35:07 +00:00
qse_awk_t* awk;
qse_awk_rcb_t rcb;
};
2008-03-04 03:31:41 +00:00
2008-12-21 21:35:07 +00:00
#define QSE_AWK_FREEREX(awk,code) qse_freerex((awk)->mmgr,code)
#define QSE_AWK_BUILDREX(awk,ptn,len,errnum) \
qse_awk_buildrex(awk,ptn,len,errnum)
#define QSE_AWK_MATCHREX(awk,code,option,str,len,substr,sublen,match,errnum) \
qse_awk_matchrex(awk,code,option,str,len,substr,sublen,match,errnum)
2008-03-04 03:31:41 +00:00
#endif