]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
35d07b844a781cdf4ba010daaec2b2e7f953965e
[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 paper output? */
46 bool no_paper_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 = true;
69 bool make_png = false;
70 bool make_preview = 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-bin";
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-bin --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 options, 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-paper", '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, "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")},
141     {0, "safe-mode", 's',  _i ("run in safe mode")},
142     {0, "version", 'v',  _i ("print version number")},
143     {0, "verbose", 'V', _i ("be verbose")},
144     {0, "warranty", 'w',  _i ("show warranty and copyright")},
145     {0,0,0,0}
146   };
147
148 static void
149 dir_info (FILE *out)
150 {
151   fputs ("\n", out);
152   fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
153   fprintf (out, "LOCAL_LILYPOND_DATADIR=\%s\"\n", LOCAL_LILYPOND_DATADIR);
154   fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
155
156   char *lilypond_prefix = getenv ("LILYPONDPREFIX");
157   fprintf (out, "LILYPONDPREFIX=\"%s\"\n",
158            (lilypond_prefix ? lilypond_prefix : ""));
159 }
160
161 static void
162 copyright ()
163 {
164   printf (_f ("Copyright (c) %s by\n%s  and others.",
165               "1996--2004",
166               AUTHORS).to_str0 ());
167   printf ("\n");
168 }
169
170 static void
171 identify (FILE *out)
172 {
173   fputs (gnu_lilypond_version_string ().to_str0 (), out);
174 }
175  
176 static void
177 notice ()
178 {
179   identify (stdout);
180   printf ("\n");
181   printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
182   printf ("\n");
183   copyright ();
184 }
185
186 static void
187 usage ()
188 {
189   /* No version number or newline here.  It confuses help2man.  */
190   printf (_f ("Usage: %s [OPTIONS]... FILE...", PROGRAM_NAME).to_str0 ());
191   printf ("\n\n");
192   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
193   printf ("\n\n");
194   printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
195   printf ("\n");
196   printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
197   printf ("\n\n");
198   printf (_ ("Options:").to_str0 ());
199   printf ("\n");
200   printf (Long_option_init::table_string (options_static).to_str0 ());
201   printf ("\n");
202   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
203   printf ("\n");
204   printf ("\n");
205 }
206
207 static void
208 warranty ()
209 {
210   identify (stdout);
211   printf ("\n");
212   copyright ();
213   printf ("\n");
214   printf (_ (WARRANTY).to_str0 ());
215 }
216
217 static void
218 setup_paths ()
219 {
220   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
221     prefix_directory[1] = lilypond_prefix;
222
223   global_path.append ("");
224
225   /* Adding mf/out make lilypond unchanged source directory, when setting
226      LILYPONDPREFIX to lilypond-x.y.z */
227   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
228
229   for (unsigned i = 0; prefix_directory[i]; i++)
230     for (char **s = suffixes; *s; s++)
231       {
232         String p = prefix_directory[i] + to_string ('/') + String (*s);
233         global_path.prepend (p);
234         
235 #if !KPATHSEA
236         /* Urg: GNU make's $ (word) index starts at 1 */
237         int i  = 1;
238         while (global_path.try_append (p + to_string (".") + to_string (i)))
239           i++;
240 #endif
241       }
242 }
243   
244 static void
245 prepend_load_path (String dir)
246 {
247   String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
248   scm_c_eval_string (s.to_str0 ());
249 }
250
251 static void
252 determine_output_options ()
253 {
254   bool found_tex = false;
255   SCM formats = ly_output_formats ();
256   for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s)) 
257     {
258       found_tex = found_tex || (ly_scm2string (ly_car (s)) == "tex");
259     }
260
261   if (make_ps && found_tex)
262     {
263       make_dvi = true;
264     }
265   if (make_pdf || make_png)
266     {
267       make_ps = true;
268     }
269 }
270
271 static void
272 main_with_guile (void *, int, char **)
273 {
274   /* Engravers use lily.scm contents, need to make Guile find it.
275      Prepend onto GUILE %load-path, very ugh. */
276   for (unsigned i = 0; prefix_directory[i]; i++)
277     {
278       prepend_load_path (prefix_directory[i]);
279       /* Junk this.  We should make real modules iso. just loading files. */
280       prepend_load_path (String (prefix_directory[i]) + "/scm");
281     }
282
283   if (verbose_global_b)
284     dir_info (stderr);
285
286   ly_c_init_guile ();
287   call_constructors ();
288   progress_indication ("\n");
289
290   determine_output_options ();  
291   all_fonts_global = new All_font_metrics (global_path.to_string ());
292
293   init_scheme_code_string += ")";
294   scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
295
296   /* We accept multiple independent music files on the command line to
297      reduce compile time when processing lots of small files.
298      Starting the GUILE engine is very time consuming.  */
299
300   SCM files = SCM_EOL;
301   SCM *tail = &files;
302   while (char const *arg = option_parser->get_next_arg ())
303     {
304       *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
305       tail = SCM_CDRLOC (*tail);
306     }
307   delete option_parser;
308   option_parser = 0;
309
310   if (files == SCM_EOL)
311     {
312       /* No FILE arguments is now a usage error to help newbies.  If you
313          want a filter, you're not a newbie and should know to use file
314          argument `-'.  */
315       usage ();
316       exit (2);
317     }
318
319   SCM result = scm_call_1 (ly_scheme_function ("lilypond-main"), files);
320   (void) result;
321
322
323   /* Unreachable.  */
324   exit (0);
325 }
326
327 static void
328 setup_localisation ()
329 {
330 #if HAVE_GETTEXT
331   /* Enable locales */
332   setlocale (LC_ALL, "");
333   
334   /* FIXME: check if this is still true.
335     Disable localisation of float values.  This breaks TeX output.  */
336   setlocale (LC_NUMERIC, "C");
337   
338   String name (PACKAGE);
339   name.to_lower ();
340   bindtextdomain (name.to_str0 (), LOCALEDIR);
341   textdomain (name.to_str0 ());
342 #endif
343 }
344
345 static void
346 parse_argv (int argc, char **argv)
347 {
348   bool help_b = false;
349   option_parser = new Getopt_long (argc, argv, options_static);
350   while (Long_option_init const *opt = (*option_parser) ())
351     {
352       switch (opt->shortname_char_)
353         {
354         case 0:
355           if (String (opt->longname_str0_) == "png")
356             make_png = true;
357           else if (String (opt->longname_str0_) == "pdf")
358             make_pdf = true;
359           else if (String (opt->longname_str0_) == "ps")
360             make_ps = true;
361           else if (String (opt->longname_str0_) == "dvi")
362             make_dvi = true;
363           else if (String (opt->longname_str0_) == "preview")
364             make_preview = true;
365           break;
366           
367         case 'v':
368           notice ();
369           exit (0);
370           break;
371         case 'o':
372           {
373             String s = option_parser->optional_argument_str0_;
374             File_name file_name (s);
375             output_name_global = file_name.to_string ();
376           }
377           break;
378         case 'e':
379           init_scheme_code_string += option_parser->optional_argument_str0_;
380           break;
381         case 'w':
382           warranty ();
383           exit (0);
384           break;
385         case 'f':
386           if (option_parser->optional_argument_str0_ == "help")
387             {
388               printf (_ ("This option is for developers only.").to_str0 ());
389               printf (_ ("Read the sources for more information.").to_str0 ());
390               exit (0);
391             }
392           output_format_global = option_parser->optional_argument_str0_;
393           break;
394         case 'H':
395           dump_header_fieldnames_global
396             .push (option_parser->optional_argument_str0_);
397           break;
398         case 'I':
399           global_path.append (option_parser->optional_argument_str0_);
400           break;
401         case 'i':
402           init_name_global = option_parser->optional_argument_str0_;
403           break;
404         case 'h':
405           help_b = true;
406           break;
407         case 'V':
408           verbose_global_b = true;
409           break;
410         case 's':
411           safe_global_b = true;
412           break;
413         case 'm':
414           no_paper_global_b = true;
415           break;
416         default:
417           assert (false);
418           break;
419         }
420     }
421
422   if (help_b)
423     {
424       usage ();
425       if (verbose_global_b)
426         dir_info (stdout);
427       exit (0);
428     }
429 }
430
431 int
432 main (int argc, char **argv)
433 {
434   setup_localisation ();  
435   setup_paths ();
436   parse_argv (argc, argv);
437   initialize_kpathsea (argv[0]);
438
439   scm_boot_guile (argc, argv, main_with_guile, 0);
440
441   /* Unreachable */
442   return 0;
443 }