stty學習記

來源:互聯網
上載者:User

<1>簡潔方式,顯示傳輸速率
stty
speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?; swtch = M-^?;
ixany iutf8

 

<2>顯示全部設定的命令
stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^/; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;
swtch = M-^?; 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

 

重要一點
    屬性前面的減號表示被禁止
    沒有減號說明使能

 

<3>從編程的角度來看

上面一堆在struct termios{} 中有對應位,或者對應字元,c_cc[]內為對應字元

在include/asm-i386/termbits.h中定義這個結構及相應位

struct termios {
    tcflag_t c_iflag;        /* input mode flags */
    tcflag_t c_oflag;        /* output mode flags */
    tcflag_t c_cflag;        /* control mode flags */
    tcflag_t c_lflag;        /* local mode flags */
    cc_t c_line;            /* line discipline */
    cc_t c_cc[NCCS];        /* control characters */
};

 

<3.1>輸入模式


c_iflag包含如下標誌:
/* c_iflag bits */
#define IGNBRK    0000001
#define BRKINT    0000002
#define IGNPAR    0000004
#define PARMRK    0000010
#define INPCK    0000020
#define ISTRIP    0000040
#define INLCR    0000100
#define IGNCR    0000200
#define ICRNL    0000400
#define IUCLC    0001000
#define IXON    0002000
#define IXANY    0004000
#define IXOFF    0010000
#define IMAXBEL    0020000
#define IUTF8    0040000

brkint     中斷時發出 INTR 訊號。
-brkint    中斷時不發出 INTR 訊號。


icrnl       輸入時將 CR 映射為 NL。
-icrnl      輸入時不將 CR 映射為 NL。


ignbrk    輸入時忽略 BREAK。
-ignbrk   輸入時不忽略 BREAK。


igncr      輸入時忽略 CR。
-igncr     輸入時不忽略 CR。


ignpar    忽略奇偶錯誤。
-ignpar   不忽略奇偶錯誤。


inlcr       輸入時將 NL 映射為 CR。
-inlcr     輸入時不將 NL 映射為 CR。


inpck     啟用同位。
-inpck    禁用同位。


istrip     將輸入字元剝離到 7 位。
-istrip    不將輸入字元剝離到 7 位。


iuclc       將大寫字母字元對應表為小寫。
-iuclc     不將大寫字母字元對應表為小寫。


ixany     允許任何字元重新啟動輸出。
-ixany    只允許 START(Ctrl-Q 按鍵順序)重新啟動輸出。


ixoff      當輸入隊列接近空或滿時,發送 START/STOP 字元。
-ixoff     不發送 START/STOP 字元。


ixon      啟用 START/STOP 輸出控制。一旦啟用 START/STOP 輸出控制,
            您可以按下 Ctrl-S 按鍵順序暫停向工作站的輸出,也可按下 Ctrl-Q 按鍵順序恢複輸出。
-ixon        禁用 START/STOP 輸出控制。


imaxbel    當輸入溢出時,回送 BEL 字元並且廢棄最後的輸入字元。
-imaxbel   當輸入溢出時,廢棄所有輸入。


parmrk     標記奇偶錯誤。
-parmrk    不標記奇偶錯誤[1]。


<3.2>輸出模式
c_oflag包含如下標誌:
/* c_oflag bits */
#define OPOST    0000001
#define OLCUC    0000002
#define ONLCR    0000004
#define OCRNL    0000010
#define ONOCR    0000020
#define ONLRET    0000040
#define OFILL    0000100
#define OFDEL    0000200
#define NLDLY    0000400
#define   NL0    0000000
#define   NL1    0000400
#define CRDLY    0003000
#define   CR0    0000000
#define   CR1    0001000
#define   CR2    0002000
#define   CR3    0003000
#define TABDLY    0014000
#define   TAB0    0000000
#define   TAB1    0004000
#define   TAB2    0010000
#define   TAB3    0014000
#define   XTABS    0014000
#define BSDLY    0020000
#define   BS0    0000000
#define   BS1    0020000
#define VTDLY    0040000
#define   VT0    0000000
#define   VT1    0040000
#define FFDLY    0100000
#define   FF0    0000000
#define   FF1    0100000

bs0, bs1                  為退格符選擇延遲樣式(bs0 表示沒有延遲)。
cr0, cr1, cr2, cr3     為 CR 字元選擇延遲樣式(cr0 表示沒有延遲)。
ff0, ff1     為換頁選擇延遲樣式(ff0 表示沒有延遲)。
nl0, nl1     為 NL 字元選擇延遲樣式(nl0 表示沒有延遲)。
ofill     使用延遲填充字元。
-ofill     使用延遲定時。
ocrnl     將 CR 字元對應表為 NL 字元。
-ocrnl     不將 CR 字元對應表為 NL 字元。
olcuc     輸出時將小寫字母字元對應表為大寫。
-olcuc     輸出時不將小寫字母字元對應表為大寫。
onlcr     將 NL 字元對應表為 CR-NL 字元。
-onlcr     不將 NL 字元對應表為 CR-NL 字元。
onlret     在終端 NL 執行 CR 功能。
-onlret     在終端 NL 不執行 CR 功能。
onocr     不在零列輸出 CR 字元。
-onocr     在零列輸出 CR 字元。
opost     處理輸出。
-opost     不處理輸出;即忽略所有其它輸出選項。
ofdel     使用 DEL 字元作為填充字元。
-ofdel     使用 NUL 字元作為填充字元。
tab0, tab1, tab2     為水平定位字元選擇延遲樣式(tab0 表示沒有延遲)。
tab3                     擴充定位字元至多個空格。
vt0, vt1             為垂直定位字元選擇延遲樣式(vt0 表示沒有延遲)[1]。

 

<3.3>控制模式
/* c_cflag bit meaning */
#define CBAUD    0010017
#define B0    0000000        /* hang up */
#define B50    0000001
#define B75    0000002
#define B110    0000003
#define B134    0000004
#define B150    0000005
#define B200    0000006
#define B300    0000007
#define B600    0000010
#define B1200    0000011
#define B1800    0000012
#define B2400    0000013
#define B4800    0000014
#define B9600    0000015
#define B19200    0000016
#define B38400    0000017
#define EXTA B19200
#define EXTB B38400
#define CSIZE    0000060
#define   CS5    0000000
#define   CS6    0000020
#define   CS7    0000040
#define   CS8    0000060
#define CSTOPB    0000100
#define CREAD    0000200
#define PARENB    0000400
#define PARODD    0001000
#define HUPCL    0002000
#define CLOCAL    0004000
#define CBAUDEX 0010000
#define    B57600 0010001
#define   B115200 0010002
#define   B230400 0010003
#define   B460800 0010004
#define   B500000 0010005
#define   B576000 0010006
#define   B921600 0010007
#define B1000000 0010010
#define B1152000 0010011
#define B1500000 0010012
#define B2000000 0010013
#define B2500000 0010014
#define B3000000 0010015
#define B3500000 0010016
#define B4000000 0010017
#define CIBAUD      002003600000    /* input baud rate (not used) */
#define CMSPAR      010000000000        /* mark or space (stick) parity */
#define CRTSCTS      020000000000        /* flow control */

clocal      假定一行沒有數據機控制。
-clocal     假定一行帶有數據機控制。
cread     啟用接收器。
-cread     禁用接收器。
cstopb     每個字元選擇兩個停止位。
-cstopb     每個字元選擇一個停止位。
cs5, cs6, cs7, cs8     選擇字元大小。
hup, hupcl     最後關閉時掛起撥號連線。
-hup, -hupcl     最後關閉時不掛起撥號連線。
parenb     啟用奇偶性校正的產生和檢測。
-parenb     禁用奇偶性校正的產生和檢測。
parodd     選擇奇數同位。
-parodd     選擇偶校正。
0     立即掛起電話線路。
speed     將工作站輸入和輸出速度設定為指定的 speed 數(以位/秒為單位)。
            並不是所有的硬體介面都支援所有的速度。
            speed 的可能值有:50、75、110、134、200、300、600、1200、1800、
            2400、4800、9600、19200、19.2、38400、38.4、exta 和 extb。
注:
exta、19200 和 19.2 是同義字;extb、38400 和 38.4 是同義字。
ispeed speed     將工作站輸入速度設定為指定的 speed 數(以位/秒為單位)。
                    並不是所有的硬體介面都支援所有的速度,而且並不是所有的硬體介面都支援該選項。
                    speed 的可能值與 speed 選項相同。
ospeed speed     將工作站輸出速度設定為指定的 speed 數(以位/秒為單位)。
                    並不是所有的硬體介面都支援所有的速度,而且並不是所有的硬體介面都支援該選項。
                    speed 的可能值與 speed 選項相同[1]。

 

<3.4>
本地模式
/* c_lflag bits */
#define ISIG    0000001
#define ICANON    0000002
#define XCASE    0000004
#define ECHO    0000010
#define ECHOE    0000020
#define ECHOK    0000040
#define ECHONL    0000100
#define NOFLSH    0000200
#define TOSTOP    0000400
#define ECHOCTL    0001000
#define ECHOPRT    0002000
#define ECHOKE    0004000
#define FLUSHO    0010000
#define PENDIN    0040000
#define IEXTEN    0100000

echo      回送每個輸入的字元。
-echo     不回送字元。
echoctl     以 ^X(Ctrl-X)回送控制字元,X 是將 100 八進位加到控制字元代碼中給出的字元。
-echoctl     不以 ^X(Ctrl-X)回送控制字元。
echoe     以“backspace space backspace”字串回送 ERASE 字元。
注:
該模式不保持對列位置的跟蹤,因此您可能在擦除定位字元和逸出序列等符號時得到意外的結果。
-echoe     不回送 ERASE 字元,只回送退格符。
echok     在 KILL 字元後回送 NL 字元。
-echok     在 KILL 字元後不回送 NL 字元。
echoke     通過擦除輸出行上的每個字元,回送 KILL 字元。
-echoke     只回送 KILL 字元。
echonl     回送 NL 字元。
-echonl     不回送 NL 字元。
echoprt     以 /(斜杠)和 / (反斜線) 向後回送擦除的字元。
-echoprt     不以 /(斜杠)和 / (反斜線) 向後回送擦除的字元。
icanon     啟用規範輸入(規範輸入允許使用 ERASE 和 KILL 字元進行輸入行的編輯)。
            請參閱 AIX 5L Version 5.2 Communications Programming Concepts 中的
             Line Discipline Module (ldterm) 中關於 canonical mode input 的討論。
-icanon     禁用規範輸入。
iexten     指定從輸入資料中識別實現性定義的功能。
            要識別以下控制字元,
            需要設定 iexten:eol2、dsusp、reprint、discard、werase、lnext。
            與這些模式關聯的功能也需要設定 iexten:imaxbel、echoke、echoprt、echoctl。
-iexten     指定從輸入資料中識別實現性定義的功能。
isig         啟用對特殊控制字元(INTR、SUSP 和 QUIT 特殊控制字元)的字元檢查。
-isig     禁用對特殊控制字元(INTR、SUSP 和 QUIT 特殊控制字元)的字元檢查。
noflsh     不清除 INTR、SUSP 或 QUIT 控制字元之後的緩衝區。
-noflsh     清除 INTR、SUSP 或 QUIT 控制字元之後的緩衝區。
pending     下次讀操作暫掛或輸入到達時,要重新輸入從原始模式轉換為規範模式後被暫掛的輸入。
            暫掛是一個內部狀態位。
-pending     沒有文本暫掛。
tostop     為背景輸出發出 SIGTOU 訊號。
-tostop     不為背景輸出發出 SIGTOU 訊號。
xcase     在輸入中回送大寫字元,並在輸出顯示的大寫字元之前加上 / (反斜線)。
-xcase     不在輸入時回送大寫字元[1]。

 

<3.5>控制字元
/* c_cc characters */
#define VINTR 0
#define VQUIT 1
#define VERASE 2
#define VKILL 3
#define VEOF 4
#define VTIME 5
#define VMIN 6
#define VSWTC 7
#define VSTART 8
#define VSTOP 9
#define VSUSP 10
#define VEOL 11
#define VREPRINT 12
#define VDISCARD 13
#define VWERASE 14
#define VLNEXT 15
#define VEOL2 16
控制指定
要將一個控制字元指定到某字串中,請輸入:
stty Control String
其中,Control 參數可以是 INTR、QUIT、ERASE、KILL、EOF、EOL、EOL2、START、STOP、
SUSP、DSUSP、REPRINT、DISCARD、WERASE、
LNEXT、MIN 或 TIME 參數。(使用字元 MIN 和 TIME 時,請加上 -icanon 選項。)[1]

 

 

<4>實驗

<4.1>列印出終端的行數和列數
stty size
24 80
即24行,80列


<4.2>
stty cols 25
把列設為25行,發現輸出變窄
stty -a
speed 38400 baud;
rows 24; columns 25;
line = 0;
intr = ^C; quit = ^/;
erase = ^?; kill = ^U;
eof = ^D; eol = <undef>;
eol2 = <undef>;
swtch = <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

<4.3>stty speed
顯示速率

<4.4>stty intr c
c變為中斷鍵,想輸入c不可能了。

<4.5>stty raw
發現ctrl+c不管用了,輸出混亂
允許原始模式輸入(不包括輸入處理,例如 erase、kill 或 interrupt);傳回奇偶(校正)位。

<4.6>stty -raw
允許規範輸入方式。

<4.7>在命令列下,禁止輸出大寫的方法
stty iuclc #開啟
stty -iuclc #恢複

<4.8>在命令列下禁止輸出小寫
stty olcuc #開啟
stty -olcuc#恢複

<4.9>stty sane
將參數重新設定為合理的值。

<4.10>忽略斷行符號符
stty igncr #開啟
發現enter不起作用了,要顯示執行效果,需用ctrl+j

stty -igncr#恢複,要顯示執行效果,需用ctrl+j

<4.11>stty erase x
退格刪除鍵變成x,想輸入x,不可能了,因為x是退格鍵
stty erase ^?返回原來退格鍵

<4.12>stty -echo
發現按鍵,什麼也沒有出現
stty echo
輸入顯示

<4.13>stty -icanon
關閉驅動程式中的規範模式處理,跳過緩衝層,使用者不能刪除字元,

測試一下
gui.c
#include<stdio.h>
main()
{
    char c;
    FILE *fp_tty;
    fp_tty=fopen("/dev/tty","r");
    if(fp_tty==NULL)
        exit(1);
    while((c=getc(fp_tty))!=EOF)
    {
        printf("%d",c);
    }
}

gcc gui.c -o gui

stty icanon
./gui
d
10010f
10210d
10010f
10210
可以用退格鍵,並且顯示數字還得按斷行符號鍵, 從10( enter)就可以看出來

stty -icanon
./gui
d100f102g103h104
10

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.