]> git.donarmstrong.com Git - lilypond.git/blob - lily/relocate.cc
Merge with master
[lilypond.git] / lily / relocate.cc
1 /*
2   relocate.cc -- implement relocation based on argv0
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "relocate.hh"
11
12 #include "config.hh"
13
14
15 /* TODO: autoconf support */
16
17 #include <sys/types.h>
18 #include <dirent.h>
19
20 #if HAVE_GETTEXT
21 #include <libintl.h>
22 #endif
23
24 #include "file-name.hh"
25 #include "file-path.hh"
26 #include "international.hh"
27 #include "lily-guile.hh"
28 #include "lily-version.hh"
29 #include "main.hh"
30 #include "version.hh"
31 #include "warn.hh"
32
33 #define FRAMEWORKDIR ".."
34
35 int
36 sane_putenv (char const *key, string value, bool overwrite)
37 {
38   if (overwrite || !getenv (key))
39     {
40       string combine = string (key) + "=" + value;
41       char *s = strdup (combine.c_str ());
42
43       if (be_verbose_global)
44         progress_indication (_f ("Setting %s to %s" , key, value.c_str ())
45                              + "\n");
46                              
47       int retval =  putenv (s);
48       /*
49         unfortunately, we can't portably free S here,
50         due to various bugs in glibc prior to 2.1.1
51        */ 
52       return retval;
53     }
54   
55   return -1;
56 }
57
58 static int
59 set_env_file (char const *key, string value, bool overwrite = false)
60 {
61   if (is_file (value))
62     return sane_putenv (key, value, overwrite);
63   else if (be_verbose_global)
64     warning (_f ("no such file: %s for %s", value, key));
65   return -1;
66 }
67
68 static int
69 set_env_dir (char const *key, string value)
70 {
71   if (is_dir (value))
72     return sane_putenv (key, value, false);
73   else if (be_verbose_global)
74     warning (_f ("no such directory: %s for %s", value, key));
75   return -1;
76 }
77
78 static int
79 prepend_env_path (char const *key, string value)
80 {
81   if (is_dir (value))
82     {
83       if (be_verbose_global)
84         progress_indication (_f ("%s=%s (prepend)\n", key, value.c_str ())); 
85
86       if (char const *cur = getenv (key))
87         value += to_string (PATHSEP) + cur;
88
89       return sane_putenv (key, value.c_str (), true);
90     }
91   else if (be_verbose_global)
92     warning (_f ("no such directory: %s for %s", value, key));
93   return -1;
94 }
95
96 #ifdef __MINGW32__
97 #include <winbase.h>
98 #endif
99
100 void
101 prefix_relocation (string prefix)
102 {
103   if (be_verbose_global)
104     warning (_f ("Relocation: compile prefix=%s, new prefix=%s",
105                  prefix_directory,
106                  prefix.c_str ()));
107   
108   string bindir = prefix + "/bin";
109   string datadir = prefix + "/share";
110   string localedir = datadir + "/locale";
111   string lilypond_datadir = datadir + "/lilypond/";
112
113   if (is_dir (lilypond_datadir + "/" + TOPLEVEL_VERSION))
114     prefix_directory = lilypond_datadir + "/" + TOPLEVEL_VERSION;
115   else if (is_dir (lilypond_datadir + "/current"))
116     prefix_directory = lilypond_datadir + "/current";
117
118 #if HAVE_GETTEXT
119   if (is_dir (localedir))
120     bindtextdomain ("lilypond", localedir.c_str ());
121 #endif
122
123   prepend_env_path ("PATH", bindir);
124 }
125
126 /*
127   UGH : this is a complete mess.
128  */
129
130 void
131 framework_relocation (string prefix)
132 {
133   if (be_verbose_global)
134     warning (_f ("Relocation: framework_prefix=%s", prefix));
135
136   sane_putenv ("INSTALLER_PREFIX", prefix, true);
137                
138   read_relocation_dir (prefix + "/etc/relocate/");
139
140   string bindir = prefix + "/bin";
141   
142   prepend_env_path ("PATH", bindir);
143 }
144
145 /*
146   UGH : this is a complete mess.
147  */
148 void
149 setup_paths (char const *argv0_ptr)
150 {
151   File_name argv0_filename (argv0_ptr);
152   
153   prefix_directory = LILYPOND_DATADIR;
154   if (relocate_binary
155       && getenv ("LILYPOND_RELOCATE_PREFIX"))
156     {
157       prefix_directory = getenv ("LILYPOND_RELOCATE_PREFIX");
158 #ifdef __MINGW32__
159       /* Normalize file name.  */
160       prefix_directory = File_name (prefix_directory).to_string ();
161 #endif /* __MINGW32__ */
162       
163       prefix_relocation (prefix_directory);
164       string bindir = prefix_directory + "/bin";
165       framework_relocation (bindir);
166     }
167   else if (relocate_binary)
168     {
169       string argv0_abs;
170       if (argv0_filename.is_absolute ())
171         {
172           argv0_abs = argv0_filename.to_string ();
173           if (be_verbose_global)
174             warning (_f ("Relocation: is absolute: argv0=%s", argv0_ptr));
175         }
176       else if (argv0_filename.dir_.length ())
177         {
178           argv0_abs = get_working_directory ()
179             + "/" + string (argv0_filename.to_string ());
180           if (be_verbose_global)
181             warning (_f ("Relocation: from cwd: argv0=%s", argv0_ptr));
182         }
183       else
184         {
185           /* Find absolute ARGV0 name, using PATH.  */
186           File_path path;
187           path.parse_path (getenv ("PATH"));
188
189           if (be_verbose_global)
190             warning (_f ("Relocation: from PATH=%s\nargv0=%s",
191                          path.to_string ().c_str (), argv0_ptr));
192
193 #ifndef __MINGW32__
194           argv0_abs = path.find (argv0_filename.to_string ());
195 #else /* __MINGW32__ */
196           char const *ext[] = {"exe", "", 0 };
197           argv0_abs = path.find (argv0_filename.to_string (), ext);
198 #endif /* __MINGW32__ */
199
200           if (argv0_abs.empty ())
201             programming_error ("cannot find absolute argv0");
202         }
203
204       string bindir = dir_name (argv0_abs);
205       string argv0_prefix = dir_name (bindir);
206       string compile_prefix = dir_name (dir_name (dir_name (prefix_directory)));
207       if (argv0_prefix != compile_prefix)
208         {
209           prefix_relocation (argv0_prefix);
210           prefix_directory = argv0_prefix;
211         }
212       if (argv0_prefix != compile_prefix || string (FRAMEWORKDIR) != "..")
213         {
214           framework_relocation (bindir + "/" + FRAMEWORKDIR);
215           prefix_directory = bindir + "/" + FRAMEWORKDIR;
216         }
217     }
218
219   /* FIXME: use LILYPOND_DATADIR.  */
220   if (char const *env = getenv ("LILYPONDPREFIX"))
221     {
222 #ifdef __MINGW32__
223       /* Normalize file name.  */
224       prefix_directory = File_name (env).to_string ();
225 #else
226       prefix_directory = env;
227 #endif
228     }
229
230   global_path.append ("");
231
232
233   /*
234     When running from build dir, a full LILYPOND_PREFIX is set-up at
235
236         $(OUTBASE)/{share,lib}/lilypond/current
237
238   */
239   
240   string build_prefix_current = prefix_directory + "/share/lilypond/" "current";
241   string build_prefix_version = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
242   if (is_dir (build_prefix_version.c_str ()))
243     prefix_directory = build_prefix_version;
244   else if (is_dir (build_prefix_current.c_str ()))
245     prefix_directory = build_prefix_current;
246   
247   /* Adding mf/out make lilypond unchanged source directory, when setting
248      LILYPONDPREFIX to lilypond-x.y.z */
249   char const *suffixes[] = {"ly", "ps", "scm", 0 };
250   
251   vector<string> dirs;
252   for (char const **s = suffixes; *s; s++)
253     {
254       string path = prefix_directory + to_string ('/') + string (*s);
255       dirs.push_back (path);
256     }
257   
258   dirs.push_back (prefix_directory + "/fonts/otf/");
259   dirs.push_back (prefix_directory + "/fonts/type1/");
260   dirs.push_back (prefix_directory + "/fonts/svg/");
261   
262   for (vsize i = 0; i < dirs.size (); i++)
263     global_path.prepend (dirs[i]);
264 }
265
266
267
268 string
269 expand_environment_variables (string orig)
270 {
271   const char *start_ptr = orig.c_str();
272   const char *ptr = orig.c_str();
273   size_t len = orig.length();
274
275   string out;
276   while (ptr < start_ptr + len)
277     {
278       char *dollar = strchr (ptr, '$');
279       
280       if (dollar != NULL)
281         {
282           char *start_var = dollar + 1;
283           char *end_var = start_var;
284           char *start_next = end_var;
285           
286           out += string (ptr, dollar - ptr);
287           ptr = dollar;
288
289           if (*start_var == '{')
290             {
291               start_var ++;
292               
293               end_var = strchr (start_var, '}');
294               
295               if (end_var == NULL)
296                 {
297                   end_var = start_var + len;
298                   start_next = end_var;
299                 }
300               else
301                 {
302                   start_next = end_var + 1; 
303                 }
304             }
305           else 
306             {
307               /*
308                 Hmm. what to do for $1 , $~ etc.?
309                */
310               do
311                 {
312                   end_var ++;
313                 }
314               while (isalnum (*end_var) || *end_var == '_');
315               start_next = end_var;
316             }
317
318           if (start_var < end_var)
319             {
320               string var_name (start_var, end_var - start_var);
321               const char *value = getenv (var_name.c_str());
322               if (value != NULL)
323                 out += string (value);
324
325               ptr = start_next;
326             }
327         }
328       else
329         break;
330
331     }
332
333   out += ptr;
334
335   return out;
336 }
337
338
339 string
340 read_line (FILE *f)
341 {
342   string out;
343   
344   int c = 0;
345   while ((c = fgetc (f)) != EOF && c != '\n')
346     out += c;
347
348   return out;
349 }
350
351 void
352 read_relocation_file (string filename)
353 {
354   if (be_verbose_global)
355     progress_indication (_f ("Relocation file: %s", filename.c_str ())
356                          + "\n");
357       
358   char const *cname = filename.c_str ();
359   FILE *f = fopen (cname, "r");
360   if (!f)
361     error (_f ("cannot open file: `%s'", cname));
362
363   while (!feof (f))
364     {
365       string line = read_line (f);
366       size_t idx = line.find (' ');
367       if (idx == NPOS)
368         continue;
369       
370       string command = line.substr (0, idx);
371       line = line.substr (idx + 1);
372       
373       if (idx == NPOS)
374         continue;
375       idx = line.find ('=');
376
377       string variable = line.substr (0, idx);
378       string value = line.substr (idx + 1);
379
380       value = expand_environment_variables (value);
381
382       if (command == "set")
383         sane_putenv (variable.c_str(), value, true);
384       else if (command == "setdir")
385         set_env_dir (variable.c_str(), value);
386       else if (command == "setfile")
387         set_env_file (variable.c_str(), value);
388       else if (command == "prependdir")
389         prepend_env_path (variable.c_str (), value);
390       else
391         error (_f ("Unknown relocation command %s", command));
392     }
393
394   fclose (f);
395 }
396
397 void
398 read_relocation_dir (string dirname)
399 {
400   if (DIR *dir = opendir (dirname.c_str ()))
401     while (struct dirent *ent = readdir (dir))
402       {
403         File_name name (ent->d_name);
404         if (name.ext_ == "reloc")
405             read_relocation_file (dirname + "/" + name.to_string ());
406       }
407 }