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