]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
patch::: 1.3.140.jcn5
[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   {_i ("EXT"), "output-format", 'f',  _i ("use output format EXT (scm, ps, tex or as)")},
105   {0, "help", 'h',  _i ("this help")},
106   {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
107   {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
108   {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
109   {0, "dependencies", 'M',  _i ("write Makefile dependencies for every input file")},
110   {_i ("DIR"), "dep-prefix", 'P',  _i ("prepend DIR to dependencies")},
111   {0, "no-paper", 'm',  _i ("produce MIDI output only")},
112   {_i ("NAME"), "output", 'o',  _i ("write output to NAME")},
113   {0, "safe", 's',  _i ("inhibit file output naming and exporting")},
114   {0, "version", 'v',  _i ("print version number")},
115   {0, "verbose", 'V', _i ("verbose")},
116   {0, "warranty", 'w',  _i ("show warranty and copyright")},
117   {_i ("EXPR"), "evaluate", 'e',_i ("evalute EXPR as Scheme after .scm init is read")},
118   {0,0,0,0}
119 };
120
121 void
122 identify (ostream* os)
123 {
124   *os << gnu_lilypond_version_str ();
125 }
126
127 void
128 usage ()
129 {
130   
131   /*
132     No version number or newline here. It confuses help2man
133    */
134   cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond");
135   cout << "\n\n";
136   cout << _ ("Typeset music and or play MIDI from FILE");
137   cout << "\n\n";
138   cout << 
139 _ (
140 "LilyPond is a music typesetter.  It produces beautiful sheet music\n"
141 "using a high level description file as input.  LilyPond is part of \n"
142 "the GNU Project.\n"
143 );
144
145   cout << '\n';
146   cout << _ ("Options:");
147   cout << '\n';
148   cout << Long_option_init::table_str (options_static);
149   cout << '\n';
150   cout << _ ("This binary was compiled with the following options:") 
151     << " " <<
152 #ifdef NDEBUG
153     "NDEBUG "
154 #endif
155 #ifdef NPRINT
156     "NPRINT "
157 #endif
158 #ifdef STRING_UTILS_INLINED
159     "STRING_UTILS_INLINED "
160 #endif
161     "\n"
162     "datadir: `" DIR_DATADIR "'\n"
163     "localedir: `" DIR_LOCALEDIR "'\n"
164     "\n";
165
166
167   cout << endl;
168
169   cout << _f ("Report bugs to %s", "bug-gnu-music@gnu.org") << endl;
170 }
171
172 void
173 version ()
174 {
175   identify (&cout);
176   cout << '\n';
177   cout << _f (""
178   "This is free software.  It is covered by the GNU General Public License,\n"
179   "and you are welcome to change it and/or distribute copies of it under\n"
180   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
181     "lilypond");
182   cout << endl;
183
184   cout << _f ("Copyright (c) %s by", "1996--2001");
185   cout << '\n';
186   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
187   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
188 }
189
190 void
191 notice ()
192 {
193   cout << '\n';
194   cout << _ ("GNU LilyPond -- The music typesetter");
195   cout << '\n';
196   cout << _f ("Copyright (c) %s by", "1996--2001");
197   cout << '\n';
198   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
199   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
200   cout << '\n';
201   cout << _ (
202              "    This program is free software; you can redistribute it and/or\n"
203              "modify it under the terms of the GNU General Public License version 2\n"
204              "as published by the Free Software Foundation.\n"
205              "\n"
206              "    This program is distributed in the hope that it will be useful,\n"
207              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
208              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
209              "General Public License for more details.\n"
210              "\n"
211              "    You should have received a copy (refer to the file COPYING) of the\n"
212              "GNU General Public License along with this program; if not, write to\n"
213              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
214              "USA.\n");
215 }
216
217 void
218 setup_paths ()
219 {
220   // facilitate binary distributions
221   char const *env_lily = getenv ("LILYPONDPREFIX");
222   String prefix_directory;
223   if (env_lily)
224     prefix_directory = env_lily;
225
226 #if HAVE_GETTEXT
227   setlocale (LC_ALL, ""); /* enable locales */
228   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
229   String lily_locale_dir;
230   String name (PACKAGE);
231   name.to_lower ();
232
233   /*
234     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
235     handy for multiple source-dir runs, though...
236    */
237   if (!prefix_directory.empty_b ())
238     {
239       lily_locale_dir = prefix_directory + "/share/locale";
240       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
241     }
242   else
243     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
244   textdomain (name.ch_C ());
245 #endif
246
247   global_path.add ("");
248   // must override (come before) "/usr/local/share/lilypond"!
249   char const *env_sz = getenv ("LILYINCLUDE");
250   if (env_sz)
251     global_path.parse_path (env_sz);
252
253
254   /*
255     Should use kpathsea, this is getting out of hand.  
256    */
257   char *suffixes[] = {"ly", "afm", "scm", "tfm", "ps", 0};
258   String prefix = prefix_directory;
259   if (prefix.empty_b ())
260     prefix =  DIR_DATADIR;
261   for (char **s = suffixes; *s; s++)
262     {
263       String p =  prefix + to_str ('/') + String (*s);
264       global_path.add (p);
265
266 #if !KPATHSEA
267       /* Urg: GNU make's $ (word) index starts at 1 */
268       int i  = 1;
269       while (global_path.try_add (p + to_str (".") + to_str (i)))
270         i++;
271 #endif
272     }
273 }
274
275 /**
276   Make input file name from command argument.
277
278   Path describes file name with added default extension,
279   ".ly" if none.  "-" is stdin.
280  */
281 Path
282 distill_inname (String str)
283 {
284   Path p = split_path (str);
285   if (str.empty_b () || str == "-")
286     p.base = "-";
287   else
288     {
289       String orig_ext = p.ext;
290       char const *extensions[] = {"ly", "fly", "sly", "", 0};
291       for (int i = 0; extensions[i]; i++)
292         {
293           p.ext = orig_ext;
294           if (*extensions[i] && !p.ext.empty_b ())
295             p.ext += ".";
296           p.ext += extensions[i];
297           if (!global_path.find (p.str ()).empty_b ())
298               break;
299         }
300       /* Reshuffle extension */
301       p = split_path (p.str ());
302     }
303   return p;
304 }
305
306 String
307 format_to_ext (String format)
308 {
309   if (format == "tex")
310     /* .lytex change put off */
311     return "tex"; // "lytex";
312   return format;
313 }
314
315 void
316 main_prog (int, char**)
317 {
318   /*
319     need to do this first. Engravers use lily.scm contents.
320    */
321   init_lily_guile ();
322   if (verbose_global_b)
323     progress_indication ("\n");
324   read_lily_scm_file ("lily.scm");
325   cout << endl;
326
327   call_constructors ();
328   all_fonts_global_p = new All_font_metrics (global_path.str ());
329
330   init_scheme_code_string += ")";
331   gh_eval_str (init_scheme_code_string.ch_C());
332   
333   int p=0;
334   const char *arg ;
335   while ((arg = oparser_p_static->get_next_arg ()) || p == 0)
336     {
337       String infile;
338       
339       if (arg)
340         infile = arg;
341       else
342         infile = "-";
343         
344       // What/when was this supposed to do?
345       // It looks like it reset the outname_str_global for every new
346       // file, but only if user didn't specify a outname?  Huh?
347       // if (outname_str_global == "")
348       {
349         Midi_def::reset_score_count ();
350         Paper_def::reset_score_count ();
351       }
352
353       Path inpath = distill_inname (infile);
354
355       /* By default, use base name of input file for output file name */
356       Path outpath = inpath;
357       if (inpath.str () != "-")
358         outpath.ext = format_to_ext (output_format_global);
359
360       /* By default, write output to cwd; do not copy directory part
361          of input file name */
362       outpath.root = "";
363       outpath.dir = "";
364       
365       if (!output_name_global.empty_b ())
366         outpath = split_path (output_name_global);
367       
368       String init;
369       if (!init_name_global.empty_b ())
370         init = init_name_global;
371       else if (!inpath.ext.empty_b ())
372         init = "init." + inpath.ext;
373       else
374         init = "init.ly";
375         
376       /* Burp: output name communication goes through _global */
377       String save_output_name_global = output_name_global;
378       output_name_global = outpath.str ();
379       do_one_file (init, inpath.str ());
380       output_name_global = save_output_name_global;
381       
382       p++;
383     }
384   delete oparser_p_static;
385   exit (exit_status_global);
386 }
387
388 static int
389 sane_putenv (char const* key, char const* value)
390 {
391   /*
392     putenv is POSIX, setenv is BSD 4.3
393     Urg, but putenv blindly overwrites environment settings.
394   */
395   if (!getenv (key))
396     return putenv ((char*)((String (key) + "=" + value).ch_C ()));
397   return -1;
398 }
399
400 int
401 main (int argc, char **argv)
402 {
403   debug_init ();                // should be first (can see that; but Why?)
404   setup_paths ();
405
406   /* Prepare GUILE for heavy memory usage.  If you have plenty memory,
407      this may speed up GUILE a bit.  If you're short on memory, these
408      settings
409     
410          export GUILE_INIT_SEGMENT_SIZE_1=36000
411          export GUILE_MAX_SEGMENT_SIZE=576000
412
413      may considerably decrease memory footprint (~*0.85), with a small
414      execution time penalty (~*1.10).  */
415   
416   sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304");
417   sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608");
418
419   ly_init_kpath (argv[0]);
420   
421   oparser_p_static = new Getopt_long (argc, argv, options_static);
422   while (Long_option_init const * opt = (*oparser_p_static) ())
423     {
424       switch (opt->shortname_ch_)
425         {
426         case 'v':
427           version ();
428           exit (0);             // we print a version anyway.
429           break;
430         case 'o':
431           {
432             String s = oparser_p_static->optional_argument_ch_C_;
433             Path p = split_path (s);
434             if (p.ext.empty_b ())
435               p.ext = format_to_ext (output_format_global);
436             output_name_global = p.str ();
437           }
438           break;
439         case 'e':
440           init_scheme_code_string +=
441             oparser_p_static->optional_argument_ch_C_;
442           break;
443         case 'w':
444           notice ();
445           exit (0);
446           break;
447         case 'f':
448             output_format_global = oparser_p_static->optional_argument_ch_C_;
449           break;
450         case 'P':
451             dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
452           break;
453         case 'H':
454           dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
455           break;
456         case 'I':
457           global_path.push (oparser_p_static->optional_argument_ch_C_);
458           break;
459         case 'i':
460           init_name_global = oparser_p_static->optional_argument_ch_C_;
461           break;
462         case 'h':
463           usage ();
464           exit (0);
465           break;
466         case 'V':
467           verbose_global_b = true;
468           break;
469         case 's':
470           safe_global_b = true;
471           break;
472         case 'M':
473           dependency_global_b = true;
474           break; 
475         case 'm':
476           no_paper_global_b = true;
477           break;
478         default:
479           assert (false);
480           break;
481         }
482     }
483   identify (&cerr);
484
485 #ifdef WINNT
486   gh_enter (argc, argv, main_prog);
487 #else
488   gh_enter (argc, argv, (void (*) (int, char**))main_prog);
489 #endif
490
491   return 0;                     // unreachable
492 }
493
494