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 "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 no_layout_global_b = false;
46 /* Selected output format.
47 One of tex, ps, scm, as.
49 String output_backend_global = "ps";
50 String output_format_global = "pdf";
52 /* Current output name. */
53 String output_name_global;
55 /* Run in safe mode? */
56 bool safe_global_b = false;
58 /* Verbose progress indication? */
59 bool verbose_global_b = false;
61 /* Scheme code to execute before parsing, after .scm init
62 This is where -e arguments are appended to.
64 String init_scheme_code_string = "(begin #t ";
66 bool make_preview = false;
67 bool make_pages = true;
71 * Miscellaneous global stuff.
73 File_path global_path;
80 static char const *AUTHORS =
81 " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
82 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
84 static char const *PROGRAM_NAME = "lilypond";
85 static char const *PROGRAM_URL = "http://lilypond.org";
87 static char const *NOTICE =
88 _i ("This program is free software. It is covered by the GNU General Public\n"
89 "License and you are welcome to change it and/or distribute copies of it\n"
90 "under certain conditions. Invoke as `lilypond --warranty' for more\n"
93 static char const *WARRANTY =
94 _i (" This program is free software; you can redistribute it and/or\n"
95 "modify it under the terms of the GNU General Public License version 2\n"
96 "as published by the Free Software Foundation.\n"
98 " This program is distributed in the hope that it will be useful,\n"
99 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
100 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
101 "General Public License for more details.\n"
103 " You should have received a copy (refer to the file COPYING) of the\n"
104 "GNU General Public License along with this program; if not, write to\n"
105 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
106 "Boston, MA 02111-1307, USA.\n");
109 /* Where the init files live. Typically:
110 LILYPOND_DATADIR = /usr/share/lilypond
111 LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
112 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
114 /* The option parser */
115 static Getopt_long *option_parser = 0;
117 /* Internationalisation kludge in two steps:
118 * use _i () to get entry in POT file
119 * call gettext () explicitely for actual "translation" */
121 static Long_option_init options_static[] =
123 {_i ("EXPR"), "evaluate", 'e',
124 _i ("set option, use -e '(ly:option-usage)' for help")},
125 /* Bug in option parser: --output =foe is taken as an abbreviation
126 for --output-format. */
127 {_i ("EXT"), "backend", 'b', _i ("select back-end to use")},
128 {_i ("EXTs"), "format", 'f', _i ("list of formats to dump")},
129 {0, "help", 'h', _i ("print this help")},
130 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
131 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
132 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
133 {0, "no-layout", 'm', _i ("produce MIDI output only")},
134 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
135 {0, "preview", 'p', _i ("generate a preview")},
136 {0, "no-pages", 0, _i ("don't generate full pages")},
137 {0, "png", 0, _i ("generate PNG")},
138 {0, "ps", 0, _i ("generate PostScript")},
139 {0, "dvi", 0, _i ("generate DVI")},
140 {0, "pdf", 0, _i ("generate PDF (default)")},
141 {0, "tex", 0, _i ("generate TeX")},
142 {0, "safe-mode", 's', _i ("run in safe mode")},
143 {0, "version", 'v', _i ("print version number")},
144 {0, "verbose", 'V', _i ("be verbose")},
145 {0, "warranty", 'w', _i ("show warranty and copyright")},
153 fprintf (out, "LILYPOND_DATADIR =\"%s\"\n", LILYPOND_DATADIR);
154 fprintf (out, "LOCAL_LILYPOND_DATADIR =\"\%s\"\n", LOCAL_LILYPOND_DATADIR);
155 fprintf (out, "LOCALEDIR =\"%s\"\n", LOCALEDIR);
157 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
158 fprintf (out, "LILYPONDPREFIX =\"%s\"\n",
159 (lilypond_prefix ? lilypond_prefix : ""));
165 printf (_f ("Copyright (c) %s by\n%s and others.",
167 AUTHORS).to_str0 ());
174 fputs (gnu_lilypond_version_string ().to_str0 (), out);
182 printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
190 /* No version number or newline here. It confuses help2man. */
191 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
193 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
195 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
197 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
199 printf (_ ("Options:").to_str0 ());
201 printf (Long_option_init::table_string (options_static).to_str0 ());
203 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
215 printf (_ (WARRANTY).to_str0 ());
221 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
222 prefix_directory[1] = lilypond_prefix;
224 global_path.append ("");
226 /* Adding mf/out make lilypond unchanged source directory, when setting
227 LILYPONDPREFIX to lilypond-x.y.z */
228 char *suffixes[] = {"ly", "otf", "mf/out", "scm", "tfm", "ps", "svg", 0};
230 for (unsigned i = 0; prefix_directory[i]; i++)
231 for (char **s = suffixes; *s; s++)
233 String p = prefix_directory[i] + to_string ('/') + String (*s);
234 global_path.prepend (p);
237 /* Urg: GNU make's $ (word) index starts at 1 */
239 while (global_path.try_append (p + to_string (".") + to_string (i)))
246 prepend_load_path (String dir)
248 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
249 scm_c_eval_string (s.to_str0 ());
252 void init_global_tweak_registry ();
255 main_with_guile (void *, int, char **)
257 /* Engravers use lily.scm contents, need to make Guile find it.
258 Prepend onto GUILE %load-path, very ugh. */
259 for (unsigned i = 0; prefix_directory[i]; i++)
261 prepend_load_path (prefix_directory[i]);
262 /* Junk this. We should make real modules iso. just loading files. */
263 prepend_load_path (String (prefix_directory[i]) + "/scm");
266 if (verbose_global_b)
270 call_constructors ();
271 init_global_tweak_registry ();
273 all_fonts_global = new All_font_metrics (global_path.to_string ());
275 init_scheme_code_string += ")";
276 scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
278 /* We accept multiple independent music files on the command line to
279 reduce compile time when processing lots of small files.
280 Starting the GUILE engine is very time consuming. */
284 while (char const *arg = option_parser->get_next_arg ())
286 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
287 tail = SCM_CDRLOC (*tail);
289 delete option_parser;
292 if (files == SCM_EOL)
294 /* No FILE arguments is now a usage error to help newbies. If you
295 want a filter, you're not a newbie and should know to use file
301 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
310 setup_localisation ()
314 setlocale (LC_ALL, "");
316 /* FIXME: check if this is still true.
317 Disable localisation of float values. This breaks TeX output. */
318 setlocale (LC_NUMERIC, "C");
320 String name (PACKAGE);
322 bindtextdomain (name.to_str0 (), LOCALEDIR);
323 textdomain (name.to_str0 ());
328 add_output_format (String format)
330 if (output_name_global != "")
331 output_name_global += ",";
332 output_name_global += format;
337 parse_argv (int argc, char **argv)
340 option_parser = new Getopt_long (argc, argv, options_static);
341 while (Long_option_init const *opt = (*option_parser) ())
343 switch (opt->shortname_char_)
346 if (String (opt->longname_str0_) == "png"
347 || String (opt->longname_str0_) == "pdf"
348 || String (opt->longname_str0_) == "ps"
349 || String (opt->longname_str0_) == "dvi"
350 || String (opt->longname_str0_) == "tex")
352 add_output_format (opt->longname_str0_);
354 else if (String (opt->longname_str0_) == "preview")
356 else if (String (opt->longname_str0_) == "no-pages")
366 String s = option_parser->optional_argument_str0_;
367 File_name file_name (s);
368 output_name_global = file_name.to_string ();
372 init_scheme_code_string += option_parser->optional_argument_str0_;
379 output_backend_global = option_parser->optional_argument_str0_;
383 output_format_global = option_parser->optional_argument_str0_;
387 dump_header_fieldnames_global
388 .push (option_parser->optional_argument_str0_);
391 global_path.append (option_parser->optional_argument_str0_);
394 init_name_global = option_parser->optional_argument_str0_;
400 verbose_global_b = true;
403 safe_global_b = true;
406 no_layout_global_b = true;
412 programming_error (to_string ("unhandled short option: %c",
413 opt->shortname_char_));
423 if (verbose_global_b)
430 main (int argc, char **argv)
432 setup_localisation ();
434 parse_argv (argc, argv);
436 initialize_kpathsea (argv[0]);
438 scm_boot_guile (argc, argv, main_with_guile, 0);