Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Benoit
minishell
Commits
8271eb44
Commit
8271eb44
authored
Jan 27, 2021
by
pêle-mêle
Browse files
complete norm for parsing functions
parent
a13320af
Changes
8
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
8271eb44
...
...
@@ -11,7 +11,8 @@ SRC += check_syntax.c
SRC
+=
parsing_handler.c
SRC
+=
parsing_states1.c
SRC
+=
parsing_states2.c
SRC
+=
parse_utils.c
SRC
+=
parsing_vars_redirs.c
SRC
+=
parsing_utils.c
SRC
+=
exec_handler.c
...
...
inc/parsing.h
View file @
8271eb44
...
...
@@ -14,7 +14,7 @@
# define S_APPEND 0x40
# define S_CMDSEP 0x80
enum
e_state
enum
e_state
{
SPACE
,
LETTER
,
...
...
@@ -29,7 +29,7 @@ enum e_state
NB_STATES
};
enum
e_parsing_error
enum
e_parsing_error
{
SQUOTE_MISSING
=
11
,
DQUOTE_MISSING
,
...
...
@@ -40,7 +40,7 @@ enum e_parsing_error
NB_PARSING_ERRORS
};
typedef
struct
s_state_machine
typedef
struct
s_state_machine
{
char
buf
[
BUF_SIZE
];
size_t
len
;
...
...
@@ -57,49 +57,58 @@ typedef struct s_state_machine
t_byte
var_state
;
t_byte
quote_state
;
uint8_t
pad
[
5
];
}
t_state_machine
;
}
t_state_machine
;
typedef
char
*
(
*
t_parse
)(
t_state_machine
*
,
char
*
);
/*
** check_syntax.c
*/
int
parsing_error
(
int
err_code
,
t_xe
*
xe
);
int
check_syntax
(
char
*
line
);
int
parsing_error
(
int
err_code
,
t_xe
*
xe
);
int
check_syntax
(
char
*
line
);
/*
** main functions
*/
t_command
*
parse_one_command
(
t_xe
*
xe
);
int
parse_input
(
char
**
line
,
char
**
env
,
int
stat_loc
,
t_command
*
command
);
t_command
*
parse_one_command
(
t_xe
*
xe
);
int
parse_input
(
char
**
line
,
char
**
env
,
int
stat_loc
,
t_command
*
command
);
/*
** parsing states
*/
char
*
space
(
t_state_machine
*
machine
,
char
*
line
);
char
*
letter
(
t_state_machine
*
machine
,
char
*
line
);
char
*
backslash
(
t_state_machine
*
machine
,
char
*
line
);
char
*
dollar
(
t_state_machine
*
machine
,
char
*
line
);
char
*
single_quote
(
t_state_machine
*
machine
,
char
*
line
);
char
*
double_quote
(
t_state_machine
*
machine
,
char
*
line
);
char
*
quoted_backslash
(
t_state_machine
*
machine
,
char
*
line
);
char
*
quoted_dollar
(
t_state_machine
*
machine
,
char
*
line
);
char
*
angle_bracket
(
t_state_machine
*
machine
,
char
*
line
);
char
*
space
(
t_state_machine
*
machine
,
char
*
line
);
char
*
letter
(
t_state_machine
*
machine
,
char
*
line
);
char
*
backslash
(
t_state_machine
*
machine
,
char
*
line
);
char
*
dollar
(
t_state_machine
*
machine
,
char
*
line
);
char
*
single_quote
(
t_state_machine
*
machine
,
char
*
line
);
char
*
double_quote
(
t_state_machine
*
machine
,
char
*
line
);
char
*
quoted_backslash
(
t_state_machine
*
machine
,
char
*
line
);
char
*
quoted_dollar
(
t_state_machine
*
machine
,
char
*
line
);
char
*
angle_bracket
(
t_state_machine
*
machine
,
char
*
line
);
/*
** variables
*/
char
*
parse_variable
(
t_state_machine
*
machine
,
char
*
line
);
int
parse_exit_status
(
t_state_machine
*
machine
);
char
*
parse_quoted_variable
(
t_state_machine
*
machine
,
char
*
line
);
/*
** redirs
*/
char
*
new_redir_info
(
t_state_machine
*
machine
,
char
*
line
);
/*
** utils
*/
int
new_command
(
t_list
**
commands
);
char
*
new_redir_info
(
t_state_machine
*
machine
,
char
*
line
);
int
add_variable
(
t_list
**
var_list
,
size_t
start
,
size_t
len
,
t_byte
split_flag
);
int
parse_exit_status
(
t_state_machine
*
machine
);
char
*
parse_quoted_variable
(
t_state_machine
*
machine
,
char
*
line
);
char
*
parse_variable
(
t_state_machine
*
machine
,
char
*
line
);
int
add_arg
(
t_state_machine
*
machine
);
int
reset_buf
(
t_state_machine
*
machine
);
int
add_to_buf
(
t_state_machine
*
machine
,
char
c
);
int
add_to_buf
(
t_state_machine
*
machine
,
char
c
);
int
reset_buf
(
t_state_machine
*
machine
);
int
add_arg
(
t_state_machine
*
machine
);
#endif
libft/Makefile
View file @
8271eb44
...
...
@@ -6,7 +6,7 @@
# By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/12/23 13:17:34 by mvidal-a #+# #+# #
# Updated: 2021/01/2
6
1
2:41:03
by mvidal-a ### ########.fr #
# Updated: 2021/01/2
7
1
1:54:52
by mvidal-a ### ########.fr #
# #
# **************************************************************************** #
...
...
@@ -74,6 +74,7 @@ SRCS_CUSTOM += ft_swap.c
SRCS_CUSTOM
+=
strjoin_free.c
SRCS_CUSTOM
+=
ft_varnamelen.c
SRCS_CUSTOM
+=
blank_str.c
SRCS_CUSTOM
+=
push_int_to_array.c
VPATH
=
part_1:part_2:bonus:custom
...
...
libft/custom/push_int_to_array.c
0 → 100644
View file @
8271eb44
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_int_to_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 11:53:41 by mvidal-a #+# #+# */
/* Updated: 2021/01/27 11:53:57 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int
*
push_int_to_array
(
int
*
array
,
size_t
array_size
,
int
nb
)
{
int
*
new_array
;
size_t
i
;
new_array
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
(
array_size
+
1
));
if
(
new_array
==
NULL
)
return
(
NULL
);
i
=
0
;
while
(
i
<
array_size
)
{
new_array
[
i
]
=
array
[
i
];
i
++
;
}
new_array
[
i
]
=
nb
;
free
(
array
);
return
(
new_array
);
}
libft/libft.h
View file @
8271eb44
...
...
@@ -6,7 +6,7 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/04 12:29:06 by mvidal-a #+# #+# */
/* Updated: 2021/01/2
6
1
2:18:43
by mvidal-a ### ########.fr */
/* Updated: 2021/01/2
7
1
1:54:32
by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
...
...
@@ -118,5 +118,6 @@ char **push_str_to_array(char **array, char *str);
char
**
rm_str_from_array
(
char
**
array
,
size_t
str_pos
);
void
sort_str_array
(
char
**
array
);
int
*
push_int_to_array
(
int
*
array
,
size_t
array_size
,
int
nb
);
#endif
notes.txt
View file @
8271eb44
...
...
@@ -20,7 +20,6 @@ then "ls"
masaki:
add_to_buf malloc error!
...
...
src/parsing/parsing_utils.c
0 → 100644
View file @
8271eb44
#include "parsing.h"
int
add_arg
(
t_state_machine
*
machine
)
{
if
(
reset_buf
(
machine
)
==
FAILURE
)
return
(
FAILURE
);
if
(
machine
->
cur_token_stack
==
&
machine
->
redir_paths
&&
((
machine
->
var_token_count
>=
1
&&
machine
->
cur_arg
[
0
]
!=
'\0'
)
||
(
machine
->
var_state
==
TRUE
&&
machine
->
var_token_count
==
0
&&
machine
->
cur_arg
[
0
]
==
'\0'
)))
{
machine
->
redir_types
[
0
]
=
AMBIG
;
}
else
if
(
machine
->
quote_state
==
TRUE
||
machine
->
cur_arg
[
0
]
!=
'\0'
)
{
*
machine
->
cur_token_stack
=
push_str_to_array
(
*
machine
->
cur_token_stack
,
machine
->
cur_arg
);
if
(
*
machine
->
cur_token_stack
==
NULL
)
return
(
FAILURE
);
machine
->
cur_arg
=
NULL
;
}
machine
->
quote_state
=
FALSE
;
return
(
SUCCESS
);
}
int
reset_buf
(
t_state_machine
*
machine
)
{
if
(
machine
->
cur_arg
==
NULL
)
machine
->
cur_arg
=
ft_strdup
(
machine
->
buf
);
else
if
(
machine
->
len
!=
0
)
machine
->
cur_arg
=
strjoin_free
(
machine
->
cur_arg
,
machine
->
buf
);
if
(
machine
->
cur_arg
==
NULL
)
return
(
FAILURE
);
if
(
machine
->
len
!=
0
)
{
machine
->
len
=
0
;
ft_bzero
(
&
machine
->
buf
,
BUF_SIZE
);
}
return
(
SUCCESS
);
}
int
add_to_buf
(
t_state_machine
*
machine
,
char
c
)
{
if
(
machine
->
len
==
BUF_SIZE
-
1
)
{
if
(
reset_buf
(
machine
)
==
FAILURE
)
return
(
FAILURE
);
}
machine
->
buf
[
machine
->
len
]
=
c
;
machine
->
len
++
;
return
(
SUCCESS
);
}
void
free_command
(
void
*
content
)
{
t_command
*
command
;
command
=
(
t_command
*
)
content
;
free_str_array
(
command
->
args
);
free_str_array
(
command
->
redir_paths
);
free
(
command
->
redir_types
);
free
(
command
);
}
void
free_commands
(
t_list
**
commands
)
{
ft_lstclear
(
commands
,
free_command
);
}
src/parsing/pars
e_util
s.c
→
src/parsing/pars
ing_vars_redir
s.c
View file @
8271eb44
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_vars_redirs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 12:14:56 by mvidal-a #+# #+# */
/* Updated: 2021/01/27 12:18:48 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int
*
push_int_to_array
(
in
t
*
array
,
size_t
array_size
,
int
nb
)
static
int
add_var_args
(
t_state_mach
in
e
*
machine
,
char
*
var_value
)
{
int
*
new_array
;
size_t
i
;
new_array
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
(
array_size
+
1
));
if
(
new_array
==
NULL
)
return
(
NULL
);
i
=
0
;
while
(
i
<
array_size
)
if
(
ft_isspace
(
*
var_value
)
&&
machine
->
cur_arg
[
0
]
!=
'\0'
)
{
new_array
[
i
]
=
array
[
i
];
i
++
;
if
(
add_arg
(
machine
)
==
FAILURE
)
return
(
FAILURE
);
machine
->
var_token_count
++
;
var_value
++
;
}
new_array
[
i
]
=
nb
;
free
(
array
);
return
(
new_array
);
while
(
ft_isspace
(
*
var_value
))
var_value
++
;
while
(
*
var_value
!=
'\0'
)
{
if
(
ft_isspace
(
*
var_value
))
{
if
(
add_arg
(
machine
)
==
FAILURE
)
return
(
FAILURE
);
machine
->
var_token_count
++
;
if
(
blank_str
(
var_value
)
==
TRUE
)
break
;
while
(
ft_isspace
(
*
var_value
))
var_value
++
;
}
if
(
add_to_buf
(
machine
,
*
var_value
++
)
==
FAILURE
)
return
(
FAILURE
);
}
return
(
SUCCESS
);
}
char
*
new_redir_info
(
t_state_machine
*
machine
,
char
*
line
)
char
*
parse_variable
(
t_state_machine
*
machine
,
char
*
line
)
{
enum
e_redir_op
redir_type
;
size_t
var_name_len
;
char
*
var_value
;
if
(
*
line
==
'<'
)
redir_type
=
FILEIN
;
else
if
(
line
[
1
]
==
'>'
)
if
(
reset_buf
(
machine
)
==
FAILURE
)
return
(
NULL
);
var_name_len
=
ft_varnamelen
(
line
);
var_value
=
get_var_value
(
machine
->
env
,
line
,
var_name_len
);
if
(
var_value
==
NULL
)
return
(
NULL
);
if
(
add_var_args
(
machine
,
var_value
)
==
FAILURE
)
{
re
dir_type
=
APPEND
;
line
++
;
f
re
e
(
var_value
)
;
return
(
NULL
)
;
}
else
redir_type
=
FILEOUT
;
machine
->
redir_types
=
(
enum
e_redir_op
*
)
push_int_to_array
((
int
*
)
machine
->
redir_types
,
ft_arraylen
(
machine
->
redir_paths
),
(
int
)
redir_type
);
free
(
var_value
);
line
+=
var_name_len
;
machine
->
var_state
=
TRUE
;
return
(
line
);
}
int
parse_exit_status
(
t_state_machine
*
machine
)
int
parse_exit_status
(
t_state_machine
*
machine
)
{
char
*
value
;
size_t
i
;
...
...
@@ -60,7 +85,7 @@ int parse_exit_status(t_state_machine *machine)
return
(
SUCCESS
);
}
char
*
parse_quoted_variable
(
t_state_machine
*
machine
,
char
*
line
)
char
*
parse_quoted_variable
(
t_state_machine
*
machine
,
char
*
line
)
{
size_t
var_name_len
;
char
*
var_value
;
...
...
@@ -85,127 +110,21 @@ char *parse_quoted_variable(t_state_machine *machine, char *line)
return
(
line
);
}
//int add_var_arg(t_state_machine *machine)
//{
//}
char
*
parse_variable
(
t_state_machine
*
machine
,
char
*
line
)
char
*
new_redir_info
(
t_state_machine
*
machine
,
char
*
line
)
{
size_t
var_name_len
;
char
*
var_value
;
size_t
i
;
enum
e_redir_op
redir_type
;
if
(
reset_buf
(
machine
)
==
FAILURE
)
return
(
NULL
);
var_name_len
=
ft_varnamelen
(
line
);
var_value
=
get_var_value
(
machine
->
env
,
line
,
var_name_len
);
if
(
var_value
==
NULL
)
return
(
NULL
);
i
=
0
;
if
(
ft_isspace
(
var_value
[
i
])
&&
machine
->
cur_arg
[
0
]
!=
'\0'
)
{
if
(
add_arg
(
machine
)
==
FAILURE
)
{
free
(
var_value
);
return
(
NULL
);
}
machine
->
var_token_count
++
;
i
++
;
}
while
(
ft_isspace
(
var_value
[
i
]))
i
++
;
while
(
var_value
[
i
]
!=
'\0'
)
if
(
*
line
==
'<'
)
redir_type
=
FILEIN
;
else
if
(
line
[
1
]
==
'>'
)
{
if
(
ft_isspace
(
var_value
[
i
]))
{
if
(
add_arg
(
machine
)
==
FAILURE
)
{
free
(
var_value
);
return
(
NULL
);
}
machine
->
var_token_count
++
;
i
++
;
// optional
if
(
blank_str
(
var_value
+
i
)
==
TRUE
)
break
;
while
(
ft_isspace
(
var_value
[
i
]))
i
++
;
}
if
(
add_to_buf
(
machine
,
var_value
[
i
])
==
FAILURE
)
{
free
(
var_value
);
return
(
NULL
);
}
i
++
;
redir_type
=
APPEND
;
line
++
;
}
free
(
var_value
);
line
+=
var_name_len
;
machine
->
var_state
=
TRUE
;
else
redir_type
=
FILEOUT
;
machine
->
redir_types
=
(
enum
e_redir_op
*
)
push_int_to_array
((
int
*
)
machine
->
redir_types
,
ft_arraylen
(
machine
->
redir_paths
),
(
int
)
redir_type
);
return
(
line
);
}
int
add_arg
(
t_state_machine
*
machine
)
{
if
(
reset_buf
(
machine
)
==
FAILURE
)
return
(
FAILURE
);
if
(
machine
->
cur_token_stack
==
&
machine
->
redir_paths
&&
((
machine
->
var_token_count
>=
1
&&
machine
->
cur_arg
[
0
]
!=
'\0'
)
||
(
machine
->
var_state
==
TRUE
&&
machine
->
var_token_count
==
0
&&
machine
->
cur_arg
[
0
]
==
'\0'
)))
{
machine
->
redir_types
[
0
]
=
AMBIG
;
}
else
if
(
machine
->
quote_state
==
TRUE
||
machine
->
cur_arg
[
0
]
!=
'\0'
)
{
*
machine
->
cur_token_stack
=
push_str_to_array
(
*
machine
->
cur_token_stack
,
machine
->
cur_arg
);
if
(
*
machine
->
cur_token_stack
==
NULL
)
return
(
FAILURE
);
machine
->
cur_arg
=
NULL
;
}
machine
->
quote_state
=
FALSE
;
return
(
SUCCESS
);
}
int
reset_buf
(
t_state_machine
*
machine
)
{
if
(
machine
->
cur_arg
==
NULL
)
machine
->
cur_arg
=
ft_strdup
(
machine
->
buf
);
// on a la len de buf
else
if
(
machine
->
len
!=
0
)
machine
->
cur_arg
=
strjoin_free
(
machine
->
cur_arg
,
machine
->
buf
);
// idem
if
(
machine
->
cur_arg
==
NULL
)
return
(
FAILURE
);
if
(
machine
->
len
!=
0
)
{
machine
->
len
=
0
;
ft_bzero
(
&
machine
->
buf
,
BUF_SIZE
);
}
return
(
SUCCESS
);
}
int
add_to_buf
(
t_state_machine
*
machine
,
char
c
)
{
if
(
machine
->
len
==
BUF_SIZE
-
1
)
{
if
(
reset_buf
(
machine
)
==
FAILURE
)
return
(
FAILURE
);
}
machine
->
buf
[
machine
->
len
]
=
c
;
machine
->
len
++
;
return
(
SUCCESS
);
}
void
free_command
(
void
*
content
)
{
t_command
*
command
;
command
=
(
t_command
*
)
content
;
free_str_array
(
command
->
args
);
free_str_array
(
command
->
redir_paths
);
free
(
command
->
redir_types
);
free
(
command
);
}
void
free_commands
(
t_list
**
commands
)
{
ft_lstclear
(
commands
,
free_command
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment