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