Skip to content

Commit

Permalink
Merge pull request #64 from JopjeKnopje/parser-fixes
Browse files Browse the repository at this point in the history
Parser fixes
  • Loading branch information
JopjeKnopje authored Feb 5, 2024
2 parents 9ecc87d + 8fabff3 commit 7b0a7a4
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 49 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ SRCS := execute/error.c \
plarser/expander_utils.c \
plarser/expander_utils2.c \
plarser/space_count.c \
plarser/heredoc_expander.c \
utils/utils_string.c \
utils/utils_path.c \
redirections/redirections.c \
Expand Down
4 changes: 3 additions & 1 deletion include/expander.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/12/12 18:45:58 by jboeve #+# #+# */
/* Updated: 2023/12/14 02:12:08 by joppe ######## odam.nl */
/* Updated: 2024/02/04 21:14:50 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -35,8 +35,10 @@ char *ex_find_var(char **envp, char *name, size_t len);
char *ex_str_append(char *s_base, char *s_append, size_t append_size);

// expander.c
size_t ex_expand_var(char **envp, t_token *t, size_t i, char **s_exp);
size_t ex_var_len(char *s);
char *ex_str_append(char *s_base, char *s_append, size_t append_size);
char *expand_var(char **envp, t_token *t, size_t i);
char *ex_expand_var_block(char **envp, t_token *t);

#endif
6 changes: 3 additions & 3 deletions include/heredoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/06 15:31:17 by ivan-mel #+# #+# */
/* Updated: 2023/12/23 00:07:50 by joppe ######## odam.nl */
/* Updated: 2024/02/03 22:08:01 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,8 +41,8 @@ t_hd_list *hd_lstnew(int fd);

// heredoc.c
bool contains_heredoc(t_cmd_list *cmds);
int handle_heredoc(t_cmd_frame *f, int *status);
int handle_heredoc(t_meta *meta, t_cmd_frame *f, int *status);
t_hd_list *append_heredoc(t_hd_list **head, int heredoc_fd);
t_hd_list *run_heredocs(t_cmd_list *cmds);
t_hd_list *run_heredocs(t_meta *meta, t_cmd_list *cmds);

#endif
8 changes: 4 additions & 4 deletions include/megashell.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* megashell.h :+: :+: :+: */
/* megashell.h :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/19 11:40:37 by jboeve #+# #+# */
/* Updated: 2023/12/11 16:44:46 by ivan-mel ### ########.fr */
/* Updated: 2024/02/04 02:11:28 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -53,7 +53,7 @@ char *prompt_get_line(void);
bool hs_read_history_file(char *s);
bool hs_add_history_file(char *s, char *line);

// aliases.c
void aliases_init(t_meta *meta);
// heredoc_expander.c
char *hd_exp(t_meta *meta, char *s);

#endif
3 changes: 2 additions & 1 deletion include/plarser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/14 16:40:07 by jboeve #+# #+# */
/* Updated: 2023/12/23 00:13:40 by joppe ######## odam.nl */
/* Updated: 2024/02/04 02:10:35 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -102,6 +102,7 @@ void pr_parse_tokenless(t_cmd_frame *frame, const t_token *next);
// lexer.c
t_tok_list *lx_list_add_token(t_tok_list **token_lst, t_token t);
t_tok_list *lx_main(char *s);
void lx_trim_space(char **cursor);

// lexer_utils.c
bool lx_is_metachar(char c);
Expand Down
6 changes: 4 additions & 2 deletions src/builtins/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef int (*t_builtin_func)(t_meta *meta, t_cmd_frame *cmd);
Expand Down Expand Up @@ -82,15 +83,16 @@ int run_builtin(t_builtin builtin, t_meta *meta, t_cmd_frame *cmd)
fd = -1;
if (cmd->heredoc_delim)
{
fd = handle_heredoc(cmd, &exit_status);
fd = handle_heredoc(meta, cmd, &exit_status);
if (fd == -1)
return (INTERNAL_FAILURE);
}
if (builtin == BUILTIN_EXIT)
return (get_builtin_func(builtin)(meta, cmd));
fds[PIPE_WRITE] = dup(STDOUT_FILENO);
fds[PIPE_READ] = dup(STDIN_FILENO);
redirections(cmd, fd);
if (!redirections(cmd, fd))
return (print_error(strerror(errno)));
exit_status = (get_builtin_func(builtin)(meta, cmd));
if (!dup_stdin(fds[PIPE_READ]))
print_error("error dup_stdin\n");
Expand Down
2 changes: 1 addition & 1 deletion src/execute/pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int pipeline_start(t_meta *meta, t_cmd_list *cmds)
t_cmd_list *const cmds_head = cmds;
int last_exit;

heredoc_pipes = run_heredocs(cmds);
heredoc_pipes = run_heredocs(meta, cmds);
if (contains_heredoc(cmds))
{
if (!heredoc_pipes)
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ int main(int argc, char *argv[], char *envp[])
(void) argc;
(void) argv;
return (megashell(envp));
return 0;
}
2 changes: 1 addition & 1 deletion src/megashell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 15:45:41 by joppe #+# #+# */
/* Updated: 2024/01/17 11:16:08 by jboeve ######## odam.nl */
/* Updated: 2024/02/04 21:17:18 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down
20 changes: 13 additions & 7 deletions src/plarser/expander.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 23:35:50 by joppe #+# #+# */
/* Updated: 2023/12/22 14:39:28 by jboeve ######## odam.nl */
/* Updated: 2024/02/04 19:12:53 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,7 @@
#include "libft.h"
#include "megashell.h"
#include "plarser.h"
#include "test_utils.h"
#include "utils.h"
#include <limits.h>
#include <readline/readline.h>
Expand All @@ -25,7 +26,7 @@
#include <unistd.h>
#include "execute.h"

static size_t ex_expand_var(char **envp, t_token *t, size_t i, char **s_exp)
size_t ex_expand_var(char **envp, t_token *t, size_t i, char **s_exp)
{
char *var;
size_t len;
Expand All @@ -51,7 +52,8 @@ static size_t ex_expand_var(char **envp, t_token *t, size_t i, char **s_exp)
*s_exp = ex_str_append(*s_exp, var, ft_strlen(var));
if (is_exit_code)
free(var);
return (((!(*s_exp)) != 0) * 0 + ((!(!(*s_exp)) != 0) + 1));
int32_t ret = (((!(*s_exp)) != 0) * 0 + ((!(!(*s_exp)) != 0) + 1));
return ret;
}

char *ex_expand_var_block(char **envp, t_token *t)
Expand All @@ -67,10 +69,7 @@ char *ex_expand_var_block(char **envp, t_token *t)
{
i += ex_expand_var(envp, t, i, &s_exp);
if (!s_exp)
{
free(s_exp);
return (NULL);
}
}
i++;
}
Expand Down Expand Up @@ -103,6 +102,7 @@ char *expand_var(char **envp, t_token *t, size_t i)
t_token ex_expand_quote_block(char **envp, t_token *t)
{
t_exp exp;
t_token ret_tok;

exp = ex_pack_struct(t, envp);
ex_step_into_quote(exp.t);
Expand All @@ -116,7 +116,9 @@ t_token ex_expand_quote_block(char **envp, t_token *t)
if (exp.end == exp.t->content + exp.t->content_len)
break ;
}
return (lx_token_set(TOKEN_ALLOC, exp.s_exp, ft_strlen(t->content)));
ret_tok = lx_token_set(TOKEN_ALLOC, exp.s_exp, ft_strlen(exp.s_exp));
ret_tok.padding = t->padding;
return (ret_tok);
}

bool ex_main(char **envp, t_tok_list *tokens)
Expand All @@ -126,6 +128,9 @@ bool ex_main(char **envp, t_tok_list *tokens)
while (tokens)
{
t = &tokens->token;
if ((tokens->prev && tokens->prev->token.kind == TOKEN_HEREDOC) \
&& (t->kind == TOKEN_QUOTE_DOUBLE || t->kind == TOKEN_QUOTE_SINGLE || t->kind == TOKEN_BLOCK_DOLLAR))
ex_step_into_quote(t);
if (t->kind == TOKEN_QUOTE_DOUBLE)
{
tokens->token = ex_expand_quote_block(envp, t);
Expand All @@ -138,6 +143,7 @@ bool ex_main(char **envp, t_tok_list *tokens)
{
if (!ex_expand_var_block(envp, t))
return (false);

}
tokens = tokens->next;
}
Expand Down
8 changes: 4 additions & 4 deletions src/plarser/expander_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/12/12 18:46:47 by jboeve #+# #+# */
/* Updated: 2023/12/13 01:13:11 by joppe ######## odam.nl */
/* Updated: 2024/01/19 15:09:09 by jboeve ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -17,10 +17,10 @@

static int ft_abs(int x)
{
const uint32_t temp = x >> 31;
const uint32_t tmp = x >> 31;

x ^= temp;
x += temp & 1;
x ^= tmp;
x += tmp & 1;
return (x);
}

Expand Down
5 changes: 3 additions & 2 deletions src/plarser/expander_utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/12/14 01:44:03 by joppe #+# #+# */
/* Updated: 2023/12/14 01:58:27 by joppe ######## odam.nl */
/* Updated: 2024/02/04 16:21:09 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

#include "plarser.h"
#include <stdio.h>

size_t get_key_len(char *key)
{
Expand Down Expand Up @@ -81,8 +82,8 @@ void ex_step_into_quote(t_token *t)
{
t->content++;
t->content_len -= 2;
t->kind = TOKEN_TEXT;
if (t->content_len == 0)
t->padding = 0;
}
t->kind = TOKEN_TEXT;
}
55 changes: 55 additions & 0 deletions src/plarser/heredoc_expander.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* heredoc_expander.c :+: :+: */
/* +:+ */
/* By: joppe <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/02/03 22:09:57 by joppe #+# #+# */
/* Updated: 2024/02/04 21:17:01 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

#include "execute.h"
#include "expander.h"
#include "libft.h"
#include "megashell.h"
#include "plarser.h"
#include <stddef.h>
#include "utils.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "test_utils.h"

char *hd_exp(t_meta *meta, char *s)
{
t_token t;
size_t i;
char *s_exp;
char *s_line;

i = 0;
s_line = NULL;
s_exp = NULL;
while (s[i])
{
if (s[i] == '$')
{
t = lx_tokenize_dollar_block((char *) s + i);
i += t.content_len;
if (t.kind != TOKEN_ERROR)
s_exp = ex_expand_var_block(meta->envp, &t);
else
s_exp = "";
s_line = ex_str_append(s_line, s_exp, strlen(s_exp));
free(s_exp);
}
else
{
s_line = ex_str_append(s_line, s + i, 1);
i++;
}
}
return s_line;
}
4 changes: 2 additions & 2 deletions src/plarser/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 16:57:13 by joppe #+# #+# */
/* Updated: 2024/01/16 13:33:14 by jboeve ######## odam.nl */
/* Updated: 2024/02/04 02:10:29 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

#include "plarser.h"
#include <stdio.h>
#include <unistd.h>

static void lx_trim_space(char **cursor)
void lx_trim_space(char **cursor)
{
while (*cursor && (**cursor == ' ' || **cursor == '\t' || **cursor == '\v' || **cursor == '\n'))
{
Expand Down
4 changes: 3 additions & 1 deletion src/plarser/lexer_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ivan-mel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/04 15:11:36 by jboeve #+# #+# */
/* Updated: 2024/01/16 12:37:35 by jboeve ######## odam.nl */
/* Updated: 2024/01/23 19:59:30 by joppe ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -24,6 +24,8 @@ static char get_metachars(int i)
'<',
'>',
' ',
'\'',
'\"',
'\t',
'\v',
'\n',
Expand Down
Loading

0 comments on commit 7b0a7a4

Please sign in to comment.