Skip to content

Commit

Permalink
feat: project completed
Browse files Browse the repository at this point in the history
  • Loading branch information
mathusanm6 committed Feb 21, 2024
1 parent b9a1d30 commit e0adc73
Show file tree
Hide file tree
Showing 34 changed files with 3,393 additions and 753 deletions.
31 changes: 31 additions & 0 deletions src/builtins/bg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "bg.h"
#include "../utils/core.h"
#include "../utils/jobs_core.h"
#include "../utils/string_utils.h"
#include <signal.h>
#include <unistd.h>

int bg(const command_without_substitution *cmd) {
if (cmd->argc != 2) {
print_error("bg: incorrect number of arguments\n");
return COMMAND_FAILURE;
}
if (strlen(cmd->argv[1]) < 2 || cmd->argv[1][0] != '%') {
print_error("bg: invalid job reference\n");
return COMMAND_FAILURE;
}
if (!is_integer(cmd->argv[1] + 1)) {
print_error("bg: invalid job reference");
return COMMAND_FAILURE;
}
unsigned job_number = atoi(cmd->argv[1] + 1);

int job_placement = get_jobs_placement_with_id(job_number);
if (job_placement == -1) {
print_error("bg: %: invalid job id");
return COMMAND_FAILURE;
}
pid_t pgid = jobs[job_placement]->pgid;
killpg(pgid, SIGCONT);
return SUCCESS;
}
9 changes: 9 additions & 0 deletions src/builtins/bg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef BG_H
#define BG_H

#include "../parser/parser.h"

int bg(const command_without_substitution *);
/* Relaunches the job specified in the command argument in the background */

#endif
4 changes: 3 additions & 1 deletion src/builtins/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
#include "print_last_command_result.h"
#include "extern_command.h"
#include "kill.h"
#include "bg.h"
#include "fg.h"

#endif
#endif
2 changes: 1 addition & 1 deletion src/builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ char *get_correct_path(const char *);
/* Returns the string, allocated and if the
* path starts with ~, replaces it with $HOME */

int cd(const command *cmd) {
int cd(const command_without_substitution *cmd) {
if (cmd->argc > 2) { // Checks if its the good number of arguments
print_error("cd: too many arguments");
return COMMAND_FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/cd.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../parser/parser.h"

int cd(const command *);
int cd(const command_without_substitution *);
/* Changes the current working directory to the "ref" directory
* if it is is valid, the previous working directory if the parameter
* is -, or $HOME if no parameter is specified.a
Expand Down
3 changes: 2 additions & 1 deletion src/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "exit.h"

int exit_jsh(const command *cmd) {
int exit_jsh(command_without_substitution *cmd) {
if (cmd->argc > 2) {
print_error("exit: too many arguments");
return COMMAND_FAILURE;
Expand All @@ -27,6 +27,7 @@ int exit_jsh(const command *cmd) {
return COMMAND_FAILURE;
}
}
free_command_without_substitution(cmd);
free_core();
exit(exit_value);
}
2 changes: 1 addition & 1 deletion src/builtins/exit.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../parser/parser.h"

int exit_jsh(const command *cmd);
int exit_jsh(command_without_substitution *cmd);
/* Exit the jsh program with the specified value
* If no value is specified, exit the program with
* the value of the last executed command*/
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/extern_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "../utils/core.h"
#include "extern_command.h"

int extern_command(const command *cmd) {
int extern_command(const command_without_substitution *cmd) {
int res_exec;

res_exec = execvp(cmd->name, cmd->argv);
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/extern_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../parser/parser.h"

int extern_command(const command *);
int extern_command(const command_without_substitution *);
/* Executes an external command, with or without
* arguments, taking into account the PATH environment variable */

Expand Down
50 changes: 50 additions & 0 deletions src/builtins/fg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "fg.h"
#include "../utils/core.h"
#include "../utils/jobs_core.h"
#include "../utils/string_utils.h"
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wait.h>

int fg(const command_without_substitution *cmd) {
if (cmd->argc != 2) {
print_error("fg: incorrect number of arguments\n");
return COMMAND_FAILURE;
}
if (strlen(cmd->argv[1]) < 2 || cmd->argv[1][0] != '%') {
print_error("fg: invalid job reference\n");
return COMMAND_FAILURE;
}
if (!is_integer(cmd->argv[1] + 1)) {
print_error("fg: invalid job reference");
return COMMAND_FAILURE;
}
unsigned job_number = atoi(cmd->argv[1] + 1);

int job_placement = get_jobs_placement_with_id(job_number);
if (job_placement == -1) {
print_error("fg: %: invalid job id");
return COMMAND_FAILURE;
}

int status;
job *j = jobs[job_placement];
pid_t pgid = j->pgid;
tcsetpgrp(STDERR_FILENO, pgid);
killpg(pgid, SIGCONT);

waitpid(j->job_process[j->process_number - 1]->pid, &status, WUNTRACED);

if (WIFSTOPPED(status)) {
j->status = STOPPED;
j->job_process[j->process_number - 1]->status = STOPPED;
print_job(j, false);
} else {
remove_job_from_jobs(j->id);
}
tcsetpgrp(STDERR_FILENO, getpgrp());

return SUCCESS;
}
9 changes: 9 additions & 0 deletions src/builtins/fg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef FG_H
#define FG_H

#include "../parser/parser.h"

int fg(const command_without_substitution *);
/* Relaunches the job specified in the command argument in the foreground */

#endif
42 changes: 38 additions & 4 deletions src/builtins/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,37 @@

#include "../utils/core.h"
#include "../utils/int_utils.h"
#include "../utils/string_utils.h"

int print_jobs(const command *cmd) {
int print_given_jobs_from_argument_index(const command_without_substitution *cmd, size_t start_index) {
update_status_of_jobs();
for (size_t i = start_index; i < cmd->argc; ++i) {
if (cmd->argv[i][0] == '%') {
if (!is_integer(cmd->argv[i] + 1)) {
print_error("jobs: %: invalid job id");
return COMMAND_FAILURE;
}
unsigned job_number = atoi(cmd->argv[i] + 1);

int job_placement = get_jobs_placement_with_id(job_number);
if (job_placement == -1) {
print_error("jobs: %: invalid job id");
return COMMAND_FAILURE;
}

char *strjb = simple_str_of_job(jobs[job_placement], false);
printf("%s\n", strjb);
free(strjb);
} else {
print_error("jobs: invalid argument");
return COMMAND_FAILURE;
}
}
remove_terminated_jobs(false);
return SUCCESS;
}

int print_jobs(const command_without_substitution *cmd) {
if (cmd->argc == 1) {
update_status_of_jobs();
for (size_t i = 0; i < job_number; ++i) {
Expand All @@ -14,8 +43,13 @@ int print_jobs(const command *cmd) {
remove_terminated_jobs(false);
return SUCCESS;
} else {
// TODO : For next milestone, take into account the -t option and the %jobs argument
print_error("jobs: too many arguments");
return COMMAND_FAILURE;

if (cmd->argv[1][0] == '-') {
// TODO : take into account the -t option
print_error("jobs: option -t not yet implemented");
return COMMAND_FAILURE;
} else {
return print_given_jobs_from_argument_index(cmd, 1);
}
}
}
2 changes: 1 addition & 1 deletion src/builtins/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../utils/core.h"

int print_jobs(const command *);
int print_jobs(const command_without_substitution *);
/**
* Prints the current jobs
*/
Expand Down
6 changes: 3 additions & 3 deletions src/builtins/kill.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
int kill_job(int job_id, int signal) {
for (size_t i = 0; i < job_number; ++i) {
if ((*(jobs + i))->id == job_id) {
if (killpg(getpgid((*(jobs + i))->pid), signal) == -1) {
if (killpg((*(jobs + i))->pgid, signal) == -1) {
print_error("kill: an error occured");
return COMMAND_FAILURE;
}
Expand All @@ -21,7 +21,7 @@ int kill_job(int job_id, int signal) {
return COMMAND_FAILURE;
}

int jsh_kill(const command *cmd) {
int jsh_kill(const command_without_substitution *cmd) {
if (cmd->argc < 2) {
print_error("kill: not enough arguments");
return COMMAND_FAILURE;
Expand Down Expand Up @@ -63,4 +63,4 @@ int jsh_kill(const command *cmd) {
}

return SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/builtins/kill.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../parser/parser.h"

int jsh_kill(const command *cmd);
int jsh_kill(const command_without_substitution *cmd);
/**
* Envoie un signal à un job ou processus
*/
Expand Down
4 changes: 2 additions & 2 deletions src/builtins/print_last_command_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "../parser/parser.h"
#include "../utils/core.h"

int print_last_command_result(const command *cmd) {
int print_last_command_result(const command_without_substitution *cmd) {
if (cmd->argc != 1) {
print_error("?: too many arguments");
return COMMAND_FAILURE;
}
printf("%d\n", last_command_exit_value);
return SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/builtins/print_last_command_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../parser/parser.h"

int print_last_command_result(const command *);
int print_last_command_result(const command_without_substitution *);
/* Display the return value of the last entered command*/

#endif
3 changes: 2 additions & 1 deletion src/builtins/pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#include "../utils/core.h"
#include "pwd.h"

int pwd(const command *cmd) {
int pwd(const command_without_substitution *cmd) {
if (cmd->argc != 1) {
print_error("pwd: too many arguments\n");
return COMMAND_FAILURE;
}
printf("%s\n", current_folder);

return SUCCESS;
}
2 changes: 1 addition & 1 deletion src/builtins/pwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../parser/parser.h"

int pwd(const command *cmd);
int pwd(const command_without_substitution *cmd);
/* Displays the absolute physical reference of the current
* directory. */

Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include "utils/constants.h"
#include "utils/core.h"
#include "utils/jobs_core.h"
#include "utils/signal_management.h"

int main() {
init_core();
init_const();
use_jsh_signal_management();

rl_outstream = stderr;
while (1) {
Expand All @@ -25,7 +27,6 @@ int main() {
current_pipeline_list = parse_pipeline_list(last_line_read);

if (current_pipeline_list == NULL) {
print_error("jsh: parse error");
last_command_exit_value = COMMAND_FAILURE;
continue;
}
Expand Down
Loading

0 comments on commit e0adc73

Please sign in to comment.