]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
*** empty log message ***
[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--2003 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 ("print 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 ("be 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 [OPTIONS]... 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   printf ("\n");
175 }
176
177 void
178 version ()
179 {
180   identify (stdout);
181   printf ("\n");
182   printf (_f (
183   "This is free software.  It is covered by the GNU General Public License,\n"
184   "and you are welcome to change it and/or distribute copies of it under\n"
185   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
186     "lilypond").to_str0 ());
187   printf ("\n");
188
189   printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
190   printf ("\n");
191   printf ("  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
192   printf ("  Jan Nieuwenhuizen <janneke@gnu.org>\n");
193 }
194
195 void
196 notice ()
197 {
198   printf ("\n");
199   printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
200   printf ("\n");
201   printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
202   printf ("\n");
203   printf ("  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
204   printf ("  Jan Nieuwenhuizen <janneke@gnu.org>\n");
205   printf ("\n");
206   printf ( _ (
207              "    This program is free software; you can redistribute it and/or\n"
208              "modify it under the terms of the GNU General Public License version 2\n"
209              "as published by the Free Software Foundation.\n"
210              "\n"
211              "    This program is distributed in the hope that it will be useful,\n"
212              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
213              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
214              "General Public License for more details.\n"
215              "\n"
216              "    You should have received a copy (refer to the file COPYING) of the\n"
217              "GNU General Public License along with this program; if not, write to\n"
218              "the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\n"
219              "USA.\n").to_str0 ());
220 }
221
222
223 /* Where LilyPond's init files live.  Typically:
224    LILYPOND_DATADIR = /usr/local/share/lilypond
225    LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
226 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
227
228 void
229 setup_paths ()
230 {
231   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
232     prefix_directory[1] = lilypond_prefix;
233
234 #if HAVE_GETTEXT
235   /* Enable locales */
236   setlocale (LC_ALL, "");
237   /* Mustn't have commas in TeX output... */
238   setlocale (LC_NUMERIC, "C");
239   String lily_locale_dir;
240   String name (PACKAGE);
241   name.to_lower ();
242   bindtextdomain (name.to_str0 (), LOCALEDIR);
243   textdomain (name.to_str0 ());
244 #endif
245
246   global_path.add ("");
247
248
249   /* Adding mf/out make lilypond unchanged source directory, when setting
250      LILYPONDPREFIX to lilypond-x.y.z */
251   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
252
253   for (unsigned i = 0; prefix_directory[i]; i++)
254     for (char **s = suffixes; *s; s++)
255       {
256         String p = prefix_directory[i] + to_string ('/') + String (*s);
257         global_path.prepend (p);
258         
259 #if !KPATHSEA
260         /* Urg: GNU make's $ (word) index starts at 1 */
261         int i  = 1;
262         while (global_path.try_add (p + to_string (".") + to_string (i)))
263           i++;
264 #endif
265       }
266   }
267   
268 /**
269   Make input file name from command argument.
270
271   Path describes file name with added default extension,
272   ".ly" if none.  "-" is stdin.
273  */
274 Path
275 distill_inname (String str)
276 {
277   Path p = split_path (str);
278   if (str.empty_b () || str == "-")
279     p.base = "-";
280   else
281     {
282       String orig_ext = p.ext;
283       char const *extensions[] = {"ly", "", 0};
284       for (int i = 0; extensions[i]; i++)
285         {
286           p.ext = orig_ext;
287           if (*extensions[i] && !p.ext.empty_b ())
288             p.ext += ".";
289           p.ext += extensions[i];
290           if (!global_path.find (p.to_string ()).empty_b ())
291               break;
292         }
293       /* Reshuffle extension */
294       p = split_path (p.to_string ());
295     }
296   return p;
297 }
298
299 String
300 format_to_ext (String format)
301 {
302   if (format == "tex")
303     /* .lytex change put off */
304     return "tex"; // "lytex";
305   return format;
306 }
307
308 void
309 prepend_load_path (String dir)
310 {
311   String s = "(set! %load-path (cons \""
312     + dir
313     + "\" %load-path))";
314   scm_c_eval_string (s.to_str0 ());
315 }
316
317 void
318 main_prog (void *, int, char **)
319 {
320   /* Engravers use lily.scm contents, need to make Guile find it.
321      Prepend onto GUILE %load-path, very ugh. */
322      
323   for (unsigned i = 0; prefix_directory[i]; i++)
324     {
325       prepend_load_path (prefix_directory[i]);
326
327       /* Junk this.  We should make real modules iso. just loading files. */
328       prepend_load_path (String (prefix_directory[i]) + "/scm");
329     }
330
331   if (verbose_global_b)
332     dirinfo (stderr);
333   
334   ly_init_guile ();
335   call_constructors ();
336
337   progress_indication ("\n");
338
339   all_fonts_global = new All_font_metrics (global_path.to_string ());
340
341   init_scheme_code_string += ")";
342   gh_eval_str ((char *)init_scheme_code_string.to_str0 ());
343   
344   int p=0;
345   const char *arg  = oparser_p_static->get_next_arg ();
346
347   /* Only exit until after running init_scheme_code, for
348      (ly-option-usage) or
349      -e "(ly-set-option 'help #t)" */
350   if (!arg)
351     {
352       usage ();
353       /* No FILE arguments is now a usage error */
354       exit (2);
355     }
356
357   do
358     {
359       String infile (arg);
360       Midi_def::reset_score_count ();
361       Paper_def::reset_score_count ();
362
363       Path inpath = distill_inname (infile);
364
365       /* By default, use base name of input file for output file name */
366       Path outpath = inpath;
367       if (inpath.to_string () != "-")
368         outpath.ext = format_to_ext (output_format_global);
369
370       /* By default, write output to cwd; do not copy directory part
371          of input file name */
372       outpath.root = "";
373       outpath.dir = "";
374       
375       if (!output_name_global.empty_b ())
376         outpath = split_path (output_name_global);
377       
378       String init;
379       if (!init_name_global.empty_b ())
380         init = init_name_global;
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.to_string ();
387       do_one_file (init, inpath.to_string ());
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     {
403       String combine = String (key) + "=" + String (value);
404       char * s = strdup(combine.to_str0 ());
405       return putenv (s);
406     }
407   return -1;
408 }
409
410 int
411 main (int argc, char **argv)
412 {
413   setup_paths ();
414
415   /*
416     
417     These settings hopefully prepare lily for a lot of memory usage.
418
419     In practice the effect on GC times is barely measurable -- larger
420     segments cost slighly less time for the conservative marking. (but
421     not impressively much)
422     
423   */
424   sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
425   sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
426
427   init_kpath (argv[0]);
428
429   bool help_b = false;
430   oparser_p_static = new Getopt_long (argc, argv, options_static);
431   while (Long_option_init const * opt = (*oparser_p_static) ())
432     {
433       switch (opt->shortname_char_)
434         {
435         case 'v':
436           version ();
437           exit (0);             // we print a version anyway.
438           break;
439         case 'o':
440           {
441             String s = oparser_p_static->optional_argument_str0_;
442             Path p = split_path (s);
443             if (s != "-" && p.ext.empty_b ())
444               p.ext = format_to_ext (output_format_global);
445             output_name_global = p.to_string ();
446           }
447           break;
448         case 'e':
449           init_scheme_code_string +=
450             oparser_p_static->optional_argument_str0_;
451           break;
452         case 'w':
453           notice ();
454           exit (0);
455           break;
456         case 'f':
457           if (oparser_p_static->optional_argument_str0_ == "help")
458             {
459               printf("See http://lilypond.org/wiki?OutputFormats for more information.\n"\
460                      "This option is for developers only.\n");
461               exit (0);
462             }
463           output_format_global = oparser_p_static->optional_argument_str0_;
464           break;
465         case 'P':
466             dependency_prefix_global = oparser_p_static->optional_argument_str0_;
467           break;
468         case 'H':
469           dump_header_fieldnames_global.push (oparser_p_static->optional_argument_str0_);
470           break;
471         case 'I':
472           global_path.push (oparser_p_static->optional_argument_str0_);
473           break;
474         case 'i':
475           init_name_global = oparser_p_static->optional_argument_str0_;
476           break;
477         case 'h':
478           help_b = true;
479           break;
480         case 'V':
481           verbose_global_b = true;
482           break;
483         case 's':
484           safe_global_b = true;
485           break;
486         case 'M':
487           dependency_global_b = true;
488           break; 
489         case 'm':
490           no_paper_global_b = true;
491           break;
492         default:
493           assert (false);
494           break;
495         }
496     }
497   identify (stderr);
498
499   if (help_b)
500     {
501       usage ();
502       if (verbose_global_b)
503         dirinfo (stdout);
504       exit (0);
505     }
506
507   scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
508
509   return 0;                     // unreachable
510 }