1919// os.cc: Host OS functions
2020#include " os.h"
2121#include < memory>
22+ #include < sys/stat.h>
2223
2324#ifdef _WIN32
2425#include < windows.h>
@@ -33,7 +34,7 @@ static const char *path_sep = "\\/";
3334static const char *path_sep = " /" ;
3435#endif
3536
36- static std::string compiler_search_path;
37+ static std::vector<std:: string> compiler_search_path;
3738
3839bool os::path_absolute (const std::string &path)
3940{
@@ -110,13 +111,49 @@ void os::init(const std::string &prog)
110111#else
111112 // No init needed.
112113#endif
113- // Store out program name
114- compiler_search_path = dir_name (prog);
114+ // Initialize the compiler search path:
115+ // The FASTBASIC_HOME environment variable:
116+ const char *env = getenv (" FASTBASIC_HOME" );
117+ if (env)
118+ compiler_search_path.emplace_back (env);
119+ // The directory of the invoked program
120+ compiler_search_path.emplace_back (dir_name (prog));
121+ // And on Linux systems, the instalation path
122+ #ifndef _WIN32
123+ compiler_search_path.emplace_back (" /usr/local/share/fastbasic" );
124+ compiler_search_path.emplace_back (" /usr/share/fastbasic" );
125+ #endif
126+ }
127+
128+ std::string os::search_path (const std::vector<std::string> &paths,
129+ const std::string &filename)
130+ {
131+ // Check each possible path:
132+ for (auto &path : paths)
133+ {
134+ struct stat st;
135+ auto f = full_path (path, filename);
136+ auto e = stat (f.c_str (), &st);
137+ if (0 == e)
138+ return f;
139+ }
140+ // Not found, return only file name
141+ return filename;
115142}
116143
117144std::string os::compiler_path (const std::string &filename)
118145{
119- return full_path (compiler_search_path, filename);
146+ return search_path (compiler_search_path, filename);
147+ }
148+
149+ std::vector<std::string> os::get_search_path (const std::string &filename)
150+ {
151+ if (filename.empty ())
152+ return compiler_search_path;
153+ std::vector<std::string> ret;
154+ for (auto &path : compiler_search_path)
155+ ret.emplace_back (full_path (path, filename));
156+ return ret;
120157}
121158
122159int os::prog_exec (std::string exe, std::vector<std::string> &args)
0 commit comments