C++實現(xiàn)模擬shell命令行(代碼解析)
更新時間:2021年12月21日 09:25:52 作者:Jxiepc
這篇文章主要介紹了C++實現(xiàn)模擬shell命令行,本文通過實例代碼進行命令行解析,代碼簡單易懂,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、解析
/** * 進行命令行解析: * 多個空格 * 分割符:< > | * */ void parse(){ std::string line; getline(std::cin, line); /** 解析字符串 */ int len = line.size(), i=0; std::string tmp; std::vector<std::string> tmp_vc; while(i < line.size()){ if(line[i] == ' '){ i++; continue; } if(line[i] == '|') { vc.push_back(tmp_vc); tmp = ""; i++; continue; } int pos = line.find(' ', i); // 獲取下一個空格的位置 tmp = line.substr(i, pos-i); // 截取字符串 tmp_vc.push_back(tmp); i = pos; } vc.push_back(tmp_vc); }
二、執(zhí)行命令函數(shù)
/** 執(zhí)行命令子函數(shù) */ void func(std::vector<std::string>& v){ char *arr[10]; pid_t pid; pid = fork(); if(pid == -1){ std::cout << "fork error" << std::endl; exit(1); }else if(pid ==0){ for(int i=0; i<v.size(); ++i) arr[i] = (char *)v[i].c_str(); arr[v.size()] = NULL; execvp(arr[0], arr); }else{ wait(NULL); } } /** 執(zhí)行命令 * -------- * 創(chuàng)建子進程執(zhí)行 * 當出現(xiàn)|需要創(chuàng)建多個子進程 * 當出現(xiàn)> <則將內(nèi)容寫入文件或者命令行 * */ void execCommnd(){ for(int i=0; i<vc.size(); ++i){ func(vc[i]); } }
三、模擬shell
/** 獲取當前所在目錄 */ void getCurPwd(){ std::string s = get_current_dir_name(); int pos = s.rfind('/'); std::string tmp = s.substr(pos+1, s.length()-pos); std::cout << tmp << "]# "; } /** 獲取當前用戶名 */ void getIdname(){ struct passwd *pwd; pwd = getpwuid(getuid()); std::cout << "[" <<pwd->pw_name << "@"; } /** 獲取當前主機名 */ void getHostName(){ char buf_w[128]; int hostname = gethostname(buf_w, sizeof(buf_w)); std::cout << buf_w << " "; } /** 顯示菜單 */ void showMenu(){ getIdname(); getHostName(); getCurPwd(); }
四、完整代碼
/*---------------------------------------------------------------------- > File Name: shellDemo.cpp > Author: Jxiepc > Mail: Jxiepc > Created Time: Sun 19 Dec 2021 11:24:21 AM CST ----------------------------------------------------------------------*/ #include <iostream> #include <string> #include <cstring> #include <vector> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <pwd.h> #include <wait.h> /* 存儲命令以及參數(shù) */ std::vector<std::vector<std::string>> vc; /** * 進行命令行解析: * 多個空格 * 分割符:< > | * */ void parse(){ std::string line; getline(std::cin, line); /** 解析字符串 */ int len = line.size(), i=0; std::string tmp; std::vector<std::string> tmp_vc; while(i < line.size()){ if(line[i] == ' '){ i++; continue; } if(line[i] == '|') { vc.push_back(tmp_vc); tmp = ""; i++; continue; } int pos = line.find(' ', i); // 獲取下一個空格的位置 tmp = line.substr(i, pos-i); // 截取字符串 tmp_vc.push_back(tmp); i = pos; } vc.push_back(tmp_vc); } /** 執(zhí)行命令子函數(shù) */ void func(std::vector<std::string>& v){ char *arr[10]; pid_t pid; pid = fork(); if(pid == -1){ std::cout << "fork error" << std::endl; exit(1); }else if(pid ==0){ for(int i=0; i<v.size(); ++i) arr[i] = (char *)v[i].c_str(); arr[v.size()] = NULL; execvp(arr[0], arr); }else{ wait(NULL); } } /** 執(zhí)行命令 * -------- * 創(chuàng)建子進程執(zhí)行 * 當出現(xiàn)|需要創(chuàng)建多個子進程 * 當出現(xiàn)> <則將內(nèi)容寫入文件或者命令行 * */ void execCommnd(){ for(int i=0; i<vc.size(); ++i){ func(vc[i]); } } /** 獲取當前所在目錄 */ void getCurPwd(){ std::string s = get_current_dir_name(); int pos = s.rfind('/'); std::string tmp = s.substr(pos+1, s.length()-pos); std::cout << tmp << "]# "; } /** 獲取當前用戶名 */ void getIdname(){ struct passwd *pwd; pwd = getpwuid(getuid()); std::cout << "[" <<pwd->pw_name << "@"; } /** 獲取當前主機名 */ void getHostName(){ char buf_w[128]; int hostname = gethostname(buf_w, sizeof(buf_w)); std::cout << buf_w << " "; } /** 顯示菜單 */ void showMenu(){ getIdname(); getHostName(); getCurPwd(); } void test(){ while(1){ showMenu(); parse(); execCommnd(); } } int main(int argc, char* argv[]) { test(); return 0; }
四、運行結(jié)果
到此這篇關(guān)于C++實現(xiàn)模擬shell命令行的文章就介紹到這了,更多相關(guān)C++ shell命令行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中putchar與getchar函數(shù)的細節(jié)及運用
C語言提供putchar函數(shù),用于給終端輸出一個字符;getchar函數(shù),可以從終端接收用戶輸入的一個字符,本文給大家分享C++中putchar與getchar函數(shù)的細節(jié)及運用,感興趣的朋友跟隨小編一起看看吧2021-07-07C語言中的內(nèi)聯(lián)函數(shù)(inline)與宏定義(#define)詳細解析
內(nèi)聯(lián)函數(shù)與宏本質(zhì)上是兩個不同的概念如果程序編寫者對于既要求快速,又要求可讀的情況下,則應(yīng)該將函數(shù)冠以inline2013-09-09