Skip to content

Commit

Permalink
Refactor/fix: rename a function, add 'CTRL + L' key management
Browse files Browse the repository at this point in the history
  • Loading branch information
SizzleUnrlsd committed Jul 26, 2023
1 parent e0b66bd commit 97997a4
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 24 deletions.
2 changes: 1 addition & 1 deletion include/basic_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
void parent_exit(shell_t *shell, int32_t wstatus);
char **parse_stdin(char *command, shell_t *shell);
char **cut_path_env(shell_t *shell, char **array);
bool format_getline(shell_t *shell, char **command);
bool char_stream_formatting(shell_t *shell, char **command);
void execute_command(char **arg, shell_t *shell);
int32_t access_file(shell_t *shell, char **path);
int32_t open_directory(shell_t *shell, char **path, char **arg);
Expand Down
122 changes: 122 additions & 0 deletions src/prompt_function/prompt_engine/char_stream_formatting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* Copyright (C) 2023 hugo
*
* This file is part of TekSH.
*
* TekSH is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TekSH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TekSH. If not, see <http://www.gnu.org/licenses/>.
*/

#include "shell.h"

int32_t
set_error_message(shell_t *shell, char first_char, char special_char, char *context)
{
uint32_t len_context = _strlen(context);

if (special_char == '|' || first_char == '|') {
EXIT_W_ECHO_ERROR_("Invalid null command.", 1);
}
if (first_char == '&') {
EXIT_W_ECHO_ERROR_("Invalid null command.", 1);
}
if (context[len_context - 1] == '&' && context[len_context - 2] == '&') {
EXIT_W_ECHO_ERROR_("Invalid null command.", 1);
}
if (special_char == '>' || special_char == '<'
|| first_char == '>' || first_char == '<') {
EXIT_W_ECHO_ERROR_("Missing name for redirect.", 1);
}
return 0;
}

int32_t
check_the_validity_of_brackets(char *command, shell_t *shell)
{
uint32_t count_open_brackets = DEFAULT(count_open_brackets);
uint32_t count_closing_brackets = DEFAULT(count_closing_brackets);

for (uint32_t i = 0; command[i]; i++) {
if (command[i] == '(')
++count_open_brackets;
if (command[i] == ')')
++count_closing_brackets;
}
if (count_open_brackets > count_closing_brackets) {
EXIT_W_ECHO_ERROR_("teksh : syntax error near `('", 1);
} else if (count_open_brackets < count_closing_brackets) {
EXIT_W_ECHO_ERROR_("teksh : syntax error near `)'", 1);
}
return 0;
}

char *
formatting_brackets(char *command)
{
int length = strlen(command);
int newLength = length;
char *result = NULL;
int j = 0;

for (int i = 0; i < length; i++) {
if (command[i] == '(' || command[i] == ')') {
newLength++; // adding 1 for each space after '(' or before ')'
}
}

result = (char *)malloc(newLength + 1); // allocating memory for the new string

for (int i = 0; i < length; i++) {
if (command[i] == '(') {
result[j++] = command[i];
result[j++] = ' ';
} else if (command[i] == ')') {
result[j++] = ' ';
result[j++] = command[i];
} else {
result[j++] = command[i];
}
}

result[j] = '\0';
return result;

}

bool
char_stream_formatting(shell_t *shell, char **command)
{
unsigned char first_char = DEFAULT(first_char);
unsigned char last_char = DEFAULT(last_char);

remove_backslash_n(*command);
delete_spaces_tabulations(*command);
replace_multiple_spaces_tabulations(*command);
del_space_end_str(*command);
(*command) = formatting_brackets(*command);
delete_semicolon_end(*command);
first_command_is_misplaced(*command, &first_char);
last_character_not_space_or_tabulation(*command, &last_char);
if (check_the_validity_of_brackets(*command, shell))
return true;

if (_strcmp(*command, "&&") == 0)
return true;
if (set_error_message(shell, first_char, last_char, *command)
|| find_shell_inconsistency(*command, shell)
|| check_the_validity_of_brackets(*command, shell))
return true;
if (*command == NULL || *command[0] == '\0')
return true;
return false;
}
35 changes: 12 additions & 23 deletions src/prompt_function/prompt_engine/prompt_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@
#include "shell.h"

volatile sig_atomic_t interrupt_flag = DEFAULT(interrupt_flag);

static volatile sig_atomic_t keep_running = 1;

static bool _tty = false;

bool current_process = false;

char *detail = DEFAULT(detail);

char _gbuf[256] = {0};
shell_t *_gshell;

uint64_t count_readline;

int load_history(const char *filename)
{
Expand All @@ -41,20 +39,8 @@ int save_history(const char *filename)
return write_history(filename);
}

char *
get_dir(void)
{
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
return strdup(cwd);
} else {
return "UNDEFINE";
}

return 0;
}

static char *concat_str(const char *str1, const char *str2, ...)
static char *
concat_str(const char *str1, const char *str2, ...)
{
va_list args;
size_t len1 = strlen(str1);
Expand Down Expand Up @@ -90,11 +76,11 @@ char *set_promt(void)
char prompt[512] = {0};
char *_detail = DEFAULT(_detail);
char *reset = "\033[0m";
char start = '{';
char end = '}';
char start = '[';
char end = ']';

if (strcmp("UNDEFINE", getgit_branch()) != 0)
sprintf(prompt, "\033[1;34m%c \033[1;31m%s\033[0m \033[1;34m%c %s", start, getgit_branch(), end, reset);
sprintf(prompt, "\033[0;34m%c\033[1;31m%s\033[0m\033[0;34m%c %s", start, getgit_branch(), end, reset);
else
sprintf(prompt, " ");

Expand All @@ -108,9 +94,9 @@ void inthand(int32_t signum UNUSED_ARG)
char _buf[256] = {0};
sprintf(_buf, "%s%s%s", "\x1b[1m", ANSI_COLOR_BLUE "•" ANSI_COLOR_RESET, detail);


if (signum == SIGINT && _tty && current_process == false) {
/* Handles the case where the prompt buffer is full */
count_readline++;
rl_free_line_state();
rl_replace_line("", 0);
rl_crlf();
Expand Down Expand Up @@ -145,6 +131,7 @@ prompt_shell(shell_t *shell)
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);

_tty = shell->print;
_gshell = shell;

signal(SIGINT, inthand);
signal(SIGTSTP, inthand);
Expand Down Expand Up @@ -181,10 +168,12 @@ prompt_shell(shell_t *shell)
}
/* In the tty */
if (_tty) {
rl_bind_key(12, clear_wrapper);
rl_completion_display_matches_hook = my_completion_display_matches;
if (line = readline(_gbuf), !line) {
return 42;
}
count_readline++;
}

garbage_collector(line, shell);
Expand Down

0 comments on commit 97997a4

Please sign in to comment.