From fa5e28d0b7375dab7c104a6d2d527fd5f186c015 Mon Sep 17 00:00:00 2001 From: lukas-sgx Date: Fri, 8 May 2026 23:46:45 +0200 Subject: [PATCH 1/2] fix: set error message on failure load plugin --- src/core/context/builtin_loader.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/context/builtin_loader.c b/src/core/context/builtin_loader.c index e437792..a45fc99 100644 --- a/src/core/context/builtin_loader.c +++ b/src/core/context/builtin_loader.c @@ -73,7 +73,8 @@ int plugin_load(main_t *main_stock) full_name = malloc(sizeof(char) * (strlen(names[i]) + 1 + 8)); full_name = strcpy(full_name, "plugins/"); full_name = strcat(full_name, names[i]); - load_one_plugin(main_stock, full_name); + if (load_one_plugin(main_stock, full_name) == CZSH_PLUGIN_ERR) + printf("Can't load plugin: %s\n", full_name); free_alloc(full_name); } free_array(names); From 9cd52a4ab58cc1f92b74e7e9b839db7b80050fd3 Mon Sep 17 00:00:00 2001 From: lukas-sgx Date: Sat, 9 May 2026 01:05:12 +0200 Subject: [PATCH 2/2] feat: implement matrix plugin example --- plugins/example/matrix/include/matrix.h | 15 ++++ plugins/example/matrix/src/matrix.c | 109 ++++++++++++++++++++++++ plugins/example/matrix/src/window.c | 38 +++++++++ 3 files changed, 162 insertions(+) create mode 100644 plugins/example/matrix/include/matrix.h create mode 100644 plugins/example/matrix/src/matrix.c create mode 100644 plugins/example/matrix/src/window.c diff --git a/plugins/example/matrix/include/matrix.h b/plugins/example/matrix/include/matrix.h new file mode 100644 index 0000000..a6327ed --- /dev/null +++ b/plugins/example/matrix/include/matrix.h @@ -0,0 +1,15 @@ +/* +** EPITECH PROJECT, 2026 +** ~/epitech/delivery/pre-release/minishell +** File description: +** mysh +*/ + +#ifndef MATRIX_H + #define MATRIX_H + +void set_blank_term(void); +void get_terminal_size(int *rows, int *cols); +void restore_terminal(struct termios *old); + +#endif diff --git a/plugins/example/matrix/src/matrix.c b/plugins/example/matrix/src/matrix.c new file mode 100644 index 0000000..06ed28c --- /dev/null +++ b/plugins/example/matrix/src/matrix.c @@ -0,0 +1,109 @@ +/* +** EPITECH PROJECT, 2026 +** ~/epitech/delivery/42sh/plugins +** File description: +** Matrix example plugin +*/ + +#include "c_zsh.h" +#include "matrix.h" +#include +#include +#include + +static char random_glyph(void) +{ + const char glyphs[] = "01ABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%&*+-"; + + return glyphs[rand() % (sizeof(glyphs) - 1)]; +} + +static void print_cell(int drop, int row) +{ + if (drop == row) + printf("\033[1;92m%c\033[0m", random_glyph()); + else if (drop > row && drop - row < 4) + printf("\033[0;32m%c\033[0m", random_glyph()); + else + putchar(' '); +} + +static void display_matrix_frame(int *drops, int rows, int cols) +{ + printf("\033[H\033[2J"); + for (int row = 0; row < rows; row++) { + for (int col = 0; col < cols; col++) + print_cell(drops[col], row); + putchar('\n'); + } + fflush(stdout); +} + +static void update_drops(int *drops, int cols, int rows) +{ + for (int col = 0; col < cols; col++) { + if (drops[col] > rows + (rand() % 12)) + drops[col] = rand() % rows; + else + drops[col]++; + } +} + +static void run_matrix(int *drops, int rows, int cols, int time_v) +{ + for (int frame = 0; frame < time_v * 12; frame++) { + display_matrix_frame(drops, rows, cols); + update_drops(drops, cols, rows); + usleep(80000); + } +} + +void matrix(int time_v) +{ + struct termios old; + int rows = 24; + int cols = 80; + int *drops = NULL; + time_t timer = time(NULL); + + if (time_v <= 0) + time_v = 10; + get_terminal_size(&rows, &cols); + drops = calloc((size_t)cols, sizeof(int)); + if (!drops) + return; + tcgetattr(STDIN_FILENO, &old); + set_blank_term(); + srand((u_int16_t)(timer)); + printf("\033[?25l\033[2J\033[H"); + fflush(stdout); + run_matrix(drops, rows, cols, time_v); + restore_terminal(&old); + free(drops); +} + +static int matrix_builtin(main_t *main_stock, command_ctx_t *ctx) +{ + int time = -1; + + (void)main_stock; + if (ctx->arg_command[0]) + time = atoi(ctx->arg_command[0]); + matrix(time); + return 0; +} + +int czsh_plugin_init(main_t *main_stock, const shell_plugin_api_t *api) +{ + if (!api || api->abi_version != CZSH_PLUGIN_ABI_VERSION) + return CZSH_PLUGIN_ERR; + if (!api->register_builtin) + return CZSH_PLUGIN_ERR; + return api->register_builtin(main_stock, "matrix", matrix_builtin, + api->plugin_handle); +} + +void czsh_plugin_shutdown(main_t *main_stock) +{ + (void)main_stock; +} diff --git a/plugins/example/matrix/src/window.c b/plugins/example/matrix/src/window.c new file mode 100644 index 0000000..09ca2ca --- /dev/null +++ b/plugins/example/matrix/src/window.c @@ -0,0 +1,38 @@ +/* +** EPITECH PROJECT, 2026 +** ~/epitech/delivery/42sh/plugins/example/matrix +** File description: +** window.c +*/ + +#include "c_zsh.h" + +void set_blank_term(void) +{ + struct termios tr; + + tcgetattr(STDIN_FILENO, &tr); + tr.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &tr); +} + +void get_terminal_size(int *rows, int *cols) +{ + struct winsize size; + + if (ioctl(STDIN_FILENO, TIOCGWINSZ, &size) == -1 || size.ws_row == 0 || + size.ws_col == 0) { + *rows = 24; + *cols = 80; + return; + } + *rows = size.ws_row; + *cols = size.ws_col; +} + +void restore_terminal(struct termios *old) +{ + printf("\033[0m\033[?25h\033[2J\033[H"); + fflush(stdout); + tcsetattr(STDIN_FILENO, TCSANOW, old); +}