msh.c 13.6 KB
Newer Older
B
Bernard Xiong 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
B
Bernard Xiong 已提交
5 6 7
 *
 * Change Logs:
 * Date           Author       Notes
8 9
 * 2013-03-30     Bernard      the first verion for finsh
 * 2014-01-03     Bernard      msh can execute module.
10
 * 2017-07-19     Aubr.Cool    limit argc to RT_FINSH_ARG_MAX
B
Bernard Xiong 已提交
11
 */
12
#include <rtthread.h>
马志远 已提交
13
#include <string.h>
14

马志远 已提交
15 16 17
#ifdef RT_USING_FINSH

#ifndef FINSH_ARG_MAX
mysterywolf's avatar
mysterywolf 已提交
18 19
#define FINSH_ARG_MAX    8
#endif /* FINSH_ARG_MAX */
B
Bernard Xiong 已提交
20 21

#include "msh.h"
马志远 已提交
22
#include "shell.h"
mysterywolf's avatar
mysterywolf 已提交
23 24 25
#ifdef DFS_USING_POSIX
#include <dfs_posix.h>
#endif /* DFS_USING_POSIX */
26
#ifdef RT_USING_MODULE
mysterywolf's avatar
mysterywolf 已提交
27 28
#include <dlmodule.h>
#endif /* RT_USING_MODULE */
29

30
typedef int (*cmd_function_t)(int argc, char **argv);
B
Bernard Xiong 已提交
31

32
int msh_help(int argc, char **argv)
B
Bernard Xiong 已提交
33
{
34 35 36 37 38
    rt_kprintf("RT-Thread shell commands:\n");
    {
        struct finsh_syscall *index;

        for (index = _syscall_table_begin;
39 40
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
41
        {
42
#if defined(FINSH_USING_DESCRIPTION) && defined(FINSH_USING_SYMTAB)
马志远 已提交
43
            rt_kprintf("%-16s - %s\n", index->name, index->desc);
44
#else
马志远 已提交
45
            rt_kprintf("%s ", index->name);
46
#endif
47 48 49
        }
    }
    rt_kprintf("\n");
B
Bernard Xiong 已提交
50

51
    return 0;
B
Bernard Xiong 已提交
52
}
马志远 已提交
53
MSH_CMD_EXPORT_ALIAS(msh_help, help, RT - Thread shell help.);
B
Bernard Xiong 已提交
54

55
#ifdef MSH_USING_BUILT_IN_COMMANDS
56 57 58 59 60 61 62 63 64 65 66 67 68
int cmd_ps(int argc, char **argv)
{
    extern long list_thread(void);
    extern int list_module(void);

#ifdef RT_USING_MODULE
    if ((argc == 2) && (strcmp(argv[1], "-m") == 0))
        list_module();
    else
#endif
        list_thread();
    return 0;
}
马志远 已提交
69
MSH_CMD_EXPORT_ALIAS(cmd_ps, ps, List threads in the system.);
70 71 72 73

#ifdef RT_USING_HEAP
int cmd_free(int argc, char **argv)
{
74
    rt_size_t total = 0, used = 0, max_used = 0;
75

76 77 78 79
    rt_memory_info(&total, &used, &max_used);
    rt_kprintf("total   : %d\n", total);
    rt_kprintf("used    : %d\n", used);
    rt_kprintf("maximum : %d\n", max_used);
80 81
    return 0;
}
马志远 已提交
82
MSH_CMD_EXPORT_ALIAS(cmd_free, free, Show the memory usage in the system.);
83
#endif /* RT_USING_HEAP */
84
#endif /* MSH_USING_BUILT_IN_COMMANDS */
85

86
static int msh_split(char *cmd, rt_size_t length, char *argv[FINSH_ARG_MAX])
B
Bernard Xiong 已提交
87
{
88 89 90
    char *ptr;
    rt_size_t position;
    rt_size_t argc;
91
    rt_size_t i;
92 93

    ptr = cmd;
马志远 已提交
94 95
    position = 0;
    argc = 0;
96 97 98 99 100 101 102

    while (position < length)
    {
        /* strip bank and tab */
        while ((*ptr == ' ' || *ptr == '\t') && position < length)
        {
            *ptr = '\0';
马志远 已提交
103 104
            ptr ++;
            position ++;
105
        }
106

马志远 已提交
107
        if (argc >= FINSH_ARG_MAX)
108 109
        {
            rt_kprintf("Too many args ! We only Use:\n");
马志远 已提交
110
            for (i = 0; i < argc; i++)
111 112 113 114 115 116 117
            {
                rt_kprintf("%s ", argv[i]);
            }
            rt_kprintf("\n");
            break;
        }

118 119 120 121 122
        if (position >= length) break;

        /* handle string */
        if (*ptr == '"')
        {
马志远 已提交
123 124 125 126
            ptr ++;
            position ++;
            argv[argc] = ptr;
            argc ++;
127 128 129 130 131 132 133 134

            /* skip this string */
            while (*ptr != '"' && position < length)
            {
                if (*ptr == '\\')
                {
                    if (*(ptr + 1) == '"')
                    {
马志远 已提交
135 136
                        ptr ++;
                        position ++;
137 138
                    }
                }
马志远 已提交
139 140
                ptr ++;
                position ++;
141 142 143 144
            }
            if (position >= length) break;

            /* skip '"' */
马志远 已提交
145 146 147
            *ptr = '\0';
            ptr ++;
            position ++;
148 149 150 151 152 153 154
        }
        else
        {
            argv[argc] = ptr;
            argc ++;
            while ((*ptr != ' ' && *ptr != '\t') && position < length)
            {
马志远 已提交
155 156
                ptr ++;
                position ++;
157 158 159 160 161 162
            }
            if (position >= length) break;
        }
    }

    return argc;
B
Bernard Xiong 已提交
163 164
}

165
static cmd_function_t msh_get_cmd(char *cmd, int size)
B
Bernard Xiong 已提交
166
{
167 168 169 170
    struct finsh_syscall *index;
    cmd_function_t cmd_func = RT_NULL;

    for (index = _syscall_table_begin;
171 172
            index < _syscall_table_end;
            FINSH_NEXT_SYSCALL(index))
173
    {
马志远 已提交
174 175
        if (strncmp(index->name, cmd, size) == 0 &&
                index->name[size] == '\0')
176 177 178 179 180 181 182
        {
            cmd_func = (cmd_function_t)index->func;
            break;
        }
    }

    return cmd_func;
B
Bernard Xiong 已提交
183 184
}

mysterywolf's avatar
mysterywolf 已提交
185
#if defined(RT_USING_MODULE) && defined(DFS_USING_POSIX)
G
Grissiom 已提交
186 187
/* Return 0 on module executed. Other value indicate error.
 */
188
int msh_exec_module(const char *cmd_line, int size)
189
{
G
Grissiom 已提交
190
    int ret;
191 192
    int fd = -1;
    char *pg_name;
193
    int length, cmd_length = 0;
194

G
Grissiom 已提交
195 196 197 198 199
    if (size == 0)
        return -RT_ERROR;
    /* get the length of command0 */
    while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
        cmd_length ++;
200 201

    /* get name length */
202
    length = cmd_length + 32;
203

G
Grissiom 已提交
204
    /* allocate program name memory */
205
    pg_name = (char *) rt_malloc(length);
G
Grissiom 已提交
206 207
    if (pg_name == RT_NULL)
        return -RT_ENOMEM;
208

G
Grissiom 已提交
209
    /* copy command0 */
210
    rt_memcpy(pg_name, cmd_line, cmd_length);
G
Grissiom 已提交
211
    pg_name[cmd_length] = '\0';
212 213

    if (strstr(pg_name, ".mo") != RT_NULL || strstr(pg_name, ".MO") != RT_NULL)
214 215
    {
        /* try to open program */
G
Grissiom 已提交
216
        fd = open(pg_name, O_RDONLY, 0);
217 218 219 220

        /* search in /bin path */
        if (fd < 0)
        {
221
            rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
222 223 224 225 226 227 228 229
            fd = open(pg_name, O_RDONLY, 0);
        }
    }
    else
    {
        /* add .mo and open program */

        /* try to open program */
G
Grissiom 已提交
230 231
        strcat(pg_name, ".mo");
        fd = open(pg_name, O_RDONLY, 0);
232 233 234 235

        /* search in /bin path */
        if (fd < 0)
        {
G
Grissiom 已提交
236
            rt_snprintf(pg_name, length - 1, "/bin/%.*s.mo", cmd_length, cmd_line);
237 238 239
            fd = open(pg_name, O_RDONLY, 0);
        }
    }
240

241 242 243 244
    if (fd >= 0)
    {
        /* found program */
        close(fd);
245
        dlmodule_exec(pg_name, cmd_line, size);
G
Grissiom 已提交
246
        ret = 0;
247 248 249
    }
    else
    {
G
Grissiom 已提交
250
        ret = -1;
251 252 253
    }

    rt_free(pg_name);
G
Grissiom 已提交
254
    return ret;
255
}
mysterywolf's avatar
mysterywolf 已提交
256
#endif /* defined(RT_USING_MODULE) && defined(DFS_USING_POSIX) */
257

258
static int _msh_exec_cmd(char *cmd, rt_size_t length, int *retp)
B
Bernard Xiong 已提交
259
{
260
    int argc;
261
    rt_size_t cmd0_size = 0;
262
    cmd_function_t cmd_func;
263
    char *argv[FINSH_ARG_MAX];
B
Bernard Xiong 已提交
264

G
Grissiom 已提交
265 266 267 268
    RT_ASSERT(cmd);
    RT_ASSERT(retp);

    /* find the size of first command */
R
roamboy 已提交
269 270
    while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
        cmd0_size ++;
G
Grissiom 已提交
271 272
    if (cmd0_size == 0)
        return -RT_ERROR;
273

R
roamboy 已提交
274 275
    cmd_func = msh_get_cmd(cmd, cmd0_size);
    if (cmd_func == RT_NULL)
G
Grissiom 已提交
276
        return -RT_ERROR;
277

R
roamboy 已提交
278
    /* split arguments */
279
    rt_memset(argv, 0x00, sizeof(argv));
280
    argc = msh_split(cmd, length, argv);
G
Grissiom 已提交
281 282
    if (argc == 0)
        return -RT_ERROR;
B
Bernard Xiong 已提交
283

284
    /* exec this command */
G
Grissiom 已提交
285 286 287 288
    *retp = cmd_func(argc, argv);
    return 0;
}

mysterywolf's avatar
mysterywolf 已提交
289
#if defined(RT_USING_LWP) && defined(DFS_USING_POSIX)
H
heyuanjie 已提交
290 291 292 293 294 295 296 297
static int _msh_exec_lwp(char *cmd, rt_size_t length)
{
    int argc;
    int cmd0_size = 0;
    char *argv[FINSH_ARG_MAX];
    int fd = -1;
    char *pg_name;

马志远 已提交
298
    extern int exec(char *, int, char **);
H
heyuanjie 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

    /* find the size of first command */
    while ((cmd[cmd0_size] != ' ' && cmd[cmd0_size] != '\t') && cmd0_size < length)
        cmd0_size ++;
    if (cmd0_size == 0)
        return -1;

    /* split arguments */
    rt_memset(argv, 0x00, sizeof(argv));
    argc = msh_split(cmd, length, argv);
    if (argc == 0)
        return -1;

    pg_name = argv[0];
    /* try to open program */
    fd = open(pg_name, O_RDONLY, 0);

    if (fd < 0)
        return -1;

    /* found program */
    close(fd);
    exec(pg_name, argc, argv);

    return 0;
}
mysterywolf's avatar
mysterywolf 已提交
325
#endif /* defined(RT_USING_LWP) && defined(DFS_USING_POSIX) */
H
heyuanjie 已提交
326

327
int msh_exec(char *cmd, rt_size_t length)
G
Grissiom 已提交
328 329 330
{
    int cmd_ret;

331
    /* strim the beginning of command */
xpxyr's avatar
xpxyr 已提交
332
    while ((length > 0) && (*cmd  == ' ' || *cmd == '\t'))
G
Grissiom 已提交
333 334 335 336 337
    {
        cmd++;
        length--;
    }

G
Grissiom 已提交
338 339 340
    if (length == 0)
        return 0;

G
Grissiom 已提交
341 342 343 344 345 346 347 348
    /* Exec sequence:
     * 1. built-in command
     * 2. module(if enabled)
     */
    if (_msh_exec_cmd(cmd, length, &cmd_ret) == 0)
    {
        return cmd_ret;
    }
mysterywolf's avatar
mysterywolf 已提交
349
#ifdef DFS_USING_POSIX
350 351
#ifdef DFS_USING_WORKDIR
    if (msh_exec_script(cmd, length) == 0)
G
Grissiom 已提交
352 353 354 355
    {
        return 0;
    }
#endif
356

357 358
#ifdef RT_USING_MODULE
    if (msh_exec_module(cmd, length) == 0)
359 360 361
    {
        return 0;
    }
362
#endif /* RT_USING_MODULE */
363

364
#ifdef RT_USING_LWP
H
heyuanjie 已提交
365 366 367 368
    if (_msh_exec_lwp(cmd, length) == 0)
    {
        return 0;
    }
369
#endif /* RT_USING_LWP */
mysterywolf's avatar
mysterywolf 已提交
370
#endif /* DFS_USING_POSIX */
H
heyuanjie 已提交
371

G
Grissiom 已提交
372 373 374 375
    /* truncate the cmd at the first space. */
    {
        char *tcmd;
        tcmd = cmd;
376
        while (*tcmd != ' ' && *tcmd != '\0')
G
Grissiom 已提交
377 378 379 380 381 382 383
        {
            tcmd++;
        }
        *tcmd = '\0';
    }
    rt_kprintf("%s: command not found.\n", cmd);
    return -1;
B
Bernard Xiong 已提交
384 385 386 387
}

static int str_common(const char *str1, const char *str2)
{
388
    const char *str = str1;
B
Bernard Xiong 已提交
389

390 391 392 393 394
    while ((*str != 0) && (*str2 != 0) && (*str == *str2))
    {
        str ++;
        str2 ++;
    }
B
Bernard Xiong 已提交
395

396
    return (str - str1);
B
Bernard Xiong 已提交
397 398
}

mysterywolf's avatar
mysterywolf 已提交
399
#ifdef DFS_USING_POSIX
400 401
void msh_auto_complete_path(char *path)
{
402
    DIR *dir = RT_NULL;
403
    struct dirent *dirent = RT_NULL;
404 405
    char *full_path, *ptr, *index;

406 407 408
    if (!path)
        return;

409
    full_path = (char *)rt_malloc(256);
410 411
    if (full_path == RT_NULL) return; /* out of memory */

G
Grissiom 已提交
412
    if (*path != '/')
413 414 415 416 417 418 419
    {
        getcwd(full_path, 256);
        if (full_path[rt_strlen(full_path) - 1]  != '/')
            strcat(full_path, "/");
    }
    else *full_path = '\0';

G
Grissiom 已提交
420 421
    index = RT_NULL;
    ptr = path;
422 423
    for (;;)
    {
424 425
        if (*ptr == '/') index = ptr + 1;
        if (!*ptr) break;
426 427

        ptr ++;
428 429 430 431 432 433 434 435
    }
    if (index == RT_NULL) index = path;

    if (index != RT_NULL)
    {
        char *dest = index;

        /* fill the parent path */
G
Grissiom 已提交
436
        ptr = full_path;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        while (*ptr) ptr ++;

        for (index = path; index != dest;)
            *ptr++ = *index++;
        *ptr = '\0';

        dir = opendir(full_path);
        if (dir == RT_NULL) /* open directory failed! */
        {
            rt_free(full_path);
            return;
        }

        /* restore the index position */
        index = dest;
    }

    /* auto complete the file or directory name */
    if (*index == '\0') /* display all of files and directories */
    {
        for (;;)
        {
            dirent = readdir(dir);
            if (dirent == RT_NULL) break;
G
Grissiom 已提交
461

462 463 464 465 466
            rt_kprintf("%s\n", dirent->d_name);
        }
    }
    else
    {
467
        rt_size_t length, min_length;
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

        min_length = 0;
        for (;;)
        {
            dirent = readdir(dir);
            if (dirent == RT_NULL) break;

            /* matched the prefix string */
            if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
            {
                if (min_length == 0)
                {
                    min_length = rt_strlen(dirent->d_name);
                    /* save dirent name */
                    strcpy(full_path, dirent->d_name);
                }
484

485
                length = str_common(dirent->d_name, full_path);
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
                if (length < min_length)
                {
                    min_length = length;
                }
            }
        }

        if (min_length)
        {
            if (min_length < rt_strlen(full_path))
            {
                /* list the candidate */
                rewinddir(dir);

                for (;;)
                {
                    dirent = readdir(dir);
                    if (dirent == RT_NULL) break;

                    if (strncmp(index, dirent->d_name, rt_strlen(index)) == 0)
                        rt_kprintf("%s\n", dirent->d_name);
                }
            }
510

511
            length = index - path;
512
            rt_memcpy(index, full_path, min_length);
513 514 515 516 517 518
            path[length + min_length] = '\0';
        }
    }

    closedir(dir);
    rt_free(full_path);
519
}
mysterywolf's avatar
mysterywolf 已提交
520
#endif /* DFS_USING_POSIX */
521

B
Bernard Xiong 已提交
522 523
void msh_auto_complete(char *prefix)
{
524 525 526
    int length, min_length;
    const char *name_ptr, *cmd_name;
    struct finsh_syscall *index;
B
Bernard Xiong 已提交
527

528 529
    min_length = 0;
    name_ptr = RT_NULL;
B
Bernard Xiong 已提交
530

531
    if (*prefix == '\0')
532 533 534 535
    {
        msh_help(0, RT_NULL);
        return;
    }
B
Bernard Xiong 已提交
536

mysterywolf's avatar
mysterywolf 已提交
537
#ifdef DFS_USING_POSIX
538 539 540 541 542 543 544 545 546 547 548 549
    /* check whether a spare in the command */
    {
        char *ptr;

        ptr = prefix + rt_strlen(prefix);
        while (ptr != prefix)
        {
            if (*ptr == ' ')
            {
                msh_auto_complete_path(ptr + 1);
                break;
            }
G
Grissiom 已提交
550

551 552
            ptr --;
        }
G
Grissiom 已提交
553 554 555 556 557 558 559 560
#ifdef RT_USING_MODULE
        /* There is a chance that the user want to run the module directly. So
         * try to complete the file names. If the completed path is not a
         * module, the system won't crash anyway. */
        if (ptr == prefix)
        {
            msh_auto_complete_path(ptr);
        }
561
#endif /* RT_USING_MODULE */
562
    }
mysterywolf's avatar
mysterywolf 已提交
563
#endif /* DFS_USING_POSIX */
G
Grissiom 已提交
564

565 566 567 568 569
    /* checks in internal command */
    {
        for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
        {
            /* skip finsh shell function */
570
            cmd_name = (const char *) index->name;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
            if (strncmp(prefix, cmd_name, strlen(prefix)) == 0)
            {
                if (min_length == 0)
                {
                    /* set name_ptr */
                    name_ptr = cmd_name;
                    /* set initial length */
                    min_length = strlen(name_ptr);
                }

                length = str_common(name_ptr, cmd_name);
                if (length < min_length)
                    min_length = length;

                rt_kprintf("%s\n", cmd_name);
            }
        }
    }

    /* auto complete string */
    if (name_ptr != NULL)
    {
        rt_strncpy(prefix, name_ptr, min_length);
    }

    return ;
B
Bernard Xiong 已提交
597
}
马志远 已提交
598
#endif /* RT_USING_FINSH */