1. stty: 虚终端设备控制命令。
stty option operation
-
echo [-echo] Echoes [does not echo] the typed characters; The default is echo.
-
raw [-raw] Disables [enables] the special meaning of the metacharacters ( ?, *, [list], [!list]); the default is -raw.
-
intr Generates an interrupt signal; usually the [Del] key is used.
-
erase (backspace) Erases the preceding character; usually the [ # ] key is used.
-
kill Deletes the entire line; usually [ @ ] or [Ctrl u] is used.
-
eof Generates the (end-of-file) signal from the terminal; usually [Ctrl-d] is used.
-
ek Resets the erase and kill keys to [#] and [@] respectively.
-
sane Sets the terminal characteristics to sensible default values.
2. Example of stty commands
-
$ stty -echo #Turns echoing off
-
$ stty echo #Turns echoing back on
-
$ stty \^u #Now [Ctrl u] is the kill key
-
$ stty sane #Reset options to reasonable values
-
When you have changes the options too many times and lost track of your changes, the sane option comes to your rescue.
-
$ stty ek #Change the kill and erase keys to their default values. This command sets the kill to [@] and the erase key to [#]
-
bash --noediting #entering a non-editing bash subshell
-
stty -a |egrep -o '\
-
stty erase X #set erase 'X' key, this change is stick to dvices
-
stty erase @ echo #set erase @,and set echo on
-
stty -a |egrep -o '\
-
stty erase ^H #reset backspace
-
stty icrnl #cause input carriage return to be converted to newline character
-
stty -icrnl #
-
stty onlcr #add carriage return to newline character
-
stty -onlcr #
-
bash --noediting;stty -icanon #disable canonical mode
-
stty icanon #able buffering of input in the terminal
3. showtty.c
-
#include <apue.h>
-
#include <termios.h>
-
-
/* mapping of flag bit position to name */
-
struct flaginfo
-
{
-
int fl_value; //flag value
-
char *fl_name; //string describing flag
-
};
-
-
/* Mapping of index to description */
-
struct cc_entry{
-
int index;
-
char *description;
-
};
-
-
-
struct flaginfo input_flags[]={
-
{IGNBRK, "Ignore break condition"},
-
{BRKINT, "Signal interrupt on break"},
-
{IGNPAR, "Ignore chars with parity errors"},
-
{PARMRK, "Mark parity errors"},
-
{INPCK, "Enable input parity check"},
-
{ISTRIP, "Strip character"},
-
{INLCR, "Map NL to CR on input"},
-
{IGNCR, "Ignore CR"},
-
{ICRNL, "Map CR to NL on input"},
-
{IXON, "Enable start/stop output control"},
-
//_IXANY, "Enable any char to restart output
-
{IXOFF, "Enable start/stop input control"},
-
{0, NULL}
-
};
-
-
struct flaginfo local_flags[]={
-
ISIG, "Enable signals",
-
ICANON, "Canonical input(erase and kill)",
-
/*_XCASE, "Canonical upper/lower apperance", */
-
ECHO, "Enable echo",
-
ECHOE, "Echo ERASE as BS-SPACE-BS",
-
ECHOK, "Echo KILL by starting new line",
-
0, NULL
-
};
-
-
-
show_baud_rate(int thespeed)
-
{
-
//print the speed in english
-
printf("the baud rate is ");
-
switch(thespeed){
-
case B300: printf("300\n"); break;
-
case B600: printf("600\n"); break;
-
case B1200: printf("1200\n"); break;
-
case B1800: printf("1800\n"); break;
-
case B2400: printf("2400\n"); break;
-
case B4800: printf("4800\n"); break;
-
case B9600: printf("9600\n"); break;
-
default: printf("Fast \n"); break;
-
}
-
}
-
-
-
void show_flagset(int thevalue,struct flaginfo thebitnames[])
-
{
-
//check each bit pattern and display descriptive title
-
-
int i=0;
-
for(i=0;thebitnames[i].fl_value;i++){
-
printf("%s is ",thebitnames[i].fl_name);
-
if(thevalue &thebitnames[i].fl_value)
-
printf("ON\n");
-
else
-
printf("OFF\n");
-
}
-
}
-
-
show_some_flags(struct termios *ttyp)
-
{
-
/* show the values of two of the flag sets_: c_iflag and c_lflag
-
add c_oflag and c_cflag is pretty routine ,just add new tales above and a
-
bit more code below */
-
show_flagset(ttyp->c_iflag,input_flags);
-
show_flagset(ttyp->c_lflag,local_flags);
-
}
-
-
int main(int argc, char *argv[])
-
{
-
struct termios ttyinfo; //this struct hold tty info
-
FILE *fp;
-
-
if((fp = fopen(ctermid(NULL),"r+"))==NULL)
-
exit(1);
-
if(tcgetattr(fileno(fp),&ttyinfo) == -1){
-
perror("Cannot get info about this terminal.");
-
exit(1);
-
}
-
//show info
-
show_baud_rate( cfgetospeed(&ttyinfo)); //show baud rate
-
printf("\n");
-
printf("The erase character is ascii %d,Ctrl- %c\n",ttyinfo.c_cc[VERASE],ttyinfo.c_cc[VERASE]-1+'A');
-
printf("\n");
-
printf("The line kill character is asii %d,Ctrl- %c\n,ttyinfo.c_cc[VKILL],ttyinfo.c_cc[VKILL]-1+'A');
-
show_some_flags(&ttyinfo);
-
return 0;
-
}
4. showtty.c 执行结果。
-
gcc -o showtty showtty.c
-
./showtty
-
the baud rate is Fast
-
-
The erase character is ascii 127,Ctrl- ?
-
-
The line kill character is asii 21,Ctrl- U
-
Ignore break condition is OFF
-
Signal interrupt on break is OFF
-
Ignore chars with parity errors is OFF
-
Mark parity errors is OFF
-
Enable input parity check is OFF
-
Strip character is OFF
-
Map NL to CR on input is OFF
-
Ignore CR is OFF
-
Map CR to NL on input is ON
-
Enable start/stop output control is ON
-
Enable start/stop input control is OFF
-
Enable signals is ON
-
Canonical input(erase and kill) is ON
-
Enable echo is ON
-
Echo ERASE as BS-SPACE-BS is ON
-
Echo KILL by starting new line is ON
阅读(1349) | 评论(0) | 转发(0) |