aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/main.cpp
blob: 95b232849b4947d3987584494d39841bbee39fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* (C) 2009,2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <vector>
#include <string>

#include <iostream>
#include <cstdlib>
#include <exception>
#include <limits>
#include <memory>

#include <botan/init.h>
#include <botan/version.h>
#include <botan/cpuid.h>

#if defined(BOTAN_HAS_HTTP_UTIL)
#include <botan/http_util.h>
#endif

using namespace Botan;

#include "apps.h"

namespace {

int help(int , char* argv[])
   {
   std::cout << "Usage: " << argv[0] << " [subcommand] [subcommand-options]" << std::endl;

   std::set<std::string> apps = AppRegistrations::instance().all_apps();

   std::cout << "Available commands:" << std::endl;

   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;
   }

int config(int argc, char* argv[])
   {
   if(argc != 2)
      {
      std::cout << "Usage: " << argv[0] << " <what>\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 = argv[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(int argc, char* argv[])
   {
   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(argc == 1)
      {
      std::cout << Botan::version_major() << "."
                << Botan::version_minor() << "."
                << Botan::version_patch() << std::endl;
      }
   else if(argc == 2 && std::string(argv[1]) == "--full")
      {
      std::cout << Botan::version_string() << std::endl;
      }
   else
      {
      std::cout << "Usage: " << argv[0] << " version [--full]" << std::endl;
      return 1;
      }

   return 0;
   }
REGISTER_APP(version);

int cpuid(int, char*[])
   {
   CPUID::print(std::cout);
   return 0;
   }
REGISTER_APP(cpuid);

#if defined(BOTAN_HAS_HTTP_UTIL)
int http_get(int argc, char* argv[])
   {
   if(argc != 2)
      {
      std::cout << "Usage " << argv[0] << " <url>" << std::endl;
      return 1;
      }

   auto resp = HTTP::GET_sync(argv[2]);
   std::cout << resp << std::endl;
   return 0;
   }
REGISTER_APP(http_get);

#endif

}

int main(int argc, char* argv[])
   {
   try
      {
      if(argc < 2)
         return help(argc, argv);

      const std::string cmd = argv[1];

      if(cmd == "help" || cmd == "-h")
         return help(argc, argv);

      Botan::LibraryInitializer init;

      AppRegistrations& apps = AppRegistrations::instance();
      if(apps.has(cmd))
         return apps.run(cmd, argc - 1, argv + 1);

      std::cerr << "Unknown command " << cmd << std::endl;
      return help(argc, argv);
      }
   catch(std::exception& e)
      {
      std::cerr << e.what() << std::endl;
      return 1;
      }
   catch(...)
      {
      std::cerr << "Unknown exception caught" << std::endl;
      return 1;
      }

   return 0;
   }