finsh.h 7.2 KB

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