2 main.cc -- implement main () entrypoint.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
21 #include "all-font-metrics.hh"
22 #include "file-name.hh"
23 #include "getopt-long.hh"
24 #include "global-ctor.hh"
26 #include "lily-guile.hh"
27 #include "lily-version.hh"
29 #include "output-def.hh"
31 #include "freetype.hh"
34 * Global options that can be overridden through command line.
37 /* Names of header fields to be dumped to a separate file. */
38 Array<String> dump_header_fieldnames_global;
40 /* Name of initialisation file. */
41 String init_name_global;
43 /* Do not calculate and write layout output? */
44 bool skip_layout_global = false;
46 /* Selected output format.
47 One of tex, ps, scm, as.
49 String output_backend_global = "ps";
50 String output_format_global = "pdf";
51 bool is_pango_format_global;
53 /* Current output name. */
54 String output_name_global;
56 /* Run in safe mode? */
57 bool be_safe_global = false;
59 /* Verbose progress indication? */
60 bool be_verbose_global = false;
62 /* Scheme code to execute before parsing, after .scm init
63 This is where -e arguments are appended to.
65 String init_scheme_code_string = "(begin #t ";
67 bool make_preview = false;
68 bool make_pages = true;
72 * Miscellaneous global stuff.
74 File_path global_path;
81 static char const *AUTHORS =
82 " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
83 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
85 static char const *PROGRAM_NAME = "lilypond";
86 static char const *PROGRAM_URL = "http://lilypond.org";
88 static char const *NOTICE =
89 _i ("This program is free software. It is covered by the GNU General Public\n"
90 "License and you are welcome to change it and/or distribute copies of it\n"
91 "under certain conditions. Invoke as `lilypond --warranty' for more\n"
94 static char const *WARRANTY =
95 _i (" This program is free software; you can redistribute it and/or\n"
96 "modify it under the terms of the GNU General Public License version 2\n"
97 "as published by the Free Software Foundation.\n"
99 " This program is distributed in the hope that it will be useful,\n"
100 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
101 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
102 "General Public License for more details.\n"
104 " You should have received a copy (refer to the file COPYING) of the\n"
105 "GNU General Public License along with this program; if not, write to\n"
106 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
107 "Boston, MA 02111-1307, USA.\n");
110 /* Where the init files live. Typically:
111 LILYPOND_DATADIR = /usr/share/lilypond
112 LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
113 char const *prefix_directories[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
115 /* The option parser */
116 static Getopt_long *option_parser = 0;
118 /* Internationalisation kludge in two steps:
119 * use _i () to get entry in POT file
120 * call gettext () explicitely for actual "translation" */
122 static Long_option_init options_static[] =
124 {_i ("EXT"), "backend", 'b', _i ("select backend to use")},
125 {_i ("EXPR"), "evaluate", 'e',
126 _i ("set option, use -e '(ly:option-usage)' for help")},
127 /* Bug in option parser: --output =foe is taken as an abbreviation
128 for --output-format. */
129 {_i ("EXTs"), "formats", 'f', _i ("list of formats to dump")},
130 {0, "help", 'h', _i ("print this help")},
131 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
132 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
133 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
134 {0, "no-layout", 'm', _i ("produce MIDI output only")},
135 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
136 {0, "preview", 'p', _i ("generate a preview")},
137 {0, "no-pages", 0, _i ("don't generate full pages")},
138 {0, "png", 0, _i ("generate PNG")},
139 {0, "ps", 0, _i ("generate PostScript")},
140 {0, "dvi", 0, _i ("generate DVI")},
141 {0, "pdf", 0, _i ("generate PDF (default)")},
142 {0, "tex", 0, _i ("generate TeX")},
143 {0, "safe-mode", 's', _i ("run in safe mode")},
144 {0, "version", 'v', _i ("print version number")},
145 {0, "verbose", 'V', _i ("be verbose")},
146 {0, "warranty", 'w', _i ("show warranty and copyright")},
154 fprintf (out, "LILYPOND_DATADIR =\"%s\"\n", LILYPOND_DATADIR);
155 fprintf (out, "LOCAL_LILYPOND_DATADIR =\"\%s\"\n", LOCAL_LILYPOND_DATADIR);
156 fprintf (out, "LOCALEDIR =\"%s\"\n", LOCALEDIR);
158 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
159 fprintf (out, "LILYPONDPREFIX =\"%s\"\n",
160 (lilypond_prefix ? lilypond_prefix : ""));
166 printf (_f ("Copyright (c) %s by\n%s and others.",
168 AUTHORS).to_str0 ());
175 fputs (gnu_lilypond_version_string ().to_str0 (), out);
183 printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
191 /* No version number or newline here. It confuses help2man. */
192 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
194 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
196 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
198 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
200 printf (_ ("Options:").to_str0 ());
202 printf (Long_option_init::table_string (options_static).to_str0 ());
204 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
216 printf (_ (WARRANTY).to_str0 ());
222 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
223 prefix_directories[1] = lilypond_prefix;
225 global_path.append ("");
227 /* Adding mf/out make lilypond unchanged source directory, when setting
228 LILYPONDPREFIX to lilypond-x.y.z */
229 char *suffixes[] = {"ly", "cff", "otf", "mf/out", "scm", "tfm", "ps", "svg",
232 for (unsigned i = 0; prefix_directories[i]; i++)
233 for (char **s = suffixes; *s; s++)
235 String p = prefix_directories[i] + to_string ('/') + String (*s);
236 global_path.prepend (p);
239 /* Urg: GNU make's $ (word) index starts at 1 */
241 while (global_path.try_append (p + to_string (".") + to_string (i)))
248 prepend_load_path (String dir)
250 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
251 scm_c_eval_string (s.to_str0 ());
254 void init_global_tweak_registry ();
255 void init_fontconfig ();
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_directories[i]; i++)
265 prepend_load_path (prefix_directories[i]);
266 /* Junk this. We should make real modules iso. just loading files. */
267 prepend_load_path (String (prefix_directories[i]) + "/scm");
270 if (be_verbose_global)
274 call_constructors ();
275 init_global_tweak_registry ();
279 is_pango_format_global = (output_backend_global != "tex"
280 && output_backend_global != "texstr");
282 all_fonts_global = new All_font_metrics (global_path.to_string ());
284 init_scheme_code_string += ")";
285 scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
287 /* We accept multiple independent music files on the command line to
288 reduce compile time when processing lots of small files.
289 Starting the GUILE engine is very time consuming. */
293 while (char const *arg = option_parser->get_next_arg ())
295 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
296 tail = SCM_CDRLOC (*tail);
298 delete option_parser;
301 if (files == SCM_EOL)
303 /* No FILE arguments is now a usage error to help newbies. If you
304 want a filter, you're not a newbie and should know to use file
310 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
319 setup_localisation ()
323 setlocale (LC_ALL, "");
325 /* FIXME: check if this is still true.
326 Disable localisation of float values. This breaks TeX output. */
327 setlocale (LC_NUMERIC, "C");
329 String name (PACKAGE);
331 bindtextdomain (name.to_str0 (), LOCALEDIR);
332 textdomain (name.to_str0 ());
337 add_output_format (String format)
339 if (output_format_global != "")
340 output_format_global += ", ";
341 output_format_global += format;
346 parse_argv (int argc, char **argv)
349 option_parser = new Getopt_long (argc, argv, options_static);
350 while (Long_option_init const *opt = (*option_parser) ())
352 switch (opt->shortname_char_)
355 if (String (opt->longname_str0_) == "png"
356 || String (opt->longname_str0_) == "pdf"
357 || String (opt->longname_str0_) == "ps"
358 || String (opt->longname_str0_) == "dvi"
359 || String (opt->longname_str0_) == "tex")
361 add_output_format (opt->longname_str0_);
363 else if (String (opt->longname_str0_) == "preview")
365 else if (String (opt->longname_str0_) == "no-pages")
375 String s = option_parser->optional_argument_str0_;
376 File_name file_name (s);
377 output_name_global = file_name.to_string ();
381 init_scheme_code_string += option_parser->optional_argument_str0_;
389 output_backend_global = option_parser->optional_argument_str0_;
393 output_format_global = option_parser->optional_argument_str0_;
397 dump_header_fieldnames_global
398 .push (option_parser->optional_argument_str0_);
401 global_path.append (option_parser->optional_argument_str0_);
404 init_name_global = option_parser->optional_argument_str0_;
410 be_verbose_global = true;
413 be_safe_global = true;
416 skip_layout_global = true;
422 programming_error (to_string ("unhandled short option: %c",
423 opt->shortname_char_));
433 if (be_verbose_global)
440 main (int argc, char **argv)
442 setup_localisation ();
444 parse_argv (argc, argv);
446 initialize_kpathsea (argv[0]);
448 scm_boot_guile (argc, argv, main_with_guile, 0);