static void search_mode(void){ int searchcap = 8; char* searchstr = malloc(sizeof(char)*searchcap); int searchlen = 0; bool searching = true; state_set_mode(MODE_SEARCH); while(searching){ char inpt = getch(); if(inpt == ERR){ /* do nothing */ }else if(inpt == ESC){ searching = false; }else if (inpt == '\n'){ searching = false; handle_cd(); }else{ if(searchlen+1 >= searchcap){ searchcap *= 2; searchstr = realloc(searchstr, sizeof(char)*searchcap); } searchstr[searchlen] = inpt; searchlen += 1; searchstr[searchlen] = 0; workdir_seek(state_get_focused_workdir(), searchstr); } if(state_get_refresh_state() != REFRESH_COMPLETE) screen_update(); } free(searchstr); state_set_mode(MODE_NORMAL); }
int main(int argc, char *argv[]) { /* The shell process itself ignores SIGINT. */ struct sigaction action; action.sa_handler = SIG_IGN; action.sa_flags = 0; sigemptyset(&action.sa_mask); if (sigaction(SIGINT, &action, NULL) == -1) { perror("sigaction"); return EXIT_FAILURE; } while (1) { // Read the next command. command_t *command = read_command(); // Check for finished background processes. int status; pid_t pid; while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { printf("Background process %d finished\n", pid); } if (!command) continue; // Ignore empty commands. if (match(command->argv[0], "exit")) { // Handle built-in exit command. if (handle_exit(command)) { free_command(command); break; } } else if (match(command->argv[0], "cd")) { // Handle built-in cd command. handle_cd(command); } else { // Fork a child process. pid = fork(); if (pid == -1) { perror("fork"); } else if (pid == 0) { // Execute command. execvp(command->argv[0], command->argv); perror(command->argv[0]); exit(EXIT_FAILURE); } else { if (command->type == FOREGROUND) { printf("Spawned foreground process pid: %d\n", pid); struct timeval t0, t1; // Wait for foreground process. gettimeofday(&t0, NULL); waitpid(pid, &status, 0); gettimeofday(&t1, NULL); printf("Foreground process %d terminated\n", pid); printf("wallclock time: %.3f msec\n", elapsed_ms(&t0, &t1)); } else { printf("Spawned background process pid: %d\n", pid); } } } free_command(command); } clear_history(); // Clear GNU readline history. return EXIT_SUCCESS; }
void foo(void) { switch(ch) { // handle 'a' case 'a': { handle_a(); multiline(123, 345); break; } // handle 'b' case 'b': handle_b(); multiline(123, 345); break; // handle 'c' and 'd' case 'c': case 'd': // c and d are really the same thing handle_cd(); multiline(123, 345); break; case 'e': { handle_a(); multiline(123, 345); } break; // case1 case (case1): { //do stuff break; } case (case2): { //do stuff break; } case (case3): /*do stuff*/ break; case (case3): statement(); { another_statement(); } break; // really should not get here default: handle_default(); multiline(123, 345); break; } multiline(123, 345); }