1
0

play_again3a.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* play_again3.c
  2. * purpose: ask if user wants another transaction
  3. * method: set tty into chr-by-chr, no-echo mode
  4. * set tty into no-delay mode
  5. * read char, return result
  6. * returns: 0=>yes, 1=>no, 2=>timeout
  7. * better: reset terminal mode on Interrupt
  8. */
  9. #include <stdio.h>
  10. #include <termios.h>
  11. #include <fcntl.h>
  12. #include <string.h>
  13. #include <signal.h>
  14. #define ASK "Do you want another transaction"
  15. #define TRIES 3 /* max tries */
  16. #define SLEEPTIME 2 /* time per try */
  17. #define BEEP putchar('\a') /* alert user */
  18. main()
  19. {
  20. int response;
  21. signal( SIGINT, SIG_IGN );
  22. signal( SIGTSTP, SIG_IGN );
  23. signal( SIGQUIT, SIG_IGN );
  24. tty_mode(0); /* save current mode */
  25. set_cr_noecho_mode(); /* set -icanon, -echo */
  26. set_nodelay_mode(); /* noinput => EOF */
  27. response = get_response(ASK, TRIES); /* get some answer */
  28. tty_mode(1); /* restore orig mode */
  29. return response;
  30. }
  31. get_response( char *question , int maxtries)
  32. /*
  33. * purpose: ask a question and wait for a y/n answer or maxtries
  34. * method: use getchar and complain about non-y/n input
  35. * returns: 0=>yes, 1=>no, 2=>timeout
  36. */
  37. {
  38. int input;
  39. printf("%s (y/n)?", question); /* ask */
  40. fflush(stdout); /* force output */
  41. while ( 1 ){
  42. sleep(SLEEPTIME); /* wait a bit */
  43. input = tolower(get_ok_char()); /* get next chr */
  44. if ( input == 'y' )
  45. return 0;
  46. if ( input == 'n' )
  47. return 1;
  48. if ( maxtries-- == 0 ) /* outatime? */
  49. return 2; /* sayso */
  50. BEEP;
  51. }
  52. }
  53. /*
  54. * skip over non-legal chars and return y,Y,n,N or EOF
  55. */
  56. get_ok_char()
  57. {
  58. int c;
  59. while( ( c = getchar() ) != EOF && strchr("yYnN",c) == NULL )
  60. ;
  61. return c;
  62. }
  63. set_cr_noecho_mode()
  64. /*
  65. * purpose: put file descriptor 0 into chr-by-chr mode and noecho mode
  66. * method: use bits in termios
  67. */
  68. {
  69. struct termios ttystate;
  70. tcgetattr( 0, &ttystate); /* read curr. setting */
  71. ttystate.c_lflag &= ~ICANON; /* no buffering */
  72. ttystate.c_lflag &= ~ECHO; /* no echo either */
  73. ttystate.c_cc[VMIN] = 1; /* get 1 char at a time */
  74. tcsetattr( 0 , TCSANOW, &ttystate); /* install settings */
  75. }
  76. set_nodelay_mode()
  77. /*
  78. * purpose: put file descriptor 0 into no-delay mode
  79. * method: use fcntl to set bits
  80. * notes: tcsetattr() will do something similar, but it is complicated
  81. */
  82. {
  83. int termflags;
  84. termflags = fcntl(0, F_GETFL); /* read curr. settings */
  85. termflags |= O_NDELAY; /* flip on nodelay bit */
  86. fcntl(0, F_SETFL, termflags); /* and install 'em */
  87. }
  88. /* how == 0 => save current mode, how == 1 => restore mode */
  89. /* this version handles termios and fcntl flags */
  90. tty_mode(int how)
  91. {
  92. static struct termios original_mode;
  93. static int original_flags;
  94. if ( how == 0 ){
  95. tcgetattr(0, &original_mode);
  96. original_flags = fcntl(0, F_GETFL);
  97. }
  98. else {
  99. tcsetattr(0, TCSANOW, &original_mode);
  100. fcntl( 0, F_SETFL, original_flags);
  101. }
  102. }