コード例 #1
0
ファイル: MusicLibrary.c プロジェクト: ttang1/ics53
/*============================
	EVALUATE COMMAND
============================*/
char evaluate_command(char cmd)
{
	switch (cmd)
	{
		case 'i':
		case 'I':
			add_song_to_MusicLibrary();
			break;
		case 'p':
		case 'P':
			print_MusicLibrary();
			break;
		case 'd':
		case 'D':
			delete_command();
			break;
		case 'l':
		case 'L':
			look_up_command();
			break;
		case 'q':
		case 'Q':
			store_MusicLibrary();
			fclose(m_fp);
			printf("quitting\n");
			return 'q';
		default:
			break;
	}
	return 'a';
}
コード例 #2
0
ファイル: readline.c プロジェクト: TacOS-team/tacos
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");
}