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 #ifdef __NAMESPACES__
00060 namespace ost {
00061 #endif
00062
00063 class CCXX_CLASS_EXPORT ScriptCommand;
00064 class CCXX_CLASS_EXPORT ScriptImage;
00065 class CCXX_CLASS_EXPORT ScriptInterp;
00066 struct _line;
00067
00068 #define MAX_LOCKS 8
00069 #define TRAP_BITS (sizeof(unsigned long) * 8)
00070 #define SCRIPT_STACK_SIZE 20
00071 #define SCRIPT_TEMP_SPACE 16
00072 #define KEYWORD_INDEX_SIZE 37
00073 #define SYMBOL_INDEX_SIZE 187
00074 #define SCRIPT_INDEX_SIZE KEYWORD_INDEX_SIZE
00075
00076 typedef bool (ScriptInterp::*scriptmethod_t)(void);
00077 typedef char *(ScriptCommand::*scriptcheck_t)(struct _line *line, ScriptImage *img);
00078
00079 typedef enum
00080 {
00081 SYM_TYPE_NORMAL = 0,
00082 SYM_TYPE_ALIAS,
00083 SYM_TYPE_FIFO,
00084 SYM_TYPE_INDEX,
00085 SYM_TYPE_SEQUENCE,
00086 SYM_TYPE_STACK,
00087 SYM_TYPE_COUNTER,
00088 SYM_TYPE_TRIGGER,
00089 SYM_TYPE_POINTER
00090 } scriptsymtype_t;
00091
00092 #pragma pack(1)
00093 typedef struct _symbol
00094 {
00095 struct _symbol *next;
00096 char *id;
00097 struct
00098 {
00099 unsigned size : 16;
00100 bool initial : 1;
00101 bool system : 1;
00102 bool readonly : 1;
00103 bool commit : 1;
00104 scriptsymtype_t type : 6;
00105 } flags;
00106 char data[1];
00107 } scriptsymbol_t;
00108
00109 typedef struct _line
00110 {
00111 struct _line *next;
00112 unsigned long cmask;
00113 unsigned long mask;
00114 unsigned short loop;
00115 unsigned short line;
00116 unsigned char argc;
00117 bool error : 1;
00118 scriptmethod_t method;
00119 char *cmd;
00120 char **args;
00121 } scriptline_t;
00122
00123 typedef struct _script
00124 {
00125 struct _script *next;
00126 struct _line *first;
00127 struct _line *trap[TRAP_BITS];
00128 struct _line *skip[10];
00129 unsigned long mask;
00130 char *name;
00131 enum
00132 {
00133 SCRIPT_TYPE_ORIGINAL,
00134 SCRIPT_TYPE_COPIED,
00135 SCRIPT_TYPE_COPY
00136 } mode : 2;
00137 } scriptname_t;
00138
00139 typedef struct
00140 {
00141 const char *keyword;
00142 scriptmethod_t method;
00143 scriptcheck_t check;
00144 } SCRKEYWORDS;
00145
00146 #pragma pack()
00147
00155 class ScriptLocks : private Mutex
00156 {
00157 private:
00158 friend class ScriptInterp;
00159
00160 ScriptInterp *locks[MAX_LOCKS];
00161
00162 void Release(ScriptInterp *interp);
00163 bool Lock(ScriptInterp *interp, unsigned id);
00164 bool Unlock(ScriptInterp *interp, unsigned id);
00165
00166 ScriptLocks();
00167 };
00168
00178 class CCXX_CLASS_EXPORT ScriptSession
00179 {
00180 private:
00181 friend class ScriptInterp;
00182 ScriptInterp *interp;
00183
00184 protected:
00191 void stepScheduler(const char *sighandler = NULL);
00192
00199 void sleepScheduler(timeout_t delay);
00200
00204 ScriptSession(ScriptInterp *interp);
00205
00206 virtual ~ScriptSession()
00207 {return;};
00208
00209 public:
00213 virtual void waitHandler(void) = 0;
00214 };
00215
00223 class CCXX_CLASS_EXPORT ScriptProperty
00224 {
00225 private:
00226 friend class ScriptInterp;
00227
00228 static ScriptProperty *first;
00229 ScriptProperty *next;
00230 const char *id;
00231
00232 protected:
00241 virtual void setProperty(char *data, char *temp, size_t size) = 0;
00242
00250 virtual void getProperty(char *data, char *temp, size_t size) = 0;
00251
00259 virtual void adjProperty(char *data, size_t size, int adjust)
00260 {return;};
00261
00267 virtual size_t getPropertySize(void)
00268 {return 0;};
00269
00270 ScriptProperty(const char *name);
00271
00272 public:
00273 static ScriptProperty* find(const char *name);
00274 };
00275
00283 class CCXX_CLASS_EXPORT ScriptModule
00284 {
00285 private:
00286 friend class ScriptInterp;
00287 friend class ScriptCommand;
00288 static ScriptModule *first;
00289 ScriptModule *next;
00290 const char *cmd;
00291
00292 protected:
00298 virtual void moduleAttach(ScriptInterp *interp)
00299 {return;};
00300
00306 virtual void moduleDetach(ScriptInterp *interp)
00307 {return;};
00308
00317 virtual char *getSession(ScriptInterp *interp, scriptline_t *line, ScriptSession **session)
00318 {return NULL;};
00319
00327 virtual char *checkScript(scriptline_t *line, ScriptImage *img)
00328 {return NULL;};
00329
00335 ScriptModule(const char *name);
00336
00343 static ScriptModule *find(const char *name);
00344 };
00345
00357 class CCXX_CLASS_EXPORT ScriptCommand : public Keydata, public Mutex
00358 {
00359 private:
00360 friend class ScriptImage;
00361 friend class ScriptInterp;
00362 friend class ScriptModule;
00363
00364 #pragma pack(1)
00365 typedef struct _keyword
00366 {
00367 struct _keyword *next;
00368 scriptmethod_t method;
00369 scriptcheck_t check;
00370 char keyword[1];
00371 } keyword_t;
00372 #pragma pack()
00373
00374
00375 keyword_t *keywords[KEYWORD_INDEX_SIZE];
00376 char *traps[TRAP_BITS];
00377 ScriptImage *active;
00378 int keyword_count;
00379 int trap_count;
00380
00381 protected:
00389 scriptmethod_t getHandler(const char *keyword);
00390
00398 char *Check(char *command, scriptline_t *line, ScriptImage *img);
00399
00406 virtual unsigned getTrapId(const char *trap);
00407
00413 virtual unsigned long getTrapDefault(void)
00414 {return 0x00000003;};
00415
00421 virtual unsigned long getTrapHandler(scriptname_t *scr)
00422 {return getTrapDefault();}
00423
00431 virtual unsigned long getTrapMask(unsigned id);
00432
00441 virtual unsigned long getTrapModifier(const char *trapname)
00442 {return getTrapMask(trapname);};
00443
00452 virtual unsigned long getTrapMask(const char *trapname);
00453
00457 char *chkIgnore(scriptline_t *line, ScriptImage *img);
00458
00462 char *chkModule(scriptline_t *line, ScriptImage *img);
00463
00467 char *chkUse(scriptline_t *line, ScriptImage *img);
00468
00475 char *chkHasModify(scriptline_t *line, ScriptImage *img);
00476
00482 char *chkHasVars(scriptline_t *line, ScriptImage *img);
00483
00491 char *chkHasList(scriptline_t *line, ScriptImage *img);
00492
00500 char *chkNoArgs(scriptline_t *line, ScriptImage *img);
00501
00509 char *chkHasArgs(scriptline_t *line, ScriptImage *img);
00510
00518 void Load(SCRKEYWORDS *keywords);
00519
00528 int Trap(const char *name);
00529
00535 inline int getCount(void)
00536 {return trap_count;};
00537
00544 virtual char *Check(scriptcheck_t check, scriptline_t *line, ScriptImage *img)
00545 {return (this->*(check))(line, img);};
00546
00555 ScriptCommand(const char *cfgfile);
00556 };
00557
00567 class CCXX_CLASS_EXPORT ScriptSymbol : public SharedMemPager
00568 {
00569 private:
00570 friend class ScriptInterp;
00571
00572 int symsize;
00573 scriptsymbol_t *index[SYMBOL_INDEX_SIZE];
00574 scriptsymbol_t *trigger;
00575
00576 unsigned getIndex(const char *symbol);
00577
00578 protected:
00593 virtual scriptsymbol_t *getEntry(const char *symbol, int size = 0);
00594
00604 virtual void Commit(scriptsymbol_t *sym);
00605
00606 public:
00612 scriptsymbol_t *getTrigger(void);
00613
00619 inline int getSymbolSize(void)
00620 {return symsize;};
00621
00622 ScriptSymbol(int size, int pgsize = 1024);
00623
00630 void *getPointer(const char *symbol);
00631
00639 bool setPointer(const char *symbol, void *data);
00640
00647 char *getSymbol(const char *symbol);
00648
00656 char *setSymbol(const char *symbol, const char *value = "");
00657
00665 char *setConst(const char *symbol, const char *value = "");
00666
00675 bool makeSequence(const char *id, unsigned char count, unsigned char recsize);
00676
00685 bool makeStack(const char *id, unsigned char count, unsigned char recsize);
00686
00695 bool makeFifo(const char *id, unsigned char count, unsigned char recsize);
00696
00703 bool makeCounter(const char *id);
00704
00712 bool postSymbol(scriptsymbol_t *sym, const char *value);
00713
00720 char *readSymbol(scriptsymbol_t *sym);
00721
00729 bool setAlias(const char *symbol, const char *source);
00730
00737 scriptsymbol_t *getAlias(const char *symbol);
00738
00746 char *setSymbol(const char *symbol, int size = 0);
00747
00755 void clrSymbol(const char *id);
00756
00760 void Purge(void);
00761 };
00762
00772 class CCXX_CLASS_EXPORT ScriptImage : public MemPager
00773 {
00774 protected:
00775 std::ifstream scrSource;
00776 std::istream *scrStream;
00777 ScriptCommand *cmds;
00778 int refcount;
00779 scriptname_t *index[SCRIPT_INDEX_SIZE];
00780 char buffer[512];
00781 char *bp;
00782 bool quote;
00783 Mutex duplock;
00784
00785 friend class ScriptInterp;
00786 friend class ScriptModule;
00787
00788 char *getToken(void);
00789
00796 scriptmethod_t getHandler(const char *keyword)
00797 {return cmds->getHandler(keyword);};
00798
00806 ScriptImage(ScriptCommand *cmdset);
00807
00811 void Purge(void);
00812
00820 scriptname_t *Include(const char *scrfile);
00821
00830 int Compile(const char *scrfile);
00831
00841 int Compile(const char *scrfile, char *name);
00842
00850 int Compile(std::istream *str, char *name, const char *scrname = NULL);
00851
00857 void Commit(void);
00858
00859 public:
00866 virtual scriptname_t *getScript(const char *name);
00867
00875 virtual scriptname_t *dupScript(const char *name, const char *target);
00876
00885 unsigned Gather(const char *suffix, scriptname_t **array, unsigned size);
00886
00893 inline std::istream *getSource(void)
00894 {return (std::istream *)&scrSource;};
00895 };
00896
00904 class CCXX_CLASS_EXPORT ScriptInterp : public ScriptSymbol
00905 {
00906 private:
00907 friend class ScriptImage;
00908 friend class ScriptSession;
00909 friend class ScriptModule;
00910
00911 #pragma pack(1)
00912 typedef struct
00913 {
00914 scriptname_t *script;
00915 scriptline_t *line, *read;
00916 unsigned char index;
00917 ScriptSymbol *local;
00918 }
00919 scriptcontext_t;
00920 #pragma pack()
00921
00922 static ScriptLocks locks;
00923 ScriptCommand *cmd;
00924 ScriptImage *image;
00925 ScriptSession *session;
00926 scriptcontext_t script[SCRIPT_STACK_SIZE + 1];
00927 char *temps[SCRIPT_TEMP_SPACE];
00928 int tempidx;
00929 int stack;
00930 size_t symsize, pgsize;
00931 bool once, loop;
00932 char packtoken;
00933 unsigned long signalmask;
00934
00935 bool scrTemplate(void);
00936 bool scrEnable(void);
00937 bool scrDisable(void);
00938 bool scrUse(void);
00939 bool scrLoadable(void);
00940 bool scrPacked(void);
00941 bool scrPack(void);
00942 bool scrUnpack(void);
00943 bool scrOn(void);
00944 bool scrSlog(void);
00945 bool scrBasename(void);
00946 bool scrDirname(void);
00947 bool scrFullpath(void);
00948 bool scrGather(void);
00949 bool scrInc(void);
00950 bool scrDec(void);
00951 bool scrFifo(void);
00952 bool scrCounter(void);
00953 bool scrPost(void);
00954 bool scrStack(void);
00955 bool scrSequence(void);
00956 bool scrDup(void);
00957 bool scrArray(void);
00958 bool scrList(void);
00959 bool scrArm(void);
00960 bool scrDisarm(void);
00961 bool scrSet(void);
00962 bool scrAlias(void);
00963 bool scrConst(void);
00964 bool scrSize(void);
00965 bool scrInit(void);
00966 bool scrClear(void);
00967 bool scrCall(void);
00968 bool scrHas(void);
00969 bool scrMissing(void);
00970 bool scrIf(void);
00971 bool scrIfThen(void);
00972 bool scrFor(void);
00973 bool scrRead(void);
00974 bool scrRepeat(void);
00975 bool scrForeach(void);
00976 bool scrTryeach(void);
00977 bool scrSwap(void);
00978 bool scrDo(void);
00979 bool scrLoop(void);
00980 bool scrBreak(void);
00981 bool scrContinue(void);
00982 bool scrReturn(void);
00983 bool scrPop(void);
00984 bool scrSelect(void);
00985 bool scrOnce(void);
00986 bool scrLock(void);
00987 bool scrTry(void);
00988 bool scrSkip(void);
00989
00990 friend class ScriptCommand;
00991
00992 protected:
00999 ScriptInterp(ScriptCommand *cmd, size_t symsize, size_t pgsize = 1024);
01000
01006 void getTrigger(bool use);
01007
01013 bool getOnce(void);
01014
01020 inline void Notify(unsigned long mask)
01021 {signalmask |= mask;};
01022
01028 inline void Notify(const char *str)
01029 {signalmask |= cmd->getTrapMask(str);};
01030
01036 inline unsigned long getMask(void);
01037
01043 inline ScriptCommand *getCommand(void)
01044 {return cmd;};
01045
01053 bool Conditional(void);
01054
01060 bool scrExit(void);
01061
01065 bool scrGoto(void);
01066
01070 bool scrData(void);
01071
01077 virtual unsigned getId(void)
01078 {return 0;};
01079
01080
01087 virtual bool getGlobalTrap(unsigned id)
01088 {return false;};
01089
01097 scriptsymbol_t *getVariable(size_t size = 0);
01098
01099
01108 virtual scriptsymbol_t *getIndirect(char *sym)
01109 {return NULL;};
01110
01114 void Advance(void);
01115
01122 void Error(const char *error);
01123
01131 void Trap(unsigned id);
01132
01139 void Trap(const char *trapname);
01140
01146 bool Push(void);
01147
01153 bool Pull(void);
01154
01164 bool Signal(const char *trapname);
01165
01173 bool Signal(unsigned trapid);
01174
01182 virtual bool Execute(scriptmethod_t method)
01183 {return (this->*(method))();};
01184
01193 virtual void Stop(unsigned long mask)
01194 {return;};
01195
01200 virtual void Exit(void) = 0;
01201
01209 virtual scriptname_t *getScriptImage(const char *label);
01210
01217 scriptname_t *getScriptCopy(const char *src);
01218
01224 virtual void sleepScheduler(timeout_t timeout)
01225 {return;};
01226
01232 virtual void stepScheduler(const char *trapname)
01233 {Trap(trapname);};
01234
01235 public:
01245 scriptsymbol_t *getLocal(const char *name, size_t size = 0);
01246
01254 bool Attach(const char *scrname);
01255
01260 void Detach(void);
01261
01268 bool Redirect(const char *scrname);
01269
01278 bool Step(const char *trapname = NULL);
01279
01285 inline bool isActive(void)
01286 {return script[stack].line;};
01287
01295 char *getOption(const char *def = NULL);
01296
01304 char *getKeyword(const char *keyword);
01305
01309 int initKeywords(int size);
01310
01318 char *getValue(const char *def = NULL);
01319
01326 char *getContent(char *sym);
01327
01333 inline scriptline_t *getScript(void)
01334 {return script[stack].line;};
01335
01341 const char *getMember(void);
01342
01348 inline scriptname_t *getObject(void)
01349 {return script[stack].script;};
01350
01357 inline ScriptImage *getImage(void)
01358 {return image;};
01359
01365 inline void autoloop(bool enable)
01366 {loop = enable;};
01367 };
01368
01378 class ScriptPackage : protected DSO
01379 {
01380 private:
01381 static ScriptPackage *first;
01382 ScriptPackage *next;
01383 char *filename;
01384
01385 ScriptPackage(char *name);
01386
01387 public:
01388 CCXX_MEMBER_EXPORT(static bool) usePackage(const char *name);
01389 };
01390
01391 #ifdef __NAMESPACES__
01392 };
01393 #endif
01394
01395 #endif
01396