]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.3.72
[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--2000 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 "lily-guile.hh"
15 #include "lily-version.hh"
16
17 #include "all-font-metrics.hh"
18 #include "getopt-long.hh"
19 #include "misc.hh"
20 #include "string.hh"
21 #include "main.hh"
22 #include "file-path.hh"
23 #include "config.h"
24 #include "file-results.hh"
25 #include "debug.hh"
26 #include "lily-guile.hh"
27 #include "paper-def.hh"
28 #include "midi-def.hh"
29 #include "global-ctor.hh"
30
31 #if HAVE_GETTEXT
32 #include <libintl.h>
33 #endif
34
35 extern "C" {
36 #include <kpathsea/kpathsea.h>
37 }
38
39 bool verbose_global_b = false;
40 bool no_paper_global_b = false;
41 bool no_timestamps_global_b = false;
42 bool find_old_relative_b = false;
43
44 char const* output_global_ch = "tex";
45 All_font_metrics *all_fonts_global_p;
46
47 String default_outname_base_global =  "lelie";
48 String outname_str_global;
49 String init_str_global;
50
51 int default_count_global;
52 File_path global_path;
53
54 bool safe_global_b = false;
55 bool experimental_features_global_b = false;
56 bool dependency_global_b = false;
57
58 int exit_status_i_;
59
60 Getopt_long * oparser_global_p = 0;
61
62 String distill_inname_str (String name_str, String& ext_r);
63
64 /*
65  Internationalisation kludge in two steps:
66    * use _i () to get entry in POT file
67    * call gettext () explicitely for actual "translation"
68
69  Note: these messages all start with lower case (ie, don't
70        follow regular localisation guidelines).
71  */
72 Long_option_init theopts[] = {
73   {0, "debug", 'd',  _i ("enable debugging output")},
74   {_i ("EXT"), "output-format", 'f',  _i ("use output format EXT (scm, ps, tex or as)")},
75   {0, "help", 'h',  _i ("this help")},
76   {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
77   {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
78   {0, "dependencies", 'M',  _i ("write Makefile dependencies for every input file")},
79   {0, "no-paper", 'm',  _i ("produce MIDI output only")},
80   {_i ("BASENAME"), "output", 'o',  _i ("write output to BASENAME[-x].extension")},
81   {0, "find-old-relative", 'Q',  _i ("show all changes in relative syntax")},
82   {0, "safe", 's',  _i ("inhibit file output naming and exporting")},
83   {0, "no-timestamps", 'T',  _i ("don't timestamp the output")},
84   {0, "test", 't',  _i ("switch on experimental features")},
85   {0, "version", 'v',  _i ("print version number")},
86   {0, "verbose", 'V', _i("verbose")},
87   {0, "warranty", 'w',  _i ("show warranty and copyright")},
88   {0,0,0, 0}
89 };
90
91 void
92 identify (ostream* os)
93 {
94   *os << gnu_lilypond_version_str () << endl;
95 }
96
97 void
98 usage ()
99 {
100   identify (&cout);
101   cout << "\n";
102   cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond");
103   cout << "\n\n";
104   cout << _ ("Typeset music and or play MIDI from FILE");
105   cout << "\n\n";
106   cout << 
107 _(
108 "LilyPond is a music typesetter.  It produces beautiful sheet music\n"
109 "using a high level description file as input.  LilyPond is part of \n"
110 "the GNU Project.\n"
111 );
112
113   cout << '\n';
114   cout << _ ("Options:");
115   cout << '\n';
116   cout << Long_option_init::table_str (theopts);
117   cout << '\n';
118   cout << _ ("This binary was compiled with the following options:") 
119     << " " <<
120 #ifdef NDEBUG
121     "NDEBUG "
122 #endif
123 #ifdef NPRINT
124     "NPRINT "
125 #endif
126 #ifdef STRING_UTILS_INLINED
127     "STRING_UTILS_INLINED "
128 #endif
129     "\n"
130     "datadir: `" DIR_DATADIR "'\n"
131     "localedir: `" DIR_LOCALEDIR "'\n"
132     "\n";
133
134
135   cout << endl;
136
137   cout << _f ("Report bugs to %s", "bug-gnu-music@gnu.org") << endl;
138 }
139
140 void
141 version ()
142 {
143   identify (&cout);
144   cout << '\n';
145   cout << _f (""
146   "This is free software.  It is covered by the GNU General Public License,\n"
147   "and you are welcome to change it and/or distribute copies of it under\n"
148   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
149     "lilypond");
150   cout << endl;
151
152   cout << _f ("Copyright (c) %s by", "1996--2000");
153   cout << "Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
154        << "Jan Nieuwenhuizen <janneke@gnu.org>\n";
155 }
156
157 void
158 notice ()
159 {
160   cout << '\n';
161   // GNU GNU?
162   cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
163   cout << '\n';
164   cout << _f ("Copyright (c) %s by", "1996--2000");
165   cout << '\n';
166   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
167   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
168   cout << '\n';
169   cout << _ (
170              "    This program is free software; you can redistribute it and/or\n"
171              "modify it under the terms of the GNU General Public License version 2\n"
172              "as published by the Free Software Foundation.\n"
173              "\n"
174              "    This program is distributed in the hope that it will be useful,\n"
175              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
176              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
177              "General Public License for more details.\n"
178              "\n"
179              "    You should have received a copy (refer to the file COPYING) of the\n"
180              "GNU General Public License along with this program; if not, write to\n"
181              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
182              "USA.\n");
183 }
184
185 void
186 setup_paths ()
187 {
188   // facilitate binary distributions
189   char const *env_lily = getenv ("LILYPONDPREFIX");
190   String prefix_directory;
191   if (env_lily)
192     prefix_directory = env_lily;
193
194 #if HAVE_GETTEXT
195   setlocale (LC_ALL, ""); /* enable locales */
196   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
197   String lily_locale_dir;
198   String name (PACKAGE);
199   name.to_lower ();
200
201   /*
202     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
203     handy for multiple source-dir runs, though...
204    */
205   if (!prefix_directory.empty_b())
206     {
207       lily_locale_dir = prefix_directory + "/share/locale";
208       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
209     }
210   else
211     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
212   textdomain (name.ch_C ());
213 #endif
214
215   global_path.add ("");
216   // must override (come before) "/usr/local/share/lilypond"!
217   char const *env_sz = getenv ("LILYINCLUDE");
218   if (env_sz)
219     global_path.parse_path (env_sz);
220
221
222   /*
223     Should use kpathsea, this is getting out of hand.  
224    */
225   char *suffixes[] = {"ly", "afm", "scm", "tfm", "ps", 0};
226   String prefix = prefix_directory;
227   if (prefix.empty_b ()) prefix =  DIR_DATADIR;
228   for (char **s = suffixes; *s; s++)
229     {
230       String p =  prefix + to_str ('/') + String (*s);
231       
232       global_path.add (p);
233     }
234 }
235
236
237 void
238 main_prog (int, char**)
239 {
240   /*
241     need to do this first. Engravers use lily.scm contents.
242    */
243   init_lily_guile ();
244   read_lily_scm_file ("lily.scm");
245   cout << endl;
246
247   call_constructors ();
248   default_outname_base_global = "lelie";
249   all_fonts_global_p = new All_font_metrics (global_path.str ());
250   
251   int p=0;
252   const char *arg ;
253   while ((arg= oparser_global_p->get_next_arg ()))
254     {
255       
256       if (outname_str_global == "")
257         {
258           Midi_def::reset_default_count ();
259           Paper_def::reset_default_count ();
260         }
261       String f (arg);
262       String i;
263       f = distill_inname_str (f, i);
264       if (f == "-")
265         default_outname_base_global = "-";
266       else
267         {
268           String a,b,c,d;
269           split_path (f, a, b, c, d);
270           default_outname_base_global = c;
271         }
272       if (outname_str_global.length_i ())
273         default_outname_base_global = outname_str_global;
274       if (init_str_global.length_i ())
275         i = init_str_global;
276       else
277         i = "init" + i;
278       do_one_file (i, f);
279       p++;
280     }
281   if (!p)
282     {
283       String i;
284       if (init_str_global.length_i ())
285         i = init_str_global;
286       else
287         i = "init.ly";
288       default_outname_base_global = "-";
289       if (outname_str_global.length_i ())
290         default_outname_base_global = outname_str_global;
291       do_one_file (i, default_outname_base_global);
292     }
293   delete oparser_global_p;
294   exit( exit_status_i_);
295 }
296
297
298 int
299 main (int argc, char **argv)
300 {
301   debug_init ();                // should be first
302   setup_paths ();
303
304   /*
305     prepare guile for heavy mem usage. 
306    */
307   setenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", 0);
308   setenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", 0);
309
310   /*
311    initialize kpathsea
312    */
313   kpse_set_program_name(argv[0], NULL);
314   kpse_maketex_option("tfm", TRUE);
315
316   oparser_global_p = new Getopt_long(argc, argv,theopts);
317   while (Long_option_init const * opt = (*oparser_global_p)())
318     {
319       switch (opt->shortname_ch_)
320         {
321         case 'v':
322           version();
323           exit (0);             // we print a version anyway.
324           break;
325         case 't':
326           experimental_features_global_b = true;
327           progress_indication ("*** enabling experimental features, you're on your own now ***\n");
328           break;
329         case 'o':
330           outname_str_global = oparser_global_p->optional_argument_ch_C_;
331           break;
332         case 'w':
333           notice ();
334           exit (0);
335           break;
336         case 'f':
337           output_global_ch = oparser_global_p->optional_argument_ch_C_;
338           break;
339         case 'Q':
340           find_old_relative_b= true;
341           break;
342         case 'I':
343           global_path.push (oparser_global_p->optional_argument_ch_C_);
344           break;
345         case 'i':
346           init_str_global = oparser_global_p->optional_argument_ch_C_;
347           break;
348         case 'h':
349           usage ();
350           exit (0);
351           break;
352         case 'V':
353           verbose_global_b = true;
354           break;
355         case 's':
356           safe_global_b = true;
357           break;
358         case 'M':
359           dependency_global_b = true;
360           break; 
361         case 'd':
362           set_debug (true);
363           break;
364         case 'm':
365           no_paper_global_b = true;
366           break;
367         case 'T':
368           no_timestamps_global_b = true;
369           break;
370         default:
371           assert (false);
372           break;
373         }
374     }
375   identify (&cerr);
376
377 #ifdef WINNT
378   gh_enter (argc, argv, main_prog);
379 #else
380   gh_enter (argc, argv, (void(*)(...))main_prog);
381 #endif
382
383   return 0;                     // unreachable
384 }
385
386 /**
387   make input file name from command arg.
388
389   @input file name
390
391   @output file name with added default extension. "" is stdin.
392           in reference argument: the extension. ".ly" if none
393  */
394 String
395 distill_inname_str (String name_str, String& ext_r)
396 {
397   String str = name_str;
398   if (str.length_i ())
399     {
400       if (str != "-")
401         {
402           String a,b,c;
403           split_path (str,a,b,c,ext_r);
404
405           // add extension if not present.
406           char const* extensions[] = {"", ".ly", ".fly", ".sly", "", 0};
407           extensions[0] = ext_r.ch_C ();
408           for (int i = 0; extensions[i]; i++)
409             {
410               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
411                 {
412                   ext_r = extensions[i];
413                   break;
414                 }
415             }
416           str = a+b+c+ext_r;
417           // in any case, assume (init).ly
418           if (!ext_r.length_i ())
419             ext_r = ".ly";
420         }
421     }
422   else 
423     {
424       str = "-";
425       ext_r = ".ly";
426     }
427   return str;
428 }
429