aboutsummaryrefslogtreecommitdiff
path: root/src/collect_redirs.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-08-02 15:29:00 +0200
committerGitHub2024-08-02 15:29:00 +0200
commitc7a4494fd97b7e80665cbd47ed96bc37bf5800e5 (patch)
treed2520064c761e00772185dcdb82f83d566634a85 /src/collect_redirs.c
parent82c1eed4f7795ef135a586af2fb334145b64b2f6 (diff)
downloadminishell-c7a4494fd97b7e80665cbd47ed96bc37bf5800e5.tar.gz
minishell-c7a4494fd97b7e80665cbd47ed96bc37bf5800e5.zip
Refactor parser
* Fix norme for free_node.c * Uglify parser.c * Merge parser updates into refactor-parser * Outsource collect_assigns and collect_redirs * Make parse_cmd.c norme conform * Make free_tokens() norme conform * Make collect_assigns.c norme conform * Some refactoring of collect_redirs * Refactor collect_redirs
Diffstat (limited to 'src/collect_redirs.c')
-rw-r--r--src/collect_redirs.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
new file mode 100644
index 0000000..76b08da
--- /dev/null
+++ b/src/collect_redirs.c
@@ -0,0 +1,80 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* collect_redirs.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
+/* Updated: 2024/08/02 15:21:04 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+static void collect_redir(t_token **tokens, t_redirection *result,
+ t_token *cur);
+static void set_redir(t_redirection *redir, int type, char *specifier);
+static int is_output_redir(int i);
+
+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_redir(tokens, result, cur);
+ else if (cur->type == REDIR_TOKEN)
+ {
+ dbg("TODO: Add parsing errmsg");
+ return (free(result), NULL);
+ }
+ else
+ cur = cur->next;
+ }
+ return (result);
+}
+
+static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur)
+{
+ set_redir(&result[is_output_redir(cur->content.redir_type)],
+ cur->content.redir_type, cur->next->content.string);
+ cur = cur->next;
+ free_token_and_connect(cur->previous);
+ if (cur->next != NULL)
+ {
+ if (cur->previous == NULL)
+ *tokens = cur->next;
+ cur = cur->next;
+ free_token_and_connect(cur->previous);
+ }
+ else
+ free_token(cur);
+}
+
+static void set_redir(t_redirection *redir, int type, char *specifier)
+{
+ redir->type = type;
+ redir->specifier = specifier;
+}
+
+static int is_output_redir(int i)
+{
+ if (i & (INPUT_FILE | INPUT_LIMITER))
+ return (0);
+ else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
+ return (1);
+ else
+ {
+ panic(UNREACHABLE);
+ return (-1);
+ }
+}