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