162 Commits

Author SHA1 Message Date
wangjianyu3
3108c27877 apps/nshlib: Save result and return ERROR if lib_get_tempbuffer() fails
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-16 22:48:34 +08:00
wangjianyu3
a3049aae56 apps/nshlib: Using lib_get_tempbuffer() to save stack space
Comparison

  Config: "esp32s3-devkit:adb" with `CONFIG_LINE_MAX=512`

  Test CMD: `ls | cat | cat | cat`

  Without this patch

    |                | Before Test CMD | After Test CMD  |
    |---------------:|----------------:|----------------:|
    | nsh_main.STACK | 0002624/0008096 | 0002624/0008096 |
    |       sh.STACK | 0003360/0004008 | 0003360/0004008 |
    |     Free/Total |   355288/403116 |   355288/403116 |

  With this patch

    |                | Before Test CMD | After Test CMD  |
    |---------------:|----------------:|----------------:|
    | nsh_main.STACK | 0001616/0008096 | 0001616/0008096 |
    |       sh.STACK | 0002352/0004008 | 0002352/0004008 |
    |     Free/Total |   355288/403116 |   354760/403116 |

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-14 19:31:54 +08:00
wangjianyu3
26173597a9 apps/nshlib: replace CONFIG_NSH_LINELEN to LINE_MAX
LINE_MAX: https://github.com/apache/nuttx/pull/15344

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2025-01-14 19:31:54 +08:00
Alin Jerpelea
27d8ae36ba nshlib: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-12-30 18:02:50 +08:00
wangjianyu3
39bd365787 nshlib/nsh_parse: Removing unnecessary value assigning about redirection
Coverity Log

  CID 1612757: (#1 of 1): UNUSED_VALUE
  assigned_value: The value -1 is assigned to param.fd_in here, but the stored value is overwritten before it can be used.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-12-19 14:11:55 +08:00
wangjianyu3
f1212a6065 nshlib/nsh_parse: Closing fds opened for redirection if necessary
Coverity Log

  CID 1612743: (#1 of 1): Resource leak (RESOURCE_LEAK)
  12. leaked_handle: The handle variable fd_out goes out of scope and leaks the handle.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-12-19 14:11:55 +08:00
wangjianyu3
3c4ddd2802 nshlib/pipeline: Concat variable arguments failed
1. Without this patch

  nsh> set var_test `uname`
  nsh> echo $var_test
  NuttX
  nsh> echo $var_test | cat
  sh [5:100]

  nsh>

2. With this patch

  nsh> set var_test `uname`
  nsh> echo $var_test
  NuttX
  nsh> echo $var_test | cat
  sh [4:100]
  NuttX
  nsh>

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-14 15:51:40 +08:00
buxiasen
3a6ecb82b5 nshlib/nsh_parse: Fix variable arguments concat error of nsh_execute()
Without this patch

  nsh> set time 5
  nsh> echo $time
  5
  nsh> sleep $time &
  sh [5:100]
  nsh> nsh: sleep: missing required argument(s)

With this patch

  nsh> set time 5
  nsh> echo $time
  5
  nsh> sleep $time &
  sh [4:100]
  nsh> ps
    PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
      0     0   0 FIFO     Kthread   - Ready              0000000000000000 0069616 Idle_Task
      1     0 224 FIFO     Kthread   - Waiting  Signal    0000000000000000 0067536 loop_task
      2     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067504 hpwork 0x501760e0 0x50176128
      3     3 100 FIFO     Task      - Running            0000000000000000 0067536 nsh_main
      4     4 100 FIFO     Task      - Waiting  Signal    0000000000000000 0067520 sh -c sleep

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-14 15:49:14 +08:00
wangjianyu3
b3e1077f87 nsh: Using sizeof() to get string length of char array
Changed from calling `strlen()` at runtime to using `sizeof()` at compile time.

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:46 +08:00
wangjianyu3
c052bd8377 nsh: Add pipeline support for nsh commandline
And nested pipeline supported.

Test
  1. Redirect in
    cat < /etc/init.d/rc.sysinit

  2. Simple pipeline
    ls | cat

  3. Nested pipeline
    ls | dd | cat | dd | cat

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:46 +08:00
wangjianyu3
3da204c23e Pack parameters of nsh_execute() as struct nsh_exec_param_s
1. Input redirect flags currently is hardcode, passing by arguments maybe better.
2. Only support redirect to path_name, redirect to fd is needed for pipeline.

Test
  1. Redirect in
    cat < /etc/init.d/rc.sysinit

  2. Redirect with FIFO
    mkfifo /dev/testfifo
    cat /dev/testfifo &
    ls > /dev/testfifo

  3. NSH Params
    set name `uname`
    echo $name

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-11-11 10:36:46 +08:00
buxiasen
790be6d639 nshlib/nsh_parse: use sh -c replace pthread detach when nsh background.
pthread & detach will still quit when parent task exit,
cause nsh_parse clone args leak. nsh should use task instead of thread

this case can reproduce the memory leak.
int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!\n");
  system("sleep 1 &");
  return 0;
}

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
2024-10-22 00:04:09 +08:00
Bowen Wang
4af9653b1e nshlib/nsh_parse: support use \\ to pass '\' to argument
Now the nsh can use \\ to pass '\' to the command

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-16 10:21:30 +08:00
ouyangxiangzhen
44d2250b6e apps: Fix compilation error after nuttx/arch.h excluded.
This patch fix compilation error after excluding nuttx/arch.h.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2024-10-16 07:12:40 +08:00
yinshengkai
2ea062642f nshlib: Add startup process buried points
Used for system startup time profiling

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2024-10-15 12:34:09 +08:00
wangjianyu3
2bfe2c2ed0 nshlib: Fix a typo error of redirect
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-10-13 15:48:57 +08:00
Bowen Wang
6f85062019 nshlib/nsh_parse: support use \" and \' to pass " and ' to arguments
Now we can pass symbol " and ' to the nsh command arguments

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-11 19:22:57 +08:00
anjiahao
c916f163f2 nsh:support wait command to wait task exit.
usage like:
  nsh> sleep 10 &
  [5:100]
  nsh>set pid1=$!
  nsh>sleep 15 &
  [6:100]
  nsh>set pid2=$!
  nsh>wait $pid1 $pid2

'wait' command will block nsh from running until pid1 and pid2 finish.
This is useful for parallel requirements to perform certain tasks

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-02 21:13:54 +08:00
anjiahao
5dcfe93a95 nsh:support $! get last pid use nsh run
test:
    nsh> hello &
    hello [4:100]
    nsh> Hello, World!!

    nsh> echo $!
    4
    nsh> echo $!
    3
    nsh> ps
      PID GROUP PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK           STACK COMMAND
        0     0   0 FIFO     Kthread   - Ready              0000000000000000 0069616 Idle_Task
        1     1 224 FIFO     Kthread   - Waiting  Signal    0000000000000000 0067536 loop_task
        2     2 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067504 hpwork 0x45ae80 0x45aea8
        3     3 100 FIFO     Task      - Running            0000000000000000 0067536 nsh_main
    nsh>

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-09-28 11:28:15 +08:00
guoshichao
573008244c greenhills: fix the variable unused warning
"nsh_parse.c", line 1565: warning #550-D: variable "quoted" was set but never
          used
    bool quoted;
         ^

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-08-20 02:50:13 +08:00
Marco Casaroli
0797ee2312 fix(nsh_parse): handle env variables correctly
The PR #2469 broke handling of environment variables because an
error in the if/else if control flow.

This fixes it.
2024-08-10 11:08:18 -03:00
Marco Casaroli
e46347ec1b nsh_fileapp: handle input redirection 2024-08-08 19:19:18 -03:00
Marco Casaroli
96100b30f2 feat(nsh): input (stdin) redirection
This adds support for `<` to redirect input on nsh commands.
2024-08-08 19:19:18 -03:00
Yanfeng Liu
31908b3128 nshlib/prompt: extend NSH prompt string management
Currently NSH prompt is defined at build time, thus improper for AMP cases
where the same NSH binary is used on different nodes as the same NSH prompt
shows on all nodes.

This patch attempts to support runtime prompt string population from ordered
sources:
  - the environment variable defined by NSH_PROMPT_ENV plus suffix
  - the NSH_PROMPT_STRING
  - the HOSTNAME plus suffix
The suffix is defined by NSH_PROMPT_SUFFIX so that to clearly separate the
command inputs.

Changes in `nshlib/`

- Kconfig:       add configs NSH_PROMPT_MAX/ENV/SUFFIX etc
- nsh.h:         adjust g_nshprompt defs, add nsh_update_prompt
- nsh_parse.c    relocate g_nshpromt to nsh_prompt.c
- nsh_init.c     revise to use nsh_update_prompt once
- nsh_session.c  revise to use methods in nsh_prompt.c
- Makefile       add nsh_prompt.c
- CMakeLists.txt add nsh_prompt.c

New additions in `nshlib/`

- nsh_prompt.c   prompt related data structures and methods.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
2024-02-25 00:55:47 -08:00
Zhe Weng
396ab2e931 nsh/alias: Fix compiler warning
Got use-after-free warning under GCC 12 with `-O3` option, and I found
that `nsh_strcat` may realloc `ptr`, then `cmdline` may point to invalid
memory.

Let `cmdline` point to the reallocated `ptr` may solve the problem.
Tested by `alias ll='ls -l'` and `ll /` on sim.

GCC output:

CC:  binfmt_unloadmodule.c In function 'nsh_aliasexpand',
    inlined from 'nsh_argument' at nsh_parse.c:1879:20:
nsh_parse.c:1196:23: error: pointer 'ptr' used after 'realloc' [-Werror=use-after-free]
 1196 |               ptr     = cmdline + len;
      |               ~~~~~~~~^~~~~~~~~~~~~~~
In function 'nsh_strcat',
    inlined from 'nsh_aliasexpand' at nsh_parse.c:1190:21,
    inlined from 'nsh_argument' at nsh_parse.c:1879:20:
nsh_parse.c:1100:27: note: call to 'realloc' here
 1100 |   argument  = (FAR char *)realloc(s1, allocsize);
      |                           ^~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-08-01 08:13:23 -07:00
chao an
ec63217b48 nshlib: disable nsh error print if NSH_DISABLE_ERROR_PRINT enabled
text	   data	    bss	    dec	    hex	filename
 398953	  27088	   4128	 430169	  69059	nuttx    /* before */
 389241	  27072	   4128	 420441	  66a59	nuttx    /* after  */
  -9712     -16

Signed-off-by: chao an <anchao@xiaomi.com>
2023-06-12 13:37:27 +08:00
Xiang Xiao
134b8b538f Replace all strcat with strlcat
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-05-08 21:35:16 +03:00
Ville Juven
4ce809e7d4 nsh/nsh_parse: Fix handling of back-quotes
The logic that handles back-quotes was faulty, i.e. example command
set FOO `ls -l` would be split into two tokens as follows:
- set FOO `ls
- -l`

This results in nsh: `: no matching ` error, this fixes that issue.
2023-04-19 03:12:18 +08:00
Ville Juven
0914c20c0d nsh/alias: Add support for alias arguments
This adds support for more complex alias handling, such as:

$ alias ls='ls -l'

Previously such an alias was not split into the command verb and the
argument correctly, instead the full alias string was handled as the
verb, which obviously fails.

This commit fixes this by expanding the alias, checking whether it has
arguments and if so, it merges the expanded alias + the old command line
together, resulting in a completely new command line.

Example (assuming the alias above has been created):

$ ls /bin

Results in a new command line: "ls -l /bin" which is then parsed and
executed.
2023-04-17 15:09:38 +09:00
Ville Juven
e20d2688ab nsh/alias: Do not expand argument lists, only the command
A resulting word that is identified to be the command name word of a
simple command shall be examined to determine whether it is an unquoted,
valid alias name.

The keyword here being "a simple command", arguments are not subject to
expansion.
2023-03-23 22:10:19 +02:00
Ville Juven
ad18a50282 nsh/nsh_parse.c: Fix variable expansion inside double quotes
This also prevents expanding anything when inside single quotes, for
full POSIX compliance.
2023-03-23 22:10:19 +02:00
Ville Juven
f9dfb51001 nsh/nshlib: Add alias support for nsh
This adds support for string aliases into nsh. There are some nuances that
are not handled correctly yet:

- Reserved words can be overloaded, which is a clear POSIX violation
2023-03-23 22:10:19 +02:00
Ville Juven
bf40833d2e nsh/nsh_parse.c: Add support for single ('') and double ("") quotes
The parser is modified to detect, handle and remove quotes from the
command string. Whatever is inside the quotes is treated as a string
literal. If no matching end quote is found, the terminal prints out
and error.
2023-03-23 22:10:19 +02:00
Xiang Xiao
359f66ad37 nshlib: Remove the remaining CONFIG_FILE_STREAM dependence
continue the change from https://github.com/apache/nuttx-apps/pull/1559

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-02-24 17:38:19 +08:00
Huang Qi
95f32fd018 nsh: Decouple with CONFIG_FILE_STREAM
Replace all fwrite/fread/fgets/... to write/read/...

Before:
```
       text    data     bss     dec     hex filename
 109827     601    6608  117036   1c92c nuttx/nuttx
```
    After:
```
       text    data     bss     dec     hex filename
108053     601    6608  115262   1c23e nuttx/nuttx
```
    After with CONFIG_FILE_STREAM disabled:
```
       text    data     bss     dec     hex filename
 105667     601    6608  112876   1b8ec nuttx/nuttx
```

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2023-02-22 13:47:41 +08:00
Xiang Xiao
a29d9ea9da fsutils/examples: Include unistd.h explicitly
to get the prototypes or macros are defined in it

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-02-05 08:46:59 +02:00
Junbo Zheng
79e770b0c6 apps/nshlib: add the missing FAR macro for all source files
Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
2022-09-22 14:22:13 +08:00
Fotis Panagiotopoulos
0cdb2cfa48 Fixed compiler warnings. 2022-08-23 01:39:59 +08:00
Norman Rasmussen
6285990a5c Remove trailing whitespace from commands as parameters 2022-01-01 22:18:07 +08:00
Norman Rasmussen
92287e2b14 Fix some edge case bugs in nsh_parse
- Handle nsh_filecat returning NULL on failure
- Background and redirect must be restored after an empty line
- Output redirection should be removed from argv like background
2022-01-01 22:18:07 +08:00
Xiang Xiao
ecae66fe59 Replace all CONFIG_NFILE_STREAMS with CONFIG_FILE_STREAM
follow up the kernel side change

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Ic27ad65d7cc2ea570921e0c17098dcb6bfe1893a
2020-09-11 17:57:58 +08:00
Masayuki Ishikawa
7f48576218 nshlib: Fix memory corruption in nsh_parse.c
Summary:
- Fix memory corruption when pthread_create() failed in nsh_execute()

Impact:

- nsh builtin command execution in background with errors

Testing:

- Tested with hifive1-revb:nsh
- Set CONFIG_MAX_TASKS=4
- Run 'sleep 1000 &' in 3 times will cause pthread_create error
- Run free, ps command

Reported-by: Yoshinori Sugino <ysgn0101@gmail.com>
Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2020-08-11 13:57:02 +02:00
chao.an
898c1ce4d2 Revert "This critical behavior was broken by this commit:"
This reverts commit 9b06b508f957ba1cc5b970d291bb5cc3277db237.

Signed-off-by: chao.an <anchao@xiaomi.com>
2020-05-18 06:53:57 -06:00
Gregory Nutt
4f54a75c48 apps/nshlib/nsh_parse.c: Add some comments.
Add some comments to nsh_parse.c emphasizing the reason that things are done in the order that they are.  Perhaps such comments will avoid similar breakage in the future.
2020-05-18 10:03:17 +08:00
Gregory Nutt
60428baed6 apps/nshlib/nsh_parse.c: Run through nxstyle. 2020-05-17 23:38:05 +01:00
Gregory Nutt
9b06b508f9 This critical behavior was broken by this commit:
commit 9a28ccf836c1b9f0eb5e1163964042eddc207697
    Author: chao.an <anchao@xiaomi.com>
    Date:   Fri Feb 21 09:54:47 2020 +0800

        nsh/parse: try the builtin configuration first

        In the case of enable the BUILTIN_APPS/FILE_APPS at the same time,
        try the builtin list first to ensure that the relevant configuration
        (stacksize, priority) can be set normally.

This commit breaks the feature because it changes the order to that the built-in application is tried first.  Hence, the version on the file system will never replace the built-in version.

That commit must be reverted in order to restore the correct functionality.

    Revert "nsh/parse: try the builtin configuration first"

    This reverts commit 9a28ccf836c1b9f0eb5e1163964042eddc207697.
2020-05-17 23:38:05 +01:00
Xiang Xiao
41d88f06e7 Run codespell -w with the latest dictonary again 2020-02-23 07:10:14 -06:00
chao.an
9a28ccf836 nsh/parse: try the builtin configuration first
In the case of enable the BUILTIN_APPS/FILE_APPS at the same time, try the builtin list first to ensure that the relevant configuration(stacksize, priority) can be set normally.
2020-02-21 07:40:47 -06:00
Xiang Xiao
857158451b Unify the void cast usage
1.Remove void cast for function because many place ignore the returned value witout cast
2.Replace void cast for variable with UNUSED macro

Change-Id: Ie644129a563244a6397036789c4c3ea83c4e9b09
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2020-01-02 23:21:01 +08:00
Gregory Nutt
0536c5b523 apps/nshlib/nsh_parse.c: Correct an error found in build testing. Commit 2a462c78aa5f4ea6dc374eedd86bc85f9f79a0c4 was insufficient. It was a mistake to revert Xiang's change of commit 9defae8af641752506d92b72ea68c8f94d24d580. But we are on a different vector now. 2019-12-24 12:18:30 -06:00