Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. simple_prompt(const char *prompt, int maxlen, bool echo)
  2. {
  3. int length;
  4. char *destination;
  5. FILE *termin,
  6. *termout;
  7. #ifdef HAVE_TERMIOS_H
  8. struct termios t_orig,
  9. t;
  10. #else
  11. #ifdef WIN32
  12. HANDLE t = NULL;
  13. LPDWORD t_orig = NULL;
  14. #endif
  15. #endif
  16. destination = (char *) malloc(maxlen + 1);
  17. if (!destination)
  18. return NULL;
  19.  
  20. /*
  21. * Do not try to collapse these into one "w+" mode file. Doesn't work on
  22. * some platforms (eg, HPUX 10.20).
  23. */
  24. termin = fopen(DEVTTY, "r");
  25. termout = fopen(DEVTTY, "w");
  26. if (!termin || !termout
  27. #ifdef WIN32
  28. /* See DEVTTY comment for msys */
  29. || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
  30. #endif
  31. )
  32. {
  33. if (termin)
  34. fclose(termin);
  35. if (termout)
  36. fclose(termout);
  37. termin = stdin;
  38. termout = stderr;
  39. }
  40.  
  41. #ifdef HAVE_TERMIOS_H
  42. if (!echo)
  43. {
  44. tcgetattr(fileno(termin), &t);
  45. t_orig = t;
  46. t.c_lflag &= ~ECHO;
  47. tcsetattr(fileno(termin), TCSAFLUSH, &t);
  48. }
  49. #else
  50. #ifdef WIN32
  51. if (!echo)
  52. {
  53. /* get a new handle to turn echo off */
  54. t_orig = (LPDWORD) malloc(sizeof(DWORD));
  55. t = GetStdHandle(STD_INPUT_HANDLE);
  56.  
  57. /* save the old configuration first */
  58. GetConsoleMode(t, t_orig);
  59.  
  60. /* set to the new mode */
  61. SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
  62. }
  63. #endif
  64. #endif
  65. if (prompt)
  66. {
  67. fputs(_(prompt), termout);
  68. fflush(termout);
  69. }
  70.  
  71. if (fgets(destination, maxlen + 1, termin) == NULL)
  72. destination[0] = '';
  73.  
  74. length = strlen(destination);
  75. if (length > 0 && destination[length - 1] != 'n')
  76. {
  77. /* eat rest of the line */
  78. char buf[128];
  79. int buflen;
  80.  
  81. do
  82. {
  83. if (fgets(buf, sizeof(buf), termin) == NULL)
  84. break;
  85. buflen = strlen(buf);
  86. } while (buflen > 0 && buf[buflen - 1] != 'n');
  87. }
  88.  
  89. if (length > 0 && destination[length - 1] == 'n')
  90. /* remove trailing newline */
  91. destination[length - 1] = '';
  92. #ifdef HAVE_TERMIOS_H
  93. if (!echo)
  94. {
  95. tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
  96. fputs("n", termout);
  97. fflush(termout);
  98. }
  99. #else
  100. #ifdef WIN32
  101. if (!echo)
  102. {
  103. /* reset to the original console mode */
  104. SetConsoleMode(t, *t_orig);
  105. fputs("n", termout);
  106. fflush(termout);
  107. free(t_orig);
  108. }
  109. #endif
  110. #endif
  111. if (termin != stdin)
  112. {
  113. fclose(termin);
  114. fclose(termout);
  115. }
  116.  
  117. return destination;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement