2023-12-02 14:36:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @file shell_port.c
|
|
|
|
|
|
* @author Letter (NevermindZZT@gmail.com)
|
|
|
|
|
|
* @brief
|
|
|
|
|
|
* @version 0.1
|
|
|
|
|
|
* @date 2019-02-22
|
|
|
|
|
|
*
|
|
|
|
|
|
* @copyright (c) 2019 Letter
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "shell.h"
|
2023-12-02 23:15:33 +08:00
|
|
|
|
#include "shell_ext.h"
|
|
|
|
|
|
#include "shell_fs.h"
|
|
|
|
|
|
#include "shell_passthrough.h"
|
|
|
|
|
|
#include "shell_secure_user.h"
|
|
|
|
|
|
#include "log.h"
|
|
|
|
|
|
// #include "telnetd.h"
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
#include <pthread.h>
|
2025-03-09 00:21:55 +08:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
|
#include <sys/stat.h>
|
2023-12-02 14:36:49 +08:00
|
|
|
|
#include "cons_cli.h"
|
|
|
|
|
|
#include "u_sleep.h"
|
2025-01-06 19:07:10 +08:00
|
|
|
|
#include "u_sema.h"
|
|
|
|
|
|
#include "u_task.h"
|
2023-12-02 23:15:33 +08:00
|
|
|
|
static Shell shell;
|
|
|
|
|
|
static ShellFs shellFs;
|
2025-01-03 18:29:25 +08:00
|
|
|
|
static char shellBuffer[512];
|
2023-12-03 17:54:26 +08:00
|
|
|
|
// static char shellPathBuffer[128] = "/";
|
2023-12-02 14:36:49 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 用户shell写
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param data 数据
|
|
|
|
|
|
* @param len 数据长度
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return unsigned short 写入实际长度
|
|
|
|
|
|
*/
|
|
|
|
|
|
signed short userShellWrite(char *data, unsigned short len)
|
2025-03-09 00:21:55 +08:00
|
|
|
|
{ return write(STDOUT_FILENO, data, len);
|
2023-12-02 14:36:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief 用户shell读
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param data 数据
|
|
|
|
|
|
* @param len 数据长度
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return unsigned short 读取实际长度
|
|
|
|
|
|
*/
|
|
|
|
|
|
signed short userShellRead(char *data, unsigned short len)
|
|
|
|
|
|
{
|
2024-11-02 21:49:13 +08:00
|
|
|
|
int rlen;
|
|
|
|
|
|
|
2025-03-09 00:21:55 +08:00
|
|
|
|
rlen = read(STDIN_FILENO, data, len);
|
2024-11-02 21:49:13 +08:00
|
|
|
|
return rlen;
|
2023-12-02 14:36:49 +08:00
|
|
|
|
}
|
2023-12-02 23:15:33 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 列出文件
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param path 路径
|
|
|
|
|
|
* @param buffer 结果缓冲
|
|
|
|
|
|
* @param maxLen 最大缓冲长度
|
|
|
|
|
|
* @return size_t 0
|
|
|
|
|
|
*/
|
|
|
|
|
|
size_t userShellListDir(char *path, char *buffer, size_t maxLen)
|
|
|
|
|
|
{
|
|
|
|
|
|
DIR *dir;
|
|
|
|
|
|
struct dirent *ptr;
|
|
|
|
|
|
int i;
|
2023-12-02 14:36:49 +08:00
|
|
|
|
|
2023-12-02 23:15:33 +08:00
|
|
|
|
if (!path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
dir = opendir(path);
|
|
|
|
|
|
if (!dir)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
memset(buffer, 0, maxLen);
|
|
|
|
|
|
while ((ptr = readdir(dir)) != NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
strcat(buffer, ptr->d_name);
|
|
|
|
|
|
strcat(buffer, "\t");
|
|
|
|
|
|
}
|
|
|
|
|
|
closedir(dir);
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2025-03-09 00:21:55 +08:00
|
|
|
|
static struct termios old_settings;
|
|
|
|
|
|
static struct termios new_settings;
|
|
|
|
|
|
|
|
|
|
|
|
void tty_set_raw_mode(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
new_settings = old_settings;
|
|
|
|
|
|
new_settings.c_lflag &= ~(ICANON | ECHO); // 禁用规范模式和回显
|
|
|
|
|
|
new_settings.c_cc[VMIN] = 1; // 读取的最小字符数
|
|
|
|
|
|
new_settings.c_cc[VTIME] = 0; // 读取的超时时间(以10ms为单位)
|
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &new_settings);
|
|
|
|
|
|
}
|
|
|
|
|
|
void tty_set_normal_mode(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &old_settings);
|
|
|
|
|
|
}
|
2023-12-02 14:36:49 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 用户shell初始化
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
void userShellInit(void)
|
|
|
|
|
|
{
|
2025-03-09 00:21:55 +08:00
|
|
|
|
tcgetattr(STDIN_FILENO, &old_settings);
|
|
|
|
|
|
tty_set_raw_mode();
|
2024-07-30 09:37:49 +08:00
|
|
|
|
task_set_obj_name(TASK_THIS, TASK_THIS, "tk_shell");
|
|
|
|
|
|
task_set_obj_name(TASK_THIS, THREAD_MAIN, "th_shell");
|
2023-12-03 17:54:26 +08:00
|
|
|
|
// shellFs.getcwd = getcwd;
|
|
|
|
|
|
// shellFs.chdir = chdir;
|
|
|
|
|
|
// shellFs.listdir = userShellListDir;
|
|
|
|
|
|
// shellFsInit(&shellFs, shellPathBuffer, sizeof(shellPathBuffer));
|
2023-12-02 14:36:49 +08:00
|
|
|
|
shell.write = userShellWrite;
|
|
|
|
|
|
shell.read = userShellRead;
|
2023-12-03 17:54:26 +08:00
|
|
|
|
// shellSetPath(&shell, shellPathBuffer);
|
2023-12-02 23:15:33 +08:00
|
|
|
|
shellInit(&shell, shellBuffer, sizeof(shellBuffer));
|
2023-12-03 17:54:26 +08:00
|
|
|
|
// shellCompanionAdd(&shell, SHELL_COMPANION_ID_FS, &shellFs);
|
2023-12-02 14:36:49 +08:00
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
|
{
|
|
|
|
|
|
shellTask(&shell);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|