nr_micro_shell.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @file nr_micro_shell.h
  3. * @author Ji Youzhou
  4. * @version V0.1
  5. * @date 28 Oct 2019
  6. * @brief [brief]
  7. * *****************************************************************************
  8. * @attention
  9. *
  10. * MIT License
  11. *
  12. * Copyright (C) 2019 Ji Youzhou. or its affiliates. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in all
  22. * copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /* Define to prevent recursive inclusion -------------------------------------*/
  33. #ifndef __nr_micro_shell_h
  34. #define __nr_micro_shell_h
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #endif
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stdio.h"
  41. #include "nr_micro_shell_config.h"
  42. #include "ansi.h"
  43. #ifndef shell_printf
  44. #define shell_printf(fmt, args...) printf(fmt, ##args);
  45. #endif
  46. typedef void (*shell_fun_t)(char, char *);
  47. typedef struct static_cmd_function_struct
  48. {
  49. char cmd[NR_SHELL_CMD_NAME_MAX_LENGTH];
  50. void (*fp)(char argc, char *argv);
  51. char *description;
  52. } static_cmd_st;
  53. typedef struct shell_history_queue_struct
  54. {
  55. unsigned short int fp;
  56. unsigned short int rp;
  57. unsigned short int len;
  58. unsigned short int index;
  59. unsigned short int store_front;
  60. unsigned short int store_rear;
  61. unsigned short int store_num;
  62. char queue[NR_SHELL_MAX_CMD_HISTORY_NUM + 1];
  63. char buf[NR_SHELL_CMD_HISTORY_BUF_LENGTH + 1];
  64. } shell_his_queue_st;
  65. typedef struct nr_shell
  66. {
  67. char user_name[NR_SHELL_USER_NAME_MAX_LENGTH];
  68. const static_cmd_st *static_cmd;
  69. shell_his_queue_st cmd_his;
  70. } shell_st;
  71. void _shell_init(shell_st *shell);
  72. void shell_parser(shell_st *shell, char *str);
  73. char *shell_cmd_complete(shell_st *shell, char *str);
  74. void shell_his_queue_init(shell_his_queue_st *queue);
  75. void shell_his_queue_add_cmd(shell_his_queue_st *queue, char *str);
  76. unsigned short int shell_his_queue_search_cmd(shell_his_queue_st *queue, char *str);
  77. void shell_his_copy_queue_item(shell_his_queue_st *queue, unsigned short i, char *str_buf);
  78. extern shell_st nr_shell;
  79. #define shell_init() \
  80. { \
  81. ansi_init(&nr_ansi); \
  82. _shell_init(&nr_shell); \
  83. }
  84. #if NR_SHELL_END_OF_LINE == 1
  85. #define NR_SHELL_END_CHAR '\r'
  86. #else
  87. #define NR_SHELL_END_CHAR '\n'
  88. #endif
  89. #if NR_SHELL_END_OF_LINE == 0
  90. #define NR_SHELL_NEXT_LINE "\n"
  91. #endif
  92. #if NR_SHELL_END_OF_LINE == 1
  93. #define NR_SHELL_NEXT_LINE "\r\n"
  94. #endif
  95. #if NR_SHELL_END_OF_LINE == 2
  96. #define NR_SHELL_NEXT_LINE "\r\n"
  97. #endif
  98. #define shell(c) \
  99. { \
  100. if (ansi_get_char(c, &nr_ansi) == NR_SHELL_END_CHAR) \
  101. { \
  102. shell_parser(&nr_shell, nr_ansi.current_line); \
  103. ansi_clear_current_line(&nr_ansi); \
  104. } \
  105. }
  106. #ifdef USING_RT_THREAD
  107. int rt_nr_shell_system_init(void);
  108. #endif
  109. #define NR_USED __attribute__((used))
  110. #define NR_SECTION(x) __attribute__((section(".rodata.nr_shell_cmd" x)))
  111. #define NR_SHELL_CMD_EXPORT_START(cmd, func) \
  112. NR_USED const static_cmd_st _nr_cmd_start_ NR_SECTION("0.end") = {#cmd, NULL}
  113. #define NR_SHELL_CMD_EXPORT(cmd, func) \
  114. NR_USED const static_cmd_st _nr_cmd_##cmd NR_SECTION("1") = {#cmd, func}
  115. #define NR_SHELL_CMD_EXPORT_END(cmd, func) \
  116. NR_USED const static_cmd_st _nr_cmd_end_ NR_SECTION("1.end") = {#cmd, NULL}
  117. #ifdef NR_SHELL_USING_EXPORT_CMD
  118. extern const static_cmd_st _nr_cmd_start_;
  119. #define nr_cmd_start_add (&_nr_cmd_start_+1)
  120. #else
  121. extern const static_cmd_st static_cmd[];
  122. #define nr_cmd_start_add (&static_cmd[0])
  123. #endif
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif
  128. /******************* (C) COPYRIGHT 2019 Ji Youzhou *****END OF FILE*****************/