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 } scriptsymtype_t;
00090
00091 #pragma pack(1)
00092 typedef struct _symbol
00093 {
00094 struct _symbol *next;
00095 char *id;
00096 struct
00097 {
00098 unsigned size : 16;
00099 bool initial : 1;
00100 bool system : 1;
00101 bool readonly : 1;
00102 bool commit : 1;
00103 scriptsymtype_t type : 6;
00104 } flags;
00105 char data[1];
00106 } scriptsymbol_t;
00107
00108 typedef struct _line
00109 {
00110 struct _line *next;
00111 unsigned long cmask;
00112 unsigned long mask;
00113 unsigned short loop;
00114 unsigned short line;
00115 unsigned char argc;
00116 bool error : 1;
00117 scriptmethod_t method;
00118 char *cmd;
00119 char **args;
00120 } scriptline_t;
00121
00122 typedef struct _script
00123 {
00124 struct _script *next;
00125 struct _line *first;
00126 struct _line *trap[TRAP_BITS];
00127 struct _line *skip[10];
00128 unsigned long mask;
00129 char *name;
00130 enum
00131 {
00132 SCRIPT_TYPE_ORIGINAL,
00133 SCRIPT_TYPE_COPIED,
00134 SCRIPT_TYPE_COPY
00135 } mode : 2;
00136 } scriptname_t;
00137
00138 typedef struct
00139 {
00140 const char *keyword;
00141 scriptmethod_t method;
00142 scriptcheck_t check;
00143 } SCRKEYWORDS;
00144
00145 #pragma pack()
00146
00154 class ScriptLocks : private Mutex
00155 {
00156 private:
00157 friend class ScriptInterp;
00158
00159 ScriptInterp *locks[MAX_LOCKS];
00160
00161 void Release(ScriptInterp *interp);
00162 bool Lock(ScriptInterp *interp, unsigned id);
00163 bool Unlock(ScriptInterp *interp, unsigned id);
00164
00165 ScriptLocks();
00166 };
00167
00177 class CCXX_CLASS_EXPORT ScriptSession
00178 {
00179 private:
00180 friend class ScriptInterp;
00181 ScriptInterp *interp;
00182
00183 protected:
00190 void stepScheduler(const char *sighandler = NULL);
00191
00198 void sleepScheduler(timeout_t delay);
00199
00203 ScriptSession(ScriptInterp *interp);
00204
00205 virtual ~ScriptSession()
00206 {return;};
00207
00208 public:
00212 virtual void waitHandler(void) = 0;
00213 };
00214
00222 class CCXX_CLASS_EXPORT ScriptProperty
00223 {
00224 private:
00225 friend class ScriptInterp;
00226
00227 static ScriptProperty *first;
00228 ScriptProperty *next;
00229 const char *id;
00230
00231 protected:
00240 virtual void setProperty(char *data, char *temp, size_t size) = 0;
00241
00249 virtual void getProperty(char *data, char *temp, size_t size) = 0;
00250
00251 ScriptProperty(const char *name);
00252
00253 public:
00254 static ScriptProperty* find(const char *name);
00255 };
00256
00264 class CCXX_CLASS_EXPORT ScriptModule
00265 {
00266 private:
00267 friend class ScriptInterp;
00268 friend class ScriptCommand;
00269 static ScriptModule *first;
00270 ScriptModule *next;
00271 const char *cmd;
00272
00273 protected:
00279 virtual void moduleAttach(ScriptInterp *interp)
00280 {return;};
00281
00287 virtual void moduleDetach(ScriptInterp *interp)
00288 {return;};
00289
00298 virtual char *getSession(ScriptInterp *interp, scriptline_t *line, ScriptSession **session)
00299 {return NULL;};
00300
00308 virtual char *checkScript(scriptline_t *line, ScriptImage *img)
00309 {return NULL;};
00310
00316 ScriptModule(const char *name);
00317
00324 static ScriptModule *find(const char *name);
00325 };
00326
00338 class CCXX_CLASS_EXPORT ScriptCommand : public Keydata, public Mutex
00339 {
00340 private:
00341 friend class ScriptImage;
00342 friend class ScriptInterp;
00343 friend class ScriptModule;
00344
00345 #pragma pack(1)
00346 typedef struct _keyword
00347 {
00348 struct _keyword *next;
00349 scriptmethod_t method;
00350 scriptcheck_t check;
00351 char keyword[1];
00352 } keyword_t;
00353 #pragma pack()
00354
00355
00356 keyword_t *keywords[KEYWORD_INDEX_SIZE];
00357 char *traps[TRAP_BITS];
00358 ScriptImage *active;
00359 int keyword_count;
00360 int trap_count;
00361
00362 protected:
00370 scriptmethod_t getHandler(const char *keyword);
00371
00379 char *Check(char *command, scriptline_t *line, ScriptImage *img);
00380
00387 virtual unsigned getTrapId(const char *trap);
00388
00394 virtual unsigned long getTrapDefault(void)
00395 {return 0x00000003;};
00396
00402 virtual unsigned long getTrapHandler(scriptname_t *scr)
00403 {return getTrapDefault();}
00404
00412 virtual unsigned long getTrapMask(unsigned id);
00413
00422 virtual unsigned long getTrapModifier(const char *trapname)
00423 {return getTrapMask(trapname);};
00424
00433 virtual unsigned long getTrapMask(const char *trapname);
00434
00438 char *chkIgnore(scriptline_t *line, ScriptImage *img);
00439
00443 char *chkModule(scriptline_t *line, ScriptImage *img);
00444
00448 char *chkUse(scriptline_t *line, ScriptImage *img);
00449
00456 char *chkHasModify(scriptline_t *line, ScriptImage *img);
00457
00463 char *chkHasVars(scriptline_t *line, ScriptImage *img);
00464
00472 char *chkHasList(scriptline_t *line, ScriptImage *img);
00473
00481 char *chkNoArgs(scriptline_t *line, ScriptImage *img);
00482
00490 char *chkHasArgs(scriptline_t *line, ScriptImage *img);
00491
00499 void Load(SCRKEYWORDS *keywords);
00500
00509 int Trap(const char *name);
00510
00516 inline int getCount(void)
00517 {return trap_count;};
00518
00525 virtual char *Check(scriptcheck_t check, scriptline_t *line, ScriptImage *img)
00526 {return (this->*(check))(line, img);};
00527
00536 ScriptCommand(const char *cfgfile);
00537 };
00538
00548 class CCXX_CLASS_EXPORT ScriptSymbol : public SharedMemPager
00549 {
00550 private:
00551 friend class ScriptInterp;
00552
00553 int symsize;
00554 scriptsymbol_t *index[SYMBOL_INDEX_SIZE];
00555 scriptsymbol_t *trigger;
00556
00557 unsigned getIndex(const char *symbol);
00558
00559 protected:
00574 virtual scriptsymbol_t *getEntry(const char *symbol, int size = 0);
00575
00585 virtual void Commit(scriptsymbol_t *sym);
00586
00587 public:
00593 scriptsymbol_t *getTrigger(void);
00594
00600 inline int getSymbolSize(void)
00601 {return symsize;};
00602
00603 ScriptSymbol(int size, int pgsize = 1024);
00604
00611 char *getSymbol(const char *symbol);
00612
00620 char *setSymbol(const char *symbol, const char *value = "");
00621
00629 char *setConst(const char *symbol, const char *value = "");
00630
00639 bool makeSequence(const char *id, unsigned char count, unsigned char recsize);
00640
00649 bool makeStack(const char *id, unsigned char count, unsigned char recsize);
00650
00659 bool makeFifo(const char *id, unsigned char count, unsigned char recsize);
00660
00667 bool makeCounter(const char *id);
00668
00676 bool postSymbol(scriptsymbol_t *sym, const char *value);
00677
00684 char *readSymbol(scriptsymbol_t *sym);
00685
00693 bool setAlias(const char *symbol, const char *source);
00694
00701 scriptsymbol_t *getAlias(const char *symbol);
00702
00710 char *setSymbol(const char *symbol, int size = 0);
00711
00719 void clrSymbol(const char *id);
00720
00724 void Purge(void);
00725 };
00726
00736 class CCXX_CLASS_EXPORT ScriptImage : public MemPager
00737 {
00738 protected:
00739 std::ifstream scrSource;
00740 std::istream *scrStream;
00741 ScriptCommand *cmds;
00742 int refcount;
00743 scriptname_t *index[SCRIPT_INDEX_SIZE];
00744 char buffer[512];
00745 char *bp;
00746 bool quote;
00747 Mutex duplock;
00748
00749 friend class ScriptInterp;
00750 friend class ScriptModule;
00751
00752 char *getToken(void);
00753
00760 scriptmethod_t getHandler(const char *keyword)
00761 {return cmds->getHandler(keyword);};
00762
00770 ScriptImage(ScriptCommand *cmdset);
00771
00775 void Purge(void);
00776
00784 scriptname_t *Include(const char *scrfile);
00785
00794 int Compile(const char *scrfile);
00795
00805 int Compile(const char *scrfile, char *name);
00806
00814 int Compile(std::istream *str, char *name, const char *scrname = NULL);
00815
00821 void Commit(void);
00822
00823 public:
00830 virtual scriptname_t *getScript(const char *name);
00831
00839 virtual scriptname_t *dupScript(const char *name, const char *target);
00840
00849 unsigned Gather(const char *suffix, scriptname_t **array, unsigned size);
00850
00857 inline std::istream *getSource(void)
00858 {return (std::istream *)&scrSource;};
00859 };
00860
00868 class CCXX_CLASS_EXPORT ScriptInterp : public ScriptSymbol
00869 {
00870 private:
00871 friend class ScriptImage;
00872 friend class ScriptSession;
00873 friend class ScriptModule;
00874
00875 #pragma pack(1)
00876 typedef struct
00877 {
00878 scriptname_t *script;
00879 scriptline_t *line, *read;
00880 unsigned char index;
00881 ScriptSymbol *local;
00882 }
00883 scriptcontext_t;
00884 #pragma pack()
00885
00886 static ScriptLocks locks;
00887 ScriptCommand *cmd;
00888 ScriptImage *image;
00889 ScriptSession *session;
00890 scriptcontext_t script[SCRIPT_STACK_SIZE + 1];
00891 char *temps[SCRIPT_TEMP_SPACE];
00892 int tempidx;
00893 int stack;
00894 size_t symsize, pgsize;
00895 bool once, loop;
00896 char packtoken;
00897 unsigned long signalmask;
00898
00899 bool scrTemplate(void);
00900 bool scrEnable(void);
00901 bool scrDisable(void);
00902 bool scrUse(void);
00903 bool scrLoadable(void);
00904 bool scrPacked(void);
00905 bool scrPack(void);
00906 bool scrUnpack(void);
00907 bool scrOn(void);
00908 bool scrSlog(void);
00909 bool scrBasename(void);
00910 bool scrDirname(void);
00911 bool scrFullpath(void);
00912 bool scrGather(void);
00913 bool scrInc(void);
00914 bool scrDec(void);
00915 bool scrFifo(void);
00916 bool scrCounter(void);
00917 bool scrPost(void);
00918 bool scrStack(void);
00919 bool scrSequence(void);
00920 bool scrDup(void);
00921 bool scrArray(void);
00922 bool scrList(void);
00923 bool scrArm(void);
00924 bool scrDisarm(void);
00925 bool scrSet(void);
00926 bool scrToday(void);
00927 bool scrAlias(void);
00928 bool scrConst(void);
00929 bool scrSize(void);
00930 bool scrInit(void);
00931 bool scrClear(void);
00932 bool scrCall(void);
00933 bool scrHas(void);
00934 bool scrMissing(void);
00935 bool scrIf(void);
00936 bool scrIfThen(void);
00937 bool scrFor(void);
00938 bool scrRead(void);
00939 bool scrRepeat(void);
00940 bool scrForeach(void);
00941 bool scrTryeach(void);
00942 bool scrSwap(void);
00943 bool scrDo(void);
00944 bool scrLoop(void);
00945 bool scrBreak(void);
00946 bool scrContinue(void);
00947 bool scrReturn(void);
00948 bool scrPop(void);
00949 bool scrSelect(void);
00950 bool scrOnce(void);
00951 bool scrLock(void);
00952 bool scrTry(void);
00953 bool scrSkip(void);
00954
00955 friend class ScriptCommand;
00956
00957 protected:
00964 ScriptInterp(ScriptCommand *cmd, size_t symsize, size_t pgsize = 1024);
00965
00971 void getTrigger(bool use);
00972
00978 bool getOnce(void);
00979
00985 inline void Notify(unsigned long mask)
00986 {signalmask |= mask;};
00987
00993 inline void Notify(const char *str)
00994 {signalmask |= cmd->getTrapMask(str);};
00995
01001 inline unsigned long getMask(void);
01002
01008 inline ScriptCommand *getCommand(void)
01009 {return cmd;};
01010
01018 bool Conditional(void);
01019
01025 bool scrExit(void);
01026
01030 bool scrGoto(void);
01031
01035 bool scrData(void);
01036
01042 virtual unsigned getId(void)
01043 {return 0;};
01044
01045
01052 virtual bool getGlobalTrap(unsigned id)
01053 {return false;};
01054
01062 scriptsymbol_t *getVariable(size_t size = 0);
01063
01064
01073 virtual scriptsymbol_t *getIndirect(char *sym)
01074 {return NULL;};
01075
01079 void Advance(void);
01080
01087 void Error(const char *error);
01088
01096 void Trap(unsigned id);
01097
01104 void Trap(const char *trapname);
01105
01111 bool Push(void);
01112
01118 bool Pull(void);
01119
01129 bool Signal(const char *trapname);
01130
01138 bool Signal(unsigned trapid);
01139
01147 virtual bool Execute(scriptmethod_t method)
01148 {return (this->*(method))();};
01149
01158 virtual void Stop(unsigned long mask)
01159 {return;};
01160
01165 virtual void Exit(void) = 0;
01166
01174 virtual scriptname_t *getScriptImage(const char *label);
01175
01182 scriptname_t *getScriptCopy(const char *src);
01183
01189 virtual void sleepScheduler(timeout_t timeout)
01190 {return;};
01191
01197 virtual void stepScheduler(const char *trapname)
01198 {Trap(trapname);};
01199
01200 public:
01210 scriptsymbol_t *getLocal(const char *name, size_t size = 0);
01211
01219 bool Attach(const char *scrname);
01220
01225 void Detach(void);
01226
01233 bool Redirect(const char *scrname);
01234
01243 bool Step(const char *trapname = NULL);
01244
01250 inline bool isActive(void)
01251 {return script[stack].line;};
01252
01260 char *getOption(const char *def = NULL);
01261
01269 char *getKeyword(const char *keyword);
01270
01274 int initKeywords(int size);
01275
01283 char *getValue(const char *def = NULL);
01284
01291 char *getContent(char *sym);
01292
01298 inline scriptline_t *getScript(void)
01299 {return script[stack].line;};
01300
01306 const char *getMember(void);
01307
01313 inline scriptname_t *getObject(void)
01314 {return script[stack].script;};
01315
01322 inline ScriptImage *getImage(void)
01323 {return image;};
01324
01330 inline void autoloop(bool enable)
01331 {loop = enable;};
01332 };
01333
01343 class ScriptPackage : protected DSO
01344 {
01345 private:
01346 static ScriptPackage *first;
01347 ScriptPackage *next;
01348 char *filename;
01349
01350 ScriptPackage(char *name);
01351
01352 public:
01353 CCXX_MEMBER_EXPORT(static bool) usePackage(const char *name);
01354 };
01355
01356 #ifdef __NAMESPACES__
01357 };
01358 #endif
01359
01360 #endif
01361