2 * Name : application_configuration
3 * Author : Chris Koeritz
5 * Copyright (c) 1994-$now By Author. This program is free software; you can
6 * redistribute it and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either version 2 of
8 * the License or (at your option) any later version. This is online at:
9 * http://www.fsf.org/copyleft/gpl.html
10 * Please send any updates to: fred@gruntose.com
13 #include "application_configuration.h"
14 #include "ini_configurator.h"
16 #include <application/windoze_helper.h>
17 #include <basis/environment.h>
18 #include <basis/functions.h>
19 #include <basis/guards.h>
20 #include <basis/mutex.h>
21 #include <basis/utf_conversion.h>
22 #include <filesystem/directory.h>
23 #include <filesystem/filename.h>
24 #include <mathematics/chaos.h>
25 #include <structures/static_memory_gremlin.h>
26 #include <textual/parser_bits.h>
27 #include <system_helper.h>
30 #include <mach-o/dyld.h>
38 #include <sys/utsname.h>
46 using namespace basis;
47 using namespace filesystem;
48 using namespace mathematics;
49 using namespace structures;
50 using namespace textual;
53 #define LOG(to_print) printf("%s\n", astring(to_print).s())
55 namespace configuration {
57 const int MAXIMUM_COMMAND_LINE = 32 * KILOBYTE;
58 // maximum command line that we'll deal with here.
60 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
61 astring application_configuration::get_cmdline_from_proc()
63 FUNCDEF("get_cmdline_from_proc");
64 static astring __check_once_app_path;
65 //hmmm: we want to use a single per app static synch here!
66 if (__check_once_app_path.length()) return __check_once_app_path;
69 __check_once_app_path = query_for_process_info();
70 return __check_once_app_path;
73 // we have not looked this app's name up in the path yet.
74 a_sprintf cmds_filename("/proc/%d/cmdline", process_id());
75 FILE *cmds_file = fopen(cmds_filename.s(), "r");
77 LOG("failed to open our process's command line file.\n");
80 //hmmm: this would be a lot nicer using a byte filer.
82 char *filebuff = new char[size + 1];
83 ssize_t chars_read = getline((char **)&filebuff, &size, cmds_file);
84 // read the first line, giving ample space for how long it might be.
85 fclose(cmds_file); // drop the file again.
86 if (!chars_read || negative(chars_read)) {
87 LOG("failed to get any characters from our process's cmdline file.\n");
90 // copy the buffer into a string, which works great since the entries in the
91 // command line are all separated by zero characters.
92 __check_once_app_path = filebuff;
94 //printf("got an app name before chewing: %s\n", __check_once_app_path.s());
95 // clean out quote characters from the name.
96 for (int i = __check_once_app_path.length() - 1; i >= 0; i--) {
97 if (__check_once_app_path[i] == '"') __check_once_app_path.zap(i, i);
99 // check if the thing has a path attached to it. if it doesn't, we need to accentuate
100 // our knowledge about the file.
101 filename testing(__check_once_app_path);
102 if (testing.had_directory()) return __check_once_app_path; // all set.
104 //printf("no dir part found, app name after chewing: %s\n", __check_once_app_path.s());
106 //hmmm: the below might be better off as a find app in path method, which relies on which.
107 // there was no directory component, so we'll try to guess one.
108 astring temp_filename(environment::TMP()
109 + a_sprintf("/zz_cmdfind.%d", chaos().inclusive(0, 999999999)));
110 system((astring("which ") + __check_once_app_path + " >" + temp_filename).s());
111 FILE *which_file = fopen(temp_filename.s(), "r");
113 LOG("failed to open the temporary output from which.\n");
116 // reallocate the file buffer.
118 filebuff = new char[size + 1];
119 chars_read = getline((char **)&filebuff, &size, which_file);
121 unlink(temp_filename.s());
122 if (!chars_read || negative(chars_read)) {
123 LOG("failed to get any characters from the which cmd output.\n");
126 // we had some luck using 'which' to locate the file, so we'll use this version.
127 __check_once_app_path = filebuff;
128 while (parser_bits::is_eol(__check_once_app_path[__check_once_app_path.end()])) {
129 __check_once_app_path.zap(__check_once_app_path.end(), __check_once_app_path.end());
133 return __check_once_app_path; // return whatever which told us.
136 // deprecated; better to use the /proc/pid/cmdline file.
137 astring application_configuration::query_for_process_info()
139 FUNCDEF("query_for_process_info");
140 astring to_return = "unknown";
141 // we ask the operating system about our process identifier and store
142 // the results in a temporary file.
144 a_sprintf tmpfile("/tmp/proc_name_check_%d_%d.txt", process_id(),
145 rando.inclusive(0, 128000));
147 a_sprintf cmd("ps -o args=\"\" %d >%s", process_id(),
150 a_sprintf cmd("ps h -O \"args\" %d >%s", process_id(),
153 // run the command to locate our process info.
154 int sysret = system(cmd.s());
155 if (negative(sysret)) {
156 LOG("failed to run ps command to get process info");
159 // open the output file for reading.
160 FILE *output = fopen(tmpfile.s(), "r");
162 LOG("failed to open the ps output file");
165 // read the file's contents into a string buffer.
166 char buff[MAXIMUM_COMMAND_LINE];
167 size_t size_read = fread(buff, 1, MAXIMUM_COMMAND_LINE, output);
169 // success at finding some text in the file at least.
170 while (size_read > 0) {
171 const char to_check = buff[size_read - 1];
172 if ( !to_check || (to_check == '\r') || (to_check == '\n')
173 || (to_check == '\t') )
177 to_return.reset(astring::UNTERMINATED, buff, size_read);
179 // couldn't read anything.
180 LOG("could not read output of process list");
187 // used as a return value when the name cannot be determined.
188 #define SET_BOGUS_NAME(error) { \
192 unlink(tmpfile.s()); \
194 astring home_dir = environment::get("HOME"); \
195 to_return = home_dir + "/failed_to_determine.exe"; \
198 astring application_configuration::application_name()
200 FUNCDEF("application_name");
203 char buffer[MAX_ABS_PATH] = { '\0' };
204 uint32_t buffsize = MAX_ABS_PATH - 1;
205 _NSGetExecutablePath(buffer, &buffsize);
206 to_return = (char *)buffer;
207 #elif defined(__UNIX__) || defined(__GNU_WINDOWS__)
208 to_return = get_cmdline_from_proc();
209 #elif defined(_MSC_VER)
210 flexichar low_buff[MAX_ABS_PATH + 1];
211 GetModuleFileName(NULL_POINTER, low_buff, MAX_ABS_PATH - 1);
212 astring buff = from_unicode_temp(low_buff);
213 buff.to_lower(); // we lower-case the name since windows seems to UC it.
216 #pragma error("hmmm: no means of finding app name is implemented.")
217 SET_BOGUS_NAME("not_implemented_for_this_OS");
222 #if defined(__UNIX__) || defined(_MSC_VER) || defined(__GNU_WINDOWS__)
223 basis::un_int application_configuration::process_id() { return getpid(); }
225 #pragma error("hmmm: need process id implementation for this OS!")
226 basis::un_int application_configuration::process_id() { return 0; }
229 astring application_configuration::current_directory()
233 char buff[MAX_ABS_PATH];
234 getcwd(buff, MAX_ABS_PATH - 1);
236 #elif defined(_MSC_VER)
237 flexichar low_buff[MAX_ABS_PATH + 1];
238 GetCurrentDirectory(MAX_ABS_PATH, low_buff);
239 to_return = from_unicode_temp(low_buff);
241 #pragma error("hmmm: need support for current directory on this OS.")
247 // implement the software product function.
248 const char *application_configuration::software_product_name()
250 #ifdef GLOBAL_PRODUCT_NAME
251 return GLOBAL_PRODUCT_NAME;
257 astring application_configuration::application_directory()
258 { return filename(application_name()).dirname().raw(); }
260 structures::version application_configuration::get_OS_version()
264 utsname kernel_parms;
265 uname(&kernel_parms);
266 to_return = version(kernel_parms.release);
267 #elif defined(_MSC_VER)
269 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
270 ::GetVersionEx(&info);
271 to_return = version(a_sprintf("%u.%u.%u.%u", basis::un_short(info.dwMajorVersion),
272 basis::un_short(info.dwMinorVersion), basis::un_short(info.dwPlatformId),
273 basis::un_short(info.dwBuildNumber)));
275 #pragma error("hmmm: need version info for this OS!")
282 const char *PATH_CONFIGURATION_FILENAME() { return "paths.ini"; }
284 astring application_configuration::application_configuration_file()
286 filename cfg_file(application_directory() + "/" + PATH_CONFIGURATION_FILENAME());
287 return cfg_file.raw();
290 const astring &application_configuration::GLOBAL_SECTION_NAME() { STATIC_STRING("Common"); }
292 const astring &application_configuration::LOGGING_FOLDER_NAME() { STATIC_STRING("LogPath"); }
294 //const astring &application_configuration::WINDOZE_VIRTUAL_ROOT_NAME()
295 //{ STATIC_STRING("VirtualUnixRoot"); }
297 const astring &application_configuration::DEFAULT_VIRTUAL_UNIX_ROOT()
298 { STATIC_STRING("c:/cygwin"); }
302 // static storage for virtual unix root, if it's used.
303 // we don't expect it to change during runtime, right? that would be fubar.
304 // so we cache it once we retrieve it.
305 SAFE_STATIC(astring, static_root_holder, )
307 const astring &application_configuration::virtual_unix_root()
309 // see if we already cached the root. it shouldn't change during runtime.
310 if (static_root_holder().length()) {
311 return static_root_holder();
314 // simple implementation for unix/linux; just tell the truth about the real root.
315 static_root_holder() = astring("/");
316 return static_root_holder();
320 use the path in our system helpers header, which should have been set during the
321 build process if this is really windows.
323 astring virtual_root = FEISTY_MEOW_VIRTUAL_UNIX_ROOT;
325 // if it has no length, we didn't get our setting! we'll limp along with a guess.
326 return DEFAULT_VIRTUAL_UNIX_ROOT();
328 static_root_holder() = virtual_root;
329 return static_root_holder();
337 ////const int MAX_LOG_PATH = 512;
338 // the maximum length of the entry stored for the log path.
340 astring application_configuration::get_logging_directory()
342 // new scheme is to just use the temporary directory, which can vary per user
343 // and which hopefully is always set to something usable.
344 astring def_log = environment::TMP();
345 // add logs directory underneath that.
347 // add the subdirectory for logs.
349 // now grab the current value for the name, if any.
350 astring log_dir = read_item(LOGGING_FOLDER_NAME());
351 // get the entry for the logging path.
353 // if the entry was absent, we set it.
354 //printf("did not find log dir in config file\n");
355 ini_configurator ini(application_configuration_file(),
356 ini_configurator::RETURN_ONLY,
357 ini_configurator::APPLICATION_DIRECTORY);
358 ini.store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
360 // they gave us something. let's replace the environment variables
361 // in their string so we resolve paths and such.
362 log_dir = parser_bits::substitute_env_vars(log_dir);
363 //printf("%s", (char *)a_sprintf("got log dir with %s value\n", log_dir.s()).s());
366 // now we make sure the directory exists.
367 filename testing(log_dir);
368 if (!testing.exists()) {
369 bool okay = directory::recursive_create(log_dir);
371 LOG(astring("failed to create logging directory: ") + log_dir);
372 // return a directory almost guaranteed to exist; best we can do in this case.
385 astring application_configuration::make_logfile_name(const astring &base_name)
386 { return get_logging_directory() + "/" + base_name; }
388 astring application_configuration::read_item(const astring &key_name)
390 filename ini_name = application_configuration_file();
391 ini_configurator ini(ini_name, ini_configurator::RETURN_ONLY,
392 ini_configurator::APPLICATION_DIRECTORY);
393 astring to_return = ini.load(GLOBAL_SECTION_NAME(), key_name, "");
395 // if the string has any length, then we process any environment
396 // variables found encoded in the value.
397 to_return = parser_bits::substitute_env_vars(to_return);