From 5259b98c75f3754a0f713bcee4bddd0ed7ce35ef Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 22 Oct 2010 11:35:14 +0200 Subject: Use new path::complete() function --- odb/odb.cxx | 366 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 180 insertions(+), 186 deletions(-) (limited to 'odb/odb.cxx') diff --git a/odb/odb.cxx b/odb/odb.cxx index 6104685..c94400e 100644 --- a/odb/odb.cxx +++ b/odb/odb.cxx @@ -4,7 +4,6 @@ // license : GNU GPL v3; see accompanying LICENSE file #include -#include // PATH_MAX #include // getenv, setenv #include // strerror #include // stat, execvp @@ -32,6 +31,7 @@ using namespace std; using cutl::fs::path; +using cutl::fs::invalid_path; static path driver_path (path const& driver); @@ -48,216 +48,202 @@ static char const* const db_macro[] = int main (int argc, char* argv[]) { - typedef vector strings; - ostream& e (cerr); - // Find the plugin. It should be in the same directory as the - // driver. - // + try + { + typedef vector strings; + + // Find the plugin. It should be in the same directory as the + // driver. + // #ifndef STATIC_PLUGIN - path plugin (plugin_path (path (argv[0]))); + path plugin (plugin_path (path (argv[0]))); #else - // Use a dummy name if the plugin is linked into the compiler. - // - path plugin ("odb"); + // Use a dummy name if the plugin is linked into the compiler. + // + path plugin ("odb"); #endif - if (plugin.empty ()) - { - e << argv[0] << ": error: unable to locate ODB GCC plugin" << endl; - e << argv[0] << ": info: make sure '" << argv[0] << ".so' is in " - << "the same directory as '" << argv[0] << "'" << endl; - return 1; - } + if (plugin.empty ()) + { + e << argv[0] << ": error: unable to locate ODB GCC plugin" << endl; + e << 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); + strings args, plugin_args; + bool v (false); - // The first argument points to the program name, which is - // g++ by default. - // + // The first argument points to the program name, which is + // g++ by default. + // #ifdef GXX_NAME - path gxx (GXX_NAME); + path gxx (GXX_NAME); - if (gxx.empty ()) - { - e << argv[0] << ": error: embedded g++ compile name is empty" << endl; - return 1; - } + if (gxx.empty ()) + { + e << argv[0] << ": error: embedded g++ compile name is empty" << endl; + return 1; + } - // If the g++ name is a relative path (starts with '.'), then use - // our own path as base. - // - if (gxx.string ()[0] == '.') - { - path dp (driver_path (path (argv[0]))); - path d (dp.directory ()); + // If the g++ name is a relative path (starts with '.'), then use + // our own path as base. + // + if (gxx.string ()[0] == '.') + { + path dp (driver_path (path (argv[0]))); + path d (dp.directory ()); - if (!d.empty ()) - gxx = d / gxx; - } + if (!d.empty ()) + gxx = d / gxx; + } - args.push_back (gxx.string ()); + args.push_back (gxx.string ()); - // Also modify LD_LIBRARY_PATH to include the lib path. - // + // Also modify LD_LIBRARY_PATH to include the lib path. + // #ifndef _WIN32 - { - string ld_paths; + { + string ld_paths; - if (char const* s = getenv ("LD_LIBRARY_PATH")) - ld_paths = s; + if (char const* s = getenv ("LD_LIBRARY_PATH")) + ld_paths = s; - path d (gxx.directory ()); + path d (gxx.directory ()); - if (!d.empty ()) - { - // Make it absolute. - // - if (d.string ()[0] != path::traits::directory_separator) + if (!d.empty ()) { - char cwd[PATH_MAX]; - if (getcwd (cwd, PATH_MAX) == 0) + d.complete (); + d /= path ("..") / path ("lib"); + + if (ld_paths.empty ()) + ld_paths = d.string (); + else + ld_paths = d.string () + path::traits::path_separator + ld_paths; + + if (setenv ("LD_LIBRARY_PATH", ld_paths.c_str (), 1) != 0) { - e << argv[0] << ": error: working directory is too deep" << endl; + e << argv[0] << ": error: unable to update environment" << endl; return 1; } - - d = path (cwd) / d; - } - - d /= path ("..") / path ("lib"); - - if (ld_paths.empty ()) - ld_paths = d.string (); - else - ld_paths = d.string () + path::traits::path_separator + ld_paths; - - if (setenv ("LD_LIBRARY_PATH", ld_paths.c_str (), 1) != 0) - { - e << argv[0] << ": error: unable to update environment" << endl; - return 1; } } - } - #endif // _WIN32 #else - args.push_back ("g++"); + args.push_back ("g++"); #endif // GXX_NAME - // Default options. - // - args.push_back ("-x"); - args.push_back ("c++"); - args.push_back ("-S"); - args.push_back ("-Wunknown-pragmas"); - args.push_back ("-fplugin=" + plugin.string ()); - - // Parse driver options. - // - for (int i = 1; i < argc; ++i) - { - string a (argv[i]); - size_t n (a.size ()); - - // -v + // Default options. // - if (a == "-v") - { - v = true; - args.push_back (a); - } - // -x + args.push_back ("-x"); + args.push_back ("c++"); + args.push_back ("-S"); + args.push_back ("-Wunknown-pragmas"); + args.push_back ("-fplugin=" + plugin.string ()); + + // Parse driver options. // - else if (a == "-x") + for (int i = 1; i < argc; ++i) { - if (++i == argc || argv[i][0] == '\0') - { - e << argv[0] << ": error: expected argument for the -x option" << endl; - return 1; - } + string a (argv[i]); + size_t n (a.size ()); - a = argv[i]; - - if (a[0] == '-') - args.push_back (a); - else + // -v + // + if (a == "-v") { - // This must be the g++ executable name. Update the first - // argument with it. - // - args[0] = a; + v = true; + args.push_back (a); } - } - // -I - // - else if (n > 1 && a[0] == '-' && a[1] == 'I') - { - args.push_back (a); - - if (n == 2) // -I /path + // -x + // + else if (a == "-x") { if (++i == argc || argv[i][0] == '\0') { - e << argv[0] << ": error: expected argument for the -I option" - << endl; + e << argv[0] << ": error: expected argument for the -x option" << endl; return 1; } - args.push_back (argv[i]); - } - } - // -D - // - else if (n > 1 && a[0] == '-' && a[1] == 'D') - { - args.push_back (a); + a = argv[i]; - if (n == 2) // -D macro - { - if (++i == argc || argv[i][0] == '\0') + if (a[0] == '-') + args.push_back (a); + else { - e << argv[0] << ": error: expected argument for the -D option" - << endl; - return 1; + // This must be the g++ executable name. Update the first + // argument with it. + // + args[0] = a; } - - args.push_back (argv[i]); } - } - // -U - // - else if (n > 1 && a[0] == '-' && a[1] == 'U') - { - args.push_back (a); + // -I + // + else if (n > 1 && a[0] == '-' && a[1] == 'I') + { + args.push_back (a); + + if (n == 2) // -I /path + { + if (++i == argc || argv[i][0] == '\0') + { + e << argv[0] << ": error: expected argument for the -I option" + << endl; + return 1; + } - if (n == 2) // -U macro + args.push_back (argv[i]); + } + } + // -D + // + else if (n > 1 && a[0] == '-' && a[1] == 'D') { - if (++i == argc || argv[i][0] == '\0') + args.push_back (a); + + if (n == 2) // -D macro { - e << argv[0] << ": error: expected argument for the -U option" - << endl; - return 1; + if (++i == argc || argv[i][0] == '\0') + { + e << argv[0] << ": error: expected argument for the -D option" + << endl; + return 1; + } + + args.push_back (argv[i]); } + } + // -U + // + else if (n > 1 && a[0] == '-' && a[1] == 'U') + { + args.push_back (a); + + if (n == 2) // -U macro + { + if (++i == argc || argv[i][0] == '\0') + { + e << argv[0] << ": error: expected argument for the -U option" + << endl; + return 1; + } - args.push_back (argv[i]); + args.push_back (argv[i]); + } } + // 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); } - // 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 - { + // Parse plugin options. + // vector av; av.push_back (argv[0]); @@ -393,48 +379,56 @@ main (int argc, char* argv[]) // Copy over arguments. // args.insert (args.end (), plugin_args.begin () + end, plugin_args.end ()); - } - catch (cli::exception const& ex) - { - e << ex << endl; - return 1; - } - // Create an execvp-compatible argument array. - // - vector exec_args; - for (strings::const_iterator i (args.begin ()), end (args.end ()); - i != end; ++i) - { - exec_args.push_back (i->c_str ()); + // Create an execvp-compatible argument array. + // + vector exec_args; - if (v) - e << *i << ' '; - } + for (strings::const_iterator i (args.begin ()), end (args.end ()); + i != end; ++i) + { + exec_args.push_back (i->c_str ()); + + if (v) + e << *i << ' '; + } - if (v) - e << endl; + if (v) + e << endl; - exec_args.push_back (0); + exec_args.push_back (0); #ifdef _WIN32 - intptr_t r (_spawnvp (_P_WAIT, exec_args[0], &exec_args[0])); + intptr_t r (_spawnvp (_P_WAIT, exec_args[0], &exec_args[0])); + + if (r == (intptr_t)(-1)) + { + e << exec_args[0] << ": error: " << strerror (errno) << endl; + return 1; + } + + return r == 0 ? 0 : 1; +#else + if (execvp (exec_args[0], const_cast (&exec_args[0])) < 0) + { + e << exec_args[0] << ": error: " << strerror (errno) << endl; + return 1; + } +#endif - if (r == (intptr_t)(-1)) + } + catch (invalid_path const& ex) { - e << exec_args[0] << ": error: " << strerror (errno) << endl; + e << argv[0] << ": error: invalid path '" << ex.path () << "'" << endl; return 1; } - - return r == 0 ? 0 : 1; -#else - if (execvp (exec_args[0], const_cast (&exec_args[0])) < 0) + catch (cli::exception const& ex) { - e << exec_args[0] << ": error: " << strerror (errno) << endl; + e << ex << endl; return 1; } -#endif + } // -- cgit v1.1