1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

fix space quotings

This commit is contained in:
Alain Magloire 2003-01-13 19:30:09 +00:00
parent 841fe74d7d
commit d6dc6bc8df
2 changed files with 123 additions and 29 deletions

View file

@ -22,7 +22,7 @@
#include "jni.h" #include "jni.h"
#include "io.h" #include "io.h"
// #define DEBUG_MONITOR //#define DEBUG_MONITOR
#define PIPE_SIZE 512 #define PIPE_SIZE 512
#define MAX_CMD_SIZE 1024 #define MAX_CMD_SIZE 1024
@ -96,7 +96,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
char eventWaitName[20]; char eventWaitName[20];
char eventTerminateName[20]; char eventTerminateName[20];
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
char buffer[100]; char buffer[1000];
#endif #endif
@ -179,16 +179,11 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
{ {
jobject item = (*env) -> GetObjectArrayElement(env, envp, i); jobject item = (*env) -> GetObjectArrayElement(env, envp, i);
jsize len = (*env) -> GetStringUTFLength(env, item); jsize len = (*env) -> GetStringUTFLength(env, item);
int nCpyLen;
const char * str = (*env) -> GetStringUTFChars(env, item, 0); const char * str = (*env) -> GetStringUTFChars(env, item, 0);
if(NULL != str) if(NULL != str)
{ {
if(0 > (nCpyLen = copyTo(szEnvBlock + nPos, str, len, MAX_ENV_SIZE - nPos - 1))) strncpy(szEnvBlock + nPos, str, len);
{ nPos += len;
ThrowByName(env, "java/Exception", "Too many environment variables");
return 0;
}
nPos += nCpyLen;
szEnvBlock[nPos] = '\0'; szEnvBlock[nPos] = '\0';
++nPos; ++nPos;
(*env) -> ReleaseStringUTFChars(env, item, str); (*env) -> ReleaseStringUTFChars(env, item, str);
@ -387,16 +382,11 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
{ {
jobject item = (*env) -> GetObjectArrayElement(env, envp, i); jobject item = (*env) -> GetObjectArrayElement(env, envp, i);
jsize len = (*env) -> GetStringUTFLength(env, item); jsize len = (*env) -> GetStringUTFLength(env, item);
int nCpyLen;
const char * str = (*env) -> GetStringUTFChars(env, item, 0); const char * str = (*env) -> GetStringUTFChars(env, item, 0);
if(NULL != str) if(NULL != str)
{ {
if(0 > (nCpyLen = copyTo(szEnvBlock + nPos, str, len, MAX_ENV_SIZE - nPos - 1))) strncpy(szEnvBlock + nPos, str, len);
{ nPos += len;
ThrowByName(env, "java/Exception", "Too many environment variables");
return 0;
}
nPos += nCpyLen;
szEnvBlock[nPos] = '\0'; szEnvBlock[nPos] = '\0';
++nPos; ++nPos;
(*env) -> ReleaseStringUTFChars(env, item, str); (*env) -> ReleaseStringUTFChars(env, item, str);
@ -664,7 +654,7 @@ unsigned int _stdcall waitProcTermination(void* pv)
int pid = *(int *)pv; int pid = *(int *)pv;
DWORD rc = 0; DWORD rc = 0;
#ifdef DEBUG_MONITOR #ifdef DEBUG_MONITOR
char buffer[100]; char buffer[1000];
#endif #endif
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pid); HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
@ -710,36 +700,46 @@ unsigned int _stdcall waitProcTermination(void* pv)
// Return number of bytes in target or -1 in case of error // Return number of bytes in target or -1 in case of error
int copyTo(char * target, const char * source, int cpyLength, int availSpace) int copyTo(char * target, const char * source, int cpyLength, int availSpace)
{ {
#ifdef DEBUG_MONITOR
char buffer[1000];
#endif
BOOL bSlash = FALSE; BOOL bSlash = FALSE;
int i, j; int i = 0, j = 0;
int totCpyLength = cpyLength; int totCpyLength = cpyLength;
BOOL bQoutedTerm = FALSE;
if(availSpace < cpyLength) if(availSpace < cpyLength)
return -1; return -1;
strncpy(target, source, cpyLength); //strncpy(target, source, cpyLength);
return cpyLength; //return cpyLength;
// Don't open this feature for a while if(('\"' == *source) && ('\"' == *(source + cpyLength)))
bQoutedTerm = TRUE; // Already quoted
else
if(strchr(source, ' ') == NULL)
bQoutedTerm = TRUE; // No reason to quotate term becase it doesn't have embedded spaces
else
{
*target = '\"';
++j;
}
for(i = 0, j = 0; i < cpyLength; ++i, ++j)
for(; i < cpyLength; ++i, ++j)
{ {
if(source[i] == '\\') if(source[i] == '\\')
bSlash = TRUE; bSlash = TRUE;
else else
if(source[i] == '"') if((source[i] == '\"') && (!bQoutedTerm || (i != 0) || i != (cpyLength)) )
{ {
if(bSlash) if(!bSlash)
{ {
if(j == availSpace) if(j == availSpace)
return -1; return -1;
target[j] = '\\'; target[j] = '\\';
++j; ++j;
bSlash = FALSE;
} }
if(j == availSpace) bSlash = FALSE;
return -1;
target[j] = '\\';
++j;
} }
else else
bSlash = FALSE; bSlash = FALSE;
@ -749,6 +749,18 @@ int copyTo(char * target, const char * source, int cpyLength, int availSpace)
target[j] = source[i]; target[j] = source[i];
} }
if(!bQoutedTerm)
{
if(j == availSpace)
return -1;
target[j] = '\"';
++j;
}
#ifdef DEBUG_MONITOR
sprintf(buffer, "copyTo: %s %d %d\n", source, j, cpyLength);
OutputDebugString(buffer);
#endif
return j; return j;
} }

View file

@ -26,6 +26,8 @@
// #define DEBUG_MONITOR // #define DEBUG_MONITOR
#define MAX_CMD_LINE_LENGTH (1024) #define MAX_CMD_LINE_LENGTH (1024)
int copyTo(char * target, const char * source, int cpyLength, int availSpace);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI HandlerRoutine( DWORD dwCtrlType) // control signal type BOOL WINAPI HandlerRoutine( DWORD dwCtrlType) // control signal type
{ {
@ -64,6 +66,24 @@ extern "C" int _tmain(int argc, TCHAR* argv[]) {
// Construct the full command line // Construct the full command line
TCHAR szCmdLine[MAX_CMD_LINE_LENGTH] = { 0 }; TCHAR szCmdLine[MAX_CMD_LINE_LENGTH] = { 0 };
int nPos = 0;
for(int i = 4; i < argc; ++i)
{
int nCpyLen;
if(0 > (nCpyLen = copyTo(szCmdLine + nPos, argv[i], _tcslen(argv[i]), MAX_CMD_LINE_LENGTH - nPos)))
{
#ifdef DEBUG_MONITOR
OutputDebugString("Not enough space to build command line\n");
#endif
return 0;
}
nPos += nCpyLen;
szCmdLine[nPos] = _T(' ');
++nPos;
}
szCmdLine[nPos] = _T('\0');
/*
for (int i = 4; i < argc; i++) { for (int i = 4; i < argc; i++) {
if(sizeof(szCmdLine) > (_tcslen(szCmdLine) + _tcslen(argv[i]))) if(sizeof(szCmdLine) > (_tcslen(szCmdLine) + _tcslen(argv[i])))
{ {
@ -75,6 +95,7 @@ extern "C" int _tmain(int argc, TCHAR* argv[]) {
OutputDebugString("Command line is too long\n"); OutputDebugString("Command line is too long\n");
#endif #endif
} }
*/
STARTUPINFO si = { sizeof(si) }; STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = { 0 }; PROCESS_INFORMATION pi = { 0 };
@ -173,5 +194,66 @@ extern "C" int _tmain(int argc, TCHAR* argv[]) {
return(dwExitCode); return(dwExitCode);
} }
// Return number of bytes in target or -1 in case of error
int copyTo(LPTSTR target, LPCTSTR source, int cpyLength, int availSpace)
{
BOOL bSlash = FALSE;
int i = 0, j = 0;
int totCpyLength = cpyLength;
BOOL bQoutedTerm = FALSE;
if(availSpace < cpyLength)
return -1;
// strncpy(target, source, cpyLength);
// return cpyLength;
if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength)))
bQoutedTerm = TRUE; // Already quoted
else
if(_tcschr(source, _T(' ')) == NULL)
bQoutedTerm = TRUE; // No reason to quotate term becase it doesn't have embedded spaces
else
{
*target = _T('\"');
++j;
}
for(; i < cpyLength; ++i, ++j)
{
if(source[i] == _T('\\'))
bSlash = TRUE;
else
if((source[i] == _T('\"')) && (!bQoutedTerm || (i != 0) || i != (cpyLength)) )
{
if(!bSlash)
{
if(j == availSpace)
return -1;
target[j] = _T('\\');
++j;
}
bSlash = FALSE;
}
else
bSlash = FALSE;
if(j == availSpace)
return -1;
target[j] = source[i];
}
if(!bQoutedTerm)
{
if(j == availSpace)
return -1;
target[j] = _T('\"');
++j;
}
return j;
}
//////////////////////////////// End of File ////////////////////////////////// //////////////////////////////// End of File //////////////////////////////////