finsh.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-03-22 Bernard first version
  9. */
  10. #ifndef __FINSH_H__
  11. #define __FINSH_H__
  12. #include <rtdef.h>
  13. #ifdef _MSC_VER
  14. #pragma section("FSymTab$f",read)
  15. #endif /* _MSC_VER */
  16. #ifdef FINSH_USING_OPTION_COMPLETION
  17. #define FINSH_COND(opt) opt,
  18. #else
  19. #define FINSH_COND(opt)
  20. #endif
  21. #ifdef FINSH_USING_DESCRIPTION
  22. #define FINSH_DESC(cmd, desc) __fsym_##cmd##_desc,
  23. #else
  24. #define FINSH_DESC(cmd, desc)
  25. #endif
  26. typedef long (*syscall_func)(void);
  27. #ifdef FINSH_USING_SYMTAB
  28. #ifdef __TI_COMPILER_VERSION__
  29. #define __TI_FINSH_EXPORT_FUNCTION(f) PRAGMA(DATA_SECTION(f,"FSymTab"))
  30. #endif /* __TI_COMPILER_VERSION__ */
  31. /**
  32. * @brief Macro to export a command along with its name, description, and options to the symbol table in MSVC.
  33. *
  34. * @param[in] name The function name associated with the command.
  35. * @param[in] cmd The command name.
  36. * @param[in] desc The description of the command.
  37. * @param[in] opt The options associated with the command, used for option completion.
  38. */
  39. #ifdef _MSC_VER
  40. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
  41. const char __fsym_##cmd##_name[] = #cmd; \
  42. const char __fsym_##cmd##_desc[] = #desc; \
  43. __declspec(allocate("FSymTab$f")) \
  44. const struct finsh_syscall __fsym_##cmd = \
  45. { \
  46. __fsym_##cmd##_name, \
  47. FINSH_DESC(cmd, desc) \
  48. FINSH_COND(opt) \
  49. (syscall_func)&name \
  50. };
  51. #pragma comment(linker, "/merge:FSymTab=mytext")
  52. #elif defined(__TI_COMPILER_VERSION__)
  53. #ifdef __TMS320C28XX__
  54. #define RT_NOBLOCKED __attribute__((noblocked))
  55. #else
  56. #define RT_NOBLOCKED
  57. #endif
  58. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
  59. __TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
  60. const char __fsym_##cmd##_name[] = #cmd; \
  61. const char __fsym_##cmd##_desc[] = #desc; \
  62. rt_used RT_NOBLOCKED const struct finsh_syscall __fsym_##cmd = \
  63. { \
  64. __fsym_##cmd##_name, \
  65. FINSH_DESC(cmd, desc) \
  66. FINSH_COND(opt) \
  67. (syscall_func)&name \
  68. };
  69. #else
  70. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
  71. const char __fsym_##cmd##_name[] rt_section(".rodata.name") = #cmd; \
  72. const char __fsym_##cmd##_desc[] rt_section(".rodata.name") = #desc; \
  73. rt_used const struct finsh_syscall __fsym_##cmd rt_section("FSymTab")= \
  74. { \
  75. __fsym_##cmd##_name, \
  76. FINSH_DESC(cmd, desc) \
  77. FINSH_COND(opt) \
  78. (syscall_func)&name \
  79. };
  80. #endif /* _MSC_VER */
  81. #endif /* FINSH_USING_SYMTAB */
  82. /**
  83. * @brief Macro definitions to simplify the declaration of exported functions or commands.
  84. */
  85. #define __MSH_GET_MACRO(_1, _2, _3, _FUN, ...) _FUN
  86. #define __MSH_GET_EXPORT_MACRO(_1, _2, _3, _4, _FUN, ...) _FUN
  87. #define _MSH_FUNCTION_CMD2(a0, a1) \
  88. MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, 0)
  89. #define _MSH_FUNCTION_CMD2_OPT(a0, a1, a2) \
  90. MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, a0##_msh_options)
  91. #define _MSH_FUNCTION_EXPORT_CMD3(a0, a1, a2) \
  92. MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, 0)
  93. #define _MSH_FUNCTION_EXPORT_CMD3_OPT(a0, a1, a2, a3) \
  94. MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, a0##_msh_options)
  95. /**
  96. * @ingroup group_finsh
  97. *
  98. * @brief This macro exports a system function to finsh shell.
  99. *
  100. * @param[in] name Name of function.
  101. * @param[in] desc Description of function, which will show in help.
  102. */
  103. #define FINSH_FUNCTION_EXPORT(name, desc)
  104. /**
  105. * @ingroup group_finsh
  106. *
  107. * @brief Exports a system function with an alias name to finsh shell.
  108. *
  109. * @param[in] name Name of function.
  110. * @param[in] alias Alias name of function.
  111. * @param[in] desc Description of function, which will show in help.
  112. */
  113. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
  114. /**
  115. * @ingroup group_finsh
  116. *
  117. * @brief Exports a command to module shell.
  118. *
  119. * @b Parameters
  120. *
  121. * <tt>[in]</tt> @b command Name of the command.
  122. *
  123. * <tt>[in]</tt> @b desc Description of the command, which will show in help list.
  124. *
  125. * <tt>[in]</tt> @b opt This is an option, enter any content to enable option completion
  126. *
  127. * @note This macro can be used in two ways:
  128. * @code MSH_CMD_EXPORT(command, desc) @endcode
  129. * or
  130. * @code MSH_CMD_EXPORT(command, desc, opt) @endcode
  131. */
  132. #define MSH_CMD_EXPORT(...) \
  133. __MSH_GET_MACRO(__VA_ARGS__, _MSH_FUNCTION_CMD2_OPT, \
  134. _MSH_FUNCTION_CMD2)(__VA_ARGS__)
  135. /**
  136. * @ingroup group_finsh
  137. *
  138. * @brief Exports a command with alias to module shell.
  139. *
  140. * @b Parameters
  141. *
  142. * <tt>[in]</tt> @b command Name of the command.
  143. *
  144. * <tt>[in]</tt> @b alias Alias of the command.
  145. *
  146. * <tt>[in]</tt> @b desc Description of the command, which will show in help list.
  147. *
  148. * <tt>[in]</tt> @b opt An option, enter any content to enable option completion.
  149. *
  150. * @note This macro can be used in two ways:
  151. * @code #define MSH_CMD_EXPORT_ALIAS(command, alias, desc) @endcode
  152. * or
  153. * @code #define MSH_CMD_EXPORT_ALIAS(command, alias, desc, opt) @endcode
  154. */
  155. #define MSH_CMD_EXPORT_ALIAS(...) \
  156. __MSH_GET_EXPORT_MACRO(__VA_ARGS__, _MSH_FUNCTION_EXPORT_CMD3_OPT, \
  157. _MSH_FUNCTION_EXPORT_CMD3)(__VA_ARGS__)
  158. /* system call table */
  159. struct finsh_syscall
  160. {
  161. const char *name; /* the name of system call */
  162. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  163. const char *desc; /* description of system call */
  164. #endif
  165. #ifdef FINSH_USING_OPTION_COMPLETION
  166. struct msh_cmd_opt *opt;
  167. #endif
  168. syscall_func func; /* the function address of system call */
  169. };
  170. /* system call item */
  171. struct finsh_syscall_item
  172. {
  173. struct finsh_syscall_item *next; /* next item */
  174. struct finsh_syscall syscall; /* syscall */
  175. };
  176. #ifdef FINSH_USING_OPTION_COMPLETION
  177. typedef struct msh_cmd_opt
  178. {
  179. rt_uint32_t id;
  180. const char *name;
  181. const char *des;
  182. } msh_cmd_opt_t;
  183. /* Command options declaration and definition macros */
  184. /**
  185. * @brief Declares a static array of command options for a specific command.
  186. *
  187. * @param[in] command The command associated with these options.
  188. */
  189. #ifdef _MSC_VER
  190. #define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[16];
  191. #else
  192. #define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[];
  193. #endif
  194. /**
  195. * @brief Starts the definition of command options for a specific command.
  196. *
  197. * @param[in] command The command these options are associated with.
  198. */
  199. #ifdef _MSC_VER
  200. #define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[16] = {
  201. #else
  202. #define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[] = {
  203. #endif
  204. /**
  205. * @brief Defines a single command option.
  206. *
  207. * @param[in] _id Unique identifier for the option.
  208. * @param[in] _name The name of the option.
  209. * @param[in] _des Description of the option.
  210. */
  211. #define CMD_OPTIONS_NODE(_id, _name, _des) {.id = _id, .name = #_name, .des = #_des},
  212. /**
  213. * Marks the end of command options definition.
  214. */
  215. #define CMD_OPTIONS_NODE_END {0},};
  216. void msh_opt_list_dump(void *options);
  217. int msh_cmd_opt_id_get(int argc, char *argv[], void *options);
  218. #define MSH_OPT_ID_GET(fun) msh_cmd_opt_id_get(argc, argv, (void*) fun##_msh_options)
  219. #define MSH_OPT_DUMP(fun) msh_opt_list_dump((void*) fun##_msh_options)
  220. #else
  221. #define CMD_OPTIONS_STATEMENT(command)
  222. #define CMD_OPTIONS_NODE_START(command)
  223. #define CMD_OPTIONS_NODE(_id, _name, _des)
  224. #define CMD_OPTIONS_NODE_END
  225. #define MSH_OPT_ID_GET(fun) ((int)(-1UL))
  226. #define MSH_OPT_DUMP(fun) do{}while(0)
  227. #endif
  228. extern struct finsh_syscall_item *global_syscall_list;
  229. extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
  230. #if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
  231. struct finsh_syscall *finsh_syscall_next(struct finsh_syscall *call);
  232. #define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
  233. #else
  234. #define FINSH_NEXT_SYSCALL(index) index++
  235. #endif
  236. #if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
  237. void finsh_set_device(const char *device_name);
  238. #endif
  239. #endif