目录
  • 一、解析
  • 二、执行命令函数
  • 三、模拟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]);
    }
}

三、模拟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>

/* 存储命令以及参数 */
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命令行的文章就介绍到这了,更多相关c++ shell命令行内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!