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