修复部分bug

This commit is contained in:
2026-02-01 15:21:17 +08:00
parent b8cabfd1c0
commit 3fb0697045
6 changed files with 637 additions and 88 deletions

View File

@@ -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 <filename>");
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 <text>");
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 <dirname>");
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 <filename>");
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 <dirname>");
break;
}
try
{
Directory.Delete(parts[1]);
Console.WriteLine($"Directory deleted: {parts[1]}");
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting directory: {ex.Message}");
}
break;
}
// default:
// {
// // û<><C3BB>ƥ<EFBFBD><C6A5>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ִ<EFBFBD><D6B4>
// Branswe.Run(line);
// break;
// }
}
}
}

282
Branswe.md Normal file
View File

@@ -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
```
## 完整示例
### 示例1Hello 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兼容性支持
- 完善了错误处理
- 添加了文件操作支持

View File

@@ -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)

View File

@@ -74,7 +74,7 @@ namespace CMLeonOS
}
// 登录成功后初始化Shell
shell = new Shell();
shell = new Shell(userSystem);
// 检查并执行启动脚本
ExecuteStartupScript();

183
Shell.cs
View File

@@ -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 <text> - 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 <text> - Change command prompt");
Console.WriteLine(" calc <expr> - Simple calculator");
Console.WriteLine(" history - Show command history");
Console.WriteLine(" background <hex> - Change background color");
Console.WriteLine(" cuitest - Test CUI framework");
Console.WriteLine(" edit <file> - Simple code editor");
Console.WriteLine(" ls <dir> - List files and directories");
Console.WriteLine(" cd <dir> - 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 <dir> - Create directory");
Console.WriteLine(" rm <file> - Remove file");
Console.WriteLine(" Use -norisk to delete files in sys folder");
Console.WriteLine(" rmdir <dir> - Remove directory");
Console.WriteLine(" cat <file> - Display file content");
Console.WriteLine(" echo <text> > <file> - Write text to file");
Console.WriteLine(" head <file> - Display first lines of file");
Console.WriteLine(" Usage: head <file> <lines>");
Console.WriteLine(" tail <file> - Display last lines of file");
Console.WriteLine(" Usage: tail <file> <lines>");
Console.WriteLine(" wc <file> - Count lines, words, characters");
Console.WriteLine(" cp <src> <dst> - Copy file");
Console.WriteLine(" mv <src> <dst> - Move/rename file");
Console.WriteLine(" touch <file> - Create empty file");
Console.WriteLine(" find <name> - Find file");
Console.WriteLine(" getdisk - Show disk information");
Console.WriteLine(" user <cmd> - User management");
Console.WriteLine(" user add admin <username> <password> - Add admin user");
Console.WriteLine(" user add user <username> <password> - Add regular user");
Console.WriteLine(" user delete <username> - Delete user");
Console.WriteLine(" user list - List all users");
Console.WriteLine(" cpass - Change password");
Console.WriteLine(" beep - Play beep sound");
Console.WriteLine(" branswe <filename> - 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 <text> - 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 <text> - Change command prompt",
" calc <expr> - Simple calculator",
" history - Show command history",
" background <hex> - Change background color",
" cuitest - Test CUI framework",
" edit <file> - Simple code editor",
" Tab key inserts 4 spaces",
" ls <dir> - List files and directories",
" cd <dir> - Change directory",
" cd .. - Go to parent directory",
" cd dir1/dir2/dir3 - Go to numbered directory",
" pwd - Show current directory",
" mkdir <dir> - Create directory",
" rm <file> - Remove file",
" Use -norisk to delete files in sys folder",
" rmdir <dir> - Remove directory",
" cat <file> - Display file content",
" echo <text> > <file> - Write text to file",
" head <file> - Display first lines of file",
" Usage: head <file> <lines>",
" tail <file> - Display last lines of file",
" Usage: tail <file> <lines>",
" wc <file> - Count lines, words, characters",
" cp <src> <dst> - Copy file",
" mv <src> <dst> - Move/rename file",
" touch <file> - Create empty file",
" find <name> - Find file",
" getdisk - Show disk information",
" user <cmd> - User management",
" user add admin <username> <password> - Add admin user",
" user add user <username> <password> - Add regular user",
" user delete <username> - Delete user",
" user list - List all users",
" cpass - Change password",
" beep - Play beep sound",
" branswe <filename> - Execute Branswe code file",
" version - Show OS version",
" about - Show about information",
" help <page> - 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());

View File

@@ -17,6 +17,7 @@ namespace CMLeonOS
private string userFilePath;
private List<User> 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;