handle the path of the engine during parsing
| 95 | |
| 96 | // handle the path of the engine during parsing |
| 97 | static std::string |
| 98 | path_handler(const std::string &path, bool run_flag, const std::string &command) |
| 99 | { |
| 100 | std::string cur_working_dir = ""; |
| 101 | char cwd[PATH_MAX]; |
| 102 | if (!getcwd(cwd, sizeof(cwd))) { |
| 103 | ink_warning("unexpected failure from getcwd() - %s", strerror(errno)); |
| 104 | } else { |
| 105 | cur_working_dir = cwd; |
| 106 | } |
| 107 | |
| 108 | if (run_flag) { |
| 109 | // no path passed in, use cwd |
| 110 | if (path.empty()) { |
| 111 | return cur_working_dir; |
| 112 | } |
| 113 | if (path[0] == '/') { |
| 114 | return path; |
| 115 | } else { |
| 116 | return Layout::relative_to(cur_working_dir, path); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // for other commands |
| 121 | // 1. passed in path |
| 122 | if (!path.empty()) { |
| 123 | std::string cmdpath; |
| 124 | if (path[0] == '/') { |
| 125 | cmdpath = check_path(path); |
| 126 | } else { |
| 127 | cmdpath = check_path(Layout::relative_to(cur_working_dir, path)); |
| 128 | } |
| 129 | if (!cmdpath.empty()) { |
| 130 | return cmdpath; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // 2. find cwd or parent path of cwd to check |
| 135 | std::string cwdpath = check_parent_path(cur_working_dir); |
| 136 | if (!cwdpath.empty()) { |
| 137 | return cwdpath; |
| 138 | } |
| 139 | |
| 140 | // 3. installed executable |
| 141 | char RealBinPath[PATH_MAX] = {0}; |
| 142 | if (!command.empty() && realpath(command.c_str(), RealBinPath) != nullptr) { |
| 143 | std::string bindir = RealBinPath; |
| 144 | |
| 145 | bindir = bindir.substr(0, bindir.find_last_of("/")); // getting the bin dir not executable path |
| 146 | bindir = check_parent_path(bindir); |
| 147 | if (!bindir.empty()) { |
| 148 | return bindir; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return path; |
| 153 | } |
| 154 |
no test coverage detected