当前位置: 首页 > article >正文

模拟实现命令行解释器shell

shell本质就是一个进程,它提供了一个用户界面,用于接收用户输入的命令,并将这些命令解释成操作系统能够理解和执行的操作。它充当了用户和操作系统内核之间的中介。例如,在 Linux 系统中,当用户在终端输入ls命令时,shell 会解释这个命令,告诉操作系统去列出当前目录下的文件和目录信息。

下面是模拟实现shell的基本代码:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

const int basesize = 1024;
const int argvnum = 64;
const int envnum = 64;

//全局的命令行参数表
char *gargv[argvnum];
int gargc = 0;

//我自己的环境变量
char *genv[envnum];

//全局变量,用来表示退出结果
int lastcode = 0;

// 全局的工作路径
char pwd[basesize];
char pwdenv[basesize * 2];

string GetName()
{
    string name = getenv("USER");
    return name.empty() ? "None" : name;
}

string GetHostName()
{
    char hostname[basesize];
    gethostname(hostname, sizeof(hostname));
    ;
    return gethostname(hostname, sizeof(hostname)) != 0 ? "None" : hostname;
}

string GetPwd()
{
    // getcwd获取当前工作路径
    if (nullptr == getcwd(pwd, sizeof(pwd)))
        return "None";
    // 讲获取的当前路径输入到pwdenv
    snprintf(pwdenv, sizeof(pwdenv), "PWD=%s", pwd);
    // 导入环境变量
    putenv(pwdenv);
    return pwd;
    // string pwd = getenv("PWD");
    // return pwd.empty() ? "None" : pwd;
}

string LastDir()
{
    string curr = GetPwd();
    if(curr == "/" || curr == "None") return curr;
    // /home/xzl/xxx
    size_t pos = curr.rfind("/");
    if(pos == std::string::npos) return curr;
    return curr.substr(pos+1);
}

string MakeCommandLine()
{
    char Command_Line[basesize];
    snprintf(Command_Line, basesize, "[%s@%s %s]#",\
     GetName().c_str(), GetHostName().c_str(), LastDir().c_str());
    return Command_Line;
}

// 打印命令行提示符
void PrintCommandLine()
{
    printf("%s", MakeCommandLine().c_str());
    fflush(stdout);
}
// 获取用户命令
bool GetCommandLine(char command_buffer[])
{
    // 获取字符串
    char *result = fgets(command_buffer, basesize, stdin);
    if (!result)
    {
        return false;
    }
    command_buffer[strlen(command_buffer) - 1] = 0;
    if (strlen(command_buffer) == 0)
        return false;
    return true;
}

//  分析命令
void ParseCommandLine(char command_buffer[])
{
    memset(gargv, 0, sizeof(gargc));
    gargc = 0;
    const char *seq = " ";
    gargv[gargc++] = strtok(command_buffer, seq);
    while (gargv[gargc++] = strtok(nullptr, seq))
        ;
    gargc--;
}

void debug()
{
    printf("argc: %d\n", gargc);
    for (int i = 0; gargv[i]; i++)
    {
        printf("argv[%d]: %s\n", i, gargv[i]);
    }
}

//执行命令
bool ExecuteCommand()
{
    pid_t id = fork();
    if (id < 0)
        return false;
    else if (id == 0)
    {
        // 子进程
        //执行命令
        execvpe(gargv[0], gargv,genv);
        //退出
        exit(1);
    }
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if (rid > 0)
    {
        if(WIFEXITED(status))
        {
            lastcode = WEXITSTATUS(status);
        }
        else//表示代码异常退出
        {
            lastcode = 100;
        }
        return true;

    }
    return false;
}

void AddEnv(const char *item)
{
    int index = 0;
    while(genv[index])
    {
        index++;
    }
    genv[index] = (char*)malloc(strlen(item)+1);
    strncpy(genv[index],item,strlen(item)+1);
    index++;
    genv[index] = nullptr;
}

//在shell中
//有些命令,必须由子进程执行
//有些命令,不能由子进程执行,要由shell自己执行 -----内建命令 built command
bool CheckAndExecBuiltCommand()
{
    //不能让子进程进行,因为子进程退出就结束了,并不能影响下一个进程的工作路径
    if (strcmp(gargv[0], "cd") == 0)
    {
        if (gargc == 2)
        {
            chdir(gargv[1]);
            lastcode = 0;
        }
        else
        {
            lastcode = 1;
        }
        return true;
    }
    else if(strcmp(gargv[0],"export")==0)
    {
        if(gargc == 2)
        {
            AddEnv(gargv[1]);
            lastcode = 0;
        }
        else
        {
            lastcode = 2;
        }
    }
    else if(strcmp(gargv[0],"env") == 0)
    {
        for(int i = 0 ;genv[i];i++)
        {
            printf("%s\n",genv[i]);
        }
        lastcode = 0;
        return true;
    }
    else if(strcmp(gargv[0],"echo") == 0)
    {
        if(gargc == 2)
        {
            //echo $?
            //echo $PATH
            //echo hello
            if(gargv[1][0] == '$')
            {
                if(gargv[1][1] == '?')
                {
                    printf("%d\n",lastcode);
                    lastcode = 0;
                }
            }
            else
            {
                printf("%s\n",gargv[1]);
                lastcode = 0;
            }
        }
        else
        {
            lastcode = 3;
        }
        return true;
    }
    return false;
}

//作为一个shell,获取环境变量应该从系统环境变量获取
//今天外面做不到就直接从父进程shell中获取环境变量
void InitEnv()
{
    extern char **environ;
    int index = 0;
    while (environ[index])
    {
        genv[index] = (char*)malloc(strlen(environ[index])+1);
        strncpy(genv[index],environ[index],strlen(environ[index]));
        index++;
    }
    genv[index] = nullptr;
    
}

int main()
{
    //初始化环境变量表
    InitEnv();
    char command_buffer[basesize];
    while (true)
    {
        // 打印命令行提示符
        PrintCommandLine();
        // 获取用户命令
        if (!GetCommandLine(command_buffer))
        {
            continue;
        }
        // printf("%s\n", command_buffer);
        //  分析命令
        ParseCommandLine(command_buffer);

        // 判断是不是内建命令
        if (CheckAndExecBuiltCommand())
        {
            continue;
        }
        // debug();
        // 执行命令
        ExecuteCommand();
    }
    return 0;
}


http://www.kler.cn/a/417663.html

相关文章:

  • 动态图推理问答算法
  • 软件工程导论三级项目报告--《软件工程》课程网站
  • 【Git】一、初识Git Git基本操作详解
  • 基于 Java 开发的 MongoDB 企业级应用全解析
  • 携程Java开发面试题及参考答案 (200道-上)
  • 求水仙花数,提取算好,打表法。或者暴力解出来。
  • 【C++初阶】第5课—动态内存管理
  • 混乱原理与程序设计。
  • clickhouse 使用global in 优化 in查询
  • 【VUE3】新版Vue3+ElementPlus全家桶开发视频项目实战
  • vxlan 手工隧道(头端复制)
  • burpsuite功能模块学习(2)
  • 后台管理-动态路由配置以及用户权限管理(vue3+element plus+koa+Sequelize )
  • android将pcm byte[]通过Librtmp进行rtmp推流
  • 【Flink-scala】DataStream编程模型之窗口计算-触发器-驱逐器
  • 深度学习:在PyTorch中进行模型验证完整流程
  • 【问题】webdriver.Chrome()设置参数executable_path报不存在
  • HDMI协议
  • AI是泡沫吗
  • Python语法基础(五)
  • 创建模态对话框窗口
  • SpringBoot 框架下的新冠密接者跟踪系统:卓越性能与稳定性保障
  • 【Python网络爬虫笔记】6- 网络爬虫中的Requests库
  • tomcat控制台中文乱码的解决方法
  • 使用LabVIEW2019x64的IMAQdx调用工业相机采图(二)
  • DataWhale—PumpkinBook(TASK07支持向量机)