diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-03-03 00:17:36 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-03-03 00:17:36 -0600 |
| commit | f7af740cf2bccde2e6edbac31a0182fafe05d25e (patch) | |
| tree | e110bdaee77f97d6dd4931e8daa28b77e19e1f63 /src/lsh_main_func.c | |
| parent | 1b8967ea87c63342f03c340d44a084ea7f62cae9 (diff) | |
pipes
Diffstat (limited to 'src/lsh_main_func.c')
| -rw-r--r-- | src/lsh_main_func.c | 71 |
1 files changed, 56 insertions, 15 deletions
diff --git a/src/lsh_main_func.c b/src/lsh_main_func.c index 1604073..ff05a2b 100644 --- a/src/lsh_main_func.c +++ b/src/lsh_main_func.c @@ -3,7 +3,6 @@ #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <sys/wait.h> #include <unistd.h> #include <stdbool.h> @@ -44,7 +43,24 @@ char *lsh_read_line(void) } } -char * strtok_cmd(char *token, cmd_t *type){ +int cleanup_token(char **parse_args_last_p, int ch_i){ + (*parse_args_last_p)[ch_i]='\0'; + if((*parse_args_last_p)[ch_i+1]=='\0') + return -1; + + //remove spaces and tabs around sep char + int i=ch_i-1; + while ((*parse_args_last_p)[i]==' '|| (*parse_args_last_p)[0]=='\t'){ + (*parse_args_last_p)[i--]='\0'; + } + + *parse_args_last_p=&(*parse_args_last_p)[ch_i+1]; + while((*parse_args_last_p)[0]==' ' || (*parse_args_last_p)[0]=='\t') + *parse_args_last_p=&(*parse_args_last_p)[1]; + return 0; +} + +char *strtok_cmd(char *token, cmd_t *type){ *type=Stdout; static char* parse_args_last_p; if(token != NULL){ @@ -85,16 +101,18 @@ char * strtok_cmd(char *token, cmd_t *type){ if (pass_check) in_quote=' '; } - } else if(parse_args_last_p[ch_i]==';'){ - if(parse_args_last_p[ch_i]=='\0') - break; - parse_args_last_p[ch_i]='\0'; - if(parse_args_last_p[ch_i+1]=='\0') - break; - parse_args_last_p=&parse_args_last_p[ch_i+1]; - while(parse_args_last_p[0]==' ' || parse_args_last_p[0]=='\t') - parse_args_last_p=&parse_args_last_p[1]; - return return_char; + } else{ + switch (parse_args_last_p[ch_i]){ + case ';': + if(cleanup_token(&parse_args_last_p, ch_i)!=0) break; + return return_char; + break; + case '|': + if(cleanup_token(&parse_args_last_p, ch_i)!=0) break; + *type=Pipe; + return return_char; + break; + } } ch_i++; @@ -116,6 +134,10 @@ CMD **lsh_split_command(char *line) tokens[position] = malloce(sizeof(CMD)); tokens[position]->cmd = token; tokens[position]->type = cmd_type; + tokens[position]->stdInFd = 0; + tokens[position]->next = NULL; + if(position>0) tokens[position-1]->next = tokens[position]; + position++; if (position * bufsize >= bufsize) { @@ -133,17 +155,33 @@ CMD **lsh_split_command(char *line) return tokens; } -int lsh_launch(char **args) +int lsh_launch(char **args, CMD *cmd) { pid_t pid, wpid; int status; + int stdOutPipe[2]; + pipe(stdOutPipe); + if(cmd->type==Pipe){ + cmd->next->stdInFd=stdOutPipe[0]; + } pid = fork(); if (pid == 0) { // Child process - if (execvp(args[0], args) == -1) { - perror("lsh"); + if(cmd->type!=Stdout){ + close(stdOutPipe[0]); // close reading end in the child + + dup2(stdOutPipe[1], 1); // send stdout to the pipe + dup2(stdOutPipe[1], 2); // send stderr to the pipe + + close(stdOutPipe[1]); // this descriptor is no longer needed } + + if(cmd->stdInFd!=0) + dup2(cmd->stdInFd,STDIN_FILENO); + + if (execvp(args[0], args) == -1) perror("lsh"); + exit(EXIT_FAILURE); } else if (pid < 0) { // Error forking @@ -153,6 +191,9 @@ int lsh_launch(char **args) do { wpid = waitpid(pid, &status, WUNTRACED); } while (!WIFEXITED(status) && !WIFSIGNALED(status)); + + close(stdOutPipe[1]); // close the write end of the pipe in the parent + if(cmd->stdInFd!=0) close(cmd->stdInFd); } return 1; |
