]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.2.9
[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   init_lily_guile ();
232   read_lily_scm_file ( "lily.scm");
233   cout << endl;
234
235   call_constructors ();
236   default_outname_base_global = "lelie";
237   all_fonts_global_p = new All_font_metrics (global_path.str ());
238   
239   int p=0;
240   const char *arg ;
241   while ((arg= oparser_global_p->get_next_arg ()))
242     {
243       
244       if (outname_str_global == "")
245         {
246           Midi_def::reset_default_count ();
247           Paper_def::reset_default_count ();
248         }
249       String f (arg);
250       String i;
251       f = distill_inname_str (f, i);
252       if (f == "-")
253         default_outname_base_global = "-";
254       else
255         {
256           String a,b,c,d;
257           split_path (f, a, b, c, d);
258           default_outname_base_global = c;
259         }
260       if (outname_str_global.length_i ())
261         default_outname_base_global = outname_str_global;
262       if (init_str_global.length_i ())
263         i = init_str_global;
264       else
265         i = "init" + i;
266       do_one_file (i, f);
267       p++;
268     }
269   if (!p)
270     {
271       String i;
272       if (init_str_global.length_i ())
273         i = init_str_global;
274       else
275         i = "init.ly";
276       default_outname_base_global = "-";
277       if (outname_str_global.length_i ())
278         default_outname_base_global = outname_str_global;
279       do_one_file (i, default_outname_base_global);
280     }
281   delete oparser_global_p;
282   exit( exit_status_i_);
283 }
284
285
286 int
287 main (int argc, char **argv)
288 {
289   debug_init ();                // should be first
290   setup_paths ();
291
292   oparser_global_p = new Getopt_long(argc, argv,theopts);
293   while (Long_option_init const * opt = (*oparser_global_p)())
294     {
295       switch (opt->shortname_ch_)
296         {
297         case 'v':
298           version();
299           exit (0);             // we print a version anyway.
300           break;
301         case 't':
302           experimental_features_global_b = true;
303           *mlog << "*** enabling experimental features, you're on your own now ***\n";
304           break;
305         case 'o':
306           outname_str_global = oparser_global_p->optional_argument_ch_C_;
307           break;
308         case 'w':
309           notice ();
310           exit (0);
311           break;
312         case 'f':
313           output_global_ch = oparser_global_p->optional_argument_ch_C_;
314           break;
315         case 'Q':
316           find_old_relative_b= true;
317           break;
318         case 'I':
319           global_path.push (oparser_global_p->optional_argument_ch_C_);
320           break;
321         case 'i':
322           init_str_global = oparser_global_p->optional_argument_ch_C_;
323           break;
324         case 'h':
325           usage ();
326           exit (0);
327           break;
328         case 'V':
329           version_ignore_global_b = true;
330           break;
331         case 's':
332           safe_global_b = true;
333           break;
334         case 'M':
335           dependency_global_b = true;
336           break; 
337         case 'd':
338           set_debug (true);
339           break;
340         case 'm':
341           no_paper_global_b = true;
342           break;
343         case 'T':
344           no_timestamps_global_b = true;
345           break;
346         default:
347           assert (false);
348           break;
349         }
350     }
351   identify ();
352
353 #ifdef WINNT
354   gh_enter (argc, argv, main_prog);
355 #else
356   gh_enter (argc, argv, (void(*)(...))main_prog);
357 #endif
358
359   return 0;                     // unreachable
360 }
361
362 /**
363   make input file name from command arg.
364
365   @input file name
366
367   @output file name with added default extension. "" is stdin.
368           in reference argument: the extension. ".ly" if none
369  */
370 String
371 distill_inname_str (String name_str, String& ext_r)
372 {
373   String str = name_str;
374   if (str.length_i ())
375     {
376       if (str != "-")
377         {
378           String a,b,c;
379           split_path (str,a,b,c,ext_r);
380
381           // add extension if not present.
382           char const* extensions[] = {"", ".ly", ".fly", ".sly", "", 0};
383           extensions[0] = ext_r.ch_C ();
384           for (int i = 0; extensions[i]; i++)
385             {
386               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
387                 {
388                   ext_r = extensions[i];
389                   break;
390                 }
391             }
392           str = a+b+c+ext_r;
393           // in any case, assume (init).ly
394           if (!ext_r.length_i ())
395             ext_r = ".ly";
396         }
397     }
398   else 
399     {
400       str = "-";
401       ext_r = ".ly";
402     }
403   return str;
404 }
405