Esempio n. 1
0
//bool secondRun stops the interpreter from recursing more than 1 level into itself
void Interpret(char* cmdLine,bool secondRun)
{
  int task = 1;
  int bg = 0, i,k,j = 0, quotation1 = 0, quotation2 = 0;
  commandT **command;

  if(cmdLine[0] == '\0') return;

  for(i = 0; i < strlen(cmdLine); i++){
    if(cmdLine[i] == '\''){
      if(quotation2) continue;
      else if(quotation1){
        quotation1 = 0;continue;
      }
      else quotation1 = 1;
    }
    if(cmdLine[i] == '"'){
      if(quotation1) continue;
      else if(quotation2){
        quotation2 = 0;continue;
      }
      else quotation2 = 1;
    }

    if(cmdLine[i] == '|' && quotation1 != 1 && quotation2 != 1){
      task ++;
    }
  }

  command = (commandT **) malloc(sizeof(commandT *) * task);
  i = strlen(cmdLine) - 1;
  while(i >= 0 && cmdLine[i] == ' ') i--;
  if(cmdLine[i] == '&'){
    if(i == 0) return;
    bg = 1;
    cmdLine[i] = '\0';
  }

  quotation1 = quotation2 = 0;
  task = 0;
  k = strlen(cmdLine);
  for(i = 0; i < k; i++, j++){
    if(cmdLine[i] == '\''){
      if(quotation2) continue;
      else if(quotation1){
        quotation1 = 0;continue;
      }
      else quotation1 = 1;
    }
    if(cmdLine[i] == '"'){
      if(quotation1) continue;
      else if(quotation2){
        quotation2 = 0;continue;
      }
      else quotation2 = 1;
    }
    if(cmdLine[i] == '|' && quotation1 != 1 && quotation2 != 1){
      parser_single(&(cmdLine[i-j]), j, &(command[task]),bg);
      task++;
      j = -1;
    }
  }
  parser_single(&(cmdLine[i-j]), j, &(command[task]),bg);

  //
  // TILDE EXPANSION
  //
  bool changed = FALSE;
  int idx =0;
    //look through all the args
  while(idx < command[0]->argc){
      //special case for "unalias x". ie dont expand x
    if(command[0]->argv[idx][0] == '~'){

      char* home = getenv("HOME");

          //get the part after the tilde and add it to home
      strcat(home,(command[0]->argv[idx])+2);
      command[0]->argv[idx] = home;
      changed = TRUE;

    }
    idx++;
  }




  //
  // ALIAS
  //


  //only expand aliases on the first run to stop aliases from recrusively expanding
  if(!secondRun){

    //look for aliases
    int idx =0;
    //look through all the args
    while(idx < command[0]->argc){
      //special case for "unalias x". ie dont expand x
      if(strcmp(command[0]->argv[0],"unalias") != 0) 
      {
        if(IsAlias(command[0]->argv[idx])){
          (command[0]->argv)[idx] = GetAliasCmd(command[0]->argv[idx]);
          changed = TRUE;
        //check for tilde expansion
        }
      }
      idx++;
    }

    //if we expanded something
    if(changed){

      int newSize = 0;//the new size of the expanded command line

      //figure out the new size
      int idx2 =0;
      while(idx2 < command[0]->argc){
        newSize = newSize + strlen(command[0]->argv[idx2]) + 1;
        idx2++;
      }

      //copy all ths args to a new cmdLine string
      char newCmdLine[newSize + 1];
      strcpy (newCmdLine, command[0]->argv[0]);

      int idx3 =1;
      while(idx3 < command[0]->argc){
        strcat(newCmdLine," ");//add space between args
        strcat(newCmdLine,command[0]->argv[idx3]);
        idx3++;
      }

      //rerun the interpreter
      Interpret(newCmdLine,TRUE);


    }
    else{
      RunCmd(command, task+1);
      free(command);
    }

  }else{
    RunCmd(command, task+1);
    free(command);
  }
}
Esempio n. 2
0
/*Parse the whole command line and split commands if a piped command is sent.*/
void Interpret(char* cmdLine)
{ 
  int task = 1;
  int bg = 0, i,k,j = 0, quotation1 = 0, quotation2 = 0;
  commandT **command;

  if(cmdLine[0] == '\0') return;

  for(i = 0; i < strlen(cmdLine); i++){
    if(cmdLine[i] == '\''){
      if(quotation2) continue;
      else if(quotation1){
        quotation1 = 0;continue;
      }
      else quotation1 = 1;
    }
    if(cmdLine[i] == '"'){
      if(quotation1) continue;
      else if(quotation2){
        quotation2 = 0;continue;
      }
      else quotation2 = 1;
    }

    if(cmdLine[i] == '|' && quotation1 != 1 && quotation2 != 1){
      task ++;
    }
  }

  command = (commandT **) malloc(sizeof(commandT *) * task);
  i = strlen(cmdLine) - 1;
  while(i >= 0 && cmdLine[i] == ' ') i--;
  if(cmdLine[i] == '&'){
    if(i == 0) return;
    bg = 1;
    cmdLine[i] = '\0';
  }

  quotation1 = quotation2 = 0;
  task = 0;
  k = strlen(cmdLine);
  for(i = 0; i < k; i++, j++){
    if(cmdLine[i] == '\''){
      if(quotation2) continue;
      else if(quotation1){
        quotation1 = 0;continue;
      }
      else quotation1 = 1;
    }
    if(cmdLine[i] == '"'){
      if(quotation1) continue;
      else if(quotation2){
        quotation2 = 0;continue;
      }
      else quotation2 = 1;
    }
    if(cmdLine[i] == '|' && quotation1 != 1 && quotation2 != 1){
      parser_single(&(cmdLine[i-j]), j, &(command[task]),bg);
      task++;
      j = -1;
    }
  }
  parser_single(&(cmdLine[i-j]), j, &(command[task]),bg);
  RunCmd(command, task+1);
  free(command);
}