From 2637b7479c3e531f891346dbe84c73805a8b5e36 Mon Sep 17 00:00:00 2001 From: Alexander Gluzsky Date: Sun, 3 Feb 2013 12:54:02 +0000 Subject: ability to change key password (because of fucked gpg which does not want to give us his stdin/stdout only via ugly windows console) git-svn-id: http://svn.miranda-ng.org/main/trunk@3406 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- .../include/libs/process/test/sparring_partner.cpp | 232 +++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 plugins/New_GPG/src/include/libs/process/test/sparring_partner.cpp (limited to 'plugins/New_GPG/src/include/libs/process/test/sparring_partner.cpp') diff --git a/plugins/New_GPG/src/include/libs/process/test/sparring_partner.cpp b/plugins/New_GPG/src/include/libs/process/test/sparring_partner.cpp new file mode 100644 index 0000000000..abfaf128f3 --- /dev/null +++ b/plugins/New_GPG/src/include/libs/process/test/sparring_partner.cpp @@ -0,0 +1,232 @@ +// Copyright (c) 2006, 2007 Julio M. Merino Vidal +// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling +// Copyright (c) 2009 Boris Schaeling +// Copyright (c) 2010 Felipe Tanus, Boris Schaeling +// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined(BOOST_POSIX_API) +# include +# include +# include +# include +#elif defined(BOOST_WINDOWS_API) +# include +#endif + +using namespace boost::program_options; + +int main(int argc, char *argv[]) +{ + options_description desc; + desc.add_options() + ("echo-stdout", value()) + ("echo-stderr", value()) + ("echo-stdout-stderr", value()) + ("echo-argv", bool_switch()) + ("exit-code", value()) + ("wait", value()) + ("is-closed-stdin", bool_switch()) + ("is-closed-stdout", bool_switch()) + ("is-closed-stderr", bool_switch()) + ("is-nul-stdin", bool_switch()) + ("is-nul-stdout", bool_switch()) + ("is-nul-stderr", bool_switch()) + ("loop", bool_switch()) + ("prefix", value()) + ("prefix-once", value()) + ("pwd", bool_switch()) + ("query-env", value()) + ("stdin-to-stdout", bool_switch()) +#if defined(BOOST_POSIX_API) + ("posix-echo-one", value >()->multitoken()) + ("posix-echo-two", value >()->multitoken()); +#elif defined(BOOST_WINDOWS_API) + ("windows-print-showwindow", bool_switch()) + ("windows-print-flags", bool_switch()); +#endif + + variables_map vm; + command_line_parser parser(argc, argv); + store(parser.options(desc).allow_unregistered().run(), vm); + notify(vm); + + if (vm.count("echo-stdout")) + { + std::cout << vm["echo-stdout"].as() << std::endl; + } + else if (vm.count("echo-stderr")) + { + std::cerr << vm["echo-stderr"].as() << std::endl; + } + else if (vm.count("echo-stdout-stderr")) + { + std::cout << vm["echo-stdout-stderr"].as() << std::endl; + std::cerr << vm["echo-stdout-stderr"].as() << std::endl; + } + else if (vm["echo-argv"].as()) + { + std::vector v, v2; + boost::push_back(v, boost::make_iterator_range(argv, argv + argc)); + boost::transform(v, std::back_inserter(v2), + "\"" + boost::lambda::_1 + "\""); + std::cout << boost::algorithm::join(v2, " ") << std::endl; + } + else if (vm.count("exit-code")) + { + return vm["exit-code"].as(); + } + else if (vm.count("wait")) + { + int sec = vm["wait"].as(); +#if defined(BOOST_POSIX_API) + sleep(sec); +#elif defined(BOOST_WINDOWS_API) + Sleep(sec * 1000); +#endif + } + else if (vm["is-closed-stdin"].as()) + { + std::string s; + std::cin >> s; + return std::cin.eof() ? EXIT_SUCCESS : EXIT_FAILURE; + } + else if (vm["is-closed-stdout"].as()) + { + std::cout << "foo" << std::endl; + return std::cout.bad() ? EXIT_SUCCESS : EXIT_FAILURE; + } + else if (vm["is-closed-stderr"].as()) + { + std::cerr << "foo" << std::endl; + return std::cerr.bad() ? EXIT_SUCCESS : EXIT_FAILURE; + } + else if (vm["is-nul-stdin"].as()) + { +#if defined(BOOST_POSIX_API) + char buffer[1]; + int res = read(STDIN_FILENO, buffer, 1); + return res != -1 ? EXIT_SUCCESS : EXIT_FAILURE; +#elif defined(BOOST_WINDOWS_API) + HANDLE h = GetStdHandle(STD_INPUT_HANDLE); + if (h == INVALID_HANDLE_VALUE) + return EXIT_FAILURE; + char buffer[1]; + DWORD read; + BOOL res = ReadFile(h, buffer, 1, &read, NULL); + CloseHandle(h); + return res ? EXIT_SUCCESS : EXIT_FAILURE; +#endif + } + else if (vm["is-nul-stdout"].as()) + { + std::cout << "foo" << std::endl; + return std::cout.bad() ? EXIT_FAILURE : EXIT_SUCCESS; + } + else if (vm["is-nul-stderr"].as()) + { + std::cerr << "foo" << std::endl; + return std::cerr.bad() ? EXIT_FAILURE : EXIT_SUCCESS; + } + else if (vm["loop"].as()) + { + for (;;); + } + else if (vm.count("prefix")) + { + std::string line; + while (std::getline(std::cin, line)) + std::cout << vm["prefix"].as() << line << std::endl; + } + else if (vm.count("prefix-once")) + { + std::string line; + std::getline(std::cin, line); + std::cout << vm["prefix-once"].as() << line << std::endl; + } + else if (vm["pwd"].as()) + { + std::cout << boost::filesystem::current_path().string() << std::endl; + } + else if (vm.count("query-env")) + { +#if defined(BOOST_POSIX_API) + const char *value = getenv(vm["query-env"].as().c_str()); + if (value == NULL) + std::cout << "undefined" << std::endl; + else + { + std::cout << "defined" << std::endl; + std::cout << value << std::endl; + } +#elif defined(BOOST_WINDOWS_API) + char buf[1024]; + DWORD res = GetEnvironmentVariableA(vm["query-env"].as().c_str(), buf, sizeof(buf)); + if (res == 0) + std::cout << "undefined" << std::endl; + else + { + std::cout << "defined" << std::endl; + std::cout << buf << std::endl; + } +#endif + } + else if (vm["stdin-to-stdout"].as()) + { + char ch; + while (std::cin >> std::noskipws >> ch) + std::cout << ch << std::flush; + } +#if defined(BOOST_POSIX_API) + else if (vm.count("posix-echo-one")) + { + using namespace boost::iostreams; + std::vector v = vm["posix-echo-one"].as >(); + int fd = boost::lexical_cast(v[0]); + file_descriptor_sink sink(fd, close_handle); + stream os(sink); + os << v[1] << std::endl; + } + else if (vm.count("posix-echo-two")) + { + using namespace boost::iostreams; + std::vector v = vm["posix-echo-two"].as >(); + int fd1 = boost::lexical_cast(v[0]); + file_descriptor_sink sink1(fd1, close_handle); + stream os1(sink1); + os1 << v[1] << std::endl; + int fd2 = boost::lexical_cast(v[2]); + file_descriptor_sink sink2(fd2, close_handle); + stream os2(sink2); + os2 << v[3] << std::endl; + } +#elif defined(BOOST_WINDOWS_API) + else if (vm["windows-print-showwindow"].as()) + { + STARTUPINFO si; + GetStartupInfo(&si); + std::cout << si.wShowWindow << std::endl; + } + else if (vm["windows-print-flags"].as()) + { + STARTUPINFO si; + GetStartupInfo(&si); + std::cout << si.dwFlags << std::endl; + } +#endif + return EXIT_SUCCESS; +} -- cgit v1.2.3