]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
* Documentation/topdocs/NEWS.texi: Add note about safe mode.
[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 Array<String> failed_files;
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? */
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;
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   {_i ("EXPR"), "evaluate", 'e',
109    _i ("set options, use -e '(ly-option-usage)' for 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")},
113   {0, "help", 'h',  _i ("print 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-mode", 's',  _i ("run in safe mode")},
122   {0, "version", 'v',  _i ("print version number")},
123   {0, "verbose", 'V', _i ("be verbose")},
124   {0, "warranty", 'w',  _i ("show warranty and copyright")},
125   {0,0,0,0}
126 };
127
128 void
129 identify (FILE *out)
130 {
131   fputs (gnu_lilypond_version_string ().to_str0 (), out);
132 }
133  
134 void
135 dirinfo (FILE *out)
136 {
137   fputs ("\n", out);
138   fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
139   fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
140   fprintf (out, "localedir: `%s'\n", LOCALEDIR);
141
142   char *lilypond_prefix = getenv ("LILYPONDPREFIX");
143   fprintf (out, "LILYPONDPREFIX: `%s'\n",
144            (lilypond_prefix ? lilypond_prefix : ""));
145 }
146
147 void
148 usage ()
149 {
150   printf ("\n");
151   /* No version number or newline here. It confuses help2man.  */
152   printf (_f ("Usage: %s [OPTIONS]... FILE...", "lilypond").to_str0 ());
153   printf ("\n\n");
154   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
155   printf ("\n\n");
156   printf(_ ("LilyPond produces beautiful music notation.\n"
157             "For more information, see http://lilypond.org/"
158 ).to_str0 ());
159
160   printf ("\n");
161   printf (_ ("Options:").to_str0 ());
162   printf ("\n");
163   printf (Long_option_init::table_string (options_static).to_str0 ());
164   printf ("\n");
165   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
166   printf ("\n");
167   printf ("\n");
168 }
169
170 void
171 version ()
172 {
173   identify (stdout);
174   printf ("\n");
175   printf (_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").to_str0 ());
180   printf ("\n");
181
182   printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
183   printf ("\n");
184   printf ("  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
185   printf ("  Jan Nieuwenhuizen <janneke@gnu.org>\n");
186 }
187
188 void
189 notice ()
190 {
191   printf ("\n");
192   printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
193   printf ("\n");
194   printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
195   printf ("\n");
196   printf ("  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
197   printf ("  Jan Nieuwenhuizen <janneke@gnu.org>\n");
198   printf ("\n");
199   printf ( _ (
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\n"
212              "USA.\n").to_str0 ());
213 }
214
215
216 /* Where LilyPond's init files live.  Typically:
217    LILYPOND_DATADIR = /usr/local/share/lilypond
218    LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
219 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
220
221 void
222 setup_paths ()
223 {
224   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
225     prefix_directory[1] = lilypond_prefix;
226
227 #if HAVE_GETTEXT
228   /* Enable locales */
229   setlocale (LC_ALL, "");
230   /* Mustn't have commas in TeX output... */
231   setlocale (LC_NUMERIC, "C");
232   String lily_locale_dir;
233   String name (PACKAGE);
234   name.to_lower ();
235   bindtextdomain (name.to_str0 (), LOCALEDIR);
236   textdomain (name.to_str0 ());
237 #endif
238
239   global_path.add ("");
240
241
242   /* Adding mf/out make lilypond unchanged source directory, when setting
243      LILYPONDPREFIX to lilypond-x.y.z */
244   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
245
246   for (unsigned i = 0; prefix_directory[i]; i++)
247     for (char **s = suffixes; *s; s++)
248       {
249         String p = prefix_directory[i] + to_string ('/') + String (*s);
250         global_path.prepend (p);
251         
252 #if !KPATHSEA
253         /* Urg: GNU make's $ (word) index starts at 1 */
254         int i  = 1;
255         while (global_path.try_add (p + to_string (".") + to_string (i)))
256           i++;
257 #endif
258       }
259   }
260   
261 /**
262   Make input file name from command argument.
263
264   Path describes file name with added default extension,
265   ".ly" if none.  "-" is stdin.
266  */
267 Path
268 distill_inname (String str)
269 {
270   Path p = split_path (str);
271   if (str.is_empty () || str == "-")
272     p.base = "-";
273   else
274     {
275       String orig_ext = p.ext;
276       char const *extensions[] = {"ly", "", 0};
277       for (int i = 0; extensions[i]; i++)
278         {
279           p.ext = orig_ext;
280           if (*extensions[i] && !p.ext.is_empty ())
281             p.ext += ".";
282           p.ext += extensions[i];
283           if (!global_path.find (p.to_string ()).is_empty ())
284               break;
285         }
286       /* Reshuffle extension */
287       p = split_path (p.to_string ());
288     }
289   return p;
290 }
291
292 void
293 prepend_load_path (String dir)
294 {
295   String s = "(set! %load-path (cons \""
296     + dir
297     + "\" %load-path))";
298   scm_c_eval_string (s.to_str0 ());
299 }
300
301 void
302 main_prog (void *, int, char **)
303 {
304   /* Engravers use lily.scm contents, need to make Guile find it.
305      Prepend onto GUILE %load-path, very ugh. */
306      
307   for (unsigned i = 0; prefix_directory[i]; i++)
308     {
309       prepend_load_path (prefix_directory[i]);
310
311       /* Junk this.  We should make real modules iso. just loading files. */
312       prepend_load_path (String (prefix_directory[i]) + "/scm");
313     }
314
315   if (verbose_global_b)
316     dirinfo (stderr);
317   
318   ly_init_guile ();
319   call_constructors ();
320
321   progress_indication ("\n");
322
323   all_fonts_global = new All_font_metrics (global_path.to_string ());
324
325   init_scheme_code_string += ")";
326   gh_eval_str ((char *)init_scheme_code_string.to_str0 ());
327   
328   int p=0;
329   const char *arg  = oparser_p_static->get_next_arg ();
330
331   /* Only exit until after running init_scheme_code, for
332      (ly-option-usage) or
333      -e "(ly-set-option 'help #t)" */
334   if (!arg)
335     {
336       usage ();
337       /* No FILE arguments is now a usage error */
338       exit (2);
339     }
340
341   do
342     {
343       String infile (arg);
344       Path inpath = distill_inname (infile);
345
346       /* By default, use base name of input file for output file name */
347       Path outpath = inpath;
348       if (inpath.to_string () != "-")
349         outpath.ext = output_format_global;
350
351       /* By default, write output to cwd; do not copy directory part
352          of input file name */
353       outpath.root = "";
354       outpath.dir = "";
355       
356       if (!output_name_global.is_empty ())
357         outpath = split_path (output_name_global);
358       
359       String init;
360       if (!init_name_global.is_empty ())
361         init = init_name_global;
362       else
363         init = "init.ly";
364         
365       do_one_file (init, inpath.to_string (), outpath.to_string ());
366       
367       p++;
368     } while ((arg  = oparser_p_static->get_next_arg ()));
369   delete oparser_p_static;
370   oparser_p_static = 0;
371
372   if (exit_status_global)
373     {
374       printf ("Failed files: ");
375       for (int i = 0; i < failed_files.size (); i++)
376         printf ("%s", failed_files[i].to_str0 ());
377     }
378   exit (exit_status_global);
379 }
380
381
382 static int
383 sane_putenv (char const* key, char const* value, bool overwrite)
384 {
385   if (overwrite || !getenv (key))
386     {
387       String combine = String (key) + "=" + String (value);
388       char * s = strdup(combine.to_str0 ());
389       return putenv (s);
390     }
391   return -1;
392 }
393
394 int
395 main (int argc, char **argv)
396 {
397   setup_paths ();
398
399   /*
400     
401     These settings hopefully prepare lily for a lot of memory usage.
402
403     In practice the effect on GC times is barely measurable -- larger
404     segments cost slighly less time for the conservative marking. (but
405     not impressively much)
406     
407   */
408   sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
409   sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
410
411   init_kpath (argv[0]);
412
413   bool help_b = false;
414   oparser_p_static = new Getopt_long (argc, argv, options_static);
415   while (Long_option_init const * opt = (*oparser_p_static) ())
416     {
417       switch (opt->shortname_char_)
418         {
419         case 'v':
420           version ();
421           exit (0);             // we print a version anyway.
422           break;
423         case 'o':
424           {
425             String s = oparser_p_static->optional_argument_str0_;
426             Path p = split_path (s);
427             if (s != "-" && p.ext.is_empty ())
428               p.ext = output_format_global;
429
430             output_name_global = p.to_string ();
431           }
432           break;
433         case 'e':
434           init_scheme_code_string +=
435             oparser_p_static->optional_argument_str0_;
436           break;
437         case 'w':
438           notice ();
439           exit (0);
440           break;
441         case 'f':
442           if (oparser_p_static->optional_argument_str0_ == "help")
443             {
444               printf("This option is for developers only;  Read the source code for more information\n");
445               
446               // actually it's not here,
447               // but we award you special points for looking here.
448               
449               exit (0);
450             }
451           output_format_global = oparser_p_static->optional_argument_str0_;
452           break;
453         case 'P':
454             dependency_prefix_global = oparser_p_static->optional_argument_str0_;
455           break;
456         case 'H':
457           dump_header_fieldnames_global.push (oparser_p_static->optional_argument_str0_);
458           break;
459         case 'I':
460           global_path.push (oparser_p_static->optional_argument_str0_);
461           break;
462         case 'i':
463           init_name_global = oparser_p_static->optional_argument_str0_;
464           break;
465         case 'h':
466           help_b = true;
467           break;
468         case 'V':
469           verbose_global_b = true;
470           break;
471         case 's':
472           safe_global_b = true;
473           break;
474         case 'M':
475           dependency_global_b = true;
476           break; 
477         case 'm':
478           no_paper_global_b = true;
479           break;
480         default:
481           assert (false);
482           break;
483         }
484     }
485
486   if (help_b)
487     {
488       usage ();
489       if (verbose_global_b)
490         dirinfo (stdout);
491       exit (0);
492     }
493
494   scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
495
496   return 0;                     // unreachable
497 }