-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
786 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
** EPITECH PROJECT, 2018 | ||
** PSU_42sh_2017 | ||
** File description: | ||
** Include file of the scripting functions | ||
*/ | ||
|
||
#ifndef SCRIPTING_H | ||
#define SCRIPTING_H | ||
|
||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "shell.h" | ||
|
||
#define SH_CHAR(c) (c == '.' || c == 's' || c == 'h') | ||
#define NB_COND (6) | ||
|
||
/* -- Structures definition --- */ | ||
typedef enum keyword_e { | ||
EMPTY, | ||
IF, | ||
ELSE, | ||
ELSIF, | ||
WHILE, | ||
FOREACH, | ||
WHICH, | ||
WHERE | ||
} keyword_t; | ||
|
||
typedef struct cond_script_s { | ||
keyword_t key; | ||
char *condition; | ||
char *command; | ||
bool end; | ||
} cond_t; | ||
|
||
/* --- Check script file --- */ | ||
bool check_script(char *); | ||
bool check_script_access(char *); | ||
bool check_script_name(char *); | ||
bool check_script_shebang(char *); | ||
|
||
/* --- Script running --- */ | ||
char *get_valid_line(FILE *, shell_t *); | ||
char *run_script(shell_t *, FILE *); | ||
FILE *open_script(char *, shell_t *); | ||
bool fill_cond(cond_t *, char *, char **); | ||
int search_keyword(cond_t *, char *); | ||
|
||
/* --- Initialization --- */ | ||
cond_t *init_conditional_line(void); | ||
|
||
/* -- Resources destruction --- */ | ||
void free_cond_line(cond_t *); | ||
|
||
#endif /* !CRIPTING_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
** EPITECH PROJECT, 2018 | ||
** PSU_42sh_2017 | ||
** File description: | ||
** Check script file | ||
*/ | ||
|
||
#include "script.h" | ||
#include <unistd.h> | ||
#include <string.h> | ||
|
||
bool check_script_shebang(char *path) | ||
{ | ||
FILE *fd = fopen(path, "r"); | ||
size_t n = 0; | ||
char *buf = NULL; | ||
bool status = false; | ||
|
||
if (!fd) | ||
return (false); | ||
if (getline(&buf, &n, fd) == -1) | ||
status = false; | ||
else if (buf && !strncmp(buf, "#!", 2)) | ||
status = true; | ||
if (buf) | ||
free(buf); | ||
fclose(fd); | ||
return (status); | ||
} | ||
|
||
bool check_script_name(char *path) | ||
{ | ||
char *tmp = malloc(sizeof(*tmp) * (strlen(path) + 1)); | ||
int j = 0; | ||
|
||
if (!tmp) | ||
return (false); | ||
for (int i = strlen(path) - 1; SH_CHAR(path[i]) && j < 3; i -= 1) { | ||
tmp[j] = path[i]; | ||
j += 1; | ||
} | ||
tmp[j] = '\0'; | ||
if (!strcmp(tmp, "hs.")) { | ||
free(tmp); | ||
return (true); | ||
} | ||
free(tmp); | ||
return (false); | ||
} | ||
|
||
bool check_script_access(char *path) | ||
{ | ||
if (access(path, R_OK) == -1) { | ||
write(1, path, strlen(path)); | ||
write(1, ": Command not found.\n", 21); | ||
return (false); | ||
} | ||
else if (access(path, X_OK) == -1) { | ||
write(1, path, strlen(path)); | ||
write(1, ": Permission denied.\n", 21); | ||
return (false); | ||
} | ||
return (true); | ||
} | ||
|
||
bool check_script(char *path) | ||
{ | ||
if (!path) | ||
return (false); | ||
if (!check_script_name(path)) | ||
return (false); | ||
if (!check_script_access(path)) | ||
return (false); | ||
if (!check_script_shebang(path)) | ||
return (false); | ||
return (true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
** EPITECH PROJECT, 2018 | ||
** PSU_42sh_2017 | ||
** File description: | ||
** Conditional structure initialization | ||
*/ | ||
|
||
#include "script.h" | ||
|
||
static bool copy_command(cond_t *cond, char *line) | ||
{ | ||
int start = 0; | ||
|
||
for (int i = 0; line[i] != '\0'; i += 1) | ||
if (line[i] == ')') | ||
start = i; | ||
if (line[start + 1] == '\0') { | ||
if (cond->key == IF || cond->key == ELSIF) { | ||
puts("if: Empty if."); | ||
return (false); | ||
} | ||
else if (cond->key == ELSE) | ||
return (true); | ||
} | ||
cond->command = malloc(sizeof(char) * (strlen(line) - start)); | ||
if (!cond->command) | ||
return (false); | ||
for (int i = start + 1, j = 0; line[i] != '\0'; i += 1, j += 1) | ||
cond->command[j] = line[i]; | ||
cond->command[strlen(line) - start - 1] = '\0'; | ||
return (true); | ||
} | ||
|
||
static bool search_then(cond_t *cond, char *line, char **words_array) | ||
{ | ||
bool then_exist = false; | ||
|
||
for (int i = 0; words_array[i]; i += 1) | ||
if (!strcmp(words_array[i], "then")) | ||
then_exist = true; | ||
if (!then_exist && cond->key != ELSE) | ||
cond->end = true; | ||
if (!then_exist || cond->key == ELSE) | ||
if (copy_command(cond, line) != true) | ||
return (false); | ||
return (true); | ||
} | ||
|
||
static bool copy_condition(cond_t *cond, char *line, int start, int end) | ||
{ | ||
char **words_array = cut_line(line); | ||
|
||
cond->condition = malloc(sizeof(char) * (end - start)); | ||
if (!cond->condition || !words_array) | ||
return (false); | ||
for (int i = start + 1, j = 0;i < end; i += 1, j += 1) | ||
cond->condition[j] = line[i]; | ||
cond->condition[end - start - 1] = '\0'; | ||
if (cond->key != IF && cond->key != ELSIF && cond->key != ELSE) | ||
if (line[end + 1] != '\0') { | ||
my_putstr(words_array[0]); | ||
puts(": Expression Syntax."); | ||
free_array_string(words_array); | ||
return (false); | ||
} | ||
free_array_string(words_array); | ||
return (true); | ||
} | ||
|
||
static bool check_condition(cond_t *cond, char *line) | ||
{ | ||
int open_bracket = 0; | ||
int close_bracket = 0; | ||
|
||
for (int i = 0; line[i] != '\0'; i += 1) { | ||
if (line[i] == '(') | ||
open_bracket = i; | ||
else if (line[i] == ')') | ||
close_bracket = i; | ||
} | ||
if (!open_bracket || !close_bracket) | ||
return (false); | ||
if (copy_condition(cond, line, open_bracket, close_bracket) != true) | ||
return (false); | ||
return (true); | ||
} | ||
|
||
bool fill_cond(cond_t *cond, char *line, char **words_array) | ||
{ | ||
if (check_condition(cond, line) != true) | ||
return (false); | ||
if (cond->key == IF || cond->key == ELSIF || cond->key == ELSE) | ||
if (search_then(cond, line, words_array) != true) | ||
return (false); | ||
return (true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
** EPITECH PROJECT, 2018 | ||
** PSU_42sh_2017 | ||
** File description: | ||
** Get valid line in the script | ||
*/ | ||
|
||
#include "script.h" | ||
|
||
static char *check_and_rect_line(char *line) | ||
{ | ||
if (!line) | ||
return (NULL); | ||
for (int i = 0; line[i] != '\0'; i += 1) { | ||
if (line[i] == '#' || line[i] == '\n') | ||
line[i] = '\0'; | ||
} | ||
if (!strncmp(line, "#!", 2) || !strcmp(line, "\n") || | ||
!strcmp(line, "")) { | ||
free(line); | ||
line = NULL; | ||
} | ||
return (line); | ||
} | ||
|
||
char *get_valid_line(FILE *fd, shell_t *shell) | ||
{ | ||
char *line = NULL; | ||
size_t n = 0; | ||
|
||
while (!(line = check_and_rect_line(line))) | ||
if (getline(&line, &n, fd) == -1) { | ||
shell->script = false; | ||
fclose(fd); | ||
return (NULL); | ||
} | ||
return (line); | ||
} |
Oops, something went wrong.