// **************************************************************** // * The main function puts everything together so that the shell * // * can work. * // **************************************************************** int main(int argc, char **argv, char **envp) { int fd, i; char c; char *input_str = calloc(BUFFERSIZE, sizeof(char*)); char *cmd = calloc(BUFFERSIZE, sizeof(char*)); // ignore the standard interrupt signal (ctrl+c) signal(SIGINT, SIG_IGN); // assign interrupt signal (ctrl+c) to the signal handler function signal(SIGINT, sig_hdlr); // prepare screen and fetch necessary data initilize(envp); // main loop might be stopped by typing 'quit', 'exit' or by pressing 'ctrl+d' while(strcmp(input_str, QUIT_CMD) != 0 && strcmp(input_str, EXIT_CMD) != 0 && c != EOF) { // get input c = getchar(); // this switch-case statement read and prepare the input string switch(c) { case NEWLINE_SYMBOL: // if no command is inserted, just print the prompt again if(input_str[0] != NULL_SYMBOL) { // erase cmd variable memset(cmd, 0, BUFFERSIZE); // parse the command line cmd = prepare_inputs(input_str); // special case: change directory call if(strncmp(cmd, CD, 2) == 0) { cd(inputs[1]); // all other command calls } else { get_cmd_pth(cmd); cmd_exec(cmd, envp); } free_arr(inputs); } // print the prompt again after hitting enter print_prompt(); memset(input_str, 0, BUFFERSIZE); break; default: // prepare the input string strncat(input_str, &c, 1); break; } } // free allocated memories free(cmd); free(input_str); free_arr(paths); // print new line if 'ctrl+d' is pressed if(c == EOF) printf(NEWLINE_STR); // end of main return 0; }
/* ** brief: it will add other operator like | ... ** we will add the new elt in the right son while the intern node is ** an operator ** @ast: our binary tree ** @oper: the operator like "|" ** @id_ope: the operator id like ID_PIPE ** return: return the new ast */ t_ast *add_operator(t_ast *ast, char *oper, char id_ope) { t_ast *tmp; t_ast *prev; t_ast *node; char **arr; if (id_ope == -1) return (ast); tmp = ast; prev = ast; if ((arr = my_str_to_wordtab(oper, my_strlen(oper), 0, -1)) == NULL) return (NULL); while (tmp != NULL && tmp->oper != -1) { prev = tmp; tmp = tmp->right; } if ((node = create_element_ast(arr, id_ope)) == NULL) return (NULL); free_arr(arr); node->left = tmp; if (tmp == prev) ast = node; else prev->right = node; return (ast); }
/* ** brief: used if the operator is ; -> travel while the intern node is ; ** @ast: the ast ** return: the entire tree */ t_ast *add_wait_opera(t_ast *ast) { t_ast *tmp; t_ast *prev; t_ast *node; char **arr; tmp = ast; prev = ast; if ((arr = my_str_to_wordtab(";", 1, 0, -1)) == NULL) return (NULL); while (tmp != NULL && tmp->oper == ID_WAIT) { prev = tmp; tmp = tmp->right; } if ((node = create_element_ast(arr, ID_WAIT)) == NULL) return (NULL); free_arr(arr); node->left = tmp; if (tmp == prev) ast = node; else prev->right = node; return (ast); }
/* ** brief: it will add our command in our ast ** to add it, we will travel our ast while we've a command ** after it, if the left son is empty we will add our command here, ** else we'll add in the right son ** @ast: the binary tree struct ** @cmd : the char * with the cmd ** end: then end string to stop the str_to_wordtab magueule ** return: return the new AST */ t_ast *add_command(t_ast *ast, char *cmd, char **array, int end) { t_ast *tmp; t_ast *prev; t_ast *node; char **arr; tmp = ast; prev = ast; arr = (cmd != NULL || array == NULL) ? (my_str_to_wordtab(cmd == NULL ? "" : cmd, cmd == NULL ? 1 : end, 0, -1)) : NULL; while (tmp != NULL && tmp->oper != -1) { prev = tmp; tmp = tmp->right; } if (!(node = create_element_ast(arr == 0 ? array : arr, -1))) return (NULL); free_arr(arr); if (tmp == prev) ast = node; else if (prev->left == NULL) prev->left = node; else prev->right = node; return (ast); }
int main() { Array arr; int i; init(&arr, 5); for (i = 1; i < 10; i ++) { insert(&arr, i); } free_arr(&arr); }
char *get_path(char **envp, char **cmd) { char **paths; char *path; paths = ft_strsplit(envp[0] + 5, ':'); path = search_path(paths, *cmd); free_arr(&paths); return (path); }
/* ** brief: the command-not-found by bash ** @cmd: the command like toto etc... ** return: -1 everytime */ void my_cnf(char *cmd, char **env) { (void)write(2, "If '", 4); (void)write(2, cmd, my_strlen(cmd)); (void)write(2, "' is not a typo you can use command-not-found to lookup", 55); (void)write(2, " the package that contains it, like this:\n\tcnf ", 47); (void)write(2, cmd, my_strlen(cmd)); (void)write(2, "\n", 1); free_arr(env); }
// **************************************************************** // * This function clears the screen, for our new shell to open * // * and prints the prompt for the first time. * // * Also, it gets all the paths where the system commands may be * // * stored, by calling get_paths(). * // **************************************************************** void initilize(char **envp) { char *cmd = calloc(BUFFERSIZE, sizeof(char*)); get_paths(envp); cmd = prepare_inputs(CLEAR); get_cmd_pth(cmd); cmd_exec(cmd, envp); sig_hdlr(0); free_arr(inputs); }
/// Deallocate task_record_t structure. static void free_task_record(task_record_t *task) { thread_record_t *thread; uint32_t i; stack_info_free_task_stack(task); free_arr((void**)task->threads_arr, task->threads); free(task->threads_arr); free_arr((void**)task->argv, task->argc); free(task->argv); free_arr((void**)task->envv, task->envc); free(task->envv); free(task->app_name); free(task->path_to_executable); free(task->path_to_boundle); free(task->bundle_path_name); free(task); }
char *get_path(char **cmd) { char **paths; char *part_path; char *path; paths = ft_strsplit(*find(ENVP, "PATH=") + 5, ':'); part_path = ft_strjoin(search_path(paths, cmd[0]), "/"); path = ft_strjoin(part_path, cmd[0]); ft_strdel(&part_path); free_arr(&paths); return (path); }
static void free_threads_array(thread_record_t **threads_arr, int count) { int cur_thread, cur_rec; for (cur_thread = 0; cur_thread <count; ++cur_thread) { for (cur_rec=0; cur_rec < threads_arr[cur_thread]->stack_records_count; ++cur_rec) { free(threads_arr[cur_thread]->call_stack[cur_rec]); } free(threads_arr[cur_thread]->call_stack); } free_arr((void**)threads_arr, count); }
int implemented_function(char **cmd) { if (!cmd) return (1); if (ft_strcmp(cmd[0], "exit") == 0) { free_arr(&cmd); return (-1); } else if (ft_strcmp(cmd[0], "env") == 0) ft_env(cmd); else if (ft_strcmp(cmd[0], "cd") == 0) ft_cd(cmd); else if (ft_strcmp(cmd[0], "setenv") == 0) ENVP = ft_setenv(cmd); else if (ft_strcmp(cmd[0], "unsetenv") == 0) ENVP = ft_unsetenv(cmd); else return (0); return (1); }