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:
parent
5c05c88342
commit
8649371ff2
24
src/file.cc
24
src/file.cc
|
@ -17,6 +17,10 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
#include <mach-o/dyld.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Kakoune
|
namespace Kakoune
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -408,4 +412,24 @@ time_t get_fs_timestamp(StringView filename)
|
||||||
return st.st_mtime;
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,8 @@ String parse_filename(StringView filename);
|
||||||
String real_path(StringView filename);
|
String real_path(StringView filename);
|
||||||
String compact_path(StringView filename);
|
String compact_path(StringView filename);
|
||||||
|
|
||||||
|
String get_kak_binary_path();
|
||||||
|
|
||||||
String read_fd(int fd);
|
String read_fd(int fd);
|
||||||
String read_file(StringView filename);
|
String read_file(StringView filename);
|
||||||
|
|
||||||
|
|
25
src/main.cc
25
src/main.cc
|
@ -24,10 +24,6 @@
|
||||||
#include "interned_string.hh"
|
#include "interned_string.hh"
|
||||||
#include "window.hh"
|
#include "window.hh"
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
|
||||||
#include <mach-o/dyld.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
@ -43,24 +39,11 @@ void run_unit_tests();
|
||||||
|
|
||||||
String runtime_directory()
|
String runtime_directory()
|
||||||
{
|
{
|
||||||
char buffer[2048];
|
String bin_path = get_kak_binary_path();
|
||||||
#if defined(__linux__) or defined(__CYGWIN__)
|
size_t last_slash = bin_path.find_last_of('/');
|
||||||
ssize_t res = readlink("/proc/self/exe", buffer, 2048);
|
if (last_slash == String::npos)
|
||||||
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)
|
|
||||||
throw runtime_error("unable to determine runtime directory");
|
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()
|
void register_env_vars()
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include "context.hh"
|
#include "context.hh"
|
||||||
#include "debug.hh"
|
#include "debug.hh"
|
||||||
|
#include "file.hh"
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
@ -15,6 +16,15 @@ static const Regex env_var_regex(R"(\bkak_(\w+)\b)");
|
||||||
|
|
||||||
ShellManager::ShellManager()
|
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,
|
String ShellManager::eval(StringView cmdline, const Context& context,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user