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