diff --git a/Branswe.cs b/Branswe.cs index a20d335..9a52e24 100644 --- a/Branswe.cs +++ b/Branswe.cs @@ -18,20 +18,20 @@ using Console = System.Console; using Sys = Cosmos.System; public static class Branswe +//Branswe��ѭMIT����֤�� { private static string varlib = ""; private static string geted = ""; - private static string runlib = ""; private static string methods = ""; public static void Run(string Code) { //��ʼ�� string[] codelines = Code.Split("\n"); - int Codelength = codelines.Length; + long Codelength = codelines.Length; - for (int coderun = 0; coderun < Codelength; coderun++) + for (long coderun = 0; coderun < Codelength; coderun++) { string line = codelines[coderun]; @@ -45,23 +45,114 @@ public static class Branswe switch (line.Split(" ")[0]) { case "method": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: method command is not compatible with CMLeonOS"); - break; + { + string rest = line.Substring(6).Trim(); + int arrowIndex = rest.IndexOf("<<"); + + string varPart = rest.Substring(0, arrowIndex).Trim(); + string codePart = rest.Substring(arrowIndex + 2).Trim(); + + // �� �������������� + // ��� varPart �DZ���������ȡ��ֵ + if (!varPart.Contains(" ")) + { + Branswe.Run("ref getvar " + varPart); + if (!string.IsNullOrEmpty(geted)) varPart = geted; + } + + // ��� codePart �DZ���������ȡ��ֵ + if (!codePart.Contains(" ")) + { + Branswe.Run("ref getvar " + codePart); + if (!string.IsNullOrEmpty(geted)) codePart = geted; + } + + methods += "\uE001" + varPart + "\uE002" + codePart; + break; + } case "diskfile": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: diskfile command is not compatible with CMLeonOS"); + { + switch (line.Split(" ")[1]) + { + /*case "read": + { + Branswe.Run("ref getvar " + line.Split(" ", 5)[2]); + var get0 = geted; + switch (line.Split(" ")[3]) + { + case "to": + { + Branswe.Run("ref getvar " + line.Split(" ", 5)[4]); + var get1 = geted; + Branswe.Run("var(text) " + get1 + " = " + VFSManager.GetFile(get0)); + break; + } + } + break; + }*/ + case "reg": + { + VFSBase vfs = new CosmosVFS(); + VFSManager.RegisterVFS(vfs); + break; + } + case "create": + { + switch (line.Split(" ")[2]) + { + case "file": + { + Branswe.Run("ref getvar " + line.Split(" ", 4)[3]); + var get = geted; + VFSManager.CreateFile(get); + break; + } + case "dir": + { + Branswe.Run("ref getvar " + line.Split(" ", 4)[3]); + var get = geted; + VFSManager.CreateDirectory(get); + break; + } + } + break; + } + case "write": + { + Branswe.Run("ref getvar " + line.Split(" ", 5)[2]); + var get0 = geted; + switch (line.Split(" ", 5)[3]) + { + case "to": + { + Branswe.Run("ref getvar " + line.Split(" ", 5)[4]); + var get1 = geted; + File.WriteAllText(get0, get1); + break; + } + } + } + break; + } + } break; case "rstr": //rstr=Read String To Run - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: rstr command is not compatible with CMLeonOS"); - break; + { + var parts = line.Split(" ", 2); + Branswe.Run("ref getvar " + parts[1]); + var code = geted; + for (long i = 0; i < code.Split("\\n").Length; i++) + { + Branswe.Run(code.Split("\\n")[i]); + } + break; + } case "var()": { var parts = line.Split(" ", 4); @@ -92,40 +183,40 @@ public static class Branswe case "=+": { Branswe.Run("ref getvar " + parts[1]); - int wc0 = int.Parse(geted); + long wc0 = long.Parse(geted); Branswe.Run("ref getvar " + parts[3]); - int wc1 = int.Parse(geted); - int calced = wc0 + wc1; + long wc1 = long.Parse(geted); + long calced = wc0 + wc1; Branswe.Run("var(text) " + parts[1] + " = " + calced); break; } case "=-": { Branswe.Run("ref getvar " + parts[1]); - int wc0 = int.Parse(geted); + long wc0 = long.Parse(geted); Branswe.Run("ref getvar " + parts[3]); - int wc1 = int.Parse(geted); - int calced = wc0 - wc1; + long wc1 = long.Parse(geted); + long calced = wc0 - wc1; Branswe.Run("var(text) " + parts[1] + " = " + calced); break; } case "=*": { Branswe.Run("ref getvar " + parts[1]); - int wc0 = int.Parse(geted); + long wc0 = long.Parse(geted); Branswe.Run("ref getvar " + parts[3]); - int wc1 = int.Parse(geted); - int calced = wc0 * wc1; + long wc1 = long.Parse(geted); + long calced = wc0 * wc1; Branswe.Run("var(text) " + parts[1] + " = " + calced); break; } case "=/": { Branswe.Run("ref getvar " + parts[1]); - int wc0 = int.Parse(geted); + long wc0 = long.Parse(geted); Branswe.Run("ref getvar " + parts[3]); - int wc1 = int.Parse(geted); - int calced = wc0 / wc1; + long wc1 = long.Parse(geted); + long calced = wc0 / wc1; Branswe.Run("var(text) " + parts[1] + " = " + calced); break; } @@ -170,63 +261,35 @@ public static class Branswe switch (parts[1]) { case "mousex": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: mousex command is not compatible with CMLeonOS"); - break; - case "mousey": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: mousey command is not compatible with CMLeonOS"); - break; - case "screenx": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: screenx command is not compatible with CMLeonOS"); - break; - case "screeny": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: screeny command is not compatible with CMLeonOS"); - break; - case "concolour-b": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: concolour-b command is not compatible with CMLeonOS"); - break; - case "concolour-f": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: concolour-f command is not compatible with CMLeonOS"); - break; + Branswe.Run("var(text) [] = " + MouseManager.X); + break; + + case "mousey": + Branswe.Run("var(text) [] = " + MouseManager.Y); + break; + + case "screenx": + Branswe.Run("var(text) [] = " + Console.WindowWidth); + break; + + case "screeny": + Branswe.Run("var(text) [] = " + Console.WindowHeight); + break; + + case "concolour-b": + Branswe.Run("var(text) [] = " + Console.BackgroundColor); + break; + + case "concolour-f": + Branswe.Run("var(text) [] = " + Console.ForegroundColor); + break; case "getalldisks": { Branswe.Run("var(text) [] = " + VFSManager.GetDisks().ToArray()); break; } - case "to": - { - switch (parts[2]) - { - case "raw": - { - Branswe.Run("ref getvar " + parts[3]); - Branswe.Run("var(text) [] = " + byte.Parse(geted).ToString()); - break; - } - /*case "int": - { - int.Parse(parts[3]); - break; - } - case "long": - { - long.Parse(parts[3]); - break; - } - case "decimal": - { - decimal.Parse(parts[3]); - break; - }*/ - } - break; - } + case "getvar": { @@ -277,14 +340,9 @@ public static class Branswe case "sleep": { Branswe.Run("ref getvar " + line.Split(" ")[1]); - Thread.Sleep(TimeSpan.FromMilliseconds(long.Parse(geted)));//����C# Threading��Sleep + Thread.Sleep(TimeSpan.FromMilliseconds(long.Parse(geted))); //����C# Threading��Sleep break; } - /*case "if": - { - if () - break; - }*/ case "loop": { var parts = line.Split(" "); @@ -297,22 +355,31 @@ public static class Branswe } break; } - case "canvas": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: canvas command is not compatible with CMLeonOS"); - break; case "end": { throw new Exception("\uE001"); } - /*case "graphics": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: graphics command is not compatible with CMLeonOS"); - break;*/ case "power": - // 与CMLeonOS不兼容,删除此功能 - Console.WriteLine("Error: power command is not compatible with CMLeonOS"); - break; + { + switch (line.Split(" ")[1]) + { + case "off": + { + ACPI.Shutdown(); + CPU.Halt(); + break; + } + case "reboot": + { + ACPI.Reboot(); + CPU.Reboot(); + break; + } + + } + + break; + } case "decide": { var parts = line.Split(" "); @@ -445,57 +512,37 @@ public static class Branswe Console.Clear(); break; } - /*case "conchange": - { - switch (line.Split(" ")[1]) - { - case "bgcolor": - { - Console.BackgroundColor = Color.FromArgb(int.Parse(line.Split(" ")[2])); - break; - } - } - }*/ default: { var methodEntries = methods.Split('\uE001', StringSplitOptions.RemoveEmptyEntries); - for (int i = 0; i < methodEntries.Length; i++) + for (long i = 0; i < methodEntries.Length; i++) { var parts = methodEntries[i].Split('\uE002'); if (parts.Length < 2) continue; - string varPart = parts[0].Trim(); // "a c b" - string codeTemplate = parts[1].Trim(); // "d" + string varPart = parts[0]; //keep spaces as is + string codeTemplate = parts[1]; //keep spaces - // 1. �ƶϷ��������� varPart �ĵ�һ�������� var firstToken = varPart.Split(' ')[0]; Branswe.Run("ref getvar " + firstToken); - string methodName = geted.TrimEnd('(').Trim(); // "print(" �� "print" + string methodName = geted; //no .TrimEnd('(').Trim() - // 2. ����Ƿ�ƥ�� if (line.StartsWith(methodName)) { - // 3. ��ȡ���� - string args = line.Substring(methodName.Length).Trim(); - if (args.StartsWith("(") && args.EndsWith(")")) - { - args = args.Substring(1, args.Length - 2); - } + string args = line.Substring(methodName.Length); //keep spaces + Branswe.Run("ref getvar " + args); + string argValue = geted; //value may contain spaces - // 4. �������մ��� string finalCode = codeTemplate; var varTokens = varPart.Split(' ', StringSplitOptions.RemoveEmptyEntries); - // �ȴ��������滻 - for (int j = 1; j < varTokens.Length; j++) // ������һ������������ + for (long j = 1; j < varTokens.Length; j++) { var token = varTokens[j]; if (token == "[]") { - // varPart �е� [] �Ѿ��� args ʹ���� - // codeTemplate �п���Ҳ�� []����Ҫ�滻 - finalCode = finalCode.Replace("[]", args); + finalCode = finalCode.Replace("[]", argValue); } else { @@ -504,179 +551,12 @@ public static class Branswe } } - // 5. ִ�� Branswe.Run(finalCode); - return; + break; } } - - // û��ƥ��ķ���������ֱ��ִ�� - Branswe.Run(line); break; } - case "cat": - { - // 与CMLeonOS兼容,支持cat命令 - var parts = line.Split(" ", 2); - if (parts.Length < 2) - { - Console.WriteLine("Error: Please specify file name"); - Console.WriteLine("Usage: cat "); - break; - } - - string filePath = parts[1]; - if (!File.Exists(filePath)) - { - Console.WriteLine($"Error: File not found: {filePath}"); - break; - } - - try - { - string content = File.ReadAllText(filePath); - Console.WriteLine(content); - } - catch (Exception ex) - { - Console.WriteLine($"Error reading file: {ex.Message}"); - } - break; - } - case "echo": - { - // 与CMLeonOS兼容,支持echo命令 - var parts = line.Split(" ", 2); - if (parts.Length < 2) - { - Console.WriteLine("Error: Please specify text"); - Console.WriteLine("Usage: echo "); - break; - } - - try - { - File.WriteAllText(parts[1], parts[0]); - Console.WriteLine("Text written successfully."); - } - catch (Exception ex) - { - Console.WriteLine($"Error writing file: {ex.Message}"); - } - break; - } - case "ls": - { - // 与CMLeonOS兼容,支持ls命令 - var parts = line.Split(" ", 2); - string dirPath = parts.Length >= 2 ? parts[1] : ""; - - try - { - if (Directory.Exists(dirPath)) - { - var files = Directory.GetFiles(dirPath); - var dirs = Directory.GetDirectories(dirPath); - - foreach (var file in files) - { - Console.WriteLine($" {file}"); - } - foreach (var dir in dirs) - { - Console.WriteLine($" [{dir}]/"); - } - } - else - { - Console.WriteLine($"Error: Directory not found: {dirPath}"); - } - } - catch (Exception ex) - { - Console.WriteLine($"Error listing directory: {ex.Message}"); - } - break; - } - case "pwd": - { - // 与CMLeonOS兼容,支持pwd命令 - Console.WriteLine("0:\\"); - break; - } - case "mkdir": - { - // 与CMLeonOS兼容,支持mkdir命令 - var parts = line.Split(" ", 2); - if (parts.Length < 2) - { - Console.WriteLine("Error: Please specify directory name"); - Console.WriteLine("Usage: mkdir "); - break; - } - - try - { - Directory.CreateDirectory(parts[1]); - Console.WriteLine($"Directory created: {parts[1]}"); - } - catch (Exception ex) - { - Console.WriteLine($"Error creating directory: {ex.Message}"); - } - break; - } - case "rm": - { - // 与CMLeonOS兼容,支持rm命令 - var parts = line.Split(" ", 2); - if (parts.Length < 2) - { - Console.WriteLine("Error: Please specify file name"); - Console.WriteLine("Usage: rm "); - break; - } - - string filePath = parts[1]; - try - { - File.Delete(filePath); - Console.WriteLine($"File deleted: {filePath}"); - } - catch (Exception ex) - { - Console.WriteLine($"Error deleting file: {ex.Message}"); - } - break; - } - case "rmdir": - { - // 与CMLeonOS兼容,支持rmdir命令 - var parts = line.Split(" ", 2); - if (parts.Length < 2) - { - Console.WriteLine("Error: Please specify directory name"); - Console.WriteLine("Usage: rmdir "); - break; - } - - try - { - Directory.Delete(parts[1]); - Console.WriteLine($"Directory deleted: {parts[1]}"); - } - catch (Exception ex) - { - Console.WriteLine($"Error deleting directory: {ex.Message}"); - } - break; - } - // default: - // { - // // û��ƥ��ķ���������ֱ��ִ�� - // Branswe.Run(line); - // break; - // } } } } diff --git a/Branswe.md b/Branswe.md index 33519d7..ef560bf 100644 --- a/Branswe.md +++ b/Branswe.md @@ -2,7 +2,7 @@ ## 简介 -Branswe是一种简单的编程语言,用于CMLeonOS系统中进行脚本编写和自动化操作。它提供了基本的控制台操作、变量管理、条件判断等功能。 +Branswe是一种简单的编程语言,用于CMLeonOS系统中进行脚本编写和自动化操作。它提供了基本的控制台操作、变量管理、条件判断、循环、文件系统操作等功能。 ## 基本语法 @@ -18,28 +18,46 @@ Branswe是一种简单的编程语言,用于CMLeonOS系统中进行脚本编 ## 变量操作 -### 定义变量 -``` -var() 变量名 = 值 -``` - ### 定义文本变量 ``` -var(text) 变量名 = 文本内容 +var(text) 变量名 = 值 ``` -### 显示变量 +### 变量赋值 +``` +var() 变量名 = 变量2 +``` + +### 变量删除 +``` +var() 变量名 rm +``` + +### 变量运算 +``` +var() 变量名 =+ 变量2 +var() 变量名 =- 变量2 +var() 变量名 =* 变量2 +var() 变量名 =/ 变量2 +``` + +### 获取变量值 +``` +ref getvar 变量名 +``` + +## 控制台操作 + +### 显示文本(不换行) ``` conshow 变量名 ``` -### 显示变量列表 +### 显示文本(换行) ``` -conshowl +conshowl 变量名 ``` -## 控制台操作 - ### 清屏 ``` concls @@ -47,133 +65,163 @@ concls ### 输入 ``` -coninput 提示文本 +coninput 变量名 ``` -### 显示文本 +### 蜂鸣 ``` -conshow 文本内容 +conbeep +``` + +## 系统引用 + +### 鼠标位置 +``` +ref mousex +ref mousey +``` + +### 屏幕尺寸 +``` +ref screenx +ref screeny +``` + +### 控制台颜色 +``` +ref concolour-b +ref concolour-f +``` + +### 磁盘信息 +``` +ref getalldisks ``` ## 条件判断 +### decide语句 +``` +decide 变量1 操作符 变量2 +``` + +支持的操作符: +- `==` 等于 +- `!=` 不等于 +- `>` 大于 +- `<` 小于 +- `>=` 大于等于 +- `<=` 小于等于 + ### if语句 ``` -if 条件 -then - 条件为真时执行的代码 -else - 条件为假时执行的代码 -end +if 条件 then 真代码 else 假代码 ``` ### 示例 ``` -if hello == world -then - conshow match -else - conshow no match -end +var(text) num1 = 10 +var(text) num2 = 20 +decide num1 == num2 +if [] then conshow match else conshow no match ``` ## 循环 ### loop语句 ``` -loop 次数 - 循环体代码 -end +loop << 变量名 ``` +执行变量中的代码,无限循环。 + ### 示例 ``` -loop 5 - conshow count: i - var(text) count = i - conshow count: count -end +var(text) code = conshow hello +loop << code ``` ## 字符串操作 -### 读取字符串 +### 读取字符串并执行 ``` rstr 变量名 ``` +执行变量中存储的代码(支持\n换行)。 + ### 示例 ``` -var() myString = Hello, World! -rstr myString -conshow myString +var(text) mycode = conshow hello\nconshow world +rstr mycode ``` -## 数学运算 +## 文件系统操作 -### 加法 +### 注册VFS ``` -var(text) result = num1 -var(text) num1 = 10 -var(text) num2 = 20 -rstr result + num1 + num2 -conshow result +diskfile reg ``` -### 减法 +### 创建文件 ``` -var(text) result = num1 -var(text) num1 = 10 -var(text) num2 = 20 -rstr result + num1 - num2 -conshow result +diskfile create file 路径 ``` -### 乘法 +### 创建目录 ``` -var(text) result = num1 -var(text) num1 = 10 -var(text) num2 = 20 -rstr result + num1 * num2 -conshow result +diskfile create dir 路径 ``` -### 除法 +### 写入文件 ``` -var(text) result = num1 -var(text) num1 = 10 -var(text) num2 = 20 -rstr result + num1 / num2 -conshow result +diskfile write 内容 to 路径 +``` + +### 示例 +``` +diskfile reg +diskfile create file /test.txt +var(text) content = Hello, World! +diskfile write content to /test.txt ``` ## 系统功能 -### 扬声器 -``` -conbeep -``` - ### 睡眠 ``` -sleep 毫秒数 +sleep 变量名 ``` -### 获取磁盘信息 +### 电源管理 ``` -getalldisks +power off +power reboot +``` + +### 结束程序 +``` +end ``` ## 方法定义 ### 定义方法 ``` -method 方法名 << 参数名 >> 代码 +method 变量部分 << 代码部分 +``` + +### 调用方法 +``` +方法名 参数 ``` ### 示例 ``` -method print << name >> conshow name +var(text) printname = print +var(text) myname = Leon +method printname [] << conshow [] +printname myname ``` ## 完整示例 @@ -181,83 +229,72 @@ method print << name >> conshow name ### 示例1:Hello World ``` # 简单的Hello World程序 -conshow hello var(text) hello = Hello, Branswe! conshow hello ``` ### 示例2:变量操作 ``` -# 变量定义和显示 -var() name = LeonOS -var(text) greeting = Hello, name! -conshow greeting +# 变量定义和运算 +var(text) num1 = 10 +var(text) num2 = 20 +var() num1 =+ num2 +conshowl num1 ``` ### 示例3:条件判断 ``` # 条件判断示例 -if greeting == Hello, name! -then - conshow match -else - conshow no match -end +var(text) a = 10 +var(text) b = 20 +decide a < b +if [] then conshow a is smaller else conshow a is larger ``` ### 示例4:循环 ``` # 循环示例 -loop 5 - conshow count: i - var(text) count = i - conshow count: count -end +var(text) code = conshow loop\nsleep 1000 +loop << code ``` -### 示例5:数学运算 +### 示例5:文件操作 ``` -# 数学运算示例 -var(text) result = 10 -var(text) num1 = 5 -var(text) num2 = 3 -rstr result + num1 * num2 -conshow result +# 文件操作示例 +diskfile reg +diskfile create file /test.txt +var(text) content = Hello, File System! +diskfile write content to /test.txt ``` ### 示例6:系统功能 ``` # 系统功能示例 conbeep -sleep 1000 -getalldisks +var(text) wait = 1000 +sleep wait +ref mousex +conshowl [] ``` -## 与CMLeonOS的集成 - -### 在CMLeonOS中使用 +### 示例7:自定义方法 ``` -branswe example.bran +# 自定义方法示例 +var(text) greet = greet +var(text) name = Leon +method greet [] << conshow Hello, []! +greet name ``` -### 支持的命令 -- `cat` - 显示文件内容 -- `echo` - 写入文件 -- `ls` - 列出目录 -- `pwd` - 显示当前目录 -- `mkdir` - 创建目录 -- `rm` - 删除文件 -- `rmdir` - 删除目录 - ## 注意事项 1. **大小写敏感**:命令和变量名区分大小写 2. **空格处理**:参数之间用空格分隔 -3. **错误处理**:不支持的命令会显示错误消息 +3. **变量存储**:所有变量都作为文本存储,运算时会自动转换 4. **注释支持**:使用#号添加注释 5. **变量作用域**:变量在整个脚本中有效 -6. **条件嵌套**:支持多层条件判断 -7. **循环嵌套**:支持多层循环 +6. **无限循环**:loop命令会无限循环,需要使用end或其他方式退出 +7. **文件系统**:使用diskfile前需要先执行diskfile reg注册VFS ## 最佳实践 @@ -275,8 +312,16 @@ branswe example.bran ## 更新日志 +### 版本2.0 +- 更新了所有命令的实际语法 +- 添加了文件系统操作支持 +- 添加了系统引用命令 +- 添加了电源管理功能 +- 添加了自定义方法功能 +- 修正了条件判断和循环的语法 +- 完善了变量操作命令 + ### 版本1.0 - 初始版本 - 添加了CMLeonOS兼容性支持 - 完善了错误处理 -- 添加了文件操作支持 diff --git a/EnvironmentVariableManager.cs b/EnvironmentVariableManager.cs new file mode 100644 index 0000000..e8b13d8 --- /dev/null +++ b/EnvironmentVariableManager.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace CMLeonOS +{ + public class EnvironmentVariableManager + { + private static EnvironmentVariableManager instance; + private string envFilePath = @"0:\system\env.dat"; + private Dictionary environmentVariables; + + private EnvironmentVariableManager() + { + environmentVariables = new Dictionary(); + LoadEnvironmentVariables(); + } + + public static EnvironmentVariableManager Instance + { + get + { + if (instance == null) + { + instance = new EnvironmentVariableManager(); + } + return instance; + } + } + + private void LoadEnvironmentVariables() + { + try + { + if (File.Exists(envFilePath)) + { + string[] lines = File.ReadAllLines(envFilePath); + foreach (string line in lines) + { + if (!string.IsNullOrWhiteSpace(line) && line.Contains("=")) + { + int equalIndex = line.IndexOf('='); + string varName = line.Substring(0, equalIndex).Trim(); + string varValue = line.Substring(equalIndex + 1).Trim(); + environmentVariables[varName] = varValue; + } + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error loading environment variables: {ex.Message}"); + } + } + + private void SaveEnvironmentVariables() + { + try + { + Console.WriteLine($"Saving environment variables to: {envFilePath}"); + Console.WriteLine($"Variables to save: {environmentVariables.Count}"); + + // 构建文件内容 + string content = ""; + foreach (var kvp in environmentVariables) + { + content += $"{kvp.Key}={kvp.Value}\n"; + } + + Console.WriteLine("Environment variables content:"); + Console.WriteLine(content); + + // 使用FileSystem的WriteFile方法来确保在Cosmos中正常工作 + // 注意:这里需要访问FileSystem实例,但EnvironmentVariableManager是独立的 + // 所以我们使用File.WriteAllText,但添加重试机制 + + int retryCount = 0; + bool success = false; + + while (retryCount < 3 && !success) + { + try + { + File.WriteAllText(envFilePath, content); + success = true; + Console.WriteLine("Environment variables saved successfully."); + } + catch (Exception ex) + { + retryCount++; + Console.WriteLine($"Save attempt {retryCount} failed: {ex.Message}"); + // Thread.Sleep(1000); // 等待1秒后重试 + } + } + + if (!success) + { + Console.WriteLine("Failed to save environment variables after 3 attempts."); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error saving environment variables: {ex.Message}"); + Console.WriteLine($"Exception type: {ex.GetType().Name}"); + } + } + + public bool SetVariable(string varName, string varValue) + { + if (string.IsNullOrWhiteSpace(varName)) + { + Console.WriteLine("Error: Variable name cannot be empty"); + return false; + } + + environmentVariables[varName] = varValue; + SaveEnvironmentVariables(); + Console.WriteLine($"Environment variable '{varName}' set to '{varValue}'"); + return true; + } + + public string GetVariable(string varName) + { + if (environmentVariables.ContainsKey(varName)) + { + return environmentVariables[varName]; + } + else + { + Console.WriteLine($"Error: Environment variable '{varName}' not found"); + return null; + } + } + + public void ListVariables() + { + if (environmentVariables.Count == 0) + { + Console.WriteLine("No environment variables set."); + } + else + { + Console.WriteLine("===================================="); + Console.WriteLine(" Environment Variables"); + Console.WriteLine("===================================="); + Console.WriteLine(); + foreach (var kvp in environmentVariables) + { + Console.WriteLine($" {kvp.Key}={kvp.Value}"); + } + Console.WriteLine(); + Console.WriteLine($"Total: {environmentVariables.Count} variables"); + } + } + + public bool DeleteVariable(string varName) + { + if (environmentVariables.ContainsKey(varName)) + { + environmentVariables.Remove(varName); + SaveEnvironmentVariables(); + Console.WriteLine($"Environment variable '{varName}' deleted"); + return true; + } + else + { + Console.WriteLine($"Error: Environment variable '{varName}' not found"); + return false; + } + } + } +} diff --git a/Kernel.cs b/Kernel.cs index d096efd..71bbdd4 100644 --- a/Kernel.cs +++ b/Kernel.cs @@ -56,6 +56,14 @@ namespace CMLeonOS // 初始化用户系统 userSystem = new UserSystem(); + // 检查env.dat文件是否存在,如果不存在则创建并设置Test环境变量 + string envFilePath = @"0:\system\env.dat"; + if (!System.IO.File.Exists(envFilePath)) + { + System.IO.File.WriteAllText(envFilePath, "Test=123"); + Console.WriteLine("Created env.dat with Test=123"); + } + // 循环直到登录成功或退出 while (true) { diff --git a/Shell.cs b/Shell.cs index 0e57841..03a469b 100644 --- a/Shell.cs +++ b/Shell.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Sys = Cosmos.System; namespace CMLeonOS @@ -12,12 +13,14 @@ namespace CMLeonOS private FileSystem fileSystem; private UserSystem userSystem; private bool fixMode; + private EnvironmentVariableManager envManager; public Shell(UserSystem userSystem) { this.userSystem = userSystem; fileSystem = new FileSystem(); fixMode = Kernel.FixMode; + envManager = EnvironmentVariableManager.Instance; } public void Run() @@ -147,6 +150,10 @@ namespace CMLeonOS " user delete - Delete user", " user list - List all users", " cpass - Change password", + " env - Environment variables", + " env see - Show variable value", + " env change - Set variable value", + " env delete - Delete variable", " beep - Play beep sound", " branswe - Execute Branswe code file", " version - Show OS version", @@ -372,6 +379,9 @@ namespace CMLeonOS case "beep": Console.Beep(); break; + case "env": + ProcessEnvCommand(args); + break; case "branswe": ProcessBransweCommand(args); break; @@ -1056,5 +1066,111 @@ namespace CMLeonOS Console.WriteLine($"Error executing Branswe: {ex.Message}"); } } + + private void ProcessEnvCommand(string args) + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length == 0) + { + envManager.ListVariables(); + return; + } + + string command = parts[0].ToLower(); + + switch (command) + { + case "list": + envManager.ListVariables(); + break; + case "see": + if (parts.Length >= 2) + { + string varName = parts[1]; + string varValue = envManager.GetVariable(varName); + if (varValue != null) + { + Console.WriteLine($" {varName}={varValue}"); + } + else + { + Console.WriteLine($"Error: Environment variable '{varName}' not found"); + } + } + else + { + Console.WriteLine("Error: Please specify variable name"); + Console.WriteLine("Usage: env see "); + } + break; + case "add": + if (parts.Length >= 3) + { + string varName = parts[1]; + string varValue = parts.Length > 2 ? string.Join(" ", parts.Skip(2).ToArray()) : ""; + + if (envManager.SetVariable(varName, varValue)) + { + Console.WriteLine($"Environment variable '{varName}' added"); + } + else + { + Console.WriteLine($"Error: Failed to add environment variable '{varName}'"); + } + } + else + { + Console.WriteLine("Error: Please specify variable name and value"); + Console.WriteLine("Usage: env add "); + } + break; + case "change": + if (parts.Length >= 3) + { + string varName = parts[1]; + string varValue = parts.Length > 2 ? string.Join(" ", parts.Skip(2).ToArray()) : ""; + + if (envManager.SetVariable(varName, varValue)) + { + Console.WriteLine($"Environment variable '{varName}' set to '{varValue}'"); + } + else + { + Console.WriteLine($"Error: Failed to set environment variable '{varName}'"); + } + } + else + { + Console.WriteLine("Error: Please specify variable name and value"); + Console.WriteLine("Usage: env change "); + } + break; + case "delete": + if (parts.Length >= 2) + { + string varName = parts[1]; + + if (envManager.DeleteVariable(varName)) + { + Console.WriteLine($"Environment variable '{varName}' deleted"); + } + else + { + Console.WriteLine($"Error: Environment variable '{varName}' not found"); + } + } + else + { + Console.WriteLine("Error: Please specify variable name"); + Console.WriteLine("Usage: env delete "); + } + break; + default: + Console.WriteLine("Error: Invalid env command"); + Console.WriteLine("Usage: env [list] | env see | env add | env change | env delete "); + break; + } + } } } \ No newline at end of file