3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Random portability stuff
23 See comments in porting.h
30 #elif defined(__APPLE__)
32 #include <mach-o/dyld.h>
33 #elif defined(__FreeBSD__)
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
44 #include "util/string.h"
48 #include "CoreFoundation/CoreFoundation.h"
55 Signal handler (grabs Ctrl-C on POSIX systems)
58 bool g_killed = false;
60 bool * signal_handler_killstatus(void)
65 #if !defined(_WIN32) // POSIX
68 void sigint_handler(int sig)
72 dstream<<DTIME<<"INFO: sigint_handler(): "
73 <<"Ctrl-C pressed, shutting down."<<std::endl;
75 // Comment out for less clutter when testing scripts
76 /*dstream<<DTIME<<"INFO: sigint_handler(): "
77 <<"Printing debug stacks"<<std::endl;
78 debug_stacks_print();*/
84 (void)signal(SIGINT, SIG_DFL);
88 void signal_handler_init(void)
90 (void)signal(SIGINT, sigint_handler);
97 BOOL WINAPI event_handler(DWORD sig)
102 case CTRL_CLOSE_EVENT:
103 case CTRL_LOGOFF_EVENT:
104 case CTRL_SHUTDOWN_EVENT:
106 if(g_killed == false)
108 dstream<<DTIME<<"INFO: event_handler(): "
109 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
110 // Comment out for less clutter when testing scripts
111 /*dstream<<DTIME<<"INFO: event_handler(): "
112 <<"Printing debug stacks"<<std::endl;
113 debug_stacks_print();*/
119 (void)signal(SIGINT, SIG_DFL);
123 case CTRL_BREAK_EVENT:
130 void signal_handler_init(void)
132 SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
141 // Default to RUN_IN_PLACE style relative paths
142 std::string path_share = "..";
143 std::string path_user = "..";
145 std::string getDataPath(const char *subpath)
147 return path_share + DIR_DELIM + subpath;
150 void pathRemoveFile(char *path, char delim)
152 // Remove filename and path delimiter
154 for(i = strlen(path)-1; i>=0; i--)
162 bool detectMSVCBuildDir(char *c_path)
164 std::string path(c_path);
165 const char *ends[] = {"bin\\Release", "bin\\Build", NULL};
166 return (removeStringEnd(path, ends) != "");
169 void initializePaths()
173 Use relative paths if RUN_IN_PLACE
176 infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
183 const DWORD buflen = 1000;
187 // Find path of executable and set path_share relative to it
188 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
189 assert(len < buflen);
190 pathRemoveFile(buf, '\\');
192 if(detectMSVCBuildDir(buf)){
193 infostream<<"MSVC build directory detected"<<std::endl;
194 path_share = std::string(buf) + "\\..\\..";
195 path_user = std::string(buf) + "\\..\\..";
198 path_share = std::string(buf) + "\\..";
199 path_user = std::string(buf) + "\\..";
208 memset(buf, 0, BUFSIZ);
209 // Get path to executable
210 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
212 pathRemoveFile(buf, '/');
214 path_share = std::string(buf) + "/..";
215 path_user = std::string(buf) + "/..";
220 #elif defined(__APPLE__)
222 //https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dyld.3.html
223 //TODO: Test this code
225 uint32_t len = sizeof(buf);
226 assert(_NSGetExecutablePath(buf, &len) != 0);
228 pathRemoveFile(buf, '/');
230 path_share = std::string(buf) + "/..";
231 path_user = std::string(buf) + "/..";
236 #elif defined(__FreeBSD__)
240 size_t len = sizeof(buf);
244 mib[2] = KERN_PROC_PATHNAME;
246 assert(sysctl(mib, 4, buf, &len, NULL, 0) != -1);
248 pathRemoveFile(buf, '/');
250 path_share = std::string(buf) + "/..";
251 path_user = std::string(buf) + "/..";
255 //TODO: Get path of executable. This assumes working directory is bin/
256 dstream<<"WARNING: Relative path not properly supported on this platform"
258 path_share = std::string("..");
259 path_user = std::string("..");
263 #else // RUN_IN_PLACE
266 Use platform-specific paths otherwise
269 infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
276 const DWORD buflen = 1000;
280 // Find path of executable and set path_share relative to it
281 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
282 assert(len < buflen);
283 pathRemoveFile(buf, '\\');
286 path_share = std::string(buf) + "\\..";
288 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
289 len = GetEnvironmentVariable("APPDATA", buf, buflen);
290 assert(len < buflen);
291 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
298 // Get path to executable
299 std::string bindir = "";
302 memset(buf, 0, BUFSIZ);
303 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
304 pathRemoveFile(buf, '/');
308 // Find share directory from these.
309 // It is identified by containing the subdirectory "builtin".
310 std::list<std::string> trylist;
311 std::string static_sharedir = STATIC_SHAREDIR;
312 if(static_sharedir != "" && static_sharedir != ".")
313 trylist.push_back(static_sharedir);
314 trylist.push_back(bindir + "/../share/" + PROJECT_NAME);
315 trylist.push_back(bindir + "/..");
317 for(std::list<std::string>::const_iterator i = trylist.begin();
318 i != trylist.end(); i++)
320 const std::string &trypath = *i;
321 if(!fs::PathExists(trypath) || !fs::PathExists(trypath + "/builtin")){
322 dstream<<"WARNING: system-wide share not found at \""
323 <<trypath<<"\""<<std::endl;
326 // Warn if was not the first alternative
327 if(i != trylist.begin()){
328 dstream<<"WARNING: system-wide share found at \""
329 <<trypath<<"\""<<std::endl;
331 path_share = trypath;
335 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
340 #elif defined(__APPLE__)
343 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
344 CFBundleRef main_bundle = CFBundleGetMainBundle();
345 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
347 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
349 dstream<<"Bundle resource path: "<<path<<std::endl;
351 path_share = std::string(path) + "/share";
356 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
358 CFRelease(resources_url);
360 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
362 #elif defined(__FreeBSD__)
364 path_share = STATIC_SHAREDIR;
365 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
369 #endif // RUN_IN_PLACE
372 } //namespace porting