finsh.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * Macro to export a command along with its name, description, and options to the symbol table in MSVC.
  33. * @param name The function name associated with the command.
  34. * @param cmd The command name.
  35. * @param desc The description of the command.
  36. * @param opt The options associated with the command, used for option completion.
  37. */
  38. #ifdef _MSC_VER
  39. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
  40. const char __fsym_##cmd##_name[] = #cmd; \
  41. const char __fsym_##cmd##_desc[] = #desc; \
  42. __declspec(allocate("FSymTab$f")) \
  43. const struct finsh_syscall __fsym_##cmd = \
  44. { \
  45. __fsym_##cmd##_name, \
  46. FINSH_DESC(cmd, desc) \
  47. FINSH_COND(opt) \
  48. (syscall_func)&name \
  49. };
  50. #pragma comment(linker, "/merge:FSymTab=mytext")
  51. #elif defined(__TI_COMPILER_VERSION__)
  52. #ifdef __TMS320C28XX__
  53. #define RT_NOBLOCKED __attribute__((noblocked))
  54. #else
  55. #define RT_NOBLOCKED
  56. #endif
  57. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
  58. __TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
  59. const char __fsym_##cmd##_name[] = #cmd; \
  60. const char __fsym_##cmd##_desc[] = #desc; \
  61. rt_used RT_NOBLOCKED const struct finsh_syscall __fsym_##cmd = \
  62. { \
  63. __fsym_##cmd##_name, \
  64. FINSH_DESC(cmd, desc) \
  65. FINSH_COND(opt) \
  66. (syscall_func)&name \
  67. };
  68. #else
  69. #define MSH_FUNCTION_EXPORT_CMD(name, cmd, desc, opt) \
  70. const char __fsym_##cmd##_name[] rt_section(".rodata.name") = #cmd; \
  71. const char __fsym_##cmd##_desc[] rt_section(".rodata.name") = #desc; \
  72. rt_used const struct finsh_syscall __fsym_##cmd rt_section("FSymTab")= \
  73. { \
  74. __fsym_##cmd##_name, \
  75. FINSH_DESC(cmd, desc) \
  76. FINSH_COND(opt) \
  77. (syscall_func)&name \
  78. };
  79. #endif /* _MSC_VER */
  80. #endif /* FINSH_USING_SYMTAB */
  81. /**
  82. * Macro definitions to simplify the declaration of exported functions or commands.
  83. */
  84. #define __MSH_GET_MACRO(_1, _2, _3, _FUN, ...) _FUN
  85. #define __MSH_GET_EXPORT_MACRO(_1, _2, _3, _4, _FUN, ...) _FUN
  86. #define _MSH_FUNCTION_CMD2(a0, a1) \
  87. MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, 0)
  88. #define _MSH_FUNCTION_CMD2_OPT(a0, a1, a2) \
  89. MSH_FUNCTION_EXPORT_CMD(a0, a0, a1, a0##_msh_options)
  90. #define _MSH_FUNCTION_EXPORT_CMD3(a0, a1, a2) \
  91. MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, 0)
  92. #define _MSH_FUNCTION_EXPORT_CMD3_OPT(a0, a1, a2, a3) \
  93. MSH_FUNCTION_EXPORT_CMD(a0, a1, a2, a0##_msh_options)
  94. /**
  95. * @ingroup finsh
  96. *
  97. * This macro exports a system function to finsh shell.
  98. *
  99. * @param name the name of function.
  100. * @param desc the description of function, which will show in help.
  101. */
  102. #define FINSH_FUNCTION_EXPORT(name, desc)
  103. /**
  104. * @ingroup finsh
  105. *
  106. * This macro exports a system function with an alias name to finsh shell.
  107. *
  108. * @param name the name of function.
  109. * @param alias the alias name of function.
  110. * @param desc the description of function, which will show in help.
  111. */
  112. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
  113. /**
  114. * @ingroup msh
  115. *
  116. * This macro exports a command to module shell.
  117. *
  118. * @param command is the name of the command.
  119. * @param desc is the description of the command, which will show in help list.
  120. * @param opt This is an option, enter any content to enable option completion
  121. */
  122. /* MSH_CMD_EXPORT(command, desc) or MSH_CMD_EXPORT(command, desc, opt) */
  123. #define MSH_CMD_EXPORT(...) \
  124. __MSH_GET_MACRO(__VA_ARGS__, _MSH_FUNCTION_CMD2_OPT, \
  125. _MSH_FUNCTION_CMD2)(__VA_ARGS__)
  126. /**
  127. * @ingroup msh
  128. *
  129. * This macro exports a command with alias to module shell.
  130. *
  131. * @param command is the name of the command.
  132. * @param alias is the alias of the command.
  133. * @param desc is the description of the command, which will show in help list.
  134. * @param opt This is an option, enter any content to enable option completion
  135. * @note
  136. * #define MSH_CMD_EXPORT_ALIAS(command, alias, desc) or
  137. * #define MSH_CMD_EXPORT_ALIAS(command, alias, desc, opt)
  138. */
  139. #define MSH_CMD_EXPORT_ALIAS(...) \
  140. __MSH_GET_EXPORT_MACRO(__VA_ARGS__, _MSH_FUNCTION_EXPORT_CMD3_OPT, \
  141. _MSH_FUNCTION_EXPORT_CMD3)(__VA_ARGS__)
  142. /* system call table */
  143. struct finsh_syscall
  144. {
  145. const char *name; /* the name of system call */
  146. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  147. const char *desc; /* description of system call */
  148. #endif
  149. #ifdef FINSH_USING_OPTION_COMPLETION
  150. struct msh_cmd_opt *opt;
  151. #endif
  152. syscall_func func; /* the function address of system call */
  153. };
  154. /* system call item */
  155. struct finsh_syscall_item
  156. {
  157. struct finsh_syscall_item *next; /* next item */
  158. struct finsh_syscall syscall; /* syscall */
  159. };
  160. #ifdef FINSH_USING_OPTION_COMPLETION
  161. typedef struct msh_cmd_opt
  162. {
  163. rt_uint32_t id;
  164. const char *name;
  165. const char *des;
  166. } msh_cmd_opt_t;
  167. /* Command options declaration and definition macros */
  168. /**
  169. * Declares a static array of command options for a specific command.
  170. * @param command The command associated with these options.
  171. */
  172. #define CMD_OPTIONS_STATEMENT(command) static struct msh_cmd_opt command##_msh_options[];
  173. /**
  174. * Starts the definition of command options for a specific command.
  175. * @param command The command these options are associated with.
  176. */
  177. #define CMD_OPTIONS_NODE_START(command) static struct msh_cmd_opt command##_msh_options[] = {
  178. /**
  179. * Defines a single command option.
  180. * @param _id Unique identifier for the option.
  181. * @param _name The name of the option.
  182. * @param _des Description of the option.
  183. */
  184. #define CMD_OPTIONS_NODE(_id, _name, _des) {.id = _id, .name = #_name, .des = #_des},
  185. /**
  186. * Marks the end of command options definition.
  187. */
  188. #define CMD_OPTIONS_NODE_END {0},};
  189. void msh_opt_list_dump(void *options);
  190. int msh_cmd_opt_id_get(int argc, char *argv[], void *options);
  191. #define MSH_OPT_ID_GET(fun) msh_cmd_opt_id_get(argc, argv, (void*) fun##_msh_options)
  192. #define MSH_OPT_DUMP(fun) msh_opt_list_dump((void*) fun##_msh_options)
  193. #else
  194. #define CMD_OPTIONS_STATEMENT(command)
  195. #define CMD_OPTIONS_NODE_START(command)
  196. #define CMD_OPTIONS_NODE(_id, _name, _des)
  197. #define CMD_OPTIONS_NODE_END
  198. #define MSH_OPT_ID_GET(fun) ((int)(-1UL))
  199. #define MSH_OPT_DUMP(fun) do{}while(0)
  200. #endif
  201. extern struct finsh_syscall_item *global_syscall_list;
  202. extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
  203. #if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
  204. struct finsh_syscall *finsh_syscall_next(struct finsh_syscall *call);
  205. #define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
  206. #else
  207. #define FINSH_NEXT_SYSCALL(index) index++
  208. #endif
  209. #if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
  210. void finsh_set_device(const char *device_name);
  211. #endif
  212. #endif