Add kak binary location to PATH env var automatically

That way the kak binary can be guaranteed to be available even
if not in user PATH.
This commit is contained in:
Maxime Coste 2014-10-30 00:50:40 +00:00
parent 5c05c88342
commit 8649371ff2
4 changed files with 40 additions and 21 deletions

View File

@ -17,6 +17,10 @@
#include <unistd.h>
#include <dirent.h>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
namespace Kakoune
{
@ -408,4 +412,24 @@ time_t get_fs_timestamp(StringView filename)
return st.st_mtime;
}
String get_kak_binary_path()
{
char buffer[2048];
#if defined(__linux__) or defined(__CYGWIN__)
ssize_t res = readlink("/proc/self/exe", buffer, 2048);
kak_assert(res != -1);
buffer[res] = '\0';
return buffer;
#elif defined(__APPLE__)
uint32_t bufsize = 2048;
_NSGetExecutablePath(buffer, &bufsize);
char* canonical_path = realpath(buffer, nullptr);
String path = canonical_path;
free(canonical_path);
return path;
#else
# error "finding executable path is not implemented on this platform"
#endif
}
}

View File

@ -29,6 +29,8 @@ String parse_filename(StringView filename);
String real_path(StringView filename);
String compact_path(StringView filename);
String get_kak_binary_path();
String read_fd(int fd);
String read_file(StringView filename);

View File

@ -24,10 +24,6 @@
#include "interned_string.hh"
#include "window.hh"
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
#include <unordered_map>
#include <locale>
#include <signal.h>
@ -43,24 +39,11 @@ void run_unit_tests();
String runtime_directory()
{
char buffer[2048];
#if defined(__linux__) or defined(__CYGWIN__)
ssize_t res = readlink("/proc/self/exe", buffer, 2048);
kak_assert(res != -1);
buffer[res] = '\0';
#elif defined(__APPLE__)
uint32_t bufsize = 2048;
_NSGetExecutablePath(buffer, &bufsize);
char* canonical_path = realpath(buffer, nullptr);
strncpy(buffer, canonical_path, 2048);
free(canonical_path);
#else
# error "finding executable path is not implemented on this platform"
#endif
char* ptr = strrchr(buffer, '/');
if (not ptr)
String bin_path = get_kak_binary_path();
size_t last_slash = bin_path.find_last_of('/');
if (last_slash == String::npos)
throw runtime_error("unable to determine runtime directory");
return String(buffer, ptr) + "/../share/kak";
return bin_path.substr(0_byte, (int)last_slash) + "/../share/kak";
}
void register_env_vars()

View File

@ -2,6 +2,7 @@
#include "context.hh"
#include "debug.hh"
#include "file.hh"
#include <cstring>
#include <sys/types.h>
@ -15,6 +16,15 @@ static const Regex env_var_regex(R"(\bkak_(\w+)\b)");
ShellManager::ShellManager()
{
const char* path = getenv("PATH");
String new_path;
if (path)
new_path = path + ":"_str;
String kak_path = get_kak_binary_path();
StringView kak_dir = kak_path.substr(0_byte, (int)kak_path.find_last_of('/'));
new_path += kak_dir;
setenv("PATH", new_path.c_str(), 1);
}
String ShellManager::eval(StringView cmdline, const Context& context,