⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 还是arm7操作源码
💻 C
字号:
/******************************************************************************
    Copyright (c) 2006 by RockOS.
    All rights reserved.

    This software is supported by the Rock Software Workroom only.

    Any bugs please contact the author with e-mail or QQ:
     E-mail : baobaoba520@yahoo.com.cn
         QQ : 59681888
*******************************************************************************
File name   : shell.c
Description : A easy Shell implement in RockOS.
            : 
            : 
Auther      : sunxinqiu
History     :
  2006-3-15   first release.
******************************************************************************/

#include "os.h"

#if MAX_SHELL_CMD > 0

/******************************************************************************
Global var  : SHCMDCB g_shCmd[MAX_SHELL_CMD];
Description : the all shell commands.
            : 
******************************************************************************/
SHCMDCB g_shCmd[MAX_SHELL_CMD];

/******************************************************************************
Function    : STATUS shell_init()
Params      : N/A
            : 
            : 
            : 
Return      : OS_SUCCESS or OS_FAIL
Description : init the shell commands list.
            : 
******************************************************************************/
STATUS shell_init()
{
    U32 i;

    /* init command set. */
    for (i = 0; i < MAX_SHELL_CMD; i++)
    {
        memset (&g_shCmd[i], 0, sizeof(SHCMDCB));

        g_shCmd[i].state = OS_CMD_FREE;
    }

    regShellCmd(help, "help", "list all shell commands and their description.");
    regShellCmd(version, "ver", "show system version information.");
    regShellCmd(memShow, "mem", "show system memory block information.");
    regShellCmd(memDump, "d", "Dump memory area.");
    regShellCmd(fsmShow, "fsm", "show FSM information.");
    regShellCmd(queueShow, "queue", "show queue information.");
    regShellCmd(msgQShow, "msgQ", "show msg queue information.");
    regShellCmd(spy, "spy", "show cpu load rate.");
    regShellCmd(taskShow, "i", "show all task information.");
    regShellCmd(ts, "ts", "suspend a task.");
    regShellCmd(tw, "tw", "wakeup a task.");
    regShellCmd(tr, "tr", "restart a task.");
    regShellCmd(td, "td", "delete a task.");

    return OS_SUCCESS;
}

/******************************************************************************
Function    : STATUS regShellCmd()
Params      : cmdFunc - the shell command function
            : name    - the shell command line to call function 'command'
            : desc    - the description information for help.
            : 
Return      : OS_SUCCESS or OS_FAIL
Description : to register a shell command.
            : 
******************************************************************************/
STATUS regShellCmd(SHELL_CMD cmdFunc, const char * name, const char * desc)
{
    HANDLE handle;

    /* check params. */
    if (name == NULL)
    {
        OS_printf("regShellCmd(): command name null, command [0x%08x]!!!\n", cmdFunc);
        return OS_FAIL;
    }

    if (cmdFunc == NULL)
    {
        OS_printf("regShellCmd(): command is null, name [%s]!!!\n", name);
        return OS_FAIL;
    }

    OS_ENTER_CRITICAL();
    for (handle = 0; handle < MAX_SHELL_CMD; handle++)
    {
        if (g_shCmd[handle].state == OS_CMD_FREE)
        {
            g_shCmd[handle].state = OS_CMD_REG;
            break;
        }
    }
    OS_LEAVE_CRITICAL();

    if (handle >= MAX_SHELL_CMD)
    {
        OS_printf("regShellCmd(): not enough shell command space!!!\n");
        return OS_FAIL;
    }

    /* save the command information. */
    g_shCmd[handle].command = cmdFunc;
    strncpy (&g_shCmd[handle].name[0], name, MAX_CMD_NAME_LEN);
    strncpy (&g_shCmd[handle].descript[0], desc, MAX_CMD_DESC_LEN);

    /* sort the command table.*/

    return OS_SUCCESS;
}

/******************************************************************************
Function    : STATUS unregShellCmd(SHELL_CMD cmdFunc)
Params      : cmdFunc - the command function.
            : 
            : 
            : 
Return      : OS_SUCCESS always.
Description : to unregister a shell command by the command function. 
            : 
******************************************************************************/
STATUS unregShellCmd(SHELL_CMD cmdFunc)
{
    HANDLE handle;

    if (cmdFunc == NULL)
    {
        OS_printf("regShellCmd(): command is null!!!\n");
        return OS_FAIL;
    }

    for (handle = 0; handle < MAX_SHELL_CMD; handle++)
    {
        if (g_shCmd[handle].command == cmdFunc)
        {
            OS_ENTER_CRITICAL();
            memset(&g_shCmd[handle], 0, sizeof(SHCMDCB));

            g_shCmd[handle].state = OS_CMD_FREE;
            OS_LEAVE_CRITICAL();
        }
    }

    /* sort the command table.*/

    return OS_SUCCESS;
}

/******************************************************************************
Function    : SHELL_CMD getShellCmd (const char * name)
Params      : name - the command name get from shell prompt.
            : 
            : 
            : 
Return      : the shell command function.
Description : to get more efficiency, this function should be optimised by a
            : dimidiate search algrithm.
            : For a simple shell, I use a sequancely search algrithm.
******************************************************************************/
SHELL_CMD getShellCmd (const char * name)
{
    SHELL_CMD command;

    HANDLE handle;

    if (name == NULL)
    {
        return NULL;
    }

    command = NULL;
    for (handle = 0; handle < MAX_SHELL_CMD; handle++)
    {
        if ((g_shCmd[handle].state == OS_CMD_REG)
            &&(strcmp(&g_shCmd[handle].name[0], name) == 0))
        {
            command = g_shCmd[handle].command;
            break;
        }
    }

    return command;
}

void exec_cmd (char * cmdName, int argc, char * argv[])
{
    int ret;

    SHELL_CMD pFunc;

    if ((cmdName == NULL)||(cmdName[0] == '\0'))
    {
        return;
    }

    pFunc = getShellCmd(cmdName);

    if (pFunc != NULL)
    {
        ret = pFunc(argc, argv);
        OS_printf("Command done, val = %d\n", ret);
    }
    else
    {
        OS_printf("Unknown command name: [%s]\n", cmdName);
    }
}

/******************************************************************************
Function    : void OSOnKey(U8 ch)
Params      : N/A
            : 
            : 
            : 
Return      : N/A
Description : This is the key or uart RxD isr hook for RockOS.
            : 
******************************************************************************/
void OSOnKey (U8 ch)
{
    HMSG hmsg;
    APP_MSG * pmsg;

    /* send a message to shell task. */
    hmsg = msgAlloc(MINI_MSG_PACK);
    pmsg = (APP_MSG *)msgMap(hmsg);
    if (pmsg != NULL)
    {
        pmsg->src = g_shellTaskId;
        pmsg->dest = g_shellTaskId;
        pmsg->msg = MSG_KEY_ARRIVE;
        pmsg->length = 1;
        pmsg->data[0] = ch;

        if (msgQSend(hmsg, g_shellTaskId, OS_NO_WAIT) != OS_SUCCESS)
        {
            msgFree(hmsg);
        }
    }
}

/******************************************************************************
Function    : void tShellEntry(void * p)
Params      : p - not used.
            : 
            : 
            : 
Return      : N/A(never return).
Description : the shell task entry point.
            : 
******************************************************************************/
void tShellEntry(void * p)
{
    HMSG hmsg;
    APP_MSG * pmsg;

    U16  length;
    char cmdLine[MAX_CMD_LINE_LEN + 1];

    int  argc;
    char * argv[MAX_CMD_ARGS];
    char cmdName[MAX_CMD_NAME_LEN+1];

    char ch;

    /* clear the cmd line. */
    length = 0;
    memset (&cmdLine[0], 0, MAX_CMD_LINE_LEN+1);
    OS_putchar('\n');
    OS_putchar('-');
    OS_putchar('>');

    while(1)
    {
        if (msgQReceive(&hmsg, OS_WAIT_FOR_EVER) != OS_SUCCESS)
        {
            continue;
        }
        pmsg = msgMap(hmsg);
        if (pmsg == NULL)
        {
            continue;
        }
        switch(pmsg->msg)
        {
        case MSG_KEY_ARRIVE:
            ch = pmsg->data[0];
            if ((ch == 0x0a)||(ch == 0x0d))
            {
                OS_putchar('\n');
                if (length != 0)
                {
                    OS_ENTER_CRITICAL();
                    cmdLine[length] = '\0';
                    if (parseCmdLine(&cmdLine[0], &cmdName[0], &argc, argv) == OS_SUCCESS)
                    {
                        exec_cmd(&cmdName[0], argc, argv);
                    }
                    else
                    {
                        OS_printf("command line [%s], parse error!!!\n", &cmdName[0]);
                    }
                    OS_putchar('\n');
                    OS_LEAVE_CRITICAL();

                    length = 0;
                    memset (&cmdLine[0], 0, MAX_CMD_LINE_LEN+1);
                }
                OS_putchar('-');
                OS_putchar('>');
            }
            else if (ch == 0x1b)    /* ESC key.*/
            {
                length = 0;
                memset (&cmdLine[0], 0, MAX_CMD_LINE_LEN+1);

                OS_putchar('\\'); /* char '\', this char must send twicely, @_@. */
                OS_putchar('\n');
                OS_putchar('-');
                OS_putchar('>');
            }
            else if (ch == '\b')
            {
                if (length > 0)
                {
                    OS_putchar('\b');
                    OS_putchar(' ');
                    OS_putchar('\b');
                    length--;
                    cmdLine[length] = '\0';
                }
            }
            else if ((ch >= 0x20) &&(ch <= 0x7E))   /* all normal key. */
            {
                OS_putchar(ch);
                if (length < MAX_CMD_LINE_LEN)
                {
                    cmdLine[length] = ch;
                    length++;
                }
                else
                {
                    OS_putchar(7);   /* beep on host. */
                }
            }
            break;

        default:
            break;
        }

        msgFree(hmsg);
    }
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -