Skip to content

Commit

Permalink
Merge pull request #71 from facetint/facetint
Browse files Browse the repository at this point in the history
Facetint
  • Loading branch information
facetint authored Mar 28, 2024
2 parents 4fc856b + ec276c2 commit 52e6c99
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 137 deletions.
37 changes: 5 additions & 32 deletions src/expander/expander.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* expander.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:17:39 by facetint #+# #+# */
/* Updated: 2024/03/26 11:21:04 by hamza ### ########.fr */
/* Updated: 2024/03/28 17:01:30 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,14 +18,6 @@
#include "../../includes/env.h"
#include <stdio.h>

/**
* replaces the string at input with replacement.
*
* @param input input string
* @param p_start start index of the string to replace (placeholder start index)
* @param p_len length of the string to replace (placeholder length)
* @param replacement replacement string
*/
char *replace_string(char *input, int p_start, int p_len, char *replacement)
{
char *head;
Expand All @@ -43,20 +35,6 @@ char *replace_string(char *input, int p_start, int p_len, char *replacement)
return result;
}

/**
* expands the variable at index in input.
*
* Example:
* $USER = "bsq"
* input = "hello $USER"
* index = 6
* returns -> 8
*
* @param input input string
* @param index index of the variable (must point to '$')
* @return length of string before variable + expanded variable length - 1
* this return value points to where you should continue for next variable.
*/
int expand_variable(char **input, int index)
{
char *str;
Expand Down Expand Up @@ -112,14 +90,12 @@ void expand_string(char **string)
}
*string = str;
}
/*
* Expandable variable but its name length is zero.
* Example: $"USER" -> USER
*/

int is_empty_variable(t_token *token)
{
return ft_strcmp(token->value, "$") == 0 && token->next && is_word(token->next->type);
}

void expand(t_token **head)
{
t_token *token;
Expand All @@ -129,15 +105,12 @@ void expand(t_token **head)
next_ptr = head;
while (token)
{
/* only unquoted word and double-quoted word tokens are expandable. */
if (token->type == UNQUOTED_WORD || token->type == DOUBLE_QUOTED_WORD)
{
/* do not expand single $ */
if (is_empty_variable(token)) {
token->value = ft_strdup("");
} else {
expand_string(&token->value);
/* only unquoted words are not protected for the split */
if (token->type == UNQUOTED_WORD)
internal_field_split(next_ptr);
}
Expand All @@ -154,7 +127,7 @@ void internal_field_split(t_token **token_ptr)

new_words = str_split(token->value, is_internal_field_sep);
if (str_arr_size(new_words) == 1)
return; /* there is no new word */
return ;
safe_free(token->value);
insert_uword_tokens(token_ptr, new_words);
safe_free(new_words);
Expand Down
19 changes: 1 addition & 18 deletions src/expander/expander_nonvariables.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:17:36 by facetint #+# #+# */
/* Updated: 2024/03/14 16:06:57 by facetint ### ########.fr */
/* Updated: 2024/03/28 17:01:01 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,23 +17,6 @@
#include "../../memory-allocator/allocator.h"
#include <stdio.h>

/**
* Removes the token from the list and inserts unquoted word tokens in its place.
*
* Example:
* k l m
* assume we have: UW D UW D UW
* and we call this function with the token 'l'
* with the strings: {"a", "b", "c", "d"}
*
* a b c d
* 1. a token list will be created: UW D UW D UW D UW
* 2. the token 'l' will be removed.
* 3. the token list will be inserted in place of the removed token.
* k a b c d m
* Result: UW D UW D UW D UW D UW D UW
* ~~~~~~~~~~~~~~~~~
*/
void insert_uword_tokens(t_token **token_ptr, char **strings)
{
t_token *token;
Expand Down
25 changes: 1 addition & 24 deletions src/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:17:52 by facetint #+# #+# */
/* Updated: 2024/03/16 21:27:15 by facetint ### ########.fr */
/* Updated: 2024/03/28 16:58:20 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,11 +16,6 @@
#include "../../includes/utils.h"
#include "../../includes/char_classification.h"

/**
* get token type from meta character
* @param input the input string
* @return the token type
*/
t_token_type get_meta_token_type(const char *input)
{
if (!input || !*input)
Expand Down Expand Up @@ -71,24 +66,6 @@ lexer_state word_state(t_token **lexer_data, char *input, int *const index)
return ((lexer_state) delimiter_state);
}

/**
* state for operators
* Example:
* cmd arg1 | cmd2
* ^
* cmd arg1 > outfile
* ^
* cmd < infile
* ^
* cmd << heredoc
* ^
* cmd arg1 >> outfile
* ^
* @param lexer_data the lexer data to append new token
* @param input the input string
* @param index the index of the first char of the a operator
* @return the next state
*/
lexer_state operator_state_l(t_token **lexer_data, char *input, int *const index)
{
int length;
Expand Down
9 changes: 2 additions & 7 deletions src/lexer/syntax_analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* syntax_analyzer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:18:35 by facetint #+# #+# */
/* Updated: 2024/03/14 22:01:21 by hamza ### ########.fr */
/* Updated: 2024/03/28 16:58:07 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,11 +22,6 @@ int is_word(t_token_type type)
type == DOUBLE_QUOTED_WORD;
}

/**
* check if the token is an operator (redirection operator or pipe)
* @param type the token type
* @return 1 if the token is an operator, 0 otherwise
*/
int is_operator(t_token_type type)
{
return type == HEREDOC_REDIRECTION ||
Expand Down
8 changes: 1 addition & 7 deletions src/lexer/unquote.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:18:41 by facetint #+# #+# */
/* Updated: 2024/03/14 21:53:10 by facetint ### ########.fr */
/* Updated: 2024/03/28 16:57:58 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -57,23 +57,17 @@ char *escaped_strdup(char *str)
return result;
}

/**
* @brief remove quotes and backslashes of words from the lexer data
* @param lexer_data the lexer data to unquote
*/
void unquote(t_token *lexer_data)
{
char *unquoted_value;
while (lexer_data)
{
/* remove quotes */
if (lexer_data->type == SINGLE_QUOTED_WORD || lexer_data->type == DOUBLE_QUOTED_WORD)
{
unquoted_value = ft_substr(lexer_data->value, 1, ft_strlen(lexer_data->value) - 2);
safe_free(lexer_data->value);
lexer_data->value = unquoted_value;
}
/* remove backlashes */
if(lexer_data->type == UNQUOTED_WORD || lexer_data->type == DOUBLE_QUOTED_WORD)
{
unquoted_value = escaped_strdup(lexer_data->value);
Expand Down
53 changes: 16 additions & 37 deletions src/lexer/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hcoskun <hcoskun@student.42.fr> +#+ +:+ +#+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:18:44 by facetint #+# #+# */
/* Updated: 2024/03/23 13:33:29 by hcoskun ### ########.fr */
/* Updated: 2024/03/28 16:53:49 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,7 +18,8 @@
#include "../../includes/char_classification.h"
#include "../../memory-allocator/allocator.h"

char *ft_str_arr_join(char **str_list, unsigned int str_count) {
char *ft_str_arr_join(char **str_list, unsigned int str_count)
{
unsigned int total_len;
unsigned int result_len;
unsigned int i;
Expand Down Expand Up @@ -63,7 +64,8 @@ char *get_cur_folder_name()
return result;
}

int skip_white_spaces(const char *str) {
int skip_white_spaces(const char *str)
{
int i;

if (!str)
Expand All @@ -74,13 +76,8 @@ int skip_white_spaces(const char *str) {
return i;
}

/**
* @brief counts the length of the string until the first invalid char.
* @param str the string to count
* @param is_valid function which returns 1 if the char is valid, 0 otherwise.
* @return length of the string until the first invalid char. (if str is NULL, return -1)
*/
int count_len(const char *str, int (*is_valid)(char)) {
int count_len(const char *str, int (*is_valid)(char))
{
int i;

if (!str)
Expand All @@ -91,16 +88,6 @@ int count_len(const char *str, int (*is_valid)(char)) {
return i;
}

/**
* @brief check is a char is escaped or not.
*
* we just check if there is a backslash before the char but we
* have to ensure that the backslash is not escaped.
*
* @param input the input string
* @param index the index of the char to check
* @return 1 if the char is escaped, 0 otherwise.
*/
int is_escaped(char *input, unsigned int index)
{
if (index == 0)
Expand All @@ -114,17 +101,8 @@ int is_escaped(char *input, unsigned int index)
return 0;
}

/**
* @brief finds char in string and return index of char.
* and ignores escaped chars.
*
* it used to get index distance of any searched char.
* e.g. if we find a quote, and we want to know how many
* chars we should skip to find the next quote.
*
* @return index of char if found, -1 otherwise.
*/
int find_char(const char *str, char looking_for) {
int find_char(const char *str, char looking_for)
{
int i;

if (!str)
Expand All @@ -147,20 +125,21 @@ int str_arr_size(char **strings)
return i;
}

int is_a_name_char(char c) {
int is_a_name_char(char c)
{
return is_name_char(c);
}

// here is 1000-point question. do you see different behavior than the bash ?
int is_internal_field_sep(char *str, int index) {
int is_internal_field_sep(char *str, int index)
{
if (is_escaped(str, index))
return 0;
if (is_field_terminator(str[index]))
return 1;
return 0;
}

char *get_prompt()
char *get_prompt()
{
return ft_str_arr_join((char *[]){ft_itoa(*get_exit_status()), " > "}, 2);
/*char *path;
Expand All @@ -177,7 +156,7 @@ char *get_prompt()
return prompt;*/
}

void unexpected_token_error(t_token *token)
void unexpected_token_error(t_token *token)
{
if (token == NULL)
return ft_putstr_fd("syntax error occurred, null token found.\n", 2);
Expand Down
14 changes: 2 additions & 12 deletions src/splitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* splitter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hamza <hamza@student.42.fr> +#+ +:+ +#+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:18:32 by facetint #+# #+# */
/* Updated: 2024/03/26 05:55:36 by hamza ### ########.fr */
/* Updated: 2024/03/28 16:59:30 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,16 +45,6 @@ static int word_count(char const *str, int(*is_delimiter)(char *, int))
return (count);
}

/**
* @brief a string split function which allows you to specify a delimiter function.
* to provide advanced splitting.
*
* Used for word splitting. See manual: 3.5.7 Word Splitting
*
* @param str string to split
* @param is_delimiter function which returns 1 if the char at index is a delimiter
* @return array of split strings
*/
char **str_split(char const *str, int(*is_delimiter)(char *, int))
{
char **result;
Expand Down

0 comments on commit 52e6c99

Please sign in to comment.