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