Browse Source

some more norminage

master
narnaud 3 years ago
parent
commit
fecf54d470
  1. 2
      Makefile
  2. 74
      lexer.c
  3. 20
      minishell.h
  4. 53
      utils_2.c

2
Makefile

@ -1,6 +1,6 @@
NAME = minishell NAME = minishell
LIBFT = libft.a LIBFT = libft.a
SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c errors.c SRCS = minishell.c lexer.c parser.c caller.c built-in.c env.c utils.c utils_2.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

74
lexer.c

@ -15,6 +15,7 @@
int create_token(t_lexer *lex, char str[]) int create_token(t_lexer *lex, char str[])
{ {
t_token *tok; t_token *tok;
t_token *tmp;
t_type type; t_type type;
type = lex->type; type = lex->type;
@ -23,22 +24,18 @@ int create_token(t_lexer *lex, char str[])
else else
lex->type = WORD; lex->type = WORD;
lex->next_type = WORD; lex->next_type = WORD;
//if (!str[0]) if (!str[0])
// return (0); return (0);
tok = lex->tokens; tok = ft_calloc(1, sizeof *tok);
if (!lex->tokens)
tok = ft_calloc(1, sizeof *tok);
else
{
while (tok->next)
tok = tok->next;
tok->next = ft_calloc(1, sizeof *tok);
tok = tok->next;
}
tok->type = type; tok->type = type;
tok->value = ft_strdup(str); tok->value = ft_strdup(str);
if (!lex->tokens) tmp = lex->tokens;
while (tmp && tmp->next)
tmp = tmp->next;
if (!tmp)
lex->tokens = tok; lex->tokens = tok;
else
tmp->next = tok;
ft_bzero(str, 1024); ft_bzero(str, 1024);
return (1); return (1);
} }
@ -92,57 +89,6 @@ int check_register(t_lexer *lex, char **line, char *tmp)
return (create_token(lex, tmp)); return (create_token(lex, tmp));
} }
char *get_var_value(t_datas *datas, char **line, int *name_len)
{
int i;
char **env;
char *name;
char *value;
i = 1;
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++;
name = ft_substr(*line, 1, i - 1);
i = 0;
*name_len = ft_strlen(name);
value = NULL;
while (datas->envp[i] && !value)
{
env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(name, env[0], *name_len + 1))
value = (free(env[0]), env[1]);
else if (++i)
ft_free_split(env);
}
if (!datas->envp[i])
value = strdup("");
else
free(env);
return (value);
}
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
int i;
char *value;
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
}
else
value = get_var_value(datas, line, &name_len);
i = 0;
while (value[i])
tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0;
*line += name_len + 1;
free(value);
return (tmp_i);
}
int check_state(t_lexer *lex, char **line) int check_state(t_lexer *lex, char **line)
{ {
t_state new; t_state new;

20
minishell.h

@ -46,16 +46,18 @@ typedef struct s_datas
char **envp; char **envp;
int exit_code; int exit_code;
} t_datas; } t_datas;
// ----------------------------------Errors.c
int file_error(char *cmd, char *file, char *msg);
// ----------------------------------Utils.c // ----------------------------------Utils.c
int is_empty(char *line); int is_empty(char *line);
void halt(int ret_code);
void sigs_handler(int sig_num);
void nothing(int sig_num);
void handle_status(t_datas *datas, int status); void handle_status(t_datas *datas, int status);
void nothing(int sig_num);
void sigs_handler(int sig_num);
void halt(int ret_code);
// ----------------------------------Utils_2.c
int replace_var(t_datas *datas, char **line, \
char *tmp, int tmp_i);
int file_error(char *cmd, char *file, char *msg);
// ----------------------------------Builtins.c // ----------------------------------Builtins.c
int close_minishell(int exit_code); int close_minishell(int exit_code);
int ft_echo(t_command *cmd); int ft_echo(t_command *cmd);
@ -92,10 +94,13 @@ typedef struct s_token
t_type type; t_type type;
struct s_token *next; struct s_token *next;
} t_token; } t_token;
char *expend_str(t_datas *datas, char *line); char *expend_str(t_datas *datas, char *line);
size_t count_arguments(t_token *tok); size_t count_arguments(t_token *tok);
void update_redir(t_datas *datas, t_command *cmd, t_token *tok); void update_redir(t_datas *datas, t_command *cmd,\
t_command *parser(t_datas *datas, t_token *tok, t_command *prev); t_token *tok);
t_command *parser(t_datas *datas, t_token *tok,\
t_command *prev);
// ----------------------------------Lexer.c // ----------------------------------Lexer.c
typedef enum e_state typedef enum e_state
@ -117,7 +122,6 @@ typedef struct s_lex
int create_token(t_lexer *lex, char str[]); int create_token(t_lexer *lex, char str[]);
int check_register(t_lexer *lex, char **line, char *tmp); int check_register(t_lexer *lex, char **line, char *tmp);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i);
int check_state(t_lexer *lex, char **line); int check_state(t_lexer *lex, char **line);
t_token *lexer(t_datas *datas, char *line); t_token *lexer(t_datas *datas, char *line);

53
errors.c → utils_2.c

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* errors.c :+: :+: :+: */ /* utils_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
@ -23,3 +23,54 @@ int file_error(char *cmd, char *file, char *msg)
ft_putchar_fd('\n', 2); ft_putchar_fd('\n', 2);
return (0); return (0);
} }
char *get_var_value(t_datas *datas, char **line, int *name_len)
{
int i;
char **env;
char *name;
char *value;
i = 1;
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++;
name = ft_substr(*line, 1, i - 1);
i = 0;
*name_len = ft_strlen(name);
value = NULL;
while (datas->envp[i] && !value)
{
env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(name, env[0], *name_len + 1))
value = (free(env[0]), env[1]);
else if (++i)
ft_free_split(env);
}
if (!datas->envp[i])
value = strdup("");
else
free(env);
return (value);
}
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
int i;
char *value;
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
}
else
value = get_var_value(datas, line, &name_len);
i = 0;
while (value[i])
tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0;
*line += name_len + 1;
free(value);
return (tmp_i);
}
Loading…
Cancel
Save