]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
* lily/pango-select.cc (select_pango_font): use ::find_pango_font,
[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 "getopt-long.hh"
29 #include "global-ctor.hh"
30 #include "kpath.hh"
31 #include "lily-guile.hh"
32 #include "lily-version.hh"
33 #include "version.hh"
34 #include "misc.hh"
35 #include "output-def.hh"
36 #include "warn.hh"
37 #include "freetype.hh"
38 #include "string-convert.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 bool is_pango_format_global;
56
57 /* Current output name. */
58 String output_name_global;
59
60 /* Run in safe mode? */
61 bool be_safe_global = false;
62
63 /* Verbose progress indication? */
64 bool be_verbose_global = false;
65
66 /* Scheme code to execute before parsing, after .scm init
67    This is where -e arguments are appended to.
68 */
69 String init_scheme_code_string = "(begin #t ";
70
71 bool make_preview = false;
72 bool make_pages = true;
73
74
75 /*
76  * Miscellaneous global stuff.
77  */
78 File_path global_path;
79
80
81 /*
82  * File globals.
83  */
84
85 static char const *AUTHORS =
86 "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
87 "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
88
89 static char const *PROGRAM_NAME = "lilypond";
90 static char const *PROGRAM_URL = "http://lilypond.org";
91
92 static char const *NOTICE =
93 _f ("This program is free software.  It is covered by the GNU General Public\n"
94     "License and you are welcome to change it and/or distribute copies of it\n"
95     "under certain conditions.  Invoke as `%s --warranty' for more\n"
96     "information.\n", "lilypond").to_str0 ();
97   
98 static char const *WARRANTY =
99 _i ("    This program is free software; you can redistribute it and/or\n"
100     "modify it under the terms of the GNU General Public License version 2\n"
101     "as published by the Free Software Foundation.\n"
102     "\n"
103     "    This program is distributed in the hope that it will be useful,\n"
104     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
105     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
106     "General Public License for more details.\n"
107     "\n"
108     "    You should have received a copy (refer to the file COPYING) of the\n"
109     "GNU General Public License along with this program; if not, write to\n"
110     "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
111     "Boston, MA 02111-1307, USA.\n");
112
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, "Directory prefix: \"%s\"\n", prefix_directory.to_str0());
162 }
163
164 static void
165 copyright ()
166 {
167   printf (_f ("Copyright (c) %s by\n%s  and others.",
168               "1996--2005",
169               AUTHORS).to_str0 ());
170   printf ("\n");
171 }
172
173 static void
174 identify (FILE *out)
175 {
176   fputs (gnu_lilypond_version_string ().to_str0 (), out);
177   fputs ("\n", out);
178 }
179  
180 static void
181 notice ()
182 {
183   identify (stdout);
184   printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
185   printf ("\n");
186   copyright ();
187 }
188
189 static void
190 usage ()
191 {
192   /* No version number or newline here.  It confuses help2man.  */
193   printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
194   printf ("\n\n");
195   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
196   printf ("\n\n");
197   printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
198   printf ("\n");
199   printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
200   printf ("\n\n");
201   printf (_ ("Options:").to_str0 ());
202   printf ("\n");
203   printf (Long_option_init::table_string (options_static).to_str0 ());
204   printf ("\n");
205   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
206   printf ("\n");
207   printf ("\n");
208 }
209
210 static void
211 warranty ()
212 {
213   identify (stdout);
214   printf ("\n");
215   copyright ();
216   printf ("\n");
217   printf (_ (WARRANTY).to_str0 ());
218 }
219
220 static void
221 setup_paths ()
222 {
223   prefix_directory = DATADIR "/lilypond/" MAJOR_VERSION "." MINOR_VERSION;
224   if (char const * env = getenv ("LILYPONDPREFIX"))
225     prefix_directory = env;
226
227   global_path.append ("");
228
229   /* Adding mf/out make lilypond unchanged source directory, when setting
230      LILYPONDPREFIX to lilypond-x.y.z */
231   char *suffixes[] = {"ly", "cff", "otf", "mf/out", "scm", "tfm", "ps", "svg",
232                       0};
233
234   for (char **s = suffixes; *s; s++)
235     {
236       String path = prefix_directory + to_string ('/') + String (*s);
237       global_path.prepend (path);
238         
239 #if !KPATHSEA
240         /* Urg: GNU make's $ (word) index starts at 1 */
241         int i  = 1;
242         while (global_path.try_append (path + to_string (".") + to_string (i)))
243           i++;
244 #endif
245     }
246 }
247   
248 static void
249 prepend_load_path (String dir)
250 {
251   String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
252   scm_c_eval_string (s.to_str0 ());
253 }
254
255 void init_global_tweak_registry ();
256 void init_fontconfig ();
257
258 static void
259 do_chroot_jail ()
260 {
261   /* Now we chroot, setuid/setgrp and chdir.  If something goes wrong,
262      we exit (this is a security-sensitive area).  First we split
263      jail_spec into its components, then we retrieve the user/group id
264      (necessarily *before* chroot'ing) and finally we perform the
265      actual actions.  */
266
267   enum Jail
268     {
269       USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
270     };
271   
272   Array<String> components = String_convert::split (jail_spec, ',');
273   if (components.size () != JAIL_MAX)
274     {
275       error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
276                  components.size ()));
277       exit (2);
278     }
279
280   /* Hmm.  */
281   errno = 0;
282
283   int uid;
284   if (passwd *passwd = getpwnam (components[USER_NAME].to_str0 ()))
285     uid = passwd->pw_uid;
286   else
287     {
288       if (errno == 0)
289         error (_f ("no such user: %s", components[USER_NAME]));
290       else 
291         error(_f ("can't get user id from user name: %s: %s",
292                   components[USER_NAME],
293                   strerror (errno)));
294       exit (3);
295     }
296
297   /* Hmm.  */
298   errno = 0;
299
300   int gid;
301   if (group *group = getgrnam (components[GROUP_NAME].to_str0 ()))
302     gid = group->gr_gid;
303   else
304     {
305       if (errno == 0) 
306         error (_f ("no such group: %s", components[GROUP_NAME]));
307       else 
308         error (_f ("can't get group id from group name: %s: ",
309                    components[GROUP_NAME],
310                    strerror (errno)));
311       exit (3);
312     }
313
314   if (chroot (components[JAIL].to_str0 ()))
315     {
316       error (_f ("can't chroot to: %s: %s", components[JAIL],
317                  strerror (errno)));
318       exit (3);
319     }
320
321   if (setgid (gid))
322     {
323       error (_f ("can't change group id to: %d: %s", gid, strerror (errno)));
324       exit (3);
325     }
326
327   if (setuid (uid))
328     {
329       error (_f ("can't change user id to: %d: %s", uid, strerror (errno)));
330       exit (3);
331     }
332
333   if (chdir (components[DIR].to_str0 ()))
334     {
335       error (_f ("can't change working directory to: %s: %s", components[DIR],
336                  strerror (errno)));
337       exit (3);
338     }
339 }
340 void test_pango();
341 static void
342 main_with_guile (void *, int, char **)
343 {
344   /* Engravers use lily.scm contents, need to make Guile find it.
345      Prepend onto GUILE %load-path, very ugh. */
346
347   prepend_load_path (prefix_directory);
348   prepend_load_path (prefix_directory + "/scm");
349
350   if (be_verbose_global)
351     dir_info (stderr);
352
353   ly_c_init_guile ();
354   call_constructors ();
355   init_global_tweak_registry ();
356   init_fontconfig ();
357   
358   init_freetype ();
359
360   is_pango_format_global = (output_backend_global != "tex"
361                             && output_backend_global != "texstr");
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
398   /* Unreachable.  */
399   exit (0);
400 }
401
402 static void
403 setup_localisation ()
404 {
405 #if HAVE_GETTEXT
406   /* Enable locales */
407   setlocale (LC_ALL, "");
408   
409   /* FIXME: check if this is still true.
410     Disable localisation of float values.  This breaks TeX output.  */
411   setlocale (LC_NUMERIC, "C");
412   
413   String name (PACKAGE);
414   name.to_lower ();
415   bindtextdomain (name.to_str0 (), LOCALEDIR);
416   textdomain (name.to_str0 ());
417 #endif
418 }
419
420 static void
421 add_output_format (String format)
422 {
423   if (output_format_global != "")
424     output_format_global += ",";
425   output_format_global += format;
426 }
427
428
429 static void
430 parse_argv (int argc, char **argv)
431 {
432   bool help_b = false;
433   option_parser = new Getopt_long (argc, argv, options_static);
434   while (Long_option_init const *opt = (*option_parser) ())
435     {
436       switch (opt->shortname_char_)
437         {
438         case 0:
439           if (String (opt->longname_str0_) == "png"
440               || String (opt->longname_str0_) == "pdf"
441               || String (opt->longname_str0_) == "ps"
442               || String (opt->longname_str0_) == "dvi"
443               || String (opt->longname_str0_) == "tex")
444             {
445               add_output_format (opt->longname_str0_);
446             }
447           else if (String (opt->longname_str0_) == "preview")
448             make_preview = true;
449           else if (String (opt->longname_str0_) == "no-pages")
450             make_pages = false;
451           break;
452           
453         case 'v':
454           notice ();
455           exit (0);
456           break;
457         case 'o':
458           {
459             String s = option_parser->optional_argument_str0_;
460             File_name file_name (s);
461             output_name_global = file_name.to_string ();
462           }
463           break;
464         case 'j':
465           jail_spec = option_parser->optional_argument_str0_;
466           break;
467         case 'e':
468           init_scheme_code_string += option_parser->optional_argument_str0_;
469           break;
470         case 'w':
471           warranty ();
472           exit (0);
473           break;
474           
475         case 'b':
476           output_backend_global = option_parser->optional_argument_str0_;
477           break;
478
479         case 'f':
480           output_format_global = option_parser->optional_argument_str0_;
481           break;
482           
483         case 'H':
484           dump_header_fieldnames_global
485             .push (option_parser->optional_argument_str0_);
486           break;
487         case 'I':
488           global_path.append (option_parser->optional_argument_str0_);
489           break;
490         case 'i':
491           init_name_global = option_parser->optional_argument_str0_;
492           break;
493         case 'h':
494           help_b = true;
495           break;
496         case 'V':
497           be_verbose_global = true;
498           break;
499         case 's':
500           be_safe_global = true;
501           break;
502         case 'p':
503           make_preview = true;
504           break;
505         default:
506           programming_error (to_string ("unhandled short option: %c",
507                                         opt->shortname_char_));
508           assert (false);
509           break;
510         }
511     }
512
513   if (help_b)
514     {
515       identify (stdout);
516       usage ();
517       if (be_verbose_global)
518         dir_info (stdout);
519       exit (0);
520     }
521 }
522
523 int
524 main (int argc, char **argv)
525 {
526   setup_localisation ();
527   setup_paths ();
528   parse_argv (argc, argv);
529   identify (stderr);
530   initialize_kpathsea (argv[0]);
531
532   scm_boot_guile (argc, argv, main_with_guile, 0);
533
534   /* Unreachable */
535   return 0;
536 }