private Dictionary<string, Host> parse(Stream stream)
{
Dictionary<string, Host> m = new Dictionary<string, Host>();
StreamReader sr = new StreamReader(stream);
List<Host> current = new List<Host>(4);
string line;
while ((line = sr.ReadLine()) != null)
{
line = line.Trim();
if (line.Length == 0 || line.StartsWith("#"))
continue;
string keyword = string.Empty, argValue = string.Empty;
bool haveKeyword = false;
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if (!haveKeyword && (c == ' ' || c == '\t' || c == '='))
{
keyword = line.Slice(0, i);
haveKeyword = true;
continue;
}
if (haveKeyword && c != '=' && c != ' ' && c != '\t')
{
argValue = line.Substring(i);
break;
}
}
if ("Host".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
current.Clear();
foreach (string pattern in Regex.Split(argValue, "[ \t]"))
{
string name = dequote(pattern);
Host c = null;
if (m.ContainsKey(name))
{
c = m[name];
}
if (c == null)
{
c = new Host();
m.Add(name, c);
}
current.Add(c);
}
}
if (current.Count == 0)
{
continue;
}
if ("HostName".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
foreach (Host c in current)
if (c.hostName == null)
c.hostName = dequote(argValue);
}
else if ("User".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
foreach (Host c in current)
if (c.user == null)
c.user = dequote(argValue);
}
else if ("Port".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
try
{
int port = int.Parse(dequote(argValue));
foreach (Host c in current)
if (c.port == 0)
c.port = port;
}
catch (FormatException)
{
}
}
else if ("IdentityFile".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
foreach (Host c in current)
if (c.identityFile == null)
c.identityFile = toFile(dequote(argValue));
}
else if ("PreferredAuthentications".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
foreach (Host c in current)
if (c.preferredAuthentications == null)
c.preferredAuthentications = nows(dequote(argValue));
}
else if ("BatchMode".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
foreach (Host c in current)
if (c.batchMode == null)
c.batchMode = yesno(dequote(argValue));
}
else if ("StrictHostKeyChecking".Equals(keyword, StringComparison.CurrentCultureIgnoreCase))
{
string value = dequote(argValue);
foreach (Host c in current)
if (c.strictHostKeyChecking == null)
c.strictHostKeyChecking = value;
}
}
return m;
}