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