|
script.hGo to the documentation of this file.00001 // Copyright (C) 1999-2001 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 #include <iostream.h> 00053 #include <fstream.h> 00054 00055 class ScriptCommand; 00056 class ScriptImage; 00057 class ScriptInterp; 00058 struct _line; 00059 00060 #define MAX_LOCKS 8 00061 #define TRAP_BITS (sizeof(unsigned long) * 8) 00062 #define SCRIPT_STACK_SIZE 10 00063 #define KEYWORD_INDEX_SIZE 37 00064 #define SYMBOL_INDEX_SIZE 187 00065 #define SCRIPT_INDEX_SIZE KEYWORD_INDEX_SIZE 00066 00067 typedef bool (ScriptInterp::*scriptmethod_t)(void); 00068 typedef char *(ScriptCommand::*scriptcheck_t)(struct _line *line, ScriptImage *img); 00069 00070 #pragma pack(1) 00071 typedef struct _symbol 00072 { 00073 struct _symbol *next; 00074 char *id; 00075 struct 00076 { 00077 unsigned size : 16; 00078 bool initial : 1; 00079 bool system : 1; 00080 bool readonly : 1; 00081 bool commit : 1; 00082 bool alias : 1; 00083 } flags; 00084 char data[1]; 00085 } scriptsymbol_t; 00086 00087 typedef struct _line 00088 { 00089 struct _line *next; 00090 unsigned long mask; 00091 unsigned short loop; 00092 unsigned short line; 00093 unsigned short argc; 00094 scriptmethod_t method; 00095 char *cmd; 00096 char **args; 00097 } scriptline_t; 00098 00099 typedef struct _script 00100 { 00101 struct _script *next; 00102 struct _line *first; 00103 struct _line *trap[TRAP_BITS]; 00104 struct _line *skip[10]; 00105 unsigned long mask; 00106 char *name; 00107 } scriptname_t; 00108 00109 typedef struct 00110 { 00111 const char *keyword; 00112 scriptmethod_t method; 00113 scriptcheck_t check; 00114 } SCRKEYWORDS; 00115 00116 #pragma pack() 00117 00125 class __EXPORT ScriptLocks : private Mutex 00126 { 00127 private: 00128 friend class ScriptInterp; 00129 00130 ScriptInterp *locks[MAX_LOCKS]; 00131 00132 void Release(ScriptInterp *interp); 00133 bool Lock(ScriptInterp *interp, unsigned id); 00134 bool Unlock(ScriptInterp *interp, unsigned id); 00135 00136 ScriptLocks(); 00137 }; 00138 00150 class __EXPORT ScriptCommand : public Keydata, public Mutex 00151 { 00152 private: 00153 friend class ScriptImage; 00154 friend class ScriptInterp; 00155 00156 #pragma pack(1) 00157 typedef struct _keyword 00158 { 00159 struct _keyword *next; 00160 scriptmethod_t method; 00161 scriptcheck_t check; 00162 char keyword[1]; 00163 } keyword_t; 00164 #pragma pack() 00165 00166 00167 keyword_t *keywords[KEYWORD_INDEX_SIZE]; 00168 char *traps[TRAP_BITS]; 00169 ScriptImage *active; 00170 int keyword_count; 00171 int trap_count; 00172 00180 scriptmethod_t getHandler(const char *keyword); 00181 00189 char *Check(char *command, scriptline_t *line, ScriptImage *img); 00190 00191 protected: 00198 virtual unsigned getTrapId(const char *trap); 00199 00205 virtual unsigned long getTrapDefault(void) 00206 {return 0x00000003;}; 00207 00213 virtual unsigned long getTrapHandler(scriptname_t *scr) 00214 {return getTrapDefault();} 00215 00223 virtual unsigned long getTrapMask(unsigned id); 00224 00233 virtual unsigned long getTrapModifier(const char *trapname) 00234 {return getTrapMask(trapname);}; 00235 00244 virtual unsigned long getTrapMask(const char *trapname); 00245 00249 char *chkIgnore(scriptline_t *line, ScriptImage *img); 00250 00257 char *chkHasModify(scriptline_t *line, ScriptImage *img); 00258 00264 char *chkHasVars(scriptline_t *line, ScriptImage *img); 00265 00273 char *chkHasList(scriptline_t *line, ScriptImage *img); 00274 00282 char *chkNoArgs(scriptline_t *line, ScriptImage *img); 00283 00291 char *chkHasArgs(scriptline_t *line, ScriptImage *img); 00292 00300 void Load(SCRKEYWORDS *keywords); 00301 00310 int Trap(const char *name); 00311 00317 inline int getCount(void) 00318 {return trap_count;}; 00319 00326 virtual char *Check(scriptcheck_t check, scriptline_t *line, ScriptImage *img) 00327 {return (this->*(check))(line, img);}; 00328 00337 ScriptCommand(const char *cfgfile); 00338 }; 00339 00349 class __EXPORT ScriptSymbol : public SharedMemPager 00350 { 00351 private: 00352 00353 int symsize; 00354 scriptsymbol_t *index[SYMBOL_INDEX_SIZE]; 00355 00356 unsigned getIndex(const char *symbol); 00357 00358 protected: 00373 virtual scriptsymbol_t *getEntry(const char *symbol, int size = 0); 00374 00384 virtual void Commit(scriptsymbol_t *sym) 00385 {return;}; 00386 00392 inline int getSymbolSize(void) 00393 {return symsize;}; 00394 00395 public: 00396 ScriptSymbol(int size, int pgsize = 1024); 00397 00404 char *getSymbol(const char *symbol); 00405 00413 char *setSymbol(const char *symbol, const char *value = ""); 00414 00422 char *setConst(const char *symbol, const char *value = ""); 00423 00431 char *setSymbol(const char *symbol, int size = 0); 00432 00440 void clrSymbol(const char *id); 00441 00445 void Purge(void); 00446 }; 00447 00457 class __EXPORT ScriptImage : public MemPager 00458 { 00459 protected: 00460 ifstream scrFile; 00461 ScriptCommand *cmds; 00462 int refcount; 00463 scriptname_t *index[SCRIPT_INDEX_SIZE]; 00464 char buffer[512]; 00465 char *bp; 00466 bool quote; 00467 00468 friend class ScriptInterp; 00469 00470 char *getToken(void); 00471 00479 ScriptImage(ScriptCommand *cmdset); 00480 00488 scriptname_t *Include(const char *scrfile); 00489 00498 int Compile(const char *scrfile); 00499 00509 int Compile(const char *scrfile, char *name); 00510 00516 void Commit(void); 00517 00526 virtual scriptname_t *getScript(const char *name); 00527 00528 public: 00535 inline ifstream &getSource(void) 00536 {return scrFile;}; 00537 }; 00538 00546 class __EXPORT ScriptInterp : public ScriptSymbol 00547 { 00548 private: 00549 typedef struct 00550 { 00551 scriptname_t *script; 00552 scriptline_t *line; 00553 unsigned index, argc; 00554 char **argv; 00555 } scriptcontext_t; 00556 00557 static ScriptLocks locks; 00558 ScriptCommand *cmd; 00559 ScriptImage *image; 00560 scriptcontext_t script[SCRIPT_STACK_SIZE + 1]; 00561 int stack; 00562 bool once, loop; 00563 char packtoken; 00564 unsigned long signalmask; 00565 00566 bool scrPacked(void); 00567 bool scrPack(void); 00568 bool scrUnpack(void); 00569 bool scrOn(void); 00570 bool scrSlog(void); 00571 bool scrElog(void); 00572 bool scrInc(void); 00573 bool scrDec(void); 00574 bool scrSet(void); 00575 bool scrConst(void); 00576 bool scrSize(void); 00577 bool scrInit(void); 00578 bool scrClear(void); 00579 bool scrCall(void); 00580 bool scrHas(void); 00581 bool scrMissing(void); 00582 bool scrIf(void); 00583 bool scrFor(void); 00584 bool scrDo(void); 00585 bool scrLoop(void); 00586 bool scrBreak(void); 00587 bool scrContinue(void); 00588 bool scrReturn(void); 00589 bool scrPop(void); 00590 bool scrSelect(void); 00591 bool scrOnce(void); 00592 bool scrTryLock(void); 00593 bool scrWaitLock(void); 00594 bool scrUnlock(void); 00595 00596 friend class ScriptCommand; 00597 00598 protected: 00605 ScriptInterp(ScriptCommand *cmd, int symsize, int pgsize = 1024); 00606 00612 bool getOnce(void); 00613 00619 inline void Notify(unsigned long mask) 00620 {signalmask |= mask;}; 00621 00627 inline void Notify(const char *str) 00628 {signalmask |= cmd->getTrapMask(str);}; 00629 00635 inline unsigned long getMask(void) 00636 {return script[stack].line->mask;}; 00637 00643 inline ScriptCommand *getCommand(void) 00644 {return cmd;}; 00645 00653 bool Conditional(void); 00654 00660 bool scrExit(void); 00661 00665 bool scrGoto(void); 00666 00670 bool scrData(void); 00671 00677 virtual unsigned getId(void) 00678 {return 0;}; 00679 00680 00688 scriptsymbol_t *getVariable(int size = 0); 00689 00690 00699 virtual scriptsymbol_t *getIndirect(char *sym) 00700 {return NULL;}; 00701 00705 void Advance(void); 00706 00713 void Error(const char *error); 00714 00722 void Trap(unsigned id); 00723 00730 void Trap(const char *trapname); 00731 00737 bool Push(void); 00738 00744 bool Pull(void); 00745 00755 bool Signal(const char *trapname); 00756 00764 bool Signal(unsigned trapid); 00765 00773 virtual bool Execute(scriptmethod_t method) 00774 {return (this->*(method))();}; 00775 00784 virtual void Stop(unsigned long mask) 00785 {return;}; 00786 00791 virtual void Exit(void) = 0; 00792 00793 public: 00801 bool Attach(const char *scrname); 00802 00807 void Detach(void); 00808 00815 bool Redirect(const char *scrname); 00816 00825 bool Step(const char *trapname = NULL); 00826 00832 inline bool isActive(void) 00833 {return script[stack].line;}; 00834 00842 char *getOption(const char *def = NULL); 00843 00851 char *getValue(const char *def = NULL); 00852 00859 char *getContent(char *sym); 00860 00866 inline scriptline_t *getScript(void) 00867 {return script[stack].line;}; 00868 00874 inline scriptname_t *getObject(void) 00875 {return script[stack].script;}; 00876 00883 inline ScriptImage *getImage(void) 00884 {return image;}; 00885 00891 inline void autoloop(bool enable) 00892 {loop = enable;}; 00893 }; 00894 00895 #ifdef __CCXX_NAMESPACE_H__ 00896 #undef __CCXX_NAMESPACE_H__ 00897 #include <cc++/namespace.h> 00898 #endif 00899 00900 #endif 00901 Generated at Thu Apr 5 14:11:40 2001 for ccscript by 1.2.1 written by Dimitri van Heesch, © 1997-2000 |