dcfa7cf6b776f1a009cdd7acd3f2024fe520e969
[supertux.git] / src / squirrel / sqdbg / sqdbgserver.h
1 #ifndef _SQ_DBGSERVER_H_\r
2 #define _SQ_DBGSERVER_H_\r
3 \r
4 #define MAX_BP_PATH 512\r
5 #define MAX_MSG_LEN 2049\r
6 \r
7 #include <set>\r
8 #include <string>\r
9 #include <vector>\r
10 \r
11 #ifdef _WIN32\r
12 #include <winsock.h>\r
13 #define sqdbg_closesocket(x) closesocket((x))\r
14 typedef socklen_t int;\r
15 #else\r
16 #include <unistd.h>\r
17 #include <errno.h>\r
18 #include <sys/socket.h>\r
19 #include <sys/types.h>\r
20 #include <netinet/in.h>\r
21 #include <arpa/inet.h>\r
22 #include <netdb.h>\r
23 #include <fcntl.h>\r
24 \r
25 #define sqdbg_closesocket(x) close((x))\r
26 typedef int SOCKET;\r
27 typedef struct timeval TIMEVAL;\r
28 #define SOCKET_ERROR -1\r
29 #define INVALID_SOCKET -1\r
30 #endif\r
31 \r
32 typedef std::basic_string<SQChar> SQDBGString;\r
33 \r
34 inline bool dbg_less(const SQChar *x,const SQChar *y)\r
35 {\r
36         int n = 0;\r
37         do {\r
38                 int xl = *x == '\\' ? '/' : tolower(*x);\r
39                 int yl = *y == '\\' ? '/' : tolower(*y);\r
40                 int diff = xl - yl;\r
41                 if(diff != 0)\r
42                         return diff > 0?true:false;\r
43                 x++; y++;\r
44         }while(*x != 0 && *y != 0);\r
45         return false;\r
46 }\r
47 \r
48 struct BreakPoint{\r
49         BreakPoint(){_line=0;}\r
50         BreakPoint(int line, const SQChar *src){ _line = line; _src = src; }\r
51         BreakPoint(const BreakPoint& bp){ _line = bp._line; _src=bp._src; }\r
52         inline bool operator<(const BreakPoint& bp) const\r
53         {\r
54                 if(_line<bp._line)\r
55                         return true;\r
56                 if(_line==bp._line){\r
57                         return dbg_less(_src.c_str(),bp._src.c_str());\r
58                 }\r
59                 return false;\r
60         }\r
61         \r
62         int _line;\r
63         SQDBGString _src;\r
64 };\r
65 \r
66 struct Watch{\r
67         Watch() { _id = 0; }\r
68         Watch(int id,const SQChar *exp) { _id = id; _exp = exp; }\r
69         Watch(const Watch &w) { _id = w._id; _exp = w._exp; }\r
70         bool operator<(const Watch& w) const { return _id<w._id; }\r
71         bool operator==(const Watch& w) const { return _id == w._id; }\r
72         int _id;\r
73         SQDBGString _exp;\r
74 };\r
75 \r
76 typedef std::set<BreakPoint> BreakPointSet;\r
77 typedef BreakPointSet::iterator BreakPointSetItor;\r
78 \r
79 typedef std::set<Watch> WatchSet;\r
80 typedef WatchSet::iterator WatchSetItor;\r
81 \r
82 typedef std::vector<SQChar> SQCharVec;\r
83 struct SQDbgServer{\r
84 public:\r
85         enum eDbgState{\r
86                 eDBG_Running,\r
87                 eDBG_StepOver,\r
88                 eDBG_StepInto,\r
89                 eDBG_StepReturn,\r
90                 eDBG_Suspended,\r
91                 eDBG_Disabled,\r
92         };\r
93 \r
94         SQDbgServer(HSQUIRRELVM v);\r
95         ~SQDbgServer();\r
96         bool Init();\r
97         //returns true if a message has been received\r
98         bool WaitForClient();\r
99         bool ReadMsg();\r
100         void BusyWait();\r
101         void Hook(int type,int line,const SQChar *src,const SQChar *func);\r
102         void ParseMsg(const char *msg);\r
103         bool ParseBreakpoint(const char *msg,BreakPoint &out);\r
104         bool ParseWatch(const char *msg,Watch &out);\r
105         bool ParseRemoveWatch(const char *msg,int &id);\r
106         void Terminated();\r
107         //\r
108         void BreakExecution();\r
109         void Send(const SQChar *s,...);\r
110         void SendChunk(const SQChar *chunk);\r
111         void Break(int line,const SQChar *src,const SQChar *type,const SQChar *error=NULL);\r
112         \r
113 \r
114         void SerializeState();\r
115         //COMMANDS\r
116         void AddBreakpoint(BreakPoint &bp);\r
117         void AddWatch(Watch &w);\r
118         void RemoveWatch(int id);\r
119         void RemoveBreakpoint(BreakPoint &bp);\r
120 \r
121         //\r
122         void SetErrorHandlers();\r
123 \r
124         //XML RELATED STUFF///////////////////////\r
125         #define MAX_NESTING 10\r
126         struct XMLElementState {\r
127                 SQChar name[256];\r
128                 bool haschildren;\r
129         };\r
130 \r
131         XMLElementState xmlstate[MAX_NESTING];\r
132         int _xmlcurrentement;\r
133 \r
134         void BeginDocument() { _xmlcurrentement = -1; }\r
135         void BeginElement(const SQChar *name);\r
136         void Attribute(const SQChar *name, const SQChar *value);\r
137         void EndElement(const SQChar *name);\r
138         void EndDocument();\r
139 \r
140         const SQChar *escape_xml(const SQChar *x);\r
141         //////////////////////////////////////////////\r
142         HSQUIRRELVM _v;\r
143         HSQOBJECT _debugroot;\r
144         eDbgState _state;\r
145         SOCKET _accept;\r
146         SOCKET _endpoint;\r
147         BreakPointSet _breakpoints;\r
148         WatchSet _watches;\r
149         int _recursionlevel; \r
150         int _maxrecursion;\r
151         int _nestedcalls;\r
152         bool _ready;\r
153         bool _autoupdate;\r
154         HSQOBJECT _serializefunc;\r
155         SQCharVec _scratchstring;\r
156                 \r
157 };\r
158 \r
159 #endif //_SQ_DBGSERVER_H_ \r