1
0

cmd.c 23 KB

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