1 Star 1 Fork 5K

王栓 / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
shell-command-development-guidelines.md 12.64 KB
一键复制 编辑 原始数据 按行查看 历史
NEEN 提交于 2021-03-12 17:59 . !197 Docs Update version 1.0.1

Shell Command Development Guidelines

Development Guidelines

You can perform the following operations to add shell commands:

  1. Include the following header files:

    #include "shell.h"
    #include "shcmd.h"
  2. Register commands. You can register commands either statically or dynamically when the system is running. In most cases, static registration is widely used by common system commands, and dynamic registration is widely used by user commands.

    1. Static registration:

      1. Register a command using a macro.

        The prototype of the macro is as follows:

        SHELLCMD_ENTRY(l, cmdType, cmdKey, paraNum, cmdHook)

        Table 1 Parameters of the SHELLCMD_ENTRY macro

        Parameter

        Description

        l

        Indicates the global variable name to be passed during the static registration. Note that the name cannot be the same as other symbol names in the system.

        cmdType

        Indicates the command type.

        • CMD_TYPE_EX: does not support standard command parameters and will mask the command keywords you entered. For example, if you enter ls /ramfs, only /ramfs will be passed to the registration function, and the ls command keyword will be masked.

        • CMD_TYPE_STD: supports standard command parameters. All the characters you entered will be passed to the registration function after being parsed.

        cmdKey

        Indicates the command keyword, which is the name used to access a shell function.

        paraNum

        Indicates the maximum number of input parameters in the execution function to be invoked. This parameter is not supported currently.

        cmdHook

        Indicates the address of the execution function, that is, the function that is actually executed by the command.

        For example:

        SHELLCMD_ENTRY(ls_shellcmd,  CMD_TYPE_EX, "ls", XARGS,  (CMD_CBK_FUNC)osShellCmdLs)
      2. Add the required options to the build/mk/liteos_tables_ldflags.mk file.

        For example, when registering the ls command, add -uls_shellcmd to the build/mk/liteos_tables_ldflags.mk file. -u is followed by the first parameter of SHELLCMD_ENTRY.

    2. Dynamic registration:

      The prototype of the function to register is as follows:

      UINT32 osCmdReg(CmdT ype cmdType, CHAR *cmdKey, UINT32 paraNum, CmdCallBackFunc cmdProc)

      Table 2 Parameters of UINT32 osCmdReg

      Parameter

      Description

      cmdType

      Indicates the command type.

      • CMD_TYPE_EX: does not support standard command parameters and will mask the command keywords you entered. For example, if you enter ls /ramfs, only /ramfs will be passed to the registration function, and the ls command keyword will be masked.

      • CMD_TYPE_STD: supports standard command parameters. All the characters you entered will be passed to the registration function after being parsed.

      cmdKey

      Indicates the command keyword, which is the name used to access a shell function.

      paraNum

      Indicates the maximum number of input parameters in the execution function to be invoked. This parameter is not supported currently. The default value is XARGS(0xFFFFFFFF).

      cmdHook

      Indicates the address of the execution function, that is, the function that is actually executed by the command.

      For example:

      osCmdReg(CMD_TYPE_EX, "ls", XARGS,  (CMD_CBK_FUNC)osShellCmdLs)

    NOTE: The command keyword must be unique. Specifically, two different commands cannot share the same command keyword. Otherwise, only one command is executed. When executing user commands sharing the same keyword, the shell executes only the first command in the help commands.

  3. Use the following function prototype to add built-in commands:

    UINT32 osShellCmdLs(UINT32 argc,  CHAR **argv)

    Table 3 Parameters of osShellCmdLs

    Parameter

    Description

    argc

    Indicates the number of parameters in the shell command.

    argv

    Indicates a pointer array, where each element points to a string. You can determine whether to pass the command keyword to the registration function by specifying the command type.

  4. Enter the shell command in either of the following methods:

    • Enter the shell command in the serial port tool.

    • Enter the shell command in the Telnet tool. For details, see telnet.

1
https://gitee.com/wang-shuan/docs.git
git@gitee.com:wang-shuan/docs.git
wang-shuan
docs
docs
master

搜索帮助