00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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