这篇文章给大家分享的是有关C++如何实现模拟shell命令行的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
/**
* 进行命令行解析:
* 多个空格
* 分割符:< > |
* */
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);
}
/** 执行命令子函数 */
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);
}
}
/** 执行命令
* --------
* 创建子进程执行
* 当出现|需要创建多个子进程
* 当出现> <则将内容写入文件或者命令行
* */
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();
}
/*----------------------------------------------------------------------
> 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>
/* 存储命令以及参数 */
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);
}
/** 执行命令子函数 */
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);
}
}
/** 执行命令
* --------
* 创建子进程执行
* 当出现|需要创建多个子进程
* 当出现> <则将内容写入文件或者命令行
* */
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;
}
感谢各位的阅读!关于“C++如何实现模拟shell命令行”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。