]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.3.71
[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       /* Urg: GNU make's $(word) index starts at 1 */
234       int i  = 1;
235       while (global_path.try_add (p + to_str (".") + to_str (i)))
236         i++;
237     }
238 }
239
240
241 void
242 main_prog (int, char**)
243 {
244   /*
245     need to do this first. Engravers use lily.scm contents.
246    */
247   init_lily_guile ();
248   read_lily_scm_file ("lily.scm");
249   cout << endl;
250
251   call_constructors ();
252   default_outname_base_global = "lelie";
253   all_fonts_global_p = new All_font_metrics (global_path.str ());
254   
255   int p=0;
256   const char *arg ;
257   while ((arg= oparser_global_p->get_next_arg ()))
258     {
259       
260       if (outname_str_global == "")
261         {
262           Midi_def::reset_default_count ();
263           Paper_def::reset_default_count ();
264         }
265       String f (arg);
266       String i;
267       f = distill_inname_str (f, i);
268       if (f == "-")
269         default_outname_base_global = "-";
270       else
271         {
272           String a,b,c,d;
273           split_path (f, a, b, c, d);
274           default_outname_base_global = c;
275         }
276       if (outname_str_global.length_i ())
277         default_outname_base_global = outname_str_global;
278       if (init_str_global.length_i ())
279         i = init_str_global;
280       else
281         i = "init" + i;
282       do_one_file (i, f);
283       p++;
284     }
285   if (!p)
286     {
287       String i;
288       if (init_str_global.length_i ())
289         i = init_str_global;
290       else
291         i = "init.ly";
292       default_outname_base_global = "-";
293       if (outname_str_global.length_i ())
294         default_outname_base_global = outname_str_global;
295       do_one_file (i, default_outname_base_global);
296     }
297   delete oparser_global_p;
298   exit( exit_status_i_);
299 }
300
301
302 int
303 main (int argc, char **argv)
304 {
305   debug_init ();                // should be first
306   setup_paths ();
307
308   /*
309     prepare guile for heavy mem usage. 
310    */
311   setenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", 0);
312   setenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", 0);
313
314   /*
315    initialize kpathsea
316    */
317   kpse_set_program_name(argv[0], NULL);
318   kpse_maketex_option("tfm", TRUE);
319
320   oparser_global_p = new Getopt_long(argc, argv,theopts);
321   while (Long_option_init const * opt = (*oparser_global_p)())
322     {
323       switch (opt->shortname_ch_)
324         {
325         case 'v':
326           version();
327           exit (0);             // we print a version anyway.
328           break;
329         case 't':
330           experimental_features_global_b = true;
331           progress_indication ("*** enabling experimental features, you're on your own now ***\n");
332           break;
333         case 'o':
334           outname_str_global = oparser_global_p->optional_argument_ch_C_;
335           break;
336         case 'w':
337           notice ();
338           exit (0);
339           break;
340         case 'f':
341           output_global_ch = oparser_global_p->optional_argument_ch_C_;
342           break;
343         case 'Q':
344           find_old_relative_b= true;
345           break;
346         case 'I':
347           global_path.push (oparser_global_p->optional_argument_ch_C_);
348           break;
349         case 'i':
350           init_str_global = oparser_global_p->optional_argument_ch_C_;
351           break;
352         case 'h':
353           usage ();
354           exit (0);
355           break;
356         case 'V':
357           verbose_global_b = true;
358           break;
359         case 's':
360           safe_global_b = true;
361           break;
362         case 'M':
363           dependency_global_b = true;
364           break; 
365         case 'd':
366           set_debug (true);
367           break;
368         case 'm':
369           no_paper_global_b = true;
370           break;
371         case 'T':
372           no_timestamps_global_b = true;
373           break;
374         default:
375           assert (false);
376           break;
377         }
378     }
379   identify (&cerr);
380
381 #ifdef WINNT
382   gh_enter (argc, argv, main_prog);
383 #else
384   gh_enter (argc, argv, (void(*)(...))main_prog);
385 #endif
386
387   return 0;                     // unreachable
388 }
389
390 /**
391   make input file name from command arg.
392
393   @input file name
394
395   @output file name with added default extension. "" is stdin.
396           in reference argument: the extension. ".ly" if none
397  */
398 String
399 distill_inname_str (String name_str, String& ext_r)
400 {
401   String str = name_str;
402   if (str.length_i ())
403     {
404       if (str != "-")
405         {
406           String a,b,c;
407           split_path (str,a,b,c,ext_r);
408
409           // add extension if not present.
410           char const* extensions[] = {"", ".ly", ".fly", ".sly", "", 0};
411           extensions[0] = ext_r.ch_C ();
412           for (int i = 0; extensions[i]; i++)
413             {
414               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
415                 {
416                   ext_r = extensions[i];
417                   break;
418                 }
419             }
420           str = a+b+c+ext_r;
421           // in any case, assume (init).ly
422           if (!ext_r.length_i ())
423             ext_r = ".ly";
424         }
425     }
426   else 
427     {
428       str = "-";
429       ext_r = ".ly";
430     }
431   return str;
432 }
433