-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
main.c
289 lines (243 loc) · 7.61 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002, 2003 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.0 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://xdebug.derickrethans.nl/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| xdebug@derickrethans.nl so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <derick@xdebug.org> |
| Marco Canini <m.canini@libero.it> |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <sys/types.h>
#ifndef WIN32
#include "config.h"
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/un.h>
#else
#include <winsock2.h>
#endif
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#ifdef HAVE_LIBEDIT
#include <signal.h>
#include <histedit.h>
#endif
#include "usefulstuff.h"
#ifdef WIN32
#define MSG_NOSIGNAL 0
#define sleep(t) Sleep((t)*1000)
#define close(fd) closesocket(fd)
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#define DEBUGCLIENT_VERSION "0.8.0"
#define DEFAULT_PORT 9000
#ifdef HAVE_LIBEDIT
static char prompt[8];
static EditLine *el = NULL;
static History *hist = NULL;
static HistEvent ev;
void initialize_libedit(const char *prog);
void deinitialize_libedit();
static char *get_prompt(EditLine *el);
void handle_sigterm(int i);
void initialize_libedit(const char *prog)
{
/* Init the builtin history */
hist = history_init();
/* Remember 100 events */
history(hist, &ev, H_SETSIZE, 100);
/* Initialize editline */
el = el_init(prog, stdin, stdout, stderr);
el_set(el, EL_EDITOR, "emacs"); /* Default editor is emacs */
el_set(el, EL_SIGNAL, 1); /* Handle signals gracefully */
el_set(el, EL_PROMPT, get_prompt); /* Set the prompt function */
/* Tell editline to use this history interface */
el_set(el, EL_HIST, history, hist);
/*
* Source the user's defaults file.
*/
/* el_source(el, "xdebug"); */
}
void deinitialize_libedit(void)
{
el_end(el);
history_end(hist);
}
static char *get_prompt(EditLine *el)
{
return prompt;
}
void handle_sigterm(int i)
{
deinitialize_libedit();
}
#endif
int main(int argc, char *argv[])
{
int port = DEFAULT_PORT; /* Port number to listen for connections */
int ssocket = 0; /* Socket file descriptor */
struct sockaddr_in server_in;
struct sockaddr_in client_in;
int client_in_len;
int fd; /* Filedescriptor for userinput */
fd_buf cxt = { NULL, 0 };
struct in_addr *iaddr;
char *buffer; /* Buffer with data from the server */
char *cmd; /* Command to send to the server */
char *prev_cmd = NULL; /* Last send command to the server */
int opt; /* Current option during parameter parsing */
int length; /* Length of read buffer */
#ifdef HAVE_LIBEDIT
int num = 0;
/* Add SIGTERM signal handler */
signal (SIGTERM, handle_sigterm);
initialize_libedit (argv[0]);
#else
fd_buf std_in = { NULL, 0 };
#endif
#ifdef WIN32
/* Initialise Windows' WinSock library */
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
#endif
/* Display copyright notice and version number */
printf("Xdebug Simple DBGp client (%s)\n", DEBUGCLIENT_VERSION);
printf("Copyright 2002-2004 by Derick Rethans.\n");
#ifdef HAVE_LIBEDIT
printf("- libedit support: enabled\n");
#endif
/* Option handling */
while (1) {
opt = getopt(argc, argv, "hp:v");
if (opt == -1) {
break;
}
switch (opt) {
case 'h':
printf("\nUsage:\n");
printf("\tdebugclient [-h] [-p port] [-v]\n");
printf("\t-h\tShow this help\n");
printf("\t-p\tSpecify the port to listen on (default = 9000)\n");
printf("\t-v\tShow version number and exit\n");
exit(0);
break;
case 'v':
exit(0);
break;
case 'p':
port = atoi(optarg);
printf("Listening on TCP port %d.\n", port);
break;
}
}
/* Main loop that listens for connections from the debug client and that
* does all the communications handling. */
while (1) {
ssocket = socket(AF_INET, SOCK_STREAM, 0);
if (ssocket < 0) {
fprintf(stderr, "socket: couldn't create socket\n");
exit(-1);
}
memset(&server_in, 0, sizeof(struct sockaddr));
server_in.sin_family = AF_INET;
server_in.sin_addr.s_addr = htonl(INADDR_ANY);
server_in.sin_port = htons((unsigned short int) port);
memset(&client_in, 0, sizeof(client_in));
client_in_len = sizeof(client_in);
/* Loop until we can bind to the listening socket. */
while (bind(ssocket, (struct sockaddr *) &server_in, sizeof(struct sockaddr_in)) < 0) {
fprintf(stderr, "bind: couldn't bind AF_INET socket?\n");
sleep(5);
}
if (listen(ssocket, 0) == -1) {
fprintf(stderr, "listen: listen call failed\n");
exit(-2);
}
printf("\nWaiting for debug server to connect.\n");
#ifdef WIN32
fd = accept(ssocket, (struct sockaddr *) &client_in, NULL);
if (fd == -1) {
printf("accept: %d\n", WSAGetLastError());
exit(-3);
}
#else
fd = accept(ssocket, (struct sockaddr *) &client_in, &client_in_len);
if (fd == -1) {
perror("accept");
exit(-3);
}
#endif
close(ssocket);
iaddr = &client_in.sin_addr;
printf("Connect\n");
/* Get the message from the server*/
while ((buffer = fd_read_line_delim(fd, &cxt, FD_RL_SOCKET, '\0', &length)) > 0) {
buffer = fd_read_line_delim(fd, &cxt, FD_RL_SOCKET, '\0', &length);
printf ("%s\n", buffer);
{
/* The server requires a new command */
#ifdef HAVE_LIBEDIT
/* Copy the prompt string */
sprintf(prompt, "(cmd) ");
if (((const char*) cmd = el_gets(el, &num)) != NULL && num != 0) {
/* Add command to history */
history(hist, &ev, H_ENTER, cmd);
/* We overwrite the \n with \0 for libedit builds */
cmd[strlen(cmd) - 1] = '\0';
#else
printf("(cmd) ");
fflush(stdout);
if ((cmd = fd_read_line(0, &std_in, FD_RL_FILE))) {
#endif
/* If there is a 'previous' command, and when the command
* just consists of an "enter", then we set the command to
* the previous command. */
if (prev_cmd && ((strlen(cmd) == 0) || (strlen(cmd) == 1 && cmd[0] == '\n'))) {
cmd = prev_cmd;
} else {
if (prev_cmd) {
free(prev_cmd);
}
prev_cmd = strdup(cmd);
}
/* Send the command to the debug server */
if (send(fd, cmd, strlen(cmd), MSG_NOSIGNAL) == -1) {
break;
}
if (send(fd, "\0", 1, MSG_NOSIGNAL) == -1) {
break;
}
/* If cmd is quit exit from while */
if (strncmp(cmd, "quit", 4) == 0) {
break;
}
}
}
}
printf("Disconnect\n\n");
close(fd);
if (prev_cmd) {
free(prev_cmd);
prev_cmd = NULL;
}
/* Sleep some time to reset the TCP/IP connection */
sleep(1);
}
}