1
0

play_again3b.c 3.1 KB

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