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