IODA Bundle
tools/odc.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "eckit/io/FileHandle.h"
12 
13 #include "eckit/sql/expression/function/FunctionFactory.h"
14 #include "odc/ODBAPIVersion.h"
15 #include "odc/odccapi.h"
16 
17 #include "TestOdaCAPI.h"
18 #include "Tool.h"
19 #include "ToolFactory.h"
20 #include "ToolRunnerApplication.h"
21 #include "TestRunnerApplication.h"
22 
23 using namespace std;
24 using namespace eckit;
25 
26 using namespace odc::tool;
27 
28 int executeCommand(int argc, char *argv[]);
29 int gdb(int argc, char *argv[]);
30 int valgrind(int argc, char *argv[]);
31 int sqlhelp(int argc, char *argv[]);
32 
33 int main(int argc, char *argv[])
34 {
35  try { return executeCommand(argc, argv);
36  } catch (std::exception& e) {
37  cerr << argv[0] << ": " << e.what() << std::endl;
38  return 1;
39  } catch (...) {
40  cerr << argv[0] << ": unknown exception" << std::endl;
41  return 1;
42  }
43 }
44 
45 int executeCommand(int argc, char *argv[])
46 {
48  if (argc < 2)
49  {
50  odb_start_with_args(argc, argv);
51  cerr << "Usage:" << endl
52  << " " << argv[0] << " <command> [<command's-parameters>]" << endl
53  << " " << argv[0] << " help <command>" << std::endl << endl
54  << "Available commands:" << std::endl;
55 
56  AbstractToolFactory::listTools(cout);
57  return 1;
58  }
59 
60  const string firstArg(argv[1]);
61 
62  if (firstArg == "g") { odb_start_with_args(argc, argv); return gdb(argc, argv); }
63  if (firstArg == "vg") { odb_start_with_args(argc, argv); return valgrind(argc, argv); }
64 
65  /// TODO: reenable
66 // if (firstArg == "testodbcapi") return odc::tool::test::test_odacapi(argc, argv);
67  if (firstArg == "test")
68  {
69  if (argc == 2) //no args => test all
70  {
71  std::cout << "Testing C API" << std::endl;
72 
73  string testCapi = std::string(argv[0]) + " testodbcapi";
74  cerr << "Executing '" << testCapi << "'" << std::endl;
75  int rc = system(testCapi.c_str());
76  if (rc) return rc;
77  }
78 
79  std::cout << std::endl << "Running tests." << std::endl;
80 
81  odc::tool::test::TestRunnerApplication testRunner(argc - 1, argv + 1);
82  testRunner.start();
83  // It never really gets here.
84  return 0;
85  }
86 
87  if (firstArg == "help")
88  {
89  odb_start_with_args(argc, argv);
90  if (argc == 2)
91  AbstractToolFactory::printToolsHelp(cout);
92  else
93  {
94  AbstractToolFactory::printToolHelp(argv[2], cout);
95  std::cout << std::endl << "Usage:" << std::endl << std::endl << "\t";
96  AbstractToolFactory::printToolUsage(argv[2], cout);
97  }
98  return 0;
99  }
100 
101  if (firstArg == "sqlhelp") return sqlhelp(argc, argv);
102 
103  if (firstArg == "-V" || firstArg == "-v" || firstArg == "--version")
104  {
105  std::cout << "ODBAPI Version: " << odc::ODBAPIVersion::version() << std::endl;
106  std::cout << "File format version: " << odc::ODBAPIVersion::formatVersionMajor() << "." << odc::ODBAPIVersion::formatVersionMinor() << std::endl;
107  return 0;
108  }
109 
110  ToolRunnerApplication runner(argc, argv);
111  return runner.start();
112 }
113 
114 int gdb(int argc, char *argv[])
115 {
116  string cmd = argv[0];
117  string args;
118  string gdbScript = argv[1];
119  for (int i = 2; i < argc; i++)
120  args += std::string(" ") + argv[i];
121 
122  PathName scriptFile (std::string(".gdb_") + std::string(argc < 3 ? "odc" : argv[2]));
123 
124  if (! scriptFile.exists())
125  {
126  string s = std::string("file ") + cmd + "\nbreak main\nrun " + args + "\ncatch throw\n";
127  FileHandle f(scriptFile);
128  f.openForWrite(1024);
129  f.write(s.c_str(), s.size());
130  f.close();
131  }
132  string vi = std::string("vi ") + scriptFile;
133  string gdb = std::string("gdb -x ") + scriptFile;
134  std::cout << "Executing '" << vi << "'" << std::endl;
135  system(vi.c_str());
136  std::cout << "Executing '" << gdb << "'" << std::endl;
137  return system(gdb.c_str());
138 }
139 
140 // valgrind --log-file=v.log --show-reachable=yes --leak-check=full ./oda test
141 int valgrind(int argc, char *argv[])
142 {
143  string cmd = argv[0];
144  string args;
145  for (int i = 2; i < argc; i++)
146  args += std::string(" ") + argv[i];
147 
148 
149  string logFile = std::string("vg.") + argv[2] + ".log";
150  string vg = std::string("valgrind --log-file=") + logFile + " --show-reachable=yes --leak-check=full "
151  + " --db-attach=yes " // --suppressions=eckit.supp "
152  + cmd + " " + args;
153  std::cout << "Executing '" << vg << "'" << std::endl;
154  return system(vg.c_str());
155 }
156 
157 int sqlhelp(int argc, char *argv[])
158 {
159  auto info = eckit::sql::expression::function::FunctionFactory::instance().functionsInfo();
160 
161  for (auto& i : info) {
162  std::cout << i.name << "/" << i.arity << " " << i.help << std::endl;
163  }
164 
165  return 0;
166 }
static unsigned int formatVersionMinor()
static const char * version()
static unsigned int formatVersionMajor()
static void registerTools()
Definition: Tool.cc:48
Definition: encode.cc:30
void odb_start_with_args(int argc, char *argv[])
Definition: odccapi.cc:85
int main(int argc, char *argv[])
Definition: tools/odc.cc:33
int sqlhelp(int argc, char *argv[])
Definition: tools/odc.cc:157
int gdb(int argc, char *argv[])
Definition: tools/odc.cc:114
int valgrind(int argc, char *argv[])
Definition: tools/odc.cc:141
int executeCommand(int argc, char *argv[])
Definition: tools/odc.cc:45