finsh.h 12 KB

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