Skip to content

Commit 1ce1036

Browse files
authored
Merge pull request #151 from marcgurevitx/tidify-windows-exec
Delete obsolete WINDOWS section of exec from ShellIntrinsics
2 parents 4ac0ca5 + 53efb64 commit 1ce1036

File tree

2 files changed

+3
-135
lines changed

2 files changed

+3
-135
lines changed

MiniScript-cpp/src/ShellExec.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ String readFromFd(HANDLE fd, bool trimTrailingNewline=true) {
4141
buffer[bytesRead] = '\0';
4242
if (trimTrailingNewline and bytesRead < bufferSize-1 and bytesRead > 0 and buffer[bytesRead-1] == '\n') {
4343
// Efficiently trim \n or \r\n from the end of the buffer
44-
buffer[bytesRead-1] = '\0';
45-
if (bytesRead > 1 and buffer[bytesRead-2] == '\r') {
46-
buffer[bytesRead-2] = '\0';
47-
}
44+
bytesRead--;
45+
if (bytesRead > 0 and buffer[bytesRead-1] == '\r') bytesRead--;
4846
trimmed = true;
4947
}
5048

51-
String s(buffer, bytesRead+1);
49+
String s(buffer, bytesRead);
5250
output += s;
5351
}
5452

MiniScript-cpp/src/ShellIntrinsics.cpp

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

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

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

0 commit comments

Comments
 (0)