updates lurching towards functionality
[feisty_meow.git] / nucleus / library / configuration / application_configuration.cpp
1 /*
2 *  Name   : application_configuration
3 *  Author : Chris Koeritz
4
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
11 */
12
13 #include "application_configuration.h"
14 #include "ini_configurator.h"
15
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
28 #ifdef __APPLE__
29   #include <mach-o/dyld.h>
30   #include <limits.h>
31 #endif
32 #ifdef _MSC_VER
33   #include <direct.h>
34   #include <process.h>
35 #else
36   #include <dirent.h>
37   #include <sys/utsname.h>
38   #include <unistd.h>
39 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/stat.h>
44
45 using namespace basis;
46 using namespace filesystem;
47 using namespace mathematics;
48 using namespace structures;
49 using namespace textual;
50
51 #undef LOG
52 #define LOG(to_print) printf("%s\n", astring(to_print).s())
53
54 namespace configuration {
55
56 const int MAXIMUM_COMMAND_LINE = 32 * KILOBYTE;
57   // maximum command line that we'll deal with here.
58
59 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
60 astring application_configuration::get_cmdline_from_proc()
61 {
62   FUNCDEF("get_cmdline_from_proc");
63   static astring __check_once_app_path;
64 //hmmm: we want to use a single per app static synch here!
65   if (__check_once_app_path.length()) return __check_once_app_path;
66
67 #ifdef __APPLE__
68   __check_once_app_path = query_for_process_info();
69   return __check_once_app_path;
70 #endif
71
72   // we have not looked this app's name up in the path yet.
73   a_sprintf cmds_filename("/proc/%d/cmdline", process_id());
74   FILE *cmds_file = fopen(cmds_filename.s(), "r");
75   if (!cmds_file) {
76     LOG("failed to open our process's command line file.\n");
77     return "unknown";
78   }
79 //hmmm: this would be a lot nicer using a byte filer.
80   size_t size = 2000;
81   char *filebuff = new char[size + 1];
82   ssize_t chars_read = getline((char **)&filebuff, &size, cmds_file);
83     // read the first line, giving ample space for how long it might be.
84   fclose(cmds_file);  // drop the file again.
85   if (!chars_read || negative(chars_read)) {
86     LOG("failed to get any characters from our process's cmdline file.\n");
87     return "unknown";
88   }
89   // copy the buffer into a string, which works great since the entries in the
90   // command line are all separated by zero characters.
91   __check_once_app_path = filebuff;
92   delete [] filebuff;
93 //printf("got an app name before chewing: %s\n", __check_once_app_path.s());
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);
97   }
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.
102
103 //printf("no dir part found, app name after chewing: %s\n", __check_once_app_path.s());
104
105 //hmmm: the below might be better off as a find app in path method, which relies on which.
106   // there was no directory component, so we'll try to guess one.
107   astring temp_filename(environment::TMP()
108       + a_sprintf("/zz_cmdfind.%d", chaos().inclusive(0, 999999999)));
109   system((astring("which ") + __check_once_app_path + " >" + temp_filename).s());
110   FILE *which_file = fopen(temp_filename.s(), "r");
111   if (!which_file) {
112     LOG("failed to open the temporary output from which.\n");
113     return "unknown";
114   }
115   // reallocate the file buffer.
116   size = 2000;
117   filebuff = new char[size + 1];
118   chars_read = getline((char **)&filebuff, &size, which_file);
119   fclose(which_file);
120   unlink(temp_filename.s());
121   if (!chars_read || negative(chars_read)) {
122     LOG("failed to get any characters from the which cmd output.\n");
123     return "unknown";
124   } else {
125     // we had some luck using 'which' to locate the file, so we'll use this version.
126     __check_once_app_path = filebuff;
127     while (parser_bits::is_eol(__check_once_app_path[__check_once_app_path.end()])) {
128       __check_once_app_path.zap(__check_once_app_path.end(), __check_once_app_path.end());
129     }
130   }
131   delete [] filebuff;
132   return __check_once_app_path;  // return whatever which told us.
133 }
134
135 // deprecated; better to use the /proc/pid/cmdline file.
136 astring application_configuration::query_for_process_info()
137 {
138   FUNCDEF("query_for_process_info");
139   astring to_return = "unknown";
140   // we ask the operating system about our process identifier and store
141   // the results in a temporary file.
142   chaos rando;
143   a_sprintf tmpfile("/tmp/proc_name_check_%d_%d.txt", process_id(),
144       rando.inclusive(0, 128000));
145 #ifdef __APPLE__
146   a_sprintf cmd("ps -o args=\"\" %d >%s", process_id(),
147       tmpfile.s());
148 #else
149   a_sprintf cmd("ps h -O \"args\" %d >%s", process_id(),
150       tmpfile.s());
151 #endif
152   // run the command to locate our process info.
153   int sysret = system(cmd.s());
154   if (negative(sysret)) {
155     LOG("failed to run ps command to get process info");
156     return to_return;
157   }
158   // open the output file for reading.
159   FILE *output = fopen(tmpfile.s(), "r");
160   if (!output) {
161     LOG("failed to open the ps output file");
162     return to_return;
163   }
164   // read the file's contents into a string buffer.
165   char buff[MAXIMUM_COMMAND_LINE];
166   size_t size_read = fread(buff, 1, MAXIMUM_COMMAND_LINE, output);
167   if (size_read > 0) {
168     // success at finding some text in the file at least.
169     while (size_read > 0) {
170       const char to_check = buff[size_read - 1];
171       if ( !to_check || (to_check == '\r') || (to_check == '\n')
172           || (to_check == '\t') )
173         size_read--;
174       else break;
175     }
176     to_return.reset(astring::UNTERMINATED, buff, size_read);
177   } else {
178     // couldn't read anything.
179     LOG("could not read output of process list");
180   }
181   unlink(tmpfile.s());
182   return to_return;
183 }
184 #endif
185
186 // used as a return value when the name cannot be determined.
187 #define SET_BOGUS_NAME(error) { \
188   LOG(error); \
189   if (output) { \
190     fclose(output); \
191     unlink(tmpfile.s()); \
192   } \
193   astring home_dir = environment::get("HOME"); \
194   to_return = home_dir + "/failed_to_determine.exe"; \
195 }
196
197 astring application_configuration::application_name()
198 {
199   FUNCDEF("application_name");
200   astring to_return;
201 #ifdef __APPLE__
202   char buffer[MAX_ABS_PATH] = { '\0' };
203   uint32_t buffsize = MAX_ABS_PATH - 1;
204   _NSGetExecutablePath(buffer, &buffsize);
205   to_return = (char *)buffer;
206 #elif defined(__UNIX__) || defined(__GNU_WINDOWS__)
207   to_return = get_cmdline_from_proc();
208 #elif defined(_MSC_VER)
209   flexichar low_buff[MAX_ABS_PATH + 1];
210   GetModuleFileName(NULL_POINTER, low_buff, MAX_ABS_PATH - 1);
211   astring buff = from_unicode_temp(low_buff);
212   buff.to_lower();  // we lower-case the name since windows seems to UC it.
213   to_return = buff;
214 #else
215   #pragma error("hmmm: no means of finding app name is implemented.")
216   SET_BOGUS_NAME("not_implemented_for_this_OS");
217 #endif
218   return to_return;
219 }
220
221 #if defined(__UNIX__) || defined(_MSC_VER) || defined(__GNU_WINDOWS__)
222   basis::un_int application_configuration::process_id() { return getpid(); }
223 #else
224   #pragma error("hmmm: need process id implementation for this OS!")
225   basis::un_int application_configuration::process_id() { return 0; }
226 #endif
227
228 astring application_configuration::current_directory()
229 {
230   astring to_return;
231 #ifdef __UNIX__
232   char buff[MAX_ABS_PATH];
233   getcwd(buff, MAX_ABS_PATH - 1);
234   to_return = buff;
235 #elif defined(_MSC_VER)
236   flexichar low_buff[MAX_ABS_PATH + 1];
237   GetCurrentDirectory(MAX_ABS_PATH, low_buff);
238   to_return = from_unicode_temp(low_buff);
239 #else
240   #pragma error("hmmm: need support for current directory on this OS.")
241   to_return = ".";
242 #endif
243   return to_return;
244 }
245
246 // implement the software product function.
247 const char *application_configuration::software_product_name()
248 {
249 #ifdef GLOBAL_PRODUCT_NAME
250   return GLOBAL_PRODUCT_NAME;
251 #else
252   return "hoople"; 
253 #endif
254 }
255
256 astring application_configuration::application_directory()
257 { return filename(application_name()).dirname().raw(); }
258
259 structures::version application_configuration::get_OS_version()
260 {
261   version to_return;
262 #ifdef __UNIX__
263   utsname kernel_parms;
264   uname(&kernel_parms);
265   to_return = version(kernel_parms.release);
266 #elif defined(_MSC_VER)
267   OSVERSIONINFO info;
268   info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
269   ::GetVersionEx(&info);
270   to_return = version(a_sprintf("%u.%u.%u.%u", basis::un_short(info.dwMajorVersion),
271       basis::un_short(info.dwMinorVersion), basis::un_short(info.dwPlatformId),
272       basis::un_short(info.dwBuildNumber)));
273 #else
274   #pragma error("hmmm: need version info for this OS!")
275 #endif
276   return to_return;
277 }
278
279 //////////////
280
281 const char *PATH_CONFIGURATION_FILENAME() { return "paths.ini"; }
282
283 astring application_configuration::application_configuration_file()
284 {
285   filename cfg_file(application_directory() + "/" + PATH_CONFIGURATION_FILENAME());
286   return cfg_file.raw();
287 }
288
289 const astring &application_configuration::GLOBAL_SECTION_NAME() { STATIC_STRING("Common"); }
290
291 const astring &application_configuration::LOGGING_FOLDER_NAME() { STATIC_STRING("LogPath"); }
292
293 const astring &application_configuration::WINDOZE_VIRTUAL_ROOT_NAME()
294 { STATIC_STRING("VirtualUnixRoot"); }
295
296 const astring &application_configuration::DEFAULT_VIRTUAL_UNIX_ROOT()
297 { STATIC_STRING("c:/cygwin"); }
298
299 //////////////
300
301 // static storage for virtual unix root, if used.
302 SAFE_STATIC(astring, static_root_holder, )
303
304 //  we don't expect it to change during runtime, right?  that would be fubar.
305 astring application_configuration::get_virtual_unix_root()
306 {
307 #ifdef __UNIX__
308   // simple implementation for unix/linux; just tell the truth about the real root.
309   return "/";
310 #endif
311 #ifdef __WIN32__
312   // see if we already cached the root.  it shouldn't change during runtime.
313   if (static_root_holder().length()) {
314     return static_root_holder();
315   }
316
317   /*
318    read the path out of the config file, which should have been set during the
319    build process if this is really windows.
320   */
321   astring virtual_root = read_item(WINDOZE_VIRTUAL_ROOT_NAME());
322   if (!virtual_root) {
323     // if it has no length, we didn't get our setting!  we'll limp along with a guess.
324     // also don't cache the failure value.  maybe it will wake up later!
325     return DEFAULT_VIRTUAL_UNIX_ROOT();
326   } else {
327     static_root_holder() = virtual_root;
328     return static_root_holder();
329   }
330
331 #endif
332 }
333
334 //////////////
335
336 ////const int MAX_LOG_PATH = 512;
337   // the maximum length of the entry stored for the log path.
338
339 astring application_configuration::get_logging_directory()
340 {
341   // new scheme is to just use the temporary directory, which can vary per user
342   // and which hopefully is always set to something usable.
343   astring def_log = environment::TMP();
344   // add logs directory underneath that.
345   def_log += "/logs";
346     // add the subdirectory for logs.
347
348   // now grab the current value for the name, if any.
349   astring log_dir = read_item(LOGGING_FOLDER_NAME());
350     // get the entry for the logging path.
351   if (!log_dir) {
352     // if the entry was absent, we set it.
353 //printf("did not find log dir in config file\n");
354     ini_configurator ini(application_configuration_file(),
355         ini_configurator::RETURN_ONLY,
356         ini_configurator::APPLICATION_DIRECTORY);
357     ini.store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
358   } else {
359     // they gave us something.  let's replace the environment variables
360     // in their string so we resolve paths and such.
361     log_dir = parser_bits::substitute_env_vars(log_dir);
362 //printf("%s", (char *)a_sprintf("got log dir with %s value\n", log_dir.s()).s());
363   }
364
365   // now we make sure the directory exists.
366   filename testing(log_dir);
367   if (!testing.exists()) {
368     bool okay = directory::recursive_create(log_dir);
369     if (!okay) {
370       LOG(astring("failed to create logging directory: ") + log_dir);
371       // return a directory almost guaranteed to exist; best we can do in this case.
372 #ifdef __UNIX__
373       return "/tmp";
374 #endif
375 #ifdef __WIN32__
376       return "c:/";
377 #endif
378     }
379   }
380     
381   return log_dir;
382 }
383
384 astring application_configuration::make_logfile_name(const astring &base_name)
385 { return get_logging_directory() + "/" + base_name; }
386
387 astring application_configuration::read_item(const astring &key_name)
388 {
389   filename ini_name = application_configuration_file();
390   ini_configurator ini(ini_name, ini_configurator::RETURN_ONLY,
391       ini_configurator::APPLICATION_DIRECTORY);
392   astring to_return =  ini.load(GLOBAL_SECTION_NAME(), key_name, "");
393   if (!!to_return) {
394     // if the string has any length, then we process any environment
395     // variables found encoded in the value.
396     to_return = parser_bits::substitute_env_vars(to_return);
397   }
398   return to_return;
399 }
400
401 } // namespace.
402