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/filename.h>
23 #include <mathematics/chaos.h>
24 #include <structures/static_memory_gremlin.h>
25 #include <textual/parser_bits.h>
28 #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.
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 //hmmm: the below might be better off as a find app in path method, which relies on which.
105 printf("no dir part found, app name after chewing: %s\n", __check_once_app_path.s());
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;
208 to_return = get_cmdline_from_proc();
209 #elif defined(__WIN32__)
210 flexichar low_buff[MAX_ABS_PATH + 1];
211 GetModuleFileName(NIL, 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(__WIN32__)
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(__WIN32__)
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(__WIN32__)
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"); }
296 const int MAX_LOG_PATH = 200;
297 // the maximum length of the entry stored for the log path.
299 astring application_configuration::get_logging_directory()
301 // start with the root of our installation.
302 astring def_log = application_directory();
303 // add logs directory underneath that.
305 // add the subdirectory for logs.
307 // now grab the current value for the name, if any.
308 astring log_dir = read_item(LOGGING_FOLDER_NAME());
309 // get the entry for the logging path.
311 // if the entry was absent, we set it.
312 ini_configurator ini(application_configuration_file(),
313 ini_configurator::RETURN_ONLY,
314 ini_configurator::APPLICATION_DIRECTORY);
315 ini.store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
317 // they gave us something. let's replace the environment variables
318 // in their string so we resolve paths and such.
319 log_dir = parser_bits::substitute_env_vars(log_dir);
322 // now we make sure the directory exists.
324 int stat_ret = stat(log_dir.observe(), &to_fill);
325 if (stat_ret || !(to_fill.st_mode & S_IFDIR) ) {
326 // if it's not anything yet or if it's not a directory, then we need
328 //if it's something besides a directory... should it be deleted?
330 int mk_ret = mkdir(log_dir.s(), 0777);
333 int mk_ret = mkdir(log_dir.s());
335 if (mk_ret) return "";
336 //can't have a log file if we can't make the directory successfully???
342 astring application_configuration::make_logfile_name(const astring &base_name)
343 { return get_logging_directory() + "/" + base_name; }
345 astring application_configuration::read_item(const astring &key_name)
347 filename ini_name = application_configuration_file();
348 ini_configurator ini(ini_name, ini_configurator::RETURN_ONLY,
349 ini_configurator::APPLICATION_DIRECTORY);
350 astring to_return = ini.load(GLOBAL_SECTION_NAME(), key_name, "");
352 // if the string has any length, then we process any environment
353 // variables found encoded in the value.
354 to_return = parser_bits::substitute_env_vars(to_return);