finsh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * File : finsh.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-03-22 Bernard first version
  13. */
  14. #ifndef __FINSH_H__
  15. #define __FINSH_H__
  16. #include <rtthread.h>
  17. /* -- the beginning of option -- */
  18. #define FINSH_NAME_MAX 16 /* max length of identifier */
  19. #define FINSH_NODE_MAX 16 /* max number of node */
  20. #define FINSH_HEAP_MAX 128 /* max length of heap */
  21. #define FINSH_STRING_MAX 128 /* max length of string */
  22. #define FINSH_VARIABLE_MAX 8 /* max number of variable */
  23. #define FINSH_STACK_MAX 64 /* max stack size */
  24. #define FINSH_TEXT_MAX 128 /* max text segment size */
  25. #define HEAP_ALIGNMENT 4 /* heap alignment */
  26. #define FINSH_GET16(x) (*(x)) | (*((x)+1) << 8)
  27. #define FINSH_GET32(x) (rt_uint32_t)(*(x)) | ((rt_uint32_t)*((x)+1) << 8) | \
  28. ((rt_uint32_t)*((x)+2) << 16) | ((rt_uint32_t)*((x)+3) << 24)
  29. #define FINSH_SET16(x, v) \
  30. do \
  31. { \
  32. *(x) = (v) & 0x00ff; \
  33. (*((x)+1)) = (v) >> 8; \
  34. } while ( 0 )
  35. #define FINSH_SET32(x, v) \
  36. do \
  37. { \
  38. *(x) = (rt_uint32_t)(v) & 0x000000ff; \
  39. (*((x)+1)) = ((rt_uint32_t)(v) >> 8) & 0x000000ff; \
  40. (*((x)+2)) = ((rt_uint32_t)(v) >> 16) & 0x000000ff; \
  41. (*((x)+3)) = ((rt_uint32_t)(v) >> 24); \
  42. } while ( 0 )
  43. /* -- the end of option -- */
  44. #if defined(RT_USING_NEWLIB) || defined (RT_USING_MINILIBC)
  45. #include <sys/types.h>
  46. #include <string.h>
  47. #else
  48. typedef unsigned char u_char;
  49. typedef unsigned short u_short;
  50. typedef unsigned long u_long;
  51. #if !defined(__CC_ARM) && !defined(__IAR_SYSTEMS_ICC__) && !defined(__ADSPBLACKFIN__)
  52. typedef unsigned int size_t;
  53. #ifndef NULL
  54. #define NULL RT_NULL
  55. #endif
  56. #define memset rt_memset
  57. #define strlen rt_strlen
  58. #define strncpy rt_strncpy
  59. #define strncmp rt_strncmp
  60. int strcmp (const char *s1, const char *s2);
  61. char *strdup(const char *s);
  62. int isalpha( int ch );
  63. int atoi(const char* s);
  64. #else
  65. /* use libc of armcc */
  66. #include <ctype.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #endif
  70. #endif
  71. #define FINSH_VERSION_MAJOR 0
  72. #define FINSH_VERSION_MINOR 5
  73. /**
  74. * @addtogroup finsh
  75. */
  76. /*@{*/
  77. #define FINSH_ERROR_OK 0 /**< No error */
  78. #define FINSH_ERROR_INVALID_TOKEN 1 /**< Invalid token */
  79. #define FINSH_ERROR_EXPECT_TYPE 2 /**< Expect a type */
  80. #define FINSH_ERROR_UNKNOWN_TYPE 3 /**< Unknown type */
  81. #define FINSH_ERROR_VARIABLE_EXIST 4 /**< Variable exist */
  82. #define FINSH_ERROR_EXPECT_OPERATOR 5 /**< Expect a operator */
  83. #define FINSH_ERROR_MEMORY_FULL 6 /**< Memory full */
  84. #define FINSH_ERROR_UNKNOWN_OP 7 /**< Unknown operator */
  85. #define FINSH_ERROR_UNKNOWN_NODE 8 /**< Unknown node */
  86. #define FINSH_ERROR_EXPECT_CHAR 9 /**< Expect a character */
  87. #define FINSH_ERROR_UNEXPECT_END 10 /**< Unexpect end */
  88. #define FINSH_ERROR_UNKNOWN_TOKEN 11 /**< Unknown token */
  89. #define FINSH_ERROR_NO_FLOAT 12 /**< Float not supported */
  90. #define FINSH_ERROR_UNKNOWN_SYMBOL 13 /**< Unknown symbol */
  91. #define FINSH_ERROR_NULL_NODE 14 /**< Null node */
  92. /*@}*/
  93. typedef long (*syscall_func)();
  94. /* system call table */
  95. struct finsh_syscall
  96. {
  97. const char* name; /* the name of system call */
  98. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  99. const char* desc; /* description of system call */
  100. #endif
  101. syscall_func func; /* the function address of system call */
  102. };
  103. /* system call item */
  104. struct finsh_syscall_item
  105. {
  106. struct finsh_syscall_item* next; /* next item */
  107. struct finsh_syscall syscall; /* syscall */
  108. };
  109. extern struct finsh_syscall *_syscall_table_begin, *_syscall_table_end;
  110. extern struct finsh_syscall_item *global_syscall_list;
  111. /* find out system call, which should be implemented in user program */
  112. struct finsh_syscall* finsh_syscall_lookup(const char* name);
  113. /* system variable table */
  114. struct finsh_sysvar
  115. {
  116. const char* name; /* the name of variable */
  117. #if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
  118. const char* desc; /* description of system variable */
  119. #endif
  120. u_char type; /* the type of variable */
  121. void* var ; /* the address of variable */
  122. };
  123. /* system variable item */
  124. struct finsh_sysvar_item
  125. {
  126. struct finsh_sysvar_item *next; /* next item */
  127. struct finsh_sysvar sysvar; /* system variable */
  128. };
  129. extern struct finsh_sysvar *_sysvar_table_begin, *_sysvar_table_end;
  130. extern struct finsh_sysvar_item* global_sysvar_list;
  131. /* find out system variable, which should be implemented in user program */
  132. struct finsh_sysvar* finsh_sysvar_lookup(const char* name);
  133. #ifdef FINSH_USING_SYMTAB
  134. #ifdef FINSH_USING_DESCRIPTION
  135. /**
  136. * @ingroup finsh
  137. *
  138. * This macro exports a system function to finsh shell.
  139. *
  140. * @param name the name of function.
  141. * @param desc the description of function, which will show in help.
  142. */
  143. #define FINSH_FUNCTION_EXPORT(name, desc) \
  144. const char __fsym_##name##_name[] = #name; \
  145. const char __fsym_##name##_desc[] = #desc; \
  146. const struct finsh_syscall __fsym_##name SECTION("FSymTab")= \
  147. { \
  148. __fsym_##name##_name, \
  149. __fsym_##name##_desc, \
  150. (syscall_func)&name \
  151. };
  152. /**
  153. * @ingroup finsh
  154. *
  155. * This macro exports a system function with an alias name to finsh shell.
  156. *
  157. * @param name the name of function.
  158. * @param alias the alias name of function.
  159. * @param desc the description of function, which will show in help.
  160. */
  161. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc) \
  162. const char __fsym_##name##_name[] = #alias; \
  163. const char __fsym_##name##_desc[] = #desc; \
  164. const struct finsh_syscall __fsym_##name SECTION("FSymTab")= \
  165. { \
  166. __fsym_##name##_name, \
  167. __fsym_##name##_desc, \
  168. (syscall_func)&name \
  169. };
  170. /**
  171. * @ingroup finsh
  172. *
  173. * This macro exports a variable to finsh shell.
  174. *
  175. * @param name the name of function.
  176. * @param type the type of variable.
  177. * @param desc the description of function, which will show in help.
  178. */
  179. #define FINSH_VAR_EXPORT(name, type, desc) \
  180. const char __vsym_##name##_name[] = #name; \
  181. const char __vsym_##name##_desc[] = #desc; \
  182. const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
  183. { \
  184. __vsym_##name##_name, \
  185. __vsym_##name##_desc, \
  186. type, \
  187. (void*)&name \
  188. };
  189. #else
  190. #define FINSH_FUNCTION_EXPORT(name, desc) \
  191. const char __fsym_##name##_name[] = #name; \
  192. const struct finsh_syscall __fsym_##name SECTION("FSymTab")= \
  193. { \
  194. __fsym_##name##_name, \
  195. (syscall_func)&name \
  196. };
  197. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc) \
  198. const char __fsym_##name##_name[] = #alias; \
  199. const struct finsh_syscall __fsym_##name SECTION("FSymTab")= \
  200. { \
  201. __fsym_##name##_name, \
  202. (syscall_func)&name \
  203. };
  204. #define FINSH_VAR_EXPORT(name, type, desc) \
  205. const char __vsym_##name##_name[] = #name; \
  206. const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
  207. { \
  208. __vsym_##name##_name, \
  209. type, \
  210. (void*)&name \
  211. };
  212. #endif
  213. #else
  214. #define FINSH_FUNCTION_EXPORT(name, desc)
  215. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc)
  216. #define FINSH_VAR_EXPORT(name, type, desc)
  217. #endif
  218. struct finsh_token
  219. {
  220. char eof;
  221. char replay;
  222. int position;
  223. u_char current_token;
  224. union {
  225. char char_value;
  226. int int_value;
  227. long long_value;
  228. } value;
  229. u_char string[128];
  230. u_char* line;
  231. };
  232. #define FINSH_IDTYPE_VAR 0x01
  233. #define FINSH_IDTYPE_SYSVAR 0x02
  234. #define FINSH_IDTYPE_SYSCALL 0x04
  235. #define FINSH_IDTYPE_ADDRESS 0x08
  236. struct finsh_node
  237. {
  238. u_char node_type; /* node node_type */
  239. u_char data_type; /* node data node_type */
  240. u_char idtype; /* id node information */
  241. union { /* value node */
  242. char char_value;
  243. short short_value;
  244. int int_value;
  245. long long_value;
  246. void* ptr;
  247. } value;
  248. union
  249. {
  250. /* point to variable identifier or function identifier */
  251. struct finsh_var *var;
  252. struct finsh_sysvar *sysvar;
  253. struct finsh_syscall*syscall;
  254. }id;
  255. /* sibling and child node */
  256. struct finsh_node *sibling, *child;
  257. };
  258. struct finsh_parser
  259. {
  260. u_char* parser_string;
  261. struct finsh_token token;
  262. struct finsh_node* root;
  263. };
  264. /**
  265. * @ingroup finsh
  266. *
  267. * The basic data type in finsh shell
  268. */
  269. enum finsh_type {
  270. finsh_type_unknown = 0, /**< unknown data type */
  271. finsh_type_void, /**< void */
  272. finsh_type_voidp, /**< void pointer */
  273. finsh_type_char, /**< char */
  274. finsh_type_uchar, /**< unsigned char */
  275. finsh_type_charp, /**< char pointer */
  276. finsh_type_short, /**< short */
  277. finsh_type_ushort, /**< unsigned short */
  278. finsh_type_shortp, /**< short pointer */
  279. finsh_type_int, /**< int */
  280. finsh_type_uint, /**< unsigned int */
  281. finsh_type_intp, /**< int pointer */
  282. finsh_type_long, /**< long */
  283. finsh_type_ulong, /**< unsigned long */
  284. finsh_type_longp /**< long pointer */
  285. };
  286. /* init finsh environment */
  287. int finsh_init(struct finsh_parser* parser);
  288. /* flush finsh node, text segment */
  289. int finsh_flush(struct finsh_parser* parser);
  290. /* reset all of finsh */
  291. int finsh_reset(struct finsh_parser* parser);
  292. #ifdef RT_USING_DEVICE
  293. void finsh_set_device(const char* device_name);
  294. #endif
  295. /* run finsh parser to generate abstract synatx tree */
  296. void finsh_parser_run (struct finsh_parser* parser, const unsigned char* string);
  297. /* run compiler to compile abstract syntax tree */
  298. int finsh_compiler_run(struct finsh_node* node);
  299. /* run finsh virtual machine */
  300. void finsh_vm_run(void);
  301. /* get variable value */
  302. struct finsh_var* finsh_var_lookup(const char* name);
  303. /* get bottom value of stack */
  304. long finsh_stack_bottom(void);
  305. /* get error number of finsh */
  306. u_char finsh_errno(void);
  307. /* get error string */
  308. const char* finsh_error_string(u_char type);
  309. #ifdef RT_USING_HEAP
  310. /**
  311. * @ingroup finsh
  312. *
  313. * This function appends a system call to finsh runtime environment
  314. * @param name the name of system call
  315. * @param func the function pointer of system call
  316. */
  317. void finsh_syscall_append(const char* name, syscall_func func);
  318. /**
  319. * @ingroup finsh
  320. *
  321. * This function appends a system variable to finsh runtime environment
  322. * @param name the name of system variable
  323. * @param type the data type of system variable
  324. * @param addr the address of system variable
  325. */
  326. void finsh_sysvar_append(const char* name, u_char type, void* addr);
  327. #endif
  328. #endif