编程语言
首页 > 编程语言> > 终端控制程序例子

终端控制程序例子

作者:互联网

#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<string.h>
#include<signal.h>
#include<fcntl.h>

#define ASK "Do you want another transaction"
#define TRIES 3
#define SLEEPTIME 2
#define BEEP putchar('\a')

void ctrl_c_handler(int);
void tty_mode(int);
void set_cr_noecho_mode();
void set_nodelay_mode();
int get_response(char *,int);
void choose_response(int);

int main()
{
 int response;
 tty_mode(0);
 set_nodelay_mode();
 set_cr_noecho_mode();
 signal(SIGINT,ctrl_c_handler);
 signal(SIGQUIT,SIG_IGN);
 response=get_response(ASK,TRIES);
 tty_mode(1);
 choose_response(response);
 return response;
}

int get_response(char *question,int maxtries)
{
 int input;
 printf("%s(y/n)?",question);
 fflush(stdout);
 while(1)
 {
  sleep(SLEEPTIME);
  input=tolower(get_ok_char());
  if(input=='y')
  {
   return 0;
  }
  else if(input=='n')
  {
   return 1;
  }
  if(maxtries--==0)
  {
   //printf("连接超时\n");
   return 2;
  }
 // BEEP;
 }
}

int get_ok_char()
{
 int c;
 while((c=getchar())!=EOF&&strchr("YyNn",c)==NULL)
  ;
 return c;
}

void set_cr_noecho_mode()
{
 struct termios ttystate;
 tcgetattr(0,&ttystate);
 ttystate.c_lflag&=~ICANON;
 ttystate.c_lflag&=~ECHO;
 ttystate.c_cc[VMIN]=1;
 tcsetattr(0,TCSANOW,&ttystate);
}

void set_nodelay_mode()
{
 int termflags;
 termflags=fcntl(0,F_GETFL);
 termflags|=O_NDELAY;
 fcntl(0,F_SETFL,termflags);
}

void tty_mode(int how)
{
 static struct termios original_mode;
 static int original_flags;
 static int stored=0;

 if(how==0)
 {
  tcgetattr(0,&original_mode);
  original_flags=fcntl(0,F_GETFL);
  stored=1;
 }

 else if(stored)
 {
  tcsetattr(0,TCSANOW,&original_mode);
  fcntl(0,F_SETFL,original_flags);
 }
}

void ctrl_c_handler(int signum)
{
 tty_mode(1);
 printf("ctrl+c进入,程序回滚\n");
 //exit(1);
}

void choose_response(int response)
{
 switch(response)
 {
 case 0:
  printf("你的选择是:确认\n");break;
 case 1:
  printf("你的选择是:否定\n");break;
 case 2:
  printf("选择超时\n");break;
  }
}
#include<stdio.h>
#include<termios.h>

#define QUESTION "DO you wanna another transacuion"

void tty_mode(int mode);
void set_crmode();
int get_response(char *);

int main()
{
 int response;
 tty_mode(0);
 set_crmode();
 response=get_response(QUESTION);
 tty_mode(1);
 printf("你的结果是:%d\n",response);
 return response;
}

int get_response(char *question)
{
 printf("%s(y/n)?",question);
 char input;
 while(1)
 {
  switch(input=getchar())
  {
   case 'Y':
   case 'y':return 0;
   case 'N':
   case 'n':return 1;
   case EOF:return 1;
   default:
         printf("\nconnot nderstand %c",input);
	 printf("Please type y or no\n");
  }

 }

}

void tty_mode(int mode)
{ 
 static struct termios original_mode;
 if(mode==0)
 {
  tcgetattr(0,&original_mode);
 // printf("c_cc[MVIM]=%d\n",original_mode.c_cc[VMIN]);
 }
 else
  tcsetattr(0,TCSANOW,&original_mode);
}


void set_crmode()
{
 struct termios ttystate;
 tcgetattr(0,&ttystate);
 ttystate.c_lflag&=~ICANON;
 ttystate.c_cc[VMIN]=1;
 tcsetattr(0,TCSANOW,&ttystate);
}
 

标签:int,void,控制程序,例子,ttystate,mode,printf,response,终端
来源: https://blog.csdn.net/suliangkuanjiayou/article/details/86666552