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 #ifndef __CCXX_FILE_H__
00053 #include <cc++/file.h>
00054 #endif
00055
00056 #include <iostream>
00057 #include <fstream>
00058
00059 class ScriptCommand;
00060 class ScriptImage;
00061 class ScriptInterp;
00062 struct _line;
00063
00064 #define MAX_LOCKS 8
00065 #define TRAP_BITS (sizeof(unsigned long) * 8)
00066 #define SCRIPT_STACK_SIZE 20
00067 #define SCRIPT_TEMP_SPACE 16
00068 #define KEYWORD_INDEX_SIZE 37
00069 #define SYMBOL_INDEX_SIZE 187
00070 #define SCRIPT_INDEX_SIZE KEYWORD_INDEX_SIZE
00071
00072 typedef bool (ScriptInterp::*scriptmethod_t)(void);
00073 typedef char *(ScriptCommand::*scriptcheck_t)(struct _line *line, ScriptImage *img);
00074
00075 typedef enum
00076 {
00077 SYM_TYPE_NORMAL = 0,
00078 SYM_TYPE_ALIAS,
00079 SYM_TYPE_FIFO,
00080 SYM_TYPE_INDEX,
00081 SYM_TYPE_SEQUENCE,
00082 SYM_TYPE_STACK,
00083 SYM_TYPE_COUNTER,
00084 SYM_TYPE_TRIGGER
00085 } scriptsymtype_t;
00086
00087 #pragma pack(1)
00088 typedef struct _symbol
00089 {
00090 struct _symbol *next;
00091 char *id;
00092 struct
00093 {
00094 unsigned size : 16;
00095 bool initial : 1;
00096 bool system : 1;
00097 bool readonly : 1;
00098 bool commit : 1;
00099 scriptsymtype_t type : 6;
00100 } flags;
00101 char data[1];
00102 } scriptsymbol_t;
00103
00104 typedef struct _line
00105 {
00106 struct _line *next;
00107 unsigned long cmask;
00108 unsigned long mask;
00109 unsigned short loop;
00110 unsigned short line;
00111 unsigned char argc;
00112 bool error : 1;
00113 scriptmethod_t method;
00114 char *cmd;
00115 char **args;
00116 } scriptline_t;
00117
00118 typedef struct _script
00119 {
00120 struct _script *next;
00121 struct _line *first;
00122 struct _line *trap[TRAP_BITS];
00123 struct _line *skip[10];
00124 unsigned long mask;
00125 char *name;
00126 enum
00127 {
00128 SCRIPT_TYPE_ORIGINAL,
00129 SCRIPT_TYPE_COPIED,
00130 SCRIPT_TYPE_COPY
00131 } mode : 2;
00132 } scriptname_t;
00133
00134 typedef struct
00135 {
00136 const char *keyword;
00137 scriptmethod_t method;
00138 scriptcheck_t check;
00139 } SCRKEYWORDS;
00140
00141 #pragma pack()
00142
00150 class __EXPORT ScriptLocks : private Mutex
00151 {
00152 private:
00153 friend class ScriptInterp;
00154
00155 ScriptInterp *locks[MAX_LOCKS];
00156
00157 void Release(ScriptInterp *interp);
00158 bool Lock(ScriptInterp *interp, unsigned id);
00159 bool Unlock(ScriptInterp *interp, unsigned id);
00160
00161 ScriptLocks();
00162 };
00163
00173 class __EXPORT ScriptSession
00174 {
00175 private:
00176 friend class ScriptInterp;
00177 ScriptInterp *interp;
00178
00179 protected:
00186 void stepScheduler(const char *sighandler = NULL);
00187
00194 void sleepScheduler(timeout_t delay);
00195
00199 ScriptSession(ScriptInterp *interp);
00200
00201 virtual ~ScriptSession()
00202 {return;};
00203
00204 public:
00208 virtual void waitHandler(void) = 0;
00209 };
00210
00218 class __EXPORT ScriptProperty
00219 {
00220 private:
00221 friend class ScriptInterp;
00222
00223 static ScriptProperty *first;
00224 ScriptProperty *next;
00225 const char *id;
00226
00227 protected:
00236 virtual void setProperty(char *data, char *temp, size_t size) = 0;
00237
00245 virtual void getProperty(char *data, char *temp, size_t size) = 0;
00246
00247 ScriptProperty(const char *name);
00248
00249 public:
00250 static ScriptProperty* find(const char *name);
00251 };
00252
00260 class __EXPORT ScriptModule
00261 {
00262 private:
00263 friend class ScriptInterp;
00264 friend class ScriptCommand;
00265 static ScriptModule *first;
00266 ScriptModule *next;
00267 const char *cmd;
00268
00269 protected:
00275 virtual void moduleAttach(ScriptInterp *interp)
00276 {return;};
00277
00283 virtual void moduleDetach(ScriptInterp *interp)
00284 {return;};
00285
00294 virtual char *getSession(ScriptInterp *interp, scriptline_t *line, ScriptSession **session)
00295 {return NULL;};
00296
00304 virtual char *checkScript(scriptline_t *line, ScriptImage *img)
00305 {return NULL;};
00306
00312 ScriptModule(const char *name);
00313
00320 static ScriptModule *find(const char *name);
00321 };
00322
00334 class __EXPORT ScriptCommand : public Keydata, public Mutex
00335 {
00336 private:
00337 friend class ScriptImage;
00338 friend class ScriptInterp;
00339 friend class ScriptModule;
00340
00341 #pragma pack(1)
00342 typedef struct _keyword
00343 {
00344 struct _keyword *next;
00345 scriptmethod_t method;
00346 scriptcheck_t check;
00347 char keyword[1];
00348 } keyword_t;
00349 #pragma pack()
00350
00351
00352 keyword_t *keywords[KEYWORD_INDEX_SIZE];
00353 char *traps[TRAP_BITS];
00354 ScriptImage *active;
00355 int keyword_count;
00356 int trap_count;
00357
00358 protected:
00366 scriptmethod_t getHandler(const char *keyword);
00367
00375 char *Check(char *command, scriptline_t *line, ScriptImage *img);
00376
00383 virtual unsigned getTrapId(const char *trap);
00384
00390 virtual unsigned long getTrapDefault(void)
00391 {return 0x00000003;};
00392
00398 virtual unsigned long getTrapHandler(scriptname_t *scr)
00399 {return getTrapDefault();}
00400
00408 virtual unsigned long getTrapMask(unsigned id);
00409
00418 virtual unsigned long getTrapModifier(const char *trapname)
00419 {return getTrapMask(trapname);};
00420
00429 virtual unsigned long getTrapMask(const char *trapname);
00430
00434 char *chkIgnore(scriptline_t *line, ScriptImage *img);
00435
00439 char *chkModule(scriptline_t *line, ScriptImage *img);
00440
00444 char *chkUse(scriptline_t *line, ScriptImage *img);
00445
00452 char *chkHasModify(scriptline_t *line, ScriptImage *img);
00453
00459 char *chkHasVars(scriptline_t *line, ScriptImage *img);
00460
00468 char *chkHasList(scriptline_t *line, ScriptImage *img);
00469
00477 char *chkNoArgs(scriptline_t *line, ScriptImage *img);
00478
00486 char *chkHasArgs(scriptline_t *line, ScriptImage *img);
00487
00495 void Load(SCRKEYWORDS *keywords);
00496
00505 int Trap(const char *name);
00506
00512 inline int getCount(void)
00513 {return trap_count;};
00514
00521 virtual char *Check(scriptcheck_t check, scriptline_t *line, ScriptImage *img)
00522 {return (this->*(check))(line, img);};
00523
00532 ScriptCommand(const char *cfgfile);
00533 };
00534
00544 class __EXPORT ScriptSymbol : public SharedMemPager
00545 {
00546 private:
00547 friend class ScriptInterp;
00548
00549 int symsize;
00550 scriptsymbol_t *index[SYMBOL_INDEX_SIZE];
00551 scriptsymbol_t *trigger;
00552
00553 unsigned getIndex(const char *symbol);
00554
00555 protected:
00570 virtual scriptsymbol_t *getEntry(const char *symbol, int size = 0);
00571
00581 virtual void Commit(scriptsymbol_t *sym);
00582
00583 public:
00589 scriptsymbol_t *getTrigger(void);
00590
00596 inline int getSymbolSize(void)
00597 {return symsize;};
00598
00599 ScriptSymbol(int size, int pgsize = 1024);
00600
00607 char *getSymbol(const char *symbol);
00608
00616 char *setSymbol(const char *symbol, const char *value = "");
00617
00625 char *setConst(const char *symbol, const char *value = "");
00626
00635 bool makeSequence(const char *id, unsigned char count, unsigned char recsize);
00636
00645 bool makeStack(const char *id, unsigned char count, unsigned char recsize);
00646
00655 bool makeFifo(const char *id, unsigned char count, unsigned char recsize);
00656
00663 bool makeCounter(const char *id);
00664
00672 bool postSymbol(scriptsymbol_t *sym, const char *value);
00673
00680 char *readSymbol(scriptsymbol_t *sym);
00681
00689 bool setAlias(const char *symbol, const char *source);
00690
00697 scriptsymbol_t *getAlias(const char *symbol);
00698
00706 char *setSymbol(const char *symbol, int size = 0);
00707
00715 void clrSymbol(const char *id);
00716
00720 void Purge(void);
00721 };
00722
00732 class __EXPORT ScriptImage : public MemPager
00733 {
00734 protected:
00735 std::ifstream scrSource;
00736 std::istream *scrStream;
00737 ScriptCommand *cmds;
00738 int refcount;
00739 scriptname_t *index[SCRIPT_INDEX_SIZE];
00740 char buffer[512];
00741 char *bp;
00742 bool quote;
00743 Mutex duplock;
00744
00745 friend class ScriptInterp;
00746 friend class ScriptModule;
00747
00748 char *getToken(void);
00749
00756 scriptmethod_t getHandler(const char *keyword)
00757 {return cmds->getHandler(keyword);};
00758
00766 ScriptImage(ScriptCommand *cmdset);
00767
00771 void Purge(void);
00772
00780 scriptname_t *Include(const char *scrfile);
00781
00790 int Compile(const char *scrfile);
00791
00801 int Compile(const char *scrfile, char *name);
00802
00810 int Compile(std::istream *str, char *name, const char *scrname = NULL);
00811
00817 void Commit(void);
00818
00819 public:
00826 virtual scriptname_t *getScript(const char *name);
00827
00835 virtual scriptname_t *dupScript(const char *name, const char *target);
00836
00845 unsigned Gather(const char *suffix, scriptname_t **array, unsigned size);
00846
00853 inline std::istream *getSource(void)
00854 {return (std::istream *)&scrSource;};
00855 };
00856
00864 class __EXPORT ScriptInterp : public ScriptSymbol
00865 {
00866 private:
00867 friend class ScriptImage;
00868 friend class ScriptSession;
00869 friend class ScriptModule;
00870
00871 #pragma pack(1)
00872 typedef struct
00873 {
00874 scriptname_t *script;
00875 scriptline_t *line, *read;
00876 unsigned char index;
00877 ScriptSymbol *local;
00878 }
00879 scriptcontext_t;
00880 #pragma pack()
00881
00882 static ScriptLocks locks;
00883 ScriptCommand *cmd;
00884 ScriptImage *image;
00885 ScriptSession *session;
00886 scriptcontext_t script[SCRIPT_STACK_SIZE + 1];
00887 char *temps[SCRIPT_TEMP_SPACE];
00888 int tempidx;
00889 int stack;
00890 size_t symsize, pgsize;
00891 bool once, loop;
00892 char packtoken;
00893 unsigned long signalmask;
00894
00895 bool scrTemplate(void);
00896 bool scrEnable(void);
00897 bool scrDisable(void);
00898 bool scrUse(void);
00899 bool scrLoadable(void);
00900 bool scrPacked(void);
00901 bool scrPack(void);
00902 bool scrUnpack(void);
00903 bool scrOn(void);
00904 bool scrSlog(void);
00905 bool scrBasename(void);
00906 bool scrDirname(void);
00907 bool scrFullpath(void);
00908 bool scrGather(void);
00909 bool scrInc(void);
00910 bool scrDec(void);
00911 bool scrFifo(void);
00912 bool scrCounter(void);
00913 bool scrPost(void);
00914 bool scrStack(void);
00915 bool scrSequence(void);
00916 bool scrDup(void);
00917 bool scrArray(void);
00918 bool scrList(void);
00919 bool scrArm(void);
00920 bool scrDisarm(void);
00921 bool scrSet(void);
00922 bool scrToday(void);
00923 bool scrAlias(void);
00924 bool scrConst(void);
00925 bool scrSize(void);
00926 bool scrInit(void);
00927 bool scrClear(void);
00928 bool scrCall(void);
00929 bool scrHas(void);
00930 bool scrMissing(void);
00931 bool scrIf(void);
00932 bool scrIfThen(void);
00933 bool scrFor(void);
00934 bool scrRead(void);
00935 bool scrRepeat(void);
00936 bool scrForeach(void);
00937 bool scrTryeach(void);
00938 bool scrSwap(void);
00939 bool scrDo(void);
00940 bool scrLoop(void);
00941 bool scrBreak(void);
00942 bool scrContinue(void);
00943 bool scrReturn(void);
00944 bool scrPop(void);
00945 bool scrSelect(void);
00946 bool scrOnce(void);
00947 bool scrLock(void);
00948 bool scrTry(void);
00949 bool scrSkip(void);
00950
00951 friend class ScriptCommand;
00952
00953 protected:
00960 ScriptInterp(ScriptCommand *cmd, size_t symsize, size_t pgsize = 1024);
00961
00967 void getTrigger(bool use);
00968
00974 bool getOnce(void);
00975
00981 inline void Notify(unsigned long mask)
00982 {signalmask |= mask;};
00983
00989 inline void Notify(const char *str)
00990 {signalmask |= cmd->getTrapMask(str);};
00991
00997 inline unsigned long getMask(void);
00998
01004 inline ScriptCommand *getCommand(void)
01005 {return cmd;};
01006
01014 bool Conditional(void);
01015
01021 bool scrExit(void);
01022
01026 bool scrGoto(void);
01027
01031 bool scrData(void);
01032
01038 virtual unsigned getId(void)
01039 {return 0;};
01040
01041
01048 virtual bool getGlobalTrap(unsigned id)
01049 {return false;};
01050
01058 scriptsymbol_t *getVariable(size_t size = 0);
01059
01060
01069 virtual scriptsymbol_t *getIndirect(char *sym)
01070 {return NULL;};
01071
01075 void Advance(void);
01076
01083 void Error(const char *error);
01084
01092 void Trap(unsigned id);
01093
01100 void Trap(const char *trapname);
01101
01107 bool Push(void);
01108
01114 bool Pull(void);
01115
01125 bool Signal(const char *trapname);
01126
01134 bool Signal(unsigned trapid);
01135
01143 virtual bool Execute(scriptmethod_t method)
01144 {return (this->*(method))();};
01145
01154 virtual void Stop(unsigned long mask)
01155 {return;};
01156
01161 virtual void Exit(void) = 0;
01162
01170 virtual scriptname_t *getScriptImage(const char *label);
01171
01178 scriptname_t *getScriptCopy(const char *src);
01179
01185 virtual void sleepScheduler(timeout_t timeout)
01186 {return;};
01187
01193 virtual void stepScheduler(const char *trapname)
01194 {Trap(trapname);};
01195
01196 public:
01206 scriptsymbol_t *getLocal(const char *name, size_t size = 0);
01207
01215 bool Attach(const char *scrname);
01216
01221 void Detach(void);
01222
01229 bool Redirect(const char *scrname);
01230
01239 bool Step(const char *trapname = NULL);
01240
01246 inline bool isActive(void)
01247 {return script[stack].line;};
01248
01256 char *getOption(const char *def = NULL);
01257
01265 char *getKeyword(const char *keyword);
01266
01270 int initKeywords(int size);
01271
01279 char *getValue(const char *def = NULL);
01280
01287 char *getContent(char *sym);
01288
01294 inline scriptline_t *getScript(void)
01295 {return script[stack].line;};
01296
01302 const char *getMember(void);
01303
01309 inline scriptname_t *getObject(void)
01310 {return script[stack].script;};
01311
01318 inline ScriptImage *getImage(void)
01319 {return image;};
01320
01326 inline void autoloop(bool enable)
01327 {loop = enable;};
01328 };
01329
01339 class ScriptPackage : protected DSO
01340 {
01341 private:
01342 static ScriptPackage *first;
01343 ScriptPackage *next;
01344 char *filename;
01345
01346 ScriptPackage(char *name);
01347
01348 public:
01349 static bool usePackage(const char *name);
01350 };
01351
01352 #ifdef __CCXX_NAMESPACE_H__
01353 #undef __CCXX_NAMESPACE_H__
01354 #include <cc++/namespace.h>
01355 #endif
01356
01357 #endif
01358