forked from loa/freertos-cli
/
shell.c
170 lines (140 loc) · 3.55 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "shell.h"
typedef struct xCOMMAND_LIST_ITEM
{
shellCommand * command;
struct xCOMMAND_LIST_ITEM * next;
} xCommandListItem;
static void helpCommand(xQueueHandle *outputQueue, char *line);
void echoCommand(xQueueHandle *outputQueue, char *line);
static shellCommand xHelpCommand =
{
(char *) "help",
(char *) "help: Lists all available command\n\r",
&helpCommand
};
static xCommandListItem xRegisteredCommands =
{
&xHelpCommand,
NULL
};
void initShell()
{
shellCommand * cmd = (shellCommand *) pvPortMalloc(sizeof(shellCommand));
cmd->name = "echo";
cmd->help = "echo: Echo text\n\r";
cmd->func = &echoCommand;
registerShellCommand(cmd);
}
void registerShellCommand(shellCommand * newCommand)
{
xCommandListItem * item = &xRegisteredCommands;
xCommandListItem * newItem = (xCommandListItem *) pvPortMalloc(sizeof(xCommandListItem));
taskENTER_CRITICAL();
// Get to the last item in list
while (item->next != NULL)
{
item = item->next;
}
// Create list item
newItem->command = newCommand;
newItem->next = NULL;
// Add item to list
item->next = newItem;
taskEXIT_CRITICAL();
}
void runShellCommand(xQueueHandle *outputQueue, char * line)
{
xCommandListItem * item = &xRegisteredCommands;
char * strNotFound = "Command not found, use \"help\" to list available commands.\n\r\3";
int len;
while (item != NULL)
{
len = strlen(item->command->name);
// Either line contains only the command or followed by space
if (strncmp(line, item->command->name, len) == 0 && (len == strlen(line) || line[len] == ' '))
{
(*item->command->func)(outputQueue, line);
break;
}
item = item->next;
}
if (item == NULL)
{
// Command was not found
if(xQueueSendToBack(*outputQueue, (void *) strNotFound, (portTickType) 100) != pdPASS)
{
sciDebug("ERROR %s:%d\n\r", __FUNCTION__, __LINE__);
}
}
}
void helpCommand(xQueueHandle *outputQueue, char *line)
{
char str[81];
int str_i, help_i, found_end;
xCommandListItem * item = &xRegisteredCommands;
// Set last char as NUL
str[80] = '\0';
// Print all command names
while (item != NULL)
{
if (strlen(item->command->help) < 81)
{
if(xQueueSendToBack(*outputQueue, (void *) item->command->help, (portTickType) 100) != pdPASS)
{
sciDebug("ERROR %s:%d\n\r", __FUNCTION__, __LINE__);
}
}
else
{
help_i = 0;
found_end = 0;
// Help text longer than 81 chars, split it up
while (found_end == 0)
{
// Get next partition of the string
for (str_i = 0; str_i < 80; str_i++)
{
str[str_i] = item->command->help[help_i++];
if (str[str_i] == '\0')
{
found_end = 1;
break;
}
}
// Print partition
if(xQueueSendToBack(*outputQueue, (void *) str, (portTickType) 100) != pdPASS)
{
sciDebug("ERROR %s:%d\n\r", __FUNCTION__, __LINE__);
}
}
}
item = item->next;
}
// Send End-of-Text
sprintf(str, "\3");
if(xQueueSendToBack(*outputQueue, (void *) str, (portTickType) 100) != pdPASS)
{
sciDebug("ERROR %s:%d\n\r", __FUNCTION__, __LINE__);
}
}
void echoCommand(xQueueHandle *outputQueue, char *line)
{
char str[81] = "\n\r\3";
int i = findchar(line, ' ');
// Incase space after "echo" isnt found or there are no text
if (i == -1 || line[i+1] == '\0')
{
if(xQueueSendToBack(*outputQueue, (void *) str, (portTickType) 100) != pdPASS)
{
sciDebug("ERROR %s:%d\n\r", __FUNCTION__, __LINE__);
}
}
else
{
sprintf(str, "%s\n\r\3", &line[i+1]);
if(xQueueSendToBack(*outputQueue, (void *) str, (portTickType) 100) != pdPASS)
{
sciDebug("ERROR %s:%d\n\r", __FUNCTION__, __LINE__);
}
}
}