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