Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1734311
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: LINUX

2015-12-15 10:33:51

1. stty: 虚终端设备控制命令。 
stty option operation

点击(此处)折叠或打开

  1. echo [-echo] Echoes [does not echo] the typed characters; The default is echo.
  2. raw [-raw]    Disables [enables] the special meaning of the metacharacters ( ?, *, [list], [!list]); the default is -raw.
  3. intr     Generates an interrupt signal; usually the [Del] key is used.
  4. erase     (backspace) Erases the preceding character; usually the [ # ] key is used.
  5. kill     Deletes the entire line; usually [ @ ] or [Ctrl u] is used.
  6. eof     Generates the (end-of-file) signal from the terminal; usually [Ctrl-d] is used.
  7. ek     Resets the erase and kill keys to [#] and [@] respectively.
  8. sane     Sets the terminal characteristics to sensible default values.
2. Example of stty commands

点击(此处)折叠或打开

  1. $ stty -echo     #Turns echoing off
  2. $ stty echo     #Turns echoing back on
  3. $ stty \^u         #Now [Ctrl u] is the kill key
  4. $ stty sane         #Reset options to reasonable values
  5. When you have changes the options too many times and lost track of your changes, the sane option comes to your rescue.
  6. $ stty ek         #Change the kill and erase keys to their default values. This command sets the kill to [@] and the erase key to [#]
  7. bash --noediting    #entering a non-editing bash subshell
  8. stty -a |egrep -o '\
  9. stty erase X        #set erase 'X' key, this change is stick to dvices
  10. stty erase @ echo         #set erase @,and set echo on
  11. stty -a |egrep -o '\
  12. stty erase ^H            #reset backspace
  13. stty icrnl             #cause input carriage return to be converted to newline character
  14. stty -icrnl         #
  15. stty onlcr            #add carriage return to newline character
  16. stty -onlcr            #
  17. bash --noediting;stty -icanon    #disable canonical mode
  18. stty icanon        #able buffering of input in the terminal
3.  showtty.c

点击(此处)折叠或打开

  1. #include <apue.h>
  2. #include <termios.h>

  3. /* mapping of flag bit position to name */
  4. struct flaginfo
  5. {
  6.     int fl_value; //flag value
  7.     char *fl_name; //string describing flag
  8.     };

  9. /* Mapping of index to description */
  10. struct cc_entry{
  11.     int index;
  12.     char *description;
  13.     };


  14. struct flaginfo input_flags[]={
  15.     {IGNBRK, "Ignore break condition"},
  16.     {BRKINT, "Signal interrupt on break"},
  17.     {IGNPAR, "Ignore chars with parity errors"},
  18.     {PARMRK, "Mark parity errors"},
  19.     {INPCK, "Enable input parity check"},
  20.     {ISTRIP, "Strip character"},
  21.     {INLCR, "Map NL to CR on input"},
  22.     {IGNCR, "Ignore CR"},
  23.     {ICRNL, "Map CR to NL on input"},
  24.     {IXON, "Enable start/stop output control"},
  25.     //_IXANY, "Enable any char to restart output
  26.     {IXOFF, "Enable start/stop input control"},
  27.     {0, NULL}
  28.     };

  29. struct flaginfo local_flags[]={
  30.     ISIG, "Enable signals",
  31.     ICANON, "Canonical input(erase and kill)",
  32.     /*_XCASE, "Canonical upper/lower apperance", */
  33.     ECHO, "Enable echo",
  34.     ECHOE, "Echo ERASE as BS-SPACE-BS",
  35.     ECHOK, "Echo KILL by starting new line",
  36.     0, NULL
  37.     };
  38.     

  39. show_baud_rate(int thespeed)
  40. {
  41.     //print the speed in english
  42.     printf("the baud rate is ");
  43.     switch(thespeed){
  44.         case B300: printf("300\n"); break;
  45.         case B600: printf("600\n"); break;
  46.         case B1200: printf("1200\n"); break;
  47.         case B1800: printf("1800\n"); break;
  48.         case B2400: printf("2400\n"); break;
  49.         case B4800: printf("4800\n"); break;
  50.         case B9600: printf("9600\n"); break;
  51.         default: printf("Fast \n"); break;
  52.         }
  53.     }


  54. void show_flagset(int thevalue,struct flaginfo thebitnames[])
  55. {
  56.     //check each bit pattern and display descriptive title

  57.     int i=0;
  58.     for(i=0;thebitnames[i].fl_value;i++){
  59.         printf("%s is ",thebitnames[i].fl_name);
  60.         if(thevalue &thebitnames[i].fl_value)
  61.             printf("ON\n");
  62.         else
  63.             printf("OFF\n");
  64.         }
  65.     }

  66. show_some_flags(struct termios *ttyp)
  67. {
  68.     /* show the values of two of the flag sets_: c_iflag and c_lflag
  69.     add c_oflag and c_cflag is pretty routine ,just add new tales above and a
  70.     bit more code below */
  71.     show_flagset(ttyp->c_iflag,input_flags);
  72.     show_flagset(ttyp->c_lflag,local_flags);
  73.     }

  74. int main(int argc, char *argv[])
  75. {
  76.     struct termios ttyinfo; //this struct hold tty info
  77.     FILE *fp;

  78.     if((fp = fopen(ctermid(NULL),"r+"))==NULL)
  79.         exit(1);
  80.     if(tcgetattr(fileno(fp),&ttyinfo) == -1){
  81.         perror("Cannot get info about this terminal.");
  82.         exit(1);
  83.         }
  84.     //show info
  85.     show_baud_rate( cfgetospeed(&ttyinfo)); //show baud rate
  86.     printf("\n");
  87.     printf("The erase character is ascii %d,Ctrl- %c\n",ttyinfo.c_cc[VERASE],ttyinfo.c_cc[VERASE]-1+'A');
  88.     printf("\n");
  89.     printf("The line kill character is asii %d,Ctrl- %c\n,ttyinfo.c_cc[VKILL],ttyinfo.c_cc[VKILL]-1+'A');
  90.     show_some_flags(&ttyinfo);
  91.     return 0;
  92. }
4. showtty.c 执行结果。

点击(此处)折叠或打开

  1. gcc -o showtty showtty.c
  2.  ./showtty
  3. the baud rate is Fast

  4. The erase character is ascii 127,Ctrl- ?

  5. The line kill character is asii 21,Ctrl- U
  6. Ignore break condition is OFF
  7. Signal interrupt on break is OFF
  8. Ignore chars with parity errors is OFF
  9. Mark parity errors is OFF
  10. Enable input parity check is OFF
  11. Strip character is OFF
  12. Map NL to CR on input is OFF
  13. Ignore CR is OFF
  14. Map CR to NL on input is ON
  15. Enable start/stop output control is ON
  16. Enable start/stop input control is OFF
  17. Enable signals is ON
  18. Canonical input(erase and kill) is ON
  19. Enable echo is ON
  20. Echo ERASE as BS-SPACE-BS is ON
  21. Echo KILL by starting new line is ON

阅读(1291) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~