cmd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * File : cmd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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. * 2006-04-30 Bernard first implementation
  13. * 2006-05-04 Bernard add list_thread,
  14. * list_sem,
  15. * list_timer
  16. * 2006-05-20 Bernard add list_mutex,
  17. * list_mailbox,
  18. * list_msgqueue,
  19. * list_event,
  20. * list_fevent,
  21. * list_mempool
  22. * 2006-06-03 Bernard display stack information in list_thread
  23. * 2006-08-10 Bernard change version to invoke rt_show_version
  24. * 2008-09-10 Bernard update the list function for finsh syscall
  25. * list and sysvar list
  26. * 2009-05-30 Bernard add list_device
  27. * 2010-04-21 yi.qiu add list_module
  28. * 2012-04-29 goprife improve the command line auto-complete feature.
  29. * 2012-06-02 lgnq add list_memheap
  30. * 2012-10-22 Bernard add MS VC++ patch.
  31. */
  32. #include <rtthread.h>
  33. #include "finsh.h"
  34. #if defined(_MSC_VER)
  35. static struct finsh_syscall* _next_syscall(struct finsh_syscall* call)
  36. {
  37. unsigned int *ptr;
  38. ptr = (unsigned int*) (call + 1);
  39. while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _syscall_table_end))
  40. ptr ++;
  41. return (struct finsh_syscall*)ptr;
  42. }
  43. #define _NEXT_SYSCALl(index) index=_next_syscall(index)
  44. #else
  45. #define _NEXT_SYSCALl(index) index++
  46. #endif
  47. rt_inline unsigned int rt_list_len(const rt_list_t *l)
  48. {
  49. unsigned int len = 0;
  50. const rt_list_t *p = l;
  51. while (p->next != l)
  52. {
  53. p = p->next;
  54. len ++;
  55. }
  56. return len;
  57. }
  58. long hello(void)
  59. {
  60. rt_kprintf("Hello RT-Thread!\n");
  61. return 0;
  62. }
  63. FINSH_FUNCTION_EXPORT(hello, say hello world);
  64. extern void rt_show_version(void);
  65. long version(void)
  66. {
  67. rt_show_version();
  68. return 0;
  69. }
  70. FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
  71. extern struct rt_object_information rt_object_container[];
  72. static long _list_thread(struct rt_list_node *list)
  73. {
  74. struct rt_thread *thread;
  75. struct rt_list_node *node;
  76. rt_uint8_t *ptr;
  77. rt_kprintf(" thread pri status sp stack size max used left tick error\n");
  78. rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
  79. for (node = list->next; node != list; node = node->next)
  80. {
  81. thread = rt_list_entry(node, struct rt_thread, list);
  82. rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);
  83. if (thread->stat == RT_THREAD_READY) rt_kprintf(" ready ");
  84. else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
  85. else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init ");
  86. else if (thread->stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
  87. ptr = (rt_uint8_t*)thread->stack_addr;
  88. while (*ptr == '#')ptr ++;
  89. rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
  90. thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
  91. thread->stack_size,
  92. thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
  93. thread->remaining_tick,
  94. thread->error);
  95. }
  96. return 0;
  97. }
  98. long list_thread(void)
  99. {
  100. return _list_thread(&rt_object_container[RT_Object_Class_Thread].object_list);
  101. }
  102. FINSH_FUNCTION_EXPORT(list_thread, list thread);
  103. static void show_wait_queue(struct rt_list_node *list)
  104. {
  105. struct rt_thread *thread;
  106. struct rt_list_node *node;
  107. for (node = list->next; node != list; node = node->next)
  108. {
  109. thread = rt_list_entry(node, struct rt_thread, tlist);
  110. rt_kprintf("%s", thread->name);
  111. if (node->next != list)
  112. rt_kprintf("/");
  113. }
  114. }
  115. #ifdef RT_USING_SEMAPHORE
  116. static long _list_sem(struct rt_list_node *list)
  117. {
  118. struct rt_semaphore *sem;
  119. struct rt_list_node *node;
  120. rt_kprintf("semaphore v suspend thread\n");
  121. rt_kprintf("-------- --- --------------\n");
  122. for (node = list->next; node != list; node = node->next)
  123. {
  124. sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
  125. if( !rt_list_isempty(&sem->parent.suspend_thread) )
  126. {
  127. rt_kprintf("%-8.*s %03d %d:", RT_NAME_MAX, sem->parent.parent.name, sem->value,
  128. rt_list_len(&sem->parent.suspend_thread) );
  129. show_wait_queue(&(sem->parent.suspend_thread));
  130. rt_kprintf("\n");
  131. }
  132. else
  133. {
  134. rt_kprintf("%-8.*s %03d %d\n", RT_NAME_MAX, sem->parent.parent.name, sem->value,
  135. rt_list_len(&sem->parent.suspend_thread));
  136. }
  137. }
  138. return 0;
  139. }
  140. long list_sem(void)
  141. {
  142. return _list_sem(&rt_object_container[RT_Object_Class_Semaphore].object_list);
  143. }
  144. FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system)
  145. #endif
  146. #ifdef RT_USING_EVENT
  147. static long _list_event(struct rt_list_node *list)
  148. {
  149. struct rt_event *e;
  150. struct rt_list_node *node;
  151. rt_kprintf("event set suspend thread\n");
  152. rt_kprintf("-------- ---------- --------------\n");
  153. for (node = list->next; node != list; node = node->next)
  154. {
  155. e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
  156. if (!rt_list_isempty(&e->parent.suspend_thread))
  157. {
  158. rt_kprintf("%-8.*s 0x%08x %03d:", RT_NAME_MAX, e->parent.parent.name,
  159. e->set, rt_list_len(&e->parent.suspend_thread));
  160. show_wait_queue(&(e->parent.suspend_thread));
  161. rt_kprintf("\n");
  162. }
  163. else
  164. {
  165. rt_kprintf("%-8.*s 0x%08x 0\n", RT_NAME_MAX, e->parent.parent.name, e->set);
  166. }
  167. }
  168. return 0;
  169. }
  170. long list_event(void)
  171. {
  172. return _list_event(&rt_object_container[RT_Object_Class_Event].object_list);
  173. }
  174. FINSH_FUNCTION_EXPORT(list_event, list event in system)
  175. #endif
  176. #ifdef RT_USING_MUTEX
  177. static long _list_mutex(struct rt_list_node *list)
  178. {
  179. struct rt_mutex *m;
  180. struct rt_list_node *node;
  181. rt_kprintf("mutex owner hold suspend thread\n");
  182. rt_kprintf("-------- -------- ---- --------------\n");
  183. for (node = list->next; node != list; node = node->next)
  184. {
  185. m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
  186. rt_kprintf("%-8.*s %-8.*s %04d %d\n", RT_NAME_MAX, m->parent.parent.name,
  187. RT_NAME_MAX, m->owner->name, m->hold, rt_list_len(&m->parent.suspend_thread));
  188. }
  189. return 0;
  190. }
  191. long list_mutex(void)
  192. {
  193. return _list_mutex(&rt_object_container[RT_Object_Class_Mutex].object_list);
  194. }
  195. FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system)
  196. #endif
  197. #ifdef RT_USING_MAILBOX
  198. static long _list_mailbox(struct rt_list_node *list)
  199. {
  200. struct rt_mailbox *m;
  201. struct rt_list_node *node;
  202. rt_kprintf("mailbox entry size suspend thread\n");
  203. rt_kprintf("-------- ---- ---- --------------\n");
  204. for (node = list->next; node != list; node = node->next)
  205. {
  206. m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
  207. if (!rt_list_isempty(&m->parent.suspend_thread))
  208. {
  209. rt_kprintf("%-8.*s %04d %04d %d:", RT_NAME_MAX, m->parent.parent.name,
  210. m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
  211. show_wait_queue(&(m->parent.suspend_thread));
  212. rt_kprintf("\n");
  213. }
  214. else
  215. {
  216. rt_kprintf("%-8.*s %04d %04d %d\n", RT_NAME_MAX, m->parent.parent.name,
  217. m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
  218. }
  219. }
  220. return 0;
  221. }
  222. long list_mailbox(void)
  223. {
  224. return _list_mailbox(&rt_object_container[RT_Object_Class_MailBox].object_list);
  225. }
  226. FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system)
  227. #endif
  228. #ifdef RT_USING_MESSAGEQUEUE
  229. static long _list_msgqueue(struct rt_list_node *list)
  230. {
  231. struct rt_messagequeue *m;
  232. struct rt_list_node *node;
  233. rt_kprintf("msgqueue entry suspend thread\n");
  234. rt_kprintf("-------- ---- --------------\n");
  235. for (node = list->next; node != list; node = node->next)
  236. {
  237. m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
  238. if (!rt_list_isempty(&m->parent.suspend_thread))
  239. {
  240. rt_kprintf("%-8.*s %04d %d:", RT_NAME_MAX, m->parent.parent.name,
  241. m->entry, rt_list_len(&m->parent.suspend_thread));
  242. show_wait_queue(&(m->parent.suspend_thread));
  243. rt_kprintf("\n");
  244. }
  245. else
  246. {
  247. rt_kprintf("%-8.*s %04d %d\n", RT_NAME_MAX, m->parent.parent.name,
  248. m->entry, rt_list_len(&m->parent.suspend_thread));
  249. }
  250. }
  251. return 0;
  252. }
  253. long list_msgqueue(void)
  254. {
  255. return _list_msgqueue(&rt_object_container[RT_Object_Class_MessageQueue].object_list);
  256. }
  257. FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system)
  258. #endif
  259. #ifdef RT_USING_MEMHEAP
  260. static long _list_memheap(struct rt_list_node *list)
  261. {
  262. struct rt_memheap *mh;
  263. struct rt_list_node *node;
  264. rt_kprintf("memheap pool size available size\n");
  265. rt_kprintf("-------- --------- --------------\n");
  266. for (node = list->next; node != list; node = node->next)
  267. {
  268. mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);
  269. rt_kprintf("%-8.*s %04d %04d\n", RT_NAME_MAX, mh->parent.name,
  270. mh->pool_size, mh->available_size);
  271. }
  272. return 0;
  273. }
  274. long list_memheap(void)
  275. {
  276. return _list_memheap(&rt_object_container[RT_Object_Class_MemHeap].object_list);
  277. }
  278. FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system)
  279. #endif
  280. #ifdef RT_USING_MEMPOOL
  281. static long _list_mempool(struct rt_list_node *list)
  282. {
  283. struct rt_mempool *mp;
  284. struct rt_list_node *node;
  285. rt_kprintf("mempool block total free suspend thread\n");
  286. rt_kprintf("-------- ---- ---- ---- --------------\n");
  287. for (node = list->next; node != list; node = node->next)
  288. {
  289. mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
  290. if (mp->suspend_thread_count > 0)
  291. {
  292. rt_kprintf("%-8.*s %04d %04d %04d %d:", RT_NAME_MAX, mp->parent.name,
  293. mp->block_size, mp->block_total_count, mp->block_free_count,
  294. mp->suspend_thread_count);
  295. show_wait_queue(&(mp->suspend_thread));
  296. rt_kprintf("\n");
  297. }
  298. else
  299. {
  300. rt_kprintf("%-8.*s %04d %04d %04d %d\n", RT_NAME_MAX, mp->parent.name,
  301. mp->block_size, mp->block_total_count, mp->block_free_count,
  302. mp->suspend_thread_count);
  303. }
  304. }
  305. return 0;
  306. }
  307. long list_mempool(void)
  308. {
  309. return _list_mempool(&rt_object_container[RT_Object_Class_MemPool].object_list);
  310. }
  311. FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
  312. #endif
  313. static long _list_timer(struct rt_list_node *list)
  314. {
  315. struct rt_timer *timer;
  316. struct rt_list_node *node;
  317. rt_kprintf("timer periodic timeout flag\n");
  318. rt_kprintf("-------- ---------- ---------- -----------\n");
  319. for (node = list->next; node != list; node = node->next)
  320. {
  321. timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
  322. rt_kprintf("%-8.*s 0x%08x 0x%08x ", RT_NAME_MAX, timer->parent.name, timer->init_tick, timer->timeout_tick);
  323. if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  324. rt_kprintf("activated\n");
  325. else
  326. rt_kprintf("deactivated\n");
  327. }
  328. rt_kprintf("current tick:0x%08x\n", rt_tick_get());
  329. return 0;
  330. }
  331. long list_timer(void)
  332. {
  333. return _list_timer(&rt_object_container[RT_Object_Class_Timer].object_list);
  334. }
  335. FINSH_FUNCTION_EXPORT(list_timer, list timer in system)
  336. #ifdef RT_USING_DEVICE
  337. static long _list_device(struct rt_list_node *list)
  338. {
  339. struct rt_device *device;
  340. struct rt_list_node *node;
  341. char * const device_type_str[] =
  342. {
  343. "Character Device",
  344. "Block Device",
  345. "Network Interface",
  346. "MTD Device",
  347. "CAN Device",
  348. "RTC",
  349. "Sound Device",
  350. "Graphic Device",
  351. "I2C Bus",
  352. "USB Slave Device",
  353. "USB Host Bus",
  354. "SPI Bus",
  355. "SPI Device",
  356. "SDIO Bus",
  357. "PM Pseudo Device",
  358. "Unknown"
  359. };
  360. rt_kprintf("device type \n");
  361. rt_kprintf("-------- ---------- \n");
  362. for (node = list->next; node != list; node = node->next)
  363. {
  364. device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
  365. rt_kprintf("%-8.*s %-8s \n", RT_NAME_MAX, device->parent.name,
  366. (device->type <= RT_Device_Class_Unknown)?
  367. device_type_str[device->type]:device_type_str[RT_Device_Class_Unknown]);
  368. }
  369. return 0;
  370. }
  371. long list_device(void)
  372. {
  373. return _list_device(&rt_object_container[RT_Object_Class_Device].object_list);
  374. }
  375. FINSH_FUNCTION_EXPORT(list_device, list device in system)
  376. #endif
  377. #ifdef RT_USING_MODULE
  378. #include <rtm.h>
  379. int list_module(void)
  380. {
  381. struct rt_module *module;
  382. struct rt_list_node *list, *node;
  383. list = &rt_object_container[RT_Object_Class_Module].object_list;
  384. rt_kprintf("module name ref\n");
  385. rt_kprintf("------------ --------\n");
  386. for (node = list->next; node != list; node = node->next)
  387. {
  388. module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
  389. rt_kprintf("%-16.*s %-04d\n", RT_NAME_MAX, module->parent.name, module->nref);
  390. }
  391. return 0;
  392. }
  393. FINSH_FUNCTION_EXPORT(list_module, list module in system)
  394. int list_mod_detail(const char *name)
  395. {
  396. int i;
  397. struct rt_module *module;
  398. /* find module */
  399. if ((module = rt_module_find(name)) != RT_NULL)
  400. {
  401. /* module has entry point */
  402. if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
  403. {
  404. struct rt_thread *thread;
  405. struct rt_list_node *tlist;
  406. rt_uint8_t *ptr;
  407. /* list main thread in module */
  408. if (module->module_thread != RT_NULL)
  409. {
  410. rt_kprintf("main thread pri status sp stack size max used left tick error\n");
  411. rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
  412. thread = module->module_thread;
  413. rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);
  414. if (thread->stat == RT_THREAD_READY) rt_kprintf(" ready ");
  415. else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
  416. else if (thread->stat == RT_THREAD_INIT) rt_kprintf(" init ");
  417. ptr = (rt_uint8_t*)thread->stack_addr;
  418. while (*ptr == '#')ptr ++;
  419. rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
  420. thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
  421. thread->stack_size,
  422. thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
  423. thread->remaining_tick,
  424. thread->error);
  425. }
  426. /* list sub thread in module */
  427. tlist = &module->module_object[RT_Object_Class_Thread].object_list;
  428. if (!rt_list_isempty(tlist)) _list_thread(tlist);
  429. #ifdef RT_USING_SEMAPHORE
  430. /* list semaphored in module */
  431. tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
  432. if (!rt_list_isempty(tlist)) _list_sem(tlist);
  433. #endif
  434. #ifdef RT_USING_MUTEX
  435. /* list mutex in module */
  436. tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
  437. if (!rt_list_isempty(tlist)) _list_mutex(tlist);
  438. #endif
  439. #ifdef RT_USING_EVENT
  440. /* list event in module */
  441. tlist = &module->module_object[RT_Object_Class_Event].object_list;
  442. if (!rt_list_isempty(tlist)) _list_event(tlist);
  443. #endif
  444. #ifdef RT_USING_MAILBOX
  445. /* list mailbox in module */
  446. tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
  447. if (!rt_list_isempty(tlist)) _list_mailbox(tlist);
  448. #endif
  449. #ifdef RT_USING_MESSAGEQUEUE
  450. /* list message queue in module */
  451. tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
  452. if (!rt_list_isempty(tlist)) _list_msgqueue(tlist);
  453. #endif
  454. #ifdef RT_USING_MEMHEAP
  455. /* list memory heap in module */
  456. tlist = &module->module_object[RT_Object_Class_MemHeap].object_list;
  457. if (!rt_list_isempty(tlist)) _list_memheap(tlist);
  458. #endif
  459. #ifdef RT_USING_MEMPOOL
  460. /* list memory pool in module */
  461. tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
  462. if (!rt_list_isempty(tlist)) _list_mempool(tlist);
  463. #endif
  464. #ifdef RT_USING_DEVICE
  465. /* list device in module */
  466. tlist = &module->module_object[RT_Object_Class_Device].object_list;
  467. if (!rt_list_isempty(tlist)) _list_device(tlist);
  468. #endif
  469. /* list timer in module */
  470. tlist = &module->module_object[RT_Object_Class_Timer].object_list;
  471. if (!rt_list_isempty(tlist)) _list_timer(tlist);
  472. }
  473. rt_kprintf("symbol address \n");
  474. rt_kprintf("-------- ----------\n");
  475. /* list module export symbols */
  476. for (i=0; i<module->nsym; i++)
  477. {
  478. rt_kprintf("%s 0x%x\n", module->symtab[i].name, module->symtab[i].addr);
  479. }
  480. }
  481. return 0;
  482. }
  483. FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
  484. #endif
  485. long list(void)
  486. {
  487. struct finsh_syscall_item *syscall_item;
  488. struct finsh_sysvar_item *sysvar_item;
  489. rt_kprintf("--Function List:\n");
  490. {
  491. struct finsh_syscall *index;
  492. for (index = _syscall_table_begin; index < _syscall_table_end; _NEXT_SYSCALl(index))
  493. {
  494. #ifdef FINSH_USING_DESCRIPTION
  495. rt_kprintf("%-16s -- %s\n", index->name, index->desc);
  496. #else
  497. rt_kprintf("%s\n", index->name);
  498. #endif
  499. }
  500. }
  501. /* list syscall list */
  502. syscall_item = global_syscall_list;
  503. while (syscall_item != NULL)
  504. {
  505. rt_kprintf("[l] %s\n", syscall_item->syscall.name);
  506. syscall_item = syscall_item->next;
  507. }
  508. rt_kprintf("--Variable List:\n");
  509. {
  510. struct finsh_sysvar *index;
  511. for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
  512. {
  513. #ifdef FINSH_USING_DESCRIPTION
  514. rt_kprintf("%-16s -- %s\n", index->name, index->desc);
  515. #else
  516. rt_kprintf("%s\n", index->name);
  517. #endif
  518. }
  519. }
  520. sysvar_item = global_sysvar_list;
  521. while (sysvar_item != NULL)
  522. {
  523. rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
  524. sysvar_item = sysvar_item->next;
  525. }
  526. return 0;
  527. }
  528. FINSH_FUNCTION_EXPORT(list, list all symbol in system)
  529. static int str_is_prefix(const char *prefix, const char *str)
  530. {
  531. while ((*prefix) && (*prefix == *str))
  532. {
  533. prefix ++;
  534. str ++;
  535. }
  536. if (*prefix == 0)
  537. return 0;
  538. return -1;
  539. }
  540. static int str_common(const char *str1, const char *str2)
  541. {
  542. const char *str = str1;
  543. while ((*str != 0) && (*str2 != 0) && (*str == *str2))
  544. {
  545. str ++;
  546. str2 ++;
  547. }
  548. return (str - str1);
  549. }
  550. void list_prefix(char *prefix)
  551. {
  552. struct finsh_syscall_item *syscall_item;
  553. struct finsh_sysvar_item *sysvar_item;
  554. rt_uint16_t func_cnt, var_cnt;
  555. int length, min_length;
  556. const char *name_ptr;
  557. func_cnt = 0;
  558. var_cnt = 0;
  559. name_ptr = RT_NULL;
  560. /* checks in system function call */
  561. {
  562. struct finsh_syscall* index;
  563. for (index = _syscall_table_begin; index < _syscall_table_end; _NEXT_SYSCALl(index))
  564. {
  565. if (str_is_prefix(prefix, index->name) == 0)
  566. {
  567. if (func_cnt == 0)
  568. {
  569. rt_kprintf("--function:\n");
  570. if (*prefix != 0)
  571. {
  572. /* set name_ptr */
  573. name_ptr = index->name;
  574. /* set initial length */
  575. min_length = strlen(name_ptr);
  576. }
  577. }
  578. func_cnt ++;
  579. if (*prefix != 0)
  580. {
  581. length = str_common(name_ptr, index->name);
  582. if (length < min_length)
  583. min_length = length;
  584. }
  585. #ifdef FINSH_USING_DESCRIPTION
  586. rt_kprintf("%-16s -- %s\n", index->name, index->desc);
  587. #else
  588. rt_kprintf("%s\n", index->name);
  589. #endif
  590. }
  591. }
  592. }
  593. /* checks in dynamic system function call */
  594. syscall_item = global_syscall_list;
  595. while (syscall_item != NULL)
  596. {
  597. if (str_is_prefix(prefix, syscall_item->syscall.name) == 0)
  598. {
  599. if (func_cnt == 0)
  600. {
  601. rt_kprintf("--function:\n");
  602. if (*prefix != 0 && name_ptr == NULL)
  603. {
  604. /* set name_ptr */
  605. name_ptr = syscall_item->syscall.name;
  606. /* set initial length */
  607. min_length = strlen(name_ptr);
  608. }
  609. }
  610. func_cnt ++;
  611. if (*prefix != 0)
  612. {
  613. length = str_common(name_ptr, syscall_item->syscall.name);
  614. if (length < min_length)
  615. min_length = length;
  616. }
  617. rt_kprintf("[l] %s\n", syscall_item->syscall.name);
  618. }
  619. syscall_item = syscall_item->next;
  620. }
  621. /* checks in system variable */
  622. {
  623. struct finsh_sysvar* index;
  624. for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
  625. {
  626. if (str_is_prefix(prefix, index->name) == 0)
  627. {
  628. if (var_cnt == 0)
  629. {
  630. rt_kprintf("--variable:\n");
  631. if (*prefix != 0 && name_ptr == NULL)
  632. {
  633. /* set name_ptr */
  634. name_ptr = index->name;
  635. /* set initial length */
  636. min_length = strlen(name_ptr);
  637. }
  638. }
  639. var_cnt ++;
  640. if (*prefix != 0)
  641. {
  642. length = str_common(name_ptr, index->name);
  643. if (length < min_length)
  644. min_length = length;
  645. }
  646. #ifdef FINSH_USING_DESCRIPTION
  647. rt_kprintf("%-16s -- %s\n", index->name, index->desc);
  648. #else
  649. rt_kprintf("%s\n", index->name);
  650. #endif
  651. }
  652. }
  653. }
  654. /* checks in dynamic system variable */
  655. sysvar_item = global_sysvar_list;
  656. while (sysvar_item != NULL)
  657. {
  658. if (str_is_prefix(prefix, sysvar_item->sysvar.name) == 0)
  659. {
  660. if (var_cnt == 0)
  661. {
  662. rt_kprintf("--variable:\n");
  663. if (*prefix != 0 && name_ptr == NULL)
  664. {
  665. /* set name_ptr */
  666. name_ptr = sysvar_item->sysvar.name;
  667. /* set initial length */
  668. min_length = strlen(name_ptr);
  669. }
  670. }
  671. var_cnt ++;
  672. if (*prefix != 0)
  673. {
  674. length = str_common(name_ptr, sysvar_item->sysvar.name);
  675. if (length < min_length)
  676. min_length = length;
  677. }
  678. rt_kprintf("[v] %s\n", sysvar_item->sysvar.name);
  679. }
  680. sysvar_item = sysvar_item->next;
  681. }
  682. /* only one matched */
  683. if (name_ptr != NULL)
  684. {
  685. rt_strncpy(prefix, name_ptr, min_length);
  686. }
  687. }
  688. #ifdef FINSH_USING_SYMTAB
  689. static int dummy = 0;
  690. FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
  691. #endif