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