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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-03-22 Bernard first version
*/
#ifndef __FINSH_H__
#define __FINSH_H__
#include <rtdef.h>
#ifdef _MSC_VER
#pragma section("FSymTab$f",read)
#endif /* _MSC_VER */
#ifdef FINSH_USING_OPTION_COMPLETION
#define FINSH_COND(opt) opt,
#else
#define FINSH_COND(opt)
#endif
#ifdef FINSH_USING_DESCRIPTION
#define FINSH_DESC(cmd, desc) __fsym_##cmd##_desc,
#else
#define FINSH_DESC(cmd, desc)
#endif
typedef long (*syscall_func)(void);
#ifdef FINSH_USING_SYMTAB
#ifdef __TI_COMPILER_VERSION__
#define __TI_FINSH_EXPORT_FUNCTION(f) PRAGMA(DATA_SECTION(f,"FSymTab"))
#endif /* __TI_COMPILER_VERSION__ */
/**
* @brief Macro to export a command along with its name, description, and options to the symbol table in MSVC.
*
* @param[in] name The function name associated with the command.
* @param[in] cmd The command name.
* @param[in] desc The description of the command.
* @param[in] opt The options associated with the command, used for option completion.
*/
#ifdef _MSC_VER
#define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
const char __fsym_##cmd##_name[] = #cmd; \
const char __fsym_##cmd##_desc[] = #desc; \
__declspec(allocate("FSymTab$f")) \
const struct finsh_syscall __fsym_##cmd = \
{ \
__fsym_##cmd##_name, \
FINSH_DESC(cmd, desc) \
FINSH_COND(opt) \
(syscall_func)&name \
};
#pragma comment(linker, "/merge:FSymTab=mytext")
#elif defined(__TI_COMPILER_VERSION__)
#ifdef __TMS320C28XX__
#define RT_NOBLOCKED __attribute__((noblocked))
#else
#define RT_NOBLOCKED
#endif
#define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
__TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
const char __fsym_##cmd##_name[] = #cmd; \
const char __fsym_##cmd##_desc[] = #desc; \
rt_used RT_NOBLOCKED const struct finsh_syscall __fsym_##cmd = \
{ \
__fsym_##cmd##_name, \
FINSH_DESC(cmd, desc) \
FINSH_COND(opt) \
(syscall_func)&name \
};
#else
#define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
const char __fsym_##cmd##_name[] rt_section(".rodata.name") = #cmd; \
const char __fsym_##cmd##_desc[] rt_section(".rodata.name") = #desc; \
rt_used const struct finsh_syscall __fsym_##cmd rt_section("FSymTab")= \
{ \
__fsym_##cmd##_name, \
FINSH_DESC(cmd, desc) \
FINSH_COND(opt) \
(syscall_func)&name \
};
#endif /* _MSC_VER */
#else
#define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt)
#endif /* FINSH_USING_SYMTAB */
/**
* @brief Macro definitions to simplify the declaration of exported functions or commands.
*/
#define __MSH_GET_MACRO(_1, _2, _3, _FUN, ...) _FUN
#define __MSH_GET_EXPORT_MACRO(_1, _2, _3, _4, _FUN, ...) _FUN
#define _MSH_FUNCTION_CMD2(a0, a1) \
MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, 0)
#define _MSH_FUNCTION_CMD2_OPT(a0, a1, a2) \
MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, a0##_msh_options)
#define _MSH_FUNCTION_EXPORT_CMD3(a0, a1, a2) \
MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, 0)
#define _MSH_FUNCTION_EXPORT_CMD3_OPT(a0, a1, a2, a3) \
MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, a0##_msh_options)
/**
* @ingroup group_finsh
*
* @brief This macro exports a system function to finsh shell.
*
* @param[in] name Name of function.
* @param[in] desc Description of function, which will show in help.
*/
#define FINSH_FUNCTION_EXPORT(name, desc)
/**
* @ingroup group_finsh
*
* @brief Exports a system function with an alias name to finsh shell.
*
* @param[in] name Name of function.
* @param[in] alias Alias name of function.
* @param[in] desc Description of function, which will show in help.
*/
#define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
/**
* @ingroup group_finsh
*
* @brief Exports a command to module shell.
*
* @b Parameters
*
* <tt>[in]</tt> @b command Name of the command.
*
* <tt>[in]</tt> @b desc Description of the command, which will show in help list.
*
* <tt>[in]</tt> @b opt This is an option, enter any content to enable option completion
*
* @note This macro can be used in two ways:
* @code MSH_CMD_EXPORT(command, desc) @endcode
* or
* @code MSH_CMD_EXPORT(command, desc, opt) @endcode
*/
#define MSH_CMD_EXPORT(...) \
__MSH_GET_MACRO(__VA_ARGS__, _MSH_FUNCTION_CMD2_OPT, \
_MSH_FUNCTION_CMD2)(__VA_ARGS__)
/**
* @ingroup group_finsh
*
* @brief Exports a command with alias to module shell.
*
* @b Parameters
*
* <tt>[in]</tt> @b command Name of the command.
*
* <tt>[in]</tt> @b alias Alias of the command.
*
* <tt>[in]</tt> @b desc Description of the command, which will show in help list.
*
* <tt>[in]</tt> @b opt An option, enter any content to enable option completion.
*
* @note This macro can be used in two ways:
* @code #define MSH_CMD_EXPORT_ALIAS(command, alias, desc) @endcode
* or
* @code #define MSH_CMD_EXPORT_ALIAS(command, alias, desc, opt) @endcode
*/
#define MSH_CMD_EXPORT_ALIAS(...) \
__MSH_GET_EXPORT_MACRO(__VA_ARGS__, _MSH_FUNCTION_EXPORT_CMD3_OPT, \
_MSH_FUNCTION_EXPORT_CMD3)(__VA_ARGS__)
/* system call table */
struct finsh_syscall
{
const char *name; /* the name of system call */
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
const char *desc; /* description of system call */
#endif
#ifdef FINSH_USING_OPTION_COMPLETION
struct msh_cmd_opt *opt;
#endif
syscall_func func; /* the function address of system call */
};
/* system call item */
struct finsh_syscall_item
{
struct finsh_syscall_item *next; /* next item */
struct finsh_syscall syscall; /* syscall */
};
#ifdef FINSH_USING_OPTION_COMPLETION
typedef struct msh_cmd_opt
{
rt_uint32_t id;
const char *name;
const char *des;
} msh_cmd_opt_t;
/* Command options declaration and definition macros */
/**
* @brief Declares a static array of command options for a specific command.
*
* @param[in] command The command associated with these options.
*/
#ifdef _MSC_VER
#define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[16];
#else
#define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[];
#endif
/**
* @brief Starts the definition of command options for a specific command.
*
* @param[in] command The command these options are associated with.
*/
#ifdef _MSC_VER
#define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[16] = {
#else
#define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[] = {
#endif
/**
* @brief Defines a single command option.
*
* @param[in] _id Unique identifier for the option.
* @param[in] _name The name of the option.
* @param[in] _des Description of the option.
*/
#define CMD_OPTIONS_NODE(_id, _name, _des) {.id = _id, .name = #_name, .des = #_des},
/**
* Marks the end of command options definition.
*/
#define CMD_OPTIONS_NODE_END {0},};
void msh_opt_list_dump(void *options);
int msh_cmd_opt_id_get(int argc, char *argv[], void *options);
#define MSH_OPT_ID_GET(fun) msh_cmd_opt_id_get(argc, argv, (void*) fun##_msh_options)
#define MSH_OPT_DUMP(fun) msh_opt_list_dump((void*) fun##_msh_options)
#else
#define CMD_OPTIONS_STATEMENT(command)
#define CMD_OPTIONS_NODE_START(command)
#define CMD_OPTIONS_NODE(_id, _name, _des)
#define CMD_OPTIONS_NODE_END
#define MSH_OPT_ID_GET(fun) ((int)(-1UL))
#define MSH_OPT_DUMP(fun) do{}while(0)
#endif
extern struct finsh_syscall_item *global_syscall_list;
extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
#if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
struct finsh_syscall *finsh_syscall_next(struct finsh_syscall *call);
#define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
#else
#define FINSH_NEXT_SYSCALL(index) index++
#endif
#if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
void finsh_set_device(const char *device_name);
#endif
#endif
|