18.2 综述
两种模式:
规范模式 以行位输入单位,比如shell命令行
非规范模式 不以行为输入单位,比如vim
每个终端设备都有一个输入和输出队列
终端行规程这个模块用于进行规范处理:
termios结构体用来检测和更改终端设备特性:
18.3-18.5 基本没啥,开发根本用不到
设备函数基本总共有以下13个:
其与终端的关系如下:
获取和设置设备属性的函数
18.6 stty命令
stty 是shell命令,功能等同于上面的tcgetattr和tcsetattr
root@ubun2004:/home/learnApue/apue.3e/daemons# stty -a
speed 38400 baud; rows 48; columns 203; 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; discard = ^O;
min = 1; time = 0;
-parenb -parodd -cmspar 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 -flusho -extproc
其中:
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; discard = ^O;
代表特殊字符,属于c_lflag字段,具体参见18.3
而下面的
-parenb -parodd -cmspar 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 -flusho -extproc
代表终端选项标志, 可能属于c_iflag字段,也可能属于c_oflag,具体参见18.5
18.7-18.8 波特率和行控制函数
不想看,基本不会用到
18.9 终端标识
控制终端的名字一直是/dev/tty,可以通过ctermid返回控制终端名字
isatty 判断是否为一个终端设备
root@ubun2004:/home/learnApue/apue.3e/termios# ./t_isatty
fd 0: tty
fd 1: tty
fd 2: tty
root@ubun2004:/home/learnApue/apue.3e/termios# ./t_isatty </etc/passwd 2>/dev/null
fd 0: not a tty
fd 1: tty
fd 2: not a tty
其中 2>/dev/null 的意思是,把错误信息重定向到/dev/null,而/dev/null会忽略所有信息,所以其实2>/dev/null就是忽略错误信息的意思
2>&1 的意思就是把错误流stderr也重定向到输出流stdout,这样stdout就同时输出两者的信息
ttyname返回终端设备路径名,需要注意控制终端和终端设备的区别,控制终端名字一般只有/dev/tty,而终端设备,可能是/dev/console之类
root@ubun2004:/home/learnApue/apue.3e/termios# ./t_ttyname < /dev/console 2> /dev/null
fd 0: /dev/console
fd 1: /dev/pts/0
fd 2: not a tty
18.10-18.11 规范模式和非规范模式
getpass 读取用户密码的函数,关闭回显
其他没啥
18.12 终端窗口的大小
下面函数用来在控制终端窗口大小改变的时候,打印出行数和列数
root@ubun2004:/home/learnApue/apue.3e/termios# ./winch
49 rows, 203 columns
SIGWINCH received
19 rows, 75 columns
SIGWINCH received
17 rows, 70 columns
SIGWINCH received
19 rows, 75 columns
SIGWINCH received
52 rows, 209 columns
SIGWINCH received
49 rows, 203 columns
18.13 termcap、terminfo和curse
主要是为了解决清屏,光标移动等而设置的函数,跟vi编辑器相关,就这些
总结
stackoverflow还是很有用的,2>/dev/null 或者 2>&1在上面可以得到很好的解答