簡介終端的兩種工作模式:以行為單位的工作模式,以字符數或時間為單位自定義模式
終端判斷函數:
- int isatty(int fd)
終端屬性的獲取與設置:
- int tcgetattr(int fd,struct termios *termptr)
- int tcsetattr(int fd,int opt,const struct termios *termptr),opt選項如下
TCSANOW:不等數據傳輸完畢就立即改變屬性。
TCSADRAIN:等待所有數據傳輸結束才改變屬性。
TCSAFLUSH:等待所有數據傳輸結束,清空輸入輸出緩沖區才改變屬性。
終端名稱的獲取:
- char *ctermid(char *ptr),如果ptr非空,則將終端名稱(/dev/tty)寫入到此ptr中并返回;若為空,分配空間寫入后返回
終端屬性結構:struct termios
struct termios{
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_cc[NCCS];
};
termios關鍵字
c_oflag:控制輸出格式
- OPOST:如果屏蔽此關鍵字,換行后縮進為上一行最后一個字符位置的后一位
c_lflag:本地模式
- ECHO:回顯,如果屏蔽則不顯示輸入的字符,像輸入密碼一樣
- ICANON:行模式,屏蔽則變成自定義模式
- ISIG:使終端產生的信號(ctrl+c/ctrl+z等)起作用,屏蔽則忽略信號
c_iflag:控制輸入格式
- ICRNL:按下回車換行,屏蔽則不換行打印一個^M
- BRKINT:當在輸入行中檢測到一個終止狀態時,產生一個中斷
c_cc:見例2
例子
1.行模式,關閉回顯
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#define MAX_PASS_LEN 8
char * getpass(const char *prompt){
static char buf[MAX_PASS_LEN+1];
char *ptr;
struct termios ts,ots;
FILE *fp;
int c;
if((fp=fopen(ctermid(NULL),"r+")) == NULL)
return 0;
setbuf(fp,NULL);
tcgetattr(fileno(fp),&ts);
ots=ts;
ts.c_lflag &= ~ECHO;
tcsetattr(fileno(fp),TCSAFLUSH,&ts);
fputs(prompt,fp);
//ptr < &buf[x] 兩個內存地址比較,最多填充到buf[MAX_PASS_LEN-1]
//*ptr=0 在接下來的一位填充0表示結束
ptr=buf;
while((c=getc(fp)) != EOF && c != '\n')
if(ptr < &buf[MAX_PASS_LEN])
*ptr++ = c;
*ptr=0;
putc('\n',fp);
tcsetattr(fileno(fp),TCSAFLUSH,&ots);
fclose(fp);
return buf;
}
int main(){
char *ptr;
if((ptr=getpass("Enter password:")) == NULL)
perror("getpass word");
printf("passowrd: %s\n",ptr);
//先ptr++移到下一位置,接著ptr副本(未移動前的位置)進行*ptr=0
while(*ptr != 0)
*ptr++ =0;
return 0;
}
2.自定義模式
將termios結構中c_lflag字段的ICANON標志關閉就使終端處于非行模式,此時回車換行不作為行結束標識返回
自定義模式下有兩種結束標識:
- c_cc數組中的VMIN變量即c_cc[VMIN]作為最少字符結束標識,字符達到VMIN個就返回,c_cc[VMIN]=0表示不限容量
- c_cc數組中的VTIME變量即c_cc[VTIME]作為最短時間結束標識,從第一個字符輸入開始,經過VTIME時間后就返回,c_cc[VTIME]=0表示不限時間
另外,也可以同時指定上述兩個變量,只要有一個變量指定的條件成立就返回
以下為簡單自定義模式,字符長度達到10就返回,對SIGINT中斷做復位處理
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <signal.h>
static struct termios save_termios;
static int ttysavefd=-1;
int tty_cbreak(int fd){
struct termios buf;
if(tcgetattr(fd,&save_termios) <0){
perror("tcgetattr error");
return -1;
}
buf=save_termios;
buf.c_lflag &= ~ICANON;
buf.c_iflag &= ~ICRNL;
buf.c_cc[VMIN]=10;
buf.c_cc[VTIME]=0;
if(tcsetattr(fd,TCSAFLUSH,&buf) <0){
perror("tcsetattr error");
return -1;
}
ttysavefd=fd;
return 0;
}
int tty_reset(int fd){
if(tcsetattr(fd,TCSAFLUSH,&save_termios) <0){
return -1;
}
return 0;
}
int main(int argc,char *argv[]){
if(signal(SIGINT,sig_catch) == SIG_ERR)
perror("signal error");
if(tty_cbreak(STDIN_FILENO) <0)
perror("tty_cbreak error");
char c[11]={0};
int i;
puts("cbreak mode,terminate with sigint");
while(i=read(STDIN_FILENO,&c,10)){
printf("\n%s\n",c);
}
return 0;
}