JavaScript Lint Code
Status: Beta
Brought to you by:
matthiasmiller
--- a/trunk/src/JavaScriptLintAPI.cpp +++ b/trunk/src/JavaScriptLintAPI.cpp @@ -43,132 +43,17 @@ */ #include "JavaScriptLintAPI.h" +#include "JavaScriptLintExec.h" + #include <assert.h> #include <iostream> #include <sstream> -#include <vector> -#include <xstring> - -#include <windows.h> // see header #pragma warning(push, 4) #pragma warning(disable: 4786) using namespace std; - -string GetLastErrorString() -{ - if (GetLastError() == ERROR_SUCCESS) - return ""; - - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, - GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &lpMsgBuf, 0, NULL); - string error = (char *)lpMsgBuf; - LocalFree(lpMsgBuf); - return error; -} - -bool ExecuteProcess(string commandline, string input, string& output, string& error) -{ - // SEE http://msdn.microsoft.com/library/en-us/dllproc/base/creating_a_child_process_with_redirected_input_and_output.asp?frame=true - output.empty(); - - // Set the bInheritHandle flag so pipe handles are inherited. - SECURITY_ATTRIBUTES saAttr; - saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); - saAttr.bInheritHandle = TRUE; - saAttr.lpSecurityDescriptor = NULL; - - // Create a pipe for the child process's STDOUT that is not inherited - HANDLE hChildStdoutRd, hChildStdoutWr; - if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) - { - error = GetLastErrorString(); - return false; - } - SetHandleInformation(hChildStdoutRd, HANDLE_FLAG_INHERIT, 0); - - // Create a pipe for the child process's STDIN that is not inherited - HANDLE hChildStdinRd, hChildStdinWr; - if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) - { - error = GetLastErrorString(); - return false; - } - SetHandleInformation(hChildStdinWr, HANDLE_FLAG_INHERIT, 0); - - // Create the child process - PROCESS_INFORMATION piProcInfo; - ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION)); - - STARTUPINFO siStartInfo; - ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); - siStartInfo.cb = sizeof(STARTUPINFO); - siStartInfo.hStdError = hChildStdoutWr; - siStartInfo.hStdOutput = hChildStdoutWr; - siStartInfo.hStdInput = hChildStdinRd; - siStartInfo.dwFlags |= STARTF_USESTDHANDLES; - siStartInfo.dwFlags |= STARTF_USESHOWWINDOW; - - if (!CreateProcess(NULL, - (char*)commandline.c_str(), // command line - NULL, // process security attributes - NULL, // primary thread security attributes - TRUE, // handles are inherited - 0, // creation flags - NULL, // use parent's environment - NULL, // use parent's current directory - &siStartInfo, // STARTUPINFO pointer - &piProcInfo)) // receives PROCESS_INFORMATION - { - error = GetLastErrorString(); - return false; - } - CloseHandle(piProcInfo.hProcess); - CloseHandle(piProcInfo.hThread); - - // write the result and close the file so the client stops reading - DWORD dwBytesWritten = 0; - if (!WriteFile(hChildStdinWr, input.c_str(), input.length(), &dwBytesWritten, NULL)) - { - error = GetLastErrorString(); - return false; - } - if (!CloseHandle(hChildStdinWr)) - { - error = GetLastErrorString(); - return false; - } - - // Close the write end of the pipe before reading from the read end of the pipe. - if (!CloseHandle(hChildStdoutWr)) - { - error = GetLastErrorString(); - return false; - } - - // Read output from the child process, and write to parent's STDOUT. - DWORD dwBytesRead; - CHAR chBuf[415]; - for (;;) - { - if (!ReadFile(hChildStdoutRd, chBuf, sizeof(chBuf), &dwBytesRead, NULL)) - { - if (GetLastError() == ERROR_BROKEN_PIPE) - break; - - error = "The output pipe could not be read. " + GetLastErrorString(); - return false; - } - if (!dwBytesRead) - break; - output.append(chBuf, dwBytesRead); - } - return true; -} namespace JSLStrings { @@ -283,7 +168,7 @@ // Run the Lint string results; - if (!ExecuteProcess(commandline, code, results, error)) + if (!JavaScriptLintAPI::ExecuteProcess(commandline, code, results, error)) { error = "Unable to run JavaScript Lint. " + error; return false;