fix cr/lfs and remove trailing whitespaces...
[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         int n = 0;
37         do {
38                 int xl = *x == '\\' ? '/' : tolower(*x);
39                 int yl = *y == '\\' ? '/' : tolower(*y);
40                 int diff = xl - yl;
41                 if(diff != 0)
42                         return diff > 0?true:false;
43                 x++; y++;
44         }while(*x != 0 && *y != 0);
45         return false;
46 }
47
48 struct BreakPoint{
49         BreakPoint(){_line=0;}
50         BreakPoint(int line, const SQChar *src){ _line = line; _src = src; }
51         BreakPoint(const BreakPoint& bp){ _line = bp._line; _src=bp._src; }
52         inline bool operator<(const BreakPoint& bp) const
53         {
54                 if(_line<bp._line)
55                         return true;
56                 if(_line==bp._line){
57                         return dbg_less(_src.c_str(),bp._src.c_str());
58                 }
59                 return false;
60         }
61
62         int _line;
63         SQDBGString _src;
64 };
65
66 struct Watch{
67         Watch() { _id = 0; }
68         Watch(int id,const SQChar *exp) { _id = id; _exp = exp; }
69         Watch(const Watch &w) { _id = w._id; _exp = w._exp; }
70         bool operator<(const Watch& w) const { return _id<w._id; }
71         bool operator==(const Watch& w) const { return _id == w._id; }
72         int _id;
73         SQDBGString _exp;
74 };
75
76 typedef std::set<BreakPoint> BreakPointSet;
77 typedef BreakPointSet::iterator BreakPointSetItor;
78
79 typedef std::set<Watch> WatchSet;
80 typedef WatchSet::iterator WatchSetItor;
81
82 typedef std::vector<SQChar> SQCharVec;
83 struct SQDbgServer{
84 public:
85         enum eDbgState{
86                 eDBG_Running,
87                 eDBG_StepOver,
88                 eDBG_StepInto,
89                 eDBG_StepReturn,
90                 eDBG_Suspended,
91                 eDBG_Disabled,
92         };
93
94         SQDbgServer(HSQUIRRELVM v);
95         ~SQDbgServer();
96         bool Init();
97         //returns true if a message has been received
98         bool WaitForClient();
99         bool ReadMsg();
100         void BusyWait();
101         void Hook(int type,int line,const SQChar *src,const SQChar *func);
102         void ParseMsg(const char *msg);
103         bool ParseBreakpoint(const char *msg,BreakPoint &out);
104         bool ParseWatch(const char *msg,Watch &out);
105         bool ParseRemoveWatch(const char *msg,int &id);
106         void Terminated();
107         //
108         void BreakExecution();
109         void Send(const SQChar *s,...);
110         void SendChunk(const SQChar *chunk);
111         void Break(int line,const SQChar *src,const SQChar *type,const SQChar *error=NULL);
112
113
114         void SerializeState();
115         //COMMANDS
116         void AddBreakpoint(BreakPoint &bp);
117         void AddWatch(Watch &w);
118         void RemoveWatch(int id);
119         void RemoveBreakpoint(BreakPoint &bp);
120
121         //
122         void SetErrorHandlers();
123
124         //XML RELATED STUFF///////////////////////
125         #define MAX_NESTING 10
126         struct XMLElementState {
127                 SQChar name[256];
128                 bool haschildren;
129         };
130
131         XMLElementState xmlstate[MAX_NESTING];
132         int _xmlcurrentement;
133
134         void BeginDocument() { _xmlcurrentement = -1; }
135         void BeginElement(const SQChar *name);
136         void Attribute(const SQChar *name, const SQChar *value);
137         void EndElement(const SQChar *name);
138         void EndDocument();
139
140         const SQChar *escape_xml(const SQChar *x);
141         //////////////////////////////////////////////
142         HSQUIRRELVM _v;
143         HSQOBJECT _debugroot;
144         eDbgState _state;
145         SOCKET _accept;
146         SOCKET _endpoint;
147         BreakPointSet _breakpoints;
148         WatchSet _watches;
149         int _recursionlevel;
150         int _maxrecursion;
151         int _nestedcalls;
152         bool _ready;
153         bool _autoupdate;
154         HSQOBJECT _serializefunc;
155         SQCharVec _scratchstring;
156
157 };
158
159 #endif //_SQ_DBGSERVER_H_