基于C语言实现shell指令的详解_C 语言

源代码来自于TI开发板
在ARM上实现shell命令解析

第一步:构建命令实现函数和命令表
1,定义结构体 和命令表

复制代码 代码如下:

typedef int (*pfnCmdLine)(int argc, char *argv[]);
//*****************************************************************************
//
//! Structure for an entry in the command list table.
//
//*****************************************************************************
typedef struct
{
    //
    //! A pointer to a string containing the name of the command.
    //
    const char *pcCmd;
    //
    //! A function pointer to the implementation of the command.
    //
    pfnCmdLine pfnCmd;
    //
    //! A pointer to a string of brief help text for the command.
    //
    const char *pcHelp;
}
tCmdLineEntry;
//*****************************************************************************
//
//! This is the command table that must be provided by the application.
//
//*****************************************************************************
extern tCmdLineEntry g_sCmdTable[];

2,编写命令执行函数  实现命令表

复制代码 代码如下:

int
Cmd_help(int argc, char *argv[])
{
    tCmdLineEntry *pEntry;
    //
    // Print some header text.
    //
    UARTprintf("\nAvailable commands\n");
    UARTprintf("------------------\n");
    //
    // Point at the beginning of the command table.
    //
    pEntry = &g_sCmdTable[0];
    //
    // Enter a loop to read each entry from the command table.  The
    // end of the table has been reached when the command name is NULL.
    //
    while(pEntry->pcCmd)
    {
        //
        // Print the command name and the brief description.
        //
        UARTprintf("%s%s\n", pEntry->pcCmd, pEntry->pcHelp);
        //
        // Advance to the next entry in the table.
        //
        pEntry++;
    }
    //
    // Return success.
    //
    return(0);
}

复制代码 代码如下:

int
Cmd_ls(int argc, char *argv[])
{
    unsigned long ulTotalSize;
    unsigned long ulFileCount;
    unsigned long ulDirCount;
    FRESULT fresult;
    FATFS *pFatFs;
    //
    // Open the current directory for access.
    //
    fresult = f_opendir(&g_sDirObject, g_cCwdBuf);
    //
    // Check for error and return if there is a problem.
    //
    if(fresult != FR_OK)
    {
        return(fresult);
    }
    ulTotalSize = 0;
    ulFileCount = 0;
    ulDirCount = 0;
    //
    // Give an extra blank line before the listing.
    //
    UARTprintf("\n");
    //
    // Enter loop to enumerate through all directory entries.
    //
    for(;;)
    {
        //
        // Read an entry from the directory.
        //
        fresult = f_readdir(&g_sDirObject, &g_sFileInfo);
        //
        // Check for error and return if there is a problem.
        //
        if(fresult != FR_OK)
        {
            return(fresult);
        }
        //
        // If the file name is blank, then this is the end of the
        // listing.
        //
        if(!g_sFileInfo.fname[0])
        {
            break;
        }
        //
        // If the attribue is directory, then increment the directory count.
        //
        if(g_sFileInfo.fattrib & AM_DIR)
        {
            ulDirCount++;
        }
        //
        // Otherwise, it is a file.  Increment the file count, and
        // add in the file size to the total.
        //
        else
        {
            ulFileCount++;
            ulTotalSize += g_sFileInfo.fsize;
        }
        //
        // Print the entry information on a single line with formatting
        // to show the attributes, date, time, size, and name.
        //
        UARTprintf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9u  %s\n",
                    (g_sFileInfo.fattrib & AM_DIR) ? 'D' : '-',
                    (g_sFileInfo.fattrib & AM_RDO) ? 'R' : '-',
                    (g_sFileInfo.fattrib & AM_HID) ? 'H' : '-',
                    (g_sFileInfo.fattrib & AM_SYS) ? 'S' : '-',
                    (g_sFileInfo.fattrib & AM_ARC) ? 'A' : '-',
                    (g_sFileInfo.fdate >> 9) + 1980,
                    (g_sFileInfo.fdate >> 5) & 15,
                     g_sFileInfo.fdate & 31,
                    (g_sFileInfo.ftime >> 11),
                    (g_sFileInfo.ftime >> 5) & 63,
                     g_sFileInfo.fsize,
                     g_sFileInfo.fname);
 // tcp_write(Rpcb,g_sFileInfo.fname,sizeof(g_sFileInfo.fname),0);
    }   // endfor
    //
    // Print summary lines showing the file, dir, and size totals.
    //
    UARTprintf("\n%4u File(s),%10u bytes total\n%4u Dir(s)",
                ulFileCount, ulTotalSize, ulDirCount);
    //
    // Get the free space.
    //
    fresult = f_getfree("/", &ulTotalSize, &pFatFs);
    //
    // Check for error and return if there is a problem.
    //
    if(fresult != FR_OK)
    {
        return(fresult);
    }
    //
    // Display the amount of free space that was calculated.
    //
    UARTprintf(", %10uK bytes free\n", ulTotalSize * pFatFs->sects_clust / 2);
    //
    // Made it to here, return with no errors.
    //
    return(0);
}

复制代码 代码如下:

tCmdLineEntry g_sCmdTable[] =
{
    { "help",   Cmd_help,      " : Display list of commands" },
    { "h",      Cmd_help,   "    : alias for help" },
    { "?",      Cmd_help,   "    : alias for help" },
    { "ls",     Cmd_ls,      "   : Display list of files" },
    { "chdir",  Cmd_cd,         ": Change directory" },
    { "cd",     Cmd_cd,      "   : alias for chdir" },
    { "pwd",    Cmd_pwd,      "  : Show current working directory" },
    { "cat",    Cmd_cat,      "  : Show contents of a text file" },
 { "rm",     CMD_Delete,   "  : Delete a file or a folder"    },
    { 0, 0, 0 }
};

第二步:编写命令解析 执行函数

复制代码 代码如下:

//*****************************************************************************
//
// cmdline.c - Functions to help with processing command lines.
//
// Copyright (c) 2007-2010 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 6594 of the Stellaris Firmware Development Package.
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup cmdline_api
//! @{
//
//*****************************************************************************
#include <string.h>
#include "cmdline.h"
//*****************************************************************************
//
// Defines the maximum number of arguments that can be parsed.
//
//*****************************************************************************
#ifndef CMDLINE_MAX_ARGS
#define CMDLINE_MAX_ARGS        8
#endif
//*****************************************************************************
//
//! Process a command line string into arguments and execute the command.
//!
//! \param pcCmdLine points to a string that contains a command line that was
//! obtained by an application by some means.
//!
//! This function will take the supplied command line string and break it up
//! into individual arguments.  The first argument is treated as a command and
//! is searched for in the command table.  If the command is found, then the
//! command function is called and all of the command line arguments are passed
//! in the normal argc, argv form.
//!
//! The command table is contained in an array named <tt>g_sCmdTable</tt> which
//! must be provided by the application.
//!
//! \return Returns \b CMDLINE_BAD_CMD if the command is not found,
//! \b CMDLINE_TOO_MANY_ARGS if there are more arguments than can be parsed.
//! Otherwise it returns the code that was returned by the command function.
//
//*****************************************************************************
int
CmdLineProcess(char *pcCmdLine)
{
    static char *argv[CMDLINE_MAX_ARGS + 1];
    char *pcChar;
    int argc;
    int bFindArg = 1;
    tCmdLineEntry *pCmdEntry;
    //
    // Initialize the argument counter, and point to the beginning of the
    // command line string.
    //
    argc = 0;
    pcChar = pcCmdLine;
    //
    // Advance through the command line until a zero character is found.
    //
    while(*pcChar)
    {
        //
        // If there is a space, then replace it with a zero, and set the flag
        // to search for the next argument.
        //
        if(*pcChar == ' ')
        {
            *pcChar = 0;
            bFindArg = 1;
        }
        //
        // Otherwise it is not a space, so it must be a character that is part
        // of an argument.
        //
        else
        {
            //
            // If bFindArg is set, then that means we are looking for the start
            // of the next argument.
            //
            if(bFindArg)
            {
                //
                // As long as the maximum number of arguments has not been
                // reached, then save the pointer to the start of this new arg
                // in the argv array, and increment the count of args, argc.
                //
                if(argc < CMDLINE_MAX_ARGS)
                {
                    argv[argc] = pcChar;
                    argc++;
                    bFindArg = 0;
                }
                //
                // The maximum number of arguments has been reached so return
                // the error.
                //
                else
                {
                    return(CMDLINE_TOO_MANY_ARGS);
                }
            }
        }
        //
        // Advance to the next character in the command line.
        //
        pcChar++;
    }
    //
    // If one or more arguments was found, then process the command.
    //
    if(argc)
    {
        //
        // Start at the beginning of the command table, to look for a matching
        // command.
        //
        pCmdEntry = &g_sCmdTable[0];
        //
        // Search through the command table until a null command string is
        // found, which marks the end of the table.
        //
        while(pCmdEntry->pcCmd)
        {
            //
            // If this command entry command string matches argv[0], then call
            // the function for this command, passing the command line
            // arguments.
            //
            if(!strcmp(argv[0], pCmdEntry->pcCmd))
            {
                return(pCmdEntry->pfnCmd(argc, argv));
            }
            //
            // Not found, so advance to the next entry.
            //
            pCmdEntry++;
        }
    }
    //
    // Fall through to here means that no matching command was found, so return
    // an error.
    //
    return(CMDLINE_BAD_CMD);
}

第三步:收到命令 调用解析函数
接收可用串口 网口等
假如收到的吗,命令为  ls -l
*cmd="ls -l";
CmdLineProcess(cmd);

时间: 2024-09-20 00:13:04

基于C语言实现shell指令的详解_C 语言的相关文章

C 语言指针变量的运算详解_C 语言

指针变量保存的是地址,本质上是一个整数,可以进行部分运算,例如加法.减法.比较等,请看下面的代码: #include <stdio.h> int main(){ int a = 10, *pa = &a, *paa = &a; double b = 99.9, *pb = &b; char c = '@', *pc = &c; //最初的值 printf("&a=%#X, pa=%#X, pb=%#X, pc=%#X\n", &

基于C/C++时间函数的使用详解_C 语言

C/C++对时间的操作也有许多值得大家注意的地方.最近,在技术群中有很多网友也多次问到过C++语言中对时间的操作.获取和显示等等的问题.下面,在这篇文章中,笔者将主要介绍在C/C++中时间和日期的使用方法.通过学习许多C/C++库,你可以有很多操作.使用时间的方法.但在这之前你需要了解一些"时间"和"日期"的概念,主要有以下几个:Coordinated Universal Time(UTC):协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(Gre

基于C语言中段错误的问题详解_C 语言

当我在linux下写c语言的时候经常会遇到段错误.所以就来细究一下. 段错误或段违规(segmentation violation)查看Expert C Programming(Peter Van Der Linden) Pg.156解释到段错误是由于内存管理单元(MMU)的异常所致,而该异常则通常是由于解除引用一个未初始化或非法的指针引起. 就是指针正在引用一个并不位于你的地址空间中的地址.书中的例子 复制代码 代码如下: int *p = 0;  *p = 17;  这里显然 地址0 并不是

基于C++类型重定义的使用详解_C 语言

这几天工作时碰到一个C++的编译错误(我使用的是Visual C++ 7.0),说是有一个类重复定义,仔细想想我们的这个项目也是做了好几个Release了, 内部代码应该不会有这样的低级错误, 真把类型给重复定义了,检查结果正如我预料的一样. 就这样, 我左右没找到原因,被一个编译错误给卡在那里了.(在我的概念中, 程序错误的等级为:编译错误->链接错误->逻辑错误, 此错误属于最低级 ).这时我仔细看了一下错误提示, 发现重复定义是由于从两个不同的路径包含了同一个头文件而引起的,同事也建议从

基于C++自动化编译工具的使用详解_C 语言

@echo off  title=HYTradingGateway自动化编译打包工具mode con cols=80 lines=40echo -------------------------------------------------------------echo 使用说明echo.                                                             echo HYTradingGateway自动化编译打包工具.修改echo 使用前提

基于Windows API分解路径问题的详解_C 语言

在很多代码中看到,由于需要获取一个路径字符串中的文件名或者扩展名,很多程序员都用了c库中的字符串查找函数来获取.这样子写得代码很多,也麻烦.其实在VC中,可以用更好的方法来获取.没有什么特别多说明,代码如下: 复制代码 代码如下:     LPTSTR szfileName = new TCHAR[MAX_PATH];     GetModuleFileName(NULL, szfileName , MAX_PATH);     // 获取盘符.文件名.扩展名等信息    wchar_t dri

基于linux下获取时间函数的详解_C 语言

//-------------------------------------------------------------//asctime(将时间和日期以字符串格式表示)#include<time.h>定义函数char * asctime(const struct tm * timeptr);函数说明asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回.此函数已经由时区转换成当地时间,字符串格式为:"We

基于C++字符串替换函数的使用详解_C 语言

在C++中,字符串替换有很多方法,这里主要说一下STL里的WString中的替换,虽然WString自带了一个Replace函数,但是只能替换一次,太不好了,因此单独写了个替换函数[函数] 复制代码 代码如下: /**         * @brief   实现字符串替换         * @param   orignStr 源串         * @param   oldStr  查找的串         * @param   newStr  替换的新串         * @return

基于C++中sprintf的错误总结详解_C 语言

sprintf 是个变参函数,使用时经常出问题,而且只要出问题通常就是能导致程序崩溃的内存访问错误.下面对sprintf 常出错误问题进行简单的总结: 1.缓冲区溢出:第一个参数的长度太短了,解决办法:将第一个参数的长度扩大.打印字符串时,尽量使用"%.ns"的形式指定最大字符数char buf[5];sprintf(buf, ":%d", 3246);printf("buf is %s\n", buf); 将buf修改为char buf[6]