cdt/cdt_6_0_x/org.eclipse.cdt.core.win32/library/Win32ProcessEx.c
author timkelly
Wed, 05 Aug 2009 17:35:39 -0500
changeset 51 49c226a8748e
parent 37 c2bce6dd59e7
permissions -rw-r--r--
CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     1
/*******************************************************************************
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     2
 * Copyright (c) 2002, 2008 QNX Software Systems and others.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     3
 * All rights reserved. This program and the accompanying materials
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     4
 * are made available under the terms of the Eclipse Public License v1.0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     5
 * which accompanies this distribution, and is available at
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     6
 * http://www.eclipse.org/legal/epl-v10.html
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     7
 *
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     8
 * Contributors:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     9
 *     QNX Software Systems - initial API and implementation
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    10
 *     Wind River Systems, Inc.  
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    11
 * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    12
 *  Win32ProcessEx.c
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    13
 *
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    14
 *  This is a JNI implementation of spawner 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    15
 *******************************************************************************/
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    16
 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    17
#include "stdafx.h"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    18
#include <string.h>
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    19
#include <stdlib.h>
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    20
#include <process.h>
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    21
#include "Spawner.h"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    22
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    23
#include "jni.h"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    24
#include "io.h"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    25
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    26
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    27
#define PIPE_SIZE 512			// Size of pipe buffer
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    28
#define MAX_CMD_SIZE 2049		// Initial size of command line
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    29
#define MAX_ENV_SIZE 4096		// Initial size of environment block
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    30
#define PIPE_NAME_LENGTH 100	// Size of pipe name buffer
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    31
#define PIPE_TIMEOUT 10000		// Default time-out value, in milliseconds.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    32
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    33
#define MAX_PROCS (100)			// Maximum number of simultaneiously runnig processes
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    34
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    35
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    36
// Process description block. Should be created for each launched process
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    37
typedef struct _procInfo {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    38
	int pid; // Process ID
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    39
	int uid; // quasi-unique process ID; we have to create it to avoid duplicated pid 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    40
	         // (actually this impossible from OS point of view but it is still possible
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    41
			 // a clash of new created and already finished process with one and the same PID.
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
    42
	// 3 events connected to this process (see starter)
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
    43
	HANDLE eventBreak;
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    44
	HANDLE eventWait;
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
    45
	HANDLE eventTerminate;
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    46
} procInfo_t, * pProcInfo_t;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    47
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    48
static int procCounter = 0; // Number of running processes
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    49
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    50
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    51
// This is a VM helper
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    52
void ThrowByName(JNIEnv *env, const char *name, const char *msg);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    53
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    54
// Creates _procInfo block for every launched procss
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    55
pProcInfo_t createProcInfo(); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    56
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    57
// Find process description for this pid
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    58
pProcInfo_t findProcInfo(int pid); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    59
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    60
// We launch separate thread for each project to trap it termination
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    61
void _cdecl waitProcTermination(void* pv) ;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    62
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    63
// This is a helper function to prevent losing of quotatin marks
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    64
static int copyTo(wchar_t * target, const wchar_t  * source, int cpyLenght, int availSpace);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    65
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    66
// Use this function to clean project descriptor and return it to the pool of available blocks.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    67
static void cleanUpProcBlock(pProcInfo_t pCurProcInfo);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    68
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    69
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    70
// Signal codes
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    71
typedef enum {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    72
	SIG_NOOP,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    73
	SIG_HUP,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    74
	SIG_INT,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    75
	SIG_KILL = 9,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    76
	SIG_TERM = 15,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    77
} signals;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    78
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    79
extern CRITICAL_SECTION cs;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    80
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    81
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    82
extern wchar_t path[MAX_PATH]; // Directory where spawner.dll is located
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    83
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    84
static HMODULE hVM = NULL;   // VM handler
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    85
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    86
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    87
static pProcInfo_t pInfo = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    88
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    89
static int nCounter = 0; // We use it to build unique synchronisation object names
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    90
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    91
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    92
// Launcher; launchess process and traps its termination
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    93
// Arguments: (see Spawner.java)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    94
//			[in]  cmdarray - array of command line elements 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    95
//			[in]  envp - array of environment variables
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    96
//			[in]  dir - working directory
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    97
//          [out] channels - streams handlers
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    98
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    99
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   100
extern "C"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   101
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   102
  (JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir, jintArray channels, jstring slaveName, jint fdm) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   103
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   104
	return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   105
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   106
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   107
void ensureSize(wchar_t** ptr, int* psize, int requiredLength) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   108
{ 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   109
	int size= *psize;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   110
	if (requiredLength > size) { 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   111
	   size= 2*size;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   112
	   if (size < requiredLength) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   113
	      size= requiredLength;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   114
	   }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   115
	   *ptr= (wchar_t *)realloc(*ptr, size * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   116
	   if (NULL == *ptr) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   117
	   	  *psize= 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   118
	   }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   119
	   else {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   120
	   	  *psize= size;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   121
	   }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   122
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   123
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   124
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   125
extern "C"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   126
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   127
  (JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir, jintArray channels) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   128
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   129
	HANDLE stdHandles[3];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   130
    PROCESS_INFORMATION pi = {0}, *piCopy;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   131
    STARTUPINFOW si;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   132
	DWORD flags = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   133
    const wchar_t  * cwd = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   134
	LPVOID envBlk = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   135
    int ret = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   136
    int nCmdLineLength= 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   137
	wchar_t * szCmdLine= 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   138
	int nBlkSize = MAX_ENV_SIZE; 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   139
	wchar_t * szEnvBlock = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   140
	jsize nCmdTokens = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   141
	jsize nEnvVars = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   142
	int i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   143
	DWORD pid = GetCurrentProcessId();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   144
	int nPos;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   145
	pProcInfo_t pCurProcInfo;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   146
	wchar_t eventBreakName[20];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   147
	wchar_t eventWaitName[20];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   148
	wchar_t eventTerminateName[20];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   149
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   150
	wchar_t buffer[1000];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   151
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   152
	int nLocalCounter;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   153
	wchar_t inPipeName[PIPE_NAME_LENGTH];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   154
	wchar_t outPipeName[PIPE_NAME_LENGTH];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   155
	wchar_t errPipeName[PIPE_NAME_LENGTH];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   156
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   157
    nCmdLineLength= MAX_CMD_SIZE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   158
    szCmdLine= (wchar_t *)malloc(nCmdLineLength * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   159
    szCmdLine[0]= _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   160
	if((HIBYTE(LOWORD(GetVersion()))) & 0x80)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   161
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   162
		ThrowByName(env, "java/io/IOException", "Does not support Windows 3.1/95/98/Me");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   163
		return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   164
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   165
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   166
    if (cmdarray == 0) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   167
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   168
		ThrowByName(env, "java/lang/NullPointerException", "No command line specified");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   169
		return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   170
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   171
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   172
   ZeroMemory(stdHandles, sizeof(stdHandles));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   173
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   174
	// Create pipe names
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   175
   EnterCriticalSection(&cs);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   176
   swprintf(inPipeName,  L"\\\\.\\pipe\\stdin%08i%010i",  pid, nCounter); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   177
   swprintf(outPipeName, L"\\\\.\\pipe\\stdout%08i%010i", pid, nCounter); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   178
   swprintf(errPipeName, L"\\\\.\\pipe\\stderr%08i%010i", pid, nCounter); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   179
   nLocalCounter = nCounter;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   180
   ++nCounter;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   181
   LeaveCriticalSection(&cs);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   182
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   183
   if ((INVALID_HANDLE_VALUE == (stdHandles[0] = CreateNamedPipeW(inPipeName, PIPE_ACCESS_OUTBOUND,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   184
									  PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   185
									  PIPE_UNLIMITED_INSTANCES, PIPE_SIZE, PIPE_SIZE, PIPE_TIMEOUT, NULL))) ||
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   186
	   (INVALID_HANDLE_VALUE == (stdHandles[1] = CreateNamedPipeW(outPipeName, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   187
									  PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   188
									  PIPE_UNLIMITED_INSTANCES, PIPE_SIZE, PIPE_SIZE, PIPE_TIMEOUT, NULL))) ||
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   189
	   (INVALID_HANDLE_VALUE == (stdHandles[2] = CreateNamedPipeW(errPipeName, PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   190
									  PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   191
									  PIPE_UNLIMITED_INSTANCES, PIPE_SIZE, PIPE_SIZE, PIPE_TIMEOUT, NULL))))		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   192
		CloseHandle(stdHandles[0]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   193
        CloseHandle(stdHandles[1]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   194
        CloseHandle(stdHandles[2]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   195
		ThrowByName(env, "java/io/IOException", "CreatePipe");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   196
		return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   197
   } 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   198
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   199
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   200
	swprintf(buffer, _T("Opened pipes: %s, %s, %s\n"), inPipeName, outPipeName, errPipeName);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   201
	OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   202
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   203
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   204
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   205
	nCmdTokens = env->GetArrayLength(cmdarray);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   206
    nEnvVars   = env->GetArrayLength(envp);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   207
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   208
	pCurProcInfo = createProcInfo();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   209
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   210
	if(NULL == pCurProcInfo)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   211
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   212
		ThrowByName(env, "java/io/IOException", "Too many processes");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   213
		return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   214
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   215
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   216
	// Construct starter's command line
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   217
	swprintf(eventBreakName, L"SABreak%p", pCurProcInfo);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   218
	swprintf(eventWaitName, L"SAWait%p", pCurProcInfo);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   219
	swprintf(eventTerminateName, L"SATerm%p", pCurProcInfo);
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   220
	pCurProcInfo -> eventBreak = CreateEventW(NULL, TRUE, FALSE, eventBreakName);
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   221
	ResetEvent(pCurProcInfo -> eventBreak);   
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   222
	pCurProcInfo -> eventWait = CreateEventW(NULL, TRUE, FALSE, eventWaitName);
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   223
	ResetEvent(pCurProcInfo -> eventWait);   
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   224
	pCurProcInfo -> eventTerminate = CreateEventW(NULL, TRUE, FALSE, eventTerminateName);
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   225
	ResetEvent(pCurProcInfo -> eventTerminate);   
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   226
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   227
	swprintf(szCmdLine, L"\"%sstarter.exe\" %i %i %s %s %s ", path, pid, nLocalCounter, eventBreakName, eventWaitName, eventTerminateName);
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   228
	nPos = wcslen(szCmdLine);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   229
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   230
	// Prepare command line
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   231
	for(i = 0; i < nCmdTokens; ++i) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   232
    	{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   233
		jstring item = (jstring)env->GetObjectArrayElement(cmdarray, i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   234
		jsize    len = env->GetStringLength(item);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   235
		int nCpyLen;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   236
		const wchar_t *  str = (const wchar_t *)env->GetStringChars(item, 0);	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   237
		if(NULL != str) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   238
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   239
			int requiredSize= nPos+len+2;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   240
			if (requiredSize > 32*1024) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   241
				ThrowByName(env, "java/io/IOException", "Command line too long");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   242
				return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   243
			}				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   244
			ensureSize(&szCmdLine, &nCmdLineLength, requiredSize);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   245
			if (NULL == szCmdLine) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   246
				ThrowByName(env, "java/io/IOException", "Not enough memory");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   247
				return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   248
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   249
			    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   250
			if(0 > (nCpyLen = copyTo(szCmdLine + nPos, str, len, nCmdLineLength - nPos)))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   251
                {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   252
				ThrowByName(env, "java/io/IOException", "Command line too long");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   253
				return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   254
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   255
			nPos += nCpyLen;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   256
			szCmdLine[nPos] = _T(' ');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   257
			++nPos;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   258
			env->ReleaseStringChars(item, (const jchar *)str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   259
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   260
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   261
	szCmdLine[nPos] = _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   262
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   263
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   264
	swprintf(buffer, _T("There are  %i environment variables \n"), nEnvVars);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   265
	OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   266
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   267
	// Prepare environment block
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   268
    if (nEnvVars > 0) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   269
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   270
		nPos = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   271
		szEnvBlock = (wchar_t *)malloc(nBlkSize * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   272
		for(i = 0; i < nEnvVars; ++i) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   273
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   274
			jstring item = (jstring)env->GetObjectArrayElement(envp, i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   275
			jsize    len = env->GetStringLength(item);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   276
			const wchar_t *  str = (const wchar_t *)env->GetStringChars(item, 0);	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   277
			if(NULL != str)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   278
				{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   279
				while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   280
					{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   281
					nBlkSize += MAX_ENV_SIZE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   282
					szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   283
					if(NULL == szEnvBlock) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   284
						{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   285
						ThrowByName(env, "java/io/IOException", "Not enough memory");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   286
						return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   287
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   288
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   289
					swprintf(buffer, _T("Realloc environment block; new length is  %i \n"), nBlkSize);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   290
					OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   291
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   292
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   293
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   294
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   295
				swprintf(buffer, _T("%s\n"), str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   296
				OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   297
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   298
				wcsncpy(szEnvBlock + nPos, str, len);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   299
				nPos += len;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   300
				szEnvBlock[nPos] = _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   301
				++nPos;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   302
				env->ReleaseStringChars(item, (const jchar *)str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   303
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   304
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   305
			szEnvBlock[nPos] = _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   306
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   307
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   308
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   309
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   310
    if (dir != 0) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   311
		{ 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   312
		const wchar_t * str = (const wchar_t *)env->GetStringChars(dir, 0);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   313
		if(NULL != str)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   314
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   315
			cwd = wcsdup(str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   316
			env->ReleaseStringChars(dir, (const jchar *)str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   317
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   318
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   319
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   320
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   321
    ZeroMemory(&si, sizeof(si));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   322
    si.cb = sizeof(si);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   323
    si.dwFlags |= STARTF_USESHOWWINDOW;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   324
    si.wShowWindow = SW_HIDE;  // Processes in the Process Group are hidden
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   325
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   326
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   327
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   328
    SetHandleInformation(stdHandles[0], HANDLE_FLAG_INHERIT, FALSE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   329
    SetHandleInformation(stdHandles[1],  HANDLE_FLAG_INHERIT, FALSE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   330
    SetHandleInformation(stdHandles[2],  HANDLE_FLAG_INHERIT, FALSE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   331
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   332
	flags = CREATE_NEW_CONSOLE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   333
	flags |= CREATE_NO_WINDOW;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   334
	flags |= CREATE_UNICODE_ENVIRONMENT;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   335
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   336
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   337
	OutputDebugStringW(szCmdLine);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   338
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   339
	// launches starter; we need it to create another console group to correctly process 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   340
	// emulation of SYSint signal (Ctrl-C)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   341
    ret = CreateProcessW(0,                /* executable name */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   342
                        szCmdLine,        /* command line */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   343
                        0,                /* process security attribute */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   344
                        0,                /* thread security attribute */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   345
                        FALSE,            /* inherits system handles */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   346
                        flags,            /* normal attached process */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   347
                        szEnvBlock,		  /* environment block */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   348
                        cwd,              /* change to the new current directory */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   349
                        &si,              /* (in)  startup information */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   350
                        &pi);             /* (out) process information */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   351
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   352
	if(NULL != cwd)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   353
		free((void *)cwd);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   354
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   355
	if(NULL != szEnvBlock)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   356
		free(szEnvBlock);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   357
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   358
    if(NULL != szCmdLine) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   359
        free(szCmdLine);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   360
      
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   361
    if (!ret) // Launching error
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   362
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   363
		char * lpMsgBuf;	    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   364
		CloseHandle(stdHandles[0]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   365
		CloseHandle(stdHandles[1]);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   366
		CloseHandle(stdHandles[2]); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   367
		FormatMessageA( 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   368
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   369
			FORMAT_MESSAGE_FROM_SYSTEM | 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   370
			FORMAT_MESSAGE_IGNORE_INSERTS,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   371
			NULL,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   372
			GetLastError(),
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   373
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   374
			(char *)&lpMsgBuf,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   375
			0,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   376
			NULL 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   377
		);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   378
		ThrowByName(env, "java/io/IOException", lpMsgBuf);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   379
		// Free the buffer.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   380
		LocalFree( lpMsgBuf );
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   381
		cleanUpProcBlock(pCurProcInfo);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   382
		ret = -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   383
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   384
    else
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   385
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   386
    	int file_handles[3];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   387
		HANDLE h[2];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   388
		int what;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   389
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   390
		EnterCriticalSection(&cs);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   391
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   392
		pCurProcInfo -> pid = pi.dwProcessId;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   393
        h[0] = pCurProcInfo -> eventWait;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   394
		h[1] = pi.hProcess;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   395
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   396
		what = WaitForMultipleObjects(2, h, FALSE, INFINITE); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   397
		if(what != WAIT_OBJECT_0) // CreateProcess failed
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   398
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   399
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   400
			swprintf(buffer, _T("Process %i failed\n"), pi.dwProcessId);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   401
			OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   402
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   403
			cleanUpProcBlock(pCurProcInfo);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   404
			ThrowByName(env, "java/io/IOException", "Launching failed");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   405
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   406
			OutputDebugStringW(_T("Process failed\n"));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   407
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   408
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   409
		else 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   410
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   411
			ret = (long)(pCurProcInfo -> uid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   412
			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   413
			// Prepare stream handlers to return to java program
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   414
			file_handles[0] = (int)stdHandles[0];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   415
			file_handles[1] = (int)stdHandles[1];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   416
			file_handles[2] = (int)stdHandles[2];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   417
			env->SetIntArrayRegion(channels, 0, 3, (jint *)file_handles);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   418
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   419
			// do the cleanup so launch the according thread
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   420
			// create a copy of the PROCESS_INFORMATION as this might get destroyed
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   421
			piCopy = (PROCESS_INFORMATION *)malloc(sizeof(PROCESS_INFORMATION));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   422
			memcpy(piCopy, &pi, sizeof(PROCESS_INFORMATION));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   423
			_beginthread(waitProcTermination, 0, (void *)piCopy);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   424
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   425
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   426
			OutputDebugStringW(_T("Process started\n"));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   427
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   428
			}				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   429
		LeaveCriticalSection(&cs);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   430
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   431
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   432
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   433
	CloseHandle(pi.hThread);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   434
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   435
    return ret;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   436
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   437
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   438
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   439
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   440
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   441
// Launcher; just launches process and don't care about it any more
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   442
// Arguments: (see Spawner.java)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   443
//			[in]  cmdarray - array of command line elements 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   444
//			[in]  envp - array of environment variables
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   445
//			[in]  dir - working directory
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   446
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   447
extern "C"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   448
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   449
  (JNIEnv * env, jobject process, jobjectArray cmdarray, jobjectArray envp, jstring dir) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   450
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   451
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   452
    SECURITY_ATTRIBUTES sa;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   453
    PROCESS_INFORMATION pi = {0};
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   454
    STARTUPINFOW si;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   455
	DWORD flags = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   456
    wchar_t * cwd = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   457
	wchar_t * envBlk = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   458
    int ret = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   459
	jsize nCmdTokens = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   460
	jsize nEnvVars = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   461
	int i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   462
	int nPos;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   463
    int nCmdLineLength= 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   464
	wchar_t * szCmdLine= 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   465
	int nBlkSize = MAX_ENV_SIZE; 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   466
	wchar_t * szEnvBlock = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   467
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   468
    nCmdLineLength= MAX_CMD_SIZE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   469
	szCmdLine= (wchar_t *)malloc(nCmdLineLength * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   470
    szCmdLine[0]= 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   471
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   472
    sa.nLength = sizeof(sa);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   473
    sa.lpSecurityDescriptor = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   474
    sa.bInheritHandle = TRUE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   475
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   476
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   477
	nCmdTokens = env->GetArrayLength(cmdarray);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   478
    nEnvVars   = env->GetArrayLength(envp);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   479
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   480
	nPos = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   481
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   482
	// Prepare command line
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   483
	for(i = 0; i < nCmdTokens; ++i) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   484
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   485
		jstring item = (jstring)env->GetObjectArrayElement(cmdarray, i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   486
		jsize    len = env->GetStringLength(item);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   487
		int nCpyLen;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   488
		const wchar_t *  str = (const wchar_t *)env->GetStringChars(item, 0);	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   489
		if(NULL != str)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   490
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   491
			int requiredSize= nPos+len+2;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   492
			if (requiredSize > 32*1024) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   493
				ThrowByName(env, "java/io/IOException", "Command line too long");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   494
				return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   495
			}				
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   496
			ensureSize(&szCmdLine, &nCmdLineLength, requiredSize);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   497
			if (NULL == szCmdLine) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   498
				ThrowByName(env, "java/io/IOException", "Not enough memory");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   499
				return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   500
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   501
			    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   502
			if(0 > (nCpyLen = copyTo(szCmdLine + nPos, str, len, nCmdLineLength - nPos)))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   503
				{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   504
				ThrowByName(env, "java/io/Exception", "Command line too long");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   505
				return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   506
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   507
			nPos += nCpyLen;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   508
			szCmdLine[nPos] = _T(' ');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   509
			++nPos;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   510
			env->ReleaseStringChars(item, (const jchar *)str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   511
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   512
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   513
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   514
	szCmdLine[nPos] = _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   515
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   516
	// Prepare environment block
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   517
    if (nEnvVars > 0) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   518
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   519
		szEnvBlock = (wchar_t *)malloc(nBlkSize * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   520
		nPos = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   521
		for(i = 0; i < nEnvVars; ++i) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   522
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   523
			jstring item = (jstring)env->GetObjectArrayElement(envp, i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   524
			jsize    len = env->GetStringLength(item);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   525
			const wchar_t *  str = (const wchar_t *)env->GetStringChars(item, 0);	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   526
			if(NULL != str)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   527
				{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   528
				while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   529
					{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   530
					nBlkSize += MAX_ENV_SIZE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   531
					szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   532
					if(NULL == szEnvBlock) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   533
						{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   534
						ThrowByName(env, "java/io/Exception", "Not enough memory");
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   535
						return 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   536
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   537
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   538
				wcsncpy(szEnvBlock + nPos, str, len);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   539
				nPos += len;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   540
				szEnvBlock[nPos] = _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   541
				++nPos;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   542
				env->ReleaseStringChars(item, (const jchar *)str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   543
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   544
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   545
    	szEnvBlock[nPos] = _T('\0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   546
		envBlk = szEnvBlock;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   547
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   548
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   549
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   550
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   551
    if (dir != 0) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   552
		{ 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   553
		const wchar_t * str = (const wchar_t *)env->GetStringChars(dir, 0);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   554
		if(NULL != str) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   555
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   556
			cwd = wcsdup(str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   557
			env->ReleaseStringChars(dir, (const jchar *)str);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   558
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   559
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   560
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   561
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   562
    ZeroMemory(&si, sizeof(si));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   563
    si.cb = sizeof(si);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   564
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   565
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   566
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   567
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   568
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   569
	flags = CREATE_NEW_CONSOLE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   570
	flags |= CREATE_UNICODE_ENVIRONMENT;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   571
    ret = CreateProcessW(0,                /* executable name */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   572
                        szCmdLine,              /* command line */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   573
                        0,                /* process security attribute */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   574
                        0,                /* thread security attribute */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   575
                        TRUE,             /* inherits system handles */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   576
                        flags,            /* normal attached process */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   577
                        envBlk,       /* environment block */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   578
                        cwd,              /* change to the new current directory */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   579
                        &si,              /* (in)  startup information */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   580
                        &pi);             /* (out) process information */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   581
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   582
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   583
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   584
	if(NULL != cwd)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   585
		free(cwd);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   586
	if(NULL != szEnvBlock)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   587
		free(szEnvBlock);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   588
	if(NULL != szCmdLine)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   589
		free(szCmdLine);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   590
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   591
    if (!ret)  // error
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   592
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   593
		char * lpMsgBuf;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   594
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   595
		FormatMessage( 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   596
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   597
			FORMAT_MESSAGE_FROM_SYSTEM | 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   598
			FORMAT_MESSAGE_IGNORE_INSERTS,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   599
			NULL,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   600
			GetLastError(),
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   601
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   602
				(wchar_t *)&lpMsgBuf,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   603
				0,
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   604
				NULL 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   605
		);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   606
		ThrowByName(env, "java/io/IOException", lpMsgBuf);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   607
		// Free the buffer.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   608
		LocalFree( lpMsgBuf );		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   609
		ret = -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   610
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   611
    else
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   612
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   613
		// Clean-up
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   614
		CloseHandle(pi.hThread);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   615
		CloseHandle(pi.hProcess);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   616
		ret = (long)pi.dwProcessId; //hProcess;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   617
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   618
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   619
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   620
    return ret;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   621
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   622
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   623
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   624
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   625
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   626
// Emulation of the signal raising
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   627
// Arguments: (see Spawner.java)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   628
//			[in]  uid - unique process ID
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   629
//			[in]  signal - signal to raise
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   630
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   631
extern "C"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   632
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   633
  (JNIEnv * env, jobject process, jint uid, jint signal) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   634
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   635
	jint ret = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   636
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   637
	HANDLE hProc;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   638
	pProcInfo_t pCurProcInfo = findProcInfo(uid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   639
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   640
	wchar_t buffer[100];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   641
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   642
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   643
	if(NULL == pCurProcInfo) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   644
		if(SIG_INT == signal) { // Try another way
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   645
			return interruptProcess(uid) ;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   646
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   647
		return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   648
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   649
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   650
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   651
	swprintf(buffer, _T("Spawner received signal %i for process %i\n"), signal, pCurProcInfo -> pid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   652
	OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   653
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   654
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   655
	hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pCurProcInfo -> pid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   656
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   657
	if(NULL == hProc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   658
		return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   659
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   660
	switch(signal)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   661
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   662
		case SIG_NOOP:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   663
			// Wait 0 msec -just check if the process has been still running
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   664
			ret = ((WAIT_TIMEOUT == WaitForSingleObject(hProc, 0)) ? 0 : -1);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   665
			break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   666
		case SIG_HUP:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   667
			// Temporary do nothing
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   668
			ret = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   669
			break;
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   670
		case SIG_KILL:
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   671
		case SIG_TERM:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   672
#ifdef DEBUG_MONITOR
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   673
			swprintf(buffer, _T("Spawner received KILL or TERM signal for process %i\n"), 
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   674
				pCurProcInfo -> pid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   675
			OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   676
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   677
		    SetEvent(pCurProcInfo -> eventTerminate);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   678
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   679
			OutputDebugStringW(_T("Spawner signalled KILL event\n"));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   680
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   681
			ret = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   682
			break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   683
		case SIG_INT:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   684
		    ResetEvent(pCurProcInfo -> eventWait);
51
49c226a8748e CDT 6.0 from cdt_6_0 branch (eclipse.org cvs repository). This overwrites previous CDT 6.0 merges.
timkelly
parents: 37
diff changeset
   685
			PulseEvent(pCurProcInfo -> eventBreak);
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   686
			ret = (WaitForSingleObject(pCurProcInfo -> eventWait, 100) == WAIT_OBJECT_0);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   687
			break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   688
		default:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   689
			break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   690
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   691
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   692
	CloseHandle(hProc);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   693
	return ret;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   694
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   695
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   696
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   697
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   698
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   699
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   700
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   701
// Wait for process termination
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   702
// Arguments: (see Spawner.java)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   703
//			[in]  uid - unique process ID
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   704
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   705
extern "C"
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   706
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   707
  (JNIEnv * env, jobject process, jint uid) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   708
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   709
    DWORD exit_code;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   710
    int what=0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   711
	HANDLE hProc;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   712
	pProcInfo_t pCurProcInfo = findProcInfo(uid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   713
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   714
	if(NULL == pCurProcInfo)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   715
		return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   716
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   717
    hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pCurProcInfo -> pid);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   718
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   719
	if(NULL == hProc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   720
		return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   721
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   722
    what = WaitForSingleObject(hProc, INFINITE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   723
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   724
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   725
    if (what == WAIT_OBJECT_0)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   726
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   727
		GetExitCodeProcess(hProc, &exit_code);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   728
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   729
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   730
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   731
	if(hProc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   732
		CloseHandle(hProc);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   733
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   734
    return exit_code;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   735
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   736
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   737
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   738
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   739
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   740
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   741
// Utilities
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   742
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   743
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   744
// Throws Java exception (will be trapped by VM).
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   745
// Arguments: 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   746
//			[in]  name - name of exception class
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   747
//			[in]  message to assign thi event
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   748
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   749
void ThrowByName(JNIEnv *env, const char *name, const char *msg)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   750
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   751
    jclass cls = env->FindClass(name);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   752
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   753
    if (cls != 0) /* Otherwise an exception has already been thrown */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   754
        env->ThrowNew(cls, msg);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   755
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   756
    /* It's a good practice to clean up the local references. */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   757
    env->DeleteLocalRef(cls);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   758
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   759
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   760
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   761
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   762
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   763
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   764
// Create process description block.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   765
// Arguments:  no
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   766
// Return : pointer to the process descriptor			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   767
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   768
pProcInfo_t createProcInfo()
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   769
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   770
	int i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   771
	pProcInfo_t p = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   772
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   773
	EnterCriticalSection(&cs);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   774
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   775
	if(NULL == pInfo)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   776
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   777
		pInfo = (pProcInfo_t)malloc(sizeof(procInfo_t) * MAX_PROCS);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   778
		ZeroMemory(pInfo, sizeof(procInfo_t) * MAX_PROCS);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   779
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   780
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   781
	for(i = 0; i < MAX_PROCS; ++i)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   782
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   783
		if(pInfo[i].pid == 0)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   784
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   785
			pInfo[i].pid = -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   786
			pInfo[i].uid = ++procCounter;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   787
			p = pInfo + i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   788
			break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   789
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   790
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   791
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   792
	LeaveCriticalSection(&cs);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   793
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   794
	return p;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   795
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   796
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   797
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   798
// Using unique process ID finds process descriptor
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   799
// Arguments:  no
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   800
// Return : pointer to the process descriptor			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   801
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   802
pProcInfo_t findProcInfo(int uid)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   803
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   804
	int i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   805
	pProcInfo_t p = NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   806
	if(NULL == pInfo)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   807
		return NULL;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   808
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   809
	for(i = 0; i < MAX_PROCS; ++i)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   810
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   811
		if(pInfo[i].uid == uid)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   812
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   813
			p = pInfo + i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   814
			break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   815
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   816
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   817
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   818
	return p;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   819
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   820
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   821
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   822
// Cleans up vacant process descriptor
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   823
// Arguments: 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   824
//				pCurProcInfo - pointer to descriptor to clean up
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   825
// Return : no
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   826
void cleanUpProcBlock(pProcInfo_t pCurProcInfo)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   827
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   828
	if(0 != pCurProcInfo -> eventBreak) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   829
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   830
		CloseHandle(pCurProcInfo -> eventBreak);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   831
		pCurProcInfo -> eventBreak = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   832
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   833
	if(0 != pCurProcInfo -> eventWait) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   834
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   835
		CloseHandle(pCurProcInfo -> eventWait);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   836
		pCurProcInfo -> eventWait = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   837
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   838
	if(0 != pCurProcInfo -> eventTerminate) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   839
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   840
		CloseHandle(pCurProcInfo -> eventTerminate);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   841
		pCurProcInfo -> eventTerminate = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   842
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   843
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   844
	pCurProcInfo -> pid = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   845
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   846
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   847
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   848
// Running in separae thread and waiting for the process termination
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   849
// Arguments:  
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   850
//			pv - (int)pv is a pid
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   851
// Return : always 0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   852
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   853
void _cdecl waitProcTermination(void* pv) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   854
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   855
	PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   856
	int i;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   857
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   858
	wchar_t buffer[1000];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   859
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   860
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   861
	// wait for process termination
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   862
	WaitForSingleObject(pi->hProcess, INFINITE);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   863
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   864
	for(i = 0; i < MAX_PROCS; ++i)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   865
	{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   866
		if(pInfo[i].pid == pi->dwProcessId)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   867
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   868
			cleanUpProcBlock(pInfo + i);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   869
#ifdef DEBUG_MONITOR
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   870
				swprintf(buffer, _T("waitProcTermination: set PID %i to 0\n"), 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   871
					pInfo[i].pid, 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   872
					GetLastError());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   873
				OutputDebugStringW(buffer);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   874
#endif
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   875
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   876
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   877
	CloseHandle(pi->hProcess);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   878
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   879
	free(pi);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   880
}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   881
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   882
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   883
// Use this utility program to process correctly quotation marks in the command line
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   884
// Arguments:  
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   885
//			target - string to copy to
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   886
//			source - string to copy from
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   887
//			cpyLength - copy length
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   888
//			availSpace - size of the target buffer
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   889
// Return :number of bytes used in target, or -1 in case of error
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   890
/////////////////////////////////////////////////////////////////////////////////////
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   891
int copyTo(wchar_t * target, const wchar_t * source, int cpyLength, int availSpace)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   892
{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   893
	BOOL bSlash = FALSE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   894
	int i = 0, j = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   895
	int totCpyLength = cpyLength;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   896
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   897
#define QUOTATION_DO   0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   898
#define QUOTATION_DONE 1
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   899
#define QUOTATION_NONE 2
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   900
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   901
	int nQuotationMode = 0;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   902
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   903
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   904
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   905
	if(availSpace <= cpyLength) // = to reserve space for final '\0'
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   906
		return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   907
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   908
	if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1)))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   909
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   910
		nQuotationMode = QUOTATION_DONE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   911
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   912
	else
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   913
	if(wcschr(source, _T(' ')) == NULL)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   914
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   915
		// No reason to quotate term becase it doesn't have embedded spaces
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   916
		nQuotationMode = QUOTATION_NONE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   917
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   918
	else
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   919
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   920
		// Needs to be quotated
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   921
		nQuotationMode = QUOTATION_DO;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   922
		*target = _T('\"');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   923
		++j;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   924
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   925
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   926
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   927
	for(; i < cpyLength; ++i, ++j) 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   928
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   929
		if(source[i] == _T('\\'))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   930
			bSlash = TRUE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   931
		else
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   932
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   933
			// Don't escape embracing quotation marks
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   934
			if((source[i] == _T('\"')) && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))) ) )
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   935
				{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   936
				if(!bSlash) // If still not escaped
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   937
					{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   938
					if(j == availSpace)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   939
						return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   940
					target[j] = _T('\\');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   941
					++j;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   942
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   943
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   944
			bSlash = FALSE;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   945
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   946
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   947
		if(j == availSpace)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   948
			return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   949
		target[j] = source[i];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   950
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   951
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   952
	if(nQuotationMode == QUOTATION_DO)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   953
		{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   954
		if(j == availSpace)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   955
			return -1;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   956
		target[j] = _T('\"');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   957
		++j;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   958
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   959
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   960
	return j;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   961
}