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