Skip to content
Snippets Groups Projects
Commit 124bd733 authored by Joey Le's avatar Joey Le
Browse files

Fixed it now

parent 966b801e
Branches
No related tags found
No related merge requests found
......@@ -10,7 +10,6 @@
#include "dshlib.h"
// Function to build a command buffer from a command line
int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
if (cmd_line == NULL || cmd_buff == NULL) {
return ERR_CMD_OR_ARGS_TOO_BIG;
......@@ -26,11 +25,11 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
char *ptr = original_line;
char *arg_start = NULL;
bool in_quotes = false; // For quotes
bool in_quotes = false;//For quotes
char quote_char = '\0';
while (*ptr != '\0') {
if ((isspace((unsigned char)*ptr) && !in_quotes) {
if ((isspace((unsigned char)*ptr) && !in_quotes)) {
if (arg_start != NULL) {
*ptr = '\0';
cmd_buff->argv[cmd_buff->argc++] = arg_start;
......@@ -60,11 +59,18 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
cmd_buff->argv[cmd_buff->argc] = NULL;
// Debugging output to verify parsing
// printf("Parsed command:\n");
// for (int i = 0; i < cmd_buff->argc; i++) {
// printf(" argv[%d]: %s\n", i, cmd_buff->argv[i]);
// }
return OK;
}
// Function to trim whitespace from a string
char *trim_whitespace(char *str) {
char *trim_whitespace(char *str) { //Had to make a new function for readability
char *end;
while (isspace((unsigned char)*str)) str++;
......@@ -76,13 +82,15 @@ char *trim_whitespace(char *str) {
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
// New null terminator
// new null terminator
*(end + 1) = '\0';
return str;
}
// Function to parse a pipeline of commands
int parse_pipeline(const char *cmd_line, command_list_t *clist) {
if (cmd_line == NULL || clist == NULL) {
return ERR_CMD_OR_ARGS_TOO_BIG;
......@@ -92,8 +100,7 @@ int parse_pipeline(const char *cmd_line, command_list_t *clist) {
if (line_copy == NULL) {
return ERR_MEMORY;
}
// Parsing using pipe
//Parsing using pipe.
clist->num = 0;
char *saveptr;
char *command = strtok_r(line_copy, "|", &saveptr);
......@@ -119,11 +126,22 @@ int parse_pipeline(const char *cmd_line, command_list_t *clist) {
return OK;
}
// Function to execute a pipeline of commands
int execute_pipeline(command_list_t *clist) {
int num_commands = clist->num;
int pipefd[2 * (num_commands - 1)];
pid_t pids[num_commands];
// for (int i = 0; i < clist->num; i++) {
// printf("Command %d:\n", i);
// for (int j = 0; clist->commands[i].argv[j] != NULL; j++) {
// printf(" argv[%d]: %s\n", j, clist->commands[i].argv[j]);
// }
// }
for (int i = 0; i < num_commands - 1; i++) {
if (pipe(pipefd + 2 * i) == -1) {
......@@ -141,7 +159,7 @@ int execute_pipeline(command_list_t *clist) {
}
if (pids[i] == 0) { // Child process
// Redirect input if not the first command
// if not first moves input
if (i > 0) {
if (dup2(pipefd[2 * (i - 1)], STDIN_FILENO) == -1) {
perror("dup2 stdin");
......@@ -157,7 +175,7 @@ int execute_pipeline(command_list_t *clist) {
}
}
// Close all pipe file descriptors
// Close all pipe file descriptors. Very important stuff
for (int j = 0; j < 2 * (num_commands - 1); j++) {
close(pipefd[j]);
}
......@@ -169,12 +187,12 @@ int execute_pipeline(command_list_t *clist) {
}
}
// Close all pipe file descriptors in the parent
// Closes all pipe
for (int i = 0; i < 2 * (num_commands - 1); i++) {
close(pipefd[i]);
}
// Wait for all child processes to finish
// waitpid so everyhting is smooth
for (int i = 0; i < num_commands; i++) {
int status;
waitpid(pids[i], &status, 0);
......@@ -188,7 +206,8 @@ int execute_pipeline(command_list_t *clist) {
return OK;
}
// Main loop for executing local commands
int exec_local_cmd_loop() {
char cmd_buff[SH_CMD_MAX];
int rc = OK;
......@@ -217,7 +236,9 @@ int exec_local_cmd_loop() {
continue;
}
rc = execute_pipeline(&cmd_list);
// Execute the pipeline
execute_pipeline(&cmd_list);
// Free memory for each command's buffer
for (int i = 0; i < cmd_list.num; i++) {
......@@ -274,4 +295,7 @@ int exec_local_cmd_loop() {
}
return rc;
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment