예제 #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;
}
예제 #2
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
}