main.c (3963B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/types.h> 5 #include <sys/wait.h> 6 #include <unistd.h> 7 8 // INFO: Struct with all user-input data from 'getline()' 9 typedef struct { 10 char *get_buff; // String from User 11 size_t s_get_buff; // Size of the Buffer 12 long get_str_len; // Lenght of the String 13 } getLineStruct; 14 15 long in_get(getLineStruct *gls); 16 void show_greet(int code); 17 char **tokenizer(getLineStruct *gls, int *tok_count); 18 int cs_exec(char **args); 19 int parse_return_info(int ret_status); 20 21 int main(void) { 22 // INFO: Some Vars 23 _Bool is_exit = 0; 24 getLineStruct in_buff = {.get_buff = NULL, .s_get_buff = 0, .get_str_len = 0}; 25 int tok_count = 0; 26 char **tok_arr = NULL; 27 int ret_value = 0; 28 int i_count = 0; 29 30 // INFO: Main Loop (Entry Point of Exec) 31 while (!is_exit) { 32 // INFO: Prep for next iteration 33 tok_count = 0; 34 35 // INFO: Show greet message 36 if (i_count == 0) { 37 printf("> "); 38 } else { 39 show_greet(parse_return_info(ret_value)); 40 } 41 42 // INFO: Reading user input 43 in_buff.get_str_len = in_get(&in_buff); 44 if (in_buff.get_str_len == -1) { 45 fprintf(stderr, "!> EOF. Exiting...\n"); 46 free(in_buff.get_buff); 47 return 1; 48 } 49 50 // INFO: Parse user input to Arr of tokens 51 tok_arr = tokenizer(&in_buff, &tok_count); 52 if (tok_arr == NULL) { 53 return 1; 54 } 55 56 // INFO: Skip iteration if empty input 57 if (tok_arr[0] == NULL) { 58 free(tok_arr); 59 continue; 60 } 61 62 // INFO: Exit handling 63 if (strcmp("exit", tok_arr[0]) == 0) { 64 printf("=> Exiting..."); 65 free(tok_arr); 66 free(in_buff.get_buff); 67 return 0; 68 } 69 70 // INFO: Exec if not CShell command 71 ret_value = cs_exec(tok_arr); 72 73 // INFO: Clear Tok array for the next cycle 74 free(tok_arr); 75 76 i_count++; 77 } 78 79 // INFO: Exit point 80 free(in_buff.get_buff); 81 return 0; 82 } 83 84 int parse_return_info(int ret_status) { 85 if (WIFEXITED(ret_status)) { 86 return WEXITSTATUS(ret_status); 87 } else { 88 return -1; 89 } 90 } 91 92 int cs_exec(char **args) { 93 int ret_status = 0; 94 95 pid_t pid = fork(); 96 if (pid == 0) { 97 execvp(args[0], args); 98 99 fprintf(stderr, "!> Programm not found!\n"); 100 101 exit(127); 102 } else { 103 wait(&ret_status); 104 } 105 106 return ret_status; 107 } 108 109 char **tokenizer(getLineStruct *gls, int *tok_count) { 110 char **tok_arr = NULL; // Array of parsed tokens 111 int tok_arr_capacity = 2; // Capacity of 'tok_arr' 112 char **tmp = NULL; // Temp pointer for 'realloc()' 113 114 // INFO: Changing all '\n' & ' ' -> '\0' 115 for (int i = 0; i < gls->get_str_len; i++) { 116 if (gls->get_buff[i] == ' ' || gls->get_buff[i] == '\n') { 117 gls->get_buff[i] = '\0'; 118 } 119 } 120 121 // INFO: Allocating mem for array of tokens 122 tok_arr = malloc(tok_arr_capacity * sizeof(char *)); 123 if (tok_arr == NULL) { 124 return NULL; 125 } 126 127 // INFO: Parse Loop 128 for (int i = 0; i < gls->get_str_len; i++) { 129 // INFO: Expanding 'tok_arr' 130 if (*tok_count == tok_arr_capacity) { 131 tok_arr_capacity *= 2; 132 tmp = realloc(tok_arr, tok_arr_capacity * sizeof(char *)); 133 if (tmp == NULL) { 134 free(tok_arr); 135 return NULL; 136 } 137 tok_arr = tmp; 138 } 139 140 // INFO: Actual parse loop 141 if (gls->get_buff[i] != '\0' && i == 0) { 142 tok_arr[*tok_count] = &gls->get_buff[i]; 143 *tok_count += 1; 144 } else if (gls->get_buff[i] != '\0' && gls->get_buff[i - 1] == '\0') { 145 tok_arr[*tok_count] = &gls->get_buff[i]; 146 *tok_count += 1; 147 } 148 } 149 150 // INFO: Expanding 'tok_arr' for NULL if needed 151 if (*tok_count == tok_arr_capacity) { 152 tok_arr_capacity++; 153 tmp = realloc(tok_arr, tok_arr_capacity * sizeof(char *)); 154 if (tmp == NULL) { 155 free(tok_arr); 156 return NULL; 157 } 158 tok_arr = tmp; 159 } 160 tok_arr[*tok_count] = NULL; 161 162 return tok_arr; 163 } 164 165 long in_get(getLineStruct *gls) { 166 return getline(&gls->get_buff, &gls->s_get_buff, stdin); 167 } 168 169 void show_greet(int code) { printf("[%d]> ", code); }