cmd.c 24 KB

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