From 50f171d5c86a21727ff6387acf694ba44281d030 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 11 Feb 2016 15:48:46 -0500 Subject: [PATCH] which command --- builtins.cpp | 6 ++++++ builtins.h | 1 + command.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/builtins.cpp b/builtins.cpp index 4662f5f..7365d1c 100644 --- a/builtins.cpp +++ b/builtins.cpp @@ -12,6 +12,9 @@ #include #include +#include "cxx/string_splitter.h" +#include "cxx/filesystem.h" + //MAXPATHLEN #include @@ -21,6 +24,9 @@ namespace ToolBox { std::string UnixToMac(const std::string path); } + +namespace fs = filesystem; + namespace { /* diff --git a/builtins.h b/builtins.h index d7d91c7..3620fa2 100644 --- a/builtins.h +++ b/builtins.h @@ -16,6 +16,7 @@ int builtin_set(Environment &e, const std::vector &, const fdmask & int builtin_unset(Environment &e, const std::vector &, const fdmask &); int builtin_export(Environment &e, const std::vector &, const fdmask &); int builtin_unexport(Environment &e, const std::vector &, const fdmask &); +int builtin_which(Environment &e, const std::vector &, const fdmask &); int builtin_evaluate(Environment &e, std::vector &&, const fdmask &); diff --git a/command.cpp b/command.cpp index bbc1c8a..d5f6b51 100644 --- a/command.cpp +++ b/command.cpp @@ -12,6 +12,8 @@ #include #include +#include "cxx/filesystem.h" +#include "cxx/string_splitter.h" #include #include @@ -19,6 +21,34 @@ extern volatile int control_c; +namespace fs = filesystem; +extern fs::path mpw_path(); + +namespace ToolBox { + std::string MacToUnix(const std::string path); + std::string UnixToMac(const std::string path); +} + +fs::path which(const Environment &env, const std::string &name) { + std::error_code ec; + + if (name.find_first_of("/:") != name.npos) { + // canonical? + fs::path p(name); + if (fs::exists(p, ec)) return name; + return ""; + } + + std::string s = env.get("commands"); + for (string_splitter ss(s, ','); ss; ++ss) { + fs::path p(ToolBox::MacToUnix(*ss)); + p /= name; + if (fs::exists(p, ec)) return p; + } + + // error.. + return ""; +} void launch_mpw(const Environment &env, const std::vector &argv, const fdmask &fds) {