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