<commit_before>﻿// SSH Handler
//
// <NAME>
//
// ssh-handler.cs

/*  Copyright 2013 <NAME>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Win32;

public class SshHandler
{
    private enum Handler { Unspecified, Putty };
    private static Handler handler = Handler.Unspecified;
    private static string puttyPath = null;

    public static void Main(string[] args)
    {
        Application.EnableVisualStyles();

        try
        {
            Regex usage = new Regex(@"^(?:/|--?)(?:h|help|usage|\?)$", RegexOptions.IgnoreCase);
            Regex putty = new Regex(@"^(?:/|--?)putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
            Uri uri = null;

            foreach (string arg in args)
            {
                if (usage.IsMatch(arg))
                {
                    Usage();
                    return;
                }

                Match match;

                if ((match = putty.Match(arg)).Success)
                {
                    handler = Handler.Putty;
                    Group group = match.Groups["putty_path"];
                    if (group.Success)
                        puttyPath = group.Value;
                }
                else
                    uri = new Uri(arg, UriKind.Absolute);
            }

            if (uri != null)
                switch (handler)
                {
                    case Handler.Unspecified:
                        if (FindPutty())
                            Putty(uri);
                        else
                            throw new Exception("Could not find a suitable SSH application.");
                        break;
                    case Handler.Putty:
                        Putty(uri);
                        break;
                }
            else
                Usage();
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private static void Usage()
    {
        MessageBox.Show("ssh-handler [/putty[:<putty-path>]] <ssh-url>\n\n" +
            "/putty[:<putty-path>] -- Use PuTTY to connect", "SSH Handler Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private static void UserPassword(Uri uri, out string user, out string password)
    {
        if (uri.UserInfo.Length != 0)
        {
            string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
            user = userInfo[0];
            password = userInfo.Length == 2 ? userInfo[1] : <PASSWORD>;
        }
        else
        {
            user = null;
            password = <PASSWORD>;
        }
    }

    private static bool FindPutty()
    {
        if (puttyPath != null)
            return true;

        foreach (string key in new string[] { "HKEY_CURRENT_USER", "HKEY_LOCAL_MACHINE" })
        {
            string path = (string)Registry.GetValue(key + @"\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1", "InstallLocation", null);
            if (path == null)
                continue;
            puttyPath = Path.Combine(path, "putty.exe");
            if (File.Exists(puttyPath))
            {
                Debug.WriteLine("Found PuTTY in registry: {0}", puttyPath, null);
                return true;
            }
            else
                puttyPath = null;
        }

        foreach (string path in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
        {
            puttyPath = Path.Combine(path, "putty.exe");
            if (File.Exists(puttyPath))
            {
                Debug.WriteLine("Found PuTTY in path: {0}", puttyPath, null);
                return true;
            }<commit_msg>Find PuTTY in the registry even if we are running in 64bit mode!<commit_after>﻿// SSH Handler
//
// <NAME>
//
// ssh-handler.cs

/*  Copyright 2013 <NAME>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Win32;

public class SshHandler
{
    private enum Handler { Unspecified, Putty };
    private static Handler handler = Handler.Unspecified;
    private static string puttyPath = null;

    public static void Main(string[] args)
    {
        Application.EnableVisualStyles();

        try
        {
            Regex usage = new Regex(@"^(?:/|--?)(?:h|help|usage|\?)$", RegexOptions.IgnoreCase);
            Regex putty = new Regex(@"^(?:/|--?)putty(?:[:=](?<putty_path>.*))?$", RegexOptions.IgnoreCase);
            Uri uri = null;

            foreach (string arg in args)
            {
                if (usage.IsMatch(arg))
                {
                    Usage();
                    return;
                }

                Match match;

                if ((match = putty.Match(arg)).Success)
                {
                    handler = Handler.Putty;
                    Group group = match.Groups["putty_path"];
                    if (group.Success)
                        puttyPath = group.Value;
                }
                else
                    uri = new Uri(arg, UriKind.Absolute);
            }

            if (uri != null)
                switch (handler)
                {
                    case Handler.Unspecified:
                        if (FindPutty())
                            Putty(uri);
                        else
                            throw new Exception("Could not find a suitable SSH application.");
                        break;
                    case Handler.Putty:
                        Putty(uri);
                        break;
                }
            else
                Usage();
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message, "SSH Handler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private static void Usage()
    {
        MessageBox.Show("ssh-handler [/putty[:<putty-path>]] <ssh-url>\n\n" +
            "/putty[:<putty-path>] -- Use PuTTY to connect", "SSH Handler Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private static void UserPassword(Uri uri, out string user, out string password)
    {
        if (uri.UserInfo.Length != 0)
        {
            string[] userInfo = uri.UserInfo.Split(new char[] { ':' }, 2);
            user = userInfo[0];
            password = userInfo.Length == 2 ? userInfo[1] : <PASSWORD>;
        }
        else
        {
            user = null;
            password = <PASSWORD>;
        }
    }

    private static bool FindPutty()
    {
        if (puttyPath != null)
            return true;

        foreach (RegistryHive hive in new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine })
        {
            IList<RegistryView> views = new List<RegistryView>(new RegistryView[] { RegistryView.Registry32 });
            if (Environment.Is64BitOperatingSystem)
                views.Insert(0, RegistryView.Registry64);

            foreach (RegistryView view in views)
                using (RegistryKey baseKey = RegistryKey.OpenBaseKey(hive, view), key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PuTTY_is1"))
                    if (key != null)
                    {
                        string path = (string)key.GetValue("InstallLocation");
                        if (path == null)
                            continue;
                        puttyPath = Path.Combine(path, "putty.exe");
                        if (File.Exists(puttyPath))
                        {
                            Debug.WriteLine("Found PuTTY in registry: {0}", puttyPath, null);
                            return true;
                        }
                        else
                            puttyPath = null;
                    }
        }

        foreach (string path in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
        {
            puttyPath = Path.Combine(path, "putty.exe");
            if (File.Exists(puttyPath))
            {
                Debug.WriteLine("Found PuTTY in path: {0}", puttyPath, null);
                return true;
            }
{'fraction_non_alphanumeric': 0.10427835593398507, 'fraction_numerical': 0.004730368968779565, 'mean_word_length': 1.5303191489361703, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1177208', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2402, 'n_words': 662}