-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: merge issue * resolve merge conflict with local * fix: merge conflict * merge confict * fix: merge conflict * fix: merge conflict * feat: finish semicolon and pipe handler as well as execution part of it * feat: added enviroment variables * feat: finished execution handler, builtins, prompts, signals * chore: change name of the prompt in _main.c and deleted _pipe_handlers.c * fix: segfault in case of empty line * feat: finish basic shell
- Loading branch information
Showing
47 changed files
with
1,499 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
### SIMPLE SHELL PROJECT | ||
<img src="https://media.tenor.com/D6Ty69OAGyQAAAAd/computer-terminal.gif" alt="shell"> | ||
#### SIMPLE SHELL PROJECTS |
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,30 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _atoi - function that convert | ||
* string into integer | ||
* | ||
* @str: to be converted | ||
* Return: converted integer | ||
*/ | ||
int _atoi(const char *str) | ||
{ | ||
int number; | ||
int sign; | ||
|
||
sign = 1; | ||
number = 0; | ||
if (*str == '-') | ||
{ | ||
sign = -1; | ||
str++; | ||
} | ||
if (*str == '+') | ||
str++; | ||
while (*str) | ||
{ | ||
number = number * 10 + (*str - 48); | ||
str++; | ||
} | ||
return (number * sign); | ||
} |
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,79 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _cd_helper - function helper of cd | ||
* that takes key and look it up | ||
* in enviroments variable and change | ||
* directory to it | ||
* | ||
* @key: envs keys | ||
* Return: 0 success otherwise error | ||
*/ | ||
int _cd_helper(const char *key) | ||
{ | ||
char *s, buffer[200]; | ||
|
||
getcwd(buffer, 200); | ||
s = _enviroment_management(GET_VALUE, key, NULL); | ||
if (chdir(s) == -1) | ||
{ | ||
free(s); | ||
return (errno); | ||
} | ||
_enviroment_management(SET_ENTRY, "OLDPWD", buffer); | ||
free(s); | ||
return (0); | ||
} | ||
|
||
/** | ||
* _cd_helper2 - function that takes | ||
* path as parameter and change | ||
* directory to it | ||
* | ||
* @path: path to change directory to it | ||
* Return: 0 on success ortherwise error | ||
*/ | ||
int _cd_helper2(const char *path) | ||
{ | ||
char buffer[200]; | ||
|
||
getcwd(buffer, 200); | ||
if (chdir(path) == -1) | ||
{ | ||
perror(_global_states(GET_SHELL_NAME, NULL)); | ||
return (errno); | ||
} | ||
_enviroment_management(SET_ENTRY, "OLDPWD", buffer); | ||
return (0); | ||
} | ||
|
||
/** | ||
* _cd - builtin function cd | ||
* is a function that allows as | ||
* to navigate through out different | ||
* folders (directories) in our operating | ||
* system | ||
* | ||
* @command: struct the stores information | ||
* about passed commands | ||
* Return: (0) success otherwise errors | ||
*/ | ||
int _cd(command_t *command) | ||
{ | ||
int len; | ||
|
||
len = _str2dlen(command->arguments + 1); | ||
|
||
if (len >= 1) | ||
{ | ||
if (_strcmp("-", command->arguments[1])) | ||
return (_cd_helper("OLDPWD")); | ||
else if (_strcmp("~", command->arguments[1])) | ||
return (_cd_helper("HOME")); | ||
else | ||
return (_cd_helper2(command->arguments[1])); | ||
} | ||
else if (!len) | ||
return (_cd_helper("HOME")); | ||
return (0); | ||
} |
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,99 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _free_command - function that frees command | ||
* | ||
* @data: data to be freed | ||
* Return: Nothing | ||
*/ | ||
void _free_command(void *data) | ||
{ | ||
command_t *cmd; | ||
|
||
cmd = data; | ||
_free_split(&cmd->arguments); | ||
free(data); | ||
} | ||
|
||
/** | ||
* _lookup_for_command - function that search | ||
* for given command on the builtins | ||
* as well as in the path | ||
* | ||
* @command: to lookup for | ||
* @type: the type of the command | ||
* Return: proper path or command if it's builtin | ||
*/ | ||
char *_lookup_for_command(char *command, command_type_t *type) | ||
{ | ||
if (_builtin_management(GET_BUILTIN, command, NULL)) | ||
{ | ||
*type = BUILTINS; | ||
return (_strdup(command)); | ||
} | ||
return (_get_command_from_path(command)); | ||
} | ||
/** | ||
* _init_command - function that initialize our | ||
* command | ||
* | ||
* @tokens: 2d array holds all command arguments | ||
* and it contain command name in the first argument | ||
* Return: allocated command | ||
*/ | ||
command_t *_init_command(char **tokens) | ||
{ | ||
command_t *command; | ||
struct stat st; | ||
char *scommand; | ||
|
||
command = malloc(sizeof(command_t)); | ||
if (!command) | ||
return (NULL); | ||
command->type = NOT_FOUND; | ||
scommand = _lookup_for_command(tokens[0], &command->type); | ||
free(tokens[0]); | ||
tokens[0] = scommand; | ||
if (command->type == NOT_FOUND && | ||
!stat(tokens[0], &st)) | ||
command->type = EXTERNAL; | ||
command->arguments = tokens; | ||
command->name = tokens[0]; | ||
return (command); | ||
} | ||
/** | ||
* _handle_command - function that takes line | ||
* and turn into an easy command to work with | ||
* | ||
* @line: to be parsed | ||
* Return: well strucered method | ||
*/ | ||
command_t *_handle_command(const char *line) | ||
{ | ||
char *trimmed_line, *command_name; | ||
char **tokens[2]; | ||
int iterator; | ||
|
||
trimmed_line = _trim_white_space(line); | ||
tokens[0] = _split(trimmed_line, " "); | ||
free(trimmed_line); | ||
if (!tokens[0]) | ||
return (NULL); | ||
iterator = 0; | ||
while (tokens[0][iterator]) | ||
{ | ||
if (tokens[0][iterator][0] == '$') | ||
{ | ||
command_name = _evaluate_enviroment_variable(tokens[0][iterator] + 1); | ||
free(tokens[0][iterator]); | ||
if (command_name) | ||
tokens[0][iterator] = command_name; | ||
else | ||
tokens[0][iterator] = _strdup(""); | ||
} | ||
iterator++; | ||
} | ||
tokens[1] = _trim_2darray(tokens[0]); | ||
_free_split(&tokens[0]); | ||
return (_init_command(tokens[1])); | ||
} |
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,43 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _delete_entry - function that removes | ||
* entry from hashmap by given key index | ||
* | ||
* @map: map to delete entry from | ||
* @key: string key of the entry | ||
* Return: (0) success, (1) error | ||
*/ | ||
int _delete_entry(map_t *map, const char *key) | ||
{ | ||
list_t *list, *tmp; | ||
int backet_index; | ||
entry_t *entry; | ||
|
||
backet_index = _get_hash_code(key); | ||
list = map->backets[backet_index]; | ||
if (!list) | ||
return (0); | ||
entry = list->data; | ||
if (_strcmp(entry->key, key)) | ||
{ | ||
map->backets[backet_index] = list->next; | ||
_clear_entry(list->data); | ||
free(list); | ||
return (0); | ||
} | ||
while (list->next) | ||
{ | ||
entry = list->next->data; | ||
if (_strcmp(entry->key, key)) | ||
{ | ||
tmp = list->next; | ||
list->next = list->next->next; | ||
_clear_entry(tmp->data); | ||
free(tmp); | ||
break; | ||
} | ||
list = list->next; | ||
} | ||
return (0); | ||
} |
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,27 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _env - builtin function that print whatever | ||
* inside our enviroment variables | ||
* | ||
* @command: command_t contains informations | ||
* about the user command | ||
* Return: 0 success, 1 error | ||
*/ | ||
int _env(command_t *command) | ||
{ | ||
char **envs, **iterator; | ||
|
||
(void)command; | ||
|
||
iterator = envs = _enviroment_management(CONVERT_INTO_2D, NULL, NULL); | ||
if (!iterator) | ||
return (1); | ||
while (*iterator) | ||
{ | ||
_fprint(1, "%s\n", *iterator); | ||
iterator++; | ||
} | ||
_free_split(&envs); | ||
return (0); | ||
} |
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,26 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _evaluate_enviroment_variable - function | ||
* that takes key and return it's respective | ||
* value | ||
* | ||
* @env_key: key to retrieve it's value from enviroment | ||
* variables | ||
* Return: corresponding value to given key | ||
*/ | ||
char *_evaluate_enviroment_variable(char *env_key) | ||
{ | ||
char *value; | ||
int number; | ||
|
||
if (_strcmp(env_key, "?")) | ||
{ | ||
number = _status_management(GET_STATUS, 0); | ||
return (_itoa(number)); | ||
} | ||
if (_strcmp(env_key, "$")) | ||
return (_itoa(getpid())); | ||
value = _enviroment_management(GET_VALUE, env_key, NULL); | ||
return (value); | ||
} |
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,16 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _exclude_comment - remove comment from | ||
* line | ||
* | ||
* @line: full line that user entered | ||
* Return: new_line without comment | ||
*/ | ||
char *_exclude_comment(const char *line) | ||
{ | ||
int comment_position; | ||
|
||
comment_position = _get_comment_position(line); | ||
return (_strslice(line, 0, comment_position)); | ||
} |
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,30 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _excute - function that excute commands | ||
* that's not part of builtins | ||
* | ||
* @command: command to be executed | ||
* Return: Nothing(void) | ||
*/ | ||
void _excute(command_t *command) | ||
{ | ||
int pid, status; | ||
char **envs; | ||
|
||
pid = fork(); | ||
if (!pid) | ||
{ | ||
envs = _enviroment_management(CONVERT_INTO_2D, NULL, NULL); | ||
execve(command->name, command->arguments, envs); | ||
_free_command(command); | ||
_free_split(&envs); | ||
perror(_global_states(GET_SHELL_NAME, NULL)); | ||
exit(errno); | ||
} | ||
else | ||
{ | ||
waitpid(pid, &status, 0); | ||
_status_management(UPDATE_STATUS, WEXITSTATUS(status)); | ||
} | ||
} |
Oops, something went wrong.