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