2017-10-14 13:42:11 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-10-15 15:25:37 -04:00
|
|
|
|
using System.Windows.Forms;
|
2017-10-14 13:42:11 -04:00
|
|
|
|
|
2017-10-15 15:25:37 -04:00
|
|
|
|
namespace ShiftOS.Main.Terminal
|
2017-10-14 13:42:11 -04:00
|
|
|
|
{
|
|
|
|
|
public static class TerminalBackend
|
|
|
|
|
{
|
|
|
|
|
// The line below gets all the terminal commands in... well... the entire ShiftOS.Engine
|
2017-11-18 11:09:54 -05:00
|
|
|
|
public static IEnumerable<TerminalCommand> instances = Assembly.GetExecutingAssembly().GetTypes()
|
|
|
|
|
.Where(t => t.IsSubclassOf(typeof(TerminalCommand)) && t.GetConstructor(Type.EmptyTypes) != null)
|
|
|
|
|
.Select(t => Activator.CreateInstance(t) as TerminalCommand);
|
2017-10-14 13:42:11 -04:00
|
|
|
|
|
2017-10-15 15:25:37 -04:00
|
|
|
|
public static List<ShiftOS.Apps.Terminal> trm = new List<ShiftOS.Apps.Terminal>();
|
|
|
|
|
public static int trmTopID = 0;
|
2017-11-21 18:47:37 -05:00
|
|
|
|
public static List<string> commandBuffer = new List<string>();
|
2017-10-14 13:42:11 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs a terminal command.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="command"></param>
|
2017-10-15 15:25:37 -04:00
|
|
|
|
/// <param name="rtb"><summary>The rich text box that the text will be written to.</summary></param>
|
|
|
|
|
public static void RunCommand(string command, int TermID)
|
2017-10-14 13:42:11 -04:00
|
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
try { name = command.Split(' ')[0]; } catch { name = command; }
|
|
|
|
|
|
|
|
|
|
var theParams = new string[command.Split(' ').Length - 1];
|
|
|
|
|
Array.Copy(command.Split(' '), 1, theParams, 0, command.Split(' ').Length - 1);
|
|
|
|
|
|
2017-11-21 17:26:14 -05:00
|
|
|
|
bool complete = false;
|
2017-10-14 13:42:11 -04:00
|
|
|
|
foreach (TerminalCommand instance in instances)
|
|
|
|
|
{
|
2017-10-15 15:25:37 -04:00
|
|
|
|
if (instance.Name.ToLower() == name.ToLower())
|
|
|
|
|
{
|
|
|
|
|
instance.TermID = TermID;
|
|
|
|
|
// Add a new line!
|
|
|
|
|
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText("\n");
|
|
|
|
|
instance.Run(theParams);
|
2017-11-21 17:26:14 -05:00
|
|
|
|
complete = true;
|
2017-11-21 18:47:37 -05:00
|
|
|
|
commandBuffer.Add(command);
|
2017-11-21 17:12:15 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-14 13:42:11 -04:00
|
|
|
|
}
|
2017-11-21 17:26:14 -05:00
|
|
|
|
if(!complete)
|
|
|
|
|
{
|
2017-11-21 17:53:24 -05:00
|
|
|
|
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.AppendText($"\n sbash: invalid command: {command.Split(' ').First()}");
|
2017-11-21 17:26:14 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-15 15:25:37 -04:00
|
|
|
|
Array.Find(trm.ToArray(), w => w.TerminalID == TermID).termmain.Text += " \n The command cannot be found. \n";
|
2017-10-14 13:42:11 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|