cmd.c 23.3 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
27 28
 * 2010-04-21     yi.qiu       add list_module
 * 2012-04-29     goprife      improve the command line auto-complete feature.
29
 * 2012-06-02     lgnq         add list_memheap
30 31 32
 */

#include <rtthread.h>
33 34 35 36
#include "finsh.h"

rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
37 38
    unsigned int len = 0;
    const rt_list_t *p = l;
39
    while (p->next != l)
40 41
    {
        p = p->next;
42
        len ++;
43 44
    }
    return len;
M
mbbill 已提交
45
}
46

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

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

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

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

extern struct rt_object_information rt_object_container[];

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

    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);
    }
94
	
95
    return 0;
96
}
qiuyiuestc's avatar
qiuyiuestc 已提交
97 98 99

long list_thread(void)
{
100
    return _list_thread(&rt_object_container[RT_Object_Class_Thread].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
101
}
102
FINSH_FUNCTION_EXPORT(list_thread, list thread);
103

104
static void show_wait_queue(struct rt_list_node *list)
105
{
106 107 108 109 110 111 112
    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);
113 114 115
		
        if (node->next != list)
			rt_kprintf("/");
116
    }
117 118 119
}

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

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

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

long list_sem(void)
{
149
    return _list_sem(&rt_object_container[RT_Object_Class_Semaphore].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
150
}
151 152 153 154
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system)
#endif

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

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

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

long list_event(void)
{
183
    return _list_event(&rt_object_container[RT_Object_Class_Event].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
184
}
185 186 187 188
FINSH_FUNCTION_EXPORT(list_event, list event in system)
#endif

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

    rt_kprintf("mutex    owner    hold suspend thread\n");
    rt_kprintf("-------- -------- ---- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
198
        m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
199 200 201 202 203
        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));
    }

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

long list_mutex(void)
{
208
    return _list_mutex(&rt_object_container[RT_Object_Class_Mutex].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
209
}
210 211 212 213
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system)
#endif

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

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

    return 0;
239
}
qiuyiuestc's avatar
qiuyiuestc 已提交
240 241 242

long list_mailbox(void)
{
243
    return _list_mailbox(&rt_object_container[RT_Object_Class_MailBox].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
244
}
245 246 247 248
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system)
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
249
static long _list_msgqueue(struct rt_list_node *list)
250
{
251 252 253 254 255 256 257
    struct rt_messagequeue *m;
    struct rt_list_node *node;

    rt_kprintf("msgqueue entry suspend thread\n");
    rt_kprintf("-------- ----  --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
258 259
        m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
260 261 262 263 264 265 266 267 268 269 270 271 272 273
        {
            rt_kprintf("%-8.*s %04d  %d:", RT_NAME_MAX, 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("%-8.*s %04d  %d\n", RT_NAME_MAX, m->parent.parent.name, 
                m->entry, rt_list_len(&m->parent.suspend_thread));
        }
    }

    return 0;
274
}
qiuyiuestc's avatar
qiuyiuestc 已提交
275 276 277

long list_msgqueue(void)
{
278
    return _list_msgqueue(&rt_object_container[RT_Object_Class_MessageQueue].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
279
}
280 281 282
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system)
#endif

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
#ifdef RT_USING_MEMHEAP
static long _list_memheap(struct rt_list_node *list)
{
    struct rt_memheap *mh;
    struct rt_list_node *node;

    rt_kprintf("memheap  pool size available size\n");
    rt_kprintf("-------- --------- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
        mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);

        rt_kprintf("%-8.*s %04d  %04d\n", RT_NAME_MAX, mh->parent.name,
                mh->pool_size, mh->available_size);
    }

    return 0;
}

long list_memheap(void)
{
    return _list_memheap(&rt_object_container[RT_Object_Class_MemHeap].object_list);
}
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system)
#endif

309
#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
310
static long _list_mempool(struct rt_list_node *list)
311
{
312 313 314 315 316 317 318
    struct rt_mempool *mp;
    struct rt_list_node *node;

    rt_kprintf("mempool  block total free suspend thread\n");
    rt_kprintf("-------- ----  ----  ---- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
319
        mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        if (mp->suspend_thread_count > 0)
        {
            rt_kprintf("%-8.*s %04d  %04d  %04d %d:", RT_NAME_MAX, 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("%-8.*s %04d  %04d  %04d %d\n", RT_NAME_MAX, mp->parent.name,
                mp->block_size, mp->block_total_count, mp->block_free_count,
                mp->suspend_thread_count);
        }
    }

    return 0;
337
}
qiuyiuestc's avatar
qiuyiuestc 已提交
338 339 340

long list_mempool(void)
{
341
    return _list_mempool(&rt_object_container[RT_Object_Class_MemPool].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
342
}
343 344 345
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
346
static long _list_timer(struct rt_list_node *list)
347
{
348 349 350 351 352 353 354
    struct rt_timer *timer;
    struct rt_list_node *node;

    rt_kprintf("timer    periodic   timeout    flag\n");
    rt_kprintf("-------- ---------- ---------- -----------\n");
    for (node = list->next; node != list; node = node->next)
    {
355
        timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
356
        rt_kprintf("%-8.*s 0x%08x 0x%08x ", RT_NAME_MAX, timer->parent.name, timer->init_tick, timer->timeout_tick);
357 358 359 360
        if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
			rt_kprintf("activated\n");
        else
			rt_kprintf("deactivated\n");
361 362 363 364 365
    }

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

    return 0;
366
}
qiuyiuestc's avatar
qiuyiuestc 已提交
367 368 369

long list_timer(void)
{
370
    return _list_timer(&rt_object_container[RT_Object_Class_Timer].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
371
}
372 373 374
FINSH_FUNCTION_EXPORT(list_timer, list timer in system)

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
375
static long _list_device(struct rt_list_node *list)
376
{
377 378 379 380 381 382 383 384 385 386 387
    struct rt_device *device;
    struct rt_list_node *node;
    const char *device_type_str[] =
    {
        "Character Device",
        "Block Device",
        "Network Interface",
        "MTD Device",
        "CAN Device",
        "RTC",
        "Sound Device",
388 389
        "Graphic Device",
		"I2C Bus",
390 391 392 393 394 395 396 397 398 399 400 401 402
        "I2C Device",
        "USB Slave Device",
        "USB Host Bus",
        "SPI Bus",
        "SPI Device",
        "SDIO Bus",
        "Unknown"
    };

    rt_kprintf("device    type      \n");
    rt_kprintf("-------- ---------- \n");
    for (node = list->next; node != list; node = node->next)
    {
403
        device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
404 405 406 407 408 409
        rt_kprintf("%-8.*s %-8s \n", RT_NAME_MAX, device->parent.name,
            (device->type <= RT_Device_Class_Unknown)?
            device_type_str[device->type]:device_type_str[RT_Device_Class_Unknown]);
    }

    return 0;
410
}
qiuyiuestc's avatar
qiuyiuestc 已提交
411 412 413

long list_device(void)
{
414
    return _list_device(&rt_object_container[RT_Object_Class_Device].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
415
}
416 417 418
FINSH_FUNCTION_EXPORT(list_device, list device in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
419
#ifdef RT_USING_MODULE
qiuyiuestc's avatar
qiuyiuestc 已提交
420 421
#include <rtm.h>

422
int list_module(void)
qiuyiuestc's avatar
qiuyiuestc 已提交
423
{
424 425
    struct rt_module *module;
    struct rt_list_node *list, *node;
qiuyiuestc's avatar
qiuyiuestc 已提交
426

427
    list = &rt_object_container[RT_Object_Class_Module].object_list;
qiuyiuestc's avatar
qiuyiuestc 已提交
428

429 430 431 432
    rt_kprintf("module name     ref\n");
    rt_kprintf("------------ --------\n");
    for (node = list->next; node != list; node = node->next)
    {
433
        module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
434
        rt_kprintf("%-16.*s %-04d\n", RT_NAME_MAX, module->parent.name, module->nref);
435
    }
M
mbbill@gmail.com 已提交
436

437
    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
438
}
qiuyiuestc's avatar
qiuyiuestc 已提交
439

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

442
int list_mod_detail(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
443
{
444 445 446 447
    int i;
    struct rt_module *module;
    
    /* find module */
448
    if ((module = rt_module_find(name)) != RT_NULL)
449 450
    {
        /* module has entry point */
451
        if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
452 453 454
        {   
            struct rt_thread *thread;
            struct rt_list_node *tlist;
455
            rt_uint8_t *ptr;
456 457

            /* list main thread in module */
458
            if (module->module_thread != RT_NULL)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
            {   
                rt_kprintf("main thread  pri  status      sp     stack size max used   left tick  error\n");
                rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
                thread = module->module_thread;
                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   ");

                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;
482
            if (!rt_list_isempty(tlist)) _list_thread(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
483
#ifdef RT_USING_SEMAPHORE
484 485
            /* list semaphored in module */
            tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
486
            if (!rt_list_isempty(tlist)) _list_sem(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
487 488
#endif
#ifdef RT_USING_MUTEX
489 490
            /* list mutex in module */
            tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
491
            if (!rt_list_isempty(tlist)) _list_mutex(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
492 493
#endif
#ifdef RT_USING_EVENT
494 495
            /* list event in module */
            tlist = &module->module_object[RT_Object_Class_Event].object_list;
496
            if (!rt_list_isempty(tlist)) _list_event(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
497 498
#endif
#ifdef RT_USING_MAILBOX
499 500
            /* list mailbox in module */
            tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
501
            if (!rt_list_isempty(tlist)) _list_mailbox(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
502 503
#endif
#ifdef RT_USING_MESSAGEQUEUE
504 505
            /* list message queue in module */
            tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
506 507 508 509 510 511
            if (!rt_list_isempty(tlist)) _list_msgqueue(tlist);
#endif
#ifdef RT_USING_MEMHEAP
            /* list memory heap in module */
            tlist = &module->module_object[RT_Object_Class_MemHeap].object_list;
            if (!rt_list_isempty(tlist)) _list_memheap(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
512 513
#endif
#ifdef RT_USING_MEMPOOL
514 515
            /* list memory pool in module */
            tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
516
            if (!rt_list_isempty(tlist)) _list_mempool(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
517 518
#endif
#ifdef RT_USING_DEVICE
519 520
            /* list device in module */
            tlist = &module->module_object[RT_Object_Class_Device].object_list;
521
            if (!rt_list_isempty(tlist)) _list_device(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
522
#endif
523 524
            /* list timer in module */
            tlist = &module->module_object[RT_Object_Class_Timer].object_list;
525
            if (!rt_list_isempty(tlist)) _list_timer(tlist);
526 527 528 529 530 531
        }

        rt_kprintf("symbol    address   \n");
        rt_kprintf("-------- ----------\n");
    
        /* list module export symbols */
532
        for (i=0; i<module->nsym; i++)
533 534 535 536 537 538
        {
            rt_kprintf("%s 0x%x\n", module->symtab[i].name, module->symtab[i].addr);
        }   
    }

    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
539
}
qiuyiuestc's avatar
qiuyiuestc 已提交
540
FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
qiuyiuestc's avatar
qiuyiuestc 已提交
541 542
#endif

D
dzzxzz 已提交
543
long list(void)
544
{
545 546
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
547 548 549

    rt_kprintf("--Function List:\n");
    {
550
        struct finsh_syscall *index;
551 552
        for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
        {
553
#ifdef FINSH_USING_DESCRIPTION
554
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
555
#else
556
            rt_kprintf("%s\n", index->name);
557
#endif
558 559 560 561 562 563 564 565 566 567 568 569 570
        }
    }

    /* 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");
    {
571
        struct finsh_sysvar *index;
572 573
        for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
        {
574
#ifdef FINSH_USING_DESCRIPTION
575
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
576
#else
577
            rt_kprintf("%s\n", index->name);
578
#endif
579 580
        }
    }
581

582 583 584 585 586 587
    sysvar_item = global_sysvar_list;
    while (sysvar_item != NULL)
    {
        rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
        sysvar_item = sysvar_item->next;
    }
588

589
    return 0;
590 591 592
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

593
static int str_is_prefix(const char *prefix, const char *str)
594
{
595 596 597 598 599 600
    while ((*prefix) && (*prefix == *str))
    {
        prefix ++;
        str ++;
    }

601 602 603
    if (*prefix == 0)
		return 0;
	
604 605 606
    return -1;
}

607
static int str_common(const char *str1, const char *str2)
608
{
609
    const char *str = str1;
610

611
    while ((*str != 0) && (*str2 != 0) && (*str == *str2))
612 613 614 615 616 617
    {
        str ++;
        str2 ++;
    }

    return (str - str1);
618 619
}

620
void list_prefix(char *prefix)
621
{
622 623
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
624 625
    rt_uint16_t func_cnt, var_cnt;
    int length, min_length;
626
    const char *name_ptr;
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

    func_cnt = 0;
    var_cnt  = 0;
    name_ptr = RT_NULL;

    /* checks in system function call */
    {
        struct finsh_syscall* index;
        for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
        {
            if (str_is_prefix(prefix, index->name) == 0)
            {
                if (func_cnt == 0)
                {
                    rt_kprintf("--function:\n");

                    if (*prefix != 0)
                    {
                        /* set name_ptr */
                        name_ptr = index->name;

                        /* set initial length */
                        min_length = strlen(name_ptr);
                    }
                }

                func_cnt ++;

                if (*prefix != 0)
                {
                    length = str_common(name_ptr, index->name);
                    if (length < min_length)
                        min_length = length;
                }
661 662

#ifdef FINSH_USING_DESCRIPTION
663
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
664
#else
665
                rt_kprintf("%s\n", index->name);
666
#endif
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
            }
        }
    }

    /* checks in dynamic system function call */
    syscall_item = global_syscall_list;
    while (syscall_item != NULL)
    {
        if (str_is_prefix(prefix, syscall_item->syscall.name) == 0)
        {
            if (func_cnt == 0)
            {
                rt_kprintf("--function:\n");
                if (*prefix != 0 && name_ptr == NULL)
                {
                    /* set name_ptr */
                    name_ptr = syscall_item->syscall.name;

                    /* set initial length */
                    min_length = strlen(name_ptr);
                }
            }

            func_cnt ++;

            if (*prefix != 0)
            {
                length = str_common(name_ptr, syscall_item->syscall.name);
                if (length < min_length)
                    min_length = length;
            }

            rt_kprintf("[l] %s\n", syscall_item->syscall.name);
        }
        syscall_item = syscall_item->next;
    }

    /* checks in system variable */
    {
        struct finsh_sysvar* index;
        for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
        {
            if (str_is_prefix(prefix, index->name) == 0)
            {
                if (var_cnt == 0)
                {
                    rt_kprintf("--variable:\n");

                    if (*prefix != 0 && name_ptr == NULL)
                    {
                        /* set name_ptr */
                        name_ptr = index->name;

                        /* set initial length */
                        min_length = strlen(name_ptr);

                    }
                }

                var_cnt ++;

                if (*prefix != 0)
                {
                    length = str_common(name_ptr, index->name);
                    if (length < min_length)
                        min_length = length;
                }
B
bernard.xiong 已提交
734

735
#ifdef FINSH_USING_DESCRIPTION
736
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
737
#else
738
                rt_kprintf("%s\n", index->name);
739
#endif
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
            }
        }
    }

    /* checks in dynamic system variable */
    sysvar_item = global_sysvar_list;
    while (sysvar_item != NULL)
    {
        if (str_is_prefix(prefix, sysvar_item->sysvar.name) == 0)
        {
            if (var_cnt == 0)
            {
                rt_kprintf("--variable:\n");
                if (*prefix != 0 && name_ptr == NULL)
                {
                    /* set name_ptr */
                    name_ptr = sysvar_item->sysvar.name;

                    /* set initial length */
                    min_length = strlen(name_ptr);
                }
            }

            var_cnt ++;

            if (*prefix != 0)
            {
                length = str_common(name_ptr, sysvar_item->sysvar.name);
                if (length < min_length)
                    min_length = length;
            }

            rt_kprintf("[v] %s\n", sysvar_item->sysvar.name);
        }
        sysvar_item = sysvar_item->next;
    }

    /* only one matched */
    if (name_ptr != NULL)
    {
        rt_strncpy(prefix, name_ptr, min_length);
    }
782 783
}

784 785 786 787
#ifdef FINSH_USING_SYMTAB
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif