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