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 // clean out quote characters from the name.
95 for (int i = __check_once_app_path.length() - 1; i >= 0; i--) {
96 if (__check_once_app_path[i] == '"') __check_once_app_path.zap(i, i);
98 // check if the thing has a path attached to it. if it doesn't, we need to accentuate
99 // our knowledge about the file.
100 filename testing(__check_once_app_path);
101 if (testing.had_directory()) return __check_once_app_path; // all set.
103 //hmmm: the below might be better off as a find app in path method, which relies on which.
105 // there was no directory component, so we'll try to guess one.
106 astring temp_filename(environment::get("TMP")
107 + a_sprintf("/zz_cmdfind.%d", chaos().inclusive(0, 999999999)));
108 system((astring("which ") + __check_once_app_path + " >" + temp_filename).s());
109 FILE *which_file = fopen(temp_filename.s(), "r");
111 LOG("failed to open the temporary output from which.\n");
114 // reallocate the file buffer.
116 filebuff = new char[size + 1];
117 chars_read = getline((char **)&filebuff, &size, which_file);
119 unlink(temp_filename.s());
120 if (!chars_read || negative(chars_read)) {
121 LOG("failed to get any characters from the which cmd output.\n");
124 // we had some luck using 'which' to locate the file, so we'll use this version.
125 __check_once_app_path = filebuff;
126 while (parser_bits::is_eol(__check_once_app_path[__check_once_app_path.end()])) {
127 __check_once_app_path.zap(__check_once_app_path.end(), __check_once_app_path.end());
131 return __check_once_app_path; // return whatever which told us.
134 // deprecated; better to use the /proc/pid/cmdline file.
135 astring application_configuration::query_for_process_info()
137 // FUNCDEF("query_for_process_info");
138 astring to_return = "unknown";
139 // we ask the operating system about our process identifier and store
140 // the results in a temporary file.
142 a_sprintf tmpfile("/tmp/proc_name_check_%d_%d.txt", process_id(),
143 rando.inclusive(0, 128000));
145 a_sprintf cmd("ps -o args=\"\" %d >%s", process_id(),
148 a_sprintf cmd("ps h -O \"args\" %d >%s", process_id(),
151 // run the command to locate our process info.
152 int sysret = system(cmd.s());
153 if (negative(sysret)) {
154 LOG("failed to run ps command to get process info");
157 // open the output file for reading.
158 FILE *output = fopen(tmpfile.s(), "r");
160 LOG("failed to open the ps output file");
163 // read the file's contents into a string buffer.
164 char buff[MAXIMUM_COMMAND_LINE];
165 size_t size_read = fread(buff, 1, MAXIMUM_COMMAND_LINE, output);
167 // success at finding some text in the file at least.
168 while (size_read > 0) {
169 const char to_check = buff[size_read - 1];
170 if ( !to_check || (to_check == '\r') || (to_check == '\n')
171 || (to_check == '\t') )
175 to_return.reset(astring::UNTERMINATED, buff, size_read);
177 // couldn't read anything.
178 LOG("could not read output of process list");
185 // used as a return value when the name cannot be determined.
186 #define SET_BOGUS_NAME(error) { \
190 unlink(tmpfile.s()); \
192 astring home_dir = env_string("HOME"); \
193 to_return = home_dir + "/failed_to_determine.exe"; \
196 astring application_configuration::application_name()
198 // FUNCDEF("application_name");
201 char buffer[MAX_ABS_PATH] = { '\0' };
202 uint32_t buffsize = MAX_ABS_PATH - 1;
203 _NSGetExecutablePath(buffer, &buffsize);
204 to_return = (char *)buffer;
206 to_return = get_cmdline_from_proc();
207 #elif defined(__WIN32__)
208 flexichar low_buff[MAX_ABS_PATH + 1];
209 GetModuleFileName(NIL, low_buff, MAX_ABS_PATH - 1);
210 astring buff = from_unicode_temp(low_buff);
211 buff.to_lower(); // we lower-case the name since windows seems to UC it.
214 #pragma error("hmmm: no means of finding app name is implemented.")
215 SET_BOGUS_NAME("not_implemented_for_this_OS");
220 #if defined(__UNIX__) || defined(__WIN32__)
221 basis::un_int application_configuration::process_id() { return getpid(); }
223 #pragma error("hmmm: need process id implementation for this OS!")
224 basis::un_int application_configuration::process_id() { return 0; }
227 astring application_configuration::current_directory()
231 char buff[MAX_ABS_PATH];
232 getcwd(buff, MAX_ABS_PATH - 1);
234 #elif defined(__WIN32__)
235 flexichar low_buff[MAX_ABS_PATH + 1];
236 GetCurrentDirectory(MAX_ABS_PATH, low_buff);
237 to_return = from_unicode_temp(low_buff);
239 #pragma error("hmmm: need support for current directory on this OS.")
245 // implement the software product function.
246 const char *application_configuration::software_product_name()
248 #ifdef GLOBAL_PRODUCT_NAME
249 return GLOBAL_PRODUCT_NAME;
255 astring application_configuration::application_directory()
256 { return filename(application_name()).dirname().raw(); }
258 structures::version application_configuration::get_OS_version()
262 utsname kernel_parms;
263 uname(&kernel_parms);
264 to_return = version(kernel_parms.release);
265 #elif defined(__WIN32__)
267 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
268 ::GetVersionEx(&info);
269 to_return = version(a_sprintf("%u.%u.%u.%u", basis::un_short(info.dwMajorVersion),
270 basis::un_short(info.dwMinorVersion), basis::un_short(info.dwPlatformId),
271 basis::un_short(info.dwBuildNumber)));
273 #pragma error("hmmm: need version info for this OS!")
280 const char *PATH_CONFIGURATION_FILENAME() { return "paths.ini"; }
282 astring application_configuration::application_configuration_file()
284 filename cfg_file(application_directory() + "/" + PATH_CONFIGURATION_FILENAME());
285 return cfg_file.raw();
288 const astring &application_configuration::GLOBAL_SECTION_NAME() { STATIC_STRING("Common"); }
290 const astring &application_configuration::LOGGING_FOLDER_NAME() { STATIC_STRING("LogPath"); }
294 const int MAX_LOG_PATH = 200;
295 // the maximum length of the entry stored for the log path.
298 astring application_configuration::installation_root()
300 astring to_return = read_item(LOCAL_FOLDER_NAME());
302 // well, if this other guy has a path, we'll give that back. otherwise,
303 // we don't know what the path should be at all.
304 to_return = filename(application_configuration_file()).dirname();
310 astring application_configuration::get_logging_directory()
312 // start with the root of our installation.
313 astring def_log = application_directory();
314 ///installation_root();
315 // add logs directory underneath that.
317 // add the subdirectory for logs.
319 // now grab the current value for the name, if any.
320 astring log_dir = read_item(LOGGING_FOLDER_NAME());
321 // get the entry for the logging path.
323 // if the entry was absent, we set it.
324 ini_configurator ini(application_configuration_file(),
325 ini_configurator::RETURN_ONLY,
326 ini_configurator::APPLICATION_DIRECTORY);
327 ini.store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
329 // they gave us something. let's replace the environment variables
330 // in their string so we resolve paths and such.
331 log_dir = parser_bits::substitute_env_vars(log_dir);
334 // now we make sure the directory exists.
336 int stat_ret = stat(log_dir.observe(), &to_fill);
337 if (stat_ret || !(to_fill.st_mode & S_IFDIR) ) {
338 // if it's not anything yet or if it's not a directory, then we need
340 //if it's something besides a directory... should it be deleted?
342 int mk_ret = mkdir(log_dir.s(), 0777);
345 int mk_ret = mkdir(log_dir.s());
347 if (mk_ret) return "";
348 //can't have a log file if we can't make the directory successfully???
354 astring application_configuration::make_logfile_name(const astring &base_name)
355 { return get_logging_directory() + "/" + base_name; }
357 ///astring application_configuration::core_bin_directory() { return read_item("core_bin"); }
359 astring application_configuration::read_item(const astring &key_name)
361 filename ini_name = application_configuration_file();
362 ini_configurator ini(ini_name, ini_configurator::RETURN_ONLY,
363 ini_configurator::APPLICATION_DIRECTORY);
364 astring to_return = ini.load(GLOBAL_SECTION_NAME(), key_name, "");
366 // if the string has any length, then we process any environment
367 // variables found encoded in the value.
368 to_return = parser_bits::substitute_env_vars(to_return);