From d2c4c34bc0b12f2f091bb59b39fc3676d66379d7 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 24 Jan 2018 21:32:05 -0800 Subject: [PATCH] Replace std::cin.get with getchar. Fix #334 To avoid C++ iostream headache: std::cin has a bad() state std::{cin,cout} are also tied by default, reading from cin will cause a flush of cout, which may deadlock --- src/command_line.cc | 4 ++-- src/language_server_api.cc | 5 +++-- src/test.cc | 9 +++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/command_line.cc b/src/command_line.cc index 3c8b023f..e1674dee 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include #include @@ -478,7 +478,7 @@ int main(int argc, char** argv) { if (HasOption(options, "--wait-for-input")) { std::cerr << std::endl << "[Enter] to exit" << std::endl; - std::cin.get(); + getchar(); } if (print_help) { diff --git a/src/language_server_api.cc b/src/language_server_api.cc index 08f3f6b5..440e0fee 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -5,6 +5,7 @@ #include #include +#include #include MessageRegistry* MessageRegistry::instance_ = nullptr; @@ -107,8 +108,8 @@ TEST_SUITE("FindIncludeLine") { } optional ReadCharFromStdinBlocking() { - char c; - if (std::cin.read(&c, 1)) + int c = getchar(); + if (c >= 0) return c; return nullopt; } diff --git a/src/test.cc b/src/test.cc index e83004f7..264be2e3 100644 --- a/src/test.cc +++ b/src/test.cc @@ -5,6 +5,7 @@ #include "serializer.h" #include "utils.h" +#include #include void Write(const std::vector& strs) { @@ -96,8 +97,8 @@ std::string FindExpectedOutputForFilename( } std::cerr << "Couldn't find expected output for " << filename << std::endl; - std::cin.get(); - std::cin.get(); + getchar(); + getchar(); return "{}"; } @@ -243,8 +244,8 @@ bool RunIndexTests(const std::string& filter_path, bool enable_update) { << "[Enter to continue - type u to update test, a to update all]"; char c = 'u'; if (!update_all) { - c = (char)std::cin.get(); - std::cin.get(); + c = getchar(); + getchar(); } if (c == 'a')