不到断气不罢休 发表于 2024-12-5 06:08:24

【Linux】模拟实现下令行表明器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;
int gargc = 0;

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

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

// 全局的工作路径
char pwd;
char pwdenv;

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

string GetHostName()
{
    char hostname;
    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;
    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 = 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 = strtok(command_buffer, seq);
    while (gargv = strtok(nullptr, seq))
      ;
    gargc--;
}

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

//执行命令
bool ExecuteCommand()
{
    pid_t id = fork();
    if (id < 0)
      return false;
    else if (id == 0)
    {
      // 子进程
      //执行命令
      execvpe(gargv, 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++;
    }
    genv = (char*)malloc(strlen(item)+1);
    strncpy(genv,item,strlen(item)+1);
    index++;
    genv = nullptr;
}

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

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

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

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【Linux】模拟实现下令行表明器shell