From b48e6fb097c62bb246629ee7a182c57e497e4130 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Sat, 19 Dec 2015 15:36:40 -0500 Subject: CLI rewrite The command line tools' origin as a collection of examples and test programs glued together led to some unfortunate problems; lots of hardcoded values, missing parameters, and obsolete crypto. Adds a small library for writing command line programs of the sort needed here (cli.h), which cuts the length of many of the commands in half and makes commands more pleasant to write and extend. Generalizes a lot of the commands also, eg previously only signing/verification with DSA/SHA-1 was included! Removes the fuzzer entry point since that's fairly useless outside of an instrumented build. Removes the in-library API for benchmarking. --- src/cli/main.cpp | 186 +++++++++---------------------------------------------- 1 file changed, 29 insertions(+), 157 deletions(-) (limited to 'src/cli/main.cpp') diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 8d229ce0e..f054e6005 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -1,187 +1,59 @@ /* -* (C) 2009,2014 Jack Lloyd +* (C) 2009,2014,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ -#include -#include - -#include -#include -#include -#include -#include +#include "cli.h" #include -#include - -#if defined(BOTAN_HAS_HTTP_UTIL) -#include -#endif - -using namespace Botan; - -#include "apps.h" +#include +#include +#include namespace { -int help(const std::vector &args) +std::string main_help() { - std::cout << "Usage: " << args[0] << " [subcommand] [subcommand-options]" << std::endl; + const std::set avail_commands = + Botan::map_keys_as_set(Botan_CLI::Command::global_registry()); - std::set apps = AppRegistrations::instance().all_appnames(); + std::ostringstream oss; - std::cout << "Available commands:" << std::endl; + oss << "Usage: botan \n"; + oss << "Available commands: "; + std::copy(avail_commands.begin(), + avail_commands.end(), + std::ostream_iterator(oss, " ")); + oss << "\n"; - size_t idx = 1; - for(auto&& app: apps) - { - std::cout << app; - - if(idx % 3 == 0) - std::cout << std::endl; - else - std::cout << std::string(18-app.size(), ' '); - - ++idx; - } - std::cout << std::endl; - - return 1; + return oss.str(); } -int config(const std::vector &args) - { - if(args.size() != 2) - { - std::cout << "Usage: " << args[0] << " \n" - << " prefix: Print install prefix\n" - << " cflags: Print include params\n" - << " ldflags: Print linker params\n" - << " libs: Print libraries" << std::endl; - return 1; - } - - const std::string arg = args[1]; - - if(arg == "prefix") - std::cout << BOTAN_INSTALL_PREFIX << std::endl; - - else if(arg == "cflags") - std::cout << "-I" << BOTAN_INSTALL_PREFIX << "/" << BOTAN_INSTALL_HEADER_DIR << std::endl; - - else if(arg == "ldflags") - std::cout << "-L" << BOTAN_INSTALL_PREFIX << "/" << BOTAN_INSTALL_LIB_DIR << std::endl; - - else if(arg == "libs") - std::cout << "-lbotan-" << version_major() << "." << version_minor() - << " " << BOTAN_LIB_LINK << std::endl; - - else - { - std::cerr << "Unknown option " << arg << " to botan config" << std::endl; - return 1; - } - - return 0; - } -REGISTER_APP(config); - -int version(const std::vector &args) - { - if(BOTAN_VERSION_MAJOR != version_major() || - BOTAN_VERSION_MINOR != version_minor() || - BOTAN_VERSION_PATCH != version_patch()) - { - std::cerr << "Warning: linked version (" - << version_major() << '.' - << version_minor() << '.' - << version_patch() - << ") does not match version built against (" - << BOTAN_VERSION_MAJOR << '.' - << BOTAN_VERSION_MINOR << '.' - << BOTAN_VERSION_PATCH << ")" << std::endl; - } - - if(args.size() == 1) - { - std::cout << Botan::version_major() << "." - << Botan::version_minor() << "." - << Botan::version_patch() << std::endl; - } - else if(args.size() == 2 && args[1] == "--full") - { - std::cout << Botan::version_string() << std::endl; - } - else - { - std::cout << "Usage: " << args[0] << " version [--full]" << std::endl; - return 1; - } - - return 0; - } -REGISTER_APP(version); - -int cpuid(const std::vector &args) - { - BOTAN_UNUSED(args); - CPUID::print(std::cout); - return 0; - } -REGISTER_APP(cpuid); - -#if defined(BOTAN_HAS_HTTP_UTIL) -int http_get(const std::vector &args) - { - if(args.size() != 2) - { - std::cout << "Usage " << args[0] << " " << std::endl; - return 1; - } - - auto resp = HTTP::GET_sync(args[1]); - std::cout << resp << std::endl; - return 0; - } -REGISTER_APP(http_get); - -#endif - } int main(int argc, char* argv[]) { - const std::vector args(argv, argv + argc); + std::cerr << Botan::runtime_version_check(BOTAN_VERSION_MAJOR, + BOTAN_VERSION_MINOR, + BOTAN_VERSION_PATCH); - try - { - if(args.size() < 2) - return help(args); - - const std::string cmd = args[1]; - - if(cmd == "help" || cmd == "-h") - return help(args); - - AppRegistrations& apps = AppRegistrations::instance(); - if(apps.has(cmd)) - return apps.run(cmd, std::vector(args.begin()+1, args.end())); + const std::string cmd_name = (argc <= 1) ? "help" : argv[1]; - std::cerr << "Unknown command " << cmd << std::endl; - return help(args); - } - catch(std::exception& e) + if(cmd_name == "help" || cmd_name == "--help") { - std::cerr << e.what() << std::endl; + std::cout << main_help(); return 1; } - catch(...) + + Botan_CLI::Command* cmd = Botan_CLI::Command::get_cmd(cmd_name); + + if(!cmd) { - std::cerr << "Unknown exception caught" << std::endl; + std::cout << "Unknown command " << cmd_name << " (try --help)\n"; return 1; } - return 0; + std::vector args(argv + 2, argv + argc); + return cmd->run(args); } -- cgit v1.2.3