终端驱动程序:几个简单例子

例1. 显示回显位的状态

#include <stdio.h>
#include <termios.h>
#include <stdlib.h>

int main(void)
{
        struct termios tty_info;
	int result;

	result = tcgetattr(0,&tty_info); /*0 represent stardard input*/
	if(result == -1){
            perror("tcgetattr");
	    exit(1);
	}

	if(info.c_lflag & ECHO)  /*c_lflag is local mode flags*/
	    printf("echo is working, cause its bit is 1\n");
	else
	    printf("echo is working, cause its bit is 0\n");

	return 0;
}

例2.改变回显位状态,若命令行参数以‘y' 开始,终端回显位被开启,其他字符则关闭

#include <stdio.h>
#include <termios.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    struct termios tty_info;

	if(argc == 1)
	    exit(0);

	if(tcgetattr(0,&tty_info) == -1){
        perror("tcgetattr");
	    exit(1);
	}

	if(argv[1][0] == 'y'){
        tty_info.c_lflag |= ECHO;
	}
	else{
	    tty_info.c_lflag &= ~ECHO;	
	}

	if(tcsetattr(0,TCSANOW,&tty_info) == 1){
        perror("tcsetattr");
		exit(2);
	}
	
    return 0;
}


例3.显示大量驱动程序属性

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

struct flag_info {
    int fl_value;
	char *fl_name;
};

struct flag_info input_flags[] = {
    IGNBRK, "Ignore break condition",
	BRKINT, "Signal interrupt on break",
	IGNPAR, "Ignore chars with parity errors",
	PARMRK, "Mark parity errors",
	INPCK,  "Enable input parity check",
	ISTRIP, "Strip character",
	INLCR,  "Map NL to CR on input",
	IGNCR,  "Ignore CR",
	ICRNL,  "Map CR to NL on input",
	IXON,   "Enable start/stop output control",
	/* _IXANY, "enable start/stop output control",*/
	IXOFF,  "Enable start/stop input control",
	0,      NULL
};

struct flag_info local_flags[] = {
    ISIG,   "Enable signals",
	ICANON, "Cannonical input(erase and kill)",
	/* _XCASE,   "Cannonical upper/lower appearance", */
	ECHO,   "Enable echo",
	ECHOE,  "ECHO ERASE as BS-SPACE-BS",
	ECHOK,  "ECHO KILL by starting new line",
	0,      NULL
};

void showbaud (int thespeed);
void show_some_flags (struct termios *tty);
void show_flagset (int thevalue,struct flag_info thebitnames[]);

int main (void)
{
    struct termios tty_info;          /*this structure hold tty information*/
	if(tcgetattr(0,&tty_info) == -1){ /*get information from stdin*/
        perror("cannot get parameters from stdin");  
		exit(1);
	}

	/*show information*/
    showbaud(cfgetospeed(&tty_info)); /*get and show the baud rate*/
	printf("The erase character is ascii %d, Ctrl - %c\n",
		tty_info.c_cc[VERASE],tty_info.c_cc[VERASE]-1+'A');
	printf("The line kill character is ascii %d, Ctrl - %c\n",
		tty_info.c_cc[VKILL],tty_info.c_cc[VKILL]-1+'A');

	show_some_flags(&tty_info);       /*show misc.flag*/

	return 0;
}

/*prints the speed in english*/
void showbaud (int thespeed)
{
    printf("the baud rate is ");
	switch(thespeed){
        case B300: printf("300\n"); break;
		case B600: printf("600\n"); break;
		case B1200: printf("1200\n"); break;
		case B1800: printf("1800\n"); break;
		case B2400: printf("2400\n"); break;
		case B4800: printf("4800\n"); break;
		case B9600: printf("9600\n"); break;
		default: printf("Fast\n"); break;
	}
}

/*show the values of two of the flag sets: c_iflag and c_lflag*/
void show_some_flags (struct termios *tty)
{
    show_flagset(tty->c_iflag,input_flags);
	printf("\n");
	show_flagset(tty->c_lflag,local_flags);
}

/*check each bit pattern and display descriptive title*/
void show_flagset (int thevalue,struct flag_info thebitnames[])
{
    int i;
	for(i=0;thebitnames[i].fl_value;i++){
        printf("%s is ",thebitnames[i].fl_name);
		if(thevalue & thebitnames[i].fl_value)
			printf("ON\n");
		else
			printf("OFF\n");
	}
}

PS:以上例子来自《Unix/Linux编程实践教程》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值