Added <executable-in-*> support for Windows
[zeroinstall/solver.git] / zeroinstall / injector / runenv.cli.cs
blob34ffb9407bdf9d8db3c3ae9f1e11e6e96cd8d698
1 /*
2 * Copyright 2006-2012 Bastian Eicher
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
23 using System;
24 using System.Collections.Generic;
25 using System.Diagnostics;
26 using System.IO;
27 using System.Text;
29 /// <summary>
30 /// This helper executable launches a command-line specified in specific environment variables.
31 /// <summary>
32 public class RunEnv
34 public static int Main(string[] args)
36 string envName = Path.GetFileName(System.Environment.GetCommandLineArgs()[0]);
37 string envFile = Environment.GetEnvironmentVariable("0install-runenv-file-" + envName);
38 string envArgs = Environment.GetEnvironmentVariable("0install-runenv-args-" + envName);
39 string userArgs = ConcatenateEscapeArgument(args);
41 ProcessStartInfo startInfo = new ProcessStartInfo(envFile, string.IsNullOrEmpty(userArgs) ? envArgs : envArgs + " " + userArgs);
42 startInfo.UseShellExecute = false;
43 Process process = Process.Start(startInfo);
44 process.WaitForExit();
45 return process.ExitCode;
48 #region StringUtils
49 /// <summary>
50 /// Escapes a string for use as a Windows command-line argument, making sure it is encapsulated within <code>"</code> if it contains whitespace characters.
51 /// </summary>
52 /// <remarks>
53 /// This coressponds to Windows' handling of command-line arguments as specified in:
54 /// http://msdn.microsoft.com/library/17w5ykft
55 /// </remarks>
56 private static string EscapeArgument(string value)
58 if (value == null) return null;
60 // Add leading quotation mark if there are whitespaces
61 bool containsWhitespace = ContainsWhitespace(value);
62 StringBuilder result = containsWhitespace ? new StringBuilder("\"", value.Length + 2) : new StringBuilder(value.Length);
64 // Split by quotation marks
65 string[] parts = value.Split('"');
66 for (int i = 0; i < parts.Length; i++)
68 // Count slashes preceeding each quotation mark
69 string slashesTrimmed = parts[i].TrimEnd('\\');
70 int slashesCount = parts[i].Length - slashesTrimmed.Length;
72 result.Append(parts[i]);
74 if (i < parts.Length - 1)
75 { // Not last part
76 for (int j = 0; j < slashesCount; j++) result.Append('\\'); // Double number of slashes
77 result.Append("\\\""); // Escaped quotation mark
79 else if (containsWhitespace)
80 { // Last part if there are whitespaces
81 for (int j = 0; j < slashesCount; j++) result.Append('\\'); // Double number of slashes
82 result.Append('"'); // Non-escaped quotation mark
86 return result.ToString();
89 /// <summary>
90 /// Combines multiple strings into one for use as a Windows command-line argument using <see cref="EscapeArgument"/>.
91 /// </summary>
92 /// <param name="parts">The strings to be combines.</param>
93 /// <remarks>
94 /// This coressponds to Windows' handling of command-line arguments as specified in:
95 /// http://msdn.microsoft.com/library/17w5ykft
96 /// </remarks>
97 private static string ConcatenateEscapeArgument(IEnumerable<string> parts)
99 if (parts == null) return null;
101 StringBuilder output = new StringBuilder();
102 bool first = true;
103 foreach (string part in parts)
105 // No separator before first or after last part
106 if (first) first = false;
107 else output.Append(' ');
109 output.Append(EscapeArgument(part));
112 return output.ToString();
115 /// <summary>
116 /// Checks whether a string contains any whitespace characters
117 /// </summary>
118 private static bool ContainsWhitespace(string text)
120 return text.Contains(" ") || text.Contains("\t") || text.Contains("\n") || text.Contains("\r");
122 #endregion