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
28 #elif defined(__APPLE__)
30 #include <mach-o/dyld.h>
31 #elif defined(__FreeBSD__)
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
42 #include "util/string.h"
46 #include "CoreFoundation/CoreFoundation.h"
53 Signal handler (grabs Ctrl-C on POSIX systems)
56 bool g_killed = false;
58 bool * signal_handler_killstatus(void)
63 #if !defined(_WIN32) // POSIX
66 void sigint_handler(int sig)
70 dstream<<DTIME<<"INFO: sigint_handler(): "
71 <<"Ctrl-C pressed, shutting down."<<std::endl;
73 // Comment out for less clutter when testing scripts
74 /*dstream<<DTIME<<"INFO: sigint_handler(): "
75 <<"Printing debug stacks"<<std::endl;
76 debug_stacks_print();*/
82 (void)signal(SIGINT, SIG_DFL);
86 void signal_handler_init(void)
88 (void)signal(SIGINT, sigint_handler);
94 BOOL WINAPI event_handler(DWORD sig)
99 case CTRL_CLOSE_EVENT:
100 case CTRL_LOGOFF_EVENT:
101 case CTRL_SHUTDOWN_EVENT:
103 if(g_killed == false)
105 dstream<<DTIME<<"INFO: event_handler(): "
106 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
107 // Comment out for less clutter when testing scripts
108 /*dstream<<DTIME<<"INFO: event_handler(): "
109 <<"Printing debug stacks"<<std::endl;
110 debug_stacks_print();*/
116 (void)signal(SIGINT, SIG_DFL);
120 case CTRL_BREAK_EVENT:
127 void signal_handler_init(void)
129 SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
138 // Default to RUN_IN_PLACE style relative paths
139 std::string path_share = "..";
140 std::string path_user = "..";
142 std::string getDataPath(const char *subpath)
144 return path_share + DIR_DELIM + subpath;
147 void pathRemoveFile(char *path, char delim)
149 // Remove filename and path delimiter
151 for(i = strlen(path)-1; i>=0; i--)
159 bool detectMSVCBuildDir(char *c_path)
161 std::string path(c_path);
162 const char *ends[] = {"bin\\Release", "bin\\Build", NULL};
163 return (removeStringEnd(path, ends) != "");
166 void initializePaths()
170 Use relative paths if RUN_IN_PLACE
173 infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
180 const DWORD buflen = 1000;
184 // Find path of executable and set path_share relative to it
185 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
186 assert(len < buflen);
187 pathRemoveFile(buf, '\\');
189 if(detectMSVCBuildDir(buf)){
190 infostream<<"MSVC build directory detected"<<std::endl;
191 path_share = std::string(buf) + "\\..\\..";
192 path_user = std::string(buf) + "\\..\\..";
195 path_share = std::string(buf) + "\\..";
196 path_user = std::string(buf) + "\\..";
205 memset(buf, 0, BUFSIZ);
206 // Get path to executable
207 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
209 pathRemoveFile(buf, '/');
211 path_share = std::string(buf) + "/..";
212 path_user = std::string(buf) + "/..";
217 #elif defined(__APPLE__)
219 //https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dyld.3.html
220 //TODO: Test this code
222 uint32_t len = sizeof(buf);
223 assert(_NSGetExecutablePath(buf, &len) != 0);
225 pathRemoveFile(buf, '/');
227 path_share = std::string(buf) + "/..";
228 path_user = std::string(buf) + "/..";
233 #elif defined(__FreeBSD__)
237 size_t len = sizeof(buf);
241 mib[2] = KERN_PROC_PATHNAME;
243 assert(sysctl(mib, 4, buf, &len, NULL, 0) != -1);
245 pathRemoveFile(buf, '/');
247 path_share = std::string(buf) + "/..";
248 path_user = std::string(buf) + "/..";
252 //TODO: Get path of executable. This assumes working directory is bin/
253 dstream<<"WARNING: Relative path not properly supported on this platform"
255 path_share = std::string("..");
256 path_user = std::string("..");
260 #else // RUN_IN_PLACE
263 Use platform-specific paths otherwise
266 infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
273 const DWORD buflen = 1000;
277 // Find path of executable and set path_share relative to it
278 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
279 assert(len < buflen);
280 pathRemoveFile(buf, '\\');
283 path_share = std::string(buf) + "\\..";
285 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
286 len = GetEnvironmentVariable("APPDATA", buf, buflen);
287 assert(len < buflen);
288 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
295 // Get path to executable
296 std::string bindir = "";
299 memset(buf, 0, BUFSIZ);
300 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
301 pathRemoveFile(buf, '/');
305 // Find share directory from these.
306 // It is identified by containing the subdirectory "builtin".
307 std::list<std::string> trylist;
308 std::string static_sharedir = STATIC_SHAREDIR;
309 if(static_sharedir != "" && static_sharedir != ".")
310 trylist.push_back(static_sharedir);
311 trylist.push_back(bindir + "/../share/" + PROJECT_NAME);
312 trylist.push_back(bindir + "/..");
314 for(std::list<std::string>::const_iterator i = trylist.begin();
315 i != trylist.end(); i++)
317 const std::string &trypath = *i;
318 if(!fs::PathExists(trypath) || !fs::PathExists(trypath + "/builtin")){
319 dstream<<"WARNING: system-wide share not found at \""
320 <<trypath<<"\""<<std::endl;
323 // Warn if was not the first alternative
324 if(i != trylist.begin()){
325 dstream<<"WARNING: system-wide share found at \""
326 <<trypath<<"\""<<std::endl;
328 path_share = trypath;
332 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
337 #elif defined(__APPLE__)
340 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
341 CFBundleRef main_bundle = CFBundleGetMainBundle();
342 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
344 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
346 dstream<<"Bundle resource path: "<<path<<std::endl;
348 path_share = std::string(path) + "/share";
353 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
355 CFRelease(resources_url);
357 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
359 #elif defined(__FreeBSD__)
361 path_share = STATIC_SHAREDIR;
362 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
366 #endif // RUN_IN_PLACE
369 } //namespace porting