finsh.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2006-2018, 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 <rtthread.h>
  13. #include "finsh_api.h"
  14. /* -- the beginning of option -- */
  15. #define FINSH_NAME_MAX 16 /* max length of identifier */
  16. #define FINSH_NODE_MAX 16 /* max number of node */
  17. #define FINSH_HEAP_MAX 128 /* max length of heap */
  18. #define FINSH_STRING_MAX 128 /* max length of string */
  19. #define FINSH_VARIABLE_MAX 8 /* max number of variable */
  20. #define FINSH_STACK_MAX 64 /* max stack size */
  21. #define FINSH_TEXT_MAX 128 /* max text segment size */
  22. #define HEAP_ALIGNMENT 4 /* heap alignment */
  23. #define FINSH_GET16(x) (*(x)) | (*((x)+1) << 8)
  24. #define FINSH_GET32(x) (rt_uint32_t)(*(x)) | ((rt_uint32_t)*((x)+1) << 8) | \
  25. ((rt_uint32_t)*((x)+2) << 16) | ((rt_uint32_t)*((x)+3) << 24)
  26. #define FINSH_SET16(x, v) \
  27. do \
  28. { \
  29. *(x) = (v) & 0x00ff; \
  30. (*((x)+1)) = (v) >> 8; \
  31. } while ( 0 )
  32. #define FINSH_SET32(x, v) \
  33. do \
  34. { \
  35. *(x) = (rt_uint32_t)(v) & 0x000000ff; \
  36. (*((x)+1)) = ((rt_uint32_t)(v) >> 8) & 0x000000ff; \
  37. (*((x)+2)) = ((rt_uint32_t)(v) >> 16) & 0x000000ff; \
  38. (*((x)+3)) = ((rt_uint32_t)(v) >> 24); \
  39. } while ( 0 )
  40. /* -- the end of option -- */
  41. /* std header file */
  42. #include <stdio.h>
  43. #include <ctype.h>
  44. #include <stdlib.h>
  45. #include <stdint.h>
  46. #include <string.h>
  47. #define FINSH_VERSION_MAJOR 1
  48. #define FINSH_VERSION_MINOR 0
  49. /**
  50. * @addtogroup finsh
  51. */
  52. /*@{*/
  53. #define FINSH_ERROR_OK 0 /**< No error */
  54. #define FINSH_ERROR_INVALID_TOKEN 1 /**< Invalid token */
  55. #define FINSH_ERROR_EXPECT_TYPE 2 /**< Expect a type */
  56. #define FINSH_ERROR_UNKNOWN_TYPE 3 /**< Unknown type */
  57. #define FINSH_ERROR_VARIABLE_EXIST 4 /**< Variable exist */
  58. #define FINSH_ERROR_EXPECT_OPERATOR 5 /**< Expect a operator */
  59. #define FINSH_ERROR_MEMORY_FULL 6 /**< Memory full */
  60. #define FINSH_ERROR_UNKNOWN_OP 7 /**< Unknown operator */
  61. #define FINSH_ERROR_UNKNOWN_NODE 8 /**< Unknown node */
  62. #define FINSH_ERROR_EXPECT_CHAR 9 /**< Expect a character */
  63. #define FINSH_ERROR_UNEXPECT_END 10 /**< Unexpect end */
  64. #define FINSH_ERROR_UNKNOWN_TOKEN 11 /**< Unknown token */
  65. #define FINSH_ERROR_NO_FLOAT 12 /**< Float not supported */
  66. #define FINSH_ERROR_UNKNOWN_SYMBOL 13 /**< Unknown symbol */
  67. #define FINSH_ERROR_NULL_NODE 14 /**< Null node */
  68. /*@}*/
  69. /* system call item */
  70. struct finsh_syscall_item
  71. {
  72. struct finsh_syscall_item* next; /* next item */
  73. struct finsh_syscall syscall; /* syscall */
  74. };
  75. extern struct finsh_syscall_item *global_syscall_list;
  76. /* system variable table */
  77. struct finsh_sysvar
  78. {
  79. const char* name; /* the name of variable */
  80. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  81. const char* desc; /* description of system variable */
  82. #endif
  83. uint8_t type; /* the type of variable */
  84. void* var ; /* the address of variable */
  85. };
  86. #if defined(_MSC_VER) || (defined(__GNUC__) && defined(__x86_64__))
  87. struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call);
  88. struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call);
  89. #define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
  90. #define FINSH_NEXT_SYSVAR(index) index=finsh_sysvar_next(index)
  91. #else
  92. #define FINSH_NEXT_SYSCALL(index) index++
  93. #define FINSH_NEXT_SYSVAR(index) index++
  94. #endif
  95. /* system variable item */
  96. struct finsh_sysvar_item
  97. {
  98. struct finsh_sysvar_item *next; /* next item */
  99. struct finsh_sysvar sysvar; /* system variable */
  100. };
  101. extern struct finsh_sysvar *_sysvar_table_begin, *_sysvar_table_end;
  102. extern struct finsh_sysvar_item* global_sysvar_list;
  103. /* find out system variable, which should be implemented in user program */
  104. struct finsh_sysvar* finsh_sysvar_lookup(const char* name);
  105. struct finsh_token
  106. {
  107. char eof;
  108. char replay;
  109. int position;
  110. uint8_t current_token;
  111. union {
  112. char char_value;
  113. int int_value;
  114. long long_value;
  115. } value;
  116. uint8_t string[FINSH_STRING_MAX];
  117. uint8_t* line;
  118. };
  119. #define FINSH_IDTYPE_VAR 0x01
  120. #define FINSH_IDTYPE_SYSVAR 0x02
  121. #define FINSH_IDTYPE_SYSCALL 0x04
  122. #define FINSH_IDTYPE_ADDRESS 0x08
  123. struct finsh_node
  124. {
  125. uint8_t node_type; /* node node_type */
  126. uint8_t data_type; /* node data node_type */
  127. uint8_t idtype; /* id node information */
  128. union { /* value node */
  129. char char_value;
  130. short short_value;
  131. int int_value;
  132. long long_value;
  133. void* ptr;
  134. } value;
  135. union
  136. {
  137. /* point to variable identifier or function identifier */
  138. struct finsh_var *var;
  139. struct finsh_sysvar *sysvar;
  140. struct finsh_syscall*syscall;
  141. }id;
  142. /* sibling and child node */
  143. struct finsh_node *sibling, *child;
  144. };
  145. struct finsh_parser
  146. {
  147. uint8_t* parser_string;
  148. struct finsh_token token;
  149. struct finsh_node* root;
  150. };
  151. /**
  152. * @ingroup finsh
  153. *
  154. * The basic data type in finsh shell
  155. */
  156. enum finsh_type {
  157. finsh_type_unknown = 0, /**< unknown data type */
  158. finsh_type_void, /**< void */
  159. finsh_type_voidp, /**< void pointer */
  160. finsh_type_char, /**< char */
  161. finsh_type_uchar, /**< unsigned char */
  162. finsh_type_charp, /**< char pointer */
  163. finsh_type_short, /**< short */
  164. finsh_type_ushort, /**< unsigned short */
  165. finsh_type_shortp, /**< short pointer */
  166. finsh_type_int, /**< int */
  167. finsh_type_uint, /**< unsigned int */
  168. finsh_type_intp, /**< int pointer */
  169. finsh_type_long, /**< long */
  170. finsh_type_ulong, /**< unsigned long */
  171. finsh_type_longp /**< long pointer */
  172. };
  173. /* init finsh environment */
  174. int finsh_init(struct finsh_parser* parser);
  175. /* flush finsh node, text segment */
  176. int finsh_flush(struct finsh_parser* parser);
  177. /* reset all of finsh */
  178. int finsh_reset(struct finsh_parser* parser);
  179. #ifdef RT_USING_DEVICE
  180. void finsh_set_device(const char* device_name);
  181. #endif
  182. /* run finsh parser to generate abstract synatx tree */
  183. void finsh_parser_run (struct finsh_parser* parser, const unsigned char* string);
  184. /* run compiler to compile abstract syntax tree */
  185. int finsh_compiler_run(struct finsh_node* node);
  186. /* run finsh virtual machine */
  187. void finsh_vm_run(void);
  188. /* get variable value */
  189. struct finsh_var* finsh_var_lookup(const char* name);
  190. /* get bottom value of stack */
  191. long finsh_stack_bottom(void);
  192. /* get error number of finsh */
  193. uint8_t finsh_errno(void);
  194. /* get error string */
  195. const char* finsh_error_string(uint8_t type);
  196. #ifdef RT_USING_HEAP
  197. /**
  198. * @ingroup finsh
  199. *
  200. * This function appends a system call to finsh runtime environment
  201. * @param name the name of system call
  202. * @param func the function pointer of system call
  203. */
  204. void finsh_syscall_append(const char* name, syscall_func func);
  205. /**
  206. * @ingroup finsh
  207. *
  208. * This function appends a system variable to finsh runtime environment
  209. * @param name the name of system variable
  210. * @param type the data type of system variable
  211. * @param addr the address of system variable
  212. */
  213. void finsh_sysvar_append(const char* name, uint8_t type, void* addr);
  214. #endif
  215. #endif