changed the split prototypes
This commit is contained in:
parent
4a39152144
commit
a00653bd30
5 changed files with 158 additions and 122 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/22 12:32:02 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/22 15:35:14 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
#include "ast/ast.h"
|
||||
#include "exec/run.h"
|
||||
#include "me/convert/numbers_to_str.h"
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/os/pipe.h"
|
||||
#include "me/os/process.h"
|
||||
|
|
@ -57,6 +58,34 @@ struct s_word_iterator
|
|||
t_state *state;
|
||||
};
|
||||
|
||||
t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out);
|
||||
t_error run_command(t_ast_command *command, t_state *state, t_command_result *out);
|
||||
t_error run_word(t_ast_word *word, t_state *state, t_word_result *out);
|
||||
|
||||
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, void *out);
|
||||
t_error run_case_(t_ast_case *case_, t_state *state, void *out);
|
||||
t_error run_case_item(t_ast_case_item *case_item, t_state *state, void *out);
|
||||
t_error run_command_substitution(t_ast_command_substitution *command_substitution, t_state *state, void *out);
|
||||
t_error run_compound_statement(t_ast_compound_statement *compound_statement, t_state *state, void *out);
|
||||
t_error run_elif(t_ast_elif *elif, t_state *state, void *out);
|
||||
t_error run_else_(t_ast_else *else_, t_state *state, void *out);
|
||||
t_error run_empty(t_ast_empty *empty, t_state *state, void *out);
|
||||
t_error run_extglob(t_ast_extglob *extglob, t_state *state, void *out);
|
||||
t_error run_file_redirection(t_ast_file_redirection *file_redirection, t_state *state, void *out);
|
||||
t_error run_for_(t_ast_for *for_, t_state *state, void *out);
|
||||
t_error run_function_definition(t_ast_function_definition *function_definition, t_state *state, void *out);
|
||||
t_error run_heredoc_redirection(t_ast_heredoc_redirection *heredoc_redirection, t_state *state, void *out);
|
||||
t_error run_if_(t_ast_if *if_, t_state *state, void *out);
|
||||
t_error run_list(t_ast_list *list, t_state *state, void *out);
|
||||
t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, void *out);
|
||||
t_error run_program(t_ast_program *program, t_state *state, void *out);
|
||||
t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out);
|
||||
t_error run_regex(t_ast_regex *regex, t_state *state, void *out);
|
||||
t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out);
|
||||
t_error run_until(t_ast_until *until, t_state *state, void *out);
|
||||
t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out);
|
||||
t_error run_while_(t_ast_while *while_, t_state *state, void *out);
|
||||
|
||||
#ifdef ERROR
|
||||
# undef ERROR
|
||||
#endif
|
||||
|
|
@ -216,63 +245,73 @@ t_error _handle_expansion_operator(t_ast_expansion *self, t_state *state, t_expa
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__raw__no_quote(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str__raw__no_quote(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
t_usize i;
|
||||
bool last_backslash;
|
||||
t_string ret;
|
||||
t_usize i;
|
||||
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_RAW_STRING || elem->data.raw_string.kind != AST_WORD_NO_QUOTE)
|
||||
return (ERROR);
|
||||
i = 0;
|
||||
ret = string_new(elem->data.raw_string.len);
|
||||
last_backslash = false;
|
||||
while (elem->data.raw_string.str[i])
|
||||
{
|
||||
if (elem->data.raw_string.str[i] != '\\')
|
||||
if (elem->data.raw_string.str[i] != '\\' || last_backslash)
|
||||
string_push_char(&ret, elem->data.raw_string.str[i]);
|
||||
last_backslash = false;
|
||||
if (elem->data.raw_string.str[i] == '\\' && !last_backslash)
|
||||
last_backslash = true;
|
||||
i++;
|
||||
}
|
||||
return (vec_str_push(out, ret.buf), NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__raw__single_quote(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str__raw__single_quote(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
t_usize i;
|
||||
t_string ret;
|
||||
t_usize i;
|
||||
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_RAW_STRING || elem->data.raw_string.kind != AST_WORD_SINGLE_QUOTE)
|
||||
return (ERROR);
|
||||
i = 1;
|
||||
ret = string_new(elem->data.raw_string.len);
|
||||
while (elem->data.raw_string.str[i])
|
||||
{
|
||||
if (elem->data.raw_string.str[i] != '\\')
|
||||
string_push_char(&ret, elem->data.raw_string.str[i]);
|
||||
i++;
|
||||
}
|
||||
string_push_char(&ret, elem->data.raw_string.str[i++]);
|
||||
string_pop(&ret);
|
||||
return (vec_str_push(out, ret.buf), NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__raw__double_quote(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str__raw__double_quote(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
t_usize i;
|
||||
bool last_backslash;
|
||||
t_string ret;
|
||||
t_usize i;
|
||||
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_RAW_STRING || elem->data.raw_string.kind != AST_WORD_DOUBLE_QUOTE)
|
||||
return (ERROR);
|
||||
i = 0;
|
||||
ret = string_new(elem->data.raw_string.len);
|
||||
last_backslash = false;
|
||||
if (elem->data.raw_string.str[0] == '"')
|
||||
i++;
|
||||
while (elem->data.raw_string.str[i])
|
||||
{
|
||||
if (elem->data.raw_string.str[i] != '\\')
|
||||
if (elem->data.raw_string.str[i] != '\\' || last_backslash)
|
||||
string_push_char(&ret, elem->data.raw_string.str[i]);
|
||||
last_backslash = false;
|
||||
if (elem->data.raw_string.str[i] == '\\' && !last_backslash)
|
||||
last_backslash = true;
|
||||
i++;
|
||||
}
|
||||
string_pop(&ret);
|
||||
if (elem->data.raw_string.len >= 2 && elem->data.raw_string.str[elem->data.raw_string.len - 1] == '"' &&
|
||||
elem->data.raw_string.str[elem->data.raw_string.len - 2] == '\\')
|
||||
string_pop(&ret);
|
||||
return (vec_str_push(out, ret.buf), NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__raw(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str__raw(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_RAW_STRING)
|
||||
return (ERROR);
|
||||
|
|
@ -285,28 +324,57 @@ t_error _ast_get_str__raw(t_ast_node elem, t_state *state, t_vec_str *out)
|
|||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__expansion(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _split_ifs_expansion(t_ast_word_kind wkind, t_state *state, t_ast_expansion *exp, t_vec_str *out)
|
||||
{
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_EXPANSION)
|
||||
t_str ifs;
|
||||
const t_str ifs_key = "IFS";
|
||||
t_expansion_result exp_out;
|
||||
|
||||
if (exp == NULL || out == NULL)
|
||||
return (ERROR);
|
||||
return (ERROR);
|
||||
ifs = NULL;
|
||||
if (hmap_env_get(state->env, (t_str *)&ifs_key))
|
||||
ifs = *hmap_env_get(state->env, (t_str *)&ifs_key);
|
||||
if (ifs == NULL)
|
||||
ifs = " \t\n";
|
||||
if (run_expansion(exp, state, &exp_out))
|
||||
return (ERROR);
|
||||
if (wkind != AST_WORD_NO_QUOTE)
|
||||
{
|
||||
if (exp_out.exists && exp_out.value != NULL)
|
||||
vec_str_push(out, str_clone(exp_out.value));
|
||||
return (NO_ERROR);
|
||||
}
|
||||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__arimethic_expansion(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str__expansion(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
t_expansion_result exp_ret;
|
||||
t_vec_str words;
|
||||
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_EXPANSION)
|
||||
return (ERROR);
|
||||
if (run_expansion(&elem->data.expansion, state->state, &exp_ret))
|
||||
return (ERROR);
|
||||
return (vec_str_push(out, exp_ret.value), NO_ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__arimethic_expansion(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_ARITHMETIC_EXPANSION)
|
||||
return (ERROR);
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str__command_substitution(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str__command_substitution(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
if (elem == NULL || state == NULL || out == NULL || elem->kind != AST_COMMAND_SUBSTITUTION)
|
||||
return (ERROR);
|
||||
return (ERROR);
|
||||
}
|
||||
|
||||
t_error _ast_get_str(t_ast_node elem, t_state *state, t_vec_str *out)
|
||||
t_error _ast_get_str(t_ast_node elem, t_word_iterator *state, t_vec_str *out)
|
||||
{
|
||||
if (elem == NULL || state == NULL || out == NULL)
|
||||
return (ERROR);
|
||||
|
|
@ -328,7 +396,7 @@ void _run_word_into_str(t_usize idx, t_ast_node *elem, t_word_iterator *state)
|
|||
(void)(idx);
|
||||
if (elem == NULL || *elem == NULL || state == NULL)
|
||||
return;
|
||||
if (_ast_get_str(*elem, state->state, &state->res.value))
|
||||
if (_ast_get_str(*elem, state, &state->res.value))
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,17 @@
|
|||
#ifndef TREE_SITTER_API_H_
|
||||
#define TREE_SITTER_API_H_
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* api.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/22 13:54:54 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/22 13:55:02 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef API_H
|
||||
#define API_H
|
||||
|
||||
#include "me/types.h"
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/19 23:30:46 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/11 19:00:41 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/22 15:10:45 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,6 +14,7 @@
|
|||
# define STR_H
|
||||
|
||||
# include "me/types.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
/// @brief Get the length of a string
|
||||
/// @param str the string
|
||||
/// @return the length of the string
|
||||
|
|
@ -102,9 +103,10 @@ t_str str_map(t_const_str s, char (*f)(t_usize, char));
|
|||
|
||||
/// @brief Split a string into a vector of strings
|
||||
/// @param str the string to be split
|
||||
/// @param chr the character to split the string on
|
||||
/// @return the vector of strings
|
||||
t_str *str_split(t_const_str str, char chr);
|
||||
/// @param chr the list of chars to be used as word delimiter
|
||||
/// @param out[out] the returned value, a vector of words
|
||||
/// @return True in case of error, false otherwise
|
||||
t_error str_split(t_const_str str, t_const_str chrs, t_vec_str *out);
|
||||
|
||||
/// @brief Remove consecutive leading and trailing characters from a string
|
||||
/// @param str the string to trim
|
||||
|
|
@ -112,4 +114,13 @@ t_str *str_split(t_const_str str, char chr);
|
|||
/// @return the trimmed string
|
||||
t_str str_trim(t_const_str str, t_const_str charset);
|
||||
|
||||
/// @brief Remove consecutive leading and trailing characters from a string
|
||||
/// @param str the string to free
|
||||
static inline void str_free(t_str str)
|
||||
{
|
||||
void mem_free(void *ptr);
|
||||
|
||||
mem_free(str);
|
||||
}
|
||||
|
||||
#endif /* STR_H */
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/07/10 18:04:36 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/22 15:29:22 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/os/process.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/printf/printf.h"
|
||||
#include "me/os/pipe.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/str/str.h"
|
||||
|
|
@ -52,31 +53,24 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
|||
return (NO_ERROR);
|
||||
}
|
||||
|
||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
||||
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
|
||||
t_string *s)
|
||||
{
|
||||
t_str *splitted_path;
|
||||
t_usize sp_index;
|
||||
t_vec_str path;
|
||||
t_usize idx;
|
||||
|
||||
splitted_path = str_split(path + 5, ':');
|
||||
if (splitted_path == NULL)
|
||||
(void)(process);
|
||||
if (str_split(path_raw + 5, ":", &path))
|
||||
return (string_free(*s), ERROR);
|
||||
sp_index = 0;
|
||||
while (splitted_path[sp_index])
|
||||
idx = 0;
|
||||
while (idx < path.len)
|
||||
{
|
||||
((void)(process), string_clear(s));
|
||||
string_push(s, splitted_path[sp_index]);
|
||||
string_push(s, "/");
|
||||
string_push(s, info->binary_path);
|
||||
sp_index++;
|
||||
string_clear(s);
|
||||
me_printf_str(s, "%s/%s", path.buffer[idx++], info->binary_path);
|
||||
if (access(s->buf, X_OK | R_OK) == 0)
|
||||
break ;
|
||||
return (vec_str_free(path), NO_ERROR);
|
||||
}
|
||||
sp_index = 0;
|
||||
while (splitted_path[sp_index])
|
||||
mem_free(splitted_path[sp_index++]);
|
||||
mem_free(splitted_path);
|
||||
return (NO_ERROR);
|
||||
return (vec_str_free(path), ERROR);
|
||||
}
|
||||
|
||||
t_error find_binary(t_spawn_info *info, t_process *process)
|
||||
|
|
|
|||
|
|
@ -6,87 +6,38 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/05/14 18:44:18 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/07/22 15:11:14 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/mem/mem.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/str/str.h"
|
||||
#include "me/string/string.h"
|
||||
#include "me/vec/vec_str.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static t_usize local_count_words(t_const_str str, char chr);
|
||||
static t_str *local_split_inner(t_const_str str, char chr, t_str *out);
|
||||
static t_str *local_split_freeall(t_str **to_free);
|
||||
|
||||
static t_usize local_count_words(t_const_str str, char chr)
|
||||
t_error str_split(t_const_str str, t_const_str chr, t_vec_str *out)
|
||||
{
|
||||
t_usize i;
|
||||
t_usize out;
|
||||
t_vec_str ret;
|
||||
t_usize idx;
|
||||
t_string buf;
|
||||
|
||||
out = 0;
|
||||
i = 0;
|
||||
while (str[i])
|
||||
if (out == NULL || chr == NULL || str == NULL)
|
||||
return (ERROR);
|
||||
idx = 0;
|
||||
buf = string_new(16);
|
||||
ret = vec_str_new(16, str_free);
|
||||
while (str[idx] != '\0')
|
||||
{
|
||||
while (str[i] && str[i] == chr)
|
||||
i++;
|
||||
if (str[i] == 0)
|
||||
return (out);
|
||||
out++;
|
||||
while (str[i] && str[i] != chr)
|
||||
i++;
|
||||
while (str[idx] != '\0' && str_find_chr(chr, str[idx]) != NULL)
|
||||
idx++;
|
||||
while (str[idx] != '\0' && str_find_chr(chr, str[idx]) == NULL)
|
||||
string_push_char(&buf, str[idx]);
|
||||
if (buf.len != 0)
|
||||
{
|
||||
vec_str_push(&ret, buf.buf);
|
||||
buf = string_new(16);
|
||||
}
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
static t_str *local_split_freeall(t_str **to_free)
|
||||
{
|
||||
while (*to_free)
|
||||
mem_free(*(to_free++));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static t_str *local_split_inner(t_const_str str, char chr, t_str *out)
|
||||
{
|
||||
t_usize str_i;
|
||||
t_usize sub_i;
|
||||
t_usize ptr_i;
|
||||
|
||||
str_i = 0;
|
||||
ptr_i = 0;
|
||||
while (str[str_i])
|
||||
{
|
||||
while (str[str_i] && str[str_i] == chr)
|
||||
str_i++;
|
||||
if (str[str_i] == 0)
|
||||
break ;
|
||||
sub_i = 0;
|
||||
while (str[str_i + sub_i] && str[str_i + sub_i] != chr)
|
||||
sub_i++;
|
||||
out[ptr_i] = mem_alloc(sizeof(char) * (sub_i + 1));
|
||||
if (out[ptr_i] == NULL)
|
||||
return (local_split_freeall(&out));
|
||||
str_l_copy(out[ptr_i++], (t_str)(str + str_i), sub_i + 1);
|
||||
str_i += sub_i;
|
||||
}
|
||||
out[ptr_i] = NULL;
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_str *str_split(t_const_str str, char chr)
|
||||
{
|
||||
t_usize ptr_len;
|
||||
t_str *out;
|
||||
|
||||
if (str == NULL || *str == 0)
|
||||
{
|
||||
out = mem_alloc(sizeof(t_str) * 1);
|
||||
*out = NULL;
|
||||
return (out);
|
||||
}
|
||||
ptr_len = local_count_words(str, chr);
|
||||
out = mem_alloc_array(sizeof(t_str), (ptr_len + 1));
|
||||
if (out == NULL)
|
||||
return (NULL);
|
||||
return (local_split_inner(str, chr, out));
|
||||
string_free(buf);
|
||||
return (*out = ret, NO_ERROR);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue