diff --git a/Branswe.cs b/Branswe.cs index 35dcfed..a20d335 100644 --- a/Branswe.cs +++ b/Branswe.cs @@ -514,6 +514,169 @@ public static class Branswe 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 new file mode 100644 index 0000000..33519d7 --- /dev/null +++ b/Branswe.md @@ -0,0 +1,282 @@ +# Branswe编程语言使用指南 + +## 简介 + +Branswe是一种简单的编程语言,用于CMLeonOS系统中进行脚本编写和自动化操作。它提供了基本的控制台操作、变量管理、条件判断等功能。 + +## 基本语法 + +### 命令格式 +``` +命令名 [参数] +``` + +### 注释 +``` +# 这是注释 +``` + +## 变量操作 + +### 定义变量 +``` +var() 变量名 = 值 +``` + +### 定义文本变量 +``` +var(text) 变量名 = 文本内容 +``` + +### 显示变量 +``` +conshow 变量名 +``` + +### 显示变量列表 +``` +conshowl +``` + +## 控制台操作 + +### 清屏 +``` +concls +``` + +### 输入 +``` +coninput 提示文本 +``` + +### 显示文本 +``` +conshow 文本内容 +``` + +## 条件判断 + +### if语句 +``` +if 条件 +then + 条件为真时执行的代码 +else + 条件为假时执行的代码 +end +``` + +### 示例 +``` +if hello == world +then + conshow match +else + conshow no match +end +``` + +## 循环 + +### loop语句 +``` +loop 次数 + 循环体代码 +end +``` + +### 示例 +``` +loop 5 + conshow count: i + var(text) count = i + conshow count: count +end +``` + +## 字符串操作 + +### 读取字符串 +``` +rstr 变量名 +``` + +### 示例 +``` +var() myString = Hello, World! +rstr myString +conshow myString +``` + +## 数学运算 + +### 加法 +``` +var(text) result = num1 +var(text) num1 = 10 +var(text) num2 = 20 +rstr result + num1 + num2 +conshow result +``` + +### 减法 +``` +var(text) result = num1 +var(text) num1 = 10 +var(text) num2 = 20 +rstr result + num1 - num2 +conshow result +``` + +### 乘法 +``` +var(text) result = num1 +var(text) num1 = 10 +var(text) num2 = 20 +rstr result + num1 * num2 +conshow result +``` + +### 除法 +``` +var(text) result = num1 +var(text) num1 = 10 +var(text) num2 = 20 +rstr result + num1 / num2 +conshow result +``` + +## 系统功能 + +### 扬声器 +``` +conbeep +``` + +### 睡眠 +``` +sleep 毫秒数 +``` + +### 获取磁盘信息 +``` +getalldisks +``` + +## 方法定义 + +### 定义方法 +``` +method 方法名 << 参数名 >> 代码 +``` + +### 示例 +``` +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 +``` + +### 示例3:条件判断 +``` +# 条件判断示例 +if greeting == Hello, name! +then + conshow match +else + conshow no match +end +``` + +### 示例4:循环 +``` +# 循环示例 +loop 5 + conshow count: i + var(text) count = i + conshow count: count +end +``` + +### 示例5:数学运算 +``` +# 数学运算示例 +var(text) result = 10 +var(text) num1 = 5 +var(text) num2 = 3 +rstr result + num1 * num2 +conshow result +``` + +### 示例6:系统功能 +``` +# 系统功能示例 +conbeep +sleep 1000 +getalldisks +``` + +## 与CMLeonOS的集成 + +### 在CMLeonOS中使用 +``` +branswe example.bran +``` + +### 支持的命令 +- `cat` - 显示文件内容 +- `echo` - 写入文件 +- `ls` - 列出目录 +- `pwd` - 显示当前目录 +- `mkdir` - 创建目录 +- `rm` - 删除文件 +- `rmdir` - 删除目录 + +## 注意事项 + +1. **大小写敏感**:命令和变量名区分大小写 +2. **空格处理**:参数之间用空格分隔 +3. **错误处理**:不支持的命令会显示错误消息 +4. **注释支持**:使用#号添加注释 +5. **变量作用域**:变量在整个脚本中有效 +6. **条件嵌套**:支持多层条件判断 +7. **循环嵌套**:支持多层循环 + +## 最佳实践 + +1. **添加注释**:在复杂逻辑前添加注释说明 +2. **使用变量**:避免硬编码,使用变量提高灵活性 +3. **错误检查**:在使用变量前检查其值 +4. **代码格式**:保持代码缩进和格式一致 +5. **测试验证**:逐步测试每个功能确保正确性 + +## 参考资料 + +- CMLeonOS Shell命令 +- Branswe编程语言规范 +- Cosmos系统文档 + +## 更新日志 + +### 版本1.0 +- 初始版本 +- 添加了CMLeonOS兼容性支持 +- 完善了错误处理 +- 添加了文件操作支持 diff --git a/Editor.cs b/Editor.cs index 2d9134e..8cd4035 100644 --- a/Editor.cs +++ b/Editor.cs @@ -142,7 +142,7 @@ namespace CMLeonOS // 设置光标位置 try { - Console.SetCursorPosition(currentColumn + 4, cursorY); + Console.SetCursorPosition(currentColumn + 3, cursorY); } catch { @@ -165,9 +165,9 @@ namespace CMLeonOS // 删除字符 if (currentColumn > 0) { - string line = lines[currentLine]; - line = line.Remove(currentColumn - 1, 1); - lines[currentLine] = line; + string backspaceLineText = lines[currentLine]; + string backspaceModifiedLine = backspaceLineText.Remove(currentColumn - 1, 1); + lines[currentLine] = backspaceModifiedLine; currentColumn--; } else if (currentLine > 0) @@ -183,11 +183,11 @@ namespace CMLeonOS break; case ConsoleKey.Enter: // 插入换行 - string currentLineText = lines[currentLine]; - string firstPart = currentLineText.Substring(0, currentColumn); - string secondPart = currentLineText.Substring(currentColumn); - lines[currentLine] = firstPart; - lines.Insert(currentLine + 1, secondPart); + string enterLineText = lines[currentLine]; + string enterFirstPart = enterLineText.Substring(0, currentColumn); + string enterSecondPart = enterLineText.Substring(currentColumn); + lines[currentLine] = enterFirstPart; + lines.Insert(currentLine + 1, enterSecondPart); currentLine++; currentColumn = 0; break; @@ -221,13 +221,21 @@ namespace CMLeonOS currentColumn = Math.Min(currentColumn, lines[currentLine].Length); } break; + case ConsoleKey.Tab: + // Tab键插入四个空格 + string tabLineText = lines[currentLine]; + string tabSpaces = " "; + string tabModifiedLine = tabLineText.Insert(currentColumn, tabSpaces); + lines[currentLine] = tabModifiedLine; + currentColumn += 4; + break; default: // 输入字符 if (char.IsLetterOrDigit(keyInfo.KeyChar) || char.IsPunctuation(keyInfo.KeyChar) || char.IsSymbol(keyInfo.KeyChar) || keyInfo.KeyChar == ' ') { - string line = lines[currentLine]; - line = line.Insert(currentColumn, keyInfo.KeyChar.ToString()); - lines[currentLine] = line; + string defaultLineText = lines[currentLine]; + string defaultModifiedLine = defaultLineText.Insert(currentColumn, keyInfo.KeyChar.ToString()); + lines[currentLine] = defaultModifiedLine; currentColumn++; } break; @@ -244,15 +252,36 @@ namespace CMLeonOS Console.BackgroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.White; - // 简单的弹窗显示 - Console.WriteLine("+----------------------------+"); - Console.WriteLine("| Save File? |"); - Console.WriteLine("+----------------------------+"); - Console.WriteLine("| Do you want to save the |"); - Console.WriteLine("| changes? |"); - Console.WriteLine("+----------------------------+"); - Console.WriteLine("| Y = Yes, N = No |"); - Console.WriteLine("+----------------------------+"); + // 获取控制台窗口大小 + int consoleWidth = 80; // 假设控制台宽度为80 + int consoleHeight = 25; // 假设控制台高度为25 + + // 弹窗内容 + string[] popupLines = { + "+----------------------------+", + "| Save File? |", + "+----------------------------+", + "| Do you want to save the |", + "| changes? |", + "+----------------------------+", + "| Y = Yes, N = No |", + "+----------------------------+" + }; + + // 计算弹窗宽度和高度 + int popupWidth = 30; // 弹窗宽度 + int popupHeight = popupLines.Length; // 弹窗高度 + + // 计算弹窗在屏幕中的位置(居中) + int popupX = (consoleWidth - popupWidth) / 2; + int popupY = (consoleHeight - popupHeight) / 2; + + // 显示弹窗 + for (int i = 0; i < popupLines.Length; i++) + { + Console.SetCursorPosition(popupX, popupY + i); + Console.WriteLine(popupLines[i]); + } // 等待用户输入 while (true) diff --git a/Kernel.cs b/Kernel.cs index 7fca771..d096efd 100644 --- a/Kernel.cs +++ b/Kernel.cs @@ -74,7 +74,7 @@ namespace CMLeonOS } // 登录成功后,初始化Shell - shell = new Shell(); + shell = new Shell(userSystem); // 检查并执行启动脚本 ExecuteStartupScript(); diff --git a/Shell.cs b/Shell.cs index fe88cf7..0e57841 100644 --- a/Shell.cs +++ b/Shell.cs @@ -13,10 +13,10 @@ namespace CMLeonOS private UserSystem userSystem; private bool fixMode; - public Shell() + public Shell(UserSystem userSystem) { + this.userSystem = userSystem; fileSystem = new FileSystem(); - userSystem = new UserSystem(); fixMode = Kernel.FixMode; } @@ -89,56 +89,135 @@ namespace CMLeonOS Sys.Power.Shutdown(); break; case "help": - Console.WriteLine("Available commands:"); - Console.WriteLine(" echo - Display text (supports \\n for newline)"); - Console.WriteLine(" clear/cls - Clear the screen"); - Console.WriteLine(" restart - Restart the system"); - Console.WriteLine(" shutdown - Shutdown the system"); - Console.WriteLine(" time - Display current time"); - Console.WriteLine(" date - Display current date"); - Console.WriteLine(" prompt - Change command prompt"); - Console.WriteLine(" calc - Simple calculator"); - Console.WriteLine(" history - Show command history"); - Console.WriteLine(" background - Change background color"); - Console.WriteLine(" cuitest - Test CUI framework"); - Console.WriteLine(" edit - Simple code editor"); - Console.WriteLine(" ls - List files and directories"); - Console.WriteLine(" cd - Change directory"); - Console.WriteLine(" cd .. - Go to parent directory"); - Console.WriteLine(" cd dir1/dir2/dir3 - Go to numbered directory"); - Console.WriteLine(" pwd - Show current directory"); - Console.WriteLine(" mkdir - Create directory"); - Console.WriteLine(" rm - Remove file"); - Console.WriteLine(" Use -norisk to delete files in sys folder"); - Console.WriteLine(" rmdir - Remove directory"); - Console.WriteLine(" cat - Display file content"); - Console.WriteLine(" echo > - Write text to file"); - Console.WriteLine(" head - Display first lines of file"); - Console.WriteLine(" Usage: head "); - Console.WriteLine(" tail - Display last lines of file"); - Console.WriteLine(" Usage: tail "); - Console.WriteLine(" wc - Count lines, words, characters"); - Console.WriteLine(" cp - Copy file"); - Console.WriteLine(" mv - Move/rename file"); - Console.WriteLine(" touch - Create empty file"); - Console.WriteLine(" find - Find file"); - Console.WriteLine(" getdisk - Show disk information"); - Console.WriteLine(" user - User management"); - Console.WriteLine(" user add admin - Add admin user"); - Console.WriteLine(" user add user - Add regular user"); - Console.WriteLine(" user delete - Delete user"); - Console.WriteLine(" user list - List all users"); - Console.WriteLine(" cpass - Change password"); - Console.WriteLine(" beep - Play beep sound"); - Console.WriteLine(" branswe - Execute Branswe code file"); - Console.WriteLine(" version - Show OS version"); - Console.WriteLine(" about - Show about information"); - Console.WriteLine(" help - Show this help message"); - Console.WriteLine(); - Console.WriteLine("Startup Script: sys\\startup.cm"); - Console.WriteLine(" Commands in this file will be executed on startup"); - Console.WriteLine(" Each line should contain one command"); - Console.WriteLine(" Lines starting with # are treated as comments"); + // 分页显示帮助信息 + string[] helpArgs = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + int pageNumber = 1; + bool showAll = false; + + if (helpArgs.Length > 0) + { + if (helpArgs[0].ToLower() == "all") + { + showAll = true; + } + else if (int.TryParse(helpArgs[0], out int page)) + { + pageNumber = page; + } + } + + string[] allCommands = { + " echo - Display text (supports \\n for newline)", + " clear/cls - Clear the screen", + " restart - Restart the system", + " shutdown - Shutdown the system", + " time - Display current time", + " date - Display current date", + " prompt - Change command prompt", + " calc - Simple calculator", + " history - Show command history", + " background - Change background color", + " cuitest - Test CUI framework", + " edit - Simple code editor", + " Tab key inserts 4 spaces", + " ls - List files and directories", + " cd - Change directory", + " cd .. - Go to parent directory", + " cd dir1/dir2/dir3 - Go to numbered directory", + " pwd - Show current directory", + " mkdir - Create directory", + " rm - Remove file", + " Use -norisk to delete files in sys folder", + " rmdir - Remove directory", + " cat - Display file content", + " echo > - Write text to file", + " head - Display first lines of file", + " Usage: head ", + " tail - Display last lines of file", + " Usage: tail ", + " wc - Count lines, words, characters", + " cp - Copy file", + " mv - Move/rename file", + " touch - Create empty file", + " find - Find file", + " getdisk - Show disk information", + " user - User management", + " user add admin - Add admin user", + " user add user - Add regular user", + " user delete - Delete user", + " user list - List all users", + " cpass - Change password", + " beep - Play beep sound", + " branswe - Execute Branswe code file", + " version - Show OS version", + " about - Show about information", + " help - Show help page (1-3)", + " help all - Show all help pages", + "", + "Startup Script: sys\\startup.cm", + " Commands in this file will be executed on startup", + " Each line should contain one command", + " Lines starting with # are treated as comments" + }; + + int commandsPerPage = 15; + int totalPages = (int)Math.Ceiling((double)allCommands.Length / commandsPerPage); + + if (showAll) + { + Console.WriteLine("===================================="); + Console.WriteLine(" Help - All Pages"); + Console.WriteLine("===================================="); + Console.WriteLine(); + + foreach (var cmd in allCommands) + { + Console.WriteLine(cmd); + } + + Console.WriteLine(); + Console.WriteLine("-- End of help --"); + } + else + { + if (pageNumber > totalPages) + { + pageNumber = totalPages; + } + if (pageNumber < 1) + { + pageNumber = 1; + } + + int startIndex = (pageNumber - 1) * commandsPerPage; + int endIndex = Math.Min(startIndex + commandsPerPage, allCommands.Length); + + Console.WriteLine("===================================="); + Console.WriteLine($" Help - Page {pageNumber}/{totalPages}"); + Console.WriteLine("===================================="); + Console.WriteLine(); + + for (int i = startIndex; i < endIndex; i++) + { + Console.WriteLine(allCommands[i]); + } + + if (pageNumber < totalPages) + { + Console.WriteLine(); + Console.WriteLine($"-- More -- Type 'help {pageNumber + 1}' for next page or 'help all' for all pages --"); + } + else + { + Console.WriteLine(); + Console.WriteLine("-- End of help --"); + } + } + + // Console.WriteLine("Startup Script: sys\\startup.cm"); + // Console.WriteLine(" Commands in this file will be executed on startup"); + // Console.WriteLine(" Each line should contain one command"); + // Console.WriteLine(" Lines starting with # are treated as comments"); break; case "time": Console.WriteLine(DateTime.Now.ToString()); diff --git a/UserSystem.cs b/UserSystem.cs index 1b46158..052ccc7 100644 --- a/UserSystem.cs +++ b/UserSystem.cs @@ -17,6 +17,7 @@ namespace CMLeonOS private string userFilePath; private List users; public bool fixmode = Kernel.FixMode; + private User currentLoggedInUser; public UserSystem() { @@ -25,6 +26,9 @@ namespace CMLeonOS // 设置用户文件路径 userFilePath = Path.Combine(sysDirectory, "user.dat"); + // 初始化当前登录用户 + currentLoggedInUser = null; + // 加载用户数据 LoadUsers(); } @@ -304,9 +308,11 @@ namespace CMLeonOS if (foundUser.Password == password) { Console.WriteLine("Login successful!"); - Console.Beep(); + // 设置当前登录用户 + currentLoggedInUser = foundUser; + // 创建用户文件夹 CreateUserFolder(foundUser.Username); @@ -477,18 +483,8 @@ namespace CMLeonOS Console.Write("Please enter your current password: "); string currentPassword = ReadPassword(); - // 查找当前登录用户 - User currentUser = null; - foreach (User user in users) - { - if (user.Username.ToLower() == "current") - { - currentUser = user; - break; - } - } - - if (currentUser == null) + // 检查是否有用户登录 + if (currentLoggedInUser == null) { Console.WriteLine("Error: No user logged in."); return false; @@ -504,7 +500,7 @@ namespace CMLeonOS { try { - currentUser.Password = newPassword; + currentLoggedInUser.Password = newPassword; SaveUsers(); Console.WriteLine("Password changed successfully!"); return true;