finsh.h 8.5 KB

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