2023-12-03 22:47:29 +08:00
|
|
|
#include "cons_cli.h"
|
2023-12-10 10:20:05 +08:00
|
|
|
#include "pm_cli.h"
|
2025-03-02 23:31:22 +08:00
|
|
|
#include "shell.h"
|
|
|
|
|
#include "u_malloc.h"
|
|
|
|
|
#include "u_sys.h"
|
|
|
|
|
#include "unistd.h"
|
2023-12-03 17:54:26 +08:00
|
|
|
#include <dirent.h>
|
2025-03-02 23:31:22 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <pthread.h>
|
2023-12-03 17:54:26 +08:00
|
|
|
#include <stddef.h>
|
2025-03-02 23:31:22 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2023-12-03 17:54:26 +08:00
|
|
|
#include <string.h>
|
2025-01-08 20:21:09 +08:00
|
|
|
#include <sys/stat.h>
|
2025-03-02 23:31:22 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <unistd.h>
|
2025-03-09 00:21:55 +08:00
|
|
|
#include <fcntl.h>
|
2023-12-03 17:54:26 +08:00
|
|
|
int ls(int argc, char *agrv[])
|
|
|
|
|
{
|
|
|
|
|
DIR *dir;
|
|
|
|
|
struct dirent *ptr;
|
|
|
|
|
int i;
|
2025-02-25 23:59:56 +08:00
|
|
|
char *path;
|
|
|
|
|
char *in_path;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2023-12-03 17:54:26 +08:00
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
2025-03-02 23:31:22 +08:00
|
|
|
in_path = "";
|
2025-02-25 23:59:56 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
in_path = agrv[1];
|
2023-12-03 17:54:26 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 23:59:56 +08:00
|
|
|
path = u_malloc(PAGE_SIZE);
|
|
|
|
|
if (path == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
2025-03-11 17:03:30 +08:00
|
|
|
ret = getcwd(path, PAGE_SIZE);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
u_free(path);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
ret = chdir(in_path);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
u_free(path);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
dir = opendir(".");
|
2023-12-03 17:54:26 +08:00
|
|
|
if (!dir)
|
|
|
|
|
{
|
2025-02-25 23:59:56 +08:00
|
|
|
u_free(path);
|
2023-12-03 17:54:26 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2025-03-11 17:03:30 +08:00
|
|
|
printf("%-20s%10s\n", "name", "size");
|
|
|
|
|
|
2023-12-03 17:54:26 +08:00
|
|
|
while ((ptr = readdir(dir)) != NULL)
|
|
|
|
|
{
|
2025-01-08 20:21:09 +08:00
|
|
|
struct stat st = {0};
|
2025-03-11 17:03:30 +08:00
|
|
|
|
|
|
|
|
ret = stat(ptr->d_name, &st);
|
|
|
|
|
printf("%-20s%10d\n", ptr->d_name, st.st_size);
|
2023-12-03 17:54:26 +08:00
|
|
|
}
|
|
|
|
|
closedir(dir);
|
2025-03-11 17:03:30 +08:00
|
|
|
chdir(path);
|
2025-02-25 23:59:56 +08:00
|
|
|
u_free(path);
|
2023-12-03 17:54:26 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), ls, ls, ls command);
|
2025-01-25 15:29:06 +08:00
|
|
|
|
|
|
|
|
int rm(int argc, char *agrv[])
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = unlink(agrv[1]);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), rm, rm, rm command);
|
2025-02-25 23:59:56 +08:00
|
|
|
int cd(int argc, char *agrv[])
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = chdir(agrv[1]);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), cd, cd, cd command);
|
2025-03-02 23:31:22 +08:00
|
|
|
int pwd(int argc, char *agrv[])
|
|
|
|
|
{
|
|
|
|
|
char *pwd;
|
|
|
|
|
char path[64];
|
|
|
|
|
|
|
|
|
|
pwd = getcwd(path, sizeof(path));
|
|
|
|
|
printf("%s\n", pwd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), pwd, pwd, pwd command);
|
2023-12-03 22:47:29 +08:00
|
|
|
int cat(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
if (argc != 2)
|
|
|
|
|
{
|
|
|
|
|
return (-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
if ((fp = fopen(argv[1], "r")) == NULL)
|
|
|
|
|
{
|
2023-12-09 23:11:55 +08:00
|
|
|
return errno;
|
2023-12-03 22:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while ((c = fgetc(fp)) != EOF)
|
|
|
|
|
{
|
2025-03-09 00:21:55 +08:00
|
|
|
write(STDOUT_FILENO, (uint8_t *)&c, 1);
|
2023-12-03 22:47:29 +08:00
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), cat, cat, cat command);
|
2025-01-08 20:21:09 +08:00
|
|
|
int hex(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
if (argc != 2)
|
|
|
|
|
{
|
|
|
|
|
return (-1);
|
|
|
|
|
}
|
2023-12-03 22:47:29 +08:00
|
|
|
|
2025-01-08 20:21:09 +08:00
|
|
|
FILE *fp;
|
|
|
|
|
char c;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if ((fp = fopen(argv[1], "rb")) == NULL)
|
|
|
|
|
{
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-25 15:29:06 +08:00
|
|
|
while ((ret = fread(&c, 1, 1, fp)) == 1)
|
2025-01-08 20:21:09 +08:00
|
|
|
{
|
|
|
|
|
printf("%02x ", c);
|
|
|
|
|
i++;
|
|
|
|
|
if (i % 16 == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("\nsize:%dB\n", i);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), hex, hex, hex command);
|
2023-12-10 10:20:05 +08:00
|
|
|
int kill(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int pid = atoi(argv[1]);
|
|
|
|
|
|
2025-03-02 23:31:22 +08:00
|
|
|
return pm_kill_task(pid, PM_KILL_TASK_ALL, 0);
|
2023-12-10 23:51:39 +08:00
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), kill, kill, kill command);
|
|
|
|
|
|
|
|
|
|
int shell_symlink(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
if (argc < 3)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
printf("%s %s %s\n", __func__, argv[1], argv[2]);
|
|
|
|
|
return symlink(argv[1], argv[2]);
|
2023-12-10 10:20:05 +08:00
|
|
|
}
|
2023-12-10 23:51:39 +08:00
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), symlink, shell_symlink, symlink command);
|
2025-03-09 00:21:55 +08:00
|
|
|
|
2025-03-02 23:31:22 +08:00
|
|
|
int shell_touch(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int fd;
|
2024-01-05 23:59:48 +08:00
|
|
|
|
2025-03-02 23:31:22 +08:00
|
|
|
fd = open(argv[1], O_CREAT, 0777);
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), touch, shell_touch, touch command);
|
|
|
|
|
int shell_mkdir(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
if (argc < 2)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = mkdir(argv[1], 0777);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), mkdir, shell_mkdir, mkdir command);
|
2024-01-05 23:59:48 +08:00
|
|
|
int shell_reboot(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
printf("sys reboot.\n");
|
|
|
|
|
sys_reboot(SYS_PROT);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), reboot, shell_reboot, reboot command);
|
|
|
|
|
|
|
|
|
|
int shell_mem_info(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
size_t total;
|
|
|
|
|
size_t free;
|
|
|
|
|
|
2025-01-06 19:07:10 +08:00
|
|
|
sys_mem_info(SYS_PROT, (umword_t *)&total, (umword_t *)&free);
|
2024-01-05 23:59:48 +08:00
|
|
|
printf("sys mem:\ntotal:%dB\nfree:%dB\n", total, free);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-02-05 14:44:49 +08:00
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), free, shell_mem_info, free command);
|
|
|
|
|
|
|
|
|
|
int shell_sys_info(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
size_t total;
|
|
|
|
|
size_t free;
|
|
|
|
|
|
|
|
|
|
printf("sys:\n");
|
2025-03-11 17:03:30 +08:00
|
|
|
printf("\tcpu usage:%d.%d\n", sys_read_cpu_usage() / 10, sys_read_cpu_usage() % 10);
|
2025-02-05 14:44:49 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), sys, shell_sys_info, sys command);
|
2025-03-09 18:18:46 +08:00
|
|
|
|
|
|
|
|
int shell_exit(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
exit(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), exit, exit, exit command);
|