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