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