cmd.c 17.7 KB
Newer Older
1 2 3 4 5 6 7
/*
 * File      : cmd.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-04-30     Bernard      first implementation
 * 2006-05-04     Bernard      add list_thread,
 *                                 list_sem,
 *                                 list_timer
 * 2006-05-20     Bernard      add list_mutex,
 *                                 list_mailbox,
 *                                 list_msgqueue,
 *                                 list_event,
 *                                 list_fevent,
 *                                 list_mempool
 * 2006-06-03     Bernard      display stack information in list_thread
 * 2006-08-10     Bernard      change version to invoke rt_show_version
 * 2008-09-10     Bernard      update the list function for finsh syscall
 *                                 list and sysvar list
 * 2009-05-30     Bernard      add list_device
qiuyiuestc's avatar
qiuyiuestc 已提交
27
 * 2010-04-21     yi.qiu          add list_module
28 29 30
 */

#include <rtthread.h>
31 32 33 34 35 36 37 38 39 40 41 42
#include "finsh.h"

rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
	unsigned int len = 0;
	const rt_list_t *p = l;
	while( p->next != l )
	{
		p = p->next;
		len++;
	}
	return len;
M
mbbill 已提交
43
}
44

45
long hello(void)
46 47 48 49 50
{
	rt_kprintf("Hello RT-Thread!\n");

	return 0;
}
51
FINSH_FUNCTION_EXPORT(hello, say hello world);
52 53

extern void rt_show_version(void);
54
long version(void)
55 56 57 58 59
{
	rt_show_version();

	return 0;
}
60
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
61 62 63 64 65

#define rt_list_entry(node, type, member) \
    ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
extern struct rt_object_information rt_object_container[];

qiuyiuestc's avatar
qiuyiuestc 已提交
66
static long _list_thread(struct rt_list_node* list)
67
{
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    struct rt_thread *thread;
    struct rt_list_node *node;
    rt_uint8_t* ptr;

    rt_kprintf(" thread  pri  status      sp     stack size max used   left tick  error\n");
    rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
    for (node = list->next; node != list; node = node->next)
    {
        thread = rt_list_entry(node, struct rt_thread, list);
        rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);

        if (thread->stat == RT_THREAD_READY)        rt_kprintf(" ready  ");
        else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
        else if (thread->stat == RT_THREAD_INIT)    rt_kprintf(" init   ");
        else if (thread->stat == RT_THREAD_CLOSE)   rt_kprintf(" close  ");

        ptr = (rt_uint8_t*)thread->stack_addr;
        while (*ptr == '#')ptr ++;

        rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
            thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
            thread->stack_size,
            thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
            thread->remaining_tick,
            thread->error);

    }
    return 0;
96
}
qiuyiuestc's avatar
qiuyiuestc 已提交
97 98 99 100 101

long list_thread(void)
{
	return _list_thread(&rt_object_container[RT_Object_Class_Thread].object_list);
}
102
FINSH_FUNCTION_EXPORT(list_thread, list thread);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

static void show_wait_queue(struct rt_list_node* list)
{
	struct rt_thread *thread;
	struct rt_list_node *node;

	for (node = list->next; node != list; node = node->next)
	{
		thread = rt_list_entry(node, struct rt_thread, tlist);
		rt_kprintf("%s", thread->name);
		if (node->next != list) rt_kprintf("/");
	}
}

#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
118
static long _list_sem(struct rt_list_node *list)
119 120
{
	struct rt_semaphore *sem;
qiuyiuestc's avatar
qiuyiuestc 已提交
121
	struct rt_list_node *node;
122 123 124 125 126

	rt_kprintf("semaphore v   suspend thread\n");
	rt_kprintf("--------  --- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
127
		sem = (struct rt_semaphore*)(rt_list_entry(node, struct rt_object, list));
M
mbbill 已提交
128
		if( !rt_list_isempty(&sem->parent.suspend_thread) )
129
		{
130 131
			rt_kprintf("%-8.*s  %03d %d:", RT_NAME_MAX, sem->parent.parent.name, sem->value,
					rt_list_len(&sem->parent.suspend_thread) );
132 133 134 135 136
			show_wait_queue(&(sem->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
137 138
			rt_kprintf("%-8.*s  %03d %d\n", RT_NAME_MAX, sem->parent.parent.name, sem->value,
					rt_list_len(&sem->parent.suspend_thread));
139 140 141 142 143
		}
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
144 145 146 147 148

long list_sem(void)
{
	return _list_sem(&rt_object_container[RT_Object_Class_Semaphore].object_list);
}
149 150 151 152
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system)
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
153
static long _list_event(struct rt_list_node *list)
154 155
{
	struct rt_event *e;
qiuyiuestc's avatar
qiuyiuestc 已提交
156
	struct rt_list_node *node;
157 158 159 160 161 162

	rt_kprintf("event    set        suspend thread\n");
	rt_kprintf("-------- ---------- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		e = (struct rt_event*)(rt_list_entry(node, struct rt_object, list));
163 164
		if( !rt_list_isempty(&e->parent.suspend_thread) )
		{
165
			rt_kprintf("%-8s  0x%08x %03d:", e->parent.parent.name, e->set, rt_list_len(&e->parent.suspend_thread));
166 167 168 169 170 171 172
			show_wait_queue(&(e->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
			rt_kprintf("%-8s  0x%08x 0\n", e->parent.parent.name, e->set);
		}
173 174 175 176
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
177 178 179 180 181

long list_event(void)
{
	return _list_event(&rt_object_container[RT_Object_Class_Event].object_list);
}
182 183 184 185
FINSH_FUNCTION_EXPORT(list_event, list event in system)
#endif

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
186
static long _list_mutex(struct rt_list_node *list)
187 188
{
	struct rt_mutex *m;
qiuyiuestc's avatar
qiuyiuestc 已提交
189
	struct rt_list_node *node;
190 191 192 193 194 195

	rt_kprintf("mutex    owner    hold suspend thread\n");
	rt_kprintf("-------- -------- ---- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		m = (struct rt_mutex*)(rt_list_entry(node, struct rt_object, list));
196 197
		rt_kprintf("%-8.*s %-8.*s %04d %d\n", RT_NAME_MAX, m->parent.parent.name,
				RT_NAME_MAX, m->owner->name, m->hold, rt_list_len(&m->parent.suspend_thread));
198 199 200 201
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
202 203 204 205 206

long list_mutex(void)
{
	return _list_mutex(&rt_object_container[RT_Object_Class_Mutex].object_list);
}
207 208 209 210
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system)
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
211
static long _list_mailbox(struct rt_list_node *list)
212 213
{
	struct rt_mailbox *m;
qiuyiuestc's avatar
qiuyiuestc 已提交
214
	struct rt_list_node *node;
215 216 217 218 219

	rt_kprintf("mailbox  entry size suspend thread\n");
	rt_kprintf("-------- ----  ---- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
220
		m = (struct rt_mailbox*)(rt_list_entry(node, struct rt_object, list));
M
mbbill 已提交
221
		if( !rt_list_isempty(&m->parent.suspend_thread) )
222
		{
M
mbbill 已提交
223
			rt_kprintf("%-8s %04d  %04d %d:", m->parent.parent.name, m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
224 225 226 227 228
			show_wait_queue(&(m->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
M
mbbill 已提交
229
			rt_kprintf("%-8s %04d  %04d %d\n", m->parent.parent.name, m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
230 231 232 233 234
		}
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
235 236 237 238 239

long list_mailbox(void)
{
	return _list_mailbox(&rt_object_container[RT_Object_Class_MailBox].object_list);
}
240 241 242 243
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system)
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
244
static long _list_msgqueue(struct rt_list_node *list)
245 246
{
	struct rt_messagequeue *m;
qiuyiuestc's avatar
qiuyiuestc 已提交
247
	struct rt_list_node *node;
248 249 250 251 252 253

	rt_kprintf("msgqueue entry suspend thread\n");
	rt_kprintf("-------- ----  --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		m = (struct rt_messagequeue*)(rt_list_entry(node, struct rt_object, list));
254 255 256 257 258 259 260 261 262 263
		if( !rt_list_isempty(&m->parent.suspend_thread) )
		{
			rt_kprintf("%-8s %04d  %d:", m->parent.parent.name, m->entry, rt_list_len(&m->parent.suspend_thread));
			show_wait_queue(&(m->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
			rt_kprintf("%-8s %04d  %d\n", m->parent.parent.name, m->entry, rt_list_len(&m->parent.suspend_thread));
		}
264 265 266 267
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
268 269 270 271 272

long list_msgqueue(void)
{
	return _list_msgqueue(&rt_object_container[RT_Object_Class_MessageQueue].object_list);
}
273 274 275 276
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system)
#endif

#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
277
static long _list_mempool(struct rt_list_node *list)
278 279
{
	struct rt_mempool *mp;
qiuyiuestc's avatar
qiuyiuestc 已提交
280
	struct rt_list_node *node;
281 282 283 284 285 286

	rt_kprintf("mempool  block total free suspend thread\n");
	rt_kprintf("-------- ----  ----  ---- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		mp = (struct rt_mempool*)rt_list_entry(node, struct rt_object, list);
287 288 289 290 291 292 293 294 295 296 297 298 299 300
		if (mp->suspend_thread_count > 0)
		{
			rt_kprintf("%-8s %04d  %04d  %04d %d:", mp->parent.name,
				mp->block_size, mp->block_total_count, mp->block_free_count,
				mp->suspend_thread_count);
			show_wait_queue(&(mp->suspend_thread));
			rt_kprintf("\n");			
		}
		else
		{
			rt_kprintf("%-8s %04d  %04d  %04d %d\n", mp->parent.name,
				mp->block_size, mp->block_total_count, mp->block_free_count,
				mp->suspend_thread_count);
		}
301 302 303 304
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
305 306 307 308 309

long list_mempool(void)
{
	return _list_mempool(&rt_object_container[RT_Object_Class_MemPool].object_list);
}
310 311 312
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
313
static long _list_timer(struct rt_list_node *list)
314 315
{
	struct rt_timer *timer;
qiuyiuestc's avatar
qiuyiuestc 已提交
316
	struct rt_list_node *node;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

	rt_kprintf("timer    periodic   timeout    flag\n");
	rt_kprintf("-------- ---------- ---------- -----------\n");
	for (node = list->next; node != list; node = node->next)
	{
		timer = (struct rt_timer*)(rt_list_entry(node, struct rt_object, list));
		rt_kprintf("%-8s 0x%08x 0x%08x ", timer->parent.name, timer->init_tick, timer->timeout_tick);
		if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) rt_kprintf("activated\n");
		else rt_kprintf("deactivated\n");
	}

	rt_kprintf("current tick:0x%08x\n", rt_tick_get());

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
332 333 334 335 336

long list_timer(void)
{
	return _list_timer(&rt_object_container[RT_Object_Class_Timer].object_list);
}
337 338 339
FINSH_FUNCTION_EXPORT(list_timer, list timer in system)

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
340
static long _list_device(struct rt_list_node *list)
341 342
{
	struct rt_device *device;
qiuyiuestc's avatar
qiuyiuestc 已提交
343
	struct rt_list_node *node;
B
bernard.xiong 已提交
344
	const char *device_type_str[] =
345 346 347 348 349
	{
		"Character Device",
		"Block Device",
		"Network Interface",
		"MTD Device",
350
		"CAN Device",
351
		"RTC",
352 353 354 355 356
		"Sound Device",
		"Graphic Device",
		"I2C Device",
		"USB Slave Device",
		"USB Host Bus",
wuyangyong's avatar
wuyangyong 已提交
357 358 359
		"SPI Bus",
		"SPI Device",
		"SDIO Bus",
360 361 362 363 364 365 366 367
		"Unknown"
	};

	rt_kprintf("device    type      \n");
	rt_kprintf("-------- ---------- \n");
	for (node = list->next; node != list; node = node->next)
	{
		device = (struct rt_device*)(rt_list_entry(node, struct rt_object, list));
wuyangyong's avatar
wuyangyong 已提交
368 369
		rt_kprintf("%-8s %-8s \n", device->parent.name,
             (device->type <= RT_Device_Class_Unknown)?device_type_str[device->type]:device_type_str[RT_Device_Class_Unknown]);
370 371 372 373
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
374 375 376 377 378

long list_device(void)
{
	return _list_device(&rt_object_container[RT_Object_Class_Device].object_list);
}
379 380 381
FINSH_FUNCTION_EXPORT(list_device, list device in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
382
#ifdef RT_USING_MODULE
qiuyiuestc's avatar
qiuyiuestc 已提交
383 384
#include <rtm.h>

385
int list_module(void)
qiuyiuestc's avatar
qiuyiuestc 已提交
386 387 388 389 390 391
{
	struct rt_module *module;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_Module].object_list;

qiuyiuestc's avatar
qiuyiuestc 已提交
392 393
	rt_kprintf("module name     ref\n");
	rt_kprintf("------------ --------\n");
qiuyiuestc's avatar
qiuyiuestc 已提交
394 395
	for (node = list->next; node != list; node = node->next)
	{
396
		module = (struct rt_module*)(rt_list_entry(node, struct rt_object, list));
qiuyiuestc's avatar
qiuyiuestc 已提交
397 398 399
		rt_kprintf("%-16s ", module->parent.name);
		rt_kprintf("%-04d \n", module->nref);
	}
M
mbbill@gmail.com 已提交
400 401 402

	return 0;

qiuyiuestc's avatar
qiuyiuestc 已提交
403
}
qiuyiuestc's avatar
qiuyiuestc 已提交
404

qiuyiuestc's avatar
qiuyiuestc 已提交
405
FINSH_FUNCTION_EXPORT(list_module, list module in system)
qiuyiuestc's avatar
qiuyiuestc 已提交
406

qiuyiuestc's avatar
qiuyiuestc 已提交
407
int list_mod_detail(const char* name)
qiuyiuestc's avatar
qiuyiuestc 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
{
	int i;
	struct rt_module *module;
	
	/* find module */
	if((module = rt_module_find(name)) != RT_NULL)
	{
		/* module has entry point */
		if(!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
		{	
			struct rt_thread *thread;
			struct rt_list_node *tlist;
			rt_uint8_t* ptr;

			/* list main thread in module */
			if(module->module_thread != RT_NULL)
			{	
				rt_kprintf("main thread  pri  status      sp     stack size max used   left tick  error\n");
				rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
				thread = module->module_thread;
				rt_kprintf("%-8s 0x%02x", thread->name, thread->current_priority);

				if (thread->stat == RT_THREAD_READY)		rt_kprintf(" ready  ");
				else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
				else if (thread->stat == RT_THREAD_INIT)	rt_kprintf(" init   ");

				ptr = (rt_uint8_t*)thread->stack_addr;
				while (*ptr == '#')ptr ++;

				rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
					thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
					thread->stack_size,
					thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
					thread->remaining_tick,
					thread->error);
			}	

			/* list sub thread in module */
			tlist = &module->module_object[RT_Object_Class_Thread].object_list;
			if(!rt_list_isempty(tlist)) _list_thread(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
448
#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
449 450 451
			/* list semaphored in module */
			tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
			if(!rt_list_isempty(tlist)) _list_sem(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
452 453
#endif
#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
454 455 456
			/* list mutex in module */
			tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
			if(!rt_list_isempty(tlist)) _list_mutex(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
457 458
#endif
#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
459 460 461
			/* list event in module */
			tlist = &module->module_object[RT_Object_Class_Event].object_list;
			if(!rt_list_isempty(tlist)) _list_event(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
462 463
#endif
#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
464 465 466
			/* list mailbox in module */
			tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
			if(!rt_list_isempty(tlist)) _list_mailbox(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
467 468
#endif
#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
469 470 471
			/* list message queue in module */
			tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
			if(!rt_list_isempty(tlist)) _list_msgqueue(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
472 473
#endif
#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
474 475 476
			/* list memory pool in module */
			tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
			if(!rt_list_isempty(tlist)) _list_mempool(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
477 478
#endif
#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
479 480 481
			/* list device in module */
			tlist = &module->module_object[RT_Object_Class_Device].object_list;
			if(!rt_list_isempty(tlist)) _list_device(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
482
#endif
qiuyiuestc's avatar
qiuyiuestc 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495
			/* list timer in module */
			tlist = &module->module_object[RT_Object_Class_Timer].object_list;
			if(!rt_list_isempty(tlist)) _list_timer(tlist);
		}

		rt_kprintf("symbol    address   \n");
		rt_kprintf("-------- ----------\n");
	
		/* list module export symbols */
		for(i=0; i<module->nsym; i++)
		{
			rt_kprintf("%s 0x%x\n", module->symtab[i].name, module->symtab[i].addr);
		}	
qiuyiuestc's avatar
qiuyiuestc 已提交
496 497 498 499
	}

	return 0;
}
qiuyiuestc's avatar
qiuyiuestc 已提交
500
FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
qiuyiuestc's avatar
qiuyiuestc 已提交
501 502
#endif

D
dzzxzz 已提交
503
long list(void)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
{
	struct finsh_syscall_item* syscall_item;
	struct finsh_sysvar_item*  sysvar_item;

	rt_kprintf("--Function List:\n");
	{
		struct finsh_syscall* index;
		for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
		{
#ifdef FINSH_USING_DESCRIPTION
			rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
			rt_kprintf("%s\n", index->name);
#endif
		}
	}

	/* list syscall list */
	syscall_item = global_syscall_list;
	while (syscall_item != NULL)
	{
		rt_kprintf("[l] %s\n", syscall_item->syscall.name);
		syscall_item = syscall_item->next;
	}

	rt_kprintf("--Variable List:\n");
	{
		struct finsh_sysvar* index;
		for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
		{
#ifdef FINSH_USING_DESCRIPTION
			rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
			rt_kprintf("%s\n", index->name);
#endif
		}
	}

	sysvar_item = global_sysvar_list;
	while (sysvar_item != NULL)
	{
		rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
		sysvar_item = sysvar_item->next;
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

553
static int str_is_prefix(const char* prefix, const char* str)
554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
	while ((*prefix) && (*prefix == *str))
	{
		prefix ++;
		str ++;
	}

	if (*prefix == 0) return 0;
	return -1;
}

void list_prefix(char* prefix)
{
	struct finsh_syscall_item* syscall_item;
568 569 570 571 572
	struct finsh_sysvar_item*  sysvar_item;
	rt_uint16_t func_cnt, var_cnt;
	const char* name_ptr;

	func_cnt = 0;
573 574
	var_cnt  = 0;
	name_ptr = RT_NULL;
B
bernard.xiong 已提交
575

576 577 578 579 580
	{
		struct finsh_syscall* index;
		for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
		{
			if (str_is_prefix(prefix, index->name) == 0)
581
			{
582
				if (func_cnt == 0)
583 584 585 586 587
					rt_kprintf("--function:\n");

				func_cnt ++;
				/* set name_ptr */
				name_ptr = index->name;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

#ifdef FINSH_USING_DESCRIPTION
				rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
				rt_kprintf("%s\n", index->name);
#endif
			}
		}
	}

	/* list syscall list */
	syscall_item = global_syscall_list;
	while (syscall_item != NULL)
	{
		if (str_is_prefix(prefix, syscall_item->syscall.name) == 0)
603
		{
604 605
			if (func_cnt == 0)
				rt_kprintf("--function:\n");
606 607 608 609
			func_cnt ++;
			/* set name_ptr */
			name_ptr = syscall_item->syscall.name;

610 611 612 613 614 615 616 617 618 619
			rt_kprintf("[l] %s\n", syscall_item->syscall.name);
		}
		syscall_item = syscall_item->next;
	}

	{
		struct finsh_sysvar* index;
		for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
		{
			if (str_is_prefix(prefix, index->name) == 0)
620 621 622
			{
				if (var_cnt == 0)
					rt_kprintf("--variable:\n");
623

624 625 626
				var_cnt ++;
				/* set name ptr */
				name_ptr = index->name;
B
bernard.xiong 已提交
627

628 629 630 631 632 633 634 635 636 637 638 639 640 641
#ifdef FINSH_USING_DESCRIPTION
				rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
				rt_kprintf("%s\n", index->name);
#endif
			}
		}
	}

	sysvar_item = global_sysvar_list;
	while (sysvar_item != NULL)
	{
		if (str_is_prefix(prefix, sysvar_item->sysvar.name) == 0)
		{
642 643 644 645 646 647 648
			if (var_cnt == 0)
				rt_kprintf("--variable:\n");

			var_cnt ++;
			/* set name ptr */
			name_ptr = sysvar_item->sysvar.name;

649 650 651
			rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
		}
		sysvar_item = sysvar_item->next;
652 653 654 655 656 657
	}

	/* only one matched */
	if ((func_cnt + var_cnt) == 1)
	{
		rt_strncpy(prefix, name_ptr, strlen(name_ptr));
658 659 660
	}
}

661 662 663 664
#ifdef FINSH_USING_SYMTAB
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif