最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于C語言實現(xiàn)shell指令的詳解

 更新時間:2013年05月27日 11:00:18   作者:  
本篇文章是對C語言實現(xiàn)shell指令的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
源代碼來自于TI開發(fā)板
在ARM上實現(xiàn)shell命令解析

第一步:構(gòu)建命令實現(xiàn)函數(shù)和命令表
1,定義結(jié)構(gòu)體 和命令表
復(fù)制代碼 代碼如下:

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,編寫命令執(zhí)行函數(shù)  實現(xiàn)命令表
復(fù)制代碼 代碼如下:

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);
}

復(fù)制代碼 代碼如下:

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);
}

復(fù)制代碼 代碼如下:

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 }
};

第二步:編寫命令解析 執(zhí)行函數(shù)
復(fù)制代碼 代碼如下:

//*****************************************************************************
//
// 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);
}

第三步:收到命令 調(diào)用解析函數(shù)
接收可用串口 網(wǎng)口等
假如收到的嗎,命令為  ls -l
*cmd="ls -l";
CmdLineProcess(cmd);

相關(guān)文章

  • C++實用庫之DNS解析的實現(xiàn)

    C++實用庫之DNS解析的實現(xiàn)

    DNS解析是一種將域名轉(zhuǎn)換為相應(yīng)的IP地址的過程,本文主要介紹了C++實用庫之DNS解析的實現(xiàn),實現(xiàn)了快速、準(zhǔn)確的域名到IP地址的轉(zhuǎn)換,感興趣的可以了解一下
    2025-03-03
  • 用C語言程序判斷大小端模式

    用C語言程序判斷大小端模式

    本文介紹了用C語言程序判斷大小端的方法,與大家分享一下。
    2013-04-04
  • 顯示任何進(jìn)程加載的DLL文件的代碼

    顯示任何進(jìn)程加載的DLL文件的代碼

    c語言實現(xiàn)的顯示任何進(jìn)程加載的DLL,方便開發(fā)軟件的朋友
    2013-05-05
  • C++實現(xiàn)LeetCode(136.單獨的數(shù)字)

    C++實現(xiàn)LeetCode(136.單獨的數(shù)字)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(136.單獨的數(shù)字),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++入門之基礎(chǔ)語法學(xué)習(xí)教程

    C++入門之基礎(chǔ)語法學(xué)習(xí)教程

    這篇文章主要介紹了C++入門之基本語法學(xué)習(xí)教程,列出了C++的關(guān)鍵字,同時講解了注釋的寫法,需要的朋友可以參考下
    2016-05-05
  • C語言中g(shù)etchar()的原理以及易錯點解析

    C語言中g(shù)etchar()的原理以及易錯點解析

    用getchar()函數(shù)讀取字符串時,字符串會存儲在輸入緩沖區(qū)中,包括輸入的回車字符,下面這篇文章主要給大家介紹了關(guān)于C語言中g(shù)etchar()的原理以及易錯點解析的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Qt實現(xiàn)簡單動態(tài)時鐘

    Qt實現(xiàn)簡單動態(tài)時鐘

    這篇文章主要為大家詳細(xì)介紹了Qt實現(xiàn)簡單動態(tài)時鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • c++冒泡排序示例分享

    c++冒泡排序示例分享

    冒泡排序是一種計算機科學(xué)領(lǐng)域的較簡單的排序算法,這篇文章主要介紹了c++冒泡排序示例,需要的朋友可以參考下
    2014-03-03
  • C++中傳值、傳地址和傳引用究竟有哪些區(qū)別

    C++中傳值、傳地址和傳引用究竟有哪些區(qū)別

    指針是一個變量,只不過這個變量存儲的是一個地址,指向內(nèi)存的一個存儲單元,而引用跟原來的變量實質(zhì)上是同一個東西,只不過是原變量的一個別名而已,這篇文章主要給大家介紹了關(guān)于C++中傳值、傳地址和傳引用究竟有哪些區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • C++ 自定義控件的移植問題

    C++ 自定義控件的移植問題

    這篇文章主要介紹了C++ 自定義控件的移植問題,十分的簡單實用,有需要的小伙伴可以參考下。
    2015-06-06

最新評論

鸡东县| 新邵县| 龙州县| 望江县| 亚东县| 昭苏县| 津南区| 昌吉市| 澄城县| 论坛| 平泉县| 铁岭县| 岑巩县| 墨玉县| 永州市| 龙江县| 靖江市| 香格里拉县| 馆陶县| 清新县| 长垣县| 西贡区| 随州市| 望都县| 吴忠市| 四子王旗| 乌鲁木齐县| 台南县| 日照市| 万全县| 慈利县| 久治县| 禹州市| 富民县| 同仁县| 东丽区| 东丰县| 肥城市| 余干县| 谢通门县| 潞城市|