exec cleanup

This commit is contained in:
Berke Viktor 2011-11-27 08:53:17 +01:00
parent 76a8166320
commit 5d50ec01cb

View File

@ -25,36 +25,33 @@
*/ */
#include <windows.h> #include <windows.h>
#include <time.h>
#include "xchat-plugin.h" #include "xchat-plugin.h"
static xchat_plugin *ph; /* plugin handle */ static xchat_plugin *ph; /* plugin handle */
static int
/*static void
run_command (char *word[], char *word_eol[], void *userdata) run_command (char *word[], char *word_eol[], void *userdata)
{ {
char commandLine[1024]; char commandLine[1024];
char buf[100]; char buffer[4096];
char buff1[256]; DWORD dwRead = 0;
DWORD dwRead,tdwRead,tdwAfail,tdwLeft, dwAvail, ctr = 0; DWORD dwLeft = 0;
DWORD dwAvail = 0;
time_t start;
double timeElapsed;
HANDLE readPipe, writePipe; HANDLE readPipe;
HANDLE writePipe;
STARTUPINFO sInfo; STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo; PROCESS_INFORMATION pInfo;
BOOL res;
DWORD reDword;
SECURITY_ATTRIBUTES secattr; SECURITY_ATTRIBUTES secattr;
ZeroMemory (&secattr, sizeof (secattr)); ZeroMemory (&secattr, sizeof (secattr));
secattr.nLength = sizeof (secattr); secattr.nLength = sizeof (secattr);
secattr.bInheritHandle = TRUE; secattr.bInheritHandle = TRUE;
xchat_printf (ph, "%d", strlen(word[2]));
if (strlen (word[2]) > 0) if (strlen (word[2]) > 0)
{ {
strcpy (commandLine, "cmd.exe /c "); strcpy (commandLine, "cmd.exe /c ");
@ -62,7 +59,6 @@ run_command (char *word[], char *word_eol[], void *userdata)
CreatePipe (&readPipe, &writePipe, &secattr, 0); CreatePipe (&readPipe, &writePipe, &secattr, 0);
ZeroMemory (&sInfo, sizeof (sInfo)); ZeroMemory (&sInfo, sizeof (sInfo));
ZeroMemory (&pInfo, sizeof (pInfo)); ZeroMemory (&pInfo, sizeof (pInfo));
sInfo.cb = sizeof (sInfo); sInfo.cb = sizeof (sInfo);
@ -74,106 +70,33 @@ run_command (char *word[], char *word_eol[], void *userdata)
CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo); CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo);
CloseHandle (writePipe); CloseHandle (writePipe);
//now read the output pipe here. start = time (0);
while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10)
//do
//{
// res=ReadFile(readPipe,buf,100,&reDword,0);
//csTemp=buf;
//m_csOutput+=csTemp.Left(reDword);
//xchat_printf (ph, "%s", buf);
//strcpy(buf, "\0");
//fflush(buf);
//}while(res);
while (PeekNamedPipe(readPipe, buff1, 1, &dwRead, &dwAvail, &tdwLeft))
{ {
if (dwRead) if (dwRead)
{ {
if (ReadFile(readPipe, buff1, sizeof(buff1) - 1, &dwRead, NULL) && dwRead != 0 ) if (ReadFile (readPipe, buffer, sizeof (buffer) - 1, &dwRead, NULL) && dwRead != 0 )
{ {
buff1[dwRead] = '\0'; buffer[dwRead] = '\0';
//cout << buff1; xchat_printf (ph, "%s\n", buffer);
xchat_printf (ph, "%s\n", buff1);
//cout.flush();
//fflush(buff1);
//memset(&buff1[0], 0, sizeof(buff1));
strcpy (buff1, "");
} }
} }
timeElapsed = difftime (time (0), start);
} }
} }
return XCHAT_EAT_ALL; if (timeElapsed >= 10)
}*/
static void
run_command (char *word[], char *word_eol[], void *userdata)
{ {
return XCHAT_EAT_ALL; xchat_printf (ph, "Execution took too long, aborting.\n");
} }
CloseHandle (readPipe);
CloseHandle (pInfo.hProcess);
CloseHandle (pInfo.hThread);
return XCHAT_EAT_XCHAT;
/*static void
run_command (char *word[], char *word_eol[], void *userdata)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
char commandLine[1024];
HANDLE pipeWriter;
HANDLE pipeReader;
char buff1[128];
DWORD dwRead,tdwRead,tdwAfail,tdwLeft, dwAvail, ctr = 0;
//CreatePipe (&pipeReader, &pipeWriter, NULL, NULL);
ZeroMemory (&si, sizeof (si));
si.cb = sizeof (si);
ZeroMemory (&pi, sizeof (pi));
si.hStdOutput = pipeWriter;
strcpy (commandLine, "cmd.exe /c ");
strcat (commandLine, word_eol[2]);
if (!CreateProcess ( NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
xchat_print (ph, "Error launching the Command Line!");
} }
while (PeekNamedPipe(pipeReader, buff1, 1, &dwRead, &dwAvail, &tdwLeft)) {
if (dwRead)
{
if (ReadFile(pipeReader, buff1, sizeof(buff1) - 1, &dwRead, NULL) && dwRead != 0 )
{
buff1[dwRead] = '\0';
xchat_printf (ph, "%s\n", buff1);
//cout.flush();
}
}
else
{
sprintf(buff1, "No data loop count = %d. Do something here\n", ++ctr);
xchat_printf (ph, "%s\n", buff1);
//cout.flush();
SleepEx(1000, FALSE);
}
}
CloseHandle (pipeWriter);
CloseHandle (pipeReader);
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
}*/
int int
xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
{ {
@ -184,7 +107,7 @@ xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugi
*plugin_version = "1.0"; *plugin_version = "1.0";
xchat_hook_command (ph, "EXEC", XCHAT_PRI_NORM, run_command, 0, 0); xchat_hook_command (ph, "EXEC", XCHAT_PRI_NORM, run_command, 0, 0);
xchat_printf (ph, "Exec plugin loaded\n"); xchat_printf (ph, "%s plugin loaded\n", *plugin_name);
return 1; /* return 1 for success */ return 1; /* return 1 for success */
} }