chry_shell.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2022, Egahp
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef CHRY_SHELL_H
  7. #define CHRY_SHELL_H
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <stdint.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include "CherryRL/chry_readline.h"
  15. /*!< check maxlen */
  16. #if defined(CONFIG_CSH_MAXLEN_PATH) && CONFIG_CSH_MAXLEN_PATH
  17. #if CONFIG_CSH_MAXLEN_PATH > 255
  18. #error "CONFIG_CSH_MAXLEN_PATH cannot be greater than 255."
  19. #endif
  20. #endif
  21. /*!< check multi-thread and lnbuff static */
  22. #if (defined(CONFIG_CSH_MULTI_THREAD) && CONFIG_CSH_MULTI_THREAD)
  23. #if !defined(CONFIG_CSH_LNBUFF_STATIC) || (CONFIG_CSH_LNBUFF_STATIC == 0)
  24. #error "CONFIG_CSH_LNBUFF_STATIC and CONFIG_CSH_MULTI_THREAD cannot be enabled at the same time."
  25. #endif
  26. #endif
  27. #if ((defined(CONFIG_CSH_NOBLOCK) && CONFIG_CSH_NOBLOCK) && \
  28. (!defined(CONFIG_CSH_LNBUFF_STATIC) || (CONFIG_CSH_LNBUFF_STATIC == 0)))
  29. #error "CONFIG_CSH_LNBUFF_STATIC and CONFIG_CSH_NOBLOCK must be enabled at the same time."
  30. #endif
  31. /*!< signal count */
  32. #define CSH_SIGNAL_COUNT 7
  33. /*!< signal */
  34. #define CSH_SIGINT 1
  35. #define CSH_SIGQUIT 3
  36. #define CSH_SIGKILL 9
  37. #define CSH_SIGTERM 15
  38. #define CSH_SIGSTOP 17
  39. #define CSH_SIGTSTP 18
  40. #define CSH_SIGCONT 19
  41. #define CSH_VAR_READ 0x80000000
  42. #define CSH_VAR_WRITE 0x40000000
  43. #define CSH_VAR_SIZE 0x3fffffff
  44. #define CSH_SIG_DFL ((chry_sighandler_t)0) /* Default action */
  45. #define CSH_SIG_IGN ((chry_sighandler_t)1) /* Ignore action */
  46. #define CSH_SIG_ERR ((chry_sighandler_t)-1) /* Error return */
  47. /*!< exec status */
  48. #define CSH_STATUS_EXEC_IDLE 0
  49. #define CSH_STATUS_EXEC_FIND 1
  50. #define CSH_STATUS_EXEC_PREP 2
  51. #define CSH_STATUS_EXEC_DONE 3
  52. typedef int (*chry_syscall_func_t)(int argc, char **argv);
  53. typedef struct {
  54. const char *path;
  55. const char *name;
  56. chry_syscall_func_t func;
  57. } chry_syscall_t;
  58. typedef struct {
  59. const char *name;
  60. void *var;
  61. uint32_t attr;
  62. } chry_sysvar_t;
  63. typedef struct {
  64. uint32_t exec;
  65. /*!< readline section */
  66. #if defined(CONFIG_CSH_LNBUFF_STATIC) && CONFIG_CSH_LNBUFF_STATIC
  67. chry_readline_t rl; /*!< readline instance */
  68. char *linebuff; /*!< readline buffer */
  69. uint16_t buffsize; /*!< readline buffer size */
  70. uint16_t linesize; /*!< readline size */
  71. #else
  72. chry_readline_t rl; /*!< readline instance */
  73. /*!< (on stack) readline buffer */
  74. /*!< (on stack) readline buffer size */
  75. /*!< (on stack) readline size */
  76. #endif
  77. /*!< commmand table section */
  78. const chry_syscall_t *cmd_tbl_beg; /*!< command table begin */
  79. const chry_syscall_t *cmd_tbl_end; /*!< command table end */
  80. /*!< variable table section */
  81. const chry_sysvar_t *var_tbl_beg; /*!< variable table begin */
  82. const chry_sysvar_t *var_tbl_end; /*!< variable table end */
  83. /*!< execute section */
  84. #if defined(CONFIG_CSH_MULTI_THREAD) && CONFIG_CSH_MULTI_THREAD
  85. int exec_code; /*!< exec return code */
  86. int exec_argc; /*!< exec argument count */
  87. const char *exec_argv[CONFIG_CSH_MAX_ARG + 3]; /*!< exec argument value */
  88. #else
  89. int exec_code; /*!< exec return code */
  90. /*!< (on stack) exec argument count */
  91. /*!< (on stack) exec argument value */
  92. #endif
  93. /*!< user host and path section */
  94. #if defined(CONFIG_CSH_MAXLEN_PATH) && CONFIG_CSH_MAXLEN_PATH
  95. int uid; /*!< now user id */
  96. const char *host; /*!< host name */
  97. const char *user[CONFIG_CSH_MAX_USER]; /*!< user name */
  98. const char *hash[CONFIG_CSH_MAX_USER]; /*!< user password hash */
  99. char path[CONFIG_CSH_MAXLEN_PATH]; /*!< path */
  100. #else
  101. int uid; /*!< now user id */
  102. const char *host; /*!< host name */
  103. const char *user[CONFIG_CSH_MAX_USER]; /*!< user name */
  104. const char *hash[CONFIG_CSH_MAX_USER]; /*!< user password hash */
  105. const char *path; /*!< path */
  106. #endif
  107. #if defined(CONFIG_CSH_SIGNAL_HANDLER) && CONFIG_CSH_SIGNAL_HANDLER
  108. void (*sighdl[CSH_SIGNAL_COUNT])(void *, int);
  109. #endif
  110. /*!< reserved section */
  111. void *data; /*!< frame data */
  112. void *user_data; /*!< user data */
  113. } chry_shell_t;
  114. typedef void (*chry_sighandler_t)(chry_shell_t *csh, int signum);
  115. typedef struct {
  116. /*!< I/O section */
  117. uint16_t (*sput)(chry_readline_t *rl, const void *, uint16_t); /*!< output callback */
  118. uint16_t (*sget)(chry_readline_t *rl, void *, uint16_t); /*!< input callback */
  119. /*!< command table section */
  120. const void *command_table_beg; /*!< static command table begin */
  121. const void *command_table_end; /*!< static command table end */
  122. /*!< variable table section */
  123. const void *variable_table_beg; /*!< static environment variable begin */
  124. const void *variable_table_end; /*!< static environment variable end */
  125. /*!< prompt buffer setcion */
  126. char *prompt_buffer; /*!< prompt buffer */
  127. uint16_t prompt_buffer_size; /*!< prompt buffer size */
  128. /*!< history buffer setcion */
  129. char *history_buffer; /*!< history buffer */
  130. uint16_t history_buffer_size; /*!< history buffer size */
  131. /*!< line buffer setcion */
  132. char *line_buffer; /*!< line buffer */
  133. uint32_t line_buffer_size; /*!< line buffer size */
  134. /*!< user host section */
  135. int uid; /*!< default user id */
  136. const char *host; /*!< host name */
  137. const char *user[CONFIG_CSH_MAX_USER]; /*!< user name */
  138. const char *hash[CONFIG_CSH_MAX_USER]; /*!< user password hash */
  139. /*!< reserved section */
  140. void *user_data;
  141. } chry_shell_init_t;
  142. int chry_shell_init(chry_shell_t *csh, const chry_shell_init_t *init);
  143. int chry_shell_task_repl(chry_shell_t *csh);
  144. void chry_shell_task_exec(chry_shell_t *csh);
  145. int chry_shell_parse(char *line, uint32_t linesize, const char **argv, uint8_t argcmax);
  146. int chry_shell_path_resolve(const char *cur, const char *path, const char **argv, uint8_t *argl, uint8_t argcmax);
  147. int chry_shell_set_host(chry_shell_t *csh, const char *host);
  148. int chry_shell_set_user(chry_shell_t *csh, uint8_t uid, const char *user, const char *hash);
  149. int chry_shell_set_path(chry_shell_t *csh, uint8_t size, const char *path);
  150. void chry_shell_get_path(chry_shell_t *csh, uint8_t size, char *path);
  151. int chry_shell_substitute_user(chry_shell_t *csh, uint8_t uid, const char *password);
  152. char *chry_shell_getenv(chry_shell_t *csh, const char *name);
  153. int chry_shell_execl(chry_shell_t *csh, const char *__path, const char *, ...);
  154. int chry_shell_execle(chry_shell_t *csh, const char *__path, const char *, ...);
  155. int chry_shell_execlp(chry_shell_t *csh, const char *__file, const char *, ...);
  156. int chry_shell_execv(chry_shell_t *csh, const char *__path, char *const __argv[]);
  157. int chry_shell_execve(chry_shell_t *csh, const char *__path, char *const __argv[], char *const __envp[]);
  158. int chry_shell_execvp(chry_shell_t *csh, const char *__file, char *const __argv[]);
  159. int csh_printf(chry_shell_t *csh, const char *fmt, ...);
  160. int csh_login(chry_shell_t *csh);
  161. #ifdef __cplusplus
  162. }
  163. #endif
  164. #endif