From: Christopher Uhlig Date: Mon, 20 Jan 2025 12:06:34 +0000 (+0100) Subject: merged X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=8f5abcdb257393a2e576fe382835e8e060662834;p=42%2Fminishell.git merged --- diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 94b2ae4..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "macos-clang-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "/usr/bin/clang", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "macos-clang-x64", - "compilerArgs": [ - "" - ] - } - ], - "version": 4 - } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 86fa44a..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "lldb", - "request": "launch", - "args": [], - "cwd": "/Users/chuhlig/Desktop/merged_minishell/", - "program": "/Users/chuhlig/Desktop/merged_minishell/minishell" - } - ] - } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 91fa952..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "clang", - "C_Cpp_Runner.cppCompilerPath": "clang++", - "C_Cpp_Runner.debuggerPath": "lldb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false, - "files.associations": { - "minishell.h": "c", - "ast.h": "c", - "env.h": "c" - } - } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 4ede37a..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: clang build active file", - "command": "/usr/bin/clang", - "args": [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file diff --git a/Makefile b/Makefile index 243e9fc..9cfa670 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ - read_heredoc.c create_files.c + read_heredoc.c create_files.c builtins_part_three.c handle_redir.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) @@ -64,7 +64,9 @@ debug: CFLAGS += -g debug: CFLAGS += -fsanitize=address -fsanitize=undefined \ -fno-sanitize-recover=all -fsanitize=float-divide-by-zero \ -fsanitize=float-cast-overflow -fno-sanitize=null \ - -fno-sanitize=alignment + -fno-sanitize=alignment -fno-sanitize=object-size \ + -fno-sanitize=vptr -fno-sanitize=return -fno-sanitize=signed-integer-overflow \ + -fno-sanitize=bounds -fno-sanitize=pointer-subtract -fno-sanitize=pointer-compare debug: CFLAGS += -DDEBUG=1 debug: clean all diff --git a/garbage b/garbage deleted file mode 100644 index 07f44dc..0000000 --- a/garbage +++ /dev/null @@ -1,1183 +0,0 @@ -_=/Users/chuhlig/Desktop/merged_minishell/minishell/./minishell -TERM=xterm-256color -USER_ZDOTDIR=/Users/chuhlig -ZDOTDIR=/Users/chuhlig -VSCODE_INJECTION=1 -VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-e47956ed09.sock -VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js -VSCODE_GIT_ASKPASS_EXTRA_ARGS= -VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) -GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh -COLORTERM=truecolor -LANG=en_US.UTF-8 -TERM_PROGRAM_VERSION=1.95.3 -TERM_PROGRAM=vscode -OLDPWD=/Users/chuhlig/Desktop/merged_minishell -PWD=/Users/chuhlig/Desktop/merged_minishell/minishell -SHLVL=1 -ORIGINAL_XDG_CURRENT_DESKTOP=undefined -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -__CF_USER_TEXT_ENCODING=0x1928E:0x0:0x2 -SHELL=/bin/zsh -HOME=/Users/chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ECcBYJZkxn/Listeners -LOGNAME=chuhlig -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin -MallocNanoZone=0 -USER=chuhlig -_=/Users/chuhlig/Desktop/merged_minishell/minishell/./minishell -TERM=xterm-256color -USER_ZDOTDIR=/Users/chuhlig -ZDOTDIR=/Users/chuhlig -VSCODE_INJECTION=1 -VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-e47956ed09.sock -VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js -VSCODE_GIT_ASKPASS_EXTRA_ARGS= -VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) -GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh -COLORTERM=truecolor -LANG=en_US.UTF-8 -TERM_PROGRAM_VERSION=1.95.3 -TERM_PROGRAM=vscode -OLDPWD=/Users/chuhlig/Desktop/merged_minishell -PWD=/Users/chuhlig/Desktop/merged_minishell/minishell -SHLVL=1 -ORIGINAL_XDG_CURRENT_DESKTOP=undefined -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -__CF_USER_TEXT_ENCODING=0x1928E:0x0:0x2 -SHELL=/bin/zsh -HOME=/Users/chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ECcBYJZkxn/Listeners -LOGNAME=chuhlig -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin -MallocNanoZone=0 -USER=chuhlig - - -//some test case about built in command - bash-3.2$ ls -Applications Music -Desktop Pictures -Documents francinette -Downloads getting-started -Library goinfre -Movies setup_docker_environment -bash-3.2$ pwd -/Users/chuhlig -bash-3.2$ ls | cd / | ls -Applications Music -Desktop Pictures -Documents francinette -Downloads getting-started -Library goinfre -Movies setup_docker_environment -bash-3.2$ pwd -/Users/chuhlig -bash-3.2$ env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ export test=test | unset USER -bash-3.2$ env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ env | grep test -bash-3.2$ ls | cd / | env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ ls | cd / | env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ ls | cd / | env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ ls | cd / | env | cd / -bash-3.2$ pwd -/Users/chuhlig -bash-3.2$ ls | cd / | env | cd ~ -bash-3.2$ ls | cd / | env | cd - -bash: cd: OLDPWD not set -bash-3.2$ ls | cd - | env -bash: cd: OLDPWD not set -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ ls | cd - | env -bash: cd: OLDPWD not set -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ - - -////////////////////////////////////////////////////////////////// - -static int handle_redirections(t_redirection *redirs) { - if (redirs[0].type == INPUT_LIMITER) { - int pipe_fds[2]; - if (pipe(pipe_fds) == -1) { - perror("pipe"); - return -1; - } - write(pipe_fds[1], redirs[0].specifier, strlen(redirs[0].specifier)); - close(pipe_fds[1]); - dup2(pipe_fds[0], STDIN_FILENO); - close(pipe_fds[0]); - } else if (redirs[0].type == INPUT_FILE) { - int fd = open(redirs[0].specifier, O_RDONLY); - if (fd == -1) { - perror(redirs[0].specifier); - return -1; - } - dup2(fd, STDIN_FILENO); - close(fd); - } - - if (redirs[1].type == OUTPUT_OVERRIDE) { - int fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd == -1) { - perror(redirs[1].specifier); - return -1; - } - dup2(fd, STDOUT_FILENO); - close(fd); - } else if (redirs[1].type == OUTPUT_APPEND) { - int fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); - if (fd == -1) { - perror(redirs[1].specifier); - return -1; - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - return 0; -} - -static int eval_rec(t_node *node, t_env **env, int in_fd) { - pid_t pid; - int p[2], result; - - if (node->type == PIPE_NODE) { - pipe(p); - pid = fork(); - if (pid == 0) { - close(p[0]); - dup2(in_fd, STDIN_FILENO); - dup2(p[1], STDOUT_FILENO); - result = eval_rec(node->content.pipe.left, env, in_fd); - exit(result); - } else { - close(p[1]); - dup2(p[0], STDIN_FILENO); - result = eval_rec(node->content.pipe.right, env, p[0]); - } - } else if (node->type == CMD_NODE) { - result = execute_cmd(&node->content.cmd, env); - } else { - panic("UNREACHABLE"); - result = EXIT_FAILURE; - } - return result; -} - -int eval(t_node *node, t_env **env) { - return eval_rec(node, env, STDIN_FILENO); -} - -int execute_cmd(t_cmd *cmd, t_env **env) { - if (handle_redirections(cmd->redirs) == -1) { - return EXIT_FAILURE; - } - - if (is_builtin(cmd->args[0])) { - return execute_builtin(cmd->args, env); - } else { - pid_t pid = fork(); - if (pid == 0) { - char *cmd_path = get_cmd_path(cmd->args[0], *env); - if (!cmd_path) { - fprintf(stderr, "%s: command not found\n", cmd->args[0]); - exit(EXIT_FAILURE); - } - execve(cmd_path, cmd->args, env_to_strlst(*env)); - perror("execve"); - exit(EXIT_FAILURE); - } else { - int status; - waitpid(pid, &status, 0); - return WEXITSTATUS(status); - } - } -} -have to update env list/tokenlist order or update the existing code to handle the order of the tokenlist/env list - -echo "Hello" | grep H this works - -also more to do on heredoc - -and for the for part of domenic removing the " ' frome the string -beside - - - -t_redirection *collect_redirs(t_token **tokens) -{ - t_redirection *result; - t_token *cur; - - cur = *tokens; - result = malloc(sizeof(t_redirection) * 2); - if (result == NULL) - return (free_tokens(*tokens), NULL); - set_redir(&result[0], 0, NULL); - set_redir(&result[1], 0, NULL); - while (cur != NULL && cur->next != NULL) - { - if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur); - else if (cur->type == REDIR_TOKEN) - return (free(result), NULL); - else - cur = cur->next; - } - if (cur && cur->type == REDIR_TOKEN) - return (free(result), NULL); - return (result); -} - -static void collect_and_check_redir(t_redirection *result, t_token **cur) -{ - char *heredoc_data; - t_token *next_token; - - heredoc_data = NULL; - if ((*cur)->content.redir_type == INPUT_LIMITER) - { - // Handle Here Document (<<) - heredoc_data = read_heredoc((*cur)->next->content.string); - if (!heredoc_data) - { - perror("Heredoc allocation failed"); - return ; - } - set_redir(&result[0], INPUT_LIMITER, heredoc_data); - } - else if ((*cur)->content.redir_type == INPUT_FILE) - { - // Handle Input File (<) - set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string)); - } - else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - { - // Handle Output File Overwrite (>) - set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string)); - } - else if ((*cur)->content.redir_type == OUTPUT_APPEND) - { - // Handle Output File Append (>>) - set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string)); - } - else - { - // Handle unexpected cases - printf("Unknown redirection type encountered\n"); - } - // Advance the token pointer to skip the redirection token and its argument - next_token = (*cur)->next; - free_token_and_connect(*cur); // Free the current redirection token - if (next_token) - { - *cur = next_token->next; // Move to the next token after the argument - } - else - { - *cur = NULL; // No more tokens - } -} - -collect args apassen x -alsso fur arg x -muss in rpl parse x -und in parse cmd collect args and x - -format args seems to work later test x - - -Test 1: ✅ echo hello world -Test 2: ✅ echo "hello world" -Test 3: ✅ echo 'hello world' -Test 4: ✅ echo hello'world' -Test 5: ✅ echo hello""world -Test 6: ✅ echo '' -Test 7: ✅ echo "$PWD" -Test 8: ✅ echo '$PWD' - -format_string problem -Test 9: ❌ echo "aspas ->'" -mini output = (aspas ->") -bash output = (aspas ->') -Test 10: ❌ echo "aspas -> ' " -mini output = (aspas -> ") -bash output = (aspas -> ' ) -Test 11: ✅ echo 'aspas ->"' -Test 12: ✅ echo 'aspas -> " ' -Test 13: ❌ echo "> >> < * ? [ ] | ; [ ] || && ( ) & # $ <<" -mini output = (> >> < README.md bash.supp bash_outfiles bonus bonus_bonus builtins extras local.supp loop.out manual_tests mini_outfiles os_specific outfiles pipes redirects syntax test_files tester wildcards ? [ ] | ; [ ] || && ( ) & # ) -bash output = (> >> < README.md bash.supp bash_outfiles bonus bonus_bonus builtins extras local.supp loop.out manual_tests mini_outfiles os_specific outfiles pipes redirects syntax test_files tester wildcards ? [ ] | ; [ ] || && ( ) & # $ <<) -Test 14: ✅ echo '> >> < * ? [ ] | ; [ ] || && ( ) & # $ <<' -Test 15: ❌ echo "exit_code ->$? user ->$USER home -> $HOME" -mini output = (exit_code ->/Users/chuhlig) -bash output = (exit_code ->0 user ->chuhlig home -> /Users/chuhlig) -Test 16: ✅ echo 'exit_code ->$? user ->$USER home -> $HOME' -Test 17: ❌ echo "$" -mini output = (0) -bash output = ($) -Test 18: ✅ echo '$' -Test 19: ❌ echo $ -mini output = (0) -bash output = ($) -Test 20: ✅ echo $? -Test 21: ❌ echo $?HELLO -mini output = () -bash output = (0HELLO) -Test 22: ✅ pwd -Test 23: ✅ pwd oi - -Test 24: ❌ export hello //stupid -mini exit code = 1 -bash exit code = 0 -Test 25: ✅ export HELLO=123 -Test 26: ✅⚠️ export A- -mini error = () -bash error = ( not a valid identifier) - fucking extra function for identifier - -Test 27: ❌ export HELLO=123 A -mini exit code = 1 -bash exit code = 0 -auch dum -Test 28: ✅ export HELLO="123 A-" -Test 29: ❌ export hello world -mini exit code = 1 -bash exit code = 0 -Test 30: ❌ export HELLO-=123 -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( not a valid identifier) -Test 31: ❌ export = -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( not a valid identifier) -Test 32: ✅⚠️ export 123 -mini error = () -bash error = ( not a valid identifier) -Test 33: ✅ unset -Test 34: ✅ unset HELLO -Test 35: ✅ unset HELLO1 HELLO2 -Test 36: ✅ unset HOME -Test 37: ✅ unset PATH -Test 38: ✅ unset SHELL - - -Test 39: ❌ cd $PWD -mini exit code = 139 -bash exit code = 0 -Test 40: ❌ cd $PWD hi -mini exit code = 139 -bash exit code = 0 -Test 41: ❌ cd 123123 -mini exit code = 139 -bash exit code = 1 -mini error = () -bash error = ( No such file or directory) - -Test 42: ✅ exit 123 -Test 43: ✅ exit 298 -Test 44: ✅ exit +100 -Test 45: ✅ exit "+100" -Test 46: ✅ exit +"100" -Test 47: ✅ exit -100 -Test 48: ✅ exit "-100" -Test 49: ✅ exit -"100" - -needs a update -Test 50: ❌ exit hello -mini exit code = 0 -bash exit code = 255 -mini error = () -bash error = ( numeric argument required) - -edge case -Test 51: ❌ exit 42 world -mini exit code = 42 -bash exit code = 1 -mini error = () -bash error = ( too many arguments) -Test 52: ✅ -———————————— pipes -Test 53: ❌ env | sort | grep -v SHLVL | grep -v ^_ -mini output = (?=0 COLORTERM=truecolor GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh HOME=/Users/chuhlig LANG=en_US.UTF-8 LOGNAME=chuhlig MallocNanoZone=0 ORIGINAL_XDG_CURRENT_DESKTOP=undefined PATH=/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/Library/Application Support/Code/User/globalStorage/github.copilot-chat/debugCommand PWD=/Users/chuhlig/Desktop/minishell/minishell_tester SHELL=/bin/zsh SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.8vcs8lnaog/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.96.2 TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ USER=chuhlig USER_ZDOTDIR=/Users/chuhlig VSCODE_GIT_ASKPASS_EXTRA_ARGS= VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-a7f217e79d.sock VSCODE_INJECTION=1 XPC_FLAGS=0x0 XPC_SERVICE_NAME=0 ZDOTDIR=/Users/chuhlig) -bash output = (COLORTERM=truecolor GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh HOME=/Users/chuhlig LANG=en_US.UTF-8 LOGNAME=chuhlig MallocNanoZone=0 ORIGINAL_XDG_CURRENT_DESKTOP=undefined PATH=/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/Library/Application Support/Code/User/globalStorage/github.copilot-chat/debugCommand PWD=/Users/chuhlig/Desktop/minishell/minishell_tester SHELL=/bin/zsh SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.8vcs8lnaog/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.96.2 TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ USER=chuhlig USER_ZDOTDIR=/Users/chuhlig VSCODE_GIT_ASKPASS_EXTRA_ARGS= VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-a7f217e79d.sock VSCODE_INJECTION=1 XPC_FLAGS=0x0 XPC_SERVICE_NAME=0 ZDOTDIR=/Users/chuhlig) -Test 54: ✅ cat ./test_files/infile_big | grep oi -Test 55: ✅ cat minishell.h | grep ");"$ -Test 56: ✅ export GHOST=123 | env | grep GHOST -———————————— redirects -Test 57: ✅ grep hi <./test_files/infile -Test 58: ✅ grep hi "./outfiles/outfile01 -Test 82: ✅ ls > ./outfiles/outfile01 -Test 83: ✅ echo hi > ./outfiles/outfile01 bye - - -check this extra - -Test 84: ❌ ls >./outfiles/outfile01 >./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -Test 85: ❌ ls >./outfiles/outfile01 >./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 86: ❌ ls >"./outfiles/outfile with spaces" -Only in ./bash_outfiles: outfile with spaces -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -mini exit code = 1 -bash exit code = 0 -mini error = ( No such file or directory) -bash error = () -Test 87: ❌ ls >"./outfiles/outfile""1""2""3""4""5" -Only in ./bash_outfiles: outfile12345 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -mini exit code = 1 -bash exit code = 0 -mini error = ( No such file or directory) -bash error = () -Test 88: ❌ ls >"./outfiles/outfile01" >./test_files/invalid_permission >"./outfiles/outfile02" -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -mini error = ( No such file or directory) -bash error = ( Permission denied) -Test 89: ✅ ls >./test_files/invalid_permission >"./outfiles/outfile01" >./test_files/invalid_permission -Test 90: ❌ cat <"./test_files/infile" >"./outfiles/outfile01" -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -hi -hello -world -42 -mini exit code = 1 -bash exit code = 0 -mini error = ( No such file or directory) -bash error = () -Test 91: ✅ echo hi >./outfiles/outfile01 | echo bye -Test 92: ❌ echo hi >./outfiles/outfile01 >./outfiles/outfile02 | echo bye -Only in ./bash_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -hi -Test 93: ✅ echo hi | echo >./outfiles/outfile01 bye -Test 94: ❌ echo hi | echo bye >./outfiles/outfile01 >./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -bye -bash outfiles: -bye -Test 95: ✅ echo hi >./outfiles/outfile01 | echo bye >./outfiles/outfile02 -Test 96: ❌ echo hi >./outfiles/outfile01 >./test_files/invalid_permission | echo bye -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 97: ✅ echo hi >./test_files/invalid_permission | echo bye -Test 98: ❌ echo hi >./test_files/invalid_permission >./outfiles/outfile01 | echo bye -Only in ./mini_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini error = () -bash error = ( Permission denied) -Test 99: ✅ echo hi | echo bye >./test_files/invalid_permission -Test 100: ❌ echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -mini exit code = 139 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) - -update for this. - -Test 101: ❌ echo hi | echo bye >./test_files/invalid_permission >./outfiles/outfile01 -Only in ./mini_outfiles: outfile01 -mini outfiles: -bye -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) -Test 102: ✅⚠️ cat <"./test_files/infile" >./test_files/invalid_permission -mini error = ( No such file or directory) -bash error = ( Permission denied) -Test 103: ✅⚠️ cat >./test_files/invalid_permission <"./test_files/infile" -mini error = ( No such file or directory) -bash error = ( Permission denied) -Test 104: ✅ cat ./outfiles/outfile01 -Test 105: ❌ cat >./outfiles/outfile01 ./test_files/invalid_permission -Test 107: ✅⚠️ cat >./test_files/invalid_permission ./outfiles/outfile01 ./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 109: ✅ ls >>./outfiles/outfile01 -Test 110: ✅ ls >> ./outfiles/outfile01 -Test 111: ✅ ls >>./outfiles/outfile01 >./outfiles/outfile01 -Test 112: ✅ ls >./outfiles/outfile01 >>./outfiles/outfile01 -Test 113: ❌ ls >./outfiles/outfile01 >>./outfiles/outfile01 >./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -Test 114: ❌ ls >>./outfiles/outfile01 >>./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -Test 115: ✅ ls >>./test_files/invalid_permission -Test 116: ❌ ls >>./test_files/invalid_permission >>./outfiles/outfile01 -Only in ./mini_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) -Test 117: ❌ ls >>./outfiles/outfile01 >>./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 118: ❌ ls >./outfiles/outfile01 >>./test_files/invalid_permission >>./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -Only in ./mini_outfiles: outfile02 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) -Test 119: ✅ ls >./test_files/invalid_permission >>./outfiles/outfile02 -Test 120: ✅⚠️ ls >>./test_files/invalid_permission >>./outfiles/outfile02 >./outfiles/outfile01 | echo bye -Test 122: ❌ echo hi >>./outfiles/outfile01 >>./outfiles/outfile02 | echo bye -Only in ./bash_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -hi -Test 123: ✅ echo hi | echo >>./outfiles/outfile01 bye -Test 124: ❌ echo hi | echo bye >>./outfiles/outfile01 >>./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -bye -bash outfiles: -bye -Test 125: ✅ echo hi >>./outfiles/outfile01 | echo bye >>./outfiles/outfile02 -Test 126: ✅ echo hi >>./test_files/invalid_permission | echo bye -Test 127: ❌ echo hi >>./test_files/invalid_permission >./outfiles/outfile01 | echo bye -Only in ./mini_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini error = () -bash error = ( Permission denied) -Test 128: ✅ echo hi | echo bye >>./test_files/invalid_permission -Test 129: ❌ echo hi | echo >>./outfiles/outfile01 bye >./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 130: ✅ cat ./outfiles/outfile -Test 131: ✅ cat +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:48:27 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:26:30 by dkaiser ### ########.fr */ +/* Updated: 2025/01/18 19:28:51 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -#include "debug_tools.h" -#include "stdlib.h" +#ifndef AST_H +# define AST_H +# include "debug_tools.h" +# include "stdlib.h" enum e_node_type { @@ -67,3 +69,5 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_node *new_string_node(char *string); void free_node(t_node *node); + +#endif \ No newline at end of file diff --git a/include/debug_tools.h b/include/debug_tools.h index 4a7ff10..6c4bc29 100644 --- a/include/debug_tools.h +++ b/include/debug_tools.h @@ -3,15 +3,17 @@ /* ::: :::::::: */ /* debug_tools.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 18:34:37 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 15:05:12 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 21:08:15 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DEBUG_TOOLS_H # define DEBUG_TOOLS_H +# include +# include "debug_tools.h" # include "libft.h" @@ -22,4 +24,9 @@ void dbg(char *str); void panic(char *msg); + + +void dbg2(const char *format, ...); + + #endif diff --git a/include/env.h b/include/env.h index 0c45aef..3fc83b7 100644 --- a/include/env.h +++ b/include/env.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 16:50:55 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:43:07 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,5 +37,6 @@ int cd(t_env **env, char **args); int ft_env(t_env *env); int builtin_exit(char **args, t_env **env); t_env *env_new(char *name); +t_env *check_existing(t_env *env, char *av); #endif \ No newline at end of file diff --git a/include/minishell.h b/include/minishell.h index ee42c79..7a573e8 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:38:44 by dkaiser ### ########.fr */ +/* Updated: 2025/01/18 19:30:43 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,11 +41,20 @@ void print_ast(t_node *ast); int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); -char *format_string(char *str, t_env *env); +char *format_string(char *str, t_env *env, int is_literal); int set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); char *read_heredoc(char *delimiter); -void create_files(t_list *files); +int handle_input_redirection(t_redirection *redir); +int handle_output_redirection(t_redirection *redir); +int handle_redirections(t_redirection *redirs); +int handle_pipe_parent(int p[2], t_node *node, t_env **env); +int handle_pipe_child(int p[2], t_node *node, + t_env **env, int in_fd); +int open_file(char *path, int flags, int mode); +int eval_rec(t_node *node, t_env **env, int in_fd); +void create_files(t_list *files); + #endif diff --git a/include/token.h b/include/token.h index 54a65f2..993a408 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/08/29 15:26:23 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:37:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,5 +49,6 @@ void free_token_and_connect(t_token *token); void free_tokens(t_token *tokens); void tokenizer(char *s, t_token **token_list, char quote_check); +void print_token(t_token *token); #endif diff --git a/lib/libft/_obj/ft_atoi.o b/lib/libft/_obj/ft_atoi.o new file mode 100644 index 0000000..efcdc0d Binary files /dev/null and b/lib/libft/_obj/ft_atoi.o differ diff --git a/lib/libft/_obj/ft_atol.o b/lib/libft/_obj/ft_atol.o new file mode 100644 index 0000000..3126061 Binary files /dev/null and b/lib/libft/_obj/ft_atol.o differ diff --git a/lib/libft/_obj/ft_bzero.o b/lib/libft/_obj/ft_bzero.o new file mode 100644 index 0000000..64ddf3a Binary files /dev/null and b/lib/libft/_obj/ft_bzero.o differ diff --git a/lib/libft/_obj/ft_calloc.o b/lib/libft/_obj/ft_calloc.o new file mode 100644 index 0000000..39c2c05 Binary files /dev/null and b/lib/libft/_obj/ft_calloc.o differ diff --git a/lib/libft/_obj/ft_isalnum.o b/lib/libft/_obj/ft_isalnum.o new file mode 100644 index 0000000..3aecc58 Binary files /dev/null and b/lib/libft/_obj/ft_isalnum.o differ diff --git a/lib/libft/_obj/ft_isalpha.o b/lib/libft/_obj/ft_isalpha.o new file mode 100644 index 0000000..da20b01 Binary files /dev/null and b/lib/libft/_obj/ft_isalpha.o differ diff --git a/lib/libft/_obj/ft_isascii.o b/lib/libft/_obj/ft_isascii.o new file mode 100644 index 0000000..2262942 Binary files /dev/null and b/lib/libft/_obj/ft_isascii.o differ diff --git a/lib/libft/_obj/ft_isdigit.o b/lib/libft/_obj/ft_isdigit.o new file mode 100644 index 0000000..a0e0f40 Binary files /dev/null and b/lib/libft/_obj/ft_isdigit.o differ diff --git a/lib/libft/_obj/ft_isprint.o b/lib/libft/_obj/ft_isprint.o new file mode 100644 index 0000000..8cf5033 Binary files /dev/null and b/lib/libft/_obj/ft_isprint.o differ diff --git a/lib/libft/_obj/ft_isspace.o b/lib/libft/_obj/ft_isspace.o new file mode 100644 index 0000000..f6f10e8 Binary files /dev/null and b/lib/libft/_obj/ft_isspace.o differ diff --git a/lib/libft/_obj/ft_itoa.o b/lib/libft/_obj/ft_itoa.o new file mode 100644 index 0000000..447812f Binary files /dev/null and b/lib/libft/_obj/ft_itoa.o differ diff --git a/lib/libft/_obj/ft_lstadd_back_bonus.o b/lib/libft/_obj/ft_lstadd_back_bonus.o new file mode 100644 index 0000000..566f7fc Binary files /dev/null and b/lib/libft/_obj/ft_lstadd_back_bonus.o differ diff --git a/lib/libft/_obj/ft_lstadd_front_bonus.o b/lib/libft/_obj/ft_lstadd_front_bonus.o new file mode 100644 index 0000000..f456510 Binary files /dev/null and b/lib/libft/_obj/ft_lstadd_front_bonus.o differ diff --git a/lib/libft/_obj/ft_lstclear_bonus.o b/lib/libft/_obj/ft_lstclear_bonus.o new file mode 100644 index 0000000..cbf23d7 Binary files /dev/null and b/lib/libft/_obj/ft_lstclear_bonus.o differ diff --git a/lib/libft/_obj/ft_lstdelone_bonus.o b/lib/libft/_obj/ft_lstdelone_bonus.o new file mode 100644 index 0000000..c357699 Binary files /dev/null and b/lib/libft/_obj/ft_lstdelone_bonus.o differ diff --git a/lib/libft/_obj/ft_lstiter_bonus.o b/lib/libft/_obj/ft_lstiter_bonus.o new file mode 100644 index 0000000..139d20d Binary files /dev/null and b/lib/libft/_obj/ft_lstiter_bonus.o differ diff --git a/lib/libft/_obj/ft_lstlast_bonus.o b/lib/libft/_obj/ft_lstlast_bonus.o new file mode 100644 index 0000000..0f262f1 Binary files /dev/null and b/lib/libft/_obj/ft_lstlast_bonus.o differ diff --git a/lib/libft/_obj/ft_lstmap_bonus.o b/lib/libft/_obj/ft_lstmap_bonus.o new file mode 100644 index 0000000..d4ed0b6 Binary files /dev/null and b/lib/libft/_obj/ft_lstmap_bonus.o differ diff --git a/lib/libft/_obj/ft_lstnew_bonus.o b/lib/libft/_obj/ft_lstnew_bonus.o new file mode 100644 index 0000000..98420b2 Binary files /dev/null and b/lib/libft/_obj/ft_lstnew_bonus.o differ diff --git a/lib/libft/_obj/ft_lstsize_bonus.o b/lib/libft/_obj/ft_lstsize_bonus.o new file mode 100644 index 0000000..1ce926d Binary files /dev/null and b/lib/libft/_obj/ft_lstsize_bonus.o differ diff --git a/lib/libft/_obj/ft_memchr.o b/lib/libft/_obj/ft_memchr.o new file mode 100644 index 0000000..23efe3d Binary files /dev/null and b/lib/libft/_obj/ft_memchr.o differ diff --git a/lib/libft/_obj/ft_memcmp.o b/lib/libft/_obj/ft_memcmp.o new file mode 100644 index 0000000..ca68348 Binary files /dev/null and b/lib/libft/_obj/ft_memcmp.o differ diff --git a/lib/libft/_obj/ft_memcpy.o b/lib/libft/_obj/ft_memcpy.o new file mode 100644 index 0000000..253e17e Binary files /dev/null and b/lib/libft/_obj/ft_memcpy.o differ diff --git a/lib/libft/_obj/ft_memmove.o b/lib/libft/_obj/ft_memmove.o new file mode 100644 index 0000000..df18bef Binary files /dev/null and b/lib/libft/_obj/ft_memmove.o differ diff --git a/lib/libft/_obj/ft_memset.o b/lib/libft/_obj/ft_memset.o new file mode 100644 index 0000000..be0b314 Binary files /dev/null and b/lib/libft/_obj/ft_memset.o differ diff --git a/lib/libft/_obj/ft_printaddr.o b/lib/libft/_obj/ft_printaddr.o new file mode 100644 index 0000000..64e9dea Binary files /dev/null and b/lib/libft/_obj/ft_printaddr.o differ diff --git a/lib/libft/_obj/ft_printf.o b/lib/libft/_obj/ft_printf.o new file mode 100644 index 0000000..44aedad Binary files /dev/null and b/lib/libft/_obj/ft_printf.o differ diff --git a/lib/libft/_obj/ft_printhex.o b/lib/libft/_obj/ft_printhex.o new file mode 100644 index 0000000..5ef814f Binary files /dev/null and b/lib/libft/_obj/ft_printhex.o differ diff --git a/lib/libft/_obj/ft_printnbr.o b/lib/libft/_obj/ft_printnbr.o new file mode 100644 index 0000000..278895f Binary files /dev/null and b/lib/libft/_obj/ft_printnbr.o differ diff --git a/lib/libft/_obj/ft_putchar_fd.o b/lib/libft/_obj/ft_putchar_fd.o new file mode 100644 index 0000000..e3a7ad2 Binary files /dev/null and b/lib/libft/_obj/ft_putchar_fd.o differ diff --git a/lib/libft/_obj/ft_putendl_fd.o b/lib/libft/_obj/ft_putendl_fd.o new file mode 100644 index 0000000..5ff74c8 Binary files /dev/null and b/lib/libft/_obj/ft_putendl_fd.o differ diff --git a/lib/libft/_obj/ft_putnbr_fd.o b/lib/libft/_obj/ft_putnbr_fd.o new file mode 100644 index 0000000..1de6482 Binary files /dev/null and b/lib/libft/_obj/ft_putnbr_fd.o differ diff --git a/lib/libft/_obj/ft_putstr_fd.o b/lib/libft/_obj/ft_putstr_fd.o new file mode 100644 index 0000000..bc970e3 Binary files /dev/null and b/lib/libft/_obj/ft_putstr_fd.o differ diff --git a/lib/libft/_obj/ft_split.o b/lib/libft/_obj/ft_split.o new file mode 100644 index 0000000..d5df61b Binary files /dev/null and b/lib/libft/_obj/ft_split.o differ diff --git a/lib/libft/_obj/ft_strcat.o b/lib/libft/_obj/ft_strcat.o new file mode 100644 index 0000000..f749d90 Binary files /dev/null and b/lib/libft/_obj/ft_strcat.o differ diff --git a/lib/libft/_obj/ft_strchr.o b/lib/libft/_obj/ft_strchr.o new file mode 100644 index 0000000..fea0b41 Binary files /dev/null and b/lib/libft/_obj/ft_strchr.o differ diff --git a/lib/libft/_obj/ft_strcmp.o b/lib/libft/_obj/ft_strcmp.o new file mode 100644 index 0000000..5c7286b Binary files /dev/null and b/lib/libft/_obj/ft_strcmp.o differ diff --git a/lib/libft/_obj/ft_strcpy.o b/lib/libft/_obj/ft_strcpy.o new file mode 100644 index 0000000..d556c8f Binary files /dev/null and b/lib/libft/_obj/ft_strcpy.o differ diff --git a/lib/libft/_obj/ft_strdup.o b/lib/libft/_obj/ft_strdup.o new file mode 100644 index 0000000..e052b40 Binary files /dev/null and b/lib/libft/_obj/ft_strdup.o differ diff --git a/lib/libft/_obj/ft_striteri.o b/lib/libft/_obj/ft_striteri.o new file mode 100644 index 0000000..e8264a9 Binary files /dev/null and b/lib/libft/_obj/ft_striteri.o differ diff --git a/lib/libft/_obj/ft_strjoin.o b/lib/libft/_obj/ft_strjoin.o new file mode 100644 index 0000000..bb8a89e Binary files /dev/null and b/lib/libft/_obj/ft_strjoin.o differ diff --git a/lib/libft/_obj/ft_strlcat.o b/lib/libft/_obj/ft_strlcat.o new file mode 100644 index 0000000..e7aff81 Binary files /dev/null and b/lib/libft/_obj/ft_strlcat.o differ diff --git a/lib/libft/_obj/ft_strlcpy.o b/lib/libft/_obj/ft_strlcpy.o new file mode 100644 index 0000000..695a120 Binary files /dev/null and b/lib/libft/_obj/ft_strlcpy.o differ diff --git a/lib/libft/_obj/ft_strlen.o b/lib/libft/_obj/ft_strlen.o new file mode 100644 index 0000000..2c66545 Binary files /dev/null and b/lib/libft/_obj/ft_strlen.o differ diff --git a/lib/libft/_obj/ft_strmapi.o b/lib/libft/_obj/ft_strmapi.o new file mode 100644 index 0000000..345a34a Binary files /dev/null and b/lib/libft/_obj/ft_strmapi.o differ diff --git a/lib/libft/_obj/ft_strncmp.o b/lib/libft/_obj/ft_strncmp.o new file mode 100644 index 0000000..683c861 Binary files /dev/null and b/lib/libft/_obj/ft_strncmp.o differ diff --git a/lib/libft/_obj/ft_strncpy.o b/lib/libft/_obj/ft_strncpy.o new file mode 100644 index 0000000..5585d29 Binary files /dev/null and b/lib/libft/_obj/ft_strncpy.o differ diff --git a/lib/libft/_obj/ft_strnstr.o b/lib/libft/_obj/ft_strnstr.o new file mode 100644 index 0000000..4f42fd4 Binary files /dev/null and b/lib/libft/_obj/ft_strnstr.o differ diff --git a/lib/libft/_obj/ft_strrchr.o b/lib/libft/_obj/ft_strrchr.o new file mode 100644 index 0000000..c6b2a16 Binary files /dev/null and b/lib/libft/_obj/ft_strrchr.o differ diff --git a/lib/libft/_obj/ft_strtrim.o b/lib/libft/_obj/ft_strtrim.o new file mode 100644 index 0000000..b4ac15c Binary files /dev/null and b/lib/libft/_obj/ft_strtrim.o differ diff --git a/lib/libft/_obj/ft_substr.o b/lib/libft/_obj/ft_substr.o new file mode 100644 index 0000000..eebbca7 Binary files /dev/null and b/lib/libft/_obj/ft_substr.o differ diff --git a/lib/libft/_obj/ft_tolower.o b/lib/libft/_obj/ft_tolower.o new file mode 100644 index 0000000..940856f Binary files /dev/null and b/lib/libft/_obj/ft_tolower.o differ diff --git a/lib/libft/_obj/ft_toupper.o b/lib/libft/_obj/ft_toupper.o new file mode 100644 index 0000000..137ae03 Binary files /dev/null and b/lib/libft/_obj/ft_toupper.o differ diff --git a/lib/libft/_obj/get_next_line.o b/lib/libft/_obj/get_next_line.o new file mode 100644 index 0000000..d5cbdc5 Binary files /dev/null and b/lib/libft/_obj/get_next_line.o differ diff --git a/lib/libft/_obj/get_next_line_utils.o b/lib/libft/_obj/get_next_line_utils.o new file mode 100644 index 0000000..0eecbe7 Binary files /dev/null and b/lib/libft/_obj/get_next_line_utils.o differ diff --git a/lib/libft/libft.a b/lib/libft/libft.a new file mode 100644 index 0000000..1a43a20 Binary files /dev/null and b/lib/libft/libft.a differ diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index f3bcfc0..7cbcdeb 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,60 +6,13 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:51:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:33:33 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include -int echo(char **av) -{ - int i; - int f; - - i = 1; - f = 1; - if (av[1] == NULL || av[1][0] == '\0') - { - write(1, "\n", 1); - return (0); - } - if (ft_strncmp(av[1], "-n", 3) == 0) - { - i++; - f = 0; - } - while (av[i]) - { - write(1, av[i], ft_strlen(av[i])); - i++; - if (av[i]) - write(1, " ", 1); - } - if (f) - write(1, "\n", 1); - return (0); -} - -void exit_shell(t_env **env, int exit_status) -{ - free_envlst(env); - exit(exit_status); -} - -int builtin_exit(char **args, t_env **env) -{ - int exit_status; - - if (args[1]) - exit_status = ft_atoi(args[1]); - else - exit_status = 0; - exit_shell(env, exit_status); - return (exit_status); -} - int unset(char **av, t_env **env) { t_env *current; @@ -69,8 +22,6 @@ int unset(char **av, t_env **env) i = 0; while (av[++i]) { - if (ft_strchr(av[i], '?')) - return (1); current = *env; prev = NULL; while (current) @@ -95,6 +46,8 @@ t_env *check_existing(t_env *env, char *av) { while (env) { + if (ft_strcmp("$", av) == 0) + return (NULL); if (ft_strcmp(env->name, av) == 0) return (env); env = env->next; @@ -102,51 +55,65 @@ t_env *check_existing(t_env *env, char *av) return (NULL); } -int export(char **av, t_env **env) +void export_export(char *av, t_env **env) { char *tmp; t_env *current; - int i; current = NULL; + tmp = ft_strchr(av, '='); + *tmp = '\0'; + current = check_existing(*env, av); + if (current) + free(current->value); + else + { + current = env_new(ft_strdup(av)); + current->next = *env; + *env = current; + } + current->value = ft_strdup(tmp + 1); +} + +int is_valid_identifier(char *str) +{ + int i; + i = 0; - while (av[++i]) + if (!ft_isalpha(str[0]) && str[0] != '_') + return (0); + while (str[i] && str[i] != '=') { - if ((ft_strchr(av[i], '='))) - { - tmp = ft_strchr(av[i], '='); - *tmp = '\0'; - if (ft_strchr(av[i], '?')) - return (1); - current = check_existing(*env, av[i]); - if (current) - free(current->value); - else - { - current = env_new(ft_strdup(av[i])); - current->next = *env; - *env = current; - } - current->value = ft_strdup(tmp + 1); - } - else - return (1); + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); + i++; } - return (0); + return (1); } -void set_return_code(int return_code, t_env **env) +int export(char **av, t_env **env) { - t_env *cur; + char *equal_sign; + int i; - cur = check_existing(*env, "?"); - if (cur) - free(cur->value); - else + i = 0; + while (av[++i]) { - cur = env_new(ft_strdup("?")); - cur->next = *env; - *env = cur; + equal_sign = ft_strchr(av[i], '='); + if (equal_sign) + *equal_sign = '\0'; + if (!is_valid_identifier(av[i])) + { + write(1, "Minishell $ export: not a valid identifier\n", 43); + if (equal_sign) + *equal_sign = '='; + continue ; + } + if (equal_sign) + { + *equal_sign = '='; + export_export(av[i], env); + } } - cur->value = ft_itoa(return_code); + return (0); } diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c new file mode 100644 index 0000000..7c73e95 --- /dev/null +++ b/src/builtins_part_three.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins_part_three.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:29:24 by chuhlig #+# #+# */ +/* Updated: 2025/01/18 18:34:29 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void exit_shell(t_env **env, int exit_status) +{ + free_envlst(env); + exit(exit_status); +} + +int builtin_exit(char **av, t_env **env) +{ + int exit_status; + + if (av[1]) + exit_status = ft_atoi(av[1]); + else + exit_status = 0; + exit_shell(env, exit_status); + return (exit_status); +} + +void set_return_code(int return_code, t_env **env) +{ + t_env *cur; + + cur = check_existing(*env, "?"); + if (cur) + free(cur->value); + else + { + cur = env_new(ft_strdup("?")); + cur->next = *env; + *env = cur; + } + cur->value = ft_itoa(return_code); +} + +int echo(char **av) +{ + int i; + int f; + + i = 1; + f = 1; + if (av[1] == NULL || av[1][0] == '\0') + { + write(1, "\n", 1); + return (0); + } + if (ft_strncmp(av[1], "-n", 3) == 0) + { + i++; + f = 0; + } + while (av[i]) + { + write(1, av[i], ft_strlen(av[i])); + i++; + if (av[i]) + write(1, " ", 1); + } + if (f) + write(1, "\n", 1); + return (0); +} diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c index 05d6943..9fabc81 100644 --- a/src/builtins_part_two.c +++ b/src/builtins_part_two.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:31:17 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:57:12 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ int cd(t_env **env, char **av) } if (chdir(current->value) == -1) return (1); + update_pwd(env); } else { @@ -101,7 +102,7 @@ int ft_env(t_env *env) { while (env != NULL) { - if (strchr(env->name, '?')) + if (ft_strchr(env->name, '?')) { env = env->next; continue ; diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 171dc06..c170a30 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:19:36 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 13:00:04 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec, static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env); -t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files) +t_redirection *collect_redirs(t_token **tokens, t_env *env, + t_list **create_files) { t_redirection *result; t_token *cur; @@ -45,28 +46,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_file return (result); } -static t_redirection *set_redir(t_redirection *redir, int type, char *spec, - t_env *env) -{ - t_redirection *result; - - redir->type = type; - if (spec != NULL) - redir->specifier = format_string(spec, env); - else - redir->specifier = spec; - if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) - { - result = malloc(sizeof(t_redirection)); - if (!result) - return (NULL); - result->type = type; - result->specifier = spec; - return (result); - } - return (NULL); -} - static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env, t_list **create_files) { @@ -89,7 +68,7 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], OUTPUT_APPEND, str, env))); next_token = (*cur)->next; - free_token_and_connect(*cur); + // free_token_and_connect(*cur); if (next_token) { *cur = next_token->next; @@ -99,6 +78,30 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, *cur = NULL; } +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) +{ + t_redirection *result; + + redir->type = type; + if (spec != NULL) + redir->specifier = format_string(spec, env, ft_atoi("0")); + else + redir->specifier = spec; + if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + { + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); + } + return (NULL); +} + + + static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { char *heredoc_data; diff --git a/src/create_files.c b/src/create_files.c index faee27f..8689f88 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* create_files.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 19:16:33 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,10 +22,10 @@ void create_files(t_list *files) { dbg("Test"); if (files->content == NULL) - continue; + continue ; file = (t_redirection *)files->content; if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) - break; + break ; if (file->type == OUTPUT_OVERRIDE) { fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); @@ -36,10 +36,10 @@ void create_files(t_list *files) fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); close(fd); } - /* if (files->next == NULL) */ - /* break; */ - /* if (((t_redirection *) files->next->content)->type == 0) */ - /* break; */ + if (files->next == NULL) + break ; + if (((t_redirection *) files->next->content)->type == 0) + break ; files = files->next; } } diff --git a/src/debug_tools.c b/src/debug_tools.c index de59703..6bee1b0 100644 --- a/src/debug_tools.c +++ b/src/debug_tools.c @@ -3,14 +3,16 @@ /* ::: :::::::: */ /* debug_tools.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 15:04:43 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:50:36 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" +#include +#include void dbg(char *msg) { diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c index a1ab7cc..5806d96 100644 --- a/src/env_to_strlst.c +++ b/src/env_to_strlst.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:34:10 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:50:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,32 @@ static char *get_var_assign(t_env *cur); -char **env_to_strlst(t_env *env) +static int getsize(t_env *env) { int size; t_env *cur; - char **result; - int i; size = 0; cur = env; - while (cur != NULL) + while (cur) { - if (ft_strchr(cur->name, '?')) - { - cur = cur->next; - continue ; - } - size++; + if (!ft_strchr(cur->name, '?')) + size++; cur = cur->next; } + return (size); +} + +char **env_to_strlst(t_env *env) +{ + int size; + t_env *cur; + char **result; + int i; + + size = 0; + cur = env; + size = getsize(env); result = malloc(sizeof(char *) * (size + 1)); if (result == NULL) return (NULL); diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 8f5b541..e2b9d66 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/16 18:38:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 19:15:46 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/format_string.c b/src/format_string.c index 5f31130..7e64039 100644 --- a/src/format_string.c +++ b/src/format_string.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 18:06:17 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 20:24:05 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,46 +14,18 @@ #include "libft.h" #include "minishell.h" -static void append_slice(char **dst, char *src, int start, int end); -static void append_var(char **dst, char *src, int *pos, t_env *env); - -char *format_string(char *str, t_env *env) +void append_var_exit_code(char **dst, t_env *env) { + char *exit_code; char *result; - int pos; - int start; - int is_literal; - pos = 0; - start = 0; - is_literal = 0; - result = NULL; - if (str == NULL) - return (NULL); - while (str[pos] != '\0') + exit_code = env_get(env, "?"); + if (exit_code) { - if (str[pos] == '\'') - { - append_slice(&result, str, start, pos); - start = pos + 1; - is_literal = !is_literal; - } - if (str[pos] == '"' && !is_literal) - { - append_slice(&result, str, start, pos); - start = pos + 1; - } - if (str[pos] == '$' && !is_literal) - { - append_slice(&result, str, start, pos); - append_var(&result, str, &pos, env); - start = pos; - continue ; - } - pos++; + result = ft_strjoin(*dst, exit_code); + free(*dst); + *dst = result; } - append_slice(&result, str, start, pos); - return (result); } static void append_slice(char **dst, char *src, int start, int end) @@ -65,9 +37,7 @@ static void append_slice(char **dst, char *src, int start, int end) if (*dst != NULL) len = ft_strlen(*dst); else - { len = 0; - } result = malloc(len + (end - start) + 1); if (!result) return ; @@ -93,27 +63,63 @@ static void append_var(char **dst, char *src, int *pos, t_env *env) i = 0; *pos += 1; - while (src[*pos + i] != '\0' && src[*pos + i] != '\'' && src[*pos - + i] != '"' && src[*pos + i] != '$') - { + while (ft_isalnum(src[*pos + i]) || src[*pos + i] == '_') i++; - } - var = malloc(i + 1); - if (var == NULL) + if (i == 0) return ; - var[i] = '\0'; - i--; - while (i >= 0) - { - var[i] = src[*pos + i]; - i--; - } + var = ft_substr(src, *pos, i); value = env_get(env, var); - if (value != NULL) + if (value) { result = ft_strjoin(*dst, value); free(*dst); *dst = result; } - *pos += ft_strlen(var); + *pos += i; + free(var); +} + +static void handle_dollar_sign(char **result, char *str, int *pos, t_env *env) +{ + if (str[*pos + 1] == '?') + { + append_var_exit_code(result, env); + *pos += 2; + } + else if (ft_isalnum(str[*pos + 1]) || str[*pos + 1] == '_') + append_var(result, str, pos, env); + else + { + append_slice(result, str, *pos, *pos + 1); + (*pos)++; + } +} + +char *format_string(char *str, t_env *env, int is_literal) +{ + char *result; + int pos; + int start; + + pos = 0; + start = 0; + result = NULL; + if (!str) + return (NULL); + while (str[pos]) + { + if (str[pos] == '\'' || (str[pos] == '\"' && !is_literal) + || (str[pos] == '$' && !is_literal)) + { + append_slice(&result, str, start, pos); + if (str[pos] == '$') + handle_dollar_sign(&result, str, &pos, env); + else + is_literal ^= (str[pos++] == '\''); + start = pos; + continue ; + } + pos++; + } + return (append_slice(&result, str, start, pos), result); } diff --git a/src/free_token.c b/src/free_token.c index 9b035ac..512ba23 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -3,14 +3,15 @@ /* ::: :::::::: */ /* free_token.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */ -/* Updated: 2024/08/02 14:23:56 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:49:48 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "token.h" +#include "debug_tools.h" void free_token(t_token *token) { @@ -19,6 +20,7 @@ void free_token(t_token *token) if (token->next != NULL) token->next->previous = NULL; free(token); + token = NULL; } void free_token_and_connect(t_token *token) @@ -28,6 +30,7 @@ void free_token_and_connect(t_token *token) if (token->next != NULL) token->next->previous = token->previous; free(token); + token = NULL; } void free_tokens(t_token *tokens) diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 543540b..c35b3bc 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/15 16:38:39 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 18:56:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,7 +86,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) } *return_code = 127; printf("%s:", cmd); - ft_putstr_fd(" command not found", 2); + ft_putstr_fd(" command not found", 2);//output is shit even if its fine for tester return (NULL); } diff --git a/src/handle_redir.c b/src/handle_redir.c new file mode 100644 index 0000000..29bba92 --- /dev/null +++ b/src/handle_redir.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ +/* Updated: 2025/01/18 18:47:31 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int handle_input_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == INPUT_FILE) + { + fd = open_file(redir->specifier, O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + else if (redir->type == INPUT_LIMITER) + { + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return (-1); + write(fd, redir->specifier, ft_strlen(redir->specifier)); + close(fd); + fd = open_file("/tmp/heredoc_tmp", O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + return (0); +} + +int handle_output_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == OUTPUT_OVERRIDE) + { + fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + else if (redir->type == OUTPUT_APPEND) + { + fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + return (0); +} + +int handle_redirections(t_redirection *redirs) +{ + if (redirs[0].type == INPUT_FILE || redirs[0].type == INPUT_LIMITER) + { + if (handle_input_redirection(&redirs[0]) < 0) + return (-1); + } + if (redirs[1].type == OUTPUT_OVERRIDE || redirs[1].type == OUTPUT_APPEND) + { + if (handle_output_redirection(&redirs[1]) < 0) + return (-1); + } + return (0); +} + +int handle_pipe_parent(int p[2], t_node *node, t_env **env) +{ + int original_stdin; + int result; + + close(p[1]); + original_stdin = dup(STDIN_FILENO); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); + dup2(original_stdin, STDIN_FILENO); + close(original_stdin); + close(p[0]); + return (result); +} + +int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd) +{ + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); + close(p[1]); + exit(eval_rec(node->content.pipe.left, env, in_fd)); +} diff --git a/src/interpreter.c b/src/interpreter.c index c7fe67c..f31e965 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,100 +6,41 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:48:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" #include "minishell.h" -int handle_redirections(t_redirection *redirs) +int eval_rec(t_node *node, t_env **env, int in_fd); + +int open_file(char *path, int flags, int mode) { int fd; - if (redirs[0].type == INPUT_FILE) - { - fd = open(redirs[0].specifier, O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - else if (redirs[0].type == INPUT_LIMITER) - { - fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier)); - close(fd); - fd = open("/tmp/heredoc_tmp", O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - if (redirs[1].type == OUTPUT_OVERRIDE) - { - fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - else if (redirs[1].type == OUTPUT_APPEND) - { - fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - return (0); + fd = open(path, flags, mode); + if (fd < 0) + perror("open"); + return (fd); } int eval_rec(t_node *node, t_env **env, int in_fd) { - pid_t pid; int p[2]; + pid_t pid; int result; - int original_stdin; if (node->type == PIPE_NODE) { - pipe(p); + if (pipe(p) == -1) + return (perror("pipe"), EXIT_FAILURE); pid = fork(); + if (pid == -1) + return (perror("fork"), close(p[0]), close(p[1]), EXIT_FAILURE); if (pid == 0) - { - close(p[0]); - dup2(in_fd, STDIN_FILENO); - dup2(p[1], STDOUT_FILENO); - result = eval_rec(node->content.pipe.left, env, in_fd); - exit(result); - } - else - { - close(p[1]); - original_stdin = dup(STDIN_FILENO); - dup2(p[0], STDIN_FILENO); - result = eval_rec(node->content.pipe.right, env, p[0]); - dup2(original_stdin, STDIN_FILENO); - close(original_stdin); - } + handle_pipe_child(p, node, env, in_fd); + result = handle_pipe_parent(p, node, env); } else if (node->type == CMD_NODE) result = execute_cmd(&node->content.cmd, env); diff --git a/src/new_node.c b/src/new_node.c index 83d9159..bbac154 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* new_node.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:25:54 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) node->content.cmd.redirs[1] = redirs[1]; node->content.cmd.create_files = create_files; free(redirs); + redirs = NULL;//1 return (node); } return (NULL); diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 92dfd12..d13bf3f 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:44:44 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) { char **args; t_redirection *redirs; - t_list *create_files; + t_list *create_files; create_files = NULL; redirs = collect_redirs(&tokens, *env, &create_files); @@ -36,6 +36,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) static char **collect_args(t_token **tokens, t_env **env) { t_token *cur; + t_token *next;//2 char **result; int i; @@ -50,12 +51,14 @@ static char **collect_args(t_token **tokens, t_env **env) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { + next = cur->next;//2 if (cur->previous) free_token(cur->previous); - result[i] = format_string(cur->content.string, *env); + result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; - cur = cur->next; + // cur = cur->next; + cur = next;//2 } result[i] = NULL; return (result); -} +} \ No newline at end of file diff --git a/src/parser.c b/src/parser.c index 1375954..aef8d70 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:06:54 by chuhlig ### ########.fr */ +/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env) if (left_side_tokens == NULL) { free_tokens(tokens); + tokens = NULL;//1 return (NULL); } else if (tokens != NULL) @@ -70,6 +71,7 @@ t_token *split_at_first(t_token **tokens, int type) if (result == split) result = NULL; free_token(split); + split = NULL;//1 return (result); } diff --git a/src/repl.c b/src/repl.c index 5079ee8..15b0a80 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 15:39:58 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 12:45:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void repl(const char *prompt, t_env **env, int *promptflag) while (1) { input = readline(prompt); - if (input == NULL) + if (input == NULL) { if (*promptflag > 1) (*promptflag)--; @@ -41,3 +41,5 @@ void repl(const char *prompt, t_env **env, int *promptflag) free(input); } } + +//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission \ No newline at end of file diff --git a/src/signal_handling.c b/src/signal_handling.c index c6273d0..6c6ca1e 100644 --- a/src/signal_handling.c +++ b/src/signal_handling.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:08:43 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 14:11:29 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 12:15:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/teest.txt b/teest.txt deleted file mode 100644 index 9daeafb..0000000 --- a/teest.txt +++ /dev/null @@ -1 +0,0 @@ -test