narnaud
3 years ago
14 changed files with 313 additions and 275 deletions
@ -0,0 +1,109 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* lexer_2.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/05/16 07:53:17 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/05/16 09:20:23 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "minishell.h" |
||||
|
|
||||
|
static char *get_var_value(t_datas *datas, char **line, int name_len); |
||||
|
|
||||
|
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 |
||||
|
{ |
||||
|
name_len = 1; |
||||
|
while (ft_isalpha((*line)[name_len]) \ |
||||
|
|| ft_isdigit((*line)[name_len]) || (*line)[name_len] == '_') |
||||
|
name_len++; |
||||
|
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; |
||||
|
free(value); |
||||
|
return (tmp_i); |
||||
|
} |
||||
|
|
||||
|
char *expend_str(t_datas *datas, char *line) |
||||
|
{ |
||||
|
char *tmp; |
||||
|
int tmp_i; |
||||
|
|
||||
|
tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp)); |
||||
|
if (!tmp) |
||||
|
free(tmp); |
||||
|
tmp_i = 0; |
||||
|
while (*line) |
||||
|
{ |
||||
|
if (*line == '$') |
||||
|
tmp_i = replace_var(datas, &line, tmp, tmp_i); |
||||
|
else |
||||
|
tmp[tmp_i++] = *(line++); |
||||
|
} |
||||
|
return (tmp); |
||||
|
} |
||||
|
|
||||
|
static char *get_var_value(t_datas *datas, char **line, int name_len) |
||||
|
{ |
||||
|
int i; |
||||
|
char **env; |
||||
|
char *name; |
||||
|
char *value; |
||||
|
|
||||
|
name = ft_substr(*line, 1, name_len - 1); |
||||
|
i = 0; |
||||
|
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); |
||||
|
} |
||||
|
free(name); |
||||
|
if (value) |
||||
|
free(env); |
||||
|
else |
||||
|
value = strdup(""); |
||||
|
return (value); |
||||
|
} |
||||
|
|
||||
|
int set_redir(t_lexer *lex, char **line, char ch) |
||||
|
{ |
||||
|
static t_type type_out[2] = {OUT, ADD}; |
||||
|
static t_type type_in[2] = {IN, HD}; |
||||
|
t_type *type; |
||||
|
|
||||
|
if (ch == '>') |
||||
|
type = type_out; |
||||
|
else |
||||
|
type = type_in; |
||||
|
if (**line == ch && (*line)++) |
||||
|
{ |
||||
|
if (**line == ch && (*line)++) |
||||
|
lex->next_type = type[1]; |
||||
|
else |
||||
|
lex->next_type = type[0]; |
||||
|
return (1); |
||||
|
} |
||||
|
return (0); |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
/* ************************************************************************** */ |
||||
|
/* */ |
||||
|
/* ::: :::::::: */ |
||||
|
/* lexer_3.c :+: :+: :+: */ |
||||
|
/* +:+ +:+ +:+ */ |
||||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
||||
|
/* +#+#+#+#+#+ +#+ */ |
||||
|
/* Created: 2022/05/16 09:18:30 by narnaud #+# #+# */ |
||||
|
/* Updated: 2022/05/16 09:56:05 by narnaud ### ########.fr */ |
||||
|
/* */ |
||||
|
/* ************************************************************************** */ |
||||
|
|
||||
|
#include "minishell.h" |
||||
|
|
||||
|
static int wc_str_cmp(char *wc, char *str) |
||||
|
{ |
||||
|
int i; |
||||
|
int j; |
||||
|
int skip ; |
||||
|
|
||||
|
i = 0; |
||||
|
j = 0; |
||||
|
skip = (wc[0] != '.' && str[0] == '.'); |
||||
|
while (wc[i] && !skip) |
||||
|
{ |
||||
|
if (wc[i] == '*' && ++i) |
||||
|
while (str[j] && str[j] != wc[i]) |
||||
|
j++; |
||||
|
else if (str[j] != wc[i]) |
||||
|
skip = 1; |
||||
|
else |
||||
|
{ |
||||
|
i++; |
||||
|
j++; |
||||
|
} |
||||
|
} |
||||
|
if (str[j] != wc[i]) |
||||
|
skip = 1; |
||||
|
return (skip); |
||||
|
} |
||||
|
|
||||
|
int create_wc(t_lexer *lex, char *wc) |
||||
|
{ |
||||
|
DIR *direct; |
||||
|
struct dirent *file; |
||||
|
|
||||
|
direct = opendir("."); |
||||
|
file = (readdir(direct), readdir(direct), readdir(direct)); |
||||
|
while (file) |
||||
|
{ |
||||
|
if (!wc_str_cmp(wc, file->d_name)) |
||||
|
create_token(lex, file->d_name); |
||||
|
file = readdir(direct); |
||||
|
} |
||||
|
closedir(direct); |
||||
|
lex->wc = 0; |
||||
|
return (1); |
||||
|
} |
@ -1,76 +0,0 @@ |
|||||
/* ************************************************************************** */ |
|
||||
/* */ |
|
||||
/* ::: :::::::: */ |
|
||||
/* utils_3.c :+: :+: :+: */ |
|
||||
/* +:+ +:+ +:+ */ |
|
||||
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ |
|
||||
/* +#+#+#+#+#+ +#+ */ |
|
||||
/* Created: 2022/05/12 14:33:58 by mea #+# #+# */ |
|
||||
/* Updated: 2022/05/12 14:51:17 by mea ### ########.fr */ |
|
||||
/* */ |
|
||||
/* ************************************************************************** */ |
|
||||
|
|
||||
#include "minishell.h" |
|
||||
|
|
||||
|
|
||||
|
|
||||
int is_valid_identifier(char *ident) |
|
||||
{ |
|
||||
int i; |
|
||||
|
|
||||
i = 0; |
|
||||
if (ft_isalpha(ident[0]) || ident[0] == '_') |
|
||||
{ |
|
||||
while (ident[i]) |
|
||||
{ |
|
||||
if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i] |
|
||||
&& ident[i] != '_')) |
|
||||
{ |
|
||||
ft_putstr_fd("export: not an identifier: ", 2); |
|
||||
ft_putstr_fd(ident, 2); |
|
||||
ft_putchar_fd('\n', 2); |
|
||||
return (0); |
|
||||
} |
|
||||
i++; |
|
||||
} |
|
||||
return (1); |
|
||||
} |
|
||||
ft_putstr_fd("export: not an identifier: ", 2); |
|
||||
ft_putstr_fd(ident, 2); |
|
||||
ft_putchar_fd('\n', 2); |
|
||||
return (0); |
|
||||
} |
|
||||
|
|
||||
int create_wc(t_lexer *lex, char *tmp) |
|
||||
{ |
|
||||
int i; |
|
||||
int j; |
|
||||
int skip ; |
|
||||
DIR * direct; |
|
||||
struct dirent *file; |
|
||||
|
|
||||
direct = opendir("."); |
|
||||
file = (readdir(direct), readdir(direct), readdir(direct)); |
|
||||
while (file) |
|
||||
{ |
|
||||
i = 0; |
|
||||
j = 0; |
|
||||
skip = (tmp[0] != '.' && file->d_name[0] == '.'); |
|
||||
while (tmp[i] && !skip) |
|
||||
{ |
|
||||
if (tmp[i] == '*') |
|
||||
while (file->d_name[j] && file->d_name[j] != tmp[i + 1]) |
|
||||
j++; |
|
||||
if (tmp[i] != '*' && file->d_name[j] != tmp[i]) |
|
||||
skip = 1; |
|
||||
else if (++i) |
|
||||
++j; |
|
||||
} |
|
||||
if (!skip) |
|
||||
create_token(lex, file->d_name); |
|
||||
file = readdir(direct); |
|
||||
} |
|
||||
closedir(direct); |
|
||||
lex->wc = 0; |
|
||||
return (1); |
|
||||
} |
|
Loading…
Reference in new issue