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 "file-path.hh"
24 #include "getopt-long.hh"
25 #include "global-ctor.hh"
27 #include "lily-guile.hh"
28 #include "lily-version.hh"
31 #include "output-def.hh"
36 * Global options that can be overridden through command line.
39 /* Names of header fields to be dumped to a separate file. */
40 Array<String> dump_header_fieldnames_global;
42 /* Name of initialisation file. */
43 String init_name_global;
45 /* Do not calculate and write paper output? */
46 bool no_paper_global_b = false;
48 /* Selected output format.
49 One of tex, ps, scm, as. */
50 String output_format_global = "ps";
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_pdf = false;
67 bool make_dvi = false;
69 bool make_png = false;
70 bool make_preview = false;
71 bool make_tex = false;
74 * Miscellaneous global stuff.
76 File_path global_path;
83 static char const *AUTHORS =
84 " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
85 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
87 static char const *PROGRAM_NAME = "lilypond";
88 static char const *PROGRAM_URL = "http://lilypond.org";
90 static char const *NOTICE =
91 _i ("This program is free software. It is covered by the GNU General Public\n"
92 "License and you are welcome to change it and/or distribute copies of it\n"
93 "under certain conditions. Invoke as `lilypond --warranty' for more\n"
96 static char const *WARRANTY =
97 _i (" This program is free software; you can redistribute it and/or\n"
98 "modify it under the terms of the GNU General Public License version 2\n"
99 "as published by the Free Software Foundation.\n"
101 " This program is distributed in the hope that it will be useful,\n"
102 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
103 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
104 "General Public License for more details.\n"
106 " You should have received a copy (refer to the file COPYING) of the\n"
107 "GNU General Public License along with this program; if not, write to\n"
108 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
109 "Boston, MA 02111-1307, USA.\n");
112 /* Where the init files live. Typically:
113 LILYPOND_DATADIR = /usr/share/lilypond
114 LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
115 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
117 /* The option parser */
118 static Getopt_long *option_parser = 0;
120 /* Internationalisation kludge in two steps:
121 * use _i () to get entry in POT file
122 * call gettext () explicitely for actual "translation" */
124 static Long_option_init options_static[] =
126 {_i ("EXPR"), "evaluate", 'e',
127 _i ("set options, use -e '(ly:option-usage)' for help")},
128 /* Bug in option parser: --output=foe is taken as an abbreviation
129 for --output-format. */
130 {_i ("EXT"), "format", 'f', _i ("select back-end to use")},
131 {0, "help", 'h', _i ("print this help")},
132 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
133 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
134 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
135 {0, "no-paper", 'm', _i ("produce MIDI output only")},
136 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
137 {0, "preview", 'p', _i ("generate a preview")},
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 [OPTIONS]... 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_directory[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", "afm", "mf/out", "scm", "tfm", "ps", 0};
231 for (unsigned i = 0; prefix_directory[i]; i++)
232 for (char **s = suffixes; *s; s++)
234 String p = prefix_directory[i] + to_string ('/') + String (*s);
235 global_path.prepend (p);
238 /* Urg: GNU make's $ (word) index starts at 1 */
240 while (global_path.try_append (p + to_string (".") + to_string (i)))
247 prepend_load_path (String dir)
249 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
250 scm_c_eval_string (s.to_str0 ());
254 determine_output_options ()
257 bool found_tex = false;
258 SCM formats = ly_output_formats ();
259 for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s))
261 found_tex = found_tex || (ly_scm2string (ly_car (s)) == "tex");
265 if (make_ps && found_tex)
269 if (make_pdf || make_png)
273 if (make_dvi && found_tex)
294 main_with_guile (void *, int, char **)
296 /* Engravers use lily.scm contents, need to make Guile find it.
297 Prepend onto GUILE %load-path, very ugh. */
298 for (unsigned i = 0; prefix_directory[i]; i++)
300 prepend_load_path (prefix_directory[i]);
301 /* Junk this. We should make real modules iso. just loading files. */
302 prepend_load_path (String (prefix_directory[i]) + "/scm");
305 if (verbose_global_b)
309 call_constructors ();
310 progress_indication ("\n");
312 determine_output_options ();
313 all_fonts_global = new All_font_metrics (global_path.to_string ());
315 init_scheme_code_string += ")";
316 scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
318 /* We accept multiple independent music files on the command line to
319 reduce compile time when processing lots of small files.
320 Starting the GUILE engine is very time consuming. */
324 while (char const *arg = option_parser->get_next_arg ())
326 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
327 tail = SCM_CDRLOC (*tail);
329 delete option_parser;
332 if (files == SCM_EOL)
334 /* No FILE arguments is now a usage error to help newbies. If you
335 want a filter, you're not a newbie and should know to use file
341 SCM result = scm_call_1 (ly_scheme_function ("lilypond-main"), files);
350 setup_localisation ()
354 setlocale (LC_ALL, "");
356 /* FIXME: check if this is still true.
357 Disable localisation of float values. This breaks TeX output. */
358 setlocale (LC_NUMERIC, "C");
360 String name (PACKAGE);
362 bindtextdomain (name.to_str0 (), LOCALEDIR);
363 textdomain (name.to_str0 ());
368 parse_argv (int argc, char **argv)
371 option_parser = new Getopt_long (argc, argv, options_static);
372 while (Long_option_init const *opt = (*option_parser) ())
374 switch (opt->shortname_char_)
377 if (String (opt->longname_str0_) == "png")
379 else if (String (opt->longname_str0_) == "pdf")
381 else if (String (opt->longname_str0_) == "ps")
383 else if (String (opt->longname_str0_) == "dvi")
385 else if (String (opt->longname_str0_) == "preview")
395 String s = option_parser->optional_argument_str0_;
396 File_name file_name (s);
397 output_name_global = file_name.to_string ();
401 init_scheme_code_string += option_parser->optional_argument_str0_;
408 if (option_parser->optional_argument_str0_ == "help")
410 printf (_ ("This option is for developers only.").to_str0 ());
411 printf (_ ("Read the sources for more information.").to_str0 ());
414 output_format_global = option_parser->optional_argument_str0_;
417 dump_header_fieldnames_global
418 .push (option_parser->optional_argument_str0_);
421 global_path.append (option_parser->optional_argument_str0_);
424 init_name_global = option_parser->optional_argument_str0_;
430 verbose_global_b = true;
433 safe_global_b = true;
436 no_paper_global_b = true;
442 programming_error (to_string ("unhandled short option: %c",
443 opt->shortname_char_));
452 if (verbose_global_b)
459 main (int argc, char **argv)
461 setup_localisation ();
463 parse_argv (argc, argv);
464 initialize_kpathsea (argv[0]);
466 scm_boot_guile (argc, argv, main_with_guile, 0);