win-nice 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cx.ps1 ADDED
@@ -0,0 +1,185 @@
1
+ # SPDX-License-Identifier: MIT OR Apache-2.0
2
+ # win-nice: managed-file
3
+ # No param(): $args sidesteps PowerShell's parameter binder entirely - see
4
+ # cap.ps1 for why that matters (codex's own flags shouldn't get bound here).
5
+ $Command = @('codex', '--dangerously-bypass-approvals-and-sandbox') + @($args)
6
+
7
+ # Fallback command line for when codex isn't a directly-launchable .exe (it's
8
+ # typically an npm-installed .cmd shim on Windows) - see CxLauncher.Run below and
9
+ # cap.ps1 for the same logic and its documented "%" limitation. cx.bat has its own,
10
+ # more severe "%" caveat (see there) that applies before this script ever runs.
11
+ $commandLine = ($Command | ForEach-Object {
12
+ $escaped = $_ -replace '"', '\"'
13
+ if ($escaped -eq '' -or $escaped -match '[\s"&|<>^]') { '"' + $escaped + '"' } else { $escaped }
14
+ }) -join ' '
15
+
16
+ $source = @"
17
+ using System;
18
+ using System.Runtime.InteropServices;
19
+ using System.Text;
20
+
21
+ public static class CxLauncher
22
+ {
23
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
24
+ struct STARTUPINFO
25
+ {
26
+ public int cb;
27
+ public string lpReserved;
28
+ public string lpDesktop;
29
+ public string lpTitle;
30
+ public int dwX;
31
+ public int dwY;
32
+ public int dwXSize;
33
+ public int dwYSize;
34
+ public int dwXCountChars;
35
+ public int dwYCountChars;
36
+ public int dwFillAttribute;
37
+ public int dwFlags;
38
+ public short wShowWindow;
39
+ public short cbReserved2;
40
+ public IntPtr lpReserved2;
41
+ public IntPtr hStdInput;
42
+ public IntPtr hStdOutput;
43
+ public IntPtr hStdError;
44
+ }
45
+
46
+ [StructLayout(LayoutKind.Sequential)]
47
+ struct PROCESS_INFORMATION
48
+ {
49
+ public IntPtr hProcess;
50
+ public IntPtr hThread;
51
+ public int dwProcessId;
52
+ public int dwThreadId;
53
+ }
54
+
55
+ [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
56
+ static extern bool CreateProcess(string lpApplicationName, StringBuilder lpCommandLine,
57
+ IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,
58
+ uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
59
+ ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
60
+
61
+ [DllImport("kernel32.dll", SetLastError = true)]
62
+ static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
63
+
64
+ [DllImport("kernel32.dll", SetLastError = true)]
65
+ static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
66
+
67
+ [DllImport("kernel32.dll")]
68
+ static extern bool CloseHandle(IntPtr hObject);
69
+
70
+ // Standard MSVCRT/CommandLineToArgvW quoting: safe for a directly-launched .exe's
71
+ // own argv parsing. No cmd.exe involved on this path, so none of its operator or
72
+ // "%" expansion semantics apply - this is the safe path, used whenever possible.
73
+ static string ArgvQuote(string arg)
74
+ {
75
+ if (arg.Length > 0 && arg.IndexOfAny(new char[] { ' ', '\t', '\n', '\v', '"' }) < 0)
76
+ return arg;
77
+
78
+ var result = new StringBuilder();
79
+ result.Append('"');
80
+ int backslashes = 0;
81
+ foreach (char c in arg)
82
+ {
83
+ if (c == '\\')
84
+ {
85
+ backslashes++;
86
+ }
87
+ else if (c == '"')
88
+ {
89
+ result.Append('\\', backslashes * 2 + 1);
90
+ result.Append('"');
91
+ backslashes = 0;
92
+ }
93
+ else
94
+ {
95
+ if (backslashes > 0) { result.Append('\\', backslashes); backslashes = 0; }
96
+ result.Append(c);
97
+ }
98
+ }
99
+ if (backslashes > 0) result.Append('\\', backslashes * 2);
100
+ result.Append('"');
101
+ return result.ToString();
102
+ }
103
+
104
+ static string BuildArgvCommandLine(string[] argv)
105
+ {
106
+ var parts = new string[argv.Length];
107
+ for (int i = 0; i < argv.Length; i++) parts[i] = ArgvQuote(argv[i]);
108
+ return string.Join(" ", parts);
109
+ }
110
+
111
+ public static int Run(string[] argv, string cmdExeCommandLine)
112
+ {
113
+ var si = new STARTUPINFO();
114
+ si.cb = Marshal.SizeOf(si);
115
+ PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
116
+
117
+ // See cap.ps1 for why .bat/.cmd targets skip the direct attempt entirely:
118
+ // CreateProcess silently re-invokes them through cmd.exe on its own, using
119
+ // unescaped text, instead of failing the way a genuinely missing exe would.
120
+ // A bare name like "codex" (typically an npm .cmd shim on Windows) isn't
121
+ // caught by that check, but CreateProcess only ever auto-appends ".exe" to
122
+ // it, so it fails cleanly here and falls through to the escaped path below.
123
+ bool isBatOrCmd = argv.Length > 0 && (
124
+ argv[0].EndsWith(".bat", StringComparison.OrdinalIgnoreCase) ||
125
+ argv[0].EndsWith(".cmd", StringComparison.OrdinalIgnoreCase));
126
+
127
+ bool created = false;
128
+ if (!isBatOrCmd)
129
+ {
130
+ var directCommandLine = new StringBuilder(BuildArgvCommandLine(argv));
131
+ created = CreateProcess(null, directCommandLine, IntPtr.Zero, IntPtr.Zero, true,
132
+ 0, IntPtr.Zero, null, ref si, out pi);
133
+ }
134
+
135
+ if (!created)
136
+ {
137
+ // Falling back to cmd.exe /c: a literal "%" in any argument could now
138
+ // trigger environment-variable expansion (cmd.exe pairs up "%" characters
139
+ // across the whole command line, even across separate arguments) and
140
+ // change what actually runs. Fail loudly here instead of silently risking
141
+ // that - there's no reliable per-character escape for "%" at this level.
142
+ foreach (var a in argv)
143
+ {
144
+ if (a.IndexOf('%') >= 0)
145
+ throw new InvalidOperationException(
146
+ "Refusing to run: argument contains '%' and the target needs the cmd.exe " +
147
+ "fallback (not a directly-launchable .exe), where '%' can trigger unintended " +
148
+ "environment-variable expansion. See README's Argument handling section.");
149
+ }
150
+
151
+ string cmdExe = Environment.SystemDirectory + "\\cmd.exe";
152
+ // /d: skip HKCU AutoRun (user-writable registry key). /v:off: disable delayed
153
+ // expansion so "!var!" in an argument can't be expanded. /s plus the extra outer
154
+ // quote pair: cmd's /S rule strips exactly that outer pair and leaves the rest of
155
+ // the string untouched - without /S, cmd strips the first and last quote of the
156
+ // whole line instead, which breaks quoting whenever the target path itself needs
157
+ // quotes AND another argument is also quoted.
158
+ var shellCommandLine = new StringBuilder("\"" + cmdExe + "\" /d /v:off /s /c \"" + cmdExeCommandLine + "\"");
159
+ created = CreateProcess(null, shellCommandLine, IntPtr.Zero, IntPtr.Zero, true,
160
+ 0, IntPtr.Zero, null, ref si, out pi);
161
+ if (!created)
162
+ throw new InvalidOperationException("CreateProcess failed: " + Marshal.GetLastWin32Error());
163
+ }
164
+
165
+ WaitForSingleObject(pi.hProcess, 0xFFFFFFFF);
166
+
167
+ uint exitCode;
168
+ GetExitCodeProcess(pi.hProcess, out exitCode);
169
+
170
+ CloseHandle(pi.hThread);
171
+ CloseHandle(pi.hProcess);
172
+
173
+ return (int)exitCode;
174
+ }
175
+ }
176
+ "@
177
+
178
+ Add-Type -TypeDefinition $source -Language CSharp
179
+
180
+ try {
181
+ exit ([CxLauncher]::Run([string[]]$Command, $commandLine))
182
+ } catch {
183
+ Write-Error $_.Exception.InnerException.Message
184
+ exit 1
185
+ }
package/bin/cy ADDED
@@ -0,0 +1,11 @@
1
+ #!/bin/sh
2
+ # SPDX-License-Identifier: MIT OR Apache-2.0
3
+ # win-nice: managed-file
4
+ # Git Bash ignores PATHEXT for bare-name resolution; this shim covers that shell.
5
+ # MSYS2_ARG_CONV_EXCL='*' stops Git Bash/MSYS from rewriting user arguments
6
+ # (e.g. /c, /d, C:\...) into Windows paths before the exec; the shim's own
7
+ # .ps1 path is converted explicitly with cygpath -w so -File gets a Windows path.
8
+ MSYS2_ARG_CONV_EXCL='*'
9
+ export MSYS2_ARG_CONV_EXCL
10
+ script=$(cygpath -w "$(dirname "$0")/cy.ps1" 2>/dev/null) || script="$(dirname "$0")/cy.ps1"
11
+ exec powershell -NoProfile -ExecutionPolicy Bypass -File "$script" "$@"
package/bin/cy.bat ADDED
@@ -0,0 +1,8 @@
1
+ @echo off
2
+ :: SPDX-License-Identifier: MIT OR Apache-2.0
3
+ :: win-nice: managed-file
4
+ :: A literal "%" in any argument gets corrupted here - see cap.bat for why (a
5
+ :: cmd.exe batch-parameter quirk, not fixable from inside a .bat). Every other
6
+ :: cmd.exe metacharacter (&|<>^) survives this hop untouched. Invoking "cy" bare
7
+ :: from an actual PowerShell session skips this file (cy.ps1 preferred).
8
+ claude --dangerously-skip-permissions %*
package/bin/cy.ps1 ADDED
@@ -0,0 +1,185 @@
1
+ # SPDX-License-Identifier: MIT OR Apache-2.0
2
+ # win-nice: managed-file
3
+ # No param(): $args sidesteps PowerShell's parameter binder entirely - see
4
+ # cap.ps1 for why that matters (claude's own flags shouldn't get bound here).
5
+ $Command = @('claude', '--dangerously-skip-permissions') + @($args)
6
+
7
+ # Fallback command line for when claude isn't a directly-launchable .exe (it's
8
+ # typically an npm-installed .cmd shim on Windows) - see CyLauncher.Run below and
9
+ # cap.ps1 for the same logic and its documented "%" limitation. cy.bat has its own,
10
+ # more severe "%" caveat (see there) that applies before this script ever runs.
11
+ $commandLine = ($Command | ForEach-Object {
12
+ $escaped = $_ -replace '"', '\"'
13
+ if ($escaped -eq '' -or $escaped -match '[\s"&|<>^]') { '"' + $escaped + '"' } else { $escaped }
14
+ }) -join ' '
15
+
16
+ $source = @"
17
+ using System;
18
+ using System.Runtime.InteropServices;
19
+ using System.Text;
20
+
21
+ public static class CyLauncher
22
+ {
23
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
24
+ struct STARTUPINFO
25
+ {
26
+ public int cb;
27
+ public string lpReserved;
28
+ public string lpDesktop;
29
+ public string lpTitle;
30
+ public int dwX;
31
+ public int dwY;
32
+ public int dwXSize;
33
+ public int dwYSize;
34
+ public int dwXCountChars;
35
+ public int dwYCountChars;
36
+ public int dwFillAttribute;
37
+ public int dwFlags;
38
+ public short wShowWindow;
39
+ public short cbReserved2;
40
+ public IntPtr lpReserved2;
41
+ public IntPtr hStdInput;
42
+ public IntPtr hStdOutput;
43
+ public IntPtr hStdError;
44
+ }
45
+
46
+ [StructLayout(LayoutKind.Sequential)]
47
+ struct PROCESS_INFORMATION
48
+ {
49
+ public IntPtr hProcess;
50
+ public IntPtr hThread;
51
+ public int dwProcessId;
52
+ public int dwThreadId;
53
+ }
54
+
55
+ [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
56
+ static extern bool CreateProcess(string lpApplicationName, StringBuilder lpCommandLine,
57
+ IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,
58
+ uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
59
+ ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
60
+
61
+ [DllImport("kernel32.dll", SetLastError = true)]
62
+ static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
63
+
64
+ [DllImport("kernel32.dll", SetLastError = true)]
65
+ static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
66
+
67
+ [DllImport("kernel32.dll")]
68
+ static extern bool CloseHandle(IntPtr hObject);
69
+
70
+ // Standard MSVCRT/CommandLineToArgvW quoting: safe for a directly-launched .exe's
71
+ // own argv parsing. No cmd.exe involved on this path, so none of its operator or
72
+ // "%" expansion semantics apply - this is the safe path, used whenever possible.
73
+ static string ArgvQuote(string arg)
74
+ {
75
+ if (arg.Length > 0 && arg.IndexOfAny(new char[] { ' ', '\t', '\n', '\v', '"' }) < 0)
76
+ return arg;
77
+
78
+ var result = new StringBuilder();
79
+ result.Append('"');
80
+ int backslashes = 0;
81
+ foreach (char c in arg)
82
+ {
83
+ if (c == '\\')
84
+ {
85
+ backslashes++;
86
+ }
87
+ else if (c == '"')
88
+ {
89
+ result.Append('\\', backslashes * 2 + 1);
90
+ result.Append('"');
91
+ backslashes = 0;
92
+ }
93
+ else
94
+ {
95
+ if (backslashes > 0) { result.Append('\\', backslashes); backslashes = 0; }
96
+ result.Append(c);
97
+ }
98
+ }
99
+ if (backslashes > 0) result.Append('\\', backslashes * 2);
100
+ result.Append('"');
101
+ return result.ToString();
102
+ }
103
+
104
+ static string BuildArgvCommandLine(string[] argv)
105
+ {
106
+ var parts = new string[argv.Length];
107
+ for (int i = 0; i < argv.Length; i++) parts[i] = ArgvQuote(argv[i]);
108
+ return string.Join(" ", parts);
109
+ }
110
+
111
+ public static int Run(string[] argv, string cmdExeCommandLine)
112
+ {
113
+ var si = new STARTUPINFO();
114
+ si.cb = Marshal.SizeOf(si);
115
+ PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
116
+
117
+ // See cap.ps1 for why .bat/.cmd targets skip the direct attempt entirely:
118
+ // CreateProcess silently re-invokes them through cmd.exe on its own, using
119
+ // unescaped text, instead of failing the way a genuinely missing exe would.
120
+ // A bare name like "claude" (typically an npm .cmd shim on Windows) isn't
121
+ // caught by that check, but CreateProcess only ever auto-appends ".exe" to
122
+ // it, so it fails cleanly here and falls through to the escaped path below.
123
+ bool isBatOrCmd = argv.Length > 0 && (
124
+ argv[0].EndsWith(".bat", StringComparison.OrdinalIgnoreCase) ||
125
+ argv[0].EndsWith(".cmd", StringComparison.OrdinalIgnoreCase));
126
+
127
+ bool created = false;
128
+ if (!isBatOrCmd)
129
+ {
130
+ var directCommandLine = new StringBuilder(BuildArgvCommandLine(argv));
131
+ created = CreateProcess(null, directCommandLine, IntPtr.Zero, IntPtr.Zero, true,
132
+ 0, IntPtr.Zero, null, ref si, out pi);
133
+ }
134
+
135
+ if (!created)
136
+ {
137
+ // Falling back to cmd.exe /c: a literal "%" in any argument could now
138
+ // trigger environment-variable expansion (cmd.exe pairs up "%" characters
139
+ // across the whole command line, even across separate arguments) and
140
+ // change what actually runs. Fail loudly here instead of silently risking
141
+ // that - there's no reliable per-character escape for "%" at this level.
142
+ foreach (var a in argv)
143
+ {
144
+ if (a.IndexOf('%') >= 0)
145
+ throw new InvalidOperationException(
146
+ "Refusing to run: argument contains '%' and the target needs the cmd.exe " +
147
+ "fallback (not a directly-launchable .exe), where '%' can trigger unintended " +
148
+ "environment-variable expansion. See README's Argument handling section.");
149
+ }
150
+
151
+ string cmdExe = Environment.SystemDirectory + "\\cmd.exe";
152
+ // /d: skip HKCU AutoRun (user-writable registry key). /v:off: disable delayed
153
+ // expansion so "!var!" in an argument can't be expanded. /s plus the extra outer
154
+ // quote pair: cmd's /S rule strips exactly that outer pair and leaves the rest of
155
+ // the string untouched - without /S, cmd strips the first and last quote of the
156
+ // whole line instead, which breaks quoting whenever the target path itself needs
157
+ // quotes AND another argument is also quoted.
158
+ var shellCommandLine = new StringBuilder("\"" + cmdExe + "\" /d /v:off /s /c \"" + cmdExeCommandLine + "\"");
159
+ created = CreateProcess(null, shellCommandLine, IntPtr.Zero, IntPtr.Zero, true,
160
+ 0, IntPtr.Zero, null, ref si, out pi);
161
+ if (!created)
162
+ throw new InvalidOperationException("CreateProcess failed: " + Marshal.GetLastWin32Error());
163
+ }
164
+
165
+ WaitForSingleObject(pi.hProcess, 0xFFFFFFFF);
166
+
167
+ uint exitCode;
168
+ GetExitCodeProcess(pi.hProcess, out exitCode);
169
+
170
+ CloseHandle(pi.hThread);
171
+ CloseHandle(pi.hProcess);
172
+
173
+ return (int)exitCode;
174
+ }
175
+ }
176
+ "@
177
+
178
+ Add-Type -TypeDefinition $source -Language CSharp
179
+
180
+ try {
181
+ exit ([CyLauncher]::Run([string[]]$Command, $commandLine))
182
+ } catch {
183
+ Write-Error $_.Exception.InnerException.Message
184
+ exit 1
185
+ }
package/bin/high ADDED
@@ -0,0 +1,11 @@
1
+ #!/bin/sh
2
+ # SPDX-License-Identifier: MIT OR Apache-2.0
3
+ # win-nice: managed-file
4
+ # Git Bash ignores PATHEXT for bare-name resolution; this shim covers that shell.
5
+ # MSYS2_ARG_CONV_EXCL='*' stops Git Bash/MSYS from rewriting user arguments
6
+ # (e.g. /c, /d, C:\...) into Windows paths before the exec; the shim's own
7
+ # .ps1 path is converted explicitly with cygpath -w so -File gets a Windows path.
8
+ MSYS2_ARG_CONV_EXCL='*'
9
+ export MSYS2_ARG_CONV_EXCL
10
+ script=$(cygpath -w "$(dirname "$0")/high.ps1" 2>/dev/null) || script="$(dirname "$0")/high.ps1"
11
+ exec powershell -NoProfile -ExecutionPolicy Bypass -File "$script" "$@"
package/bin/high.bat ADDED
@@ -0,0 +1,12 @@
1
+ @echo off
2
+ :: SPDX-License-Identifier: MIT OR Apache-2.0
3
+ :: win-nice: managed-file
4
+ if "%~1"=="" (
5
+ echo usage: high ^<command^> [args...] 1>&2
6
+ exit /b 1
7
+ )
8
+ :: A literal "%" in any argument gets corrupted here - see cap.bat for why (a
9
+ :: cmd.exe batch-parameter quirk, not fixable from inside a .bat). Every other
10
+ :: cmd.exe metacharacter (&|<>^) survives this hop untouched. Invoking "high"
11
+ :: bare from an actual PowerShell session skips this file (high.ps1 preferred).
12
+ powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0high.ps1" %*
package/bin/high.ps1 ADDED
@@ -0,0 +1,193 @@
1
+ # SPDX-License-Identifier: MIT OR Apache-2.0
2
+ # win-nice: managed-file
3
+ # No param(): nothing here needs a named parameter, and $args sidesteps
4
+ # PowerShell's parameter binder entirely - see cap.ps1 for why that matters.
5
+ $Command = $args
6
+
7
+ if (-not $Command -or $Command.Count -eq 0) {
8
+ Write-Error "usage: high <command> [args...]"
9
+ exit 1
10
+ }
11
+
12
+ # Fallback command line for when the target isn't a directly-launchable .exe (see
13
+ # HighLauncher.Run below) - re-parsed by cmd.exe (via "cmd.exe /c"), so quoting must
14
+ # neutralize its operators (&|<>^) and not just whitespace - see cap.ps1 for the
15
+ # same logic and its documented "%" limitation. high.bat has its own, more severe
16
+ # "%" caveat (see there) that applies before this script ever runs.
17
+ $commandLine = ($Command | ForEach-Object {
18
+ $escaped = $_ -replace '"', '\"'
19
+ if ($escaped -eq '' -or $escaped -match '[\s"&|<>^]') { '"' + $escaped + '"' } else { $escaped }
20
+ }) -join ' '
21
+
22
+ $source = @"
23
+ using System;
24
+ using System.Runtime.InteropServices;
25
+ using System.Text;
26
+
27
+ public static class HighLauncher
28
+ {
29
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
30
+ struct STARTUPINFO
31
+ {
32
+ public int cb;
33
+ public string lpReserved;
34
+ public string lpDesktop;
35
+ public string lpTitle;
36
+ public int dwX;
37
+ public int dwY;
38
+ public int dwXSize;
39
+ public int dwYSize;
40
+ public int dwXCountChars;
41
+ public int dwYCountChars;
42
+ public int dwFillAttribute;
43
+ public int dwFlags;
44
+ public short wShowWindow;
45
+ public short cbReserved2;
46
+ public IntPtr lpReserved2;
47
+ public IntPtr hStdInput;
48
+ public IntPtr hStdOutput;
49
+ public IntPtr hStdError;
50
+ }
51
+
52
+ [StructLayout(LayoutKind.Sequential)]
53
+ struct PROCESS_INFORMATION
54
+ {
55
+ public IntPtr hProcess;
56
+ public IntPtr hThread;
57
+ public int dwProcessId;
58
+ public int dwThreadId;
59
+ }
60
+
61
+ [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
62
+ static extern bool CreateProcess(string lpApplicationName, StringBuilder lpCommandLine,
63
+ IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles,
64
+ uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory,
65
+ ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
66
+
67
+ [DllImport("kernel32.dll", SetLastError = true)]
68
+ static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
69
+
70
+ [DllImport("kernel32.dll", SetLastError = true)]
71
+ static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
72
+
73
+ [DllImport("kernel32.dll")]
74
+ static extern bool CloseHandle(IntPtr hObject);
75
+
76
+ // Standard MSVCRT/CommandLineToArgvW quoting: safe for a directly-launched .exe's
77
+ // own argv parsing. No cmd.exe involved on this path, so none of its operator or
78
+ // "%" expansion semantics apply - this is the safe path, used whenever possible.
79
+ static string ArgvQuote(string arg)
80
+ {
81
+ if (arg.Length > 0 && arg.IndexOfAny(new char[] { ' ', '\t', '\n', '\v', '"' }) < 0)
82
+ return arg;
83
+
84
+ var result = new StringBuilder();
85
+ result.Append('"');
86
+ int backslashes = 0;
87
+ foreach (char c in arg)
88
+ {
89
+ if (c == '\\')
90
+ {
91
+ backslashes++;
92
+ }
93
+ else if (c == '"')
94
+ {
95
+ result.Append('\\', backslashes * 2 + 1);
96
+ result.Append('"');
97
+ backslashes = 0;
98
+ }
99
+ else
100
+ {
101
+ if (backslashes > 0) { result.Append('\\', backslashes); backslashes = 0; }
102
+ result.Append(c);
103
+ }
104
+ }
105
+ if (backslashes > 0) result.Append('\\', backslashes * 2);
106
+ result.Append('"');
107
+ return result.ToString();
108
+ }
109
+
110
+ static string BuildArgvCommandLine(string[] argv)
111
+ {
112
+ var parts = new string[argv.Length];
113
+ for (int i = 0; i < argv.Length; i++) parts[i] = ArgvQuote(argv[i]);
114
+ return string.Join(" ", parts);
115
+ }
116
+
117
+ // Priority is passed via dwCreationFlags, applied atomically at creation - no
118
+ // Job Object needed. Windows' CreateProcess inherits IDLE/BELOW_NORMAL priority
119
+ // by default to children that don't request a priority of their own; HIGH and
120
+ // above are NOT inherited by default (see README).
121
+ public static int Run(uint priorityClass, string[] argv, string cmdExeCommandLine)
122
+ {
123
+ var si = new STARTUPINFO();
124
+ si.cb = Marshal.SizeOf(si);
125
+ PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
126
+
127
+ // See cap.ps1 for why .bat/.cmd targets skip the direct attempt entirely:
128
+ // CreateProcess silently re-invokes them through cmd.exe on its own, using
129
+ // unescaped text, instead of failing the way a genuinely missing exe would.
130
+ bool isBatOrCmd = argv.Length > 0 && (
131
+ argv[0].EndsWith(".bat", StringComparison.OrdinalIgnoreCase) ||
132
+ argv[0].EndsWith(".cmd", StringComparison.OrdinalIgnoreCase));
133
+
134
+ bool created = false;
135
+ if (!isBatOrCmd)
136
+ {
137
+ var directCommandLine = new StringBuilder(BuildArgvCommandLine(argv));
138
+ created = CreateProcess(null, directCommandLine, IntPtr.Zero, IntPtr.Zero, true,
139
+ priorityClass, IntPtr.Zero, null, ref si, out pi);
140
+ }
141
+
142
+ if (!created)
143
+ {
144
+ // Falling back to cmd.exe /c: a literal "%" in any argument could now
145
+ // trigger environment-variable expansion (cmd.exe pairs up "%" characters
146
+ // across the whole command line, even across separate arguments) and
147
+ // change what actually runs. Fail loudly here instead of silently risking
148
+ // that - there's no reliable per-character escape for "%" at this level.
149
+ foreach (var a in argv)
150
+ {
151
+ if (a.IndexOf('%') >= 0)
152
+ throw new InvalidOperationException(
153
+ "Refusing to run: argument contains '%' and the target needs the cmd.exe " +
154
+ "fallback (not a directly-launchable .exe), where '%' can trigger unintended " +
155
+ "environment-variable expansion. See README's Argument handling section.");
156
+ }
157
+
158
+ string cmdExe = Environment.SystemDirectory + "\\cmd.exe";
159
+ // /d: skip HKCU AutoRun (user-writable registry key). /v:off: disable delayed
160
+ // expansion so "!var!" in an argument can't be expanded. /s plus the extra outer
161
+ // quote pair: cmd's /S rule strips exactly that outer pair and leaves the rest of
162
+ // the string untouched - without /S, cmd strips the first and last quote of the
163
+ // whole line instead, which breaks quoting whenever the target path itself needs
164
+ // quotes AND another argument is also quoted.
165
+ var shellCommandLine = new StringBuilder("\"" + cmdExe + "\" /d /v:off /s /c \"" + cmdExeCommandLine + "\"");
166
+ created = CreateProcess(null, shellCommandLine, IntPtr.Zero, IntPtr.Zero, true,
167
+ priorityClass, IntPtr.Zero, null, ref si, out pi);
168
+ if (!created)
169
+ throw new InvalidOperationException("CreateProcess failed: " + Marshal.GetLastWin32Error());
170
+ }
171
+
172
+ WaitForSingleObject(pi.hProcess, 0xFFFFFFFF);
173
+
174
+ uint exitCode;
175
+ GetExitCodeProcess(pi.hProcess, out exitCode);
176
+
177
+ CloseHandle(pi.hThread);
178
+ CloseHandle(pi.hProcess);
179
+
180
+ return (int)exitCode;
181
+ }
182
+ }
183
+ "@
184
+
185
+ Add-Type -TypeDefinition $source -Language CSharp
186
+
187
+ $HIGH_PRIORITY_CLASS = 0x00000080
188
+ try {
189
+ exit ([HighLauncher]::Run($HIGH_PRIORITY_CLASS, [string[]]$Command, $commandLine))
190
+ } catch {
191
+ Write-Error $_.Exception.InnerException.Message
192
+ exit 1
193
+ }
package/bin/idle ADDED
@@ -0,0 +1,11 @@
1
+ #!/bin/sh
2
+ # SPDX-License-Identifier: MIT OR Apache-2.0
3
+ # win-nice: managed-file
4
+ # Git Bash ignores PATHEXT for bare-name resolution; this shim covers that shell.
5
+ # MSYS2_ARG_CONV_EXCL='*' stops Git Bash/MSYS from rewriting user arguments
6
+ # (e.g. /c, /d, C:\...) into Windows paths before the exec; the shim's own
7
+ # .ps1 path is converted explicitly with cygpath -w so -File gets a Windows path.
8
+ MSYS2_ARG_CONV_EXCL='*'
9
+ export MSYS2_ARG_CONV_EXCL
10
+ script=$(cygpath -w "$(dirname "$0")/idle.ps1" 2>/dev/null) || script="$(dirname "$0")/idle.ps1"
11
+ exec powershell -NoProfile -ExecutionPolicy Bypass -File "$script" "$@"
package/bin/idle.bat ADDED
@@ -0,0 +1,12 @@
1
+ @echo off
2
+ :: SPDX-License-Identifier: MIT OR Apache-2.0
3
+ :: win-nice: managed-file
4
+ if "%~1"=="" (
5
+ echo usage: idle ^<command^> [args...] 1>&2
6
+ exit /b 1
7
+ )
8
+ :: A literal "%" in any argument gets corrupted here - see cap.bat for why (a
9
+ :: cmd.exe batch-parameter quirk, not fixable from inside a .bat). Every other
10
+ :: cmd.exe metacharacter (&|<>^) survives this hop untouched. Invoking "idle"
11
+ :: bare from an actual PowerShell session skips this file (idle.ps1 preferred).
12
+ powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0idle.ps1" %*