Skip to content

Commit

Permalink
Merge pull request #61 from ErwannLesech/add_ionumbers
Browse files Browse the repository at this point in the history
feat<ionumbers>: add ionumbers in lexer
  • Loading branch information
ErwannLesech authored Jan 17, 2024
2 parents ef7e1a0 + 9b3c4de commit b0bf768
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 17 deletions.
54 changes: 54 additions & 0 deletions src/execute/redirections.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "../ast/ast.h"
#include "ast_eval.h"

#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>

int redir_output(struct ast_node *node, bool logger_enabled)
{
if (logger_enabled)
{
printf("redir_output\n");
}
int stat = 0;
char *file_name = node->children[node->children_count - 1]->value;
int fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
{
return -1;
}

int stdout_fd = dup(STDOUT_FILENO);
if (stdout_fd == -1)
{
return -1;
}

// boucle pour dup les différents redirs

// mettre à null les nodes redirs et réaranger l'arbre

exec_cmd(node->children[0], false);

close(fd);
int status;
waitpid(pid, &status, 0);
stat = WEXITSTATUS(status);
return stat;
}

int redir_manager(struct ast_node *ast, bool logger_enabled)
{
if (logger_enabled)
{
printf("redir_manager\n");
}
if (ast->children_count < 2)
{
return 0;
}
return 0;
}
14 changes: 14 additions & 0 deletions src/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ char *get_word(struct lexer *lexer, bool *is_diactivated)
word = realloc(word, sizeof(char) * (word_index + 1));
word[word_index] = '\0';

if (is_number(word) && (lexer->data[lexer->index] == '>'
|| lexer->data[lexer->index] == '<' ))
{
lexer->curr_tok.type = TOKEN_IONUMBER;
}

// Skip spaces and tabs
while (lexer->data[lexer->index] == ' '
|| lexer->data[lexer->index] == '\t')
Expand Down Expand Up @@ -250,6 +256,14 @@ struct token parse_input_for_tok(struct lexer *lexer)
return token;
}

if (lexer->curr_tok.type == TOKEN_IONUMBER)
{
token.type = TOKEN_IONUMBER;
token.data = word;
lexer->curr_tok.type = TOKEN_EOL;
return token;
}

// Check if the word is a variable name
if (lexer->curr_tok.type == TOKEN_VARIABLE
|| lexer->curr_tok.type == TOKEN_VARIABLE_AND_DOUBLE_QUOTE)
Expand Down
1 change: 1 addition & 0 deletions src/lexer/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stddef.h>

#include "token.h"
#include "../options/options.h"

/**
* \struct lexer
Expand Down
59 changes: 56 additions & 3 deletions src/lexer/tests/lexer2_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Test(lexer2, token_redir_stick_left)
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_eq(tok.type, TOKEN_IONUMBER);
cr_assert_str_eq(tok.data, "2");
token_free(tok);

Expand Down Expand Up @@ -248,7 +248,7 @@ Test(lexer2, token_redir_stick_left2)
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_eq(tok.type, TOKEN_IONUMBER);
cr_assert_str_eq(tok.data, "2");
token_free(tok);

Expand All @@ -265,6 +265,58 @@ Test(lexer2, token_redir_stick_left2)
lexer_free(lexer);
}

Test(lexer2, token_redir_stick_left_alpha)
{
struct lexer *lexer = lexer_new("echo a>file");
struct token tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_str_eq(tok.data, "echo");
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_str_eq(tok.data, "a");
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_REDIR);
cr_assert_str_eq(tok.data, ">");
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_str_eq(tok.data, "file");
token_free(tok);

lexer_free(lexer);
}

Test(lexer2, token_redir_stick_left_alphanum)
{
struct lexer *lexer = lexer_new("echo 13>file");
struct token tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_str_eq(tok.data, "echo");
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_IONUMBER);
cr_assert_str_eq(tok.data, "13");
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_REDIR);
cr_assert_str_eq(tok.data, ">");
token_free(tok);

tok = lexer_pop(lexer);
cr_assert_eq(tok.type, TOKEN_WORD);
cr_assert_str_eq(tok.data, "file");
token_free(tok);

lexer_free(lexer);
}

Test(lexer2, token_redir_stick_left3)
{
struct lexer *lexer = lexer_new("ls -la >| file");
Expand Down Expand Up @@ -1052,4 +1104,5 @@ Test (lexer2, personalized_variable2)
token_free(tok);

lexer_free(lexer);
}
}

1 change: 1 addition & 0 deletions src/lexer/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum token_type
TOKEN_OR, // ||
TOKEN_PIPE, // |
TOKEN_NEGATE, // \!
TOKEN_IONUMBER, // [0-9]+
TOKEN_REDIR, // >, <, >>, >&, <&, >|, <>
TOKEN_DOUBLE_QUOTE, // "
TOKEN_WORD_ASSIGNMENT, // variable=
Expand Down
13 changes: 13 additions & 0 deletions src/options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "options.h"

#include <ctype.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
Expand All @@ -16,6 +17,18 @@

#include "../ast/ast.h"

int is_number(char *str)
{
int i = 0;
while (str[i])
{
if (!isdigit(str[i]))
return 0;
i++;
}
return 1;
}

bool check_logger(int *argc, char **argv)
{
bool logger_enabled = false;
Expand Down
7 changes: 7 additions & 0 deletions src/options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ void logger(char *str, enum logger_step step, bool logger_enabled);
*/
void pretty_print(struct ast_node *ast, bool pretty_print_enabled, int *depths);

/**
* \brief Check if the given string is a number.
* \param str The string to check.
* \return True if the string is a number, false otherwise.
*/
int is_number(char *str);

#endif /* ! OPTIONS_H */
1 change: 1 addition & 0 deletions src/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../ast/ast.h"
#include "../lexer/lexer.h"
#include "../lexer/token.h"
#include "../options/options.h"

/**
* \brief Parse loop the given lexer
Expand Down
14 changes: 0 additions & 14 deletions src/parser/parser_functionnal.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <ctype.h>

#include "parser.h"

struct ast_node *prefix(struct lexer *lexer)
Expand All @@ -14,18 +12,6 @@ struct ast_node *prefix(struct lexer *lexer)
return redirection(lexer);
}

int is_number(char *str)
{
int i = 0;
while (str[i])
{
if (!isdigit(str[i]))
return 0;
i++;
}
return 1;
}

struct ast_node *redirection(struct lexer *lexer)
{
struct ast_node *current = ast_node_new(AST_REDIRECTION);
Expand Down

0 comments on commit b0bf768

Please sign in to comment.