]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
3375142449239b40e8baffc8f95c8ef63c1f3ef1
[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 <string.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 "input-file-results.hh"
30 #include "warn.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;
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   {_i ("EXPR"), "evaluate", 'e',
107    _i ("set options, use -e '(ly-option-usage)' for help")},
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")},
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 #if 0
120   /*
121     should audit again.
122    */
123   {0, "safe", 's',  _i ("inhibit file output naming and exporting")},
124 #endif
125   {0, "version", 'v',  _i ("print version number")},
126   {0, "verbose", 'V', _i ("verbose")},
127   {0, "warranty", 'w',  _i ("show warranty and copyright")},
128   {0,0,0,0}
129 };
130
131 void
132 identify (FILE *out)
133 {
134   fputs (gnu_lilypond_version_string ().to_str0 (), out);
135 }
136
137 void
138 dirinfo (FILE *out)
139 {
140   fputs ("\n", out);
141   fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
142   fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
143   fprintf (out, "localedir: `%s'\n", LOCALEDIR);
144
145   char *lilypond_prefix = getenv ("LILYPONDPREFIX");
146   fprintf (out, "LILYPONDPREFIX: `%s'\n",
147            (lilypond_prefix ? lilypond_prefix : ""));
148 }
149
150 void
151 usage ()
152 {
153   printf ("\n");
154   /* No version number or newline here. It confuses help2man.  */
155   printf (_f ("Usage: %s [OPTION]... FILE...", "lilypond").to_str0 ());
156   printf ("\n\n");
157   printf (_ ("Typeset music and or play MIDI from FILE").to_str0 ());
158   printf ("\n\n");
159   printf(_ (
160 "LilyPond is a music typesetter.  It produces beautiful sheet music\n"
161 "using a high level description file as input.  LilyPond is part of \n"
162 "the GNU Project.\n"
163 ).to_str0 ());
164
165   printf ("\n");
166   printf (_ ("Options:").to_str0 ());
167   printf ("\n");
168   printf (Long_option_init::table_string (options_static).to_str0 ());
169   printf ("\n");
170   printf ("\n");
171
172   printf (_f ("Report bugs to %s", "bug-lilypond@gnu.org").to_str0 ());
173   printf ("\n");
174 }
175
176 void
177 version ()
178 {
179   identify (stdout);
180   printf ("\n");
181   printf (_f (
182   "This is free software.  It is covered by the GNU General Public License,\n"
183   "and you are welcome to change it and/or distribute copies of it under\n"
184   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
185     "lilypond").to_str0 ());
186   printf ("\n");
187
188   printf (_f ("Copyright (c) %s by", "1996--2002").to_str0 ());
189   printf ("\n");
190   printf ("  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
191   printf ("  Jan Nieuwenhuizen <janneke@gnu.org>\n");
192 }
193
194 void
195 notice ()
196 {
197   printf ("\n");
198   printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
199   printf ("\n");
200   printf (_f ("Copyright (c) %s by", "1996--2002").to_str0 ());
201   printf ("\n");
202   printf ("  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
203   printf ("  Jan Nieuwenhuizen <janneke@gnu.org>\n");
204   printf ("\n");
205   printf ( _ (
206              "    This program is free software; you can redistribute it and/or\n"
207              "modify it under the terms of the GNU General Public License version 2\n"
208              "as published by the Free Software Foundation.\n"
209              "\n"
210              "    This program is distributed in the hope that it will be useful,\n"
211              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
212              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
213              "General Public License for more details.\n"
214              "\n"
215              "    You should have received a copy (refer to the file COPYING) of the\n"
216              "GNU General Public License along with this program; if not, write to\n"
217              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
218              "USA.\n").to_str0 ());
219 }
220
221
222 /* Where LilyPond's init files live.  Typically:
223    LILYPOND_DATADIR = /usr/local/share/lilypond
224    LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
225 char const *prefix_directory[2] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR};
226
227 void
228 setup_paths ()
229 {
230   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
231     prefix_directory[1] = lilypond_prefix;
232
233 #if HAVE_GETTEXT
234   setlocale (LC_ALL, ""); /* enable locales */
235   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
236   String lily_locale_dir;
237   String name (PACKAGE);
238   name.to_lower ();
239
240   /*
241     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
242     handy for multiple source-dir runs, though...
243    */
244   if (!String (prefix_directory[0]).empty_b ())
245     {
246       lily_locale_dir = String (prefix_directory[0]) + "/share/locale";
247       bindtextdomain (name.to_str0 (), lily_locale_dir.to_str0 ());
248     }
249   else
250     bindtextdomain (name.to_str0 (), LOCALEDIR);
251   textdomain (name.to_str0 ());
252 #endif
253
254   global_path.add ("");
255
256
257   /* Adding mf/out make lilypond unchanged source directory, when setting
258      LILYPONDPREFIX to lilypond-x.y.z */
259   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
260
261   for (unsigned i = 0; i < sizeof (prefix_directory)
262          / sizeof (*prefix_directory); i++)
263     for (char **s = suffixes; *s; s++)
264       {
265         String p = prefix_directory[i] + to_string ('/') + String (*s);
266         global_path.add (p);
267         
268 #if !KPATHSEA
269         /* Urg: GNU make's $ (word) index starts at 1 */
270         int i  = 1;
271         while (global_path.try_add (p + to_string (".") + to_string (i)))
272           i++;
273 #endif
274       }
275   }
276   
277 /**
278   Make input file name from command argument.
279
280   Path describes file name with added default extension,
281   ".ly" if none.  "-" is stdin.
282  */
283 Path
284 distill_inname (String str)
285 {
286   Path p = split_path (str);
287   if (str.empty_b () || str == "-")
288     p.base = "-";
289   else
290     {
291       String orig_ext = p.ext;
292       char const *extensions[] = {"ly", "fly", "sly", "", 0};
293       for (int i = 0; extensions[i]; i++)
294         {
295           p.ext = orig_ext;
296           if (*extensions[i] && !p.ext.empty_b ())
297             p.ext += ".";
298           p.ext += extensions[i];
299           if (!global_path.find (p.string ()).empty_b ())
300               break;
301         }
302       /* Reshuffle extension */
303       p = split_path (p.string ());
304     }
305   return p;
306 }
307
308 String
309 format_to_ext (String format)
310 {
311   if (format == "tex")
312     /* .lytex change put off */
313     return "tex"; // "lytex";
314   return format;
315 }
316
317 void
318 prepend_load_path (String dir)
319 {
320   String s = "(set! %load-path (cons \""
321     + dir
322     + "\" %load-path))";
323   scm_c_eval_string (s.to_str0 ());
324 }
325
326 void
327 main_prog (void *, int, char **)
328 {
329   /* Engravers use lily.scm contents, need to make Guile find it.
330      Prepend onto GUILE %load-path, very ugh. */
331      
332   for (unsigned i = 0; i < sizeof (prefix_directory)
333          / sizeof (*prefix_directory); i++)
334     {
335       prepend_load_path (prefix_directory[i]);
336       /* Junk this.  We should make real modules iso. just loading files. */
337       prepend_load_path (String (prefix_directory[i]) + "/scm");
338     }
339
340   if (verbose_global_b)
341     dirinfo (stderr);
342   
343   ly_init_guile ();
344
345   progress_indication ("\n");
346
347   call_constructors ();
348   all_fonts_global = new All_font_metrics (global_path.string ());
349
350   init_scheme_code_string += ")";
351   gh_eval_str ((char *)init_scheme_code_string.to_str0 ());
352   
353   int p=0;
354   const char *arg  = oparser_p_static->get_next_arg ();
355
356   /* Only exit until after running init_scheme_code, for
357      (ly-option-usage) or
358      -e "(ly-set-option 'help #t)" */
359   if (!arg)
360     {
361       usage ();
362       /* No FILE arguments is now a usage error */
363       exit (2);
364     }
365
366   do
367     {
368       String infile (arg);
369       Midi_def::reset_score_count ();
370       Paper_def::reset_score_count ();
371
372       Path inpath = distill_inname (infile);
373
374       /* By default, use base name of input file for output file name */
375       Path outpath = inpath;
376       if (inpath.string () != "-")
377         outpath.ext = format_to_ext (output_format_global);
378
379       /* By default, write output to cwd; do not copy directory part
380          of input file name */
381       outpath.root = "";
382       outpath.dir = "";
383       
384       if (!output_name_global.empty_b ())
385         outpath = split_path (output_name_global);
386       
387       String init;
388       if (!init_name_global.empty_b ())
389         init = init_name_global;
390       else if (!inpath.ext.empty_b ())
391         init = "init." + inpath.ext;
392       else
393         init = "init.ly";
394         
395       /* Burp: output name communication goes through _global */
396       String save_output_name_global = output_name_global;
397       output_name_global = outpath.string ();
398       do_one_file (init, inpath.string ());
399       output_name_global = save_output_name_global;
400       
401       p++;
402     } while ((arg  = oparser_p_static->get_next_arg ()));
403   delete oparser_p_static;
404   oparser_p_static = 0;
405   exit (exit_status_global);
406 }
407
408
409 static int
410 sane_putenv (char const* key, char const* value, bool overwrite)
411 {
412   if (overwrite || !getenv (key))
413     {
414       String combine = String (key) + "=" + String (value);
415       char * s = strdup(combine.to_str0 ());
416       return putenv (s);
417     }
418   return -1;
419 }
420
421 int
422 main (int argc, char **argv)
423 {
424   setup_paths ();
425
426   /*
427     
428     These settings hopefully prepare lily for a lot of memory usage.
429
430     In practice the effect on GC times is barely measurable -- larger
431     segments cost slighly less time for the conservative marking. (but
432     not impressively much)
433     
434   */
435   sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
436   sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
437
438   ly_init_kpath (argv[0]);
439
440   bool help_b = false;
441   oparser_p_static = new Getopt_long (argc, argv, options_static);
442   while (Long_option_init const * opt = (*oparser_p_static) ())
443     {
444       switch (opt->shortname_char_)
445         {
446         case 'v':
447           version ();
448           exit (0);             // we print a version anyway.
449           break;
450         case 'o':
451           {
452             String s = oparser_p_static->optional_argument_str0_;
453             Path p = split_path (s);
454             if (s != "-" && p.ext.empty_b ())
455               p.ext = format_to_ext (output_format_global);
456             output_name_global = p.string ();
457           }
458           break;
459         case 'e':
460           init_scheme_code_string +=
461             oparser_p_static->optional_argument_str0_;
462           break;
463         case 'w':
464           notice ();
465           exit (0);
466           break;
467         case 'f':
468           if (oparser_p_static->optional_argument_str0_ == "help")
469             {
470               printf("See http://lilypond.org/wiki?OutputFormats for more information.\n"\
471                      "This option is for developers only.\n");
472               exit (0);
473             }
474           output_format_global = oparser_p_static->optional_argument_str0_;
475           break;
476         case 'P':
477             dependency_prefix_global = oparser_p_static->optional_argument_str0_;
478           break;
479         case 'H':
480           dump_header_fieldnames_global.push (oparser_p_static->optional_argument_str0_);
481           break;
482         case 'I':
483           global_path.push (oparser_p_static->optional_argument_str0_);
484           break;
485         case 'i':
486           init_name_global = oparser_p_static->optional_argument_str0_;
487           break;
488         case 'h':
489           help_b = true;
490           break;
491         case 'V':
492           verbose_global_b = true;
493           break;
494         case 's':
495           safe_global_b = true;
496           break;
497         case 'M':
498           dependency_global_b = true;
499           break; 
500         case 'm':
501           no_paper_global_b = true;
502           break;
503         default:
504           assert (false);
505           break;
506         }
507     }
508   identify (stderr);
509
510   if (help_b)
511     {
512       usage ();
513       if (verbose_global_b)
514         dirinfo (stdout);
515       exit (0);
516     }
517
518   scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
519
520   return 0;                     // unreachable
521 }