Jean-Marc Lasgouttes wrote:
> Angus> So, the alternative way forward is to replace all existing use
> Angus> of GetEnvPath with 'os::internal_path(GetEnv("foo"))' as is
> Angus> done in my 'package' class and to remove GetEnvPath entirely.
> 
> Getting rid of GetEnvPath is probably good. Are there places where we
> need the functionality?

It transpires that we do need it after all. Example:

I have a $HOME/bin directory in my path.
This directory contains a symbolic link xlyx13x that points to
$HOME/lyx/13x/build-xforms/src/lyx

If, from say $HOME, I try and launch lyx as 'xlyx13x', then the executable 
is launched. However, argv[0] is "xlyx13x". There's no path information 
there at all. MakeAbsPath(os::internal_path(argv[0])) will return 
/home/Angus/xlyx13x. Everything thereafter falls over :(

This problem occurs independently to LyX's installed or in-place status or 
whether it is invoked through a symbolic link or not. argv[0] will contain 
exectly what is typed at the command line.

So, I found I needed getEnvPath after all. 
Angus


string const abs_dir_from_binary_name(string const & exe)
{
        string const exe_path = os::internal_path(exe);
        if (os::is_absolute_path(exe_path))
                return exe_path;

        // Two possibilities present themselves:
        // 1. The binary is relative to the CWD.
        string const abs_exe_path = MakeAbsPath(exe_path);
        if (lyx::FileInfo(abs_exe_path, true).isOK())
                return abs_exe_path;

        // 2. exe must be the name of the binary only and it
        // can be found on the PATH.
        string const exe_name = OnlyFilename(exe_path);
        if (exe_name != exe_path)
                return string();

        vector<string> const path = getEnvPath("PATH");
        vector<string>::const_iterator it = path.begin();
        vector<string>::const_iterator const end = path.end();
        for (; it != end; ++it) {
                if (!os::is_absolute_path(*it))
                        // Someone is playing silly buggers.
                        return string();

                string const exe_path = AddPath(*it, exe_name);
                if (lyx::FileInfo(exe_path, true).isOK())
                        return exe_path;
        }

        // Didn't find anything.
        return string();
}

Package::Package(string const & command_line_arg0,
                 string const & command_line_system_lyxdir,
                 string const & command_line_user_lyxdir)
        : explicit_user_lyxdir_(false)
{
        string const abs_binary =
                abs_dir_from_binary_name(command_line_arg0);
        if (abs_binary.empty()) {
                using boost::format;
                lyxerr << format(_("Unable to determine the path to the "
                                   "LyX binary from the command line %1%"))
                                 % command_line_arg0
                       << std::endl;
                bail_out();
        }
        ...
}


-- 
Angus

Reply via email to