forked from embedded2014/freertos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
150 lines (124 loc) · 2.92 KB
/
shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "shell.h"
#include <stddef.h>
#include "clib.h"
#include <string.h>
#include "fio.h"
#include "filesystem.h"
#include "FreeRTOS.h"
#include "task.h"
#include "host.h"
typedef struct {
const char *name;
cmdfunc *fptr;
const char *desc;
} cmdlist;
void ls_command(int, char **);
void man_command(int, char **);
void cat_command(int, char **);
void ps_command(int, char **);
void host_command(int, char **);
void help_command(int, char **);
void host_command(int, char **);
void mmtest_command(int, char **);
void dumpsys_command(int, char **);
#define MKCL(n, d) {.name=#n, .fptr=n ## _command, .desc=d}
cmdlist cl[]={
MKCL(ls, "List directory"),
MKCL(man, "Show the manual of the command"),
MKCL(cat, "Concatenate files and print on the stdout"),
MKCL(ps, "Report a snapshot of the current processes"),
MKCL(host, "Run command on host"),
MKCL(mmtest, "heap memory allocation test"),
MKCL(help, "help"),
MKCL(dumpsys, "Dump task information")
};
int parse_command(char *str, char *argv[]){
int b_quote=0, b_dbquote=0;
int i;
int count=0, p=0;
for(i=0; str[i]; ++i){
if(str[i]=='\'')
++b_quote;
if(str[i]=='"')
++b_dbquote;
if(str[i]==' '&&b_quote%2==0&&b_dbquote%2==0){
str[i]='\0';
argv[count++]=&str[p];
p=i+1;
}
}
/* last one */
argv[count++]=&str[p];
return count;
}
void ls_command(int n, char *argv[]){
}
int filedump(const char *filename){
char buf[128];
int fd=fs_open(filename, 0, O_RDONLY);
if(fd==OPENFAIL)
return 0;
fio_printf(1, "\r\n");
int count;
while((count=fio_read(fd, buf, sizeof(buf)))>0){
fio_write(1, buf, count);
}
fio_close(fd);
return 1;
}
void ps_command(int n, char *argv[]){
signed char buf[1024];
vTaskList(buf);
fio_printf(1, "\r\n%s\r\n", buf);
}
void cat_command(int n, char *argv[]){
if(n==1){
fio_printf(2, "\r\nUsage: cat <filename>\r\n");
return;
}
if(!filedump(argv[1]))
fio_printf(2, "\r\n%s no such file or directory.\r\n", argv[1]);
}
void man_command(int n, char *argv[]){
if(n==1){
fio_printf(2, "\r\nUsage: man <command>\r\n");
return;
}
char buf[128]="/romfs/manual/";
strcat(buf, argv[1]);
if(!filedump(buf))
fio_printf(2, "\r\nManual not available.\r\n");
}
void host_command(int n, char *argv[]){
if(n>1){
int len=strlen(argv[1]), rnt;
if(argv[1][0]=='\''){
argv[1][len-1]='\0';
rnt=host_system(argv[1]+1);
}else
rnt=host_system(argv[1]);
fio_printf(1, "\r\nfinish with exit code %d.\r\n", rnt);
}else
fio_printf(2, "\r\nUsage: host 'command'\r\n");
}
void help_command(int n,char *argv[]){
int i;
fio_printf(1, "\r\n");
for(i=0;i<sizeof(cl)/sizeof(cl[0]); ++i){
fio_printf(1, "%s - %s\r\n", cl[i].name, cl[i].desc);
}
}
void dumpsys_command(int n, char *argv[]) {
if (n < 2) {
fio_printf(2, "Usage: %s [pid]", argv[0]);
return;
}
}
cmdfunc *do_command(const char *cmd){
int i;
for(i=0; i<sizeof(cl)/sizeof(cl[0]); ++i){
if(strcmp(cl[i].name, cmd)==0)
return cl[i].fptr;
}
return NULL;
}