Esempio n. 1
0
cmdLine* cmdEnvReplace(List* env, cmdLine* line) {
    char* value;
    cmdLine* tempLine= line;
    int i;
    for (i= 0; i < tempLine->argCount; i++) {
        if (tempLine->arguments[i][0] == '$') {
            value= valueOfName(env, &(tempLine->arguments[i][1]));
            if (value == (char*)-1) {
                printf("the argument '%s' was not found\n", tempLine->arguments[i]);
                return 0;
            }
            if (replaceCmdArg(tempLine, i, value) == -1) {
                printf("error while replacing argument '%s'\n", tempLine->arguments[i]);
                return 0;
            }
        }
    }
    return tempLine;
}
Esempio n. 2
0
File: task2.c Progetto: liranr23/C
int main (int argc , char* argv[]){
  char dir[PATH_MAX];
  char BUFFER[LINE_LENGTH];
  int commands_counter = 0;
  linked_list *head = 0;
  var_list *var_head = 0;
  cmdLine *command;
  while(1){
    getcwd(dir, PATH_MAX);
    printf("The current dir is: %s \n", dir);
    fgets(BUFFER, LINE_LENGTH, stdin);
    command = parseCmdLines(BUFFER);
    if(strncmp(command->arguments[0], "!", 1) == 0){
      int place = atoi(&command->arguments[0][1]);
      if(place < 0 || place > commands_counter){
	perror("Error, out of bounds");
      }else{
	freeCmdLines(command);
	char *repeat = getCommand(head, place);
	command = parseCmdLines(repeat);
	strcpy(BUFFER, repeat);
      }
    }
    int i;
    for(i = 0; i<command->argCount; i++){
      if(strncmp(command->arguments[i], "$", 1) == 0){
	char *change = &command->arguments[i][1];
	char *fut_change = getArgu(var_head, change);
	replaceCmdArg(command, i, fut_change);
      }
    }
    if(strcmp(command->arguments[0], "set") == 0){
      var_list *newVar = malloc(sizeof(var_list));
      char *str1 = calloc(strlen(command->arguments[1])+1, sizeof(char));
      char *str2 = calloc(strlen(command->arguments[2])+1, sizeof(char));
      strcpy(str1, command->arguments[1]);
      strcpy(str2, command->arguments[2]);
      newVar->name = str1;
      newVar->value = str2;
      newVar->next = 0;
      var_head = setValue(var_head, newVar);
    }
    else if(strcmp(command->arguments[0], "delete") == 0){
      var_head = delete_var(var_head, command->arguments[1]);
    }
    else if((choose_action(command, head, var_head)) == 1){
      freeCmdLines(command);
      break;
    }
    linked_list *newNode = malloc(sizeof(linked_list));
    char *str = calloc(strlen(BUFFER)+1, sizeof(char));
    strcpy(str, BUFFER);
    newNode->command = str;
    newNode->next = 0;
    head = list_append(head, newNode);
    freeCmdLines(command);
    commands_counter++;
  }
  list_free(head);
  var_free(var_head);
  return 0;
}