1
0

finsh.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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) && \
  56. !defined(__IAR_SYSTEMS_ICC__) && \
  57. !defined(__ADSPBLACKFIN__) && \
  58. !defined(_MSC_VER)
  59. /* only for GNU GCC */
  60. #if !(defined(__GNUC__) && defined(__x86_64__))
  61. typedef unsigned int size_t;
  62. #else
  63. #include <stdio.h>
  64. #endif
  65. #ifndef NULL
  66. #define NULL RT_NULL
  67. #endif
  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 1
  76. #define FINSH_VERSION_MINOR 0
  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) || (defined(__GNUC__) && defined(__x86_64__))
  128. struct finsh_syscall* finsh_syscall_next(struct finsh_syscall* call);
  129. struct finsh_sysvar* finsh_sysvar_next(struct finsh_sysvar* call);
  130. #define FINSH_NEXT_SYSCALL(index) index=finsh_syscall_next(index)
  131. #define FINSH_NEXT_SYSVAR(index) index=finsh_sysvar_next(index)
  132. #else
  133. #define FINSH_NEXT_SYSCALL(index) index++
  134. #define FINSH_NEXT_SYSVAR(index) index++
  135. #endif
  136. /* system variable item */
  137. struct finsh_sysvar_item
  138. {
  139. struct finsh_sysvar_item *next; /* next item */
  140. struct finsh_sysvar sysvar; /* system variable */
  141. };
  142. extern struct finsh_sysvar *_sysvar_table_begin, *_sysvar_table_end;
  143. extern struct finsh_sysvar_item* global_sysvar_list;
  144. /* find out system variable, which should be implemented in user program */
  145. struct finsh_sysvar* finsh_sysvar_lookup(const char* name);
  146. #ifdef FINSH_USING_SYMTAB
  147. #ifdef __TI_COMPILER_VERSION__
  148. #define __TI_FINSH_EXPORT_FUNCTION(f) PRAGMA(DATA_SECTION(f,"FSymTab"))
  149. #define __TI_FINSH_EXPORT_VAR(v) PRAGMA(DATA_SECTION(v,"VSymTab"))
  150. #endif
  151. #ifdef FINSH_USING_DESCRIPTION
  152. #ifdef _MSC_VER
  153. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
  154. const char __fsym_##cmd##_name[] = #cmd; \
  155. const char __fsym_##cmd##_desc[] = #desc; \
  156. __declspec(allocate("FSymTab$f")) \
  157. const struct finsh_syscall __fsym_##cmd = \
  158. { \
  159. __fsym_##cmd##_name, \
  160. __fsym_##cmd##_desc, \
  161. (syscall_func)&name \
  162. };
  163. #pragma comment(linker, "/merge:FSymTab=mytext")
  164. #define FINSH_VAR_EXPORT(name, type, desc) \
  165. const char __vsym_##name##_name[] = #name; \
  166. const char __vsym_##name##_desc[] = #desc; \
  167. __declspec(allocate("VSymTab")) \
  168. const struct finsh_sysvar __vsym_##name = \
  169. { \
  170. __vsym_##name##_name, \
  171. __vsym_##name##_desc, \
  172. type, \
  173. (void*)&name \
  174. };
  175. #elif defined(__TI_COMPILER_VERSION__)
  176. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
  177. __TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
  178. const char __fsym_##cmd##_name[] = #cmd; \
  179. const char __fsym_##cmd##_desc[] = #desc; \
  180. const struct finsh_syscall __fsym_##cmd = \
  181. { \
  182. __fsym_##cmd##_name, \
  183. __fsym_##cmd##_desc, \
  184. (syscall_func)&name \
  185. };
  186. #define FINSH_VAR_EXPORT(name, type, desc) \
  187. __TI_FINSH_EXPORT_VAR(__vsym_##name); \
  188. const char __vsym_##name##_name[] = #name; \
  189. const char __vsym_##name##_desc[] = #desc; \
  190. const struct finsh_sysvar __vsym_##name = \
  191. { \
  192. __vsym_##name##_name, \
  193. __vsym_##name##_desc, \
  194. type, \
  195. (void*)&name \
  196. };
  197. #else
  198. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
  199. const char __fsym_##cmd##_name[] SECTION(".rodata.name") = #cmd; \
  200. const char __fsym_##cmd##_desc[] SECTION(".rodata.name") = #desc; \
  201. const struct finsh_syscall __fsym_##cmd SECTION("FSymTab")= \
  202. { \
  203. __fsym_##cmd##_name, \
  204. __fsym_##cmd##_desc, \
  205. (syscall_func)&name \
  206. };
  207. #define FINSH_VAR_EXPORT(name, type, desc) \
  208. const char __vsym_##name##_name[] SECTION(".rodata.name") = #name; \
  209. const char __vsym_##name##_desc[] SECTION(".rodata.name") = #desc; \
  210. const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
  211. { \
  212. __vsym_##name##_name, \
  213. __vsym_##name##_desc, \
  214. type, \
  215. (void*)&name \
  216. };
  217. #endif
  218. #else
  219. #ifdef _MSC_VER
  220. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
  221. const char __fsym_##cmd##_name[] = #cmd; \
  222. __declspec(allocate("FSymTab$f")) \
  223. const struct finsh_syscall __fsym_##cmd = \
  224. { \
  225. __fsym_##cmd##_name, \
  226. (syscall_func)&name \
  227. };
  228. #pragma comment(linker, "/merge:FSymTab=mytext")
  229. #define FINSH_VAR_EXPORT(name, type, desc) \
  230. const char __vsym_##name##_name[] = #name; \
  231. __declspec(allocate("VSymTab")) const struct finsh_sysvar __vsym_##name = \
  232. { \
  233. __vsym_##name##_name, \
  234. type, \
  235. (void*)&name \
  236. };
  237. #elif defined(__TI_COMPILER_VERSION__)
  238. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
  239. __TI_FINSH_EXPORT_FUNCTION(__fsym_##cmd); \
  240. const char __fsym_##cmd##_name[] = #cmd; \
  241. const struct finsh_syscall __fsym_##cmd = \
  242. { \
  243. __fsym_##cmd##_name, \
  244. (syscall_func)&name \
  245. };
  246. #define FINSH_VAR_EXPORT(name, type, desc) \
  247. __TI_FINSH_EXPORT_VAR(__vsym_##name); \
  248. const char __vsym_##name##_name[] = #name; \
  249. const struct finsh_sysvar __vsym_##name = \
  250. { \
  251. __vsym_##name##_name, \
  252. type, \
  253. (void*)&name \
  254. };
  255. #else
  256. #define FINSH_FUNCTION_EXPORT_CMD(name, cmd, desc) \
  257. const char __fsym_##cmd##_name[] = #cmd; \
  258. const struct finsh_syscall __fsym_##cmd SECTION("FSymTab")= \
  259. { \
  260. __fsym_##cmd##_name, \
  261. (syscall_func)&name \
  262. };
  263. #define FINSH_VAR_EXPORT(name, type, desc) \
  264. const char __vsym_##name##_name[] = #name; \
  265. const struct finsh_sysvar __vsym_##name SECTION("VSymTab")= \
  266. { \
  267. __vsym_##name##_name, \
  268. type, \
  269. (void*)&name \
  270. };
  271. #endif
  272. #endif /* end of FINSH_USING_DESCRIPTION */
  273. #endif /* end of FINSH_USING_SYMTAB */
  274. /**
  275. * @ingroup finsh
  276. *
  277. * This macro exports a system function to finsh shell.
  278. *
  279. * @param name the name of function.
  280. * @param desc the description of function, which will show in help.
  281. */
  282. #define FINSH_FUNCTION_EXPORT(name, desc) \
  283. FINSH_FUNCTION_EXPORT_CMD(name, name, desc)
  284. /**
  285. * @ingroup finsh
  286. *
  287. * This macro exports a system function with an alias name to finsh shell.
  288. *
  289. * @param name the name of function.
  290. * @param alias the alias name of function.
  291. * @param desc the description of function, which will show in help.
  292. */
  293. #define FINSH_FUNCTION_EXPORT_ALIAS(name, alias, desc) \
  294. FINSH_FUNCTION_EXPORT_CMD(name, alias, desc)
  295. /**
  296. * @ingroup finsh
  297. *
  298. * This macro exports a command to module shell.
  299. *
  300. * @param command the name of command.
  301. * @param desc the description of command, which will show in help.
  302. */
  303. #ifdef FINSH_USING_MSH
  304. #define MSH_CMD_EXPORT(command, desc) \
  305. FINSH_FUNCTION_EXPORT_CMD(command, __cmd_##command, desc)
  306. #define MSH_CMD_EXPORT_ALIAS(command, alias, desc) \
  307. FINSH_FUNCTION_EXPORT_ALIAS(command, __cmd_##alias, desc)
  308. #else
  309. #define MSH_CMD_EXPORT(command, desc)
  310. #define MSH_CMD_EXPORT_ALIAS(command, alias, desc)
  311. #endif
  312. struct finsh_token
  313. {
  314. char eof;
  315. char replay;
  316. int position;
  317. u_char current_token;
  318. union {
  319. char char_value;
  320. int int_value;
  321. long long_value;
  322. } value;
  323. u_char string[FINSH_STRING_MAX];
  324. u_char* line;
  325. };
  326. #define FINSH_IDTYPE_VAR 0x01
  327. #define FINSH_IDTYPE_SYSVAR 0x02
  328. #define FINSH_IDTYPE_SYSCALL 0x04
  329. #define FINSH_IDTYPE_ADDRESS 0x08
  330. struct finsh_node
  331. {
  332. u_char node_type; /* node node_type */
  333. u_char data_type; /* node data node_type */
  334. u_char idtype; /* id node information */
  335. union { /* value node */
  336. char char_value;
  337. short short_value;
  338. int int_value;
  339. long long_value;
  340. void* ptr;
  341. } value;
  342. union
  343. {
  344. /* point to variable identifier or function identifier */
  345. struct finsh_var *var;
  346. struct finsh_sysvar *sysvar;
  347. struct finsh_syscall*syscall;
  348. }id;
  349. /* sibling and child node */
  350. struct finsh_node *sibling, *child;
  351. };
  352. struct finsh_parser
  353. {
  354. u_char* parser_string;
  355. struct finsh_token token;
  356. struct finsh_node* root;
  357. };
  358. /**
  359. * @ingroup finsh
  360. *
  361. * The basic data type in finsh shell
  362. */
  363. enum finsh_type {
  364. finsh_type_unknown = 0, /**< unknown data type */
  365. finsh_type_void, /**< void */
  366. finsh_type_voidp, /**< void pointer */
  367. finsh_type_char, /**< char */
  368. finsh_type_uchar, /**< unsigned char */
  369. finsh_type_charp, /**< char pointer */
  370. finsh_type_short, /**< short */
  371. finsh_type_ushort, /**< unsigned short */
  372. finsh_type_shortp, /**< short pointer */
  373. finsh_type_int, /**< int */
  374. finsh_type_uint, /**< unsigned int */
  375. finsh_type_intp, /**< int pointer */
  376. finsh_type_long, /**< long */
  377. finsh_type_ulong, /**< unsigned long */
  378. finsh_type_longp /**< long pointer */
  379. };
  380. /* init finsh environment */
  381. int finsh_init(struct finsh_parser* parser);
  382. /* flush finsh node, text segment */
  383. int finsh_flush(struct finsh_parser* parser);
  384. /* reset all of finsh */
  385. int finsh_reset(struct finsh_parser* parser);
  386. #ifdef RT_USING_DEVICE
  387. void finsh_set_device(const char* device_name);
  388. #endif
  389. /* run finsh parser to generate abstract synatx tree */
  390. void finsh_parser_run (struct finsh_parser* parser, const unsigned char* string);
  391. /* run compiler to compile abstract syntax tree */
  392. int finsh_compiler_run(struct finsh_node* node);
  393. /* run finsh virtual machine */
  394. void finsh_vm_run(void);
  395. /* get variable value */
  396. struct finsh_var* finsh_var_lookup(const char* name);
  397. /* get bottom value of stack */
  398. long finsh_stack_bottom(void);
  399. /* get error number of finsh */
  400. u_char finsh_errno(void);
  401. /* get error string */
  402. const char* finsh_error_string(u_char type);
  403. #ifdef RT_USING_HEAP
  404. /**
  405. * @ingroup finsh
  406. *
  407. * This function appends a system call to finsh runtime environment
  408. * @param name the name of system call
  409. * @param func the function pointer of system call
  410. */
  411. void finsh_syscall_append(const char* name, syscall_func func);
  412. /**
  413. * @ingroup finsh
  414. *
  415. * This function appends a system variable to finsh runtime environment
  416. * @param name the name of system variable
  417. * @param type the data type of system variable
  418. * @param addr the address of system variable
  419. */
  420. void finsh_sysvar_append(const char* name, u_char type, void* addr);
  421. #endif
  422. #endif