-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblsh.cs
111 lines (103 loc) · 3.35 KB
/
blsh.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Classes:
// Initialize: Static class for seting up shell environment
// Session: Creates environment variables
// Built-Ins: Built-in shell methods - see class for list
//NEXT move checkini and check history into one method
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.ComponentModel;
using System.Text;
namespace Blsh
{
public class theShell
{
public static string ver = "0.001";
public static string license = "GPLv3";
public static string authors = "Brian Pritt";
public static void Main()
{
string input = null;
Console.Clear();
Initialize.makeFile(".blsh_history");
Initialize.makeFile("blsh.ini");
Initialize.checkIni();//looks for .ini file makes one if does not exist
Initialize newInit = new Initialize();
newInit.ConfigEnv();
Session newSession = new Session (newInit.GetPath(), newInit.GetBinaries());
Directory.SetCurrentDirectory(newSession.GetPath());
ConsoleKeyInfo keyinf;
do
{
Console.Write(newSession.GetPath() + " $ ");
input = Console.ReadLine();
string[] splitCommand = input.Split(" ");
string command = splitCommand[0];
string[] argsArray = splitCommand.Skip(1).ToArray();
string args = string.Join(" ", argsArray);
Session.AddCommand(input);//add to history
Promulgate(newSession, command, args); //Magic
} while (input != "exit");
}
public static void Promulgate(Session currentSession, string command, string args)
{
//only works on exe files right now, need to add support for native MacOS / linux
string external = command + ".exe" ;
int exit;
if (command == "exit")
{
Console.WriteLine("Exiting...");
EndSession(currentSession);
}
else if (BuiltIns.builtins.ContainsKey(command))
{
int code = BuiltIns.runBuiltIns(currentSession, command, args);
}
else
{
//Cycle through arry of paths and
Process process = new Process();
string[] paths = currentSession.GetBin();
foreach (string bin in paths)
{
try
{
process.StartInfo = new ProcessStartInfo(bin + external, args );
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
// exit = process.ExitCode;
}
catch (Win32Exception w)
{
//figure out how to put exceptions output later, since it is likely going to throw many when it cycles through list
}
}
}
}
public static void EndSession(Session currentSession){
List<string> history = currentSession.GetCommands();
string hist = @".blsh_history";
if (!File.Exists(hist))
{
Console.WriteLine("Creating files nessicary for richer experience");
// only MacOS rn
using(FileStream fs = File.Create(hist))
{
Byte[] fle = new UTF8Encoding(true).GetBytes("");
fs.Write(fle,0,fle.Length);
fs.Dispose();
}
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(hist, true))
{
foreach(string line in history)
{
file.WriteLine(line);
}
}
}
}
}