From c35e0d08b881acc38cec741e862180eed20e2d20 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 7 May 2010 10:12:04 +0200 Subject: Use cli parser to parse options in driver --- odb/odb.cxx | 117 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 69 insertions(+), 48 deletions(-) (limited to 'odb/odb.cxx') diff --git a/odb/odb.cxx b/odb/odb.cxx index 3ee2eb5..eca2dca 100644 --- a/odb/odb.cxx +++ b/odb/odb.cxx @@ -15,6 +15,9 @@ #include // size_t #include +#include +#include + using namespace std; static string @@ -25,10 +28,6 @@ main (int argc, char* argv[]) { typedef vector strings; - string file; - strings args; - bool v (false); - // Find the plugin. It should be in the same directory as the // driver. // @@ -36,12 +35,15 @@ main (int argc, char* argv[]) if (plugin.empty ()) { - cerr << argv[0] << ": error: unable to locate ODB plugin" << endl; + cerr << argv[0] << ": error: unable to locate ODB GCC plugin" << endl; cerr << argv[0] << ": info: make sure '" << argv[0] << ".so' is in " << "the same directory as '" << argv[0] << "'" << endl; return 1; } + strings args, plugin_args; + bool v (false); + // The first argument points to the program name, which is // g++ by default. // @@ -55,6 +57,8 @@ main (int argc, char* argv[]) args.push_back ("-DODB_COMPILER"); args.push_back ("-fplugin=" + plugin); + // Parse driver options. + // for (int i = 1; i < argc; ++i) { string a (argv[i]); @@ -144,68 +148,85 @@ main (int argc, char* argv[]) args.push_back (argv[i]); } } - // Everything else except the last argument (file to compile) is passed - // to the plugin. + // Store everything else in a list so that we can parse it with the + // cli parser. This is the only reliable way to find out where the + // options end. // else + plugin_args.push_back (a); + } + + // Parse plugin options. + // + try + { + vector av; + av.push_back (argv[0]); + + for (strings::iterator i (plugin_args.begin ()), e (plugin_args.end ()); + i != e; ++i) { - if (n > 1 && a[0] == '-') - { - string k, v; + av.push_back (const_cast (i->c_str ())); + } - if (n > 2 && a[1] == '-') - k = string (a, 2); // long format - else - k = string (a, 1); // short format + int ac (static_cast (av.size ())); + cli::argv_file_scanner scan (ac, &av[0], "--options-file"); - // If the next argument is not the last, then we may have a - // value. - // - if (i + 2 < argc) - { - a = argv[i + 1]; - n = a.size (); - - if (n > 1 && a[0] != '-') - { - v = a; - ++i; - } - } + options ops (scan); + size_t end (scan.end () - 1); // We have one less in plugin_args. - string o ("-fplugin-arg-odb-"); - o += k; + if (end == plugin_args.size ()) + { + cerr << argv[0] << ": error: input file expected" << endl; + return 1; + } - if (!v.empty ()) - { - o += '='; - o += v; - } + // Encode plugin options. + // + for (size_t i (0); i < end; ++i) + { + string k, v; + string a (plugin_args[i]); - args.push_back (o); - } + if (a.size () > 2) + k = string (a, 2); // long format else + k = string (a, 1); // short format + + // If there are more arguments then we may have a value. + // + if (i + 1 < end) { - if (file.empty ()) - file = a; - else + a = plugin_args[i + 1]; + if (a.size () > 1 && a[0] != '-') { - cerr << argv[0] << ": error: second input file specified: '" - << a << "'" << endl; - return 1; + v = a; + ++i; } } + + string o ("-fplugin-arg-odb-"); + o += k; + + if (!v.empty ()) + { + o += '='; + o += v; + } + + args.push_back (o); } - } - if (file.empty ()) + // Copy over arguments. + // + args.insert (args.end (), plugin_args.begin () + end, plugin_args.end ()); + } + catch (cli::exception const& ex) { - cerr << argv[0] << ": error: input file expected" << endl; + cerr << ex << endl; return 1; } - args.push_back (file); - // Create an execvp-compatible argument array. // vector exec_args; -- cgit v1.1