-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.c
200 lines (169 loc) · 3.98 KB
/
cli.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* mµOS - my micro OS
*
* Copyright (C)
* 2019 Christian Thäter <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef MUOS_CLI
#include <muos/cli.h>
#include <muos/serial.h>
#include <muos/io.h>
#include <stdlib.h>
#include <string.h>
#define COMMAND(name, ...) static const char __flash cli_cmd_##name##_str[] = #name;
CLI_COMMANDS
#undef COMMAND
const char __flash * const __flash muos_cli_cmd_names[] =
{
#define COMMAND(name, ...) cli_cmd_##name##_str,
CLI_COMMANDS
#undef COMMAND
};
//PLANNED: prompt
//PLANNED: parameter/prototype description for automatic parsing
//PLANNED: use # as comment line
void
MUOS_CLI_NOCMD (const char*);
void
muos_cli (const char* line)
{
//FIXME: hardcoded hw for cli
if (muos_serial_status[0].lineedit_echo)
muos_output_nl (0); //FIXME: linedit should support prompt/newline
line += strspn_P (line, MUOS_COALESCE_PSTR(MUOS_CLI_DELIM));
#ifdef MUOS_CLI_COMMENT
if (strncmp_P (line, MUOS_COALESCE_PSTR(MUOS_CLI_COMMENT), sizeof(MUOS_CLI_COMMENT)-1) == 0)
return;
#endif
uint8_t len = strcspn_P (line, MUOS_COALESCE_PSTR(MUOS_CLI_DELIM));
const char* cont = line + len;
cont += strspn_P (cont, MUOS_COALESCE_PSTR(MUOS_CLI_DELIM));
if (len)
{
enum muos_cli_cmd_id id;
for (id = 0; id < CLI_CMD_MAX_ID; ++id)
{
if (strncmp_P (line, muos_cli_cmd_names[id], len) == 0)
break;
}
if (id == CLI_CMD_MAX_ID)
{
MUOS_CLI_NOCMD (line);
return;
}
command_impl[id] (cont);
}
}
int32_t
muos_cli_arg_int32 (const char** cont)
{
if (*cont)
{
char* end;
int32_t r = strtol(*cont, &end, 0);
if (end == *cont)
{
*cont = NULL;
}
else
{
*cont = end;
*cont += strspn_P (*cont, MUOS_COALESCE_PSTR(MUOS_CLI_DELIM));
}
return r;
}
return 0;
}
uint16_t
muos_cli_arg_uint16 (const char** cont)
{
int32_t r = muos_cli_arg_int32 (cont);
if (*cont)
{
if (r < 0 || r > UINT16_MAX)
{
*cont = NULL;
}
}
return r;
}
int16_t
muos_cli_arg_int16 (const char** cont)
{
int32_t r = muos_cli_arg_int32 (cont);
if (*cont)
{
if (r < INT16_MIN || r > INT16_MAX)
{
*cont = NULL;
}
}
return r;
}
uint8_t
muos_cli_arg_uint8 (const char** cont)
{
int32_t r = muos_cli_arg_int32 (cont);
if (*cont)
{
if (r < 0 || r > UINT8_MAX)
{
*cont = NULL;
}
}
return r;
}
int8_t
muos_cli_arg_int8 (const char** cont)
{
int32_t r = muos_cli_arg_int32 (cont);
if (*cont)
{
if (r < INT8_MIN || r > INT8_MAX)
{
*cont = NULL;
}
}
return r;
}
void
muos_cli_arg_strncpy (const char** cont, char* dest, size_t len, const char* delim)
{
if (*cont)
{
size_t slen = strcspn_P (*cont, delim);
if (slen < len)
{
if (dest)
{
strncpy (dest, *cont, slen);
dest[slen] = '\0';
}
*cont += slen;
*cont += strspn_P (*cont, delim);
}
else if (dest)
{
*dest = '\0';
*cont = NULL;
}
}
}
//size_t
//muos_cli_arg_strlen (const char** cont, const char* delim);
//void
//muos_cli_arg_skip (const char** cont, const char* delim);
#endif