first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / 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/filename.h>
23 #include <mathematics/chaos.h>
24 #include <structures/static_memory_gremlin.h>
25 #include <textual/parser_bits.h>
26
27 #ifdef __APPLE__
28   #include <mach-o/dyld.h>
29   #include <limits.h>
30 #endif
31 #ifdef __WIN32__
32   #include <direct.h>
33   #include <process.h>
34 #else
35   #include <dirent.h>
36 #endif
37 #ifdef __UNIX__
38   #include <sys/utsname.h>
39   #include <unistd.h>
40 #endif
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/stat.h>
45
46 using namespace basis;
47 using namespace filesystem;
48 using namespace mathematics;
49 using namespace structures;
50 using namespace textual;
51
52 #undef LOG
53 #define LOG(to_print) printf("%s\n", astring(to_print).s())
54
55 namespace configuration {
56
57 const int MAXIMUM_COMMAND_LINE = 32 * KILOBYTE;
58   // maximum command line that we'll deal with here.
59
60 #ifdef __UNIX__
61 astring application_configuration::get_cmdline_from_proc()
62 {
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;
67
68 #ifdef __APPLE__
69   __check_once_app_path = query_for_process_info();
70   return __check_once_app_path;
71 #endif
72
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");
76   if (!cmds_file) {
77     LOG("failed to open our process's command line file.\n");
78     return "unknown";
79   }
80 //hmmm: this would be a lot nicer using a byte filer.
81   size_t size = 2000;
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");
88     return "unknown";
89   }
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;
93   delete [] 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);
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 //hmmm: the below might be better off as a find app in path method, which relies on which.
104
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");
110   if (!which_file) {
111     LOG("failed to open the temporary output from which.\n");
112     return "unknown";
113   }
114   // reallocate the file buffer.
115   size = 2000;
116   filebuff = new char[size + 1];
117   chars_read = getline((char **)&filebuff, &size, which_file);
118   fclose(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");
122     return "unknown";
123   } else {
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());
128     }
129   }
130   delete [] filebuff;
131   return __check_once_app_path;  // return whatever which told us.
132 }
133
134 // deprecated; better to use the /proc/pid/cmdline file.
135 astring application_configuration::query_for_process_info()
136 {
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.
141   chaos rando;
142   a_sprintf tmpfile("/tmp/proc_name_check_%d_%d.txt", process_id(),
143       rando.inclusive(0, 128000));
144 #ifdef __APPLE__
145   a_sprintf cmd("ps -o args=\"\" %d >%s", process_id(),
146       tmpfile.s());
147 #else
148   a_sprintf cmd("ps h -O \"args\" %d >%s", process_id(),
149       tmpfile.s());
150 #endif
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");
155     return to_return;
156   }
157   // open the output file for reading.
158   FILE *output = fopen(tmpfile.s(), "r");
159   if (!output) {
160     LOG("failed to open the ps output file");
161     return to_return;
162   }
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);
166   if (size_read > 0) {
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') )
172         size_read--;
173       else break;
174     }
175     to_return.reset(astring::UNTERMINATED, buff, size_read);
176   } else {
177     // couldn't read anything.
178     LOG("could not read output of process list");
179   }
180   unlink(tmpfile.s());
181   return to_return;
182 }
183 #endif
184
185 // used as a return value when the name cannot be determined.
186 #define SET_BOGUS_NAME(error) { \
187   LOG(error); \
188   if (output) { \
189     fclose(output); \
190     unlink(tmpfile.s()); \
191   } \
192   astring home_dir = env_string("HOME"); \
193   to_return = home_dir + "/failed_to_determine.exe"; \
194 }
195
196 astring application_configuration::application_name()
197 {
198 //  FUNCDEF("application_name");
199   astring to_return;
200 #ifdef __APPLE__
201   char buffer[MAX_ABS_PATH] = { '\0' };
202   uint32_t buffsize = MAX_ABS_PATH - 1;
203   _NSGetExecutablePath(buffer, &buffsize);
204   to_return = (char *)buffer;
205 #elif __UNIX__
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.
212   to_return = buff;
213 #else
214   #pragma error("hmmm: no means of finding app name is implemented.")
215   SET_BOGUS_NAME("not_implemented_for_this_OS");
216 #endif
217   return to_return;
218 }
219
220 #if defined(__UNIX__) || defined(__WIN32__)
221   basis::un_int application_configuration::process_id() { return getpid(); }
222 #else
223   #pragma error("hmmm: need process id implementation for this OS!")
224   basis::un_int application_configuration::process_id() { return 0; }
225 #endif
226
227 astring application_configuration::current_directory()
228 {
229   astring to_return;
230 #ifdef __UNIX__
231   char buff[MAX_ABS_PATH];
232   getcwd(buff, MAX_ABS_PATH - 1);
233   to_return = buff;
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);
238 #else
239   #pragma error("hmmm: need support for current directory on this OS.")
240   to_return = ".";
241 #endif
242   return to_return;
243 }
244
245 // implement the software product function.
246 const char *application_configuration::software_product_name()
247 {
248 #ifdef GLOBAL_PRODUCT_NAME
249   return GLOBAL_PRODUCT_NAME;
250 #else
251   return "hoople"; 
252 #endif
253 }
254
255 astring application_configuration::application_directory()
256 { return filename(application_name()).dirname().raw(); }
257
258 structures::version application_configuration::get_OS_version()
259 {
260   version to_return;
261 #ifdef __UNIX__
262   utsname kernel_parms;
263   uname(&kernel_parms);
264   to_return = version(kernel_parms.release);
265 #elif defined(__WIN32__)
266   OSVERSIONINFO info;
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)));
272 #else
273   #pragma error("hmmm: need version info for this OS!")
274 #endif
275   return to_return;
276 }
277
278 //////////////
279
280 const char *PATH_CONFIGURATION_FILENAME() { return "paths.ini"; }
281
282 astring application_configuration::application_configuration_file()
283 {
284   filename cfg_file(application_directory() + "/" + PATH_CONFIGURATION_FILENAME());
285   return cfg_file.raw();
286 }
287
288 const astring &application_configuration::GLOBAL_SECTION_NAME() { STATIC_STRING("Common"); }
289
290 const astring &application_configuration::LOGGING_FOLDER_NAME() { STATIC_STRING("LogPath"); }
291
292 //////////////
293
294 const int MAX_LOG_PATH = 200;
295   // the maximum length of the entry stored for the log path.
296
297 /*
298 astring application_configuration::installation_root()
299 {
300   astring to_return = read_item(LOCAL_FOLDER_NAME());
301   if (!to_return) {
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();
305   }
306   return to_return;
307 }
308 */
309
310 astring application_configuration::get_logging_directory()
311 {
312   // start with the root of our installation.
313   astring def_log = application_directory();
314 ///installation_root();
315   // add logs directory underneath that.
316   def_log += "/logs";
317     // add the subdirectory for logs.
318
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.
322   if (!log_dir) {
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);
328   } else {
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);
332   }
333
334   // now we make sure the directory exists.
335   struct stat to_fill;
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
339     // to create it.
340 //if it's something besides a directory... should it be deleted?
341 #ifdef __UNIX__
342     int mk_ret = mkdir(log_dir.s(), 0777);
343 #endif
344 #ifdef __WIN32__
345     int mk_ret = mkdir(log_dir.s());
346 #endif
347     if (mk_ret) return "";
348 //can't have a log file if we can't make the directory successfully???
349   }
350
351   return log_dir;
352 }
353
354 astring application_configuration::make_logfile_name(const astring &base_name)
355 { return get_logging_directory() + "/" + base_name; }
356
357 ///astring application_configuration::core_bin_directory() { return read_item("core_bin"); }
358
359 astring application_configuration::read_item(const astring &key_name)
360 {
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, "");
365   if (!!to_return) {
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);
369   }
370   return to_return;
371 }
372
373 } // namespace.
374