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