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