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