char *my_readline(t_shell *shell, char *prompt) { t_rl *base; char *output; base = &shell->rl; init_readline(base); output = NULL; if (!(CHCK(base->flags, 0x01))) { xfree(prompt); return (get_next_line(STDIN_FILENO)); } else if (prompt) { if (signal(SIGWINCH, term_size) == SIG_ERR) perror("42sh"); #ifdef __HREACT_ON__ hreact_displaying(shell); #endif print_prompt(base, prompt); output = getline(base, shell); save_to_history(shell, output); } reset_readline(base); return (output); }
static void test_after_history_reset_ok(void) { const char *const str = "string"; save_to_history(str); VALIDATE_HISTORY(0, str); }
static void test_add_after_increasing_ok(void) { const char *const str = "longstringofmeaninglesstext"; int i; for(i = 0; i < INITIAL_SIZE; i++) { save_to_history(str + i); } resize_history(INITIAL_SIZE*2); for(i = 0; i < INITIAL_SIZE; i++) { save_to_history(str + i); } }
int main() { int bg; char *args[ARGS_SIZE]; int cnt; char new_dir[MAX_PATH + 1]; // new_dir[0] = '\0'; char buf[MAX_PATH + 1]; memcpy(curdir, getcwd(buf, MAX_PATH + 1), MAX_PATH + 1); signal(SIGCHLD, handle_child_death); // free(shell_command_history); shell_command_history = (struct history_item **) malloc(HISTORY_SIZE*sizeof(struct history_item *)); for(int i = 0; i < HISTORY_SIZE; i++) { shell_command_history[i] = (struct history_item *) malloc(sizeof(struct history_item)); } while(1) { bg = 0; cnt = getcmd("\n>> ", args, &bg); args[cnt] = NULL; /* for (int i = 0; i < cnt; i++) printf("\nArg[%d] = %s", i, args[i]); */ printf("\n"); int history_element_to_execute; if ( (history_element_to_execute = atoi(args[0])) > 0) { printf("history_element_to_execute: %d\n", history_element_to_execute); memcpy(args, shell_command_history[history_element_to_execute-1], ARGS_SIZE); } save_to_history(args); if (strcmp(args[0],"history") == 0) { if (args[1] != NULL) { printf("history does not require any arguments.\n"); exit(-1); } show_history(); continue; } if (strcmp(args[0], "cd") == 0) { new_dir[0] = '\0'; strcat(new_dir, curdir); printf("%s\n", new_dir); if (args[1][0] == '/') { new_dir[0] = '\0'; } char slash[] = {'/', '\0'}; strcat(new_dir, slash); strcat(new_dir, args[1]); strcat(new_dir, slash); curdir[0] = '\0'; strcat(curdir, new_dir); chdir(new_dir); continue; } if (strcmp(args[0], "pwd") == 0) { char* cwd; cwd = getcwd(buf, MAX_PATH + 1); printf("%s", cwd); continue; } if (strcmp(args[0], "exit") == 0) { exit(0); } if (strcmp(args[0], "jobs") == 0) { print_jobs(); continue; } if (strcmp(args[0], "fg") == 0) { // waitpid(); int pos = strtol(args[1], NULL, 10); struct job * selected_job = get_job_at_pos(pos); if (selected_job == NULL) { printf("There was an issue. The selected job, %d, has not been found.\n", pos); continue; } int status = 0; waitpid(selected_job->pid, &status, 0); continue; } // char * file = malloc(sizeof); int child_pid = 0; if((child_pid = fork()) == 0) { int output_filename_index = cnt - 1; if (bg) { output_filename_index--; } if (output_filename_index < 1) { output_filename_index = 1; } printf("cnt: %d.\n", cnt); char* output_filename = malloc((MAX_PATH+1)*sizeof(char)); if (strcmp(args[output_filename_index - 1], ">") == 0) { char* arg = args[output_filename_index]; memcpy(output_filename, arg, strlen(arg)+1); printf("%s\n", output_filename); close(1); open(output_filename, O_WRONLY | O_CREAT, 0666); // int fd[2] = {0,1}; // pipe(fd); args[output_filename_index - 1] = NULL; } int result = execvp(args[0], args); if (result < 0) { printf("We got the errno %d", errno); } } else { // continue execution } if (bg) { // printf("\nBackground enabled..\n"); int inserted_job_pos = insert_job(child_pid, args); printf("Created job with pid: %d, at pos: %d", child_pid, inserted_job_pos); } else { // printf("\nBackground not enabled \n"); int status = 0; waitpid(child_pid, &status, 0); } printf("\n\n"); } }