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