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