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