2 main.cc -- implement main: entrypoints
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
22 #include "lily-guile.hh"
23 #include "lily-version.hh"
24 #include "all-font-metrics.hh"
25 #include "getopt-long.hh"
29 #include "file-path.hh"
30 #include "file-results.hh"
32 #include "lily-guile.hh"
33 #include "paper-def.hh"
34 #include "midi-def.hh"
35 #include "global-ctor.hh"
38 static int sane_putenv (char const* key, char const* value, bool overwrite = false);
41 Global options that can be overridden through command line.
44 /* Write dependencies file? */
45 bool dependency_global_b = false;
47 /* Prepend to dependencies */
48 String dependency_prefix_global;
50 /* Names of header fields to be dumped to a separate file. */
51 Array<String> dump_header_fieldnames_global;
53 /* Name of initialisation file. */
54 String init_name_global;
56 /* Do not calculate and write paper output? */
57 bool no_paper_global_b = false;
59 /* Selected output format.
60 One of tex, ps, scm, as. */
61 String output_format_global = "tex";
63 /* Current output name. */
64 String output_name_global;
66 /* Run in safe mode? -- FIXME: should be re-analised */
67 bool safe_global_b = false;
69 /* Verbose progress indication? */
70 bool verbose_global_b = false;
72 /* Scheme code to execute before parsing, after .scm init */
73 String init_scheme_code_string = "(begin #t ";
81 All_font_metrics *all_fonts_global_p;
82 int exit_status_global;
83 File_path global_path;
85 /* Number of current score output block. If there's more than one
86 score block, this counter will be added to the output filename. */
87 int score_count_global;
95 /* The option parser */
96 static Getopt_long *oparser_p_static = 0;
99 Internationalisation kludge in two steps:
100 * use _i () to get entry in POT file
101 * call gettext () explicitely for actual "translation"
103 Note: these messages all start with lower case (ie, don't
104 follow regular localisation guidelines).
106 static Long_option_init options_static[] = {
107 /* print example usage: lilypond -e "" ? */
108 {_i ("EXPR"), "evaluate", 'e',_i ("Scheme options: try -e \"(set-lily-option 'help 0)\" for more help.")},
109 /* another bug in option parser: --output=foe is taken as an abbreviation
110 for --output-format */
111 {_i ("EXT"), "format", 'f', _i ("use output format EXT (tex [default], pdftex, ps, scm or as)")},
112 {0, "help", 'h', _i ("this help")},
113 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
114 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
115 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
116 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
117 {0, "no-paper", 'm', _i ("produce MIDI output only")},
118 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
119 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
120 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
121 {0, "version", 'v', _i ("print version number")},
122 {0, "verbose", 'V', _i ("verbose")},
123 {0, "warranty", 'w', _i ("show warranty and copyright")},
130 fputs(gnu_lilypond_version_str ().ch_C(), os);
138 No version number or newline here. It confuses help2man
140 cout << _f ("Usage: %s [OPTION]... FILE...", "lilypond").ch_C();
142 cout << _ ("Typeset music and or play MIDI from FILE").ch_C();
146 "LilyPond is a music typesetter. It produces beautiful sheet music\n"
147 "using a high level description file as input. LilyPond is part of \n"
152 cout << _ ("Options:").ch_C();
154 cout << Long_option_init::table_str (options_static).ch_C();
156 cout << _ ("This binary was compiled with the following options:") .ch_C()
162 "datadir: `" DIR_DATADIR "'\n"
163 "localedir: `" DIR_LOCALEDIR "'\n"
169 cout << _f ("Report bugs to %s", "bug-lilypond@gnu.org").ch_C() << endl;
178 "This is free software. It is covered by the GNU General Public License,\n"
179 "and you are welcome to change it and/or distribute copies of it under\n"
180 "certain conditions. Invoke as `%s --warranty' for more information.\n",
184 cout << _f ("Copyright (c) %s by", "1996--2002").ch_C();
186 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
187 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
194 cout << _ ("GNU LilyPond -- The music typesetter").ch_C();
196 cout << _f ("Copyright (c) %s by", "1996--2002").ch_C();
198 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
199 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
202 " This program is free software; you can redistribute it and/or\n"
203 "modify it under the terms of the GNU General Public License version 2\n"
204 "as published by the Free Software Foundation.\n"
206 " This program is distributed in the hope that it will be useful,\n"
207 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
208 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
209 "General Public License for more details.\n"
211 " You should have received a copy (refer to the file COPYING) of the\n"
212 "GNU General Public License along with this program; if not, write to\n"
213 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
217 String prefix_directory;
222 // facilitate binary distributions
223 char const *env_lily = getenv ("LILYPONDPREFIX");
226 prefix_directory = env_lily;
229 setlocale (LC_ALL, ""); /* enable locales */
230 setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
231 String lily_locale_dir;
232 String name (PACKAGE);
236 urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
237 handy for multiple source-dir runs, though...
239 if (!prefix_directory.empty_b ())
241 lily_locale_dir = prefix_directory + "/share/locale";
242 bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
245 bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
246 textdomain (name.ch_C ());
249 global_path.add ("");
252 /* Adding mf/out make lilypond unchanged source directory, when setting
253 LILYPONDPREFIX to lilypond-x.y.z */
254 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
256 if (prefix_directory.empty_b ())
257 prefix_directory = DIR_DATADIR;
258 for (char **s = suffixes; *s; s++)
260 String p = prefix_directory + to_str ('/') + String (*s);
264 /* Urg: GNU make's $ (word) index starts at 1 */
266 while (global_path.try_add (p + to_str (".") + to_str (i)))
273 Make input file name from command argument.
275 Path describes file name with added default extension,
276 ".ly" if none. "-" is stdin.
279 distill_inname (String str)
281 Path p = split_path (str);
282 if (str.empty_b () || str == "-")
286 String orig_ext = p.ext;
287 char const *extensions[] = {"ly", "fly", "sly", "", 0};
288 for (int i = 0; extensions[i]; i++)
291 if (*extensions[i] && !p.ext.empty_b ())
293 p.ext += extensions[i];
294 if (!global_path.find (p.str ()).empty_b ())
297 /* Reshuffle extension */
298 p = split_path (p.str ());
304 format_to_ext (String format)
307 /* .lytex change put off */
308 return "tex"; // "lytex";
313 main_prog (void * , int, char**)
316 need to do this first. Engravers use lily.scm contents.
320 prepend onto GUILE loadpath.
325 init_lily_guile (prefix_directory);
328 call_constructors ();
329 all_fonts_global_p = new All_font_metrics (global_path.str ());
331 init_scheme_code_string += ")";
332 gh_eval_str ((char *)init_scheme_code_string.ch_C());
335 const char *arg = oparser_p_static->get_next_arg ();
340 /* No FILE arguments is now a usage error */
348 /* What/when was this supposed to do?
349 It looks like it reset the outname_str_global for every new
350 file, but only if user didn't specify a outname? Huh?
352 // if (outname_str_global == "")
356 Midi_def::reset_score_count ();
357 Paper_def::reset_score_count ();
360 Path inpath = distill_inname (infile);
362 /* By default, use base name of input file for output file name */
363 Path outpath = inpath;
364 if (inpath.str () != "-")
365 outpath.ext = format_to_ext (output_format_global);
367 /* By default, write output to cwd; do not copy directory part
368 of input file name */
372 if (!output_name_global.empty_b ())
373 outpath = split_path (output_name_global);
376 if (!init_name_global.empty_b ())
377 init = init_name_global;
378 else if (!inpath.ext.empty_b ())
379 init = "init." + inpath.ext;
383 /* Burp: output name communication goes through _global */
384 String save_output_name_global = output_name_global;
385 output_name_global = outpath.str ();
386 do_one_file (init, inpath.str ());
387 output_name_global = save_output_name_global;
390 } while ((arg = oparser_p_static->get_next_arg ()));
391 delete oparser_p_static;
392 oparser_p_static = 0;
393 exit (exit_status_global);
398 sane_putenv (char const* key, char const* value, bool overwrite)
400 if (overwrite || !getenv (key))
401 return putenv ((char*)((String (key) + "=" + value).ch_C ()));
406 main (int argc, char **argv)
410 /* Prepare GUILE for heavy memory usage. If you have plenty memory,
411 this may speed up GUILE a bit. If you're short on memory, these
414 export GUILE_INIT_SEGMENT_SIZE_1=36000
415 export GUILE_MAX_SEGMENT_SIZE=576000
417 may considerably decrease memory footprint (~*0.85), with a small
418 execution time penalty (~*1.10). However, if this 15% gain in memory
419 usage prevents swapping, the execution time falls drastically. */
421 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
422 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
424 ly_init_kpath (argv[0]);
426 oparser_p_static = new Getopt_long (argc, argv, options_static);
427 while (Long_option_init const * opt = (*oparser_p_static) ())
429 switch (opt->shortname_ch_)
433 exit (0); // we print a version anyway.
437 String s = oparser_p_static->optional_argument_ch_C_;
438 Path p = split_path (s);
439 if (s != "-" && p.ext.empty_b ())
440 p.ext = format_to_ext (output_format_global);
441 output_name_global = p.str ();
445 init_scheme_code_string +=
446 oparser_p_static->optional_argument_ch_C_;
453 output_format_global = oparser_p_static->optional_argument_ch_C_;
456 dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
459 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
462 global_path.push (oparser_p_static->optional_argument_ch_C_);
465 init_name_global = oparser_p_static->optional_argument_ch_C_;
472 verbose_global_b = true;
475 safe_global_b = true;
478 dependency_global_b = true;
481 no_paper_global_b = true;
491 scm_boot_guile (argc, argv, main_prog, 0);
493 scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
496 return 0; // unreachable