jruby-launcher 0.9.9-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module JRubyLauncher
2
+ VERSION = "0.9.9"
3
+ end
@@ -0,0 +1,4 @@
1
+ class Gem::ConfigFile
2
+ PLATFORM_DEFAULTS.delete('install')
3
+ PLATFORM_DEFAULTS.delete('update')
4
+ end
data/nbexecloader.h ADDED
@@ -0,0 +1,43 @@
1
+ #ifndef _NBEXECLOADER_H
2
+ #define _NBEXECLOADER_H
3
+
4
+ #include "utilsfuncs.h"
5
+
6
+ #define HELP_MSG ""
7
+
8
+ class NBExecLoader {
9
+ typedef int (*StartPlatform)(int argc, char *argv[], const char *help, const char *name);
10
+
11
+ public:
12
+ NBExecLoader()
13
+ : hLib(0) {
14
+ }
15
+ ~NBExecLoader() {
16
+ if (hLib) {
17
+ FreeLibrary(hLib);
18
+ }
19
+ }
20
+ int start(const char *path, int argc, char *argv[], const char *name) {
21
+ if (!hLib) {
22
+ hLib = LoadLibrary(path);
23
+ if (!hLib) {
24
+ logErr(true, true, "Cannot load \"%s\".", path);
25
+ return -1;
26
+ }
27
+ }
28
+
29
+ StartPlatform startPlatform = (StartPlatform) GetProcAddress(hLib, "startPlatform");
30
+ if (!startPlatform) {
31
+ logErr(true, true, "Cannot start platform, failed to find startPlatform() in %s", path);
32
+ return -1;
33
+ }
34
+ logMsg("Starting platform... \n\tBinary name is: %s\n", name);
35
+ return startPlatform(argc, argv, HELP_MSG, name);
36
+ }
37
+
38
+ private:
39
+ HMODULE hLib;
40
+ };
41
+
42
+ #endif /* _NBEXECLOADER_H */
43
+
data/ng.c ADDED
@@ -0,0 +1,642 @@
1
+ /*
2
+
3
+ Copyright 2004, 2010, Martian Software, Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+
17
+ */
18
+
19
+ /**
20
+ * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
21
+ * @author Pete Kirkham (Win32 port)
22
+ */
23
+
24
+ #ifdef WIN32
25
+ #include <direct.h>
26
+ #include <winsock2.h>
27
+ #else
28
+ #include <arpa/inet.h>
29
+ #include <netdb.h>
30
+ #include <netinet/in.h>
31
+ #include <sys/socket.h>
32
+ #include <sys/types.h>
33
+ #endif
34
+
35
+ #include <stdio.h>
36
+ #include <stdlib.h>
37
+ #include <string.h>
38
+ #include <unistd.h>
39
+
40
+ #define NAILGUN_VERSION "0.7.1"
41
+
42
+ #define BUFSIZE (2048)
43
+
44
+ #ifdef WIN32
45
+ HANDLE NG_STDIN_FILENO;
46
+ HANDLE NG_STDOUT_FILENO;
47
+ HANDLE NG_STDERR_FILENO;
48
+ #define FILE_SEPARATOR '\\'
49
+ #define MSG_WAITALL 0
50
+ #else
51
+ #define NG_STDIN_FILENO STDIN_FILENO
52
+ #define NG_STDOUT_FILENO STDOUT_FILENO
53
+ #define NG_STDERR_FILENO STDERR_FILENO
54
+ #define FILE_SEPARATOR '/'
55
+ typedef int HANDLE;
56
+ typedef unsigned int SOCKET;
57
+ /* buffer used for reading an writing chunk data */
58
+ char buf[BUFSIZE];
59
+ #endif
60
+
61
+ #ifndef MIN
62
+ #define MIN(a,b) ((a<b)?(a):(b))
63
+ #endif
64
+
65
+ #ifndef MSG_WAITALL
66
+ #define MSG_WAITALL 0x40 /* wait for full request or error */
67
+ #endif
68
+
69
+ #ifdef WIN32
70
+ #define NAILGUN_FILESEPARATOR "NAILGUN_FILESEPARATOR=\\"
71
+ #define NAILGUN_PATHSEPARATOR "NAILGUN_PATHSEPARATOR=;"
72
+ #else
73
+ #define NAILGUN_FILESEPARATOR "NAILGUN_FILESEPARATOR=/"
74
+ #define NAILGUN_PATHSEPARATOR "NAILGUN_PATHSEPARATOR=:"
75
+ #endif
76
+
77
+ #define NAILGUN_CLIENT_NAME_EXE "ng.exe"
78
+
79
+ #define NAILGUN_PORT_DEFAULT "2113"
80
+ #define NAILGUN_CLIENT_NAME "ng"
81
+ #define CHUNK_HEADER_LEN (5)
82
+
83
+ #define NAILGUN_SOCKET_FAILED (999)
84
+ #define NAILGUN_CONNECT_FAILED (998)
85
+ #define NAILGUN_UNEXPECTED_CHUNKTYPE (997)
86
+ #define NAILGUN_EXCEPTION_ON_SERVER (996)
87
+ #define NAILGUN_CONNECTION_BROKEN (995)
88
+ #define NAILGUN_BAD_ARGUMENTS (994)
89
+
90
+ #define CHUNKTYPE_STDIN '0'
91
+ #define CHUNKTYPE_STDOUT '1'
92
+ #define CHUNKTYPE_STDERR '2'
93
+ #define CHUNKTYPE_STDIN_EOF '.'
94
+ #define CHUNKTYPE_ARG 'A'
95
+ #define CHUNKTYPE_ENV 'E'
96
+ #define CHUNKTYPE_DIR 'D'
97
+ #define CHUNKTYPE_CMD 'C'
98
+ #define CHUNKTYPE_EXIT 'X'
99
+
100
+
101
+ /* the socket connected to the nailgun server */
102
+ int nailgunsocket = 0;
103
+
104
+ /**
105
+ * Clean up the application.
106
+ */
107
+ void cleanUpAndExit (int exitCode) {
108
+
109
+
110
+ #ifdef WIN32
111
+ CancelIo(STDIN_FILENO);
112
+ WSACleanup();
113
+ if (nailgunsocket) {
114
+ closesocket(nailgunsocket);
115
+ }
116
+ #else
117
+ close(nailgunsocket);
118
+ #endif
119
+
120
+ exit(exitCode);
121
+ }
122
+
123
+ #ifdef WIN32
124
+ /**
125
+ * Handles an error.
126
+ * Shows the message for the latest error then exits.
127
+ */
128
+ void handleError () {
129
+ LPVOID lpMsgBuf;
130
+ int error = GetLastError();
131
+
132
+ FormatMessage(
133
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
134
+ FORMAT_MESSAGE_FROM_SYSTEM |
135
+ FORMAT_MESSAGE_IGNORE_INSERTS,
136
+ NULL,
137
+ error,
138
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
139
+ (LPTSTR) &lpMsgBuf,
140
+ 0,
141
+ NULL);
142
+
143
+ /* Display the string. */
144
+ MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONERROR );
145
+
146
+ /* Free the buffer. */
147
+ LocalFree( lpMsgBuf );
148
+
149
+ cleanUpAndExit(error);
150
+ }
151
+ #endif
152
+
153
+ /**
154
+ * Writes everything in the specified buffer to the specified
155
+ * socket handle.
156
+ *
157
+ * @param s the socket descriptor
158
+ * @param buf the buffer containing the data to send
159
+ * @param len the number of bytes to send. Also used to
160
+ * return the number of bytes sent.
161
+ * @return total bytes written or 0 if failure
162
+ */
163
+ int sendAll(SOCKET s, char *buf, int len) {
164
+ int total = 0;
165
+ int bytesleft = len;
166
+ int n = 0;
167
+
168
+ while(total < len) {
169
+ n = send(s, buf+total, bytesleft, 0);
170
+
171
+ if (n == -1) {
172
+ break;
173
+ }
174
+
175
+ total += n;
176
+ bytesleft -= n;
177
+ }
178
+
179
+ return n==-1 ? 0:total;
180
+ }
181
+
182
+ /**
183
+ * Sends a chunk header noting the specified payload size and chunk type.
184
+ *
185
+ * @param size the payload size
186
+ * @param chunkType the chunk type identifier
187
+ */
188
+ void sendHeader(unsigned int size, char chunkType) {
189
+ /* buffer used for reading and writing chunk headers */
190
+ char header[CHUNK_HEADER_LEN];
191
+
192
+ header[0] = (size >> 24) & 0xff;
193
+ header[1] = (size >> 16) & 0xff;
194
+ header[2] = (size >> 8) & 0xff;
195
+ header[3] = size & 0xff;
196
+ header[4] = chunkType;
197
+
198
+ sendAll(nailgunsocket, header, CHUNK_HEADER_LEN);
199
+ }
200
+
201
+ /**
202
+ * Sends a null-terminated string with the specified chunk type.
203
+ *
204
+ * @param chunkType the chunk type identifier
205
+ * @param text the null-terminated string to send
206
+ */
207
+ void sendText(char chunkType, char *text) {
208
+ int len = strlen(text);
209
+ sendHeader(len, chunkType);
210
+ sendAll(nailgunsocket, text, len);
211
+ }
212
+
213
+ /**
214
+ * Exits the client if the nailgun server ungracefully shut down the connection.
215
+ */
216
+ void handleSocketClose() {
217
+ cleanUpAndExit(NAILGUN_CONNECTION_BROKEN);
218
+ }
219
+
220
+ /**
221
+ * Receives len bytes from the nailgun socket and copies them to the specified file descriptor.
222
+ * Used to route data to stdout or stderr on the client.
223
+ *
224
+ * @param destFD the destination file descriptor (stdout or stderr)
225
+ * @param len the number of bytes to copy
226
+ */
227
+ void recvToFD(HANDLE destFD, char *buf, unsigned long len) {
228
+ unsigned long bytesRead = 0;
229
+ int bytesCopied;
230
+
231
+ while (bytesRead < len) {
232
+ unsigned long bytesRemaining = len - bytesRead;
233
+ int bytesToRead = (BUFSIZE < bytesRemaining) ? BUFSIZE : bytesRemaining;
234
+ int thisPass = 0;
235
+
236
+ thisPass = recv(nailgunsocket, buf, bytesToRead, MSG_WAITALL);
237
+
238
+ bytesRead += thisPass;
239
+
240
+ bytesCopied = 0;
241
+
242
+ while(bytesCopied < thisPass) {
243
+ #ifdef WIN32
244
+ DWORD thisWrite = 0;
245
+
246
+ WriteFile(destFD, buf + bytesCopied, thisPass - bytesCopied,
247
+ &thisWrite, NULL);
248
+
249
+ if (thisWrite < 0) {
250
+ break;
251
+ }
252
+
253
+ bytesCopied += thisWrite;
254
+ #else
255
+ bytesCopied += write(destFD, buf + bytesCopied, thisPass - bytesCopied);
256
+ #endif
257
+ }
258
+ }
259
+ }
260
+
261
+
262
+ /**
263
+ * Processes an exit chunk from the server. This is just a string
264
+ * containing the exit code in decimal format. It should fit well
265
+ * within our buffer, so assume that it does.
266
+ *
267
+ * @param len the current length of the buffer containing the exit code.
268
+ */
269
+ void processExit(char *buf, unsigned long len) {
270
+ int exitcode;
271
+ int bytesToRead = (BUFSIZE - 1 < len) ? BUFSIZE - 1 : len;
272
+ int bytesRead = recv(nailgunsocket, buf, bytesToRead, MSG_WAITALL);
273
+
274
+ if (bytesRead < 0) {
275
+ handleSocketClose();
276
+ }
277
+
278
+ buf[bytesRead] = 0;
279
+
280
+ exitcode = atoi(buf);
281
+
282
+ cleanUpAndExit(exitcode);
283
+ }
284
+
285
+ /**
286
+ * Processes data from the nailgun server.
287
+ */
288
+ void processnailgunstream() {
289
+ /* buffer used for receiving and writing nail output chunks */
290
+ char buf[BUFSIZE];
291
+
292
+ /*for (;;) {*/
293
+ int bytesRead = 0;
294
+ unsigned long len;
295
+ char chunkType;
296
+
297
+ bytesRead = recv(nailgunsocket, buf, CHUNK_HEADER_LEN, 0);
298
+
299
+ if (bytesRead < CHUNK_HEADER_LEN) {
300
+ handleSocketClose();
301
+ }
302
+
303
+ len = ((buf[0] << 24) & 0xff000000)
304
+ | ((buf[1] << 16) & 0x00ff0000)
305
+ | ((buf[2] << 8) & 0x0000ff00)
306
+ | ((buf[3]) & 0x000000ff);
307
+
308
+ chunkType = buf[4];
309
+
310
+ switch(chunkType) {
311
+ case CHUNKTYPE_STDOUT: recvToFD(NG_STDOUT_FILENO, buf, len);
312
+ break;
313
+ case CHUNKTYPE_STDERR: recvToFD(NG_STDERR_FILENO, buf, len);
314
+ break;
315
+ case CHUNKTYPE_EXIT: processExit(buf, len);
316
+ break;
317
+ default: fprintf(stderr, "Unexpected chunk type %d ('%c')\n", chunkType, chunkType);
318
+ cleanUpAndExit(NAILGUN_UNEXPECTED_CHUNKTYPE);
319
+ }
320
+ /*}*/
321
+ }
322
+
323
+ /**
324
+ * Sends len bytes from buf to the nailgun server in a stdin chunk.
325
+ *
326
+ * @param buf the bytes to send
327
+ * @param len the number of bytes to send
328
+ */
329
+ void sendStdin(char *buf, unsigned int len) {
330
+ sendHeader(len, CHUNKTYPE_STDIN);
331
+ sendAll(nailgunsocket, buf, len);
332
+ }
333
+
334
+ /**
335
+ * Sends a stdin-eof chunk to the nailgun server
336
+ */
337
+ void processEof() {
338
+ sendHeader(0, CHUNKTYPE_STDIN_EOF);
339
+ }
340
+
341
+
342
+ #ifdef WIN32
343
+ /**
344
+ * Thread main for reading from stdin and sending
345
+ */
346
+ DWORD WINAPI processStdin (LPVOID lpParameter) {
347
+ /* buffer used for reading and sending stdin chunks */
348
+ char buf[BUFSIZE];
349
+
350
+ for (;;) {
351
+ DWORD numberOfBytes = 0;
352
+
353
+ if (!ReadFile(NG_STDIN_FILENO, buf, BUFSIZE, &numberOfBytes, NULL)) {
354
+ if (numberOfBytes != 0) {
355
+ handleError();
356
+ }
357
+ }
358
+
359
+ if (numberOfBytes > 0) {
360
+ sendStdin(buf, numberOfBytes);
361
+ } else {
362
+ processEof();
363
+ break;
364
+ }
365
+ }
366
+
367
+ return 0;
368
+ }
369
+ #else
370
+ /**
371
+ * Reads from stdin and transmits it to the nailgun server in a stdin chunk.
372
+ * Sends a stdin-eof chunk if necessary.
373
+ *
374
+ * @return zero if eof has been reached.
375
+ */
376
+ int processStdin() {
377
+ int bytesread = read(STDIN_FILENO, buf, BUFSIZE);
378
+ if (bytesread > 0) {
379
+ sendStdin(buf, bytesread);
380
+ } else if (bytesread == 0) {
381
+ processEof();
382
+ }
383
+ return(bytesread);
384
+ }
385
+ #endif
386
+
387
+ #ifdef WIN32
388
+ /**
389
+ * Initialise Windows sockets
390
+ */
391
+ void initSockets () {
392
+ WSADATA win_socket_data; /* required to initialise winsock */
393
+
394
+ WSAStartup(2, &win_socket_data);
395
+ }
396
+ #endif
397
+
398
+ #ifdef WIN32
399
+ /**
400
+ * Initialise the asynchronous io.
401
+ */
402
+ void initIo () {
403
+ SECURITY_ATTRIBUTES securityAttributes;
404
+ DWORD threadId = 0;
405
+
406
+ /* create non-blocking console io */
407
+ AllocConsole();
408
+
409
+ securityAttributes.bInheritHandle = TRUE;
410
+ securityAttributes.lpSecurityDescriptor = NULL;
411
+ securityAttributes.nLength = 0;
412
+
413
+ NG_STDIN_FILENO = GetStdHandle(STD_INPUT_HANDLE);
414
+ NG_STDOUT_FILENO = GetStdHandle(STD_OUTPUT_HANDLE);
415
+ NG_STDERR_FILENO = GetStdHandle(STD_ERROR_HANDLE);
416
+
417
+ if (!CreateThread(&securityAttributes, 0, &processStdin, NULL, 0, &threadId)) {
418
+ handleError();
419
+ }
420
+ }
421
+ #endif
422
+
423
+ /**
424
+ * Trims any path info from the beginning of argv[0] to determine
425
+ * the name used to launch the client.
426
+ *
427
+ * @param s argv[0]
428
+ */
429
+ char *shortClientName(char *s) {
430
+ char *result = strrchr(s, FILE_SEPARATOR);
431
+ return ((result == NULL) ? s : result + 1);
432
+ }
433
+
434
+ /**
435
+ * Returns true if the specified string is the name of the nailgun
436
+ * client. The comparison is made case-insensitively for windows.
437
+ *
438
+ * @param s the program name to check
439
+ */
440
+ int isNailgunClientName(char *s) {
441
+ #ifdef WIN32
442
+ return (!strcasecmp(s, NAILGUN_CLIENT_NAME) ||
443
+ !strcasecmp(s, NAILGUN_CLIENT_NAME_EXE));
444
+ #else
445
+ return(!(strcmp(s, NAILGUN_CLIENT_NAME)));
446
+ #endif
447
+ }
448
+
449
+ /**
450
+ * Displays usage info and bails
451
+ */
452
+ void usage(int exitcode) {
453
+
454
+ fprintf(stderr, "Usage: ng class [--nailgun-options] [args]\n");
455
+ fprintf(stderr, " (to execute a class)\n");
456
+ fprintf(stderr, " or: ng alias [--nailgun-options] [args]\n");
457
+ fprintf(stderr, " (to execute an aliased class)\n");
458
+ fprintf(stderr, " or: alias [--nailgun-options] [args]\n");
459
+ fprintf(stderr, " (to execute an aliased class, where \"alias\"\n");
460
+ fprintf(stderr, " is both the alias for the class and a symbolic\n");
461
+ fprintf(stderr, " link to the ng client)\n\n");
462
+
463
+ fprintf(stderr, "where options include:\n");
464
+ fprintf(stderr, " --nailgun-D<name>=<value> set/override a client environment variable\n");
465
+ fprintf(stderr, " --nailgun-version print product version and exit\n");
466
+ fprintf(stderr, " --nailgun-showversion print product version and continue\n");
467
+ fprintf(stderr, " --nailgun-server to specify the address of the nailgun server\n");
468
+ fprintf(stderr, " (default is localhost)\n");
469
+ fprintf(stderr, " --nailgun-port to specify the port of the nailgun server\n");
470
+ fprintf(stderr, " (default is 2113)\n");
471
+ fprintf(stderr, " --nailgun-help print this message and exit\n");
472
+
473
+ cleanUpAndExit(exitcode);
474
+ }
475
+
476
+ int nailgunClientMain(int argc, char *argv[], char *env[]) {
477
+ int i;
478
+ struct sockaddr_in server_addr;
479
+ char *nailgun_server; /* server as specified by user */
480
+ char *nailgun_port; /* port as specified by user */
481
+ char *cwd;
482
+ u_short port; /* port */
483
+ struct hostent *hostinfo;
484
+ char *cmd;
485
+ int firstArgIndex; /* the first argument _to pass to the server_ */
486
+
487
+ #ifndef WIN32
488
+ fd_set readfds;
489
+ int eof = 0;
490
+ #endif
491
+
492
+ #ifdef WIN32
493
+ initSockets();
494
+ #endif
495
+
496
+ /* start with environment variable. default to localhost if not defined. */
497
+ nailgun_server = getenv("NAILGUN_SERVER");
498
+ if (nailgun_server == NULL) {
499
+ nailgun_server = "127.0.0.1";
500
+ }
501
+
502
+ /* start with environment variable. default to normal nailgun port if not defined */
503
+ nailgun_port = getenv("NAILGUN_PORT");
504
+ if (nailgun_port == NULL) {
505
+ nailgun_port = NAILGUN_PORT_DEFAULT;
506
+ }
507
+
508
+ /* look at the command used to launch this program. if it was "ng", then the actual
509
+ command to issue to the server must be specified as another argument. if it
510
+ wasn't ng, assume that the desired command name was symlinked to ng in the user's
511
+ filesystem, and use the symlink name (without path info) as the command for the server. */
512
+ cmd = shortClientName(argv[0]);
513
+
514
+ if (isNailgunClientName(cmd)) {
515
+ cmd = NULL;
516
+ }
517
+
518
+ firstArgIndex = 1;
519
+
520
+ /* quite possibly the lamest commandline parsing ever.
521
+ look for the two args we care about (--nailgun-server and
522
+ --nailgun-port) and NULL them and their parameters after
523
+ reading them if found. later, when we send args to the
524
+ server, skip the null args. */
525
+ for (i = 1; i < argc; ++i) {
526
+ if (!strcmp("--nailgun-server", argv[i])) {
527
+ if (i == argc - 1) usage(NAILGUN_BAD_ARGUMENTS);
528
+ nailgun_server = argv[i + 1];
529
+ argv[i] = argv[i + 1] = NULL;
530
+ ++i;
531
+ } else if(!strcmp("--nailgun-port", argv[i])) {
532
+ if (i == argc - 1) usage(NAILGUN_BAD_ARGUMENTS);
533
+ nailgun_port = argv[i + 1];
534
+ argv[i] = argv[i + 1]= NULL;
535
+ ++i;
536
+ } else if (!strcmp("--nailgun-version", argv[i])) {
537
+ printf("NailGun client version %s\n", NAILGUN_VERSION);
538
+ cleanUpAndExit(0);
539
+ } else if (!strcmp("--nailgun-showversion", argv[i])) {
540
+ printf("NailGun client version %s\n", NAILGUN_VERSION);
541
+ argv[i] = NULL;
542
+ } else if (!strcmp("--nailgun-help", argv[i])) {
543
+ usage(0);
544
+ } else if (cmd == NULL) {
545
+ cmd = argv[i];
546
+ firstArgIndex = i + 1;
547
+ }
548
+ }
549
+
550
+ /* if there's no command, we should only display usage info
551
+ if the version number was not displayed. */
552
+ if (cmd == NULL) {
553
+ usage(NAILGUN_BAD_ARGUMENTS);
554
+ }
555
+
556
+ /* jump through a series of connection hoops */
557
+ hostinfo = gethostbyname(nailgun_server);
558
+
559
+ if (hostinfo == NULL) {
560
+ fprintf(stderr, "Unknown host: %s\n", nailgun_server);
561
+ cleanUpAndExit(NAILGUN_CONNECT_FAILED);
562
+ }
563
+
564
+ port = atoi(nailgun_port);
565
+
566
+ if ((nailgunsocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
567
+ perror("socket");
568
+ cleanUpAndExit(NAILGUN_SOCKET_FAILED);
569
+ }
570
+
571
+ server_addr.sin_family = AF_INET;
572
+ server_addr.sin_port = htons(port);
573
+ server_addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
574
+
575
+ memset(&(server_addr.sin_zero), '\0', 8);
576
+
577
+ if (connect(nailgunsocket, (struct sockaddr *)&server_addr,
578
+ sizeof(struct sockaddr)) == -1) {
579
+ perror("connect");
580
+ cleanUpAndExit(NAILGUN_CONNECT_FAILED);
581
+ }
582
+
583
+ /* ok, now we're connected. first send all of the command line
584
+ arguments for the server, if any. remember that we may have
585
+ marked some arguments NULL if we read them to specify the
586
+ nailgun server and/or port */
587
+ for(i = firstArgIndex; i < argc; ++i) {
588
+ if (argv[i] != NULL) sendText(CHUNKTYPE_ARG, argv[i]);
589
+ }
590
+
591
+ /* now send environment */
592
+ sendText(CHUNKTYPE_ENV, NAILGUN_FILESEPARATOR);
593
+ sendText(CHUNKTYPE_ENV, NAILGUN_PATHSEPARATOR);
594
+ for(i = 0; env[i]; ++i) {
595
+ sendText(CHUNKTYPE_ENV, env[i]);
596
+ }
597
+
598
+ /* now send the working directory */
599
+ cwd = getcwd(NULL, BUFSIZE);
600
+ sendText(CHUNKTYPE_DIR, cwd);
601
+ free(cwd);
602
+
603
+ /* and finally send the command. this marks the point at which
604
+ streams are linked between client and server. */
605
+ sendText(CHUNKTYPE_CMD, cmd);
606
+
607
+
608
+ /* initialise the std-* handles and the thread to send stdin to the server */
609
+ #ifdef WIN32
610
+ initIo();
611
+ #endif
612
+
613
+ /* stream forwarding loop */
614
+ while(1) {
615
+ #ifndef WIN32
616
+ FD_ZERO(&readfds);
617
+
618
+ /* don't select on stdin if we've already reached its end */
619
+ if (!eof) {
620
+ FD_SET(NG_STDIN_FILENO, &readfds);
621
+ }
622
+
623
+ FD_SET(nailgunsocket, &readfds);
624
+ if (select (nailgunsocket + 1, &readfds, NULL, NULL, NULL) == -1) {
625
+ perror("select");
626
+ }
627
+
628
+ if (FD_ISSET(nailgunsocket, &readfds)) {
629
+ #endif
630
+ processnailgunstream();
631
+ #ifndef WIN32
632
+ } else if (FD_ISSET(NG_STDIN_FILENO, &readfds)) {
633
+ if (!processStdin()) {
634
+ FD_CLR(NG_STDIN_FILENO, &readfds);
635
+ eof = 1;
636
+ }
637
+ }
638
+ #endif
639
+ }
640
+
641
+ /* normal termination is triggered by the server, and so occurs in processExit(), above */
642
+ }