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 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