xv6-user/sh.c
void
runcmd(struct cmd *cmd)
{
  int p[2];
  struct backcmd *bcmd;
  struct execcmd *ecmd;
  struct listcmd *lcmd;
  struct pipecmd *pcmd;
  struct redircmd *rcmd;

  if(cmd == 0)
    exit(1);

  switch(cmd->type){
  default:
    panic("runcmd");

  case EXEC:
    ecmd = (struct execcmd*)cmd;
    if(ecmd->argv[0] == 0)
      exit(1);
    
    exec(ecmd->argv[0], ecmd->argv);

    int i;
    char env_cmd[64];
    for(i=0; i<nenv; i++)
    {
      char *s_tmp = env_cmd;
      char *d_tmp = envs[i].value;
      while((*s_tmp = *d_tmp++))
        s_tmp++;
      *s_tmp++ = '/';
      d_tmp = ecmd->argv[0];
      while((*s_tmp++ = *d_tmp++))
        ;

      exec(env_cmd, ecmd->argv);
    }
    fprintf(2, "exec %s failed\n", ecmd->argv[0]);
    break;

  case REDIR:
    rcmd = (struct redircmd*)cmd;
    close(rcmd->fd);
    if(open(rcmd->file, rcmd->mode) < 0){
      fprintf(2, "open %s failed\n", rcmd->file);
      exit(1);
    }
    runcmd(rcmd->cmd);
    break;

  case LIST:
    lcmd = (struct listcmd*)cmd;
    if(fork1() == 0)
      runcmd(lcmd->left);
    wait(0);
    runcmd(lcmd->right);
    break;

  case PIPE:
    pcmd = (struct pipecmd*)cmd;
    if(pipe(p) < 0)
      panic("pipe");
    if(fork1() == 0){
      close(1);
      dup(p[1]);
      close(p[0]);
      close(p[1]);
      runcmd(pcmd->left);
    }
    if(fork1() == 0){
      close(0);
      dup(p[0]);
      close(p[0]);
      close(p[1]);
      runcmd(pcmd->right);
    }
    close(p[0]);
    close(p[1]);
    wait(0);
    wait(0);
    break;

  case BACK:
    bcmd = (struct backcmd*)cmd;
    if(fork1() == 0)
      runcmd(bcmd->cmd);
    break;
  }
  exit(0);
}
// Execute cmd.  Never returns.
Execute cmd.  Never returns.
Execute cmd . Never returns .
void
runcmd(struct cmd *cmd)
{
  int p[2];
  struct backcmd *bcmd;
  struct execcmd *ecmd;
  struct listcmd *lcmd;
  struct pipecmd *pcmd;
  struct redircmd *rcmd;
  if(cmd == 0)
    exit(1);
  switch(cmd->type){
  default:
    panic("runcmd");
  case EXEC:
    ecmd = (struct execcmd*)cmd;
    if(ecmd->argv[0] == 0)
      exit(1);
    exec(ecmd->argv[0], ecmd->argv);
    int i;
    char env_cmd[64];
    for(i=0; i<nenv; i++)
    {
      char *s_tmp = env_cmd;
      char *d_tmp = envs[i].value;
      while((*s_tmp = *d_tmp++))
        s_tmp++;
      *s_tmp++ = '/';
      d_tmp = ecmd->argv[0];
      while((*s_tmp++ = *d_tmp++))
        ;
      exec(env_cmd, ecmd->argv);
    }
    fprintf(2, "exec %s failed\n", ecmd->argv[0]);
    break;
  case REDIR:
    rcmd = (struct redircmd*)cmd;
    close(rcmd->fd);
    if(open(rcmd->file, rcmd->mode) < 0){
      fprintf(2, "open %s failed\n", rcmd->file);
      exit(1);
    }
    runcmd(rcmd->cmd);
    break;
  case LIST:
    lcmd = (struct listcmd*)cmd;
    if(fork1() == 0)
      runcmd(lcmd->left);
    wait(0);
    runcmd(lcmd->right);
    break;
  case PIPE:
    pcmd = (struct pipecmd*)cmd;
    if(pipe(p) < 0)
      panic("pipe");
    if(fork1() == 0){
      close(1);
      dup(p[1]);
      close(p[0]);
      close(p[1]);
      runcmd(pcmd->left);
    }
    if(fork1() == 0){
      close(0);
      dup(p[0]);
      close(p[0]);
      close(p[1]);
      runcmd(pcmd->right);
    }
    close(p[0]);
    close(p[1]);
    wait(0);
    wait(0);
    break;
  case BACK:
    bcmd = (struct backcmd*)cmd;
    if(fork1() == 0)
      runcmd(bcmd->cmd);
    break;
  }
  exit(0);
}
void runcmd ( struct cmd * cmd ) { int p [ 2 ] ; struct backcmd * bcmd ; struct execcmd * ecmd ; struct listcmd * lcmd ; struct pipecmd * pcmd ; struct redircmd * rcmd ; if ( cmd == 0 ) exit ( 1 ) ; switch ( cmd -> type ) { default : panic ( " " ) ; case EXEC : ecmd = ( struct execcmd * ) cmd ; if ( ecmd -> argv [ 0 ] == 0 ) exit ( 1 ) ; exec ( ecmd -> argv [ 0 ] , ecmd -> argv ) ; int i ; char env_cmd [ 64 ] ; for ( i = 0 ; i < nenv ; i ++ ) { char * s_tmp = env_cmd ; char * d_tmp = envs [ i ] . value ; while ( ( * s_tmp = * d_tmp ++ ) ) s_tmp ++ ; * s_tmp ++ = ' ' ; d_tmp = ecmd -> argv [ 0 ] ; while ( ( * s_tmp ++ = * d_tmp ++ ) ) ; exec ( env_cmd , ecmd -> argv ) ; } fprintf ( 2 , " \n " , ecmd -> argv [ 0 ] ) ; break ; case REDIR : rcmd = ( struct redircmd * ) cmd ; close ( rcmd -> fd ) ; if ( open ( rcmd -> file , rcmd -> mode ) < 0 ) { fprintf ( 2 , " \n " , rcmd -> file ) ; exit ( 1 ) ; } runcmd ( rcmd -> cmd ) ; break ; case LIST : lcmd = ( struct listcmd * ) cmd ; if ( fork1 ( ) == 0 ) runcmd ( lcmd -> left ) ; wait ( 0 ) ; runcmd ( lcmd -> right ) ; break ; case PIPE : pcmd = ( struct pipecmd * ) cmd ; if ( pipe ( p ) < 0 ) panic ( " " ) ; if ( fork1 ( ) == 0 ) { close ( 1 ) ; dup ( p [ 1 ] ) ; close ( p [ 0 ] ) ; close ( p [ 1 ] ) ; runcmd ( pcmd -> left ) ; } if ( fork1 ( ) == 0 ) { close ( 0 ) ; dup ( p [ 0 ] ) ; close ( p [ 0 ] ) ; close ( p [ 1 ] ) ; runcmd ( pcmd -> right ) ; } close ( p [ 0 ] ) ; close ( p [ 1 ] ) ; wait ( 0 ) ; wait ( 0 ) ; break ; case BACK : bcmd = ( struct backcmd * ) cmd ; if ( fork1 ( ) == 0 ) runcmd ( bcmd -> cmd ) ; break ; } exit ( 0 ) ; }
Execute cmd.
Execute cmd .