15 #include "executionEnvironment.h"
16 #include "pandaVersion.h"
23 #include <sys/param.h>
29 #define getcwd _getcwd
43 #include <mach-o/dyld.h>
45 #include <crt_externs.h>
47 #define environ (*_NSGetEnviron())
56 extern char **environ;
59 #include <sys/types.h>
60 #include <sys/sysctl.h>
63 #if defined(IS_LINUX) || defined(IS_FREEBSD)
78 #ifndef STATIC_INIT_GETENV
79 #define PREREAD_ENVIRONMENT
86 #if !defined(WIN32_VC) && defined(HAVE_GLOBAL_ARGV) && defined(PROTOTYPE_GLOBAL_ARGV)
87 extern char **GLOBAL_ARGV;
88 extern int GLOBAL_ARGC;
103 ExecutionEnvironment::
104 ExecutionEnvironment() {
105 read_environment_variables();
124 size_t dollar = str.find(
'$');
125 while (dollar != string::npos && dollar + 1 < str.length()) {
126 size_t start = dollar + 1;
128 if (str[start] ==
'$') {
130 result += str.substr(last, start - last);
137 if (str[start] ==
'{') {
139 end = str.find(
'}', start + 1);
140 if (end != string::npos) {
141 varname = str.substr(start + 1, end - (start + 1));
148 while (end < str.length() && (isalnum(str[end]) || str[end] ==
'_')) {
151 varname = str.substr(start, end - start);
155 result += str.substr(last, dollar - last);
160 dollar = str.find(
'$', last);
163 result += str.substr(last);
178 static size_t bufsize = 1024;
179 static wchar_t *buffer = NULL;
181 if (buffer == (
wchar_t *)NULL) {
182 buffer =
new wchar_t[bufsize];
185 while (_wgetcwd(buffer, bufsize) == (
wchar_t *)NULL) {
186 if (errno != ERANGE) {
191 bufsize = bufsize * 2;
192 buffer =
new wchar_t[bufsize];
193 assert(buffer != (
wchar_t *)NULL);
202 static size_t bufsize = 1024;
203 static char *buffer = NULL;
205 if (buffer == (
char *)NULL) {
206 buffer =
new char[bufsize];
209 while (getcwd(buffer, bufsize) == (
char *)NULL) {
210 if (errno != ERANGE) {
215 bufsize = bufsize * 2;
216 buffer =
new char[bufsize];
217 assert(buffer != (
char *)NULL);
232 bool ExecutionEnvironment::
233 ns_has_environment_variable(
const string &var)
const {
234 #ifdef PREREAD_ENVIRONMENT
235 return _variables.count(var) != 0;
237 return getenv(var.c_str()) != (
char *)NULL;
248 string ExecutionEnvironment::
249 ns_get_environment_variable(
const string &var)
const {
250 EnvironmentVariables::const_iterator evi;
251 evi = _variables.find(var);
252 if (evi != _variables.end()) {
253 return (*evi).second;
261 }
else if (var ==
"TEMP") {
263 }
else if (var ==
"USER_APPDATA") {
265 }
else if (var ==
"COMMON_APPDATA") {
267 }
else if (var ==
"MAIN_DIR") {
270 if (!ns_has_environment_variable(
"PANDA_INCOMPATIBLE_PYTHON") && Py_IsInitialized()) {
276 PyGILState_STATE state = PyGILState_Ensure();
280 PyObject *obj = PySys_GetObject((
char*)
"argv");
281 if (obj != NULL && PyList_Check(obj)) {
282 PyObject *item = PyList_GetItem(obj, 0);
284 if (PyUnicode_Check(item)) {
285 Py_ssize_t size = PyUnicode_GetSize(item);
286 wchar_t *data =
new wchar_t[size + 1];
287 #if PY_MAJOR_VERSION >= 3
288 if (PyUnicode_AsWideChar(item, data, size) != -1) {
290 if (PyUnicode_AsWideChar((PyUnicodeObject*) item, data, size) != -1) {
292 wstring wstr (data, size);
297 #if PY_MAJOR_VERSION < 3
298 else if (PyString_Check(item)) {
299 char *str = PyString_AsString(item);
300 if (str != (
char *)NULL) {
309 PyGILState_Release(state);
312 if (main_dir.empty()) {
322 if (!_binary_name.empty()) {
329 #ifndef PREREAD_ENVIRONMENT
330 const char *def = getenv(var.c_str());
331 if (def != (
char *)NULL) {
343 static struct {
int id;
const char *name; } csidl_table[] = {
344 { CSIDL_ADMINTOOLS,
"ADMINTOOLS" },
345 { CSIDL_ALTSTARTUP,
"ALTSTARTUP" },
346 { CSIDL_APPDATA,
"APPDATA" },
347 { CSIDL_BITBUCKET,
"BITBUCKET" },
348 { CSIDL_CDBURN_AREA,
"CDBURN_AREA" },
349 { CSIDL_COMMON_ADMINTOOLS,
"COMMON_ADMINTOOLS" },
350 { CSIDL_COMMON_ALTSTARTUP,
"COMMON_ALTSTARTUP" },
351 { CSIDL_COMMON_APPDATA,
"COMMON_APPDATA" },
352 { CSIDL_COMMON_DESKTOPDIRECTORY,
"COMMON_DESKTOPDIRECTORY" },
353 { CSIDL_COMMON_DOCUMENTS,
"COMMON_DOCUMENTS" },
354 { CSIDL_COMMON_FAVORITES,
"COMMON_FAVORITES" },
355 { CSIDL_COMMON_MUSIC,
"COMMON_MUSIC" },
356 { CSIDL_COMMON_OEM_LINKS,
"COMMON_OEM_LINKS" },
357 { CSIDL_COMMON_PICTURES,
"COMMON_PICTURES" },
358 { CSIDL_COMMON_PROGRAMS,
"COMMON_PROGRAMS" },
359 { CSIDL_COMMON_STARTMENU,
"COMMON_STARTMENU" },
360 { CSIDL_COMMON_STARTUP,
"COMMON_STARTUP" },
361 { CSIDL_COMMON_TEMPLATES,
"COMMON_TEMPLATES" },
362 { CSIDL_COMMON_VIDEO,
"COMMON_VIDEO" },
363 { CSIDL_COMPUTERSNEARME,
"COMPUTERSNEARME" },
364 { CSIDL_CONNECTIONS,
"CONNECTIONS" },
365 { CSIDL_CONTROLS,
"CONTROLS" },
366 { CSIDL_COOKIES,
"COOKIES" },
367 { CSIDL_DESKTOP,
"DESKTOP" },
368 { CSIDL_DESKTOPDIRECTORY,
"DESKTOPDIRECTORY" },
369 { CSIDL_DRIVES,
"DRIVES" },
370 { CSIDL_FAVORITES,
"FAVORITES" },
371 { CSIDL_FONTS,
"FONTS" },
372 { CSIDL_HISTORY,
"HISTORY" },
373 { CSIDL_INTERNET,
"INTERNET" },
374 { CSIDL_INTERNET_CACHE,
"INTERNET_CACHE" },
375 { CSIDL_LOCAL_APPDATA,
"LOCAL_APPDATA" },
376 { CSIDL_MYDOCUMENTS,
"MYDOCUMENTS" },
377 { CSIDL_MYMUSIC,
"MYMUSIC" },
378 { CSIDL_MYPICTURES,
"MYPICTURES" },
379 { CSIDL_MYVIDEO,
"MYVIDEO" },
380 { CSIDL_NETHOOD,
"NETHOOD" },
381 { CSIDL_NETWORK,
"NETWORK" },
382 { CSIDL_PERSONAL,
"PERSONAL" },
383 { CSIDL_PRINTERS,
"PRINTERS" },
384 { CSIDL_PRINTHOOD,
"PRINTHOOD" },
385 { CSIDL_PROFILE,
"PROFILE" },
386 { CSIDL_PROGRAM_FILES,
"PROGRAM_FILES" },
387 { CSIDL_PROGRAM_FILESX86,
"PROGRAM_FILESX86" },
388 { CSIDL_PROGRAM_FILES_COMMON,
"PROGRAM_FILES_COMMON" },
389 { CSIDL_PROGRAM_FILES_COMMONX86,
"PROGRAM_FILES_COMMONX86" },
390 { CSIDL_PROGRAMS,
"PROGRAMS" },
391 { CSIDL_RECENT,
"RECENT" },
392 { CSIDL_RESOURCES,
"RESOURCES" },
393 { CSIDL_RESOURCES_LOCALIZED,
"RESOURCES_LOCALIZED" },
394 { CSIDL_SENDTO,
"SENDTO" },
395 { CSIDL_STARTMENU,
"STARTMENU" },
396 { CSIDL_STARTUP,
"STARTUP" },
397 { CSIDL_SYSTEM,
"SYSTEM" },
398 { CSIDL_SYSTEMX86,
"SYSTEMX86" },
399 { CSIDL_TEMPLATES,
"TEMPLATES" },
400 { CSIDL_WINDOWS,
"WINDOWS" },
404 for (
int i = 0; csidl_table[i].name != NULL; ++i) {
405 if (strcmp(var.c_str(), csidl_table[i].name) == 0) {
406 wchar_t buffer[MAX_PATH];
407 if (SHGetSpecialFolderPathW(NULL, buffer, csidl_table[i].
id,
true)) {
426 void ExecutionEnvironment::
427 ns_set_environment_variable(
const string &var,
const string &value) {
428 _variables[var] = value;
429 string putstr = var +
"=" + value;
432 char *put = (
char *)malloc(putstr.length() + 1);
433 strcpy(put, putstr.c_str());
442 void ExecutionEnvironment::
443 ns_shadow_environment_variable(
const string &var,
const string &value) {
444 _variables[var] = value;
445 string putstr = var +
"=" + value;
453 void ExecutionEnvironment::
454 ns_clear_shadow(
const string &var) {
455 EnvironmentVariables::iterator vi = _variables.find(var);
456 if (vi == _variables.end()) {
460 #ifdef PREREAD_ENVIRONMENT
462 const char *def = getenv(var.c_str());
463 if (def != (
char *)NULL) {
466 _variables.erase(vi);
468 #endif // PREREAD_ENVIRONMENT
478 int ExecutionEnvironment::
479 ns_get_num_args()
const {
492 string ExecutionEnvironment::
493 ns_get_arg(
int n)
const {
494 assert(n >= 0 && n < ns_get_num_args());
505 string ExecutionEnvironment::
506 ns_get_binary_name()
const {
507 if (_binary_name.empty()) {
520 string ExecutionEnvironment::
521 ns_get_dtool_name()
const {
522 if (_dtool_name.empty()) {
550 void ExecutionEnvironment::
551 read_environment_variables() {
552 #ifdef PREREAD_ENVIRONMENT
553 #if defined(IS_OSX) || defined(IS_FREEBSD) || defined(IS_LINUX)
558 for (envp = environ; envp && *envp; envp++) {
563 for (envc = *envp; envc && *envc && strncmp(envc,
"=", 1) != 0; envc++) {
564 variable += (char) *envc;
567 if (strncmp(envc,
"=", 1) == 0) {
568 for (envc++; envc && *envc; envc++) {
569 value += (char) *envc;
573 if (!variable.empty()) {
574 _variables[variable] = value;
577 #elif defined(HAVE_PROC_SELF_ENVIRON)
581 pifstream proc(
"/proc/self/environ");
583 cerr <<
"Cannot read /proc/self/environ; environment variables unavailable.\n";
588 while (!proc.eof() && !proc.fail()) {
592 while (!proc.eof() && !proc.fail() && ch !=
'=' && ch !=
'\0') {
593 variable += (char)ch;
599 while (!proc.eof() && !proc.fail() && ch !=
'\0') {
605 if (!variable.empty()) {
606 _variables[variable] = value;
611 cerr <<
"Warning: environment variables unavailable to dconfig.\n";
613 #endif // PREREAD_ENVIRONMENT
622 void ExecutionEnvironment::
630 HMODULE dllhandle = GetModuleHandle(
"libp3dtool_d.dll");
632 HMODULE dllhandle = GetModuleHandle(
"libp3dtool.dll");
634 if (dllhandle != 0) {
635 static const DWORD buffer_size = 1024;
636 wchar_t buffer[buffer_size];
637 DWORD size = GetModuleFileNameW(dllhandle, buffer, buffer_size);
646 #if defined(__APPLE__)
649 if (_dtool_name.empty()) {
650 uint32_t ic = _dyld_image_count();
651 for (uint32_t i = 0; i < ic; ++i) {
652 const char *buffer = _dyld_get_image_name(i);
653 const char *tail = strrchr(buffer,
'/');
654 if (tail && (strcmp(tail,
"/libp3dtool." PANDA_ABI_VERSION_STR
".dylib") == 0
655 || strcmp(tail,
"/libp3dtool.dylib") == 0)) {
656 _dtool_name = buffer;
662 #if defined(IS_FREEBSD) || defined(IS_LINUX)
665 char origin[PATH_MAX + 1];
667 if (_dtool_name.empty()) {
668 void *dtool_handle = dlopen(
"libp3dtool.so." PANDA_ABI_VERSION_STR, RTLD_NOW | RTLD_NOLOAD);
669 if (dtool_handle != NULL && dlinfo(dtool_handle, RTLD_DI_ORIGIN, origin) != -1) {
670 _dtool_name = origin;
671 _dtool_name +=
"/libp3dtool.so." PANDA_ABI_VERSION_STR;
674 dtool_handle = dlopen(
"libp3dtool.so", RTLD_NOW | RTLD_NOLOAD);
675 if (dtool_handle != NULL && dlinfo(dtool_handle, RTLD_DI_ORIGIN, origin) != -1) {
676 _dtool_name = origin;
677 _dtool_name +=
"/libp3dtool.so";
683 #if defined(IS_FREEBSD)
686 if (_dtool_name.empty()) {
688 dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
690 while (map != NULL) {
691 char *tail = strrchr(map->l_name,
'/');
692 char *head = strchr(map->l_name,
'/');
693 if (tail && head && (strcmp(tail,
"/libp3dtool.so." PANDA_ABI_VERSION_STR) == 0
694 || strcmp(tail,
"/libp3dtool.so") == 0)) {
702 #if defined(HAVE_PROC_SELF_MAPS) || defined(HAVE_PROC_CURPROC_MAP)
705 if (_dtool_name.empty()) {
706 #ifdef HAVE_PROC_CURPROC_MAP
707 pifstream maps(
"/proc/curproc/map");
709 pifstream maps(
"/proc/self/maps");
711 while (!maps.fail() && !maps.eof()) {
712 char buffer[PATH_MAX];
714 maps.getline(buffer, PATH_MAX);
715 char *tail = strrchr(buffer,
'/');
716 char *head = strchr(buffer,
'/');
717 if (tail && head && (strcmp(tail,
"/libp3dtool.so." PANDA_ABI_VERSION_STR) == 0
718 || strcmp(tail,
"/libp3dtool.so") == 0)) {
730 if (_binary_name.empty()) {
731 static const DWORD buffer_size = 1024;
732 wchar_t buffer[buffer_size];
733 DWORD size = GetModuleFileNameW(NULL, buffer, buffer_size);
742 #if defined(__APPLE__)
744 if (_binary_name.empty()) {
745 char *pathbuf =
new char[PATH_MAX];
746 uint32_t bufsize = PATH_MAX;
747 if (_NSGetExecutablePath(pathbuf, &bufsize) == 0) {
748 _binary_name = pathbuf;
754 #if defined(IS_FREEBSD)
757 if (_binary_name.empty()) {
758 size_t bufsize = 4096;
760 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
762 if (sysctl(mib, 4, (
void*) buffer, &bufsize, NULL, 0) == -1) {
765 _binary_name = buffer;
770 #if defined(HAVE_PROC_SELF_EXE) || defined(HAVE_PROC_CURPROC_FILE)
774 if (_binary_name.empty()) {
775 char readlinkbuf [PATH_MAX];
776 #ifdef HAVE_PROC_CURPROC_FILE
777 int pathlen = readlink(
"/proc/curproc/file", readlinkbuf, PATH_MAX - 1);
779 int pathlen = readlink(
"/proc/self/exe", readlinkbuf, PATH_MAX - 1);
782 readlinkbuf[pathlen] = 0;
783 _binary_name = readlinkbuf;
791 #if defined(WIN32_VC)
796 LPWSTR cmdline = GetCommandLineW();
798 LPWSTR *wargv = CommandLineToArgvW(cmdline, &argc);
801 cerr <<
"CommandLineToArgvW failed; command-line arguments unavailable to config.\n";
807 for (
int i = 0; i < argc; ++i) {
808 wstring wtext(wargv[i]);
812 if (_binary_name.empty()) {
816 _args.push_back(encoder.
get_text());
823 #elif defined(IS_FREEBSD)
826 size_t bufsize = 4096;
828 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0};
830 if (sysctl(mib, 4, (
void*) buffer, &bufsize, NULL, 0) == -1) {
833 if (_binary_name.empty()) {
834 _binary_name = buffer;
836 int idx = strlen(buffer) + 1;
837 while (idx < bufsize) {
838 _args.push_back((
char*)(buffer + idx));
839 int newidx = strlen(buffer + idx);
844 #elif defined(HAVE_GLOBAL_ARGV)
845 int argc = GLOBAL_ARGC;
849 if (GLOBAL_ARGV != NULL) {
850 if (_binary_name.empty() && argc > 0) {
851 _binary_name = GLOBAL_ARGV[0];
855 for (
int i = 1; i < argc; i++) {
856 _args.push_back(GLOBAL_ARGV[i]);
860 #elif defined(HAVE_PROC_SELF_CMDLINE) || defined(HAVE_PROC_CURPROC_CMDLINE)
867 #ifdef HAVE_PROC_CURPROC_CMDLINE
868 pifstream proc(
"/proc/curproc/cmdline");
870 cerr <<
"Cannot read /proc/curproc/cmdline; command-line arguments unavailable to config.\n";
872 pifstream proc(
"/proc/self/cmdline");
874 cerr <<
"Cannot read /proc/self/cmdline; command-line arguments unavailable to config.\n";
879 while (!proc.eof() && !proc.fail()) {
882 while (!proc.eof() && !proc.fail() && ch !=
'\0') {
888 if (_binary_name.empty())
891 _args.push_back(arg);
903 if (!_binary_name.empty()) {
904 char newpath [PATH_MAX + 1];
905 if (realpath(_binary_name.c_str(), newpath) != NULL) {
906 _binary_name = newpath;
910 if (!_dtool_name.empty()) {
911 char newpath [PATH_MAX + 1];
912 if (realpath(_dtool_name.c_str(), newpath) != NULL) {
913 _dtool_name = newpath;
920 if (_dtool_name.empty()) {
921 _dtool_name = _binary_name;
This class can be used to convert text between multiple representations, e.g.
string get_text() const
Returns the current text, as encoded via the current encoding system.
bool make_true_case()
On a case-insensitive operating system (e.g.
string get_dirname() const
Returns the directory part of the filename.
static const Filename & get_home_directory()
Returns a path to the user's home directory, if such a thing makes sense in the current OS...
static TextEncoder::Encoding get_filesystem_encoding()
Specifies the default encoding to be used for all subsequent Filenames objects.
The name of a file, such as a texture file or an Egg file.
static const Filename & get_user_appdata_directory()
Returns a path to a system-defined directory appropriate for creating a subdirectory for storing appl...
static const Filename & get_temp_directory()
Returns a path to a system-defined temporary directory.
string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
static string get_environment_variable(const string &var)
Returns the definition of the indicated environment variable, or the empty string if the variable is ...
void make_absolute()
Converts the filename to a fully-qualified pathname from the root (if it is a relative pathname)...
void set_encoding(Encoding encoding)
Specifies how the string set via set_text() is to be interpreted.
static const Filename & get_common_appdata_directory()
Returns a path to a system-defined directory appropriate for creating a subdirectory for storing appl...
Encapsulates access to the environment variables and command-line arguments at the time of execution...
static Filename from_os_specific_w(const wstring &os_specific, Type type=T_general)
The wide-string variant of from_os_specific().
static Filename get_cwd()
Returns the name of the current working directory.
static string expand_string(const string &str)
Reads the string, looking for environment variable names marked by a $.
void set_wtext(const wstring &wtext)
Changes the text that is stored in the encoder.
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).