mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 15:30:27 +00:00
拆分代码6
This commit is contained in:
36
shell/Commands/Script/BransweCommand.cs
Normal file
36
shell/Commands/Script/BransweCommand.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace CMLeonOS.Commands.Script
|
||||||
|
{
|
||||||
|
public static class BransweCommand
|
||||||
|
{
|
||||||
|
public static void ProcessBransweCommand(string args, CMLeonOS.FileSystem fileSystem, Action<string> showError)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(args))
|
||||||
|
{
|
||||||
|
showError("Error: Please specify file name");
|
||||||
|
showError("Usage: branswe <filename>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string filePath = fileSystem.GetFullPath(args);
|
||||||
|
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
{
|
||||||
|
showError($"Error: File not found: {args}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string fileContent = File.ReadAllText(filePath);
|
||||||
|
Branswe.Run(fileContent);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
showError($"Error executing Branswe: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
shell/Commands/Script/ComCommand.cs
Normal file
50
shell/Commands/Script/ComCommand.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace CMLeonOS.Commands.Script
|
||||||
|
{
|
||||||
|
public static class ComCommand
|
||||||
|
{
|
||||||
|
public static void ExecuteCommandFile(string args, CMLeonOS.FileSystem fileSystem, Shell shell, Action<string> showError, Action<string> showWarning)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(args))
|
||||||
|
{
|
||||||
|
showError("Usage: com <filename.cm>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string filePath = fileSystem.GetFullPath(args);
|
||||||
|
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
{
|
||||||
|
showError($"File not found: {args}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string[] lines = fileSystem.ReadFile(filePath).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
|
||||||
|
|
||||||
|
if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0])))
|
||||||
|
{
|
||||||
|
showWarning("Command file is empty");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (string line in lines)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#"))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
shell.ExecuteCommand(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
showError($"Error executing command file: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
185
shell/Commands/Script/LuaCommand.cs
Normal file
185
shell/Commands/Script/LuaCommand.cs
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using UniLua;
|
||||||
|
|
||||||
|
namespace CMLeonOS.Commands.Script
|
||||||
|
{
|
||||||
|
public static class LuaCommand
|
||||||
|
{
|
||||||
|
public static void ExecuteLuaScript(string args, CMLeonOS.FileSystem fileSystem, Shell shell, Action<string> showError, Action<string> showWarning)
|
||||||
|
{
|
||||||
|
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
if (parts.Length == 0)
|
||||||
|
{
|
||||||
|
showError("Error: Please specify Lua script file or use --shell for interactive mode");
|
||||||
|
showError("Usage: lua <file> or lua --shell");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parts.Length == 1 && parts[0] == "--shell")
|
||||||
|
{
|
||||||
|
EnterLuaShell(showError);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string filePath = parts[0];
|
||||||
|
string originalPath = filePath;
|
||||||
|
|
||||||
|
if (!filePath.StartsWith("0:\\") && !filePath.StartsWith("0:/"))
|
||||||
|
{
|
||||||
|
string currentDir = fileSystem.CurrentDirectory;
|
||||||
|
if (currentDir == "/" || currentDir == "\\")
|
||||||
|
{
|
||||||
|
filePath = "0:\\" + filePath.TrimStart('/').TrimStart('\\');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
filePath = Path.Combine(currentDir, filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
{
|
||||||
|
showError($"Error: File not found: {filePath}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string scriptContent = File.ReadAllText(filePath);
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(scriptContent))
|
||||||
|
{
|
||||||
|
showWarning("Script file is empty");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ILuaState lua = LuaAPI.NewState();
|
||||||
|
lua.L_OpenLibs();
|
||||||
|
|
||||||
|
UniLua.ThreadStatus loadResult = lua.L_LoadString(scriptContent);
|
||||||
|
|
||||||
|
if (loadResult == UniLua.ThreadStatus.LUA_OK)
|
||||||
|
{
|
||||||
|
UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0);
|
||||||
|
|
||||||
|
if (callResult == UniLua.ThreadStatus.LUA_OK)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string errorMsg = lua.ToString(-1);
|
||||||
|
if (string.IsNullOrWhiteSpace(errorMsg))
|
||||||
|
{
|
||||||
|
showError($"Script execution error: Unknown error");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
showError($"Script execution error: {errorMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string errorMsg = lua.ToString(-1);
|
||||||
|
if (string.IsNullOrWhiteSpace(errorMsg))
|
||||||
|
{
|
||||||
|
showError($"Script load error: Unknown error");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
showError($"Script load error: {errorMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
showError($"Lua execution error: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void EnterLuaShell(Action<string> showError)
|
||||||
|
{
|
||||||
|
Console.WriteLine("====================================");
|
||||||
|
Console.WriteLine(" Lua Interactive Shell");
|
||||||
|
Console.WriteLine("====================================");
|
||||||
|
Console.WriteLine("Type 'exit' or 'quit' to exit");
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
ILuaState lua = LuaAPI.NewState();
|
||||||
|
lua.L_OpenLibs();
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
Console.Write("lua> ");
|
||||||
|
string input = Console.ReadLine();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(input))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.ToLower() == "exit" || input.ToLower() == "quit")
|
||||||
|
{
|
||||||
|
Console.WriteLine("Exiting Lua shell...");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UniLua.ThreadStatus loadResult = lua.L_LoadString(input);
|
||||||
|
|
||||||
|
if (loadResult == UniLua.ThreadStatus.LUA_OK)
|
||||||
|
{
|
||||||
|
UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0);
|
||||||
|
|
||||||
|
if (callResult == UniLua.ThreadStatus.LUA_OK)
|
||||||
|
{
|
||||||
|
int top = lua.GetTop();
|
||||||
|
if (top > 0)
|
||||||
|
{
|
||||||
|
for (int i = 1; i <= top; i++)
|
||||||
|
{
|
||||||
|
string result = lua.ToString(i);
|
||||||
|
if (!string.IsNullOrWhiteSpace(result))
|
||||||
|
{
|
||||||
|
Console.WriteLine(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string errorMsg = lua.ToString(-1);
|
||||||
|
if (string.IsNullOrWhiteSpace(errorMsg))
|
||||||
|
{
|
||||||
|
showError($"Execution error: Unknown error");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
showError($"Execution error: {errorMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string errorMsg = lua.ToString(-1);
|
||||||
|
if (string.IsNullOrWhiteSpace(errorMsg))
|
||||||
|
{
|
||||||
|
showError($"Load error: Unknown error");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
showError($"Load error: {errorMsg}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
showError($"Lua error: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
240
shell/Shell.cs
240
shell/Shell.cs
@@ -618,44 +618,7 @@ namespace CMLeonOS
|
|||||||
|
|
||||||
public void ExecuteCommandFile(string args)
|
public void ExecuteCommandFile(string args)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(args))
|
Commands.Script.ComCommand.ExecuteCommandFile(args, fileSystem, this, ShowError, ShowWarning);
|
||||||
{
|
|
||||||
ShowError("Usage: com <filename.cm>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string filePath = fileSystem.GetFullPath(args);
|
|
||||||
|
|
||||||
if (!System.IO.File.Exists(filePath))
|
|
||||||
{
|
|
||||||
ShowError($"File not found: {args}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string[] lines = fileSystem.ReadFile(filePath).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
|
|
||||||
|
|
||||||
if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0])))
|
|
||||||
{
|
|
||||||
ShowWarning("Command file is empty");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (string line in lines)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("#"))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ExecuteCommand(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
ShowError($"Error executing command file: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HeadFile(string args)
|
public void HeadFile(string args)
|
||||||
@@ -800,32 +763,7 @@ namespace CMLeonOS
|
|||||||
|
|
||||||
public void ProcessBransweCommand(string args)
|
public void ProcessBransweCommand(string args)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(args))
|
Commands.Script.BransweCommand.ProcessBransweCommand(args, fileSystem, ShowError);
|
||||||
{
|
|
||||||
ShowError("Error: Please specify file name");
|
|
||||||
ShowError("Usage: branswe <filename>");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string filePath = fileSystem.GetFullPath(args);
|
|
||||||
|
|
||||||
if (!File.Exists(filePath))
|
|
||||||
{
|
|
||||||
ShowError($"Error: File not found: {args}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string fileContent = File.ReadAllText(filePath);
|
|
||||||
// Console.WriteLine($"Executing Branswe code from: {args}");
|
|
||||||
Branswe.Run(fileContent);
|
|
||||||
// Console.WriteLine("Branswe execution completed.");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
ShowError($"Error executing Branswe: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GrepFile(string args)
|
public void GrepFile(string args)
|
||||||
@@ -1763,179 +1701,7 @@ namespace CMLeonOS
|
|||||||
|
|
||||||
public void ExecuteLuaScript(string args)
|
public void ExecuteLuaScript(string args)
|
||||||
{
|
{
|
||||||
string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
Commands.Script.LuaCommand.ExecuteLuaScript(args, fileSystem, this, ShowError, ShowWarning);
|
||||||
|
|
||||||
if (parts.Length == 0)
|
|
||||||
{
|
|
||||||
ShowError("Error: Please specify Lua script file or use --shell for interactive mode");
|
|
||||||
ShowError("Usage: lua <file> or lua --shell");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parts.Length == 1 && parts[0] == "--shell")
|
|
||||||
{
|
|
||||||
EnterLuaShell();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string filePath = parts[0];
|
|
||||||
string originalPath = filePath;
|
|
||||||
|
|
||||||
if (!filePath.StartsWith("0:\\") && !filePath.StartsWith("0:/"))
|
|
||||||
{
|
|
||||||
if (prompt == "/" || prompt == "\\")
|
|
||||||
{
|
|
||||||
filePath = "0:\\" + filePath.TrimStart('/').TrimStart('\\');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
filePath = Path.Combine(prompt, filePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!File.Exists(filePath))
|
|
||||||
{
|
|
||||||
ShowError($"Error: File not found: {filePath}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string scriptContent = File.ReadAllText(filePath);
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(scriptContent))
|
|
||||||
{
|
|
||||||
ShowWarning("Script file is empty");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ILuaState lua = LuaAPI.NewState();
|
|
||||||
lua.L_OpenLibs();
|
|
||||||
|
|
||||||
UniLua.ThreadStatus loadResult = lua.L_LoadString(scriptContent);
|
|
||||||
|
|
||||||
if (loadResult == UniLua.ThreadStatus.LUA_OK)
|
|
||||||
{
|
|
||||||
UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0);
|
|
||||||
|
|
||||||
if (callResult == UniLua.ThreadStatus.LUA_OK)
|
|
||||||
{
|
|
||||||
// 不要动这里
|
|
||||||
// ShowSuccess("Script run successfully");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string errorMsg = lua.ToString(-1);
|
|
||||||
if (string.IsNullOrWhiteSpace(errorMsg))
|
|
||||||
{
|
|
||||||
ShowError($"Script execution error: Unknown error");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ShowError($"Script execution error: {errorMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string errorMsg = lua.ToString(-1);
|
|
||||||
if (string.IsNullOrWhiteSpace(errorMsg))
|
|
||||||
{
|
|
||||||
ShowError($"Script load error: Unknown error");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ShowError($"Script load error: {errorMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
ShowError($"Lua execution error: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EnterLuaShell()
|
|
||||||
{
|
|
||||||
Console.WriteLine("====================================");
|
|
||||||
Console.WriteLine(" Lua Interactive Shell");
|
|
||||||
Console.WriteLine("====================================");
|
|
||||||
Console.WriteLine("Type 'exit' or 'quit' to exit");
|
|
||||||
Console.WriteLine();
|
|
||||||
|
|
||||||
ILuaState lua = LuaAPI.NewState();
|
|
||||||
lua.L_OpenLibs();
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
Console.Write("lua> ");
|
|
||||||
string input = Console.ReadLine();
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(input))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (input.ToLower() == "exit" || input.ToLower() == "quit")
|
|
||||||
{
|
|
||||||
Console.WriteLine("Exiting Lua shell...");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
UniLua.ThreadStatus loadResult = lua.L_LoadString(input);
|
|
||||||
|
|
||||||
if (loadResult == UniLua.ThreadStatus.LUA_OK)
|
|
||||||
{
|
|
||||||
UniLua.ThreadStatus callResult = lua.PCall(0, 0, 0);
|
|
||||||
|
|
||||||
if (callResult == UniLua.ThreadStatus.LUA_OK)
|
|
||||||
{
|
|
||||||
int top = lua.GetTop();
|
|
||||||
if (top > 0)
|
|
||||||
{
|
|
||||||
for (int i = 1; i <= top; i++)
|
|
||||||
{
|
|
||||||
string result = lua.ToString(i);
|
|
||||||
if (!string.IsNullOrWhiteSpace(result))
|
|
||||||
{
|
|
||||||
Console.WriteLine(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string errorMsg = lua.ToString(-1);
|
|
||||||
if (string.IsNullOrWhiteSpace(errorMsg))
|
|
||||||
{
|
|
||||||
ShowError($"Execution error: Unknown error");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ShowError($"Execution error: {errorMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
string errorMsg = lua.ToString(-1);
|
|
||||||
if (string.IsNullOrWhiteSpace(errorMsg))
|
|
||||||
{
|
|
||||||
ShowError($"Load error: Unknown error");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ShowError($"Load error: {errorMsg}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
ShowError($"Lua error: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetDnsServer(string args)
|
public void SetDnsServer(string args)
|
||||||
|
|||||||
Reference in New Issue
Block a user