2013-03-06 36 views
1

我正在另一个shell中运行这个基本的shell程序。我无法弄清楚为什么在执行“ls”后我的shell不能继续运行。我没有退出它,但它回到原来的壳。如果想要使用它,我必须每次运行我的shell程序。我想这是什么叉()应该做的。我只希望我的shell使用退出命令退出,我用if语句编码。任何建议将不胜感激。哦,不理会gettoks()解析器函数,我不知道如何使用它输入,所以我写了如果其他语句的字符串输入cmSTR而不是使用gettoks()解析器。主要是因为我想不出如何将输入传递到它如何让我的shell在命令后运行

#include <iostream> 
#include <unistd.h> 
#include <sys/types.h> 
#include <errno.h> 
#include <signal.h> 
#include <cstdlib> 
#include <sys/wait.h> 

using namespace std; 
// Initializing counters for trapping 
static int cc_counter = 0; 
static int cz_counter = 0; 
static int cq_counter = 0; 
//Functions for trap signal handler 
void cc_handler(int signo) 
{ 
++cc_counter; 
} 
void cz_handler(int signo) 
{ 
++cz_counter; 
} 
void cq_handler(int signo) 
{ 
++cq_counter; 
} 

//********************************************************* 
// 
// Extern Declarations 
// 
//********************************************************* 
using namespace std; 
extern "C" 
{ 
extern char **gettoks(); 
} 


//********************************************************* 
// 
// Main Function 
// 
//********************************************************* 
int main(int argc, char *argv[]) 
{ 
// local variables 
int ii; 
char **toks; 
int retval; 

// initialize local variables 
ii = 0; 
toks = NULL; 
retval = 0; 
char buf[1000];//Initialize of size for current working directory 
string cmSTR;//String to hold input 
int status;//Initialization of status for fork() 
pid_t pid;//Declaration of pid 

// main (infinite) loop 
while(true) 
{ 
    signal(SIGINT, cc_handler);// Traps Ctrl+C 
    signal(SIGTSTP, cz_handler);// Traps Ctrl+Z 
    signal(SIGQUIT, cq_handler);// Traps Ctrl+\ 
    //prompt and show current working directory 
    cout <<("RS_SHELL:") << getcwd(buf,1000) << "\t"; 
    getline(cin ,cmSTR);//read input from keyboard 
    // if else loop to switch based on command input 
    if(cmSTR == "ls")// if ls, then execute arguement 
    { 
    execl("/bin/ls", "ls", NULL);//System call to execute ls 

    } 
    else if(cmSTR == "exit")//if exit, then execute block of code 
    { 
     cout << "Ctrl C entered: " << ++cc_counter << "times"<< endl; 
     cout << "Ctrl Z entered: " << ++cz_counter << "times"<< endl; 
     cout << "Ctrl Back Slash entered: " << ++cq_counter << "times"<< endl; 
     exit(1); 
     } 
    else if(cmSTR == "guish")// if guish, execute guish shell 
    { 
     execvp("guish", NULL); 
     } 
     //if input is not any of previous commands then fork() 
    else if(cmSTR != "ls" && cmSTR != "exit" && cmSTR != "guish" && cmSTR != "\n") 
    { 
     pid = fork(); 
     if (pid < 0)//Loop to fork parent and child process 
     { 
      fprintf(stderr, "Fork Failed"); 
      exit(-1); 
      } 
      else if (pid == 0)//Child process 
     { 
      execvp("guish", NULL);//system call to execute guish shell 

      } 
      else //Parent process 
      { 
       waitpid(-1, &status,0); 
       exit(0); 
       } 
       } 




    // get arguments 
    toks = gettoks(); 

    if(toks[0] != NULL) 
{ 
    // simple loop to echo all arguments 
    for(ii=0; toks[ii] != NULL; ii++) 
    { 
     cout << "Argument " << ii << ": " << toks[ii] << endl; 
    } 

    if(!strcmp(toks[0], "exit")) 
    break; 
} 
    } 

    // return to calling environment 
    return(retval); 
    } 

回答

0

当你怀疑,execl及其相关功能叠加一个新的进程当前进程。因此,在启动lsexecl调用之后,程序将不再存在以继续运行。

如果你希望你的shell程序运行后ls周围停留,你需要fork()通话execl("/bin/ls", "ls", NULL);之前。另外,如果你想从ls的输出出现在你的shell的同一个控制台中,我想你可能打算这样做,你需要将输出从ls返回到你的shell,然后将输出写到你的外壳的控制台。例如参见Writing my own shell… stuck on pipes?

+0

以外的if语句? – user2125805 2013-03-06 22:31:56

+0

是的,我会倾向于'if(if)'之前的'fork()',而不是分别为每个'if'个案分叉 - DRY(不要重复自己)是一个很好的座右铭。 – Simon 2013-03-07 09:39:16