shell.c 19.5 KB
Newer Older
1
/*
B
Bernard Xiong 已提交
2
 *  shell implementation for finsh shell.
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
 *
 * Change Logs:
 * Date           Author       Notes
B
Bernard Xiong 已提交
27
 * 2006-04-30     Bernard      the first version for FinSH
28 29 30
 * 2006-05-08     Bernard      change finsh thread stack to 2048
 * 2006-06-03     Bernard      add support for skyeye
 * 2006-09-24     Bernard      remove the code related with hardware
B
bernard.xiong 已提交
31
 * 2010-01-18     Bernard      fix down then up key bug.
32
 * 2010-03-19     Bernard      fix backspace issue and fix device read in shell.
33
 * 2010-04-01     Bernard      add prompt output when start and remove the empty history
34 35
 * 2011-02-23     Bernard      fix variable section end issue of finsh shell
 *                             initialization when use GNU GCC compiler.
36 37 38 39 40
 */

#include <rthw.h>

#include "finsh.h"
41
#include "shell.h"
42

B
Bernard Xiong 已提交
43 44 45 46
#ifdef FINSH_USING_MSH
#include "msh.h"
#endif

47 48 49 50
#ifdef _WIN32
#include <stdio.h> /* for putchar */
#endif

51
/* finsh thread */
52
static struct rt_thread finsh_thread;
53
ALIGN(RT_ALIGN_SIZE)
54
static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE];
55
struct finsh_shell *shell;
56

57 58
#if defined(FINSH_USING_MSH) || (defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR))
#if defined(RT_USING_DFS)
59
#include <dfs_posix.h>
60
#endif
61
const char *finsh_get_prompt()
62
{
63 64
#define _MSH_PROMPT "msh "
#define _PROMPT     "finsh "
B
bernard 已提交
65
    static char finsh_prompt[RT_CONSOLEBUF_SIZE + 1] = {0};
66 67 68

#ifdef FINSH_USING_MSH
    if (msh_is_used()) strcpy(finsh_prompt, _MSH_PROMPT);
B
bernard 已提交
69
    else
70
#endif
71
        strcpy(finsh_prompt, _PROMPT);
72

73
#if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR)
B
bernard 已提交
74
    /* get current working directory */
B
bernard 已提交
75
    getcwd(&finsh_prompt[rt_strlen(finsh_prompt)], RT_CONSOLEBUF_SIZE - rt_strlen(finsh_prompt));
76
#endif
77

B
bernard 已提交
78 79 80
    strcat(finsh_prompt, ">");

    return finsh_prompt;
81 82 83
}
#endif

84 85
static rt_err_t finsh_rx_ind(rt_device_t dev, rt_size_t size)
{
B
bernard 已提交
86
    RT_ASSERT(shell != RT_NULL);
87

B
bernard 已提交
88 89
    /* release semaphore to let finsh thread rx data */
    rt_sem_release(&shell->rx_sem);
90

B
bernard 已提交
91
    return RT_EOK;
92 93
}

B
bernard.xiong@gmail.com 已提交
94 95 96 97 98 99 100
/**
 * @ingroup finsh
 *
 * This function sets the input device of finsh shell.
 *
 * @param device_name the name of new input device.
 */
101
void finsh_set_device(const char *device_name)
102
{
B
bernard 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
    rt_device_t dev = RT_NULL;

    RT_ASSERT(shell != RT_NULL);
    dev = rt_device_find(device_name);
    if (dev == RT_NULL)
    {
        rt_kprintf("finsh: can not find device: %s\n", device_name);
        return;
    }

    /* check whether it's a same device */
    if (dev == shell->device) return;
    /* open this device and set the new device in finsh shell */
116
    if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | \
117
                       RT_DEVICE_FLAG_STREAM) == RT_EOK)
B
bernard 已提交
118 119 120 121 122
    {
        if (shell->device != RT_NULL)
        {
            /* close old finsh device */
            rt_device_close(shell->device);
W
weety 已提交
123
            rt_device_set_rx_indicate(shell->device, RT_NULL);
B
bernard 已提交
124
        }
125

126 127 128
        /* clear line buffer before switch to new device */
        memset(shell->line, 0, sizeof(shell->line));
        shell->line_curpos = shell->line_position = 0;
129

B
bernard 已提交
130 131 132
        shell->device = dev;
        rt_device_set_rx_indicate(dev, finsh_rx_ind);
    }
133 134
}

B
bernard.xiong@gmail.com 已提交
135 136 137 138 139 140 141
/**
 * @ingroup finsh
 *
 * This function returns current finsh shell input device.
 *
 * @return the finsh shell input device name is returned.
 */
142
const char *finsh_get_device()
143
{
B
bernard 已提交
144 145
    RT_ASSERT(shell != RT_NULL);
    return shell->device->parent.name;
146 147
}

B
bernard.xiong@gmail.com 已提交
148 149 150 151 152 153 154 155 156 157
/**
 * @ingroup finsh
 *
 * This function set the echo mode of finsh shell.
 *
 * FINSH_OPTION_ECHO=0x01 is echo mode, other values are none-echo mode.
 *
 * @param echo the echo mode
 */
void finsh_set_echo(rt_uint32_t echo)
158
{
B
bernard 已提交
159 160
    RT_ASSERT(shell != RT_NULL);
    shell->echo_mode = (rt_uint8_t)echo;
161 162
}

B
bernard.xiong@gmail.com 已提交
163 164 165 166 167 168 169
/**
 * @ingroup finsh
 *
 * This function gets the echo mode of finsh shell.
 *
 * @return the echo mode
 */
170 171
rt_uint32_t finsh_get_echo()
{
B
bernard 已提交
172
    RT_ASSERT(shell != RT_NULL);
173

B
bernard 已提交
174
    return shell->echo_mode;
175 176
}

177
static void shell_auto_complete(char *prefix)
178 179
{

B
bernard 已提交
180
    rt_kprintf("\n");
B
Bernard Xiong 已提交
181
#ifdef FINSH_USING_MSH
B
bernard 已提交
182 183 184 185
    if (msh_is_used() == RT_TRUE)
    {
        msh_auto_complete(prefix);
    }
186
    else
B
Bernard Xiong 已提交
187
#endif
B
bernard 已提交
188
    {
189 190
#ifndef FINSH_USING_MSH_ONLY
        extern void list_prefix(char * prefix);
B
bernard 已提交
191
        list_prefix(prefix);
B
bernard 已提交
192
#endif
B
bernard 已提交
193
    }
B
Bernard Xiong 已提交
194

B
bernard 已提交
195
    rt_kprintf("%s%s", FINSH_PROMPT, prefix);
196
}
197

B
bernard 已提交
198
#ifndef FINSH_USING_MSH_ONLY
199
void finsh_run_line(struct finsh_parser *parser, const char *line)
200
{
201
    const char *err_str;
B
bernard 已提交
202 203

    rt_kprintf("\n");
204
    finsh_parser_run(parser, (unsigned char *)line);
B
bernard 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

    /* compile node root */
    if (finsh_errno() == 0)
    {
        finsh_compiler_run(parser->root);
    }
    else
    {
        err_str = finsh_error_string(finsh_errno());
        rt_kprintf("%s\n", err_str);
    }

    /* run virtual machine */
    if (finsh_errno() == 0)
    {
        char ch;
        finsh_vm_run();

        ch = (unsigned char)finsh_stack_bottom();
        if (ch > 0x20 && ch < 0x7e)
        {
            rt_kprintf("\t'%c', %d, 0x%08x\n",
227 228 229
                       (unsigned char)finsh_stack_bottom(),
                       (unsigned int)finsh_stack_bottom(),
                       (unsigned int)finsh_stack_bottom());
B
bernard 已提交
230 231 232 233
        }
        else
        {
            rt_kprintf("\t%d, 0x%08x\n",
234 235
                       (unsigned int)finsh_stack_bottom(),
                       (unsigned int)finsh_stack_bottom());
B
bernard 已提交
236 237
        }
    }
238

239 240
    finsh_flush(parser);
}
B
bernard 已提交
241
#endif
242

243
#ifdef FINSH_USING_HISTORY
244
static rt_bool_t shell_handle_history(struct finsh_shell *shell)
245
{
246
#if defined(_WIN32)
B
bernard 已提交
247 248
    int i;
    rt_kprintf("\r");
249

250
    for (i = 0; i <= 60; i++)
B
bernard 已提交
251 252
        putchar(' ');
    rt_kprintf("\r");
253 254

#else
B
bernard 已提交
255
    rt_kprintf("\033[2K\r");
256
#endif
B
bernard 已提交
257 258
    rt_kprintf("%s%s", FINSH_PROMPT, shell->line);
    return RT_FALSE;
259 260
}

261
static void shell_push_history(struct finsh_shell *shell)
262
{
B
bernard 已提交
263 264 265 266 267 268 269 270 271 272
    if (shell->line_position != 0)
    {
        /* push history */
        if (shell->history_count >= FINSH_HISTORY_LINES)
        {
            /* move history */
            int index;
            for (index = 0; index < FINSH_HISTORY_LINES - 1; index ++)
            {
                memcpy(&shell->cmd_history[index][0],
273
                       &shell->cmd_history[index + 1][0], FINSH_CMD_SIZE);
B
bernard 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
            }
            memset(&shell->cmd_history[index][0], 0, FINSH_CMD_SIZE);
            memcpy(&shell->cmd_history[index][0], shell->line, shell->line_position);

            /* it's the maximum history */
            shell->history_count = FINSH_HISTORY_LINES;
        }
        else
        {
            memset(&shell->cmd_history[shell->history_count][0], 0, FINSH_CMD_SIZE);
            memcpy(&shell->cmd_history[shell->history_count][0], shell->line, shell->line_position);

            /* increase count and set current history position */
            shell->history_count ++;
        }
    }
    shell->current_history = shell->history_count;
291 292 293 294 295 296
}
#endif

#ifndef RT_USING_HEAP
struct finsh_shell _shell;
#endif
297
void finsh_thread_entry(void *parameter)
298 299 300
{
    char ch;

B
bernard 已提交
301 302
    /* normal is echo mode */
    shell->echo_mode = 1;
303

B
bernard 已提交
304
#ifndef FINSH_USING_MSH_ONLY
305
    finsh_init(&shell->parser);
B
bernard 已提交
306
#endif
B
bernard 已提交
307
    rt_kprintf(FINSH_PROMPT);
308

B
bernard 已提交
309 310 311
    /* set console device as shell device */
    if (shell->device == RT_NULL)
    {
P
prife 已提交
312
#ifdef RT_USING_CONSOLE
B
bernard 已提交
313 314 315
        shell->device = rt_console_get_device();
        RT_ASSERT(shell->device);
        rt_device_set_rx_indicate(shell->device, finsh_rx_ind);
316
        rt_device_open(shell->device, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX));
P
prife 已提交
317
#else
B
bernard 已提交
318
        RT_ASSERT(shell->device);
P
prife 已提交
319
#endif
B
bernard 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    }

    while (1)
    {
        /* wait receive */
        if (rt_sem_take(&shell->rx_sem, RT_WAITING_FOREVER) != RT_EOK) continue;

        /* read one character from device */
        while (rt_device_read(shell->device, 0, &ch, 1) == 1)
        {
            /*
             * handle control key
             * up key  : 0x1b 0x5b 0x41
             * down key: 0x1b 0x5b 0x42
             * right key:0x1b 0x5b 0x43
             * left key: 0x1b 0x5b 0x44
             */
            if (ch == 0x1b)
            {
                shell->stat = WAIT_SPEC_KEY;
                continue;
            }
            else if (shell->stat == WAIT_SPEC_KEY)
            {
                if (ch == 0x5b)
                {
                    shell->stat = WAIT_FUNC_KEY;
                    continue;
                }

                shell->stat = WAIT_NORMAL;
            }
            else if (shell->stat == WAIT_FUNC_KEY)
            {
                shell->stat = WAIT_NORMAL;

                if (ch == 0x41) /* up key */
                {
358
#ifdef FINSH_USING_HISTORY
B
bernard 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372
                    /* prev history */
                    if (shell->current_history > 0)
                        shell->current_history --;
                    else
                    {
                        shell->current_history = 0;
                        continue;
                    }

                    /* copy the history command */
                    memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
                           FINSH_CMD_SIZE);
                    shell->line_curpos = shell->line_position = strlen(shell->line);
                    shell_handle_history(shell);
373
#endif
B
bernard 已提交
374 375 376 377
                    continue;
                }
                else if (ch == 0x42) /* down key */
                {
378
#ifdef FINSH_USING_HISTORY
B
bernard 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                    /* next history */
                    if (shell->current_history < shell->history_count - 1)
                        shell->current_history ++;
                    else
                    {
                        /* set to the end of history */
                        if (shell->history_count != 0)
                            shell->current_history = shell->history_count - 1;
                        else
                            continue;
                    }

                    memcpy(shell->line, &shell->cmd_history[shell->current_history][0],
                           FINSH_CMD_SIZE);
                    shell->line_curpos = shell->line_position = strlen(shell->line);
                    shell_handle_history(shell);
395
#endif
B
bernard 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
                    continue;
                }
                else if (ch == 0x44) /* left key */
                {
                    if (shell->line_curpos)
                    {
                        rt_kprintf("\b");
                        shell->line_curpos --;
                    }

                    continue;
                }
                else if (ch == 0x43) /* right key */
                {
                    if (shell->line_curpos < shell->line_position)
                    {
                        rt_kprintf("%c", shell->line[shell->line_curpos]);
                        shell->line_curpos ++;
                    }

                    continue;
                }

            }

            /* handle CR key */
            if (ch == '\r')
            {
                char next;

                if (rt_device_read(shell->device, 0, &next, 1) == 1)
427 428 429 430
                {
                    if (next == '\0') ch = 'r'; /* linux telnet will issue '\0' */
                    else ch = next;
                }
B
bernard 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
                else ch = '\r';
            }
            /* handle tab key */
            else if (ch == '\t')
            {
                int i;
                /* move the cursor to the beginning of line */
                for (i = 0; i < shell->line_curpos; i++)
                    rt_kprintf("\b");

                /* auto complete */
                shell_auto_complete(&shell->line[0]);
                /* re-calculate position */
                shell->line_curpos = shell->line_position = strlen(shell->line);

                continue;
            }
            /* handle backspace key */
            else if (ch == 0x7f || ch == 0x08)
            {
                /* note that shell->line_curpos >= 0 */
                if (shell->line_curpos == 0)
                    continue;

                shell->line_position--;
                shell->line_curpos--;

                if (shell->line_position > shell->line_curpos)
                {
                    int i;

                    rt_memmove(&shell->line[shell->line_curpos],
                               &shell->line[shell->line_curpos + 1],
                               shell->line_position - shell->line_curpos);
                    shell->line[shell->line_position] = 0;

                    rt_kprintf("\b%s  \b", &shell->line[shell->line_curpos]);

                    /* move the cursor to the origin position */
                    for (i = shell->line_curpos; i <= shell->line_position; i++)
                        rt_kprintf("\b");
                }
                else
                {
                    rt_kprintf("\b \b");
                    shell->line[shell->line_position] = 0;
                }

                continue;
            }

            /* handle end of line, break */
            if (ch == '\r' || ch == '\n')
            {
485
#ifdef FINSH_USING_HISTORY
B
bernard 已提交
486
                shell_push_history(shell);
487
#endif
B
bernard 已提交
488

489
#ifdef FINSH_USING_MSH
B
bernard 已提交
490 491 492 493 494 495
                if (msh_is_used() == RT_TRUE)
                {
                    rt_kprintf("\n");
                    msh_exec(shell->line, shell->line_position);
                }
                else
496
#endif
B
bernard 已提交
497
                {
498
#ifndef FINSH_USING_MSH_ONLY
B
bernard 已提交
499 500 501 502 503
                    /* add ';' and run the command line */
                    shell->line[shell->line_position] = ';';

                    if (shell->line_position != 0) finsh_run_line(&shell->parser, shell->line);
                    else rt_kprintf("\n");
504
#endif
B
bernard 已提交
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
                }

                rt_kprintf(FINSH_PROMPT);
                memset(shell->line, 0, sizeof(shell->line));
                shell->line_curpos = shell->line_position = 0;
                break;
            }

            /* it's a large line, discard it */
            if (shell->line_position >= FINSH_CMD_SIZE)
                shell->line_position = 0;

            /* normal character */
            if (shell->line_curpos < shell->line_position)
            {
                int i;

                rt_memmove(&shell->line[shell->line_curpos + 1],
                           &shell->line[shell->line_curpos],
                           shell->line_position - shell->line_curpos);
                shell->line[shell->line_curpos] = ch;
                if (shell->echo_mode)
                    rt_kprintf("%s", &shell->line[shell->line_curpos]);

                /* move the cursor to new position */
                for (i = shell->line_curpos; i < shell->line_position; i++)
                    rt_kprintf("\b");
            }
            else
            {
                shell->line[shell->line_position] = ch;
B
Bernard Xiong 已提交
536 537
                if (shell->echo_mode)
                    rt_kprintf("%c", ch);
B
bernard 已提交
538 539 540 541 542
            }

            ch = 0;
            shell->line_position ++;
            shell->line_curpos++;
543 544 545 546 547 548
            if (shell->line_position >= 80)
            {
                /* clear command line */
                shell->line_position = 0;
                shell->line_curpos = 0;
            }
B
bernard 已提交
549 550
        } /* end of device read */
    }
551 552
}

553
void finsh_system_function_init(const void *begin, const void *end)
554
{
555 556
    _syscall_table_begin = (struct finsh_syscall *) begin;
    _syscall_table_end = (struct finsh_syscall *) end;
557 558
}

559
void finsh_system_var_init(const void *begin, const void *end)
560
{
561 562
    _sysvar_table_begin = (struct finsh_sysvar *) begin;
    _sysvar_table_end = (struct finsh_sysvar *) end;
563 564
}

淡漠想敏's avatar
淡漠想敏 已提交
565
#if defined(__ICCARM__) || defined(__ICCRX__)               /* for IAR compiler */
566 567 568 569
#ifdef FINSH_USING_SYMTAB
#pragma section="FSymTab"
#pragma section="VSymTab"
#endif
570
#elif defined(__ADSPBLACKFIN__) /* for VisaulDSP++ Compiler*/
571 572 573 574 575 576
#ifdef FINSH_USING_SYMTAB
extern "asm" int __fsymtab_start;
extern "asm" int __fsymtab_end;
extern "asm" int __vsymtab_start;
extern "asm" int __vsymtab_end;
#endif
577 578 579 580
#elif defined(_MSC_VER)
#pragma section("FSymTab$a", read)
const char __fsym_begin_name[] = "__start";
const char __fsym_begin_desc[] = "begin of finsh";
581
__declspec(allocate("FSymTab$a")) const struct finsh_syscall __fsym_begin =
582
{
B
bernard 已提交
583 584 585
    __fsym_begin_name,
    __fsym_begin_desc,
    NULL
586 587 588 589 590
};

#pragma section("FSymTab$z", read)
const char __fsym_end_name[] = "__end";
const char __fsym_end_desc[] = "end of finsh";
591
__declspec(allocate("FSymTab$z")) const struct finsh_syscall __fsym_end =
592
{
B
bernard 已提交
593 594 595
    __fsym_end_name,
    __fsym_end_desc,
    NULL
596
};
597
#endif
B
bernard.xiong@gmail.com 已提交
598 599 600 601 602 603

/*
 * @ingroup finsh
 *
 * This function will initialize finsh shell
 */
B
Bernard Xiong 已提交
604
int finsh_system_init(void)
605
{
B
bernard 已提交
606
    rt_err_t result;
607 608 609

#ifdef FINSH_USING_SYMTAB
#ifdef __CC_ARM                 /* ARM C Compiler */
wuyangyong's avatar
wuyangyong 已提交
610 611 612 613
    extern const int FSymTab$$Base;
    extern const int FSymTab$$Limit;
    extern const int VSymTab$$Base;
    extern const int VSymTab$$Limit;
B
bernard 已提交
614
    finsh_system_function_init(&FSymTab$$Base, &FSymTab$$Limit);
615
#ifndef FINSH_USING_MSH_ONLY
B
bernard 已提交
616
    finsh_system_var_init(&VSymTab$$Base, &VSymTab$$Limit);
617
#endif
淡漠想敏's avatar
淡漠想敏 已提交
618
#elif defined (__ICCARM__) || defined(__ICCRX__)      /* for IAR Compiler */
619 620 621 622
    finsh_system_function_init(__section_begin("FSymTab"),
                               __section_end("FSymTab"));
    finsh_system_var_init(__section_begin("VSymTab"),
                          __section_end("VSymTab"));
G
Grissiom 已提交
623 624
#elif defined (__GNUC__) || defined(__TI_COMPILER_VERSION__)
    /* GNU GCC Compiler and TI CCS */
B
bernard 已提交
625 626 627 628 629 630
    extern const int __fsymtab_start;
    extern const int __fsymtab_end;
    extern const int __vsymtab_start;
    extern const int __vsymtab_end;
    finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
    finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
631 632 633
#elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
    finsh_system_function_init(&__fsymtab_start, &__fsymtab_end);
    finsh_system_var_init(&__vsymtab_start, &__vsymtab_end);
634
#elif defined(_MSC_VER)
B
bernard 已提交
635
    unsigned int *ptr_begin, *ptr_end;
636

637 638
    ptr_begin = (unsigned int *)&__fsym_begin;
    ptr_begin += (sizeof(struct finsh_syscall) / sizeof(unsigned int));
B
bernard 已提交
639
    while (*ptr_begin == 0) ptr_begin ++;
640

641 642
    ptr_end = (unsigned int *) &__fsym_end;
    ptr_end --;
B
bernard 已提交
643
    while (*ptr_end == 0) ptr_end --;
644

B
bernard 已提交
645
    finsh_system_function_init(ptr_begin, ptr_end);
646 647 648
#endif
#endif

B
bernard 已提交
649
    /* create or set shell structure */
650
#ifdef RT_USING_HEAP
651
    shell = (struct finsh_shell *)rt_malloc(sizeof(struct finsh_shell));
B
bernard 已提交
652 653 654 655 656
    if (shell == RT_NULL)
    {
        rt_kprintf("no memory for shell\n");
        return -1;
    }
B
bernard 已提交
657
#else
B
bernard 已提交
658
    shell = &_shell;
B
bernard 已提交
659 660
#endif

B
bernard 已提交
661
    memset(shell, 0, sizeof(struct finsh_shell));
662

B
bernard 已提交
663 664
    rt_sem_init(&(shell->rx_sem), "shrx", 0, 0);
    result = rt_thread_init(&finsh_thread,
665 666 667 668
                            "tshell",
                            finsh_thread_entry, RT_NULL,
                            &finsh_thread_stack[0], sizeof(finsh_thread_stack),
                            FINSH_THREAD_PRIORITY, 10);
669

B
bernard 已提交
670 671 672
    if (result == RT_EOK)
        rt_thread_startup(&finsh_thread);
    return 0;
673
}
B
Bernard Xiong 已提交
674 675
INIT_COMPONENT_EXPORT(finsh_system_init);