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