finsh.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #else
  82. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt)
  83. #endif /* FINSH_USING_SYMTAB */
  84. /**
  85. * @brief Macro definitions to simplify the declaration of exported functions or commands.
  86. */
  87. #define __MSH_GET_MACRO(_1, _2, _3, _FUN, ...) _FUN
  88. #define __MSH_GET_EXPORT_MACRO(_1, _2, _3, _4, _FUN, ...) _FUN
  89. #define _MSH_FUNCTION_CMD2(a0, a1) \
  90. MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, 0)
  91. #define _MSH_FUNCTION_CMD2_OPT(a0, a1, a2) \
  92. MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, a0##_msh_options)
  93. #define _MSH_FUNCTION_EXPORT_CMD3(a0, a1, a2) \
  94. MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, 0)
  95. #define _MSH_FUNCTION_EXPORT_CMD3_OPT(a0, a1, a2, a3) \
  96. MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, a0##_msh_options)
  97. /**
  98. * @ingroup group_finsh
  99. *
  100. * @brief Exports a command to module shell.
  101. *
  102. * @b Parameters
  103. *
  104. * <tt>[in]</tt> @b command Name of the command.
  105. *
  106. * <tt>[in]</tt> @b desc Description of the command, which will show in help list.
  107. *
  108. * <tt>[in]</tt> @b opt This is an option, enter any content to enable option completion
  109. *
  110. * @note This macro can be used in two ways:
  111. * @code MSH_CMD_EXPORT(command, desc) @endcode
  112. * or
  113. * @code MSH_CMD_EXPORT(command, desc, opt) @endcode
  114. */
  115. #define MSH_CMD_EXPORT(...) \
  116. __MSH_GET_MACRO(__VA_ARGS__, _MSH_FUNCTION_CMD2_OPT, \
  117. _MSH_FUNCTION_CMD2)(__VA_ARGS__)
  118. /**
  119. * @ingroup group_finsh
  120. *
  121. * @brief Exports a command with alias to module shell.
  122. *
  123. * @b Parameters
  124. *
  125. * <tt>[in]</tt> @b command Name of the command.
  126. *
  127. * <tt>[in]</tt> @b alias Alias of the command.
  128. *
  129. * <tt>[in]</tt> @b desc Description of the command, which will show in help list.
  130. *
  131. * <tt>[in]</tt> @b opt An option, enter any content to enable option completion.
  132. *
  133. * @note This macro can be used in two ways:
  134. * @code #define MSH_CMD_EXPORT_ALIAS(command, alias, desc) @endcode
  135. * or
  136. * @code #define MSH_CMD_EXPORT_ALIAS(command, alias, desc, opt) @endcode
  137. */
  138. #define MSH_CMD_EXPORT_ALIAS(...) \
  139. __MSH_GET_EXPORT_MACRO(__VA_ARGS__, _MSH_FUNCTION_EXPORT_CMD3_OPT, \
  140. _MSH_FUNCTION_EXPORT_CMD3)(__VA_ARGS__)
  141. /* system call table */
  142. struct finsh_syscall
  143. {
  144. const char *name; /* the name of system call */
  145. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  146. const char *desc; /* description of system call */
  147. #endif
  148. #ifdef FINSH_USING_OPTION_COMPLETION
  149. struct msh_cmd_opt *opt;
  150. #endif
  151. syscall_func func; /* the function address of system call */
  152. };
  153. /* system call item */
  154. struct finsh_syscall_item
  155. {
  156. struct finsh_syscall_item *next; /* next item */
  157. struct finsh_syscall syscall; /* syscall */
  158. };
  159. #ifdef FINSH_USING_OPTION_COMPLETION
  160. typedef struct msh_cmd_opt
  161. {
  162. rt_uint32_t id;
  163. const char *name;
  164. const char *des;
  165. } msh_cmd_opt_t;
  166. /* Command options declaration and definition macros */
  167. /**
  168. * @brief Declares a static array of command options for a specific command.
  169. *
  170. * @param[in] command The command associated with these options.
  171. */
  172. #ifdef _MSC_VER
  173. #define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[16];
  174. #else
  175. #define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[];
  176. #endif
  177. /**
  178. * @brief Starts the definition of command options for a specific command.
  179. *
  180. * @param[in] command The command these options are associated with.
  181. */
  182. #ifdef _MSC_VER
  183. #define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[16] = {
  184. #else
  185. #define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[] = {
  186. #endif
  187. /**
  188. * @brief Defines a single command option.
  189. *
  190. * @param[in] _id Unique identifier for the option.
  191. * @param[in] _name The name of the option.
  192. * @param[in] _des Description of the option.
  193. */
  194. #define CMD_OPTIONS_NODE(_id, _name, _des) {.id = _id, .name = #_name, .des = #_des},
  195. /**
  196. * Marks the end of command options definition.
  197. */
  198. #define CMD_OPTIONS_NODE_END {0},};
  199. void msh_opt_list_dump(void *options);
  200. int msh_cmd_opt_id_get(int argc, char *argv[], void *options);
  201. #define MSH_OPT_ID_GET(fun) msh_cmd_opt_id_get(argc, argv, (void*) fun##_msh_options)
  202. #define MSH_OPT_DUMP(fun) msh_opt_list_dump((void*) fun##_msh_options)
  203. #else
  204. #define CMD_OPTIONS_STATEMENT(command)
  205. #define CMD_OPTIONS_NODE_START(command)
  206. #define CMD_OPTIONS_NODE(_id, _name, _des)
  207. #define CMD_OPTIONS_NODE_END
  208. #define MSH_OPT_ID_GET(fun) ((int)(-1UL))
  209. #define MSH_OPT_DUMP(fun) do{}while(0)
  210. #endif
  211. extern struct finsh_syscall_item *global_syscall_list;
  212. extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
  213. #if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
  214. struct finsh_syscall *finsh_syscall_next(struct finsh_syscall *call);
  215. #define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
  216. #else
  217. #define FINSH_NEXT_SYSCALL(index) index++
  218. #endif
  219. #if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
  220. void finsh_set_device(const char *device_name);
  221. #endif
  222. #endif