]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
* lily/main.cc (setup_paths): Update.
[lilypond.git] / lily / main.cc
1 /*
2   main.cc -- implement main () entrypoint.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "main.hh"
10
11 #include <cassert>
12 #include <clocale>
13 #include <cstring>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <pwd.h>
17 #include <grp.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20
21 #include "config.hh"
22
23 #if HAVE_GETTEXT
24 #include <libintl.h>
25 #endif
26
27 #include "all-font-metrics.hh"
28 #include "file-name.hh"
29 #include "freetype.hh"
30 #include "getopt-long.hh"
31 #include "global-ctor.hh"
32 #include "lily-guile.hh"
33 #include "lily-version.hh"
34 #include "misc.hh"
35 #include "output-def.hh"
36 #include "string-convert.hh"
37 #include "version.hh"
38 #include "warn.hh"
39
40 /*
41  * Global options that can be overridden through command line.
42  */
43
44 /* Names of header fields to be dumped to a separate file. */
45 Array<String> dump_header_fieldnames_global;
46
47 /* Name of initialisation file. */
48 String init_name_global;
49
50 /* Selected output format.
51    One of tex, ps, scm, as.
52 */
53 String output_backend_global = "ps";
54 String output_format_global = "pdf";
55
56 bool is_pango_format_global;
57 bool is_TeX_format_global;
58
59 /* Current output name. */
60 String output_name_global;
61
62 /* Run in safe mode? */
63 bool be_safe_global = false;
64
65 /* Provide URI links to the original file */
66 bool point_and_click_global = true;
67
68 /* Verbose progress indication? */
69 bool be_verbose_global = false;
70
71 /* Scheme code to execute before parsing, after .scm init
72    This is where -e arguments are appended to.
73 */
74 String init_scheme_code_string = "(begin #t ";
75
76 bool make_preview = false;
77 bool make_pages = true;
78
79 /*
80  * Miscellaneous global stuff.
81  */
82 File_path global_path;
83
84 /*
85  * File globals.
86  */
87
88 static char const *AUTHORS
89 = "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
90   "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
91
92 static char const *PROGRAM_NAME = "lilypond";
93 static char const *PROGRAM_URL = "http://lilypond.org";
94
95 static char const *NOTICE
96 = _i ("This program is free software.  It is covered by the GNU General Public\n"
97       "License and you are welcome to change it and/or distribute copies of it\n"
98       "under certain conditions.  Invoke as `%s --warranty' for more\n"
99       "information.\n");
100
101 static char const *WARRANTY
102 = _i ("    This program is free software; you can redistribute it and/or\n"
103       "modify it under the terms of the GNU General Public License version 2\n"
104       "as published by the Free Software Foundation.\n"
105       "\n"
106       "    This program is distributed in the hope that it will be useful,\n"
107       "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
108       "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
109       "General Public License for more details.\n"
110       "\n"
111       "    You should have received a copy (refer to the file COPYING) of the\n"
112       "GNU General Public License along with this program; if not, write to\n"
113       "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
114       "Boston, MA 02111-1307, USA.\n");
115
116 /* Where the init files live.  Typically:
117    LILYPOND_DATADIR = /usr/share/lilypond
118 */
119 String prefix_directory;
120
121 /* The jail specification: USER,GROUP,JAIL,DIR. */
122 String jail_spec;
123
124 /*  The option parser */
125 static Getopt_long *option_parser = 0;
126
127 /* Internationalisation kludge in two steps:
128  * use _i () to get entry in POT file
129  * call gettext () explicitely for actual "translation"  */
130
131 static Long_option_init options_static[]
132 = {
133   {_i ("EXT"), "backend", 'b', _i ("select backend to use")},
134   {_i ("EXPR"), "evaluate", 'e',
135    _i ("set option, use -e '(ly:option-usage)' for help")},
136   /* Bug in option parser: --output =foe is taken as an abbreviation
137      for --output-format.  */
138   {_i ("EXTs"), "formats", 'f', _i ("list of formats to dump")},
139   {0, "help", 'h',  _i ("print this help")},
140   {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
141   {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
142   {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
143   {_i ("FILE"), "output", 'o',  _i ("write output to FILE (suffix will be added)")},
144   {_i ("USER,GROUP,JAIL,DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP and cd into DIR")},
145   {0, "preview", 'p',  _i ("generate a preview")},
146   {0, "no-pages", 0, _i ("don't generate full pages")},
147   {0, "png", 0, _i ("generate PNG")},
148   {0, "ps", 0, _i ("generate PostScript")},
149   {0, "dvi", 0, _i ("generate DVI")},
150   {0, "pdf", 0, _i ("generate PDF (default)")},
151   {0, "tex", 0, _i ("generate TeX")},
152   {0, "safe-mode", 's',  _i ("run in safe mode")},
153   {0, "version", 'v',  _i ("print version number")},
154   {0, "verbose", 'V', _i ("be verbose")},
155   {0, "warranty", 'w',  _i ("show warranty and copyright")},
156   {0, 0, 0, 0}
157 };
158
159 #define LILYPOND_DATADIR PACKAGE_DATADIR "/" TOPLEVEL_VERSION
160
161 static void
162 dir_info (FILE *out)
163 {
164   fputs ("\n", out);
165   fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
166   if (char const *env = getenv ("LILYPONDPREFIX"))
167     fprintf (out, "LILYPONDPREFIX=\"%s\"\n", env);
168   fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
169
170   fprintf (out, "\nEffective prefix: \"%s\"\n", prefix_directory.to_str0 ());
171 }
172
173 static void
174 copyright ()
175 {
176   printf (_f ("Copyright (c) %s by\n%s  and others.",
177               "1996--2005",
178               AUTHORS).to_str0 ());
179   printf ("\n");
180 }
181
182 static void
183 identify (FILE *out)
184 {
185   fputs (gnu_lilypond_version_string ().to_str0 (), out);
186   fputs ("\n", out);
187 }
188
189 static void
190 notice ()
191 {
192   identify (stdout);
193   puts (_f (NOTICE, PROGRAM_NAME).to_str0 ());
194   printf ("\n");
195   copyright ();
196 }
197
198 static void
199 usage ()
200 {
201   /* No version number or newline here.  It confuses help2man.  */
202   printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
203   printf ("\n\n");
204   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
205   printf ("\n\n");
206   printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
207   printf ("\n");
208   printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
209   printf ("\n\n");
210   printf (_ ("Options:").to_str0 ());
211   printf ("\n");
212   printf (Long_option_init::table_string (options_static).to_str0 ());
213   printf ("\n");
214   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
215   printf ("\n");
216   printf ("\n");
217 }
218
219 static void
220 warranty ()
221 {
222   identify (stdout);
223   printf ("\n");
224   copyright ();
225   printf ("\n");
226   printf (_ (WARRANTY).to_str0 ());
227 }
228
229 static void
230 setup_paths ()
231 {
232   prefix_directory = LILYPOND_DATADIR;
233   if (char const *env = getenv ("LILYPONDPREFIX"))
234     prefix_directory = env;
235
236   global_path.append ("");
237
238   /* Adding mf/out make lilypond unchanged source directory, when setting
239      LILYPONDPREFIX to lilypond-x.y.z */
240   char *suffixes[] = {"ly", "ps", "scm", 0 };
241
242   Array<String> dirs;
243   for (char **s = suffixes; *s; s++)
244     {
245       String path = prefix_directory + to_string ('/') + String (*s);
246       dirs.push (path);
247     }
248
249   /*
250     ugh. C&P font-config.cc
251   */
252   struct stat statbuf; 
253   String builddir = prefix_directory + "/mf/out/";
254   if (stat (builddir.to_str0 (), &statbuf) == 0)
255     {
256       dirs.push (builddir.to_str0 ());
257     }
258   else
259     {
260       dirs.push (prefix_directory + "/fonts/otf/");
261       dirs.push (prefix_directory + "/fonts/type1/");
262       dirs.push (prefix_directory + "/fonts/cff/");
263       dirs.push (prefix_directory + "/fonts/svg/");
264       dirs.push (prefix_directory + "/fonts/cff/");
265     }
266
267   for (int i = 0; i < dirs.size (); i++)
268     global_path.prepend (dirs[i]);  
269 }
270   
271 static void
272 prepend_load_path (String dir)
273 {
274   String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
275   scm_c_eval_string (s.to_str0 ());
276 }
277
278 void init_global_tweak_registry ();
279 void init_fontconfig ();
280
281 static void
282 do_chroot_jail ()
283 {
284   /* Now we chroot, setuid/setgrp and chdir.  If something goes wrong,
285      we exit (this is a security-sensitive area).  First we split
286      jail_spec into its components, then we retrieve the user/group id
287      (necessarily *before* chroot'ing) and finally we perform the
288      actual actions.  */
289
290   enum Jail
291     {
292       USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
293     };
294
295   Array<String> components = String_convert::split (jail_spec, ',');
296   if (components.size () != JAIL_MAX)
297     {
298       error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
299                  components.size ()));
300       exit (2);
301     }
302
303   /* Hmm.  */
304   errno = 0;
305
306   int uid;
307   if (passwd * passwd = getpwnam (components[USER_NAME].to_str0 ()))
308     uid = passwd->pw_uid;
309   else
310     {
311       if (errno == 0)
312         error (_f ("no such user: %s", components[USER_NAME]));
313       else
314         error (_f ("can't get user id from user name: %s: %s",
315                    components[USER_NAME],
316                    strerror (errno)));
317       exit (3);
318     }
319
320   /* Hmm.  */
321   errno = 0;
322
323   int gid;
324   if (group * group = getgrnam (components[GROUP_NAME].to_str0 ()))
325     gid = group->gr_gid;
326   else
327     {
328       if (errno == 0)
329         error (_f ("no such group: %s", components[GROUP_NAME]));
330       else
331         error (_f ("can't get group id from group name: %s: ",
332                    components[GROUP_NAME],
333                    strerror (errno)));
334       exit (3);
335     }
336
337   if (chroot (components[JAIL].to_str0 ()))
338     {
339       error (_f ("can't chroot to: %s: %s", components[JAIL],
340                  strerror (errno)));
341       exit (3);
342     }
343
344   if (setgid (gid))
345     {
346       error (_f ("can't change group id to: %d: %s", gid, strerror (errno)));
347       exit (3);
348     }
349
350   if (setuid (uid))
351     {
352       error (_f ("can't change user id to: %d: %s", uid, strerror (errno)));
353       exit (3);
354     }
355
356   if (chdir (components[DIR].to_str0 ()))
357     {
358       error (_f ("can't change working directory to: %s: %s", components[DIR],
359                  strerror (errno)));
360       exit (3);
361     }
362 }
363
364 void test_pango ();
365
366 static void
367 main_with_guile (void *, int, char **)
368 {
369   /* Engravers use lily.scm contents, need to make Guile find it.
370      Prepend onto GUILE %load-path, very ugh. */
371
372   prepend_load_path (prefix_directory);
373   prepend_load_path (prefix_directory + "/scm");
374
375   if (be_verbose_global)
376     dir_info (stderr);
377   is_TeX_format_global = (output_backend_global == "tex"
378                           || output_backend_global == "texstr");
379
380   is_pango_format_global = !is_TeX_format_global;
381
382   ly_c_init_guile ();
383   call_constructors ();
384   init_global_tweak_registry ();
385   init_fontconfig ();
386
387   init_freetype ();
388
389   all_fonts_global = new All_font_metrics (global_path.to_string ());
390
391   init_scheme_code_string += ")";
392   scm_c_eval_string ((char *) init_scheme_code_string.to_str0 ());
393
394   /* We accept multiple independent music files on the command line to
395      reduce compile time when processing lots of small files.
396      Starting the GUILE engine is very time consuming.  */
397
398   SCM files = SCM_EOL;
399   SCM *tail = &files;
400   while (char const *arg = option_parser->get_next_arg ())
401     {
402       *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
403       tail = SCM_CDRLOC (*tail);
404     }
405   delete option_parser;
406   option_parser = 0;
407
408   if (files == SCM_EOL)
409     {
410       /* No FILE arguments is now a usage error to help newbies.  If you
411          want a filter, you're not a newbie and should know to use file
412          argument `-'.  */
413       usage ();
414       exit (2);
415     }
416
417   if (! jail_spec.is_empty ())
418     do_chroot_jail ();
419
420   SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
421   (void) result;
422
423   /* Unreachable.  */
424   exit (0);
425 }
426
427 static void
428 setup_localisation ()
429 {
430 #if HAVE_GETTEXT
431   /* Enable locales */
432   setlocale (LC_ALL, "");
433
434   /* FIXME: check if this is still true.
435      Disable localisation of float values.  This breaks TeX output.  */
436   setlocale (LC_NUMERIC, "C");
437
438   String name (PACKAGE);
439   name.to_lower ();
440   bindtextdomain (name.to_str0 (), LOCALEDIR);
441   textdomain (name.to_str0 ());
442 #endif
443 }
444
445 static void
446 add_output_format (String format)
447 {
448   if (output_format_global != "")
449     output_format_global += ",";
450   output_format_global += format;
451 }
452
453 static void
454 parse_argv (int argc, char **argv)
455 {
456   bool show_help = false;
457   option_parser = new Getopt_long (argc, argv, options_static);
458   while (Long_option_init const *opt = (*option_parser) ())
459     {
460       switch (opt->shortname_char_)
461         {
462         case 0:
463           if (String (opt->longname_str0_) == "png"
464               || String (opt->longname_str0_) == "pdf"
465               || String (opt->longname_str0_) == "ps"
466               || String (opt->longname_str0_) == "dvi"
467               || String (opt->longname_str0_) == "tex")
468             {
469               add_output_format (opt->longname_str0_);
470             }
471           else if (String (opt->longname_str0_) == "preview")
472             make_preview = true;
473           else if (String (opt->longname_str0_) == "no-pages")
474             make_pages = false;
475           break;
476
477         case 'v':
478           notice ();
479           exit (0);
480           break;
481         case 'o':
482           {
483             String s = option_parser->optional_argument_str0_;
484             File_name file_name (s);
485             output_name_global = file_name.to_string ();
486           }
487           break;
488         case 'j':
489           jail_spec = option_parser->optional_argument_str0_;
490           break;
491         case 'e':
492           init_scheme_code_string += option_parser->optional_argument_str0_;
493           break;
494         case 'w':
495           warranty ();
496           exit (0);
497           break;
498           
499         case 'b':
500           output_backend_global = option_parser->optional_argument_str0_;
501           break;
502
503         case 'f':
504           output_format_global = option_parser->optional_argument_str0_;
505           break;
506           
507         case 'H':
508           dump_header_fieldnames_global
509             .push (option_parser->optional_argument_str0_);
510           break;
511         case 'I':
512           global_path.append (option_parser->optional_argument_str0_);
513           break;
514         case 'i':
515           init_name_global = option_parser->optional_argument_str0_;
516           break;
517         case 'h':
518           show_help = true;
519           break;
520         case 'V':
521           be_verbose_global = true;
522           break;
523         case 's':
524           be_safe_global = true;
525           break;
526         case 'p':
527           make_preview = true;
528           break;
529         default:
530           programming_error (to_string ("unhandled short option: %c",
531                                         opt->shortname_char_));
532           assert (false);
533           break;
534         }
535     }
536
537   if (show_help)
538     {
539       identify (stdout);
540       usage ();
541       if (be_verbose_global)
542         dir_info (stdout);
543       exit (0);
544     }
545 }
546
547 int
548 main (int argc, char **argv)
549 {
550   setup_localisation ();
551   setup_paths ();
552   parse_argv (argc, argv);
553   identify (stderr);
554
555   scm_boot_guile (argc, argv, main_with_guile, 0);
556
557   /* Unreachable */
558   return 0;
559 }