cmd.c 24.7 KB
Newer Older
1
/*
B
Bernard Xiong 已提交
2
 *  RT-Thread finsh shell commands
3
 *
B
Bernard Xiong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
 *
 *  This file is part of RT-Thread (http://www.rt-thread.org)
 *  Maintainer: bernard.xiong <bernard.xiong at gmail.com>
 *
 *  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * 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
42 43
 * 2010-04-21     yi.qiu       add list_module
 * 2012-04-29     goprife      improve the command line auto-complete feature.
44
 * 2012-06-02     lgnq         add list_memheap
45
 * 2012-10-22     Bernard      add MS VC++ patch.
46
 * 2016-06-02     armink       beautify the list_thread command
47 48 49
 */

#include <rtthread.h>
50 51
#include "finsh.h"

52
long hello(void)
53
{
54
    rt_kprintf("Hello RT-Thread!\n");
55

56
    return 0;
57
}
58
FINSH_FUNCTION_EXPORT(hello, say hello world);
59 60

extern void rt_show_version(void);
61
long version(void)
62
{
63
    rt_show_version();
64

65
    return 0;
66
}
67
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
B
Bernard Xiong 已提交
68
MSH_CMD_EXPORT(version, show RT-Thread version information);
69

70
static int object_name_maxlen(const char *type_name, struct rt_list_node *list)
71
{
72
    struct rt_list_node *node;
73
    struct rt_object *object = NULL;
74
    int max_length = rt_strlen(type_name), length;
75

76 77 78 79
    rt_enter_critical();
    for (node = list->next; node != list; node = node->next)
    {
        object = rt_list_entry(node, struct rt_object, list);
80

81 82
        length = rt_strlen(object->name);
        if (length > max_length) max_length = length;
83
    }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    rt_exit_critical();

    if (max_length > RT_NAME_MAX || max_length == 0) max_length = RT_NAME_MAX;

    return max_length;
}

rt_inline void object_split(int len)
{
    while (len--) rt_kprintf("-");
}

static long _list_thread(struct rt_list_node *list)
{
    int maxlen;
    rt_uint8_t *ptr;
    struct rt_thread *thread;
    struct rt_list_node *node;
102
    const char *item_title = "thread";
103

104
    maxlen = object_name_maxlen(item_title, list);
105

106
    rt_kprintf("%-*.s pri  status      sp     stack size max used left tick  error\n", maxlen, item_title); object_split(maxlen);
107
    rt_kprintf(     " ---  ------- ---------- ----------  ------  ---------- ---\n");
108 109
    for (node = list->next; node != list; node = node->next)
    {
110
        rt_uint8_t stat;
111
        thread = rt_list_entry(node, struct rt_thread, list);
112
        rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority);
113

114
        stat = (thread->stat & RT_THREAD_STAT_MASK);
115 116 117 118
        if (stat == RT_THREAD_READY)        rt_kprintf(" ready  ");
        else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
        else if (stat == RT_THREAD_INIT)    rt_kprintf(" init   ");
        else if (stat == RT_THREAD_CLOSE)   rt_kprintf(" close  ");
119

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

123
        rt_kprintf(" 0x%08x 0x%08x    %02d%%   0x%08x %03d\n",
124 125
                   thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
                   thread->stack_size,
126 127
                   (thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100
                        / thread->stack_size,
128 129
                   thread->remaining_tick,
                   thread->error);
130
    }
131

132
    return 0;
133
}
qiuyiuestc's avatar
qiuyiuestc 已提交
134 135 136

long list_thread(void)
{
137 138 139 140
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Thread);
    return _list_thread(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
141
}
142
FINSH_FUNCTION_EXPORT(list_thread, list thread);
B
Bernard Xiong 已提交
143
MSH_CMD_EXPORT(list_thread, list thread);
144

145
static void show_wait_queue(struct rt_list_node *list)
146
{
147 148 149 150 151 152 153
    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);
D
dzzxzz@gmail.com 已提交
154

155
        if (node->next != list)
D
dzzxzz@gmail.com 已提交
156
            rt_kprintf("/");
157
    }
158 159 160
}

#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
161
static long _list_sem(struct rt_list_node *list)
162
{
163
    int maxlen;
164 165
    struct rt_semaphore *sem;
    struct rt_list_node *node;
166
    const char *item_title = "semaphore";
167

168
    maxlen = object_name_maxlen(item_title, list);
169

170
    rt_kprintf("%-*.s v   suspend thread\n", maxlen, item_title); object_split(maxlen);
171
    rt_kprintf(     " --- --------------\n");
172 173
    for (node = list->next; node != list; node = node->next)
    {
174
        sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
175
        if (!rt_list_isempty(&sem->parent.suspend_thread))
176
        {
177 178
            rt_kprintf("%-*.*s %03d %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
179 180 181
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
182 183 184 185 186
            show_wait_queue(&(sem->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
187 188
            rt_kprintf("%-*.*s %03d %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
189 190 191
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
192 193 194 195
        }
    }

    return 0;
196
}
qiuyiuestc's avatar
qiuyiuestc 已提交
197 198 199

long list_sem(void)
{
200 201 202 203 204
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Semaphore);

    return _list_sem(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
205
}
B
Bernard Xiong 已提交
206 207
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system);
MSH_CMD_EXPORT(list_sem, list semaphore in system);
208 209 210
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
211
static long _list_event(struct rt_list_node *list)
212
{
213
    int maxlen;
214 215
    struct rt_event *e;
    struct rt_list_node *node;
216
    const char *item_title = "event";
217

218
    maxlen = object_name_maxlen(item_title, list);
219

220
    rt_kprintf("%-*.s      set    suspend thread\n", maxlen, item_title); object_split(maxlen);
221
    rt_kprintf(     "  ---------- --------------\n");
222 223
    for (node = list->next; node != list; node = node->next)
    {
224 225
        e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&e->parent.suspend_thread))
226
        {
227 228
            rt_kprintf("%-*.*s  0x%08x %03d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
229 230 231
                       e->parent.parent.name,
                       e->set,
                       rt_list_len(&e->parent.suspend_thread));
232 233 234 235 236
            show_wait_queue(&(e->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
237 238
            rt_kprintf("%-*.*s  0x%08x 0\n",
                       maxlen, RT_NAME_MAX, e->parent.parent.name, e->set);
239 240 241 242
        }
    }

    return 0;
243
}
qiuyiuestc's avatar
qiuyiuestc 已提交
244 245 246

long list_event(void)
{
247 248 249 250
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Event);
    return _list_event(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
251
}
B
Bernard Xiong 已提交
252 253
FINSH_FUNCTION_EXPORT(list_event, list event in system);
MSH_CMD_EXPORT(list_event, list event in system);
254 255 256
#endif

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
257
static long _list_mutex(struct rt_list_node *list)
258
{
259
    int maxlen;
260 261
    struct rt_mutex *m;
    struct rt_list_node *node;
262
    const char *item_title = "mutex";
263

264 265 266
    maxlen = object_name_maxlen(item_title, list);

    rt_kprintf("%-*.s   owner  hold suspend thread\n", maxlen, item_title); object_split(maxlen);
267
    rt_kprintf(     " -------- ---- --------------\n");
268 269
    for (node = list->next; node != list; node = node->next)
    {
270
        m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
271 272
        rt_kprintf("%-*.*s %-8.*s %04d %d\n",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
273 274 275 276 277
                   m->parent.parent.name,
                   RT_NAME_MAX,
                   m->owner->name,
                   m->hold,
                   rt_list_len(&m->parent.suspend_thread));
278 279 280
    }

    return 0;
281
}
qiuyiuestc's avatar
qiuyiuestc 已提交
282 283 284

long list_mutex(void)
{
285 286 287 288 289
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Mutex);

    return _list_mutex(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
290
}
B
Bernard Xiong 已提交
291 292
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system);
MSH_CMD_EXPORT(list_mutex, list mutex in system);
293 294 295
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
296
static long _list_mailbox(struct rt_list_node *list)
297
{
298
    int maxlen;
299 300
    struct rt_mailbox *m;
    struct rt_list_node *node;
G
gbcwbz 已提交
301
    const char *item_title = "mailbox";
302

303
    maxlen = object_name_maxlen(item_title, list);
304

305 306
    rt_kprintf("%-*.s entry size suspend thread\n", maxlen, item_title); object_split(maxlen);
    rt_kprintf(     " ----  ---- --------------\n");
307 308
    for (node = list->next; node != list; node = node->next)
    {
309 310
        m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
311
        {
312 313
            rt_kprintf("%-*.*s %04d  %04d %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
314 315 316 317
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
318 319 320 321 322
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
323 324
            rt_kprintf("%-*.*s %04d  %04d %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
325 326 327 328
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
329 330 331 332
        }
    }

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

long list_mailbox(void)
{
337 338 339 340
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MailBox);
    return _list_mailbox(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
341
}
B
Bernard Xiong 已提交
342 343
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system);
MSH_CMD_EXPORT(list_mailbox, list mail box in system);
344 345 346
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
347
static long _list_msgqueue(struct rt_list_node *list)
348
{
349
    int maxlen;
350 351
    struct rt_messagequeue *m;
    struct rt_list_node *node;
G
gbcwbz 已提交
352
    const char *item_title = "msgqueue";
353

354
    maxlen = object_name_maxlen(item_title, list);
355

356
    rt_kprintf("%-*.s entry suspend thread\n", maxlen, item_title); object_split(maxlen);
357
    rt_kprintf(     " ----  --------------\n");
358 359
    for (node = list->next; node != list; node = node->next)
    {
360 361
        m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
362
        {
363 364
            rt_kprintf("%-*.*s %04d  %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
365 366 367
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
368 369 370 371 372
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
373 374
            rt_kprintf("%-*.*s %04d  %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
375 376 377
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
378 379 380 381
        }
    }

    return 0;
382
}
qiuyiuestc's avatar
qiuyiuestc 已提交
383 384 385

long list_msgqueue(void)
{
386 387 388 389
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MessageQueue);
    return _list_msgqueue(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
390
}
B
Bernard Xiong 已提交
391 392
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system);
MSH_CMD_EXPORT(list_msgqueue, list message queue in system);
393 394
#endif

395 396 397
#ifdef RT_USING_MEMHEAP
static long _list_memheap(struct rt_list_node *list)
{
398
    int maxlen;
399 400
    struct rt_memheap *mh;
    struct rt_list_node *node;
401
    const char *item_title = "memheap";
402

403
    maxlen = object_name_maxlen(item_title, list);
404

405
    rt_kprintf("%-*.s  pool size  max used size available size\n", maxlen, item_title); object_split(maxlen);
406
    rt_kprintf(      " ---------- ------------- --------------\n");
407 408 409 410
    for (node = list->next; node != list; node = node->next)
    {
        mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);

411 412
        rt_kprintf("%-*.*s %-010d %-013d %-05d\n",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
413 414 415 416
                   mh->parent.name,
                   mh->pool_size,
                   mh->max_used_size,
                   mh->available_size);
417 418 419 420 421 422 423
    }

    return 0;
}

long list_memheap(void)
{
424 425 426 427
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MemHeap);
    return _list_memheap(&info->object_list);
428
}
B
Bernard Xiong 已提交
429 430
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system);
MSH_CMD_EXPORT(list_memheap, list memory heap in system);
431 432
#endif

433
#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
434
static long _list_mempool(struct rt_list_node *list)
435
{
436
    int maxlen;
437 438
    struct rt_mempool *mp;
    struct rt_list_node *node;
439
    const char *item_title = "mempool";
440

441
    maxlen = object_name_maxlen(item_title, list);
442

443
    rt_kprintf("%-*.s block total free suspend thread\n", maxlen, item_title); object_split(maxlen);
444
    rt_kprintf(     " ----  ----  ---- --------------\n");
445 446
    for (node = list->next; node != list; node = node->next)
    {
447
        mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
448 449
        if (mp->suspend_thread_count > 0)
        {
450 451
            rt_kprintf("%-*.*s %04d  %04d  %04d %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
452 453 454 455 456
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
457
            show_wait_queue(&(mp->suspend_thread));
D
dzzxzz@gmail.com 已提交
458
            rt_kprintf("\n");
459 460 461
        }
        else
        {
462 463
            rt_kprintf("%-*.*s %04d  %04d  %04d %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
464 465 466 467 468
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
469 470 471 472
        }
    }

    return 0;
473
}
qiuyiuestc's avatar
qiuyiuestc 已提交
474 475 476

long list_mempool(void)
{
477 478 479 480
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MemPool);
    return _list_mempool(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
481
}
482
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
B
Bernard Xiong 已提交
483
MSH_CMD_EXPORT(list_mempool, list memory pool in system);
484 485
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
486
static long _list_timer(struct rt_list_node *list)
487
{
488
    int maxlen;
489 490
    struct rt_timer *timer;
    struct rt_list_node *node;
491
    const char *item_title = "timer";
492

493
    maxlen = object_name_maxlen(item_title, list);
494

495
    rt_kprintf("%-*.s  periodic   timeout       flag\n", maxlen, item_title); object_split(maxlen);
496
    rt_kprintf(     " ---------- ---------- -----------\n");
497 498
    for (node = list->next; node != list; node = node->next)
    {
499
        timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
500 501
        rt_kprintf("%-*.*s 0x%08x 0x%08x ",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
502 503 504
                   timer->parent.name,
                   timer->init_tick,
                   timer->timeout_tick);
505
        if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
D
dzzxzz@gmail.com 已提交
506
            rt_kprintf("activated\n");
507
        else
D
dzzxzz@gmail.com 已提交
508
            rt_kprintf("deactivated\n");
509 510 511 512 513
    }

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

    return 0;
514
}
qiuyiuestc's avatar
qiuyiuestc 已提交
515 516 517

long list_timer(void)
{
518 519 520 521
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Timer);
    return _list_timer(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
522
}
B
Bernard Xiong 已提交
523 524
FINSH_FUNCTION_EXPORT(list_timer, list timer in system);
MSH_CMD_EXPORT(list_timer, list timer in system);
525 526

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
527
static long _list_device(struct rt_list_node *list)
528
{
529
    int maxlen;
530 531
    struct rt_device *device;
    struct rt_list_node *node;
532
    char *const device_type_str[] =
533 534 535 536 537 538 539 540
    {
        "Character Device",
        "Block Device",
        "Network Interface",
        "MTD Device",
        "CAN Device",
        "RTC",
        "Sound Device",
541
        "Graphic Device",
542
        "I2C Bus",
543 544 545 546 547
        "USB Slave Device",
        "USB Host Bus",
        "SPI Bus",
        "SPI Device",
        "SDIO Bus",
548
        "PM Pseudo Device",
549 550
        "Pipe",
        "Portal Device",
551 552
        "Timer Device",
        "Miscellaneous Device",
553 554
        "Unknown"
    };
G
gbcwbz 已提交
555 556
    const char *item_title = "device";

557
    maxlen = object_name_maxlen(item_title, list);
558

559 560
    rt_kprintf("%-*.s         type         ref count\n", maxlen, item_title); object_split(maxlen);
    rt_kprintf(     " -------------------- ----------\n");
561 562
    for (node = list->next; node != list; node = node->next)
    {
563
        device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
564 565
        rt_kprintf("%-*.*s %-20s %-8d\n",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
566 567 568
                   device->parent.name,
                   (device->type <= RT_Device_Class_Unknown) ?
                   device_type_str[device->type] :
569 570
                   device_type_str[RT_Device_Class_Unknown],
                   device->ref_count);
571 572 573
    }

    return 0;
574
}
qiuyiuestc's avatar
qiuyiuestc 已提交
575 576 577

long list_device(void)
{
578 579 580 581
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Device);
    return _list_device(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
582
}
B
Bernard Xiong 已提交
583 584
FINSH_FUNCTION_EXPORT(list_device, list device in system);
MSH_CMD_EXPORT(list_device, list device in system);
585 586
#endif

D
dzzxzz 已提交
587
long list(void)
588
{
B
bernard 已提交
589
#ifndef FINSH_USING_MSH_ONLY
590 591
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
B
bernard 已提交
592
#endif
593

594 595
    rt_kprintf("--Function List:\n");
    {
596
        struct finsh_syscall *index;
D
dzzxzz@gmail.com 已提交
597
        for (index = _syscall_table_begin;
598 599
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
600
        {
601 602
            /* skip the internal command */
            if (strncmp((char *)index->name, "__", 2) == 0) continue;
B
Bernard Xiong 已提交
603

604
#ifdef FINSH_USING_DESCRIPTION
605
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
606
#else
607
            rt_kprintf("%s\n", index->name);
608
#endif
609 610 611
        }
    }

B
bernard 已提交
612
#ifndef FINSH_USING_MSH_ONLY
613 614 615 616 617 618 619 620 621 622
    /* 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");
    {
623
        struct finsh_sysvar *index;
624
        for (index = _sysvar_table_begin;
625 626
                index < _sysvar_table_end;
                FINSH_NEXT_SYSVAR(index))
627
        {
628
#ifdef FINSH_USING_DESCRIPTION
629
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
630
#else
631
            rt_kprintf("%s\n", index->name);
632
#endif
633 634
        }
    }
635

636 637 638 639 640 641
    sysvar_item = global_sysvar_list;
    while (sysvar_item != NULL)
    {
        rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
        sysvar_item = sysvar_item->next;
    }
B
bernard 已提交
642
#endif
643

644
    return 0;
645 646 647
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

B
bernard 已提交
648
#ifndef FINSH_USING_MSH_ONLY
649
static int str_is_prefix(const char *prefix, const char *str)
650
{
651 652 653 654 655 656
    while ((*prefix) && (*prefix == *str))
    {
        prefix ++;
        str ++;
    }

657
    if (*prefix == 0)
D
dzzxzz@gmail.com 已提交
658 659
        return 0;

660 661 662
    return -1;
}

663
static int str_common(const char *str1, const char *str2)
664
{
665
    const char *str = str1;
666

667
    while ((*str != 0) && (*str2 != 0) && (*str == *str2))
668 669 670 671 672 673
    {
        str ++;
        str2 ++;
    }

    return (str - str1);
674 675
}

676
void list_prefix(char *prefix)
677
{
678 679
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
680 681
    rt_uint16_t func_cnt, var_cnt;
    int length, min_length;
682
    const char *name_ptr;
683 684 685

    func_cnt = 0;
    var_cnt  = 0;
D
dzzxzz@gmail.com 已提交
686
    min_length = 0;
687 688 689 690
    name_ptr = RT_NULL;

    /* checks in system function call */
    {
D
dzzxzz@gmail.com 已提交
691 692
        struct finsh_syscall *index;
        for (index = _syscall_table_begin;
693 694
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
695
        {
696 697 698
            /* skip internal command */
            if (str_is_prefix("__", index->name) == 0) continue;

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
            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;
                }
723 724

#ifdef FINSH_USING_DESCRIPTION
725
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
726
#else
727
                rt_kprintf("%s\n", index->name);
728
#endif
729 730 731 732 733 734 735 736 737 738 739 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
            }
        }
    }

    /* 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 */
    {
768
        struct finsh_sysvar *index;
769
        for (index = _sysvar_table_begin;
770 771
                index < _sysvar_table_end;
                FINSH_NEXT_SYSVAR(index))
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
        {
            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 已提交
798

799
#ifdef FINSH_USING_DESCRIPTION
800
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
801
#else
802
                rt_kprintf("%s\n", index->name);
803
#endif
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
            }
        }
    }

    /* 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);
    }
846
}
B
bernard 已提交
847
#endif
848

B
bernard 已提交
849
#if defined(FINSH_USING_SYMTAB) && !defined(FINSH_USING_MSH_ONLY)
850 851 852
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif