]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.3.91
[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 #include "kpath.hh"
31
32
33 #if HAVE_GETTEXT
34 #include <libintl.h>
35 #endif
36
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   *os << gnu_lilypond_version_str ();
96 }
97
98 void
99 usage ()
100 {
101   
102   /*
103     No version number or newline here. It confuses help2man
104    */
105   cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond");
106   cout << "\n\n";
107   cout << _ ("Typeset music and or play MIDI from FILE");
108   cout << "\n\n";
109   cout << 
110 _(
111 "LilyPond is a music typesetter.  It produces beautiful sheet music\n"
112 "using a high level description file as input.  LilyPond is part of \n"
113 "the GNU Project.\n"
114 );
115
116   cout << '\n';
117   cout << _ ("Options:");
118   cout << '\n';
119   cout << Long_option_init::table_str (theopts);
120   cout << '\n';
121   cout << _ ("This binary was compiled with the following options:") 
122     << " " <<
123 #ifdef NDEBUG
124     "NDEBUG "
125 #endif
126 #ifdef NPRINT
127     "NPRINT "
128 #endif
129 #ifdef STRING_UTILS_INLINED
130     "STRING_UTILS_INLINED "
131 #endif
132     "\n"
133     "datadir: `" DIR_DATADIR "'\n"
134     "localedir: `" DIR_LOCALEDIR "'\n"
135     "\n";
136
137
138   cout << endl;
139
140   cout << _f ("Report bugs to %s", "bug-gnu-music@gnu.org") << endl;
141 }
142
143 void
144 version ()
145 {
146   identify (&cout);
147   cout << '\n';
148   cout << _f (""
149   "This is free software.  It is covered by the GNU General Public License,\n"
150   "and you are welcome to change it and/or distribute copies of it under\n"
151   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
152     "lilypond");
153   cout << endl;
154
155   cout << _f ("Copyright (c) %s by", "1996--2000");
156   cout << "Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
157        << "Jan Nieuwenhuizen <janneke@gnu.org>\n";
158 }
159
160 void
161 notice ()
162 {
163   cout << '\n';
164   // GNU GNU?
165   cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
166   cout << '\n';
167   cout << _f ("Copyright (c) %s by", "1996--2000");
168   cout << '\n';
169   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
170   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
171   cout << '\n';
172   cout << _ (
173              "    This program is free software; you can redistribute it and/or\n"
174              "modify it under the terms of the GNU General Public License version 2\n"
175              "as published by the Free Software Foundation.\n"
176              "\n"
177              "    This program is distributed in the hope that it will be useful,\n"
178              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
179              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
180              "General Public License for more details.\n"
181              "\n"
182              "    You should have received a copy (refer to the file COPYING) of the\n"
183              "GNU General Public License along with this program; if not, write to\n"
184              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
185              "USA.\n");
186 }
187
188 void
189 setup_paths ()
190 {
191   // facilitate binary distributions
192   char const *env_lily = getenv ("LILYPONDPREFIX");
193   String prefix_directory;
194   if (env_lily)
195     prefix_directory = env_lily;
196
197 #if HAVE_GETTEXT
198   setlocale (LC_ALL, ""); /* enable locales */
199   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
200   String lily_locale_dir;
201   String name (PACKAGE);
202   name.to_lower ();
203
204   /*
205     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
206     handy for multiple source-dir runs, though...
207    */
208   if (!prefix_directory.empty_b())
209     {
210       lily_locale_dir = prefix_directory + "/share/locale";
211       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
212     }
213   else
214     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
215   textdomain (name.ch_C ());
216 #endif
217
218   global_path.add ("");
219   // must override (come before) "/usr/local/share/lilypond"!
220   char const *env_sz = getenv ("LILYINCLUDE");
221   if (env_sz)
222     global_path.parse_path (env_sz);
223
224
225   /*
226     Should use kpathsea, this is getting out of hand.  
227    */
228   char *suffixes[] = {"ly", "afm", "scm", "tfm", "ps", 0};
229   String prefix = prefix_directory;
230   if (prefix.empty_b ()) prefix =  DIR_DATADIR;
231   for (char **s = suffixes; *s; s++)
232     {
233       String p =  prefix + to_str ('/') + String (*s);
234       global_path.add (p);
235
236 #if !KPATHSEA
237       /*
238       Although kpathsea seems nice, it is not universally available 
239       (GNU/Windows). 
240
241       Compiling kpathsea seems not possible without
242       (compiling) a matching tex installation.  Apart from the fact
243       that I cannot get kpathsea compiled for GNU/Windows, another
244       major problem is that TeX installations may be different on
245       different clients, so it wouldn't work anyway.  While ugly,
246       this code is simple and effective.
247         -- jcn
248       */
249
250       /* Urg: GNU make's $(word) index starts at 1 */
251       int i  = 1;
252       while (global_path.try_add (p + to_str (".") + to_str (i)))
253         i++;
254 #endif
255     }
256 }
257
258
259 void
260 main_prog (int, char**)
261 {
262   /*
263     need to do this first. Engravers use lily.scm contents.
264    */
265   init_lily_guile ();
266   if (verbose_global_b)
267     progress_indication ("\n");
268   read_lily_scm_file ("lily.scm");
269   cout << endl;
270
271   call_constructors ();
272   default_outname_base_global = "lelie";
273   all_fonts_global_p = new All_font_metrics (global_path.str ());
274   
275   int p=0;
276   const char *arg ;
277   while ((arg= oparser_global_p->get_next_arg ()))
278     {
279       
280       if (outname_str_global == "")
281         {
282           Midi_def::reset_default_count ();
283           Paper_def::reset_default_count ();
284         }
285       String f (arg);
286       String i;
287       f = distill_inname_str (f, i);
288       if (f == "-")
289         default_outname_base_global = "-";
290       else
291         {
292           String a,b,c,d;
293           split_path (f, a, b, c, d);
294           default_outname_base_global = c;
295         }
296       if (outname_str_global.length_i ())
297         default_outname_base_global = outname_str_global;
298       if (init_str_global.length_i ())
299         i = init_str_global;
300       else
301         i = "init" + i;
302       do_one_file (i, f);
303       p++;
304     }
305   if (!p)
306     {
307       String i;
308       if (init_str_global.length_i ())
309         i = init_str_global;
310       else
311         i = "init.ly";
312       default_outname_base_global = "-";
313       if (outname_str_global.length_i ())
314         default_outname_base_global = outname_str_global;
315       do_one_file (i, default_outname_base_global);
316     }
317   delete oparser_global_p;
318   exit( exit_status_i_);
319 }
320
321
322 int
323 main (int argc, char **argv)
324 {
325   debug_init ();                // should be first
326   setup_paths ();
327
328   /*
329     prepare guile for heavy mem usage.
330
331     putenv is POSIX, setenv is BSD 4.3
332    */
333   putenv ("GUILE_INIT_SEGMENT_SIZE_1=4194304");
334   putenv ("GUILE_MAX_SEGMENT_SIZE=8388608");
335
336   ly_init_kpath (argv[0]);
337   
338   oparser_global_p = new Getopt_long(argc, argv,theopts);
339   while (Long_option_init const * opt = (*oparser_global_p)())
340     {
341       switch (opt->shortname_ch_)
342         {
343         case 'v':
344           version();
345           exit (0);             // we print a version anyway.
346           break;
347         case 't':
348           experimental_features_global_b = true;
349           progress_indication ("*** enabling experimental features, you're on your own now ***\n");
350           break;
351         case 'o':
352           outname_str_global = oparser_global_p->optional_argument_ch_C_;
353           break;
354         case 'w':
355           notice ();
356           exit (0);
357           break;
358         case 'f':
359           output_global_ch = oparser_global_p->optional_argument_ch_C_;
360           break;
361         case 'Q':
362           find_old_relative_b= true;
363           break;
364         case 'I':
365           global_path.push (oparser_global_p->optional_argument_ch_C_);
366           break;
367         case 'i':
368           init_str_global = oparser_global_p->optional_argument_ch_C_;
369           break;
370         case 'h':
371           usage ();
372           exit (0);
373           break;
374         case 'V':
375           verbose_global_b = true;
376           break;
377         case 's':
378           safe_global_b = true;
379           break;
380         case 'M':
381           dependency_global_b = true;
382           break; 
383         case 'd':
384           set_debug (true);
385           break;
386         case 'm':
387           no_paper_global_b = true;
388           break;
389         case 'T':
390           no_timestamps_global_b = true;
391           break;
392         default:
393           assert (false);
394           break;
395         }
396     }
397   identify (&cerr);
398
399 #ifdef WINNT
400   gh_enter (argc, argv, main_prog);
401 #else
402   gh_enter (argc, argv, (void(*)(int, char**))main_prog);
403 #endif
404
405   return 0;                     // unreachable
406 }
407
408 /**
409   make input file name from command arg.
410
411   @input file name
412
413   @output file name with added default extension. "" is stdin.
414           in reference argument: the extension. ".ly" if none
415  */
416 String
417 distill_inname_str (String name_str, String& ext_r)
418 {
419   String str = name_str;
420   if (str.length_i ())
421     {
422       if (str != "-")
423         {
424           String a,b,c;
425           split_path (str,a,b,c,ext_r);
426
427           // add extension if not present.
428           char const* extensions[] = {"", ".ly", ".fly", ".sly", "", 0};
429           extensions[0] = ext_r.ch_C ();
430           for (int i = 0; extensions[i]; i++)
431             {
432               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
433                 {
434                   ext_r = extensions[i];
435                   break;
436                 }
437             }
438           str = a+b+c+ext_r;
439           // in any case, assume (init).ly
440           if (!ext_r.length_i ())
441             ext_r = ".ly";
442         }
443     }
444   else 
445     {
446       str = "-";
447       ext_r = ".ly";
448     }
449   return str;
450 }
451