int main(int argc, char **argv, char **envp) { init(); welcomeScreen(); shellPrompt(); while (TRUE) { userInput = getchar(); switch (userInput) { case '\n': shellPrompt(); break; default: getTextLine(); handleUserCommand(); shellPrompt(); break; } } printf("\n"); return 0; }
int main(int argc, char **argv, char **envp) { init(); welcomeScreen(); shellPrompt(); // prints the prompt while (TRUE) { userInput = getchar(); switch (userInput) { case '\n': // if the user hits the enter key shellPrompt(); // nothing happens break; default: getTextLine(); // store user input in buffer handleUserCommand(); // execute user command shellPrompt(); // print the prompt break; } } printf("\n"); return 0; }
int main(int argc, char* argv[],char* envp[]) { getEnv(envp); #ifdef DEBUG printf("%s\t%s\t%s\n",envp[0],envp[1],envp[2]); #endif if(argc > 1) { executeScript(argv[1],envp); exit(0); } while(TRUE){ shellPrompt(); char* input=malloc(MAX_LENGTH); scanf("%s",input);//different from standard scanf which ignores space! /*int i=scanf("%[^\n]%*c",input); if(i==0){ char tmp; scanf("%c",&tmp); continue; }*/ //gets(input); //fgets(input,MAX_LENGTH,stdin); #ifdef DEBUG printf("(main) input is: %s\n",input); #endif executeCmd(input,envp); } }
int main(int argc, char *agrv[]) { getcwd(filePath, PATH_LEN);//获取当前目录路径 shellPrompt(); return 0; }
void readline(shell_struct *shell) { int i; int end = 0; /*stoppe la saisie lorsqu'il vaut 1*/ int cur_col = 0; /*dans le terminal, indique la position du pointeur*/ struct winsize ws; ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); int colonnes = ws.ws_col; cur_col = shellPrompt(0); fflush(stdout); printf("\033[s"); struct termios oldt; if (tcgetattr(0, &oldt) != 0) { perror("Erreur tcget"); } struct termios newt = oldt; cfmakeraw(&newt); /* On change la structure termios default, on ajout un retour chariot a chaque newline detecté */ //newt.c_oflag |= (ONLCR | OPOST); if (tcsetattr(0, TCSANOW, &newt) != 0) { perror("Erreur tcset"); } char *com_hist; int count = 0;/*sauvegarde le nombre de char dans le buffer*/ int pos = 0;/*position du curseur dans le buffer buf*/ /*variables pour la tabulation*/ struct tab tabu; init_tab(&tabu); shell->buf[count] = '\0'; do { int t, m; int c = getbigchar(); switch (c) { case 1: /*C^a*/ while (pos > 0) { move_left(&pos,&cur_col,colonnes); } break; case 3: /*C^c*/ shell->buf[0] = '\0'; break; case 4: /*C^D*/ shell->end_b = 1; end = 1; shell->buf[0] = '\n'; break; case 5: /*C^e*/ while (pos != count) { move_right(&pos,&cur_col,colonnes,count); } break; case 9:/*TAB autocompletion */ tabu.tab = 1; /*tab est pressé pour la première fois*/ if (tabu.noccur == 0){ /*on récupère le dernier mot*/ tabu.lw = lastword(shell->buf, &(shell->buf[pos]), &(tabu.after_com), ' '); /*on sauvegarde la position*/ tabu.save_pos = pos - strlen(tabu.lw); } /*on efface*/ while( pos > tabu.save_pos){ move_left(&pos,&cur_col,colonnes); del_char(&pos, &cur_col, colonnes, &count, shell); } ++tabu.noccur; tabu.suggest = look_up_command(tabu.lw, tabu.noccur, tabu.after_com); if (!strncmp(tabu.suggest, "",1)){ tabu.noccur = 1; tabu.suggest = tabu.lw; /*on reprend le mot de base*/ } /*on insère*/ i = 0; while (tabu.suggest[i] != '\0') { insert_char(&pos, &cur_col, colonnes, &count, shell, tabu.suggest[i]); i++; } break; case 11:/*^Ck*/ strncpy(shell->save_buf, &shell->buf[pos], count - pos); shell->save_buf[(count + 1) - pos] = '\0'; while(pos != count) { del_char(&pos,&cur_col,colonnes,&count,shell); } break; case 13:/*Enter*/ end = 1; while (pos != count) { move_right(&pos,&cur_col,colonnes,count); } strncmp(shell->buf, "\0", 1) ? insert_new_cmd(shell->buf) : 0; shell->buf[pos] = '\0'; break; case 25:/*C^y*/ i = 0; while (shell->save_buf[i] != '\0') { insert_char(&pos, &cur_col, colonnes, &count, shell, shell->save_buf[i]); i++; } break; case 2: /*C^b*/ case KLEFT: move_left(&pos, &cur_col, colonnes); break; case 6:/*C^f*/ case KRIGHT: move_right(&pos, &cur_col, colonnes, count); break; case KUP:/*historique -*/ if (!is_empty_hist()){ printf("\033[u"); printf("\033[K"); cur_col = shellPrompt(0); com_hist = get_prev_cmd(); printf("%s", com_hist); strncpy(shell->buf, com_hist, strlen(com_hist) + 1); cur_col += strlen(com_hist); count = strlen(com_hist); pos = count; } break; case KDOWN:/*historique +*/ if (!is_empty_hist()){ printf("\033[u"); printf("\033[K"); cur_col = shellPrompt(0); com_hist = get_next_cmd(); printf("%s", com_hist); strncpy(shell->buf, com_hist, strlen(com_hist) + 1); cur_col += strlen(com_hist); count = strlen(com_hist); pos = count; } break; case KDEL: /* Touche suppr */ del_char(&pos,&cur_col,colonnes,&count,shell); break; case 127: /* Touche retour (del) */ tabu.noccur = 0; if (pos == 0) { break; } if (pos < count) { for (i = pos - 1; i < count; i++) { shell->buf[i] = shell->buf[i + 1]; } } else { shell->buf[pos - 1] = '\0'; } if (cur_col > 0) { printf("\033[D"); cur_col--; } else { printf("\033[A\033[%dC", colonnes - 1); cur_col = colonnes - 1; } count--; pos--; printf("%s ", &shell->buf[pos]); t = count - pos + 1; m = (t + cur_col - 1) / colonnes; if (m > 0) { printf("\033[%dA", m); } t -= m * colonnes; if (t > 0) { printf("\033[%dD", t); } else if (t < 0) { printf("\033[%dC", -t); } break; default: if (tabu.tab){ tabu.noccur = 0; } insert_char(&pos, &cur_col, colonnes, &count, shell, c); break; } fflush(stdout); } while (!end); if (tcsetattr(0, TCSANOW, &oldt) != 0) { perror("erreur tcset"); } printf("\n"); }
int main(int argc, char **argv, char **envp) { //Buffer to hold our input commands char buffer[BUF_LIM]; //Index for cycling through buffer int index; //Stores pointers to token strings char *tokens[BUF_LIM]; //Process ID pid_t pid; //Status of process int status; //Used for deciding file descriptors int again, in, out, inout, back, err_redir; //File descriptors int fd[2], fdin, fdout; char returnChar; //Will display the entire current working directory as a prompt shellPrompt(); //used to exit by pressing ctrl+c signal(SIGINT, SIG_IGN); signal(SIGINT, handle_signal); //Get buffer from user and loop the shell while(fgets(buffer,BUF_LIM,stdin) != NULL) { //used to exit by pressing ctrl+c signal(SIGINT, SIG_IGN); signal(SIGINT, handle_signal); //If user hit return, reprint shell prompt and get user input again switch(returnChar = buffer[0]) { case '\n': shellPrompt(); break; default: //Parse our buffer command Parser(buffer, tokens, &index, &again, &in, &out, &inout, &back, &err_redir); //If if((in && inout) || (out && inout)) { fprintf(stderr, "Not implemented\n"); continue; } //Call fork pid = fork(); //Fork doesn't work if(pid < 0) { perror("fork error"); exit(1); } //child process else if(!pid) { //Execute the command execute(tokens, fd, fdin, fdout, again, in, out, inout, back, err_redir, pid, status); bzero(buffer, BUF_LIM); } waitpid(pid, &status, 0); shellPrompt(); break; } } return 0; }