using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; class MiniLinkLauncher { static void Pump(Stream input, Stream output) { byte[] buffer = new byte[4096]; int count; while ((count = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, count); output.Flush(); } } static string Quote(string arg) { return "\"" + System.Text.RegularExpressions.Regex.Replace(arg, "(\\\\*)\"", "$1$1\\\"").TrimEnd('\\') + new string('\\', arg.Reverse().TakeWhile(c => c == '\\').Count() * 2) + "\""; } static int Main(string[] args) { try { string root = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string[] paths = File.ReadAllLines(Path.Combine(root, "launcher.paths")); bool linked = File.Exists(paths[1]); var info = new ProcessStartInfo(linked ? paths[0] : paths[2]); info.Arguments = String.Join(" ", (linked ? new[] { paths[1] }.Concat(args) : args).Select(Quote)); info.UseShellExecute = false; info.CreateNoWindow = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.RedirectStandardError = true; info.EnvironmentVariables["CODEX_HOME"] = paths[3]; info.EnvironmentVariables["MINILINK_CONFIG"] = paths[4]; if (!linked) info.EnvironmentVariables.Remove("CODEX_CLI_PATH"); using (var process = Process.Start(info)) { Task.Run(() => { try { Pump(Console.OpenStandardInput(), process.StandardInput.BaseStream); process.StandardInput.Close(); } catch {} }); var output = Task.Run(() => Pump(process.StandardOutput.BaseStream, Console.OpenStandardOutput())); var error = Task.Run(() => Pump(process.StandardError.BaseStream, Console.OpenStandardError())); process.WaitForExit(); Task.WaitAll(output, error); return process.ExitCode; } } catch (Exception error) { Console.Error.WriteLine("minilink launcher: " + error.Message); return 1; } } }