]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
*** empty log message ***
[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     {
283       make_pdf = true;
284       make_ps = true;
285       if (found_tex)
286         {
287           make_dvi = true;
288           make_tex = true;
289         }
290     }
291 }
292
293 static void
294 main_with_guile (void *, int, char **)
295 {
296   /* Engravers use lily.scm contents, need to make Guile find it.
297      Prepend onto GUILE %load-path, very ugh. */
298   for (unsigned i = 0; prefix_directory[i]; i++)
299     {
300       prepend_load_path (prefix_directory[i]);
301       /* Junk this.  We should make real modules iso. just loading files. */
302       prepend_load_path (String (prefix_directory[i]) + "/scm");
303     }
304
305   if (verbose_global_b)
306     dir_info (stderr);
307
308   ly_c_init_guile ();
309   call_constructors ();
310   progress_indication ("\n");
311
312   determine_output_options ();  
313   all_fonts_global = new All_font_metrics (global_path.to_string ());
314
315   init_scheme_code_string += ")";
316   scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
317
318   /* We accept multiple independent music files on the command line to
319      reduce compile time when processing lots of small files.
320      Starting the GUILE engine is very time consuming.  */
321
322   SCM files = SCM_EOL;
323   SCM *tail = &files;
324   while (char const *arg = option_parser->get_next_arg ())
325     {
326       *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
327       tail = SCM_CDRLOC (*tail);
328     }
329   delete option_parser;
330   option_parser = 0;
331
332   if (files == SCM_EOL)
333     {
334       /* No FILE arguments is now a usage error to help newbies.  If you
335          want a filter, you're not a newbie and should know to use file
336          argument `-'.  */
337       usage ();
338       exit (2);
339     }
340
341   SCM result = scm_call_1 (ly_scheme_function ("lilypond-main"), files);
342   (void) result;
343
344
345   /* Unreachable.  */
346   exit (0);
347 }
348
349 static void
350 setup_localisation ()
351 {
352 #if HAVE_GETTEXT
353   /* Enable locales */
354   setlocale (LC_ALL, "");
355   
356   /* FIXME: check if this is still true.
357     Disable localisation of float values.  This breaks TeX output.  */
358   setlocale (LC_NUMERIC, "C");
359   
360   String name (PACKAGE);
361   name.to_lower ();
362   bindtextdomain (name.to_str0 (), LOCALEDIR);
363   textdomain (name.to_str0 ());
364 #endif
365 }
366
367 static void
368 parse_argv (int argc, char **argv)
369 {
370   bool help_b = false;
371   option_parser = new Getopt_long (argc, argv, options_static);
372   while (Long_option_init const *opt = (*option_parser) ())
373     {
374       switch (opt->shortname_char_)
375         {
376         case 0:
377           if (String (opt->longname_str0_) == "png")
378             make_png = true;
379           else if (String (opt->longname_str0_) == "pdf")
380             make_pdf = true;
381           else if (String (opt->longname_str0_) == "ps")
382             make_ps = true;
383           else if (String (opt->longname_str0_) == "dvi")
384             make_dvi = true;
385           else if (String (opt->longname_str0_) == "preview")
386             make_preview = true;
387           break;
388           
389         case 'v':
390           notice ();
391           exit (0);
392           break;
393         case 'o':
394           {
395             String s = option_parser->optional_argument_str0_;
396             File_name file_name (s);
397             output_name_global = file_name.to_string ();
398           }
399           break;
400         case 'e':
401           init_scheme_code_string += option_parser->optional_argument_str0_;
402           break;
403         case 'w':
404           warranty ();
405           exit (0);
406           break;
407         case 'f':
408           if (option_parser->optional_argument_str0_ == "help")
409             {
410               printf (_ ("This option is for developers only.").to_str0 ());
411               printf (_ ("Read the sources for more information.").to_str0 ());
412               exit (0);
413             }
414           output_format_global = option_parser->optional_argument_str0_;
415           break;
416         case 'H':
417           dump_header_fieldnames_global
418             .push (option_parser->optional_argument_str0_);
419           break;
420         case 'I':
421           global_path.append (option_parser->optional_argument_str0_);
422           break;
423         case 'i':
424           init_name_global = option_parser->optional_argument_str0_;
425           break;
426         case 'h':
427           help_b = true;
428           break;
429         case 'V':
430           verbose_global_b = true;
431           break;
432         case 's':
433           safe_global_b = true;
434           break;
435         case 'm':
436           no_paper_global_b = true;
437           break;
438         case 'p':
439           make_preview = true;
440           break;
441         default:
442           programming_error (to_string ("unhandled short option: %c",
443                                         opt->shortname_char_));
444           assert (false);
445           break;
446         }
447     }
448
449   if (help_b)
450     {
451       usage ();
452       if (verbose_global_b)
453         dir_info (stdout);
454       exit (0);
455     }
456 }
457
458 int
459 main (int argc, char **argv)
460 {
461   setup_localisation ();  
462   setup_paths ();
463   parse_argv (argc, argv);
464   initialize_kpathsea (argv[0]);
465
466   scm_boot_guile (argc, argv, main_with_guile, 0);
467
468   /* Unreachable */
469   return 0;
470 }