2 main.cc -- implement main () entrypoint.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
21 #include "lily-guile.hh"
22 #include "lily-version.hh"
23 #include "all-font-metrics.hh"
24 #include "getopt-long.hh"
28 #include "file-path.hh"
29 #include "input-file-results.hh"
31 #include "lily-guile.hh"
32 #include "paper-def.hh"
33 #include "midi-def.hh"
34 #include "global-ctor.hh"
37 Array<String> failed_files;
40 * Global options that can be overridden through command line.
43 /* Write dependencies file? */
44 bool dependency_global_b = false;
46 /* Prepend to dependencies */
47 String dependency_prefix_global;
49 /* Names of header fields to be dumped to a separate file. */
50 Array<String> dump_header_fieldnames_global;
52 /* Name of initialisation file. */
53 String init_name_global;
55 /* Do not calculate and write paper output? */
56 bool no_paper_global_b = false;
58 /* Selected output format.
59 One of tex, ps, scm, as. */
60 String output_format_global = "tex";
62 /* Current output name. */
63 String output_name_global;
65 /* Run in safe mode? */
66 bool safe_global_b = false;
68 /* Verbose progress indication? */
69 bool verbose_global_b = false;
71 /* Scheme code to execute before parsing, after .scm init */
72 String init_scheme_code_string = "(begin #t ";
76 * Miscellaneous global stuff.
79 All_font_metrics *all_fonts_global;
80 int exit_status_global;
81 File_path global_path;
83 /* Number of current score output block. If there's more than one
84 score block, this counter will be added to the output filename. */
85 int score_count_global;
92 static char const *AUTHORS =
93 " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
94 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
96 static char const *PROGRAM_NAME = "lilypond-bin";
97 static char const *PROGRAM_URL = "http://lilypond.org";
99 static char const *NOTICE =
100 _i ("This program is free software. It is covered by the GNU General Public\n"
101 "License and you are welcome to change it and/or distribute copies of it\n"
102 "under certain conditions. Invoke as `lilypond-bin --warranty' for more\n"
105 static char const *WARRANTY =
106 _i (" This program is free software; you can redistribute it and/or\n"
107 "modify it under the terms of the GNU General Public License version 2\n"
108 "as published by the Free Software Foundation.\n"
110 " This program is distributed in the hope that it will be useful,\n"
111 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
112 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
113 "General Public License for more details.\n"
115 " You should have received a copy (refer to the file COPYING) of the\n"
116 "GNU General Public License along with this program; if not, write to\n"
117 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
118 "Boston, MA 02111-1307, USA.\n");
121 /* Where the init files live. Typically:
122 LILYPOND_DATADIR = /usr/share/lilypond
123 LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
124 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
126 /* The option parser */
127 static Getopt_long *option_parser = 0;
129 /* Internationalisation kludge in two steps:
130 * use _i () to get entry in POT file
131 * call gettext () explicitely for actual "translation" */
133 static Long_option_init options_static[] =
135 {_i ("EXPR"), "evaluate", 'e',
136 _i ("set options, use -e '(ly-option-usage)' for help")},
137 /* Bug in option parser: --output=foe is taken as an abbreviation
138 for --output-format. */
139 {_i ("EXT"), "format", 'f', _i ("use output format EXT")},
140 {0, "help", 'h', _i ("print this help")},
141 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
142 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
143 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
144 {0, "dependencies", 'M', _i ("write Makefile dependencies")},
145 {0, "no-paper", 'm', _i ("produce MIDI output only")},
146 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
147 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
148 {0, "safe-mode", 's', _i ("run in safe mode")},
149 {0, "version", 'v', _i ("print version number")},
150 {0, "verbose", 'V', _i ("be verbose")},
151 {0, "warranty", 'w', _i ("show warranty and copyright")},
159 fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
160 fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
161 fprintf (out, "localedir: `%s'\n", LOCALEDIR);
163 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
164 fprintf (out, "LILYPONDPREFIX: `%s'\n",
165 (lilypond_prefix ? lilypond_prefix : ""));
171 printf (_f ("Copyright (c) %s by\n%s and others.",
173 AUTHORS).to_str0 ());
180 fputs (gnu_lilypond_version_string ().to_str0 (), out);
188 printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
196 /* No version number or newline here. It confuses help2man. */
197 printf (_f ("Usage: %s [OPTIONS]... FILE...", PROGRAM_NAME).to_str0 ());
199 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
201 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
203 printf (_f ("For more information, see %s,", PROGRAM_URL).to_str0 ());
205 printf (_ ("Options:").to_str0 ());
207 printf (Long_option_init::table_string (options_static).to_str0 ());
209 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
221 printf (_ (WARRANTY).to_str0 ());
227 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
228 prefix_directory[1] = lilypond_prefix;
230 global_path.add ("");
232 /* Adding mf/out make lilypond unchanged source directory, when setting
233 LILYPONDPREFIX to lilypond-x.y.z */
234 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
236 for (unsigned i = 0; prefix_directory[i]; i++)
237 for (char **s = suffixes; *s; s++)
239 String p = prefix_directory[i] + to_string ('/') + String (*s);
240 global_path.prepend (p);
243 /* Urg: GNU make's $ (word) index starts at 1 */
245 while (global_path.try_add (p + to_string (".") + to_string (i)))
252 prepend_load_path (String dir)
254 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
255 scm_c_eval_string (s.to_str0 ());
259 main_with_guile (void *, int, char **)
261 /* Engravers use lily.scm contents, need to make Guile find it.
262 Prepend onto GUILE %load-path, very ugh. */
263 for (unsigned i = 0; prefix_directory[i]; i++)
265 prepend_load_path (prefix_directory[i]);
266 /* Junk this. We should make real modules iso. just loading files. */
267 prepend_load_path (String (prefix_directory[i]) + "/scm");
270 if (verbose_global_b)
274 call_constructors ();
275 progress_indication ("\n");
277 all_fonts_global = new All_font_metrics (global_path.to_string ());
279 init_scheme_code_string += ")";
280 gh_eval_str ((char*) init_scheme_code_string.to_str0 ());
282 /* We accept multiple independent music files on the command line to
283 reduce compile time when processing lots of small files.
284 Starting the GUILE engine is very time consuming. */
286 while (char const *arg = option_parser->get_next_arg ())
290 /* Code to debug memory leaks. Cannot call from within .ly
291 since then we get the protects from the parser state too. */
293 scm_call_0 (ly_scheme_function ("dump-gc-protects"));
299 delete option_parser;
302 /* No FILE arguments is now a usage error to help newbies. If you
303 want a filter, you're not a newbie and should know to use file
311 if (exit_status_global)
313 printf ("Failed files: ");
314 for (int i = 0; i < failed_files.size (); i++)
315 printf ("%s ", failed_files[i].to_str0 ());
318 exit (exit_status_global);
322 setup_localisation ()
326 setlocale (LC_ALL, "");
328 /* FIXME: check if this is still true.
329 Disable localisation of float values. This breaks TeX output. */
330 setlocale (LC_NUMERIC, "C");
332 String name (PACKAGE);
334 bindtextdomain (name.to_str0 (), LOCALEDIR);
335 textdomain (name.to_str0 ());
340 parse_argv (int argc, char **argv)
343 option_parser = new Getopt_long (argc, argv, options_static);
344 while (Long_option_init const * opt = (*option_parser) ())
346 switch (opt->shortname_char_)
354 String s = option_parser->optional_argument_str0_;
355 Path p = split_path (s);
356 if (s != "-" && p.ext.is_empty ())
357 p.ext = output_format_global;
358 output_name_global = p.to_string ();
362 init_scheme_code_string += option_parser->optional_argument_str0_;
369 if (option_parser->optional_argument_str0_ == "help")
371 printf (_ ("This option is for developers only.").to_str0 ());
372 printf (_ ("Read the sources for more information.").to_str0 ());
375 output_format_global = option_parser->optional_argument_str0_;
378 dependency_prefix_global = option_parser->optional_argument_str0_;
381 dump_header_fieldnames_global
382 .push (option_parser->optional_argument_str0_);
385 global_path.push (option_parser->optional_argument_str0_);
388 init_name_global = option_parser->optional_argument_str0_;
394 verbose_global_b = true;
397 safe_global_b = true;
400 dependency_global_b = true;
403 no_paper_global_b = true;
414 if (verbose_global_b)
421 main (int argc, char **argv)
423 setup_localisation ();
425 parse_argv (argc, argv);
426 initialize_kpathsea (argv[0]);
428 scm_boot_guile (argc, argv, main_with_guile, 0);