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