Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(command): add input_string #25

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions include/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,4 @@ char **get_complete_command_array(Command *cmd);
*/
void init_command(Command *cmd);

/**
* @brief Get the complete command (command + arguments) as a string
* @param cmd: the command to get
* @return the complete command as a string
*/
char *get_complete_command(const Command *cmd);

#endif // COMMAND_H
1 change: 1 addition & 0 deletions include/typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef struct
char *command;
char **arguments;
char **complete_command;
char *input_string;
int num_arguments;
Redirection redirection;
} Command;
Expand Down
28 changes: 19 additions & 9 deletions src/background_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@
static BackgroundProcess background_processes[MAX_BACKGROUND_PROCESSES];
static size_t num_background_processes = 0;

void check_completed_background_processes() {
void check_completed_background_processes()
{
size_t i = 0;
while (i < num_background_processes) {
while (i < num_background_processes)
{
pid_t pid = background_processes[i].pid;
int status;
int result = waitpid(pid, &status, WNOHANG);
if (result == -1) {
if (result == -1)
{
perror("waitpid");
} else if (result > 0) { // The process with PID 'pid' has completed
}
else if (result > 0)
{ // The process with PID 'pid' has completed
// Display the correct symbol based on the exit status
char symbol = i == num_background_processes-1 ? '+' : i == num_background_processes-2 ? '-' : ' ';
char symbol = i == num_background_processes - 1 ? '+' : i == num_background_processes - 2 ? '-'
: ' ';
printf("[%d]%c Done %s\n", background_processes[i].job_number, symbol, background_processes[i].command);

// Free the memory for the completed process command
free(background_processes[i].command);

// Remove the completed process from the list
for (size_t j = i; j < num_background_processes - 1; ++j) {
for (size_t j = i; j < num_background_processes - 1; ++j)
{
background_processes[j] = background_processes[j + 1];
}
num_background_processes--;
} else {
}
else
{
// Move to the next background process if the current one hasn't completed
i++;
}
}
}

void add_background_process(pid_t pid, const Command *command) {
void add_background_process(pid_t pid, const Command *command)
{
background_processes[num_background_processes].pid = pid;
background_processes[num_background_processes].job_number = num_background_processes + 1;
background_processes[num_background_processes].command = get_complete_command(command);
background_processes[num_background_processes].command = command->input_string;
printf("[%d] %d\n", background_processes[num_background_processes].job_number, pid);
num_background_processes++;
}
30 changes: 3 additions & 27 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Command evaluate_command(const char *input)

// Get the complete command
cmd.complete_command = get_complete_command_array(&cmd);
cmd.input_string = strdup(input);

free(input_copy);
return cmd;
Expand All @@ -60,33 +61,6 @@ void handle_quotes(char **token)
}
}

char *get_complete_command(const Command *cmd)
{
int total_length = strlen(cmd->command) + 1; // Include space for the null terminator

for (int i = 0; i < cmd->num_arguments; ++i)
{
total_length += strlen(cmd->arguments[i]) + 1; // Include space for the null terminator and space between arguments
}

char *complete_command = malloc(total_length);
if (complete_command == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}

strcpy(complete_command, cmd->command);

for (int i = 0; i < cmd->num_arguments; ++i)
{
strcat(complete_command, " "); // Add space between arguments
strcat(complete_command, cmd->arguments[i]);
}

return complete_command;
}

char **get_complete_command_array(Command *cmd)
{
char **new_arguments = malloc((cmd->num_arguments + 2) * sizeof(char *));
Expand All @@ -109,6 +83,7 @@ void init_command(Command *cmd)
cmd->command = NULL;
cmd->arguments = NULL;
cmd->complete_command = NULL;
cmd->input_string = NULL;
cmd->redirection.input_file = NULL;
cmd->redirection.output_file = NULL;
cmd->redirection.append_input = 0;
Expand All @@ -127,4 +102,5 @@ void free_command(Command *cmd)
free(cmd->redirection.input_file);
free(cmd->redirection.output_file);
free(cmd->complete_command);
free(cmd->input_string);
}
Loading