Skip to content

Commit 294869c

Browse files
committed
Delete obsolete WINDOWS section of exec from ShellIntrinsics
1 parent 66f9461 commit 294869c

File tree

1 file changed

+0
-130
lines changed

1 file changed

+0
-130
lines changed

MiniScript-cpp/src/ShellIntrinsics.cpp

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,135 +1126,6 @@ static IntrinsicResult intrinsic_rawDataSetUtf8(Context *context, IntrinsicResul
11261126
return IntrinsicResult(nBytes);
11271127
}
11281128

1129-
#if WINDOWS
1130-
// timeout : The time to wait in milliseconds before killing the child process.
1131-
bool CreateChildProcess(const String& cmd, String& out, String& err, DWORD& returnCode, DWORD timeout) {
1132-
SECURITY_ATTRIBUTES saAttr;
1133-
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
1134-
saAttr.bInheritHandle = TRUE;
1135-
saAttr.lpSecurityDescriptor = nullptr;
1136-
1137-
HANDLE hChildStd_OUT_Rd = nullptr;
1138-
HANDLE hChildStd_OUT_Wr = nullptr;
1139-
HANDLE hChildStd_ERR_Rd = nullptr;
1140-
HANDLE hChildStd_ERR_Wr = nullptr;
1141-
1142-
// Create a pipe for the child process's STDOUT.
1143-
if (!CreatePipe(&hChildStd_OUT_Rd, &hChildStd_OUT_Wr, &saAttr, 0))
1144-
return false;
1145-
1146-
// Ensure the read handle to the pipe for STDOUT is not inherited.
1147-
SetHandleInformation(hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0);
1148-
1149-
// Create a pipe for the child process's STDERR.
1150-
if (!CreatePipe(&hChildStd_ERR_Rd, &hChildStd_ERR_Wr, &saAttr, 0))
1151-
return false;
1152-
1153-
// Ensure the read handle to the pipe for STDERR is not inherited.
1154-
SetHandleInformation(hChildStd_ERR_Rd, HANDLE_FLAG_INHERIT, 0);
1155-
1156-
STARTUPINFO siStartInfo;
1157-
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
1158-
siStartInfo.cb = sizeof(STARTUPINFO);
1159-
siStartInfo.hStdError = hChildStd_ERR_Wr;
1160-
siStartInfo.hStdOutput = hChildStd_OUT_Wr;
1161-
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
1162-
1163-
PROCESS_INFORMATION piProcInfo;
1164-
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
1165-
1166-
// Start the child process.
1167-
if (!CreateProcessA(nullptr,
1168-
(LPSTR)cmd.c_str(), // command line
1169-
nullptr, // process security attributes
1170-
nullptr, // primary thread security attributes
1171-
TRUE, // handles are inherited
1172-
0, // creation flags
1173-
nullptr, // use parent's environment
1174-
nullptr, // use parent's current directory
1175-
&siStartInfo, // STARTUPINFO pointer
1176-
&piProcInfo)) // receives PROCESS_INFORMATION
1177-
{
1178-
return false;
1179-
}
1180-
1181-
// Close handles to the stdin and stdout pipes no longer needed by the child process.
1182-
// If they are not explicitly closed, there is no way to recognize that the child process has completed.
1183-
CloseHandle(hChildStd_OUT_Wr);
1184-
CloseHandle(hChildStd_ERR_Wr);
1185-
1186-
// Read output from the child process's pipe for STDOUT
1187-
// and print to the parent process's STDOUT.
1188-
DWORD dwRead;
1189-
CHAR chBuf[4096];
1190-
bool bSuccess = FALSE;
1191-
1192-
for (;;) {
1193-
bSuccess = ReadFile(hChildStd_OUT_Rd, chBuf, 4096, &dwRead, nullptr);
1194-
if (!bSuccess || dwRead == 0) break;
1195-
1196-
String outputStr(chBuf, dwRead);
1197-
out += outputStr;
1198-
}
1199-
1200-
// Read from STDERR
1201-
for (;;) {
1202-
bSuccess = ReadFile(hChildStd_ERR_Rd, chBuf, 4096, &dwRead, nullptr);
1203-
if (!bSuccess || dwRead == 0) break;
1204-
1205-
String errorStr(chBuf, dwRead);
1206-
err += errorStr;
1207-
}
1208-
1209-
// Wait until child process exits or timeout
1210-
DWORD waitResult = WaitForSingleObject(piProcInfo.hProcess, timeout);
1211-
if (waitResult == WAIT_TIMEOUT) {
1212-
// If the process is still running after the timeout, terminate it
1213-
TerminateProcess(piProcInfo.hProcess, 1); // Use 1 or another number to indicate forced termination
1214-
1215-
err += "Timed out";
1216-
returnCode = 124 << 8; // (124 is status code used by `timeout` command)
1217-
}
1218-
1219-
// Regardless of the outcome, try to get the exit code
1220-
if (!GetExitCodeProcess(piProcInfo.hProcess, &returnCode)) {
1221-
returnCode = (DWORD)-1; // Use -1 or another value to indicate that getting the exit code failed
1222-
}
1223-
1224-
// Close handles to the child process and its primary thread.
1225-
CloseHandle(piProcInfo.hProcess);
1226-
CloseHandle(piProcInfo.hThread);
1227-
1228-
// Close the remaining pipe handles.
1229-
CloseHandle(hChildStd_OUT_Rd);
1230-
CloseHandle(hChildStd_ERR_Rd);
1231-
1232-
return true;
1233-
}
1234-
1235-
static IntrinsicResult intrinsic_exec(Context* context, IntrinsicResult partialResult) {
1236-
String cmd = "cmd /k " + context->GetVar("cmd").ToString();
1237-
String out;
1238-
String err;
1239-
DWORD returnCode;
1240-
1241-
double timeoutSecs = context->GetVar("timeout").DoubleValue();
1242-
double timeoutMs = (timeoutSecs == 0) ? INFINITE : (timeoutSecs * 1000);
1243-
1244-
if (!CreateChildProcess(cmd, out, err, returnCode, timeoutMs)) {
1245-
Error("Failed to create child process.");
1246-
}
1247-
1248-
// Build our result map.
1249-
ValueDict result;
1250-
result.SetValue("output", Value(out));
1251-
result.SetValue("errors", Value(err));
1252-
result.SetValue("status", Value(returnCode));
1253-
return IntrinsicResult(result);
1254-
}
1255-
#else
1256-
1257-
12581129
static IntrinsicResult intrinsic_exec(Context *context, IntrinsicResult partialResult) {
12591130
double now = context->vm->RunTime();
12601131
if (partialResult.Done()) {
@@ -1286,7 +1157,6 @@ static IntrinsicResult intrinsic_exec(Context *context, IntrinsicResult partialR
12861157
return IntrinsicResult(data, false);
12871158
}
12881159
}
1289-
#endif
12901160

12911161
static bool disallowAssignment(ValueDict& dict, Value key, Value value) {
12921162
return true;

0 commit comments

Comments
 (0)