summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-10-30 00:50:40 +0000
committerMaxime Coste <frrrwww@gmail.com>2014-10-30 14:02:13 +0000
commit8649371ff23df34c0bf6ca34c1d8b61d7482821e (patch)
tree3ff6b39c01fae24e3423ef5631ae57d419aa9fc1 /src
parent5c05c88342134e76299f5d11a898e7a6048e6f1b (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/file.cc24
-rw-r--r--src/file.hh2
-rw-r--r--src/main.cc25
-rw-r--r--src/shell_manager.cc10
4 files changed, 40 insertions, 21 deletions
diff --git a/src/file.cc b/src/file.cc
index a8c20205..19f2a764 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -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
+}
+
}
diff --git a/src/file.hh b/src/file.hh
index e3c03fba..99bb3715 100644
--- a/src/file.hh
+++ b/src/file.hh
@@ -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);
diff --git a/src/main.cc b/src/main.cc
index 6e973411..4402d53c 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -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()
diff --git a/src/shell_manager.cc b/src/shell_manager.cc
index af2aa98d..8ac683ef 100644
--- a/src/shell_manager.cc
+++ b/src/shell_manager.cc
@@ -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,