1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termio.h>
#include <unistd.h>
#include "shell.h"
/* cette fonction reconfigure le terminal, et stocke */
/* la configuration initiale a l'adresse prev */
int reconfigure_terminal (struct termios *prev)
{
struct termios new;
if (tcgetattr(fileno(stdin),prev)==-1) {
perror("tcgetattr");
return -1;
}
new.c_iflag=prev->c_iflag;
new.c_oflag=prev->c_oflag;
new.c_cflag=prev->c_cflag;
new.c_lflag=0;//ECHO;
new.c_cc[VMIN]=1;
new.c_cc[VTIME]=0;
new.c_cc[VERASE]=1;
if (tcsetattr(fileno(stdin),TCSANOW,&new)==-1) {
perror("tcsetattr");
return -1;
}
return 0;
}
/* cette fonction restaure le terminal avec la */
/* configuration stockee a l'adresse prev */
int restaure_terminal (struct termios *prev)
{
return tcsetattr(fileno(stdin),TCSANOW,prev);
}
int main (int argc,char *argv[])
{
struct termios prev;
int nb,c;
int end = 0, aff = 1;
if (reconfigure_terminal(&prev)==-1) return 1;
nb=0;
while(!end) {
/* Affiche le prompt : la première fois + si la touche return est utilisée */
if (aff) {
char *invite = prompt();
if (invite != NULL) {
printf("%s",invite);
fflush(stdout);
free (invite) , invite = NULL;
}
aff = 0;
}
c=getchar();
switch(c) {
/* (void) printf("carac[%d]=(%d,%o,%x)",nb,c,c,c); */
/* CTRL-D */
case 4 : {
end = 1;
break;
}
/* Tabulation */
case 9 : {
printf("COMPLETION\n");
aff = 1;
break;
}
/* Return */
case 10 : {
printf("\n");
nb = 0;
aff = 1;
break;
}
case 20 : {
// Marche pô
break;
}
/* Backspace */
case 127 : {
if(nb>0) {
printf("\b \b");
nb--;
}
break;
}
default : {
if(c>32) {
printf("%c",c);
nb++;
}
break;
}
}
}
if (restaure_terminal(&prev)==-1) return 1;
return 0;
} |
Partager