類比 tcgetattr() 和 tcsetattr() 改變終端機屬性的指令。
顯示精簡屬性 (settings that deviate from sane values.):
$ stty speed 38400 baud; line = 0; -brkint -imaxbel iutf8
顯示所有屬性 (here carried out on a virtual console):
$ stty -a speed 38400 baud; rows 25; columns 80; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts -ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
- 第 1 行:終端機 line speed、window size、和 line discipline (0 代表 N_TTY,new line discipline)。
- 再來 3 行:終端機特殊字元設定。min 和 time 用在 noncanonical 模式輸入。
- 剩下依序是 c_cflag, c_iflag, c_oflag, and c_lflag,前置 hyphen (-) 代表關閉,否則啟用。
將中斷字元改為 Ctrl-L:
$ stty intr Ctrl-L $ stty intr 014 $ stty intr 0xC $ stty intr <實際字元>
如果要設定的字元會被 shell 或終端機驅動程式特別解釋,需要前置 literal next character (通常是 ^V):
$ stty intr Ctrl-VCtrl-L
啟用或停用 TOSTOP
$ stty tostop $ stty -tostop
有時程式改變終端機屬性而當掉卡住,可輸入:
Ctrl-J stty sane Ctrl-J
來回復 terminal flags 和特殊字元為 reasonable 狀態。Ctrl-J (ASCII 10) 是換行。在有些模式,終端機驅動程式不再對應 Enter 鍵 (ASCII 13) 到換行字元。第一個 Ctr-J 確保全新的指令行。
操作在指定終端機,不是 standard input,需要有權限:
# stty -a -F /dev/tty3 # stty -a < /dev/tty3
將中斷字元改為 Ctrl-L:
$ stty intr Ctrl-L $ stty speed 38400 baud; line = 0; intr = ^L;
開始 sleep,發現 Ctrl-C 沒作用,只是 echoed:
$ sleep 10 ^C
Ctrl-L 結束 sleep,顯示 termination status:
$ echo $? 130
130 表示程式由 signal number 130 – 128 = 2 (SIGINT) 所殺。
然後停用中斷字元:
$ stty intr undef $ stty speed 38400 baud; line = 0; intr = <undef>;
現在 Ctrl-C 和 Ctrl-L 都不會產生 SIGINT signal,必須用 Ctrl-\ 代替來結束程式:
$ sleep 10 ^C^L Control-C and Control-L are simply echoed Type Control-\ to generate SIGQUIT Quit
$ stty sane Return terminal to a sane state
參考
- TLPI §62
沒有留言:
張貼留言