/************************************************************************* * * HTCd - Copyright (C) 1998-2006 Henrik Rydberg * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef TOKENH #define TOKENH #include #include #define TOK_ARGS (mstream& out,Env& env) #define TOK_IMPL(func) static void impl_##func TOK_ARGS throw(merror_t) #define TOK_ADD(func,a,mod,help) \ T.add(new binary_t(uscore2dot(#func),sref(help),mod,sref(a),impl_##func),0,1) mstring uscore2dot(const char* s); class Env; typedef void (*s_func_t)TOK_ARGS; class tok_t { public: static const unsigned CONSTANT,RESTRICT,NORMAL,SYSONLY,INIT; static const unsigned RUNNABLE,FUNCTION,BINARY; static const unsigned REFERENCE,DBREF,CRON; static const unsigned PRIVATE,PUBLIC,XMODES; static inline unsigned tight(unsigned xmod) { // INIT is ignored here simply because no mode can // read variables in INIT mode... defaults to public. return (xmod&SYSONLY)?SYSONLY:(xmod&NORMAL)?PRIVATE:PUBLIC; } mstring name,body; unsigned mod; int is_clear(int xmod) const { return (mod&xmod)!=0; } int is_replaceable(const tok_t& t) const { return (mod&t.mod)==t.mod; } int is_const() const { return (mod&CONSTANT)!=0; } int is_runnable() const { return (mod&RUNNABLE)!=0; } int is_func() const { return (mod&FUNCTION)!=0; } int is_cron() const { return (mod&CRON)!=0; } int is_binary() const { return (mod&BINARY)!=0; } int is_reference() const { return (mod&REFERENCE)!=0; } int is_dbref() const { return (mod&DBREF)!=0; } tok_t(const sref& s,const sref& b,unsigned m) : name(s),body(b),mod(m) {} virtual ~tok_t(); static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<48> tree; }; /////////////////////////////////////////////////////////////////// class reference_t : public tok_t { public: mstring data; reference_t(const sref& s,const sref& b,unsigned m): tok_t(s,b,m|REFERENCE|CONSTANT) {} static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<64> tree; }; /////////////////////////////////////////////////////////////////// class dbref_t : public tok_t { public: dbmap db; dbref_t(const sref& s,const sref& b,unsigned m): tok_t(s,b,m|DBREF|CONSTANT) {} static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<136> tree; }; /////////////////////////////////////////////////////////////////// class runnable_t : public tok_t { public: mstring arg; runnable_t(const sref& s,const sref& b,unsigned m,const sref& a): tok_t(s,b,m|RUNNABLE|CONSTANT),arg(a) {} static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<64> tree; }; /////////////////////////////////////////////////////////////////// class func_t : public runnable_t { public: expr_t x; func_t(const sref& s,const sref& b,unsigned m,const sref& a): runnable_t(s,b,m|FUNCTION,a) { x=body; } static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<128> tree; }; /////////////////////////////////////////////////////////////////// class cron_t : public func_t { public: int last,interval; cron_t(const sref& s,const sref& b,unsigned m,int i): func_t(s,b,m|CRON,ms_empty),last(0),interval(i) {} static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<136> tree; }; /////////////////////////////////////////////////////////////////// class binary_t : public runnable_t { public: s_func_t func; binary_t(const sref& s,const sref& b,unsigned m,const sref& a,s_func_t f): runnable_t(s,b,m|BINARY,a),func(f) {} static void* operator new(size_t n); static void operator delete(void* p); private: static uniheap<72> tree; }; /////////////////////////////////////////////////////////////////// mstream& operator<<(mstream&,const runnable_t& ftok); mstring Mode(const tok_t& tok); mstring Desc(const tok_t& tok); /////////////////////////////////////////////////////////////////// struct tokmap { typedef tok_t* val_t; typedef mrvec map_t; typedef map_t::iterator iterator; typedef map_t::const_iterator const_iterator; struct comp_t { sref key; comp_t(const sref& skey) : key(skey) {} bool operator()(val_t a,val_t b) const { return (a?((const sref&)a->name):key)<(b?((const sref&)b->name):key); } }; tokmap* up; map_t list; tokmap() : up(0),list() {} tokmap(tokmap& p) : up(&p),list() {} ~tokmap() { clear(); } iterator begin() { return list.begin(); } const_iterator begin() const { return list.begin(); } iterator end() { return list.end(); } const_iterator end() const { return list.end(); } void clear(); iterator erase(iterator p) { delete *p; return list.erase(p); } tok_t* get_this(const sref& t); const tok_t* get_this(const sref& t) const; const tok_t* get(const sref& t) const; tok_t* get(const sref& t); tok_t* set(tok_t* tok,int replace,int force) throw(merror_t); tok_t* add(tok_t* tok,int replace,int force) throw(merror_t); }; /////////////////////////////////////////////////////////////////// int is_tokname(const sref& s); int is_arglist(const sref& s); tok_t* Cond(tokmap& map,const sref& s,const sref& b,unsigned m) throw(merror_t); tok_t* Global(tokmap& map,const sref& s,const sref& b,unsigned m,int rpl=0) throw(merror_t); tok_t* Const(tokmap& map,const sref& s,const sref& b,unsigned m,int rpl=0) throw(merror_t); func_t* Func(tokmap& map,const sref& s,const sref& b, unsigned m,const sref& a,int rpl=0) throw(merror_t); cron_t* Cron(tokmap& map,const sref& s,const sref& b, unsigned m,int i,int rpl=0) throw(merror_t); binary_t* Binary(tokmap& map,const sref& s,const sref& b, unsigned m,const sref& a,s_func_t f,int rpl=0) throw(merror_t); reference_t* Reference(tokmap& map,const sref& s,const sref& b, unsigned m,int rpl=0) throw(merror_t); dbref_t* DBRef(tokmap& map,const sref& s, const sref& b,unsigned m,int rpl=0) throw(merror_t); void ClearToken(tokmap& map,const sref& s) throw(merror_t); /////////////////////////////////////////////////////////////////// const int XPUBLIC=tok_t::RESTRICT; const int XPRIVATE=XPUBLIC|tok_t::NORMAL; const int XSYSTEM=XPRIVATE|tok_t::SYSONLY; const int XINIT=tok_t::INIT; #endif