亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C > 正文

實現基于C語言的shell指令

2020-02-24 14:34:39
字體:
來源:轉載
供稿:網友

下面武林技術頻道的小編就為大家帶來一篇實現基于C語言的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
#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 g_sCmdTable 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);

以上就是實現基于C語言的shell指令的全部內容,如果大家想了解更多相關內容,請持續關注本站,本站小編將在第一時間為大家帶來更好的經典內容。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
奇米4444一区二区三区| 国产日韩中文在线| 日韩成人在线视频网站| 国产欧美日韩中文字幕在线| 亚洲欧洲一区二区三区在线观看| 欧美一区第一页| 日韩在线观看视频免费| 久久久久久亚洲精品| 91在线观看免费观看| 最近2019年日本中文免费字幕| 亚洲天堂第二页| 日韩在线播放一区| 国产欧美精品一区二区三区介绍| 精品美女久久久久久免费| 欧美在线观看www| 日本久久亚洲电影| 精品亚洲一区二区| 欧美区二区三区| 亚洲久久久久久久久久久| 国产精品久久久一区| 亚洲九九九在线观看| 91超碰caoporn97人人| 欧美亚洲另类制服自拍| 中文欧美在线视频| 亚洲欧洲黄色网| 日韩精品久久久久久久玫瑰园| 国产97免费视| 欧美激情奇米色| 欧美在线不卡区| 国产成人aa精品一区在线播放| 91极品女神在线| 精品福利视频导航| 久久久久国产精品www| 亚洲国产精品视频在线观看| 91精品国产91久久久久久久久| 亚洲精品视频免费在线观看| 欧美在线观看网址综合| 国产精品你懂得| 青草青草久热精品视频在线网站| 国产精品色午夜在线观看| 国产精品老牛影院在线观看| 18久久久久久| 日本成人精品在线| 久久人人爽国产| 美乳少妇欧美精品| 日韩欧美aaa| 一区三区二区视频| 欧美性猛交xxxx乱大交3| 国产精品久久久久国产a级| 亚洲欧洲日韩国产| 欧美日韩国产成人高清视频| 欧美在线国产精品| 成人综合国产精品| 久久精品视频网站| 国产美女精彩久久| 亚洲精品一区二区三区婷婷月| 琪琪亚洲精品午夜在线| 欧洲s码亚洲m码精品一区| 综合国产在线观看| 久久久精品欧美| 美女精品视频一区| 日韩一区二区三区xxxx| 久久久中精品2020中文| 久久婷婷国产麻豆91天堂| 国产91色在线播放| 精品国产精品三级精品av网址| 视频一区视频二区国产精品| 91精品国产91久久久久| 久久久精品视频成人| 久久久久久久影院| 三级精品视频久久久久| 亚洲视频自拍偷拍| 久久亚洲欧美日韩精品专区| 久久久久久久国产精品视频| 成人精品aaaa网站| 2020欧美日韩在线视频| 国内精品模特av私拍在线观看| 欧美韩日一区二区| 亚洲视频第一页| 欧美交受高潮1| 亚洲男人的天堂在线播放| 亚洲女人被黑人巨大进入al| 亚洲精品一区中文字幕乱码| 国产激情综合五月久久| 91久久国产精品91久久性色| 成人有码在线视频| 亚洲伊人成综合成人网| 亚洲九九九在线观看| 亚洲第一福利网| 久久成人精品一区二区三区| 精品久久中文字幕久久av| 亚洲第一中文字幕在线观看| 9.1国产丝袜在线观看| 色小说视频一区| 国产精品高潮呻吟久久av野狼| 日韩av在线一区二区| 亚洲无亚洲人成网站77777| 久久久久久久久中文字幕| 精品亚洲一区二区三区在线观看| 亚洲无限av看| 91在线精品视频| 精品综合久久久久久97| 亚洲情综合五月天| 国产免费一区视频观看免费| 国产91成人在在线播放| 欧美中文字幕在线播放| 国内揄拍国内精品| 色av中文字幕一区| 久久在线视频在线| 欧美久久精品午夜青青大伊人| 国产精品久久久久久中文字| 亚洲欧美第一页| 欧美自拍视频在线观看| 91国产美女视频| 北条麻妃久久精品| 久久精品91久久久久久再现| 中文字幕视频一区二区在线有码| 久久久久久网站| 精品久久久久久久中文字幕| 另类视频在线观看| 91免费看视频.| 黑人巨大精品欧美一区二区| 久久久综合av| 国产精品视频xxxx| www.日韩.com| 91av网站在线播放| 91香蕉嫩草神马影院在线观看| 亚洲国产精品久久久| 影音先锋日韩有码| 亚洲有声小说3d| 欧美黄色片视频| 欧美贵妇videos办公室| 欧美激情啊啊啊| 日韩久久免费电影| 欧美成人午夜激情视频| 成人久久一区二区| 色悠悠国产精品| 国产精品igao视频| 亚洲一区国产精品| 欧美激情第99页| 久久国产一区二区三区| 国产日韩欧美电影在线观看| 久久好看免费视频| 91九色蝌蚪国产| 国内精品一区二区三区四区| 色综合伊人色综合网| 国产日本欧美视频| 亚洲电影免费观看高清| 日韩电影在线观看免费| 亚洲一区av在线播放| 国产日本欧美一区二区三区在线| 日韩电影免费观看在线观看| 国产精品第二页| 欧美猛交ⅹxxx乱大交视频| 日本国产欧美一区二区三区| 国产欧美日韩视频| 久久不射热爱视频精品| 91夜夜揉人人捏人人添红杏| 中文字幕精品久久久久| 91精品91久久久久久| 精品中文字幕在线观看| 亚洲国产精品va在线| 青青a在线精品免费观看| www.国产精品一二区|