Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

script.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2000 Open Source Telecom Corporation.
00002 //  
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 // 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 // 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software 
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 // 
00017 // As a special exception to the GNU General Public License, permission is 
00018 // granted for additional uses of the text contained in its release 
00019 // of ccscript.
00020 // 
00021 // The exception is that, if you link the ccscript library with other
00022 // files to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the ccscript library code into it.
00026 // 
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 // 
00030 // This exception applies only to the code released under the 
00031 // name ccscript.  If you copy code from other releases into a copy of
00032 // ccscript, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 // 
00037 // If you write modifications of your own for ccscript, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.  
00040 
00041 #ifndef __CCXX_SCRIPT_H__
00042 #define __CCXX_SCRIPT_H__
00043 
00044 #ifndef __CCXX_MISC_H__
00045 #include <cc++/misc.h>
00046 #else
00047 #ifdef  __CCXX_NAMESPACE_H__
00048 #include <cc++/macros.h>
00049 #endif
00050 #endif
00051 
00052 class ScriptCommand;
00053 class ScriptImage;
00054 class ScriptInterp;
00055 struct _line;
00056 
00057 #define MAX_LOCKS 8
00058 #define TRAP_BITS (sizeof(unsigned long) * 8)
00059 #define SCRIPT_STACK_SIZE 10
00060 #define KEYWORD_INDEX_SIZE 37
00061 #define SYMBOL_INDEX_SIZE 187
00062 #define SCRIPT_INDEX_SIZE KEYWORD_INDEX_SIZE
00063 
00064 typedef bool (ScriptInterp::*scriptmethod_t)(void);
00065 typedef char *(ScriptCommand::*scriptcheck_t)(struct _line *line);
00066 
00067 #pragma pack(1)
00068 typedef struct _symbol
00069 {
00070         struct _symbol *next;
00071         char *id;
00072         struct
00073         {
00074                 unsigned size : 16;
00075                 bool initial : 1;
00076                 bool system : 1;
00077                 bool readonly : 1;
00078                 bool commit : 1;
00079         } flags;
00080         char data[1];           
00081 }       scriptsymbol_t;         
00082         
00083 typedef struct _line
00084 {
00085         struct _line *next;
00086         unsigned long mask;
00087         unsigned short loop;
00088         unsigned short line;
00089         unsigned short argc;
00090         scriptmethod_t method;
00091         char *cmd;
00092         char **args;
00093 } scriptline_t;
00094 
00095 typedef struct _script
00096 {
00097         struct _script *next;
00098         struct _line *first;
00099         struct _line *trap[TRAP_BITS];
00100         struct _line *skip[10];
00101         unsigned long mask;
00102         char *name;
00103 } scriptname_t;
00104 
00105 typedef struct
00106 {
00107         const char *keyword;
00108         scriptmethod_t method;
00109         scriptcheck_t check;
00110 }       SCRKEYWORDS;
00111 
00112 #pragma pack()
00113 
00121 class ScriptLocks : private Mutex
00122 {
00123 private:
00124         friend class ScriptInterp;
00125 
00126         ScriptInterp *locks[MAX_LOCKS];
00127         
00128         void Release(ScriptInterp *interp);
00129         bool Lock(ScriptInterp *interp, unsigned id);
00130         bool Unlock(ScriptInterp *interp, unsigned id);
00131 
00132         ScriptLocks();
00133 };
00134 
00146 class ScriptCommand : public Keydata, public Mutex
00147 {
00148 private:
00149         friend class ScriptImage;
00150         friend class ScriptInterp;
00151 
00152 #pragma pack(1)
00153         typedef struct _keyword
00154         {
00155                 struct _keyword *next;
00156                 scriptmethod_t method;
00157                 scriptcheck_t check;
00158                 char keyword[1];
00159         } keyword_t;
00160 #pragma pack()
00161 
00162 
00163         keyword_t *keywords[KEYWORD_INDEX_SIZE];
00164         char *traps[TRAP_BITS];
00165         ScriptImage *active;
00166         int keyword_count;
00167         int trap_count;
00168 
00176         scriptmethod_t getHandler(const char *keyword);
00177 
00184         char *Check(char *command, scriptline_t *line);
00185 
00186 protected:
00193         virtual unsigned getTrapId(const char *trap);
00194 
00200         virtual unsigned long getTrapDefault(void)
00201                 {return 0x00000003;};
00202         
00208         virtual unsigned long getTrapHandler(scriptname_t *scr)
00209                 {return getTrapDefault();}
00210 
00218         virtual unsigned long getTrapMask(unsigned id);
00219 
00228         virtual unsigned long getTrapModifier(const char *trapname)
00229                 {return getTrapMask(trapname);};
00230 
00239         virtual unsigned long getTrapMask(const char *trapname);
00240 
00244         char *chkIgnore(scriptline_t *line);
00245 
00252         char *chkHasModify(scriptline_t *line);
00253 
00259         char *chkHasVars(scriptline_t *line);
00260 
00268         char *chkHasList(scriptline_t *line);
00269 
00277         char *chkNoArgs(scriptline_t *line);
00278 
00286         char *chkHasArgs(scriptline_t *line);
00287 
00295         void Load(SCRKEYWORDS *keywords);
00296 
00305         int Trap(const char *name);
00306 
00312         inline int getCount(void)
00313                 {return trap_count;};
00314 
00321         virtual char *Check(scriptcheck_t check, scriptline_t *line)
00322                 {return (this->*(check))(line);};
00323 
00332         ScriptCommand(const char *cfgfile);
00333 };
00334 
00344 class ScriptSymbol : public SharedMemPager
00345 {
00346 private:
00347 
00348         int symsize;
00349         scriptsymbol_t *index[SYMBOL_INDEX_SIZE];
00350 
00351         unsigned getIndex(const char *symbol);
00352 
00353 protected:
00368         virtual scriptsymbol_t *getEntry(const char *symbol, int size = 0);
00369 
00379         virtual void Commit(scriptsymbol_t *sym)
00380                 {return;};
00381 
00387         inline int getSymbolSize(void)
00388                 {return symsize;};
00389 
00390 public:
00391         ScriptSymbol(int size, int pgsize = 1024);
00392         
00399         char *getSymbol(const char *symbol);
00400 
00408         char *setSymbol(const char *symbol, const char *value = "");
00409 
00417         char *setSymbol(const char *symbol, int size = 0);
00418 
00426         void clrSymbol(const char *id);
00427 
00431         void Purge(void);
00432 };
00433 
00443 class ScriptImage : public MemPager
00444 {
00445 private:
00446         ifstream scrFile;       
00447         ScriptCommand *cmds;
00448         int refcount;
00449         scriptname_t *index[SCRIPT_INDEX_SIZE];
00450         char buffer[512];
00451         char *bp;
00452         bool quote;
00453 
00454         friend class ScriptInterp;
00455 
00456         char *getToken(void);
00457 
00458 protected:
00466         ScriptImage(ScriptCommand *cmdset);
00467         
00475         scriptname_t *Include(const char *scrfile);
00476 
00485         int Compile(const char *scrfile);
00486 
00496         int Compile(const char *scrfile, char *name);
00497 
00503         void Commit(void);
00504 
00513         virtual scriptname_t *getScript(const char *name);
00514 };      
00515 
00523 class ScriptInterp : public ScriptSymbol
00524 {
00525 private:
00526         typedef struct
00527         {
00528                 scriptname_t *script;
00529                 scriptline_t *line;
00530                 unsigned index, argc;
00531                 char **argv;
00532         }       scriptcontext_t;
00533 
00534         static ScriptLocks locks;
00535         ScriptCommand *cmd;
00536         ScriptImage *image;
00537         scriptcontext_t script[SCRIPT_STACK_SIZE + 1];
00538         int stack;
00539         bool once, loop;
00540         char packtoken;
00541         unsigned long signalmask;
00542 
00543         bool scrPacked(void);
00544         bool scrPack(void);
00545         bool scrUnpack(void);
00546         bool scrOn(void);
00547         bool scrSlog(void);     
00548         bool scrElog(void);
00549         bool scrInc(void);
00550         bool scrDec(void);
00551         bool scrSet(void);
00552         bool scrSize(void);
00553         bool scrInit(void);
00554         bool scrClear(void);
00555         bool scrCall(void);
00556         bool scrIf(void);
00557         bool scrFor(void);
00558         bool scrDo(void);
00559         bool scrLoop(void);
00560         bool scrBreak(void);
00561         bool scrContinue(void);
00562         bool scrReturn(void);
00563         bool scrPop(void);
00564         bool scrSelect(void);
00565         bool scrOnce(void);
00566         bool scrTryLock(void);
00567         bool scrWaitLock(void);
00568         bool scrUnlock(void);
00569 
00570         friend class ScriptCommand;
00571 
00572 protected:
00579         ScriptInterp(ScriptCommand *cmd, int symsize, int pgsize = 1024);
00580 
00586         bool getOnce(void);
00587 
00593         inline void Notify(unsigned long mask)
00594                 {signalmask |= mask;};
00595 
00601         inline void Notify(const char *str)
00602                 {signalmask |= cmd->getTrapMask(str);};
00603 
00609         inline unsigned long getMask(void)
00610                 {return script[stack].line->mask;};
00611 
00617         inline ScriptCommand *getCommand(void)
00618                 {return cmd;};
00619 
00627         bool Conditional(void);
00628 
00634         bool scrExit(void);
00635 
00639         bool scrGoto(void);
00640 
00646         virtual unsigned getId(void)
00647                 {return 0;};
00648 
00649         
00657         scriptsymbol_t *getVariable(int size = 0);
00658 
00659 
00668         virtual scriptsymbol_t *getIndirect(char *sym)
00669                 {return NULL;};
00670 
00674         void Advance(void);
00675 
00682         void Error(const char *error);
00683 
00691         void Trap(unsigned id);
00692 
00699         void Trap(const char *trapname);
00700 
00706         bool Push(void);
00707 
00713         bool Pull(void);
00714 
00724         bool Signal(const char *trapname);
00725 
00733         bool Signal(unsigned trapid);
00734 
00742         virtual bool Execute(scriptmethod_t method)
00743                 {return (this->*(method))();};
00744 
00753         virtual void Stop(unsigned long mask)
00754                 {return;};
00755 
00760         virtual void Exit(void) = 0;
00761 
00762 public:
00770         bool Attach(const char *scrname);
00771 
00776         void Detach(void);
00777 
00784         bool Redirect(const char *scrname);
00785 
00794         bool Step(const char *trapname = NULL);
00795 
00801         inline bool isActive(void)
00802                 {return script[stack].line;};
00803                 
00811         char *getOption(const char *def = NULL);
00812 
00820         char *getValue(const char *def = NULL);
00821 
00828         char *getContent(char *sym);
00829 
00835         inline scriptline_t *getScript(void)
00836                 {return script[stack].line;};
00837 
00843         inline void autoloop(bool enable)
00844                 {loop = enable;};
00845 };      
00846 
00847 #ifdef  __CCXX_NAMESPACE_H__
00848 #undef  __CCXX_NAMESPACE_H__
00849 #include <cc++/namespace.h>
00850 #endif
00851 
00852 #endif
00853 

Generated at Fri Dec 15 09:10:19 2000 for CommonC++ by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000