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