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