Ejemplo n.º 1
0
int execute_builtin(cmd_t* cmd, job_list_t* jobs, history_t* hist) {
  // List jobs.
  if (strcmp(cmd->args[0], "jobs") == 0) {
    add_to_history(hist, cmd);
    return builtin_jobs(jobs);
  }

  // List history.
  if (strcmp(cmd->args[0], "history") == 0) {
    int exit_code = builtin_history(hist);
    add_to_history(hist, cmd);
    return exit_code;
  }

  // Exit.
  if (strcmp(cmd->args[0], "exit") == 0) {
    return builtin_exit();
  }

  // Change directory.
  if (strcmp(cmd->args[0], "cd") == 0) {
    int exit_code = builtin_cd(cmd);
    if (exit_code == 0) {
      add_to_history(hist, cmd);
    }
    return exit_code;
  }

  // Print working directory.
  if (strcmp(cmd->args[0], "pwd") == 0) {
    add_to_history(hist, cmd);
    return builtin_pwd();
  }

  // Bring to foreground.
  if (strcmp(cmd->args[0], "fg") == 0) {
    int exit_code = builtin_fg(cmd, jobs);
    if (exit_code == 0) {
      add_to_history(hist, cmd);
    }
    return exit_code;
  }

  // Execute from history.
  char* endptr = NULL;
  long num = strtol(cmd->args[0], &endptr, 10);
  if (*endptr == '\0' && !(num == 0 && errno == EINVAL)) {
    // Do not save to history to avoid confusion.
    int exit_code = builtin_exec_from_history(cmd, jobs, hist);
    freecmd(cmd);
    return exit_code;
  }

  // Command not found.
  return COMMAND_NOT_FOUND;
}
Ejemplo n.º 2
0
Archivo: dsh.c Proyecto: bsb20/dsh
void execute_job(job_t* job) {
	/*Check commandline for built-in commands*/
    if (check_command(job, "cd")) {         //change directory - alter current directory and remove completed job
        chdir(job->first_process->argv[1]);
        remove_job(job);
    }
    else if (check_command(job, "jobs")) {  //display current jobs and their statuses (stati?)
        remove_job(job);
        builtin_jobs();
    }
    else if (check_command(job, "bg")) {    //resume background job with specified number 
        resume_background_job(atoi(job->first_process->argv[1])); //atoi interprets string as integer value
        remove_job(job);
    }
    else if (check_command(job, "fg")) {    //continue foreground job with specified number 
        resume_foreground_job(atoi(job->first_process->argv[1]));
        remove_job(job);
    } 
    else {                                  //Default case, not built in command - spawn the new job
		spawn_job(job, !job->bg);
    }
}
Ejemplo n.º 3
0
// si le premier parametre est une commande integree,
// l'executer et renvoyer "vrai"
bool builtin_command(char **argv) {
    if (argv[0] == NULL)       // commande vide
        return true;
    if (strcmp(argv[0], "&") == 0) // ignorer & tout seul
        return true;

    builtin("exit", exit_try());
    builtin("quit", exit_try());

    builtin("jobs", builtin_jobs());
    builtin("fg",   builtin_fg(argv));
    builtin("bg",   builtin_bg(argv));
    builtin("int",  builtin_int(argv));
    builtin("term", builtin_term(argv));
    builtin("stop", builtin_stop(argv));
    builtin("wait", builtin_wait());

    builtin("dirs", dirs_print());
    builtin("cd", dirs_cd(argv[1]));
    builtin("pushd", dirs_pushd(argv[1]));
    builtin("popd", dirs_popd());

    return false; // ce n'est pas une commande integree
}