aboutsummaryrefslogtreecommitdiff
path: root/bpkg/utility.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-10-10 06:12:31 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-10-10 06:12:31 +0200
commit803acc23f8cea3079681e9e624702e104adfd775 (patch)
treef29e26a18a834d1ddcab4789142b371ade47dbbc /bpkg/utility.cxx
parent0502b85bacfa8f7735de6f5030b320829e50fb54 (diff)
Implement disfigure step in build command
Diffstat (limited to 'bpkg/utility.cxx')
-rw-r--r--bpkg/utility.cxx41
1 files changed, 41 insertions, 0 deletions
diff --git a/bpkg/utility.cxx b/bpkg/utility.cxx
index 9b194df..6cf6c35 100644
--- a/bpkg/utility.cxx
+++ b/bpkg/utility.cxx
@@ -4,6 +4,7 @@
#include <bpkg/utility>
+#include <iostream> // cout cin
#include <system_error>
#include <butl/process>
@@ -17,6 +18,46 @@ using namespace butl;
namespace bpkg
{
bool
+ yn_prompt (const char* prompt, char def)
+ {
+ // Writing a robust Y/N prompt is more difficult than one would
+ // expect...
+ //
+ string a;
+ do
+ {
+ *diag_stream << prompt << ' ';
+
+ // getline() will set the failbit if it failed to extract anything,
+ // not even the delimiter and eofbit if it reached eof before seeing
+ // the delimiter.
+ //
+ //
+ getline (cin, a);
+
+ bool f (cin.fail ());
+ bool e (cin.eof ());
+
+ if (f || e)
+ *diag_stream << endl; // Assume no delimiter (newline).
+
+ if (f)
+ fail << "unable to read y/n answer from STDOUT";
+
+ if (a.empty () && def != '\0')
+ {
+ // Don't treat eof as the default answer. We need to see the
+ // actual newline.
+ //
+ if (!e)
+ a = def;
+ }
+ } while (a != "y" && a != "n");
+
+ return a == "y";
+ }
+
+ bool
exists (const path& f)
{
try