]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.5.0
[lilypond.git] / lily / main.cc
1 /*
2   main.cc -- implement main: entrypoints
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdlib.h>
10 #include <iostream.h>
11 #include <assert.h>
12 #include <locale.h>
13
14 #include "config.h"
15
16 #if HAVE_GETTEXT
17 #include <libintl.h>
18 #endif
19
20 #include "lily-guile.hh"
21 #include "lily-version.hh"
22 #include "all-font-metrics.hh"
23 #include "getopt-long.hh"
24 #include "misc.hh"
25 #include "string.hh"
26 #include "main.hh"
27 #include "file-path.hh"
28 #include "file-results.hh"
29 #include "debug.hh"
30 #include "lily-guile.hh"
31 #include "paper-def.hh"
32 #include "midi-def.hh"
33 #include "global-ctor.hh"
34 #include "kpath.hh"
35
36
37 /*
38   Global options that can be overridden through command line.
39 */
40
41 /* Write dependencies file? */
42 bool dependency_global_b = false;
43
44 /* Prepend to dependencies */
45 String dependency_prefix_global;
46
47 /* Names of header fields to be dumped to a separate file. */
48 Array<String> dump_header_fieldnames_global;
49
50 /* Name of initialisation file. */
51 String init_name_global;
52
53 /* Do not calculate and write paper output? */
54 bool no_paper_global_b = false;
55
56 /* Selected output format.
57    One of tex, ps, scm, as. */
58 String output_format_global = "tex";
59
60 /* Current output name. */
61 String output_name_global;
62
63 /* Run in safe mode? -- FIXME: should be re-analised */
64 bool safe_global_b = false;
65
66 /* Verbose progress indication? */
67 bool verbose_global_b = false;
68
69 /* Scheme code to execute before parsing, after .scm init */
70 String init_scheme_code_string = "(begin #t ";
71
72
73 /*
74   Misc. global stuff.
75  */
76
77
78 All_font_metrics *all_fonts_global_p;
79 int exit_status_global;
80 File_path global_path;
81
82 /* Number of current score output block.  If there's more than one
83    score block, this counter will be added to the output filename. */
84 int score_count_global;
85
86
87
88 /*
89   File globals.
90  */
91
92 /*  The option parser */
93 static Getopt_long *oparser_p_static = 0;
94
95 /*
96  Internationalisation kludge in two steps:
97    * use _i () to get entry in POT file
98    * call gettext () explicitely for actual "translation"
99
100  Note: these messages all start with lower case (ie, don't
101        follow regular localisation guidelines).
102  */
103 static Long_option_init options_static[] = {
104   /* print example usage:  lilypond -e "(set-lily-option 'help 0)" ? */
105   {_i ("EXPR"), "evaluate", 'e',_i ("evalute EXPR as Scheme after .scm init is read")},
106   /* another bug in option parser: --output=foe is taken as an abbreviation
107      for --output-format */
108   {_i ("EXT"), "format", 'f',  _i ("use output format EXT (scm, ps, tex or as)")},
109   {0, "help", 'h',  _i ("this help")},
110   {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
111   {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
112   {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
113   {0, "dependencies", 'M',  _i ("write Makefile dependencies for every input file")},
114   {0, "no-paper", 'm',  _i ("produce MIDI output only")},
115   {_i ("FILE"), "output", 'o',  _i ("write output to FILE")},
116   {_i ("DIR"), "dep-prefix", 'P',  _i ("prepend DIR to dependencies")},
117   {0, "safe", 's',  _i ("inhibit file output naming and exporting")},
118   {0, "version", 'v',  _i ("print version number")},
119   {0, "verbose", 'V', _i ("verbose")},
120   {0, "warranty", 'w',  _i ("show warranty and copyright")},
121   {0,0,0,0}
122 };
123
124 void
125 identify (ostream* os)
126 {
127   *os << gnu_lilypond_version_str ();
128 }
129
130 void
131 usage ()
132 {
133   
134   /*
135     No version number or newline here. It confuses help2man
136    */
137   cout << _f ("Usage: %s [OPTION]... FILE...", "lilypond");
138   cout << "\n\n";
139   cout << _ ("Typeset music and or play MIDI from FILE");
140   cout << "\n\n";
141   cout << 
142 _ (
143 "LilyPond is a music typesetter.  It produces beautiful sheet music\n"
144 "using a high level description file as input.  LilyPond is part of \n"
145 "the GNU Project.\n"
146 );
147
148   cout << '\n';
149   cout << _ ("Options:");
150   cout << '\n';
151   cout << Long_option_init::table_str (options_static);
152   cout << '\n';
153   cout << _ ("This binary was compiled with the following options:") 
154     << " " <<
155 #ifdef NDEBUG
156     "NDEBUG "
157 #endif
158 #ifdef NPRINT
159     "NPRINT "
160 #endif
161 #ifdef STRING_UTILS_INLINED
162     "STRING_UTILS_INLINED "
163 #endif
164     "\n"
165     "datadir: `" DIR_DATADIR "'\n"
166     "localedir: `" DIR_LOCALEDIR "'\n"
167     "\n";
168
169
170   cout << endl;
171
172   cout << _f ("Report bugs to %s", "bug-lilypond@gnu.org") << endl;
173 }
174
175 void
176 version ()
177 {
178   identify (&cout);
179   cout << '\n';
180   cout << _f (""
181   "This is free software.  It is covered by the GNU General Public License,\n"
182   "and you are welcome to change it and/or distribute copies of it under\n"
183   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
184     "lilypond");
185   cout << endl;
186
187   cout << _f ("Copyright (c) %s by", "1996--2001");
188   cout << '\n';
189   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
190   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
191 }
192
193 void
194 notice ()
195 {
196   cout << '\n';
197   cout << _ ("GNU LilyPond -- The music typesetter");
198   cout << '\n';
199   cout << _f ("Copyright (c) %s by", "1996--2001");
200   cout << '\n';
201   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
202   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
203   cout << '\n';
204   cout << _ (
205              "    This program is free software; you can redistribute it and/or\n"
206              "modify it under the terms of the GNU General Public License version 2\n"
207              "as published by the Free Software Foundation.\n"
208              "\n"
209              "    This program is distributed in the hope that it will be useful,\n"
210              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
211              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
212              "General Public License for more details.\n"
213              "\n"
214              "    You should have received a copy (refer to the file COPYING) of the\n"
215              "GNU General Public License along with this program; if not, write to\n"
216              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
217              "USA.\n");
218 }
219
220 void
221 setup_paths ()
222 {
223   // facilitate binary distributions
224   char const *env_lily = getenv ("LILYPONDPREFIX");
225   String prefix_directory;
226   if (env_lily)
227     prefix_directory = env_lily;
228
229 #if HAVE_GETTEXT
230   setlocale (LC_ALL, ""); /* enable locales */
231   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
232   String lily_locale_dir;
233   String name (PACKAGE);
234   name.to_lower ();
235
236   /*
237     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
238     handy for multiple source-dir runs, though...
239    */
240   if (!prefix_directory.empty_b ())
241     {
242       lily_locale_dir = prefix_directory + "/share/locale";
243       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
244     }
245   else
246     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
247   textdomain (name.ch_C ());
248 #endif
249
250   global_path.add ("");
251   // must override (come before) "/usr/local/share/lilypond"!
252   char const *env_sz = getenv ("LILYINCLUDE");
253   if (env_sz)
254     global_path.parse_path (env_sz);
255
256
257   /* Adding mf/out make lilypond unchanged source directory, when setting
258      LILYPONDPREFIX to lilypond-x.y.z */
259   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
260   String prefix = prefix_directory;
261   if (prefix.empty_b ())
262     prefix =  DIR_DATADIR;
263   for (char **s = suffixes; *s; s++)
264     {
265       String p =  prefix + to_str ('/') + String (*s);
266       global_path.add (p);
267
268 #if !KPATHSEA
269       /* Urg: GNU make's $ (word) index starts at 1 */
270       int i  = 1;
271       while (global_path.try_add (p + to_str (".") + to_str (i)))
272         i++;
273 #endif
274     }
275 }
276
277 /**
278   Make input file name from command argument.
279
280   Path describes file name with added default extension,
281   ".ly" if none.  "-" is stdin.
282  */
283 Path
284 distill_inname (String str)
285 {
286   Path p = split_path (str);
287   if (str.empty_b () || str == "-")
288     p.base = "-";
289   else
290     {
291       String orig_ext = p.ext;
292       char const *extensions[] = {"ly", "fly", "sly", "", 0};
293       for (int i = 0; extensions[i]; i++)
294         {
295           p.ext = orig_ext;
296           if (*extensions[i] && !p.ext.empty_b ())
297             p.ext += ".";
298           p.ext += extensions[i];
299           if (!global_path.find (p.str ()).empty_b ())
300               break;
301         }
302       /* Reshuffle extension */
303       p = split_path (p.str ());
304     }
305   return p;
306 }
307
308 String
309 format_to_ext (String format)
310 {
311   if (format == "tex")
312     /* .lytex change put off */
313     return "tex"; // "lytex";
314   return format;
315 }
316
317 void
318 main_prog (int, char**)
319 {
320   /*
321     need to do this first. Engravers use lily.scm contents.
322    */
323   init_lily_guile ();
324   if (verbose_global_b)
325     progress_indication ("\n");
326   read_lily_scm_file ("lily.scm");
327   cout << endl;
328
329   call_constructors ();
330   all_fonts_global_p = new All_font_metrics (global_path.str ());
331
332   init_scheme_code_string += ")";
333   gh_eval_str ((char *)init_scheme_code_string.ch_C());
334   
335   int p=0;
336   const char *arg  = oparser_p_static->get_next_arg ();
337
338   if (!arg)
339     {
340       usage ();
341       /* No FILE arguments is now a usage error */
342       exit (2);
343     }
344   else
345     do 
346     {
347       String infile (arg);
348         
349       /* What/when was this supposed to do?
350        It looks like it reset the outname_str_global for every new
351        file, but only if user didn't specify a outname?  Huh?
352
353        // if (outname_str_global == "")
354
355       */
356       {
357         Midi_def::reset_score_count ();
358         Paper_def::reset_score_count ();
359       }
360
361       Path inpath = distill_inname (infile);
362
363       /* By default, use base name of input file for output file name */
364       Path outpath = inpath;
365       if (inpath.str () != "-")
366         outpath.ext = format_to_ext (output_format_global);
367
368       /* By default, write output to cwd; do not copy directory part
369          of input file name */
370       outpath.root = "";
371       outpath.dir = "";
372       
373       if (!output_name_global.empty_b ())
374         outpath = split_path (output_name_global);
375       
376       String init;
377       if (!init_name_global.empty_b ())
378         init = init_name_global;
379       else if (!inpath.ext.empty_b ())
380         init = "init." + inpath.ext;
381       else
382         init = "init.ly";
383         
384       /* Burp: output name communication goes through _global */
385       String save_output_name_global = output_name_global;
386       output_name_global = outpath.str ();
387       do_one_file (init, inpath.str ());
388       output_name_global = save_output_name_global;
389       
390       p++;
391     } while ((arg  = oparser_p_static->get_next_arg ()));
392   delete oparser_p_static;
393   oparser_p_static = 0;
394   exit (exit_status_global);
395 }
396
397 static int
398 sane_putenv (char const* key, char const* value)
399 {
400   /*
401     putenv is POSIX, setenv is BSD 4.3
402     Urg, but putenv blindly overwrites environment settings.
403   */
404   if (!getenv (key))
405     return putenv ((char*)((String (key) + "=" + value).ch_C ()));
406   return -1;
407 }
408
409 int
410 main (int argc, char **argv)
411 {
412   setup_paths ();
413
414   /* Prepare GUILE for heavy memory usage.  If you have plenty memory,
415      this may speed up GUILE a bit.  If you're short on memory, these
416      settings
417     
418          export GUILE_INIT_SEGMENT_SIZE_1=36000
419          export GUILE_MAX_SEGMENT_SIZE=576000
420
421      may considerably decrease memory footprint (~*0.85), with a small
422      execution time penalty (~*1.10).  However, if this 15% gain in memory
423      usage prevents swapping, the execution time falls drastically. */
424   
425   sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304");
426   sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608");
427
428   ly_init_kpath (argv[0]);
429   
430   oparser_p_static = new Getopt_long (argc, argv, options_static);
431   while (Long_option_init const * opt = (*oparser_p_static) ())
432     {
433       switch (opt->shortname_ch_)
434         {
435         case 'v':
436           version ();
437           exit (0);             // we print a version anyway.
438           break;
439         case 'o':
440           {
441             String s = oparser_p_static->optional_argument_ch_C_;
442             Path p = split_path (s);
443             if (s != "-" && p.ext.empty_b ())
444               p.ext = format_to_ext (output_format_global);
445             output_name_global = p.str ();
446           }
447           break;
448         case 'e':
449           init_scheme_code_string +=
450             oparser_p_static->optional_argument_ch_C_;
451           break;
452         case 'w':
453           notice ();
454           exit (0);
455           break;
456         case 'f':
457             output_format_global = oparser_p_static->optional_argument_ch_C_;
458           break;
459         case 'P':
460             dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
461           break;
462         case 'H':
463           dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
464           break;
465         case 'I':
466           global_path.push (oparser_p_static->optional_argument_ch_C_);
467           break;
468         case 'i':
469           init_name_global = oparser_p_static->optional_argument_ch_C_;
470           break;
471         case 'h':
472           usage ();
473           exit (0);
474           break;
475         case 'V':
476           verbose_global_b = true;
477           break;
478         case 's':
479           safe_global_b = true;
480           break;
481         case 'M':
482           dependency_global_b = true;
483           break; 
484         case 'm':
485           no_paper_global_b = true;
486           break;
487         default:
488           assert (false);
489           break;
490         }
491     }
492   identify (&cerr);
493
494 #ifdef WINNT
495   gh_enter (argc, argv, main_prog);
496 #else
497   gh_enter (argc, argv, (void (*) (int, char**))main_prog);
498 #endif
499
500   return 0;                     // unreachable
501 }
502
503