Bug 568079: Add missing bracers

Change-Id: Icd916d224d4900cf2227f868f8b94cdae13c3f6b
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
This commit is contained in:
Torbjörn Svensson 2020-10-31 16:26:19 +01:00
parent 82bc0b67e5
commit 3ae7b88630
19 changed files with 258 additions and 193 deletions

View file

@ -45,7 +45,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
}
/*
* Make sure we can create our pipes before forking.
* Make sure we can create our pipes before forking.
*/
if (channels != NULL && console) {
if (pipe(pipe2) < 0) {
@ -112,8 +112,9 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
int fdlimit = sysconf(_SC_OPEN_MAX);
int fd = 3;
while (fd < fdlimit)
while (fd < fdlimit) {
close(fd++);
}
}
if (envp[0] == NULL) {
@ -133,8 +134,9 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
channels[1] = fdm; /* Output Stream. */
if (console) {
/* close the write end of pipe1 */
if (close(pipe2[1]) == -1)
if (close(pipe2[1]) == -1) {
perror("close(pipe2[1])");
}
channels[2] = pipe2[0]; /* stderr Stream. */
} else {
channels[2] = fdm; /* Error Stream. */
@ -148,6 +150,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
free(full_path);
return -1; /*NOT REACHED */
}
#ifdef __STAND_ALONE__
int main(int argc, char **argv, char **envp) {
const char *path = "./bufferring_test";

View file

@ -41,7 +41,7 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
}
/*
* Make sure we can create our pipes before forking.
* Make sure we can create our pipes before forking.
*/
if (channels != NULL) {
if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) {
@ -66,16 +66,19 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
if (channels != NULL) {
/* Close the write end of pipe0 */
if (close(pipe0[1]) == -1)
if (close(pipe0[1]) == -1) {
perror("close(pipe0[1])");
}
/* Close the read end of pipe1 */
if (close(pipe1[0]) == -1)
if (close(pipe1[0]) == -1) {
perror("close(pipe1[0])");
}
/* Close the read end of pipe2 */
if (close(pipe2[0]) == -1)
if (close(pipe2[0]) == -1) {
perror("close(pipe2[0]))");
}
/* redirections */
dup2(pipe0[0], STDIN_FILENO); /* dup stdin */
@ -88,8 +91,9 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
int fdlimit = sysconf(_SC_OPEN_MAX);
int fd = 3;
while (fd < fdlimit)
while (fd < fdlimit) {
close(fd++);
}
}
setpgid(getpid(), getpid());
@ -108,16 +112,19 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
if (channels != NULL) {
/* close the read end of pipe1 */
if (close(pipe0[0]) == -1)
if (close(pipe0[0]) == -1) {
perror("close(pipe0[0])");
}
/* close the write end of pipe2 */
if (close(pipe1[1]) == -1)
if (close(pipe1[1]) == -1) {
perror("close(pipe1[1])");
}
/* close the write end of pipe2 */
if (close(pipe2[1]) == -1)
if (close(pipe2[1]) == -1) {
perror("close(pipe2[1])");
}
channels[0] = pipe0[1]; /* Output Stream. */
channels[1] = pipe1[0]; /* Input Stream. */
@ -136,8 +143,9 @@ int wait0(pid_t pid) {
int status;
int val = -1;
if (pid < 0)
if (pid < 0) {
return -1;
}
for (;;) {
if (waitpid(pid, &status, 0) < 0) {

View file

@ -24,8 +24,9 @@
static void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = (*env)->FindClass(env, name);
if (cls != 0) /* Otherwise an exception has already been thrown */
if (cls != 0) { /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg);
}
/* It's a good practice to clean up the local references. */
(*env)->DeleteLocalRef(env, cls);

View file

@ -43,24 +43,28 @@ int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct
char line[20];
line[0] = 0;
*amaster = ptym_open(line);
if (*amaster < 0)
if (*amaster < 0) {
return -1;
}
*aslave = ptys_open(*amaster, line);
if (*aslave < 0) {
close(*amaster);
return -1;
}
if (name)
if (name) {
strcpy(name, line);
}
#ifndef TCSAFLUSH
#define TCSAFLUSH TCSETAF
#endif
if (termp)
if (termp) {
(void) tcsetattr(*aslave, TCSAFLUSH, termp);
}
#ifdef TIOCSWINSZ
if (winp)
if (winp) {
(void) ioctl(*aslave, TIOCSWINSZ, (char *)winp);
}
#endif
return 0;
}
@ -91,8 +95,9 @@ int ptym_open(char *pts_name) {
#else
fdm = getpt();
#endif
if (fdm < 0)
if (fdm < 0) {
return -1;
}
if (grantpt(fdm) < 0) { /* grant access to slave */
close(fdm);
return -2;

View file

@ -34,8 +34,9 @@ const int path_def_len = 5; /* strlen(PATH_DEF); */
char* path_val(char *const envp[]) {
int i;
if (envp == NULL || envp[0] == NULL)
if (envp == NULL || envp[0] == NULL) {
return getenv("PATH");
}
for (i = 0; envp[i] != NULL; i++) {
char *p = envp[i];
@ -60,7 +61,7 @@ char* pfind(const char *name, char *const envp[]) {
return NULL;
}
/* For absolute name or name with a path, check if it is an executable. */
/* For absolute name or name with a path, check if it is an executable. */
if (name[0] == '/' || name[0] == '.') {
if (access(name, X_OK) == 0) {
return strdup(name);
@ -68,7 +69,7 @@ char* pfind(const char *name, char *const envp[]) {
return NULL;
}
/* Search in the PATH environment. */
/* Search in the PATH environment. */
path = path_val(envp);
if (path == NULL || strlen(path) <= 0) {
@ -76,7 +77,7 @@ char* pfind(const char *name, char *const envp[]) {
return NULL;
}
/* The value return by getenv() is readonly */
/* The value return by getenv() is read-only */
path = strdup(path);
tok = strtok_r(path, ":", &sp);
@ -103,12 +104,13 @@ int main(int argc, char **argv)
int i;
char *fullpath;
for (i=1; i<argc; i++) {
for (i = 1; i < argc; i++) {
fullpath = pfind(argv[i], NULL);
if (fullpath == NULL)
if (fullpath == NULL) {
printf("Unable to find %s in $PATH.\n", argv[i]);
else
} else {
printf("Found %s @ %s.\n", argv[i], fullpath);
}
}
}
#endif

View file

@ -44,8 +44,7 @@ Java_org_eclipse_cdt_utils_pty_PTYInputStream_read0(JNIEnv *env, jobject jobj, j
status = -1;
} else if (status == -1) {
/* Error, toss an exception */
/* Ignore the error for now, the debugger will attempt
* to close this multiple time. */
/* Ignore the error for now, the debugger will attempt to close this multiple time. */
#if 0
jclass exception = (*env)->FindClass(env, "java/io/IOException");
if (exception == NULL) {

View file

@ -30,11 +30,9 @@
*/
#if DEBUGIT
static void print_array(char **c_array)
{
static void print_array(char **c_array) {
if (c_array) {
char **p = c_array;
for (; *p; p++) {
for (char **p = c_array; *p; p++) {
if (*p) {
fprintf(stderr, " %s", *p);
}
@ -51,8 +49,9 @@ static char** alloc_c_array(JNIEnv *env, jobjectArray j_array) {
jint c_array_size = (*env)->GetArrayLength(env, j_array);
char **c_array = calloc(c_array_size + 1, sizeof(*c_array));
if (c_array == NULL)
if (c_array == NULL) {
return NULL;
}
for (i = 0; i < c_array_size; i++) {
jstring j_str = (jstring)(*env)->GetObjectArrayElement(env, j_array, i);
@ -67,8 +66,7 @@ static char** alloc_c_array(JNIEnv *env, jobjectArray j_array) {
static void free_c_array(char **c_array) {
if (c_array) {
char **p = c_array;
for (; *p; p++) {
for (char **p = c_array; *p; p++) {
if (*p) {
free(*p);
}
@ -86,16 +84,19 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
int fd[3];
pid_t pid = -1;
if (jchannels == NULL)
if (jchannels == NULL) {
goto bail_out;
}
cmd = alloc_c_array(env, jcmd);
if (cmd == NULL)
if (cmd == NULL) {
goto bail_out;
}
envp = alloc_c_array(env, jenv);
if (envp == NULL)
if (envp == NULL) {
goto bail_out;
}
#if DEBUGIT
fprintf(stderr, "command:");
@ -107,8 +108,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
#endif
pid = exec_pty(cmd[0], cmd, envp, dirpath, fd, pts_name, masterFD, console);
if (pid < 0)
if (pid < 0) {
goto bail_out;
}
jobject cls = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$UnixChannel");
jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
@ -119,10 +121,12 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath);
(*env)->ReleaseStringUTFChars(env, jslaveName, pts_name);
if (cmd)
if (cmd) {
free_c_array(cmd);
if (envp)
}
if (envp) {
free_c_array(envp);
}
return pid;
}
@ -135,12 +139,14 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *env, jobject jobj, jobj
pid_t pid = -1;
cmd = alloc_c_array(env, jcmd);
if (cmd == NULL)
if (cmd == NULL) {
goto bail_out;
}
envp = alloc_c_array(env, jenv);
if (envp == NULL)
if (envp == NULL) {
goto bail_out;
}
#if DEBUGIT
fprintf(stderr, "command:");
@ -151,14 +157,17 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *env, jobject jobj, jobj
#endif
pid = exec0(cmd[0], cmd, envp, dirpath, NULL);
if (pid < 0)
if (pid < 0) {
goto bail_out;
}
bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath);
if (cmd)
if (cmd) {
free_c_array(cmd);
if (envp)
}
if (envp) {
free_c_array(envp);
}
return pid;
}
@ -173,24 +182,29 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *env, jobject jobj, jobj
jclass channelClass = NULL;
jmethodID channelConstructor = NULL;
if (jchannels == NULL)
if (jchannels == NULL) {
goto bail_out;
}
channelClass = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$UnixChannel");
if (channelClass == 0)
if (channelClass == 0) {
goto bail_out;
}
channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(I)V");
if (channelConstructor == 0)
if (channelConstructor == 0) {
goto bail_out;
}
cmd = alloc_c_array(env, jcmd);
if (cmd == NULL)
if (cmd == NULL) {
goto bail_out;
}
envp = alloc_c_array(env, jenv);
if (envp == NULL)
if (envp == NULL) {
goto bail_out;
}
#if DEBUGIT
fprintf(stderr, "command:");
@ -200,8 +214,9 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *env, jobject jobj, jobj
fprintf(stderr, "dirpath: %s\n", dirpath);
#endif
pid = exec0(cmd[0], cmd, envp, dirpath, fd);
if (pid < 0)
if (pid < 0) {
goto bail_out;
}
for (jsize i = 0; i < 3; i++) {
jobject chan = (*env)->NewObject(env, channelClass, channelConstructor, fd[i]);
@ -209,10 +224,12 @@ Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *env, jobject jobj, jobj
}
bail_out: (*env)->ReleaseStringUTFChars(env, jdir, dirpath);
if (cmd)
if (cmd) {
free_c_array(cmd);
if (envp)
}
if (envp) {
free_c_array(envp);
}
return pid;
}

View file

@ -308,8 +308,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
jsize len = (*env)->GetStringLength(env, item);
const wchar_t *str = (const wchar_t*) (*env)->GetStringChars(env, item, 0);
if (NULL != str) {
while ((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
{
while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0'
nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (wchar_t*) realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
if (NULL == szEnvBlock) {
@ -373,17 +372,19 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
&si, /* (in) startup information */
&pi); /* (out) process information */
if (NULL != cwd)
if (NULL != cwd) {
free((void*) cwd);
}
if (NULL != szEnvBlock)
if (NULL != szEnvBlock) {
free(szEnvBlock);
}
if (NULL != szCmdLine)
if (NULL != szCmdLine) {
free(szCmdLine);
}
if (!ret) // Launching error
{
if (!ret) { // Launching error
char *lpMsgBuf;
CloseHandle(stdHandles[0]);
CloseHandle(stdHandles[1]);
@ -407,8 +408,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
h[1] = pi.hProcess;
what = WaitForMultipleObjects(2, h, FALSE, INFINITE);
if (what != WAIT_OBJECT_0) // CreateProcess failed
{
if (what != WAIT_OBJECT_0) { // CreateProcess failed
#ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Process %i failed\n"), pi.dwProcessId);
OutputDebugStringW(buffer);
@ -529,8 +529,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *
jsize len = (*env)->GetStringLength(env, item);
const wchar_t *str = (const wchar_t*) (*env)->GetStringChars(env, item, 0);
if (NULL != str) {
while ((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
{
while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0'
nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (wchar_t*) realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
if (NULL == szEnvBlock) {
@ -573,15 +572,17 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *
&si, /* (in) startup information */
&pi); /* (out) process information */
if (NULL != cwd)
if (NULL != cwd) {
free(cwd);
if (NULL != szEnvBlock)
}
if (NULL != szEnvBlock) {
free(szEnvBlock);
if (NULL != szCmdLine)
}
if (NULL != szCmdLine) {
free(szCmdLine);
}
if (!ret) // error
{
if (!ret) { // error
char *lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
@ -635,8 +636,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise(JNIEnv *
hProc = OpenProcess(SYNCHRONIZE, 0, pCurProcInfo->pid);
if (NULL == hProc)
if (NULL == hProc) {
return -1;
}
switch (signal) {
case SIG_NOOP:
@ -649,26 +651,26 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_raise(JNIEnv *
break;
case SIG_TERM:
#ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received TERM signal for process %i\n"),
pCurProcInfo -> pid);
OutputDebugStringW(buffer);
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received TERM signal for process %i\n"),
pCurProcInfo -> pid);
OutputDebugStringW(buffer);
#endif
SetEvent(pCurProcInfo->eventTerminate);
#ifdef DEBUG_MONITOR
OutputDebugStringW(_T("Spawner signaled TERM event\n"));
OutputDebugStringW(_T("Spawner signaled TERM event\n"));
#endif
ret = 0;
break;
case SIG_KILL:
#ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received KILL signal for process %i\n"),
pCurProcInfo -> pid);
OutputDebugStringW(buffer);
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("Spawner received KILL signal for process %i\n"),
pCurProcInfo -> pid);
OutputDebugStringW(buffer);
#endif
SetEvent(pCurProcInfo->eventKill);
#ifdef DEBUG_MONITOR
OutputDebugStringW(_T("Spawner signaled KILL event\n"));
OutputDebugStringW(_T("Spawner signaled KILL event\n"));
#endif
ret = 0;
break;
@ -705,13 +707,15 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv
HANDLE hProc;
pProcInfo_t pCurProcInfo = findProcInfo(uid);
if (NULL == pCurProcInfo)
if (NULL == pCurProcInfo) {
return -1;
}
hProc = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, 0, pCurProcInfo->pid);
if (NULL == hProc)
if (NULL == hProc) {
return -1;
}
what = WaitForSingleObject(hProc, INFINITE);
@ -719,8 +723,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv
GetExitCodeProcess(hProc, &exit_code);
}
if (hProc)
if (hProc) {
CloseHandle(hProc);
}
return exit_code;
}
@ -736,8 +741,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_waitFor(JNIEnv
void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = (*env)->FindClass(env, name);
if (cls != 0) /* Otherwise an exception has already been thrown */
if (cls != 0) { /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg);
}
/* It's a good practice to clean up the local references. */
(*env)->DeleteLocalRef(env, cls);
@ -781,8 +787,9 @@ pProcInfo_t createProcInfo() {
pProcInfo_t findProcInfo(int uid) {
int i;
pProcInfo_t p = NULL;
if (NULL == pInfo)
if (NULL == pInfo) {
return NULL;
}
for (i = 0; i < MAX_PROCS; ++i) {
if (pInfo[i].uid == uid) {
@ -832,8 +839,7 @@ void cleanUpProcBlock(pProcInfo_t pCurProcInfo) {
// pv - pointer to PROCESS_INFORMATION struct
// Return : no
/////////////////////////////////////////////////////////////////////////////////////
void _cdecl waitProcTermination(void* pv)
{
void _cdecl waitProcTermination(void* pv) {
PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv;
int i;
#ifdef DEBUG_MONITOR
@ -843,10 +849,8 @@ void _cdecl waitProcTermination(void* pv)
// wait for process termination
WaitForSingleObject(pi->hProcess, INFINITE);
for(i = 0; i < MAX_PROCS; ++i)
{
if(pInfo[i].pid == pi->dwProcessId)
{
for (i = 0; i < MAX_PROCS; ++i) {
if (pInfo[i].pid == pi->dwProcessId) {
cleanUpProcBlock(pInfo + i);
#ifdef DEBUG_MONITOR
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("waitProcTermination: set PID %i to 0\n"),
@ -880,8 +884,9 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
int nQuotationMode = 0;
if (availSpace <= cpyLength) // = to reserve space for final '\0'
if (availSpace <= cpyLength) { // = to reserve space for final '\0'
return -1;
}
if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) {
nQuotationMode = QUOTATION_DONE;
@ -896,16 +901,16 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
}
for (; i < cpyLength; ++i, ++j) {
if (source[i] == _T('\\'))
if (source[i] == _T('\\')) {
bSlash = TRUE;
else {
} else {
// Don't escape embracing quotation marks
if ((source[i] == _T('\"'))
&& !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) {
if (!bSlash) // If still not escaped
{
if (j == availSpace)
if (!bSlash) { // If still not escaped
if (j == availSpace) {
return -1;
}
target[j] = _T('\\');
++j;
}
@ -913,14 +918,16 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
bSlash = FALSE;
}
if (j == availSpace)
if (j == availSpace) {
return -1;
}
target[j] = source[i];
}
if (nQuotationMode == QUOTATION_DO) {
if (j == availSpace)
if (j == availSpace) {
return -1;
}
target[j] = _T('\"');
++j;
}

View file

@ -98,22 +98,22 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_rea
if (err == ERROR_IO_PENDING) {
// asynchronous i/o is still in progress
// check on the results of the asynchronous read
if (GetOverlappedResult(handle, &overlapped, &nNumberOfBytesRead, TRUE))
if (GetOverlappedResult(handle, &overlapped, &nNumberOfBytesRead, TRUE)) {
err = 0;
// if there was a problem ...
else
} else { // if there was a problem ...
err = GetLastError();
}
}
if (err == ERROR_BROKEN_PIPE) // Pipe was closed
if (err == ERROR_BROKEN_PIPE) { // Pipe was closed
break;
}
if (err != 0) {
char *lpMsgBuf;
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Read failed - %i, error %i\n"), fd, err);
OutputDebugStringW(buffer);
#endif
if (err != ERROR_MORE_DATA) // Otherwise error means just that there are more data
{ // than buffer can accept
if (err != ERROR_MORE_DATA) { // Otherwise error means just that there are more data than buffer can accept
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
@ -135,19 +135,21 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_rea
}
}
}
if (nNumberOfBytesRead > 0)
if (nNumberOfBytesRead > 0) {
(*env)->SetByteArrayRegion(env, buf, nBuffOffset, nNumberOfBytesRead, tmpBuf);
else
} else {
break;
}
nBuffOffset += nNumberOfBytesRead;
if (nNumberOfBytesRead != nNumberOfBytesToRead)
if (nNumberOfBytesRead != nNumberOfBytesToRead) {
break;
else {
} else {
// Is there data left in the pipe?
DWORD bytesAvailable = 0;
if (!PeekNamedPipe(handle, NULL, 0, NULL, &bytesAvailable, NULL) || bytesAvailable == 0)
if (!PeekNamedPipe(handle, NULL, 0, NULL, &bytesAvailable, NULL) || bytesAvailable == 0) {
// No bytes left
break;
}
}
}
CloseHandle(overlapped.hEvent);
@ -169,14 +171,14 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerInputStream_clo
int rc;
HANDLE handle = channelToHandle(env, channel);
#ifdef DEBUG_MONITOR
_TCHAR buffer[1000];
_stprintf(buffer, _T("Close %i\n"), fd);
OutputDebugStringW(buffer);
_TCHAR buffer[1000];
_stprintf(buffer, _T("Close %i\n"), fd);
OutputDebugStringW(buffer);
#endif
rc = (CloseHandle(handle) ? 0 : -1);
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Closed %i\n"), fd);
OutputDebugStringW(buffer);
_stprintf(buffer, _T("Closed %i\n"), fd);
OutputDebugStringW(buffer);
#endif
return (rc ? GetLastError() : 0);
}
@ -232,15 +234,15 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_SpawnerOutputStream_cl
int rc;
HANDLE handle = channelToHandle(env, channel);
#ifdef DEBUG_MONITOR
_TCHAR buffer[1000];
_stprintf(buffer, _T("Close %i\n"), fd);
OutputDebugStringW(buffer);
_TCHAR buffer[1000];
_stprintf(buffer, _T("Close %i\n"), fd);
OutputDebugStringW(buffer);
#endif
FlushFileBuffers(handle);
rc = (CloseHandle(handle) ? 0 : -1);
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Closed %i\n"), fd);
OutputDebugStringW(buffer);
_stprintf(buffer, _T("Closed %i\n"), fd);
OutputDebugStringW(buffer);
#endif
return (rc ? GetLastError() : 0);
}

View file

@ -53,8 +53,9 @@ JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_pty_PTY_openMaster(JNIEnv *
master = rand();
/* Make sure masterFD does not exist */
while (fd2pty.find(master) != fd2pty.end())
while (fd2pty.find(master) != fd2pty.end()) {
master++;
}
sprintf(line, "winpty_%i", master);
@ -86,8 +87,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_change_1window_1size(J
fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL)
if (winpty != NULL) {
return winpty_set_size(winpty, width, height);
}
}
return 0;
@ -121,15 +123,18 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYInputStream_read0(JNIEn
BOOL ret = ReadFile(handle, buffer, buf_len, &amount, &over);
if (!ret) {
DWORD error = GetLastError();
if (error == ERROR_IO_PENDING)
if (error == ERROR_IO_PENDING) {
ret = GetOverlappedResult(handle, &over, &amount, TRUE);
}
}
if (ret && amount > 0)
if (ret && amount > 0) {
memcpy(data, buffer, amount);
}
if (!ret || amount == 0)
if (!ret || amount == 0) {
amount = -1;
}
if (!ret && fd2pty.find(fd) != fd2pty.end()) {
int rc = winpty_get_exit_code(winpty);
@ -190,10 +195,12 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYOutputStream_write0(JNI
BOOL ret = WriteFile(handle, buffer, buf_len, &written, &over);
env->ReleaseByteArrayElements(buf, data, 0);
if (!ret && GetLastError() == ERROR_IO_PENDING)
if (!ret && GetLastError() == ERROR_IO_PENDING) {
ret = GetOverlappedResult(handle, &over, &written, TRUE);
if (!ret || (int) written != buf_len)
}
if (!ret || (int) written != buf_len) {
written = -1;
}
delete[] buffer;
}
@ -227,10 +234,11 @@ static std::wstring convertSlashes(const wchar_t *path) {
std::wstring ret;
for (int i = 0; path[i] != L'\0'; ++i) {
if (path[i] == L'/')
if (path[i] == L'/') {
ret.push_back(L'\\');
else
} else {
ret.push_back(path[i]);
}
}
return ret;
@ -241,12 +249,14 @@ static std::wstring convertSlashes(const wchar_t *path) {
static std::wstring argvToCommandLine(const std::vector<std::wstring> &argv) {
std::wstring result;
for (size_t argIndex = 0; argIndex < argv.size(); ++argIndex) {
if (argIndex > 0)
if (argIndex > 0) {
result.push_back(L' ');
}
const wchar_t *arg = argv[argIndex].c_str();
const bool quote = wcschr(arg, L' ') != NULL || wcschr(arg, L'\t') != NULL || *arg == L'\0';
if (quote)
if (quote) {
result.push_back(L'\"');
}
int bsCount = 0;
for (const wchar_t *p = arg; *p != L'\0'; ++p) {
if (*p == L'\\') {
@ -285,8 +295,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
jint argc = env->GetArrayLength(jcmd);
jint envc = env->GetArrayLength(jenv);
if (jchannels == NULL || env->GetArrayLength(jchannels) != 3)
if (jchannels == NULL || env->GetArrayLength(jchannels) != 3) {
goto bail_out;
}
fd = masterFD;
fd2pty_Iter = fd2pty.find(fd);
@ -298,10 +309,11 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
for (i = 0; i < argc; i++) {
jstring j_str = (jstring) env->GetObjectArrayElement(jcmd, i);
const wchar_t *w_str = (const wchar_t*) env->GetStringChars(j_str, NULL);
if (i == 0)
if (i == 0) {
argVector.push_back(convertSlashes(w_str));
else
} else {
argVector.push_back(w_str);
}
env->ReleaseStringChars(j_str, (const jchar*) w_str);
env->DeleteLocalRef(j_str);
}
@ -352,8 +364,9 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_waitFor(JNIEnv *env, j
BOOL success;
do {
success = GetHandleInformation(handle, &flags);
if (success)
if (success) {
Sleep(500);
}
} while (success);
fd2rc_Iter = fd2rc.find(fd);

View file

@ -49,8 +49,9 @@ HMODULE PTYExplicitLoadLibrary(LPCSTR pszModuleName) {
}
FARPROC WINAPI PTYDliNotifyHook(unsigned dliNotify, PDelayLoadInfo pdli) {
if (dliNotify == dliNotePreLoadLibrary)
if (dliNotify == dliNotePreLoadLibrary) {
return (FARPROC) PTYExplicitLoadLibrary(pdli->szDll);
}
return NULL;
}

View file

@ -91,8 +91,9 @@ int interruptProcess(int pid) {
FreeLibrary(hmod);
hmod = NULL;
if (success)
if (success) {
return 0; // 0 == OK; if not, try old-school way
}
}
#ifdef DEBUG_MONITOR
@ -102,14 +103,13 @@ int interruptProcess(int pid) {
consoleHWND = NULL;
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Try to interrupt process %i\n"), pid);
OutputDebugStringW(buffer);
_stprintf(buffer, _T("Try to interrupt process %i\n"), pid);
OutputDebugStringW(buffer);
#endif
// Find console
EnumWindows(find_child_console, (LPARAM) pid);
if (NULL != consoleHWND) // Yes, we found out it
{
if (NULL != consoleHWND) { // Yes, we found out it
// We are going to switch focus to console,
// send Ctrl-C and then restore focus
BYTE control_scan_code = (BYTE) MapVirtualKey(VK_CONTROL, 0);
@ -123,20 +123,23 @@ int interruptProcess(int pid) {
foreground_window = GetForegroundWindow();
if (foreground_window) {
/* NT 5.0, and apparently also Windows 98, will not allow
a Window to be set to foreground directly without the
user's involvement. The workaround is to attach
ourselves to the thread that owns the foreground
window, since that is the only thread that can set the
foreground window. */
* a Window to be set to foreground directly without the
* user's involvement. The workaround is to attach
* ourselves to the thread that owns the foreground
* window, since that is the only thread that can set the
* foreground window.
*/
DWORD foreground_thread, child_thread;
foreground_thread = GetWindowThreadProcessId(foreground_window, NULL);
if (foreground_thread == GetCurrentThreadId()
|| !AttachThreadInput(GetCurrentThreadId(), foreground_thread, TRUE))
|| !AttachThreadInput(GetCurrentThreadId(), foreground_thread, TRUE)) {
foreground_thread = 0;
}
child_thread = GetWindowThreadProcessId(consoleHWND, NULL);
if (child_thread == GetCurrentThreadId() || !AttachThreadInput(GetCurrentThreadId(), child_thread, TRUE))
if (child_thread == GetCurrentThreadId() || !AttachThreadInput(GetCurrentThreadId(), child_thread, TRUE)) {
child_thread = 0;
}
/* Set the foreground window to the child. */
if (SetForegroundWindow(consoleHWND)) {
@ -153,24 +156,24 @@ int interruptProcess(int pid) {
SetForegroundWindow(foreground_window);
}
/* Detach from the foreground and child threads now that
the foreground switching is over. */
if (foreground_thread)
/* Detach from the foreground and child threads now that the foreground switching is over. */
if (foreground_thread) {
AttachThreadInput(GetCurrentThreadId(), foreground_thread, FALSE);
if (child_thread)
}
if (child_thread) {
AttachThreadInput(GetCurrentThreadId(), child_thread, FALSE);
}
#ifdef DEBUG_MONITOR
_stprintf(buffer, _T("Sent Ctrl-C & Ctrl-Break to process %i\n"), pid);
OutputDebugStringW(buffer);
_stprintf(buffer, _T("Sent Ctrl-C & Ctrl-Break to process %i\n"), pid);
OutputDebugStringW(buffer);
#endif
}
}
#ifdef DEBUG_MONITOR
else {
} else {
_stprintf(buffer, _T("Cannot find console for process %i\n"), pid);
OutputDebugStringW(buffer);
}
#endif
}
return rc;
}

View file

@ -35,10 +35,11 @@ BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpRese
InitializeCriticalSection(&cs);
GetModuleFileNameW(hModule, path, MAX_PATH);
p = wcsrchr(path, _T('\\'));
if (NULL != p)
if (NULL != p) {
*(p + 1) = _T('\0');
else
} else {
wcscat(path, L"\\");
}
}
break;
case DLL_THREAD_ATTACH:

View file

@ -36,8 +36,7 @@ void DisplayErrorMessage();
//BOOL KillProcessEx(DWORD dwProcessId); // Handle of the process
///////////////////////////////////////////////////////////////////////////////
BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) // control signal type
{
BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) { // control signal type
BOOL ret = TRUE;
switch (dwCtrlType) {
case CTRL_C_EVENT:
@ -66,8 +65,9 @@ bool _isCygwin = true;
bool isCygwin(HANDLE process) {
// Have we checked before?
if (cygwinBin != NULL || !_isCygwin)
if (cygwinBin != NULL || !_isCygwin) {
return _isCygwin;
}
// See if this process loaded cygwin, need a different SIGINT for them
HMODULE mods[1024];
@ -239,7 +239,7 @@ int main() {
CloseHandle(stdHandles[0]);
CloseHandle(stdHandles[1]);
CloseHandle(stdHandles[2]);
return -1;;
return -1;
}
SetHandleInformation(stdHandles[0], HANDLE_FLAG_INHERIT, TRUE);
SetHandleInformation(stdHandles[1], HANDLE_FLAG_INHERIT, TRUE);
@ -254,21 +254,21 @@ int main() {
CloseHandle(stdHandles[0]);
CloseHandle(stdHandles[1]);
CloseHandle(stdHandles[2]);
return -1;;
return -1;
}
#ifdef DEBUG_MONITOR_DETAILS
wchar_t * lpvEnv = GetEnvironmentStringsW();
// If the returned pointer is NULL, exit.
if (lpvEnv == NULL)
OutputDebugStringW(_T("Cannot Read Environment\n"));
else {
if (lpvEnv == NULL) {
OutputDebugStringW(_T("Cannot Read Environment\n"));
} else {
// Variable strings are separated by NULL byte, and the block is
// terminated by a NULL byte.
OutputDebugStringW(_T("Starter: Environment\n"));
for (wchar_t * lpszVariable = (wchar_t *) lpvEnv; *lpszVariable; lpszVariable+=wcslen(lpszVariable) + 1) {
for (wchar_t * lpszVariable = (wchar_t *) lpvEnv; *lpszVariable; lpszVariable += wcslen(lpszVariable) + 1) {
swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), _T("%s\n"), lpszVariable);
OutputDebugStringW(buffer);
}
@ -307,8 +307,9 @@ int main() {
// to our own job object.
BOOL f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE, CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &si, &pi);
// If breaking away from job is not permitted, retry without breakaway flag
if (!f)
if (!f) {
f = CreateProcessW(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
}
// We don't need them any more
CloseHandle(stdHandles[0]);
@ -462,8 +463,9 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
#define QUOTATION_NONE 2
int nQuotationMode = 0;
if (availSpace <= cpyLength) // = to reserve space for '\0'
if (availSpace <= cpyLength) { // = to reserve space for '\0'
return -1;
}
if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) {
// Already done
@ -479,29 +481,35 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
}
for (; i < cpyLength; ++i, ++j) {
if (source[i] == _T('\\'))
if (source[i] == _T('\\')) {
bSlash = TRUE;
else
// Don't escape embracing quotation marks
if ((source[i] == _T('\"')) && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) {
if (!bSlash) {
if (j == availSpace)
return -1;
target[j] = _T('\\');
++j;
} else {
// Don't escape embracing quotation marks
if ((source[i] == _T('\"'))
&& !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))))) {
if (!bSlash) {
if (j == availSpace) {
return -1;
}
target[j] = _T('\\');
++j;
}
bSlash = FALSE;
} else {
bSlash = FALSE;
}
bSlash = FALSE;
} else
bSlash = FALSE;
}
if (j == availSpace)
if (j == availSpace) {
return -1;
}
target[j] = source[i];
}
if (nQuotationMode == QUOTATION_DO) {
if (j == availSpace)
if (j == availSpace) {
return -1;
}
target[j] = _T('\"');
++j;
}

View file

@ -46,11 +46,10 @@
* IOException
*/
#ifndef __MINGW32__
static void closeAndthrowIOException(int fd, JNIEnv *env, const char *msg)
static void closeAndthrowIOException(int fd, JNIEnv *env, const char *msg) {
#else
static void closeAndthrowIOException(HANDLE handle, JNIEnv *env, const char *msg)
static void closeAndthrowIOException(HANDLE handle, JNIEnv *env, const char *msg) {
#endif
{
char buff[256];
#ifndef __MINGW32__
sprintf(buff, "%s: %s", msg, strerror(errno));
@ -342,8 +341,7 @@ JNIEXPORT jlong JNICALL FUNC(open0)(JNIEnv *env, jobject jobj, jstring portName,
}
JNIEXPORT void JNICALL FUNC(close0)
(JNIEnv *env, jobject jobj, jlong handle)
{
(JNIEnv *env, jobject jobj, jlong handle) {
#ifndef __MINGW32__
close(handle);
#else
@ -435,8 +433,7 @@ JNIEXPORT jint JNICALL FUNC(read1)(JNIEnv *env, jobject jobj, jlong jhandle, jby
}
JNIEXPORT void JNICALL FUNC(write0)
(JNIEnv *env, jobject jobj, jlong jhandle, jint b)
{
(JNIEnv *env, jobject jobj, jlong jhandle, jint b) {
#ifndef __MINGW32__
char buff = b;
write(jhandle, &buff, 1);
@ -475,8 +472,7 @@ JNIEXPORT void JNICALL FUNC(write0)
#endif
}
JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size)
{
JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jbyteArray bytes, jint offset, jint size) {
#ifndef __MINGW32__
while (size > 0) {
jbyte buff[256];
@ -512,8 +508,7 @@ JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jb
if (GetLastError() != ERROR_IO_PENDING) {
throwIOException(env, "Error writing to port");
return;
}
else {
} else {
switch (WaitForSingleObject(olp.hEvent, INFINITE)) {
case WAIT_OBJECT_0:
if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) {