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