csh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2022, Egahp
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef CSH_H
  7. #define CSH_H
  8. #include <stdint.h>
  9. #include "csh_config.h"
  10. /*!< argument check */
  11. #ifndef CONFIG_CSH_DEBUG
  12. #define CONFIG_CSH_DEBUG 0
  13. #endif
  14. /*!< default row */
  15. #ifndef CONFIG_CSH_DFTROW
  16. #define CONFIG_CSH_DFTROW 25
  17. #endif
  18. /*!< default column */
  19. #ifndef CONFIG_CSH_DFTCOL
  20. #define CONFIG_CSH_DFTCOL 80
  21. #endif
  22. /*!< history support <+550byte> */
  23. #ifndef CONFIG_CSH_HISTORY
  24. #define CONFIG_CSH_HISTORY 1
  25. #endif
  26. /*!< completion support <+1100byte> */
  27. #ifndef CONFIG_CSH_COMPLETION
  28. #define CONFIG_CSH_COMPLETION 1
  29. #endif
  30. /*!< max completion item list count (use stack 4 x count byte) */
  31. #ifndef CONFIG_CSH_MAX_COMPLETION
  32. #define CONFIG_CSH_MAX_COMPLETION 48
  33. #endif
  34. /*!< prompt edit support <+1000byte> */
  35. #ifndef CONFIG_CSH_PROMPTEDIT
  36. #define CONFIG_CSH_PROMPTEDIT 1
  37. #endif
  38. /*!< prompt segment count */
  39. #ifndef CONFIG_CSH_PROMPTSEG
  40. #define CONFIG_CSH_PROMPTSEG 7
  41. #endif
  42. /*!< xterm support */
  43. #ifndef CONFIG_CSH_XTERM
  44. #define CONFIG_CSH_XTERM 0
  45. #endif
  46. /*!< newline */
  47. #ifndef CONFIG_CSH_NEWLINE
  48. #define CONFIG_CSH_NEWLINE "\r\n"
  49. #endif
  50. /*!< tab space count */
  51. #ifndef CONFIG_CSH_SPACE
  52. #define CONFIG_CSH_SPACE 4
  53. #endif
  54. /*!< independent ctrl map */
  55. #ifndef CONFIG_CSH_CTRLMAP
  56. #define CONFIG_CSH_CTRLMAP 0
  57. #endif
  58. /*!< independent alt map */
  59. #ifndef CONFIG_CSH_ALTMAP
  60. #define CONFIG_CSH_ALTMAP 0
  61. #endif
  62. /*!< refresh prompt */
  63. #ifndef CONFIG_CSH_REFRESH_PROMPT
  64. #define CONFIG_CSH_REFRESH_PROMPT 1
  65. #endif
  66. /*!< no waiting for sget */
  67. #ifndef CONFIG_CSH_NOBLOCK
  68. #define CONFIG_CSH_NOBLOCK 0
  69. #endif
  70. /*!< help information */
  71. #ifndef CONFIG_CSH_HELP
  72. #define CONFIG_CSH_HELP ""
  73. #endif
  74. /*!< path length 0:const path, <=255:variable path */
  75. #ifndef CONFIG_CSH_MAXLEN_PATH
  76. #define CONFIG_CSH_MAXLEN_PATH 128
  77. #endif
  78. /*!< path segment count */
  79. #ifndef CONFIG_CSH_MAXSEG_PATH
  80. #define CONFIG_CSH_MAXSEG_PATH 16
  81. #endif
  82. /*!< user count */
  83. #ifndef CONFIG_CSH_MAX_USER
  84. #define CONFIG_CSH_MAX_USER 1
  85. #endif
  86. /*!< max argument count */
  87. #ifndef CONFIG_CSH_MAX_ARG
  88. #define CONFIG_CSH_MAX_ARG 8
  89. #endif
  90. /*!< linebuffer static or on stack */
  91. #ifndef CONFIG_CSH_LNBUFF_STATIC
  92. #define CONFIG_CSH_LNBUFF_STATIC 1
  93. #endif
  94. /*!< linebuffer size (valid only if lnbuff on stack) */
  95. #ifndef CONFIG_CSH_LNBUFF_SIZE
  96. #define CONFIG_CSH_LNBUFF_SIZE 256
  97. #endif
  98. /*!< multi-thread mode */
  99. #ifndef CONFIG_CSH_MULTI_THREAD
  100. #define CONFIG_CSH_MULTI_THREAD 0
  101. #endif
  102. /*!< independent signal handler (for multi instances) */
  103. #ifndef CONFIG_CSH_SIGNAL_HANDLER
  104. #define CONFIG_CSH_SIGNAL_HANDLER 0
  105. #endif
  106. /*!< Ctrl+c/d/q/s/z/\ F1-F12 UE <+120byte> */
  107. #ifndef CONFIG_CSH_USER_CALLBACK
  108. #define CONFIG_CSH_USER_CALLBACK 1
  109. #endif
  110. /*!< enable macro export symbol table */
  111. #ifndef CONFIG_CSH_SYMTAB
  112. #define CONFIG_CSH_SYMTAB 1
  113. #endif
  114. /*!< print buffer size */
  115. #ifndef CONFIG_CSH_PRINT_BUFFER_SIZE
  116. #define CONFIG_CSH_PRINT_BUFFER_SIZE 512
  117. #endif
  118. #define CONFIG_READLINE_DEBUG CONFIG_CSH_DEBUG
  119. #define CONFIG_READLINE_DFTROW CONFIG_CSH_DFTROW
  120. #define CONFIG_READLINE_DFTCOL CONFIG_CSH_DFTCOL
  121. #define CONFIG_READLINE_HISTORY CONFIG_CSH_HISTORY
  122. #define CONFIG_READLINE_COMPLETION CONFIG_CSH_COMPLETION
  123. #define CONFIG_READLINE_MAX_COMPLETION CONFIG_CSH_MAX_COMPLETION
  124. #define CONFIG_READLINE_PROMPTEDIT CONFIG_CSH_PROMPTEDIT
  125. #define CONFIG_READLINE_PROMPTSEG CONFIG_CSH_PROMPTSEG
  126. #define CONFIG_READLINE_XTERM CONFIG_CSH_XTERM
  127. #define CONFIG_READLINE_NEWLINE CONFIG_CSH_NEWLINE
  128. #define CONFIG_READLINE_SPACE CONFIG_CSH_SPACE
  129. #define CONFIG_READLINE_CTRLMAP CONFIG_CSH_CTRLMAP
  130. #define CONFIG_READLINE_ALTMAP CONFIG_CSH_ALTMAP
  131. #define CONFIG_READLINE_REFRESH_PROMPT CONFIG_CSH_REFRESH_PROMPT
  132. #define CONFIG_READLINE_NOBLOCK CONFIG_CSH_NOBLOCK
  133. #define CONFIG_READLINE_HELP CONFIG_CSH_HELP
  134. #include "CherryRL/chry_readline.h"
  135. #include "chry_shell.h"
  136. #if defined(__CC_ARM) || defined(__CLANG_ARM) || defined(__GNUC__) || defined(__ADSPBLACKFIN__)
  137. #define __CSH_SECTION(x) __attribute__((section(x)))
  138. #define __CSH_USED __attribute__((used))
  139. #elif defined(__IAR_SYSTEMS_ICC__)
  140. #define __CSH_SECTION(x) @x
  141. #define __CSH_USED __root
  142. #else
  143. #define __CSH_SECTION(x)
  144. #define __CSH_USED
  145. #endif
  146. #if defined(CONFIG_CSH_SYMTAB) && CONFIG_CSH_SYMTAB
  147. #if defined(_MSC_VER)
  148. #pragma section("FSymTab$f", read)
  149. #pragma section("VSymTab", read)
  150. #endif
  151. #if defined(__TI_COMPILER_VERSION__)
  152. #defien __TI_CSH_PRAGMA(x) _Pragma(#x)
  153. #endif
  154. #endif
  155. #if defined(CONFIG_CSH_SYMTAB) && CONFIG_CSH_SYMTAB
  156. #if defined(_MSC_VER)
  157. #define CSH_EXPORT_CALL(name, func, path) \
  158. __declspec(allocate("FSymTab$f")) \
  159. const chry_syscall_t __fsym_##name##func = { \
  160. path, \
  161. #name, \
  162. (chry_syscall_func_t)func, \
  163. };
  164. #pragma comment(linker, "/merge:FSymTab=mytext")
  165. #define CSH_EXPORT_VAR(name, var, attr) \
  166. __declspec(allocate("VSymTab")) const chry_sysvar_t __vsym_##name##var = { \
  167. #name, \
  168. (void *)&var, \
  169. attr, \
  170. };
  171. #elif defined(__TI_COMPILER_VERSION__)
  172. #define CSH_EXPORT_CALL(name, func, path) \
  173. __TI_CSH_PRAGMA(DATA_SECTION(__fsym_##name##func, "FSymTab")); \
  174. __attribute__((used)) const chry_syscall_t __fsym_##name##func = { \
  175. path, \
  176. #name, \
  177. (chry_syscall_func_t)func, \
  178. };
  179. #define CSH_EXPORT_VAR(name, var, attr) \
  180. __TI_CSH_PRAGMA(DATA_SECTION(__vsym_##name##var, "VSymTab")); \
  181. __attribute__((used)) const chry_sysvar_t __vsym_##name##var = { \
  182. #name, \
  183. (void *)&var, \
  184. attr, \
  185. };
  186. #else
  187. #define CSH_EXPORT_CALL(name, func, path) \
  188. __CSH_USED const chry_syscall_t __fsym_##name##func __CSH_SECTION("FSymTab") = { \
  189. path, \
  190. #name, \
  191. (chry_syscall_func_t)func, \
  192. };
  193. #define CSH_EXPORT_VAR(name, var, attr) \
  194. __CSH_USED const chry_sysvar_t __vsym_##name##var __CSH_SECTION("VSymTab") = { \
  195. #name, \
  196. (void *)var, \
  197. attr, \
  198. };
  199. #endif
  200. #else
  201. #define CSH_EXPORT_CALL(name, func, path)
  202. #define CSH_EXPORT_VAR(name, var, attr)
  203. #endif
  204. /*****************************************************************************
  205. * @brief export csh command (default path "/bin")
  206. *
  207. * @param[in] func function pointer
  208. * @param[in] _dummy (compatible descriptions)
  209. *
  210. *****************************************************************************/
  211. #define CSH_CMD_EXPORT(func, _dummy) \
  212. CSH_EXPORT_CALL(func, func, "/bin")
  213. /*****************************************************************************
  214. * @brief export csh command with alias (default path "/bin" )
  215. *
  216. * @param[in] func function pointer
  217. * @param[in] name command name
  218. * @param[in] _dummy (compatible descriptions)
  219. *
  220. *****************************************************************************/
  221. #define CSH_CMD_EXPORT_ALIAS(func, name, _dummy) \
  222. CSH_EXPORT_CALL(name, func, "/bin")
  223. /*****************************************************************************
  224. * @brief export csh command (default path "/sbin")
  225. *
  226. * @param[in] func function pointer
  227. * @param[in] _dummy (compatible descriptions)
  228. *
  229. *****************************************************************************/
  230. #define CSH_SCMD_EXPORT(func, _dummy) \
  231. CSH_EXPORT_CALL(func, func, "/sbin")
  232. /*****************************************************************************
  233. * @brief export csh command with alias (default path "/sbin" )
  234. *
  235. * @param[in] func function pointer
  236. * @param[in] name command name
  237. * @param[in] _dummy (compatible descriptions)
  238. *
  239. *****************************************************************************/
  240. #define CSH_SCMD_EXPORT_ALIAS(func, name, _dummy) \
  241. CSH_EXPORT_CALL(name, func, "/sbin")
  242. /*****************************************************************************
  243. * @brief export csh command with path
  244. *
  245. * @param[in] func function pointer
  246. * @param[in] name command name
  247. * @param[in] path command path
  248. *
  249. * @example CSH_CMD_EXPORT_PATH(test_func1, test1, "/bin"); // ok
  250. * CSH_CMD_EXPORT_PATH(test_func2, test2, "/sbin/test"); // ok
  251. * CSH_CMD_EXPORT_PATH(test_func2, test2, "/sbin/test/");// error, cannot end with '/'
  252. * CSH_CMD_EXPORT_PATH(test_func2, test2, "/"); // error, cannot be the root path "/"
  253. *
  254. *****************************************************************************/
  255. #define CSH_CMD_EXPORT_PATH(func, name, path) \
  256. CSH_EXPORT_CALL(name, func, path)
  257. /*****************************************************************************
  258. * @brief export csh read-only variable
  259. *
  260. * @param[in] var variable
  261. * @param[in] name variable name
  262. * @param[in] size variable size
  263. *
  264. *****************************************************************************/
  265. #define CSH_RVAR_EXPORT(var, name, size) \
  266. CSH_EXPORT_VAR(name, var, 0x80000000 | (size))
  267. /*****************************************************************************
  268. * @brief export csh write-only variable
  269. *
  270. * @param[in] var variable
  271. * @param[in] name variable name
  272. * @param[in] size variable size
  273. *
  274. *****************************************************************************/
  275. #define CSH_WVAR_EXPORT(var, name, size) \
  276. CSH_EXPORT_VAR(name, var, 0x40000000 | (size))
  277. /*****************************************************************************
  278. * @brief export csh read-write variable
  279. *
  280. * @param[in] var variable
  281. * @param[in] name variable name
  282. * @param[in] size variable size
  283. *
  284. *****************************************************************************/
  285. #define CSH_RWVAR_EXPORT(var, name, size) \
  286. CSH_EXPORT_VAR(name, var, 0xc0000000 | (size))
  287. #endif