cshell

Simple shell implementation on C for Linux
git clone https://git.nfu.su/cshell.git
Log | Files | Refs

commit 84986b9bbe2c42a1f0273efe513e9345ae386361
parent de18b7de9ea82a466e3ecff296020eec5e36116d
Author: Oleg Levkin <uventik999@gmail.com>
Date:   Sun, 21 Jun 2026 18:11:06 +0300

[NEW]: tokenizer(): New function to parse user input

Diffstat:
A.gitignore | 1+
DCShell | 0
Mmain.c | 96++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
3 files changed, 82 insertions(+), 15 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +a.out diff --git a/CShell b/CShell Binary files differ. diff --git a/main.c b/main.c @@ -3,41 +3,107 @@ #include <sys/types.h> #include <unistdio.h> +// INFO: Struct with all user-input data from 'getline()' typedef struct { - char *get_buff; - size_t s_get_buff; + char *get_buff; // String from User + size_t s_get_buff; // Size of the Buffer + long get_str_len; // Lenght of the String } getLineStruct; -void in_get(getLineStruct *gls); +long in_get(getLineStruct *gls); void show_greet(void); +char **tokenizer(getLineStruct *gls, int *tok_count); int main(void) { - - // First Inits + // INFO: Some Vars _Bool is_exit = 0; - getLineStruct in_buff = {.get_buff = NULL, .s_get_buff = 0}; - char *tokens[128]; + getLineStruct in_buff = {.get_buff = NULL, .s_get_buff = 0, .get_str_len = 0}; + int tok_count = 0; + char **tok_arr = NULL; - // INFO: Main Cycle (Entry Point of Exec) + // INFO: Main Loop (Entry Point of Exec) while (!is_exit) { + tok_count = 0; + + // INFO: Show greet message show_greet(); - in_get(&in_buff); - free(in_buff.get_buff); + // INFO: Reading user input + in_buff.get_str_len = in_get(&in_buff); + if (in_buff.get_str_len == -1) { + fprintf(stderr, "!> EOF. Exiting...\n"); + free(in_buff.get_buff); + return 1; + } + + // INFO: Parse user input to Arr of tokens + tok_arr = tokenizer(&in_buff, &tok_count); + if (tok_arr == NULL) { + return 1; + } + + // WARN: Debug Output + for (int i = 0; i < tok_count; i++) { + printf("%d> %s\n", i, tok_arr[i]); + } + + free(tok_arr); } + free(in_buff.get_buff); return 0; } -void tokenizer(getLineStruct *gls, char **tok) { - int i = 0; +char **tokenizer(getLineStruct *gls, int *tok_count) { + char **tok_arr = NULL; + int tok_arr_capacity = 2; + char **tmp = NULL; + + for (int i = 0; i < gls->get_str_len; i++) { + if (gls->get_buff[i] == ' ' || gls->get_buff[i] == '\n') { + gls->get_buff[i] = '\0'; + } + } - for (int a = 0;; a++) { + tok_arr = malloc(tok_arr_capacity * sizeof(char *)); + if (tok_arr == NULL) { + return NULL; } + + for (int i = 0; i < gls->get_str_len; i++) { + if (*tok_count == tok_arr_capacity) { + tok_arr_capacity *= 2; + tmp = realloc(tok_arr, tok_arr_capacity * sizeof(char *)); + if (tmp == NULL) { + return NULL; + } + tok_arr = tmp; + } + + if (gls->get_buff[i] != '\0' && i == 0) { + tok_arr[*tok_count] = &gls->get_buff[i]; + *tok_count += 1; + } else if (gls->get_buff[i] != '\0' && gls->get_buff[i - 1] == '\0') { + tok_arr[*tok_count] = &gls->get_buff[i]; + *tok_count += 1; + } + } + + if (*tok_count == tok_arr_capacity) { + tok_arr_capacity++; + tmp = realloc(tok_arr, tok_arr_capacity * sizeof(char *)); + if (tmp == NULL) { + return NULL; + } + tok_arr = tmp; + } + tok_arr[*tok_count] = NULL; + + return tok_arr; } -void in_get(getLineStruct *gls) { - getline(&gls->get_buff, &gls->s_get_buff, stdin); +long in_get(getLineStruct *gls) { + return getline(&gls->get_buff, &gls->s_get_buff, stdin); } void show_greet(void) { printf("> "); }