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

Cmd loop had a problem I think

parent 408575e6
No related branches found
No related tags found
No related merge requests found
...@@ -174,91 +174,73 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) { ...@@ -174,91 +174,73 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
return OK; return OK;
} }
int exec_local_cmd_loop() { int execute_pipeline(command_list_t *cmd_list) {
char cmd_buff[SH_CMD_MAX]; int num_commands = cmd_list->num;
int rc = OK; int pipefd[2];
int prev_pipe_read = -1;
while (1) { pid_t pids[num_commands];
printf("%s", SH_PROMPT);
if (fgets(cmd_buff, SH_CMD_MAX, stdin) == NULL) { for (int i = 0; i < num_commands; i++) {
printf("\n"); if (i < num_commands - 1) {
break; if (pipe(pipefd) == -1) {
perror("pipe");
return ERR_MEMORY;
} }
cmd_buff[strcspn(cmd_buff, "\n")] = '\0';
if (strlen(cmd_buff) == 0) {
printf("%s\n", CMD_WARN_NO_CMD);
rc = WARN_NO_CMDS;
continue;
} }
if (strstr(cmd_buff, "|") != NULL) { pids[i] = fork();
command_list_t cmd_list; if (pids[i] == -1) {
if (parse_pipeline(cmd_buff, &cmd_list) != OK) { perror("fork");
fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT); return ERR_MEMORY;
rc = ERR_TOO_MANY_COMMANDS;
continue;
} }
if (execute_pipeline(&cmd_list) != OK) { if (pids[i] == 0) {
fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT); if (prev_pipe_read != -1) {
rc = ERR_MEMORY; dup2(prev_pipe_read, STDIN_FILENO);
close(prev_pipe_read);
} }
for (int i = 0; i < cmd_list.num; i++) { if (i < num_commands - 1) {
free(cmd_list.commands[i]._cmd_buffer); dup2(pipefd[1], STDOUT_FILENO);
} close(pipefd[1]);
} else { close(pipefd[0]);
cmd_buff_t cmd;
if (build_cmd_buff(cmd_buff, &cmd) != OK) {
fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
rc = ERR_MEMORY;
continue;
} }
if (strcmp(cmd.argv[0], EXIT_CMD) == 0) { cmd_buff_t *cmd = &cmd_list->commands[i];
free(cmd._cmd_buffer); execvp(cmd->argv[0], cmd->argv);
break; perror("execvp");
exit(EXIT_FAILURE);
} else {
if (prev_pipe_read != -1) {
close(prev_pipe_read);
} }
if (strcmp(cmd.argv[0], "cd") == 0) { if (i < num_commands - 1) {
if (cmd.argc == 1) { prev_pipe_read = pipefd[0];
chdir(getenv("HOME")); close(pipefd[1]);
} else if (cmd.argc == 2) {
if (chdir(cmd.argv[1]) != 0) {
perror("cd");
} }
} else {
fprintf(stderr, "%s\n", CMD_ERR_PIPE_LIMIT);
} }
free(cmd._cmd_buffer);
continue;
} }
pid_t pid = fork(); if (prev_pipe_read != -1) {
if (pid < 0) { char buffer[1024];
perror("fork failed"); ssize_t bytes_read;
rc = ERR_MEMORY; while ((bytes_read = read(prev_pipe_read, buffer, sizeof(buffer) - 1)) {
} else if (pid == 0) { if (bytes_read < 0) {
execvp(cmd.argv[0], cmd.argv); perror("read");
perror("execvp failed"); break;
exit(EXIT_FAILURE);
} else {
int status;
wait(&status);
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0) {
fprintf(stderr, "Command failed with exit code %d\n", WEXITSTATUS(status));
} }
buffer[bytes_read] = '\0';
printf("%s", buffer);
} }
close(prev_pipe_read);
} }
free(cmd._cmd_buffer);
} for (int i = 0; i < num_commands; i++) {
waitpid(pids[i], NULL, 0);
} }
return rc; return OK;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment