2 main.cc -- implement main: entrypoints
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
20 #include "lily-guile.hh"
21 #include "lily-version.hh"
22 #include "all-font-metrics.hh"
23 #include "getopt-long.hh"
27 #include "file-path.hh"
28 #include "file-results.hh"
30 #include "lily-guile.hh"
31 #include "paper-def.hh"
32 #include "midi-def.hh"
33 #include "global-ctor.hh"
38 Global options that can be overridden through command line.
41 /* Write dependencies file? */
42 bool dependency_global_b = false;
44 /* Prepend to dependencies */
45 String dependency_prefix_global;
47 /* Names of header fields to be dumped to a separate file. */
48 Array<String> dump_header_fieldnames_global;
50 /* Name of initialisation file. */
51 String init_name_global;
53 /* Do not calculate and write paper output? */
54 bool no_paper_global_b = false;
56 /* Selected output format.
57 One of tex, ps, scm, as. */
58 String output_format_global = "tex";
60 /* Current output name. */
61 String output_name_global;
63 /* Run in safe mode? -- FIXME: should be re-analised */
64 bool safe_global_b = false;
66 /* Verbose progress indication? */
67 bool verbose_global_b = false;
69 /* Scheme code to execute before parsing, after .scm init */
70 String init_scheme_code_string = "(begin #t ";
78 All_font_metrics *all_fonts_global_p;
79 int exit_status_global;
80 File_path global_path;
82 /* Number of current score output block. If there's more than one
83 score block, this counter will be added to the output filename. */
84 int score_count_global;
92 /* The option parser */
93 static Getopt_long *oparser_p_static = 0;
96 Internationalisation kludge in two steps:
97 * use _i () to get entry in POT file
98 * call gettext () explicitely for actual "translation"
100 Note: these messages all start with lower case (ie, don't
101 follow regular localisation guidelines).
103 static Long_option_init options_static[] = {
104 /* print example usage: lilypond -e "(set-lily-option 'help 0)" ? */
105 {_i ("EXPR"), "evaluate", 'e',_i ("evalute EXPR as Scheme after .scm init is read")},
106 /* another bug in option parser: --output=foe is taken as an abbreviation
107 for --output-format */
108 {_i ("EXT"), "format", 'f', _i ("use output format EXT (tex [default], pdftex, ps, scm or as)")},
109 {0, "help", 'h', _i ("this help")},
110 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
111 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
112 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
113 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
114 {0, "no-paper", 'm', _i ("produce MIDI output only")},
115 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
116 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
117 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
118 {0, "version", 'v', _i ("print version number")},
119 {0, "verbose", 'V', _i ("verbose")},
120 {0, "warranty", 'w', _i ("show warranty and copyright")},
125 identify (ostream* os)
127 *os << gnu_lilypond_version_str ();
135 No version number or newline here. It confuses help2man
137 cout << _f ("Usage: %s [OPTION]... FILE...", "lilypond");
139 cout << _ ("Typeset music and or play MIDI from FILE");
143 "LilyPond is a music typesetter. It produces beautiful sheet music\n"
144 "using a high level description file as input. LilyPond is part of \n"
149 cout << _ ("Options:");
151 cout << Long_option_init::table_str (options_static);
153 cout << _ ("This binary was compiled with the following options:")
159 "datadir: `" DIR_DATADIR "'\n"
160 "localedir: `" DIR_LOCALEDIR "'\n"
166 cout << _f ("Report bugs to %s", "bug-lilypond@gnu.org") << endl;
175 "This is free software. It is covered by the GNU General Public License,\n"
176 "and you are welcome to change it and/or distribute copies of it under\n"
177 "certain conditions. Invoke as `%s --warranty' for more information.\n",
181 cout << _f ("Copyright (c) %s by", "1996--2001");
183 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
184 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
191 cout << _ ("GNU LilyPond -- The music typesetter");
193 cout << _f ("Copyright (c) %s by", "1996--2001");
195 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
196 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
199 " This program is free software; you can redistribute it and/or\n"
200 "modify it under the terms of the GNU General Public License version 2\n"
201 "as published by the Free Software Foundation.\n"
203 " This program is distributed in the hope that it will be useful,\n"
204 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
205 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
206 "General Public License for more details.\n"
208 " You should have received a copy (refer to the file COPYING) of the\n"
209 "GNU General Public License along with this program; if not, write to\n"
210 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
217 // facilitate binary distributions
218 char const *env_lily = getenv ("LILYPONDPREFIX");
219 String prefix_directory;
221 prefix_directory = env_lily;
224 setlocale (LC_ALL, ""); /* enable locales */
225 setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
226 String lily_locale_dir;
227 String name (PACKAGE);
231 urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
232 handy for multiple source-dir runs, though...
234 if (!prefix_directory.empty_b ())
236 lily_locale_dir = prefix_directory + "/share/locale";
237 bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
240 bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
241 textdomain (name.ch_C ());
244 global_path.add ("");
245 // must override (come before) "/usr/local/share/lilypond"!
246 char const *env_sz = getenv ("LILYINCLUDE");
248 global_path.parse_path (env_sz);
251 /* Adding mf/out make lilypond unchanged source directory, when setting
252 LILYPONDPREFIX to lilypond-x.y.z */
253 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
254 String prefix = prefix_directory;
255 if (prefix.empty_b ())
256 prefix = DIR_DATADIR;
257 for (char **s = suffixes; *s; s++)
259 String p = prefix + to_str ('/') + String (*s);
263 /* Urg: GNU make's $ (word) index starts at 1 */
265 while (global_path.try_add (p + to_str (".") + to_str (i)))
272 Make input file name from command argument.
274 Path describes file name with added default extension,
275 ".ly" if none. "-" is stdin.
278 distill_inname (String str)
280 Path p = split_path (str);
281 if (str.empty_b () || str == "-")
285 String orig_ext = p.ext;
286 char const *extensions[] = {"ly", "fly", "sly", "", 0};
287 for (int i = 0; extensions[i]; i++)
290 if (*extensions[i] && !p.ext.empty_b ())
292 p.ext += extensions[i];
293 if (!global_path.find (p.str ()).empty_b ())
296 /* Reshuffle extension */
297 p = split_path (p.str ());
303 format_to_ext (String format)
306 /* .lytex change put off */
307 return "tex"; // "lytex";
312 main_prog (void * closure, int, char**)
315 need to do this first. Engravers use lily.scm contents.
318 if (verbose_global_b)
319 progress_indication ("\n");
320 read_lily_scm_file ("lily.scm");
323 call_constructors ();
324 all_fonts_global_p = new All_font_metrics (global_path.str ());
326 init_scheme_code_string += ")";
327 gh_eval_str ((char *)init_scheme_code_string.ch_C());
330 const char *arg = oparser_p_static->get_next_arg ();
335 /* No FILE arguments is now a usage error */
343 /* What/when was this supposed to do?
344 It looks like it reset the outname_str_global for every new
345 file, but only if user didn't specify a outname? Huh?
347 // if (outname_str_global == "")
351 Midi_def::reset_score_count ();
352 Paper_def::reset_score_count ();
355 Path inpath = distill_inname (infile);
357 /* By default, use base name of input file for output file name */
358 Path outpath = inpath;
359 if (inpath.str () != "-")
360 outpath.ext = format_to_ext (output_format_global);
362 /* By default, write output to cwd; do not copy directory part
363 of input file name */
367 if (!output_name_global.empty_b ())
368 outpath = split_path (output_name_global);
371 if (!init_name_global.empty_b ())
372 init = init_name_global;
373 else if (!inpath.ext.empty_b ())
374 init = "init." + inpath.ext;
378 /* Burp: output name communication goes through _global */
379 String save_output_name_global = output_name_global;
380 output_name_global = outpath.str ();
381 do_one_file (init, inpath.str ());
382 output_name_global = save_output_name_global;
385 } while ((arg = oparser_p_static->get_next_arg ()));
386 delete oparser_p_static;
387 oparser_p_static = 0;
388 exit (exit_status_global);
392 sane_putenv (char const* key, char const* value)
395 putenv is POSIX, setenv is BSD 4.3
396 Urg, but putenv blindly overwrites environment settings.
399 return putenv ((char*)((String (key) + "=" + value).ch_C ()));
404 main (int argc, char **argv)
408 /* Prepare GUILE for heavy memory usage. If you have plenty memory,
409 this may speed up GUILE a bit. If you're short on memory, these
412 export GUILE_INIT_SEGMENT_SIZE_1=36000
413 export GUILE_MAX_SEGMENT_SIZE=576000
415 may considerably decrease memory footprint (~*0.85), with a small
416 execution time penalty (~*1.10). However, if this 15% gain in memory
417 usage prevents swapping, the execution time falls drastically. */
419 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304");
420 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608");
422 ly_init_kpath (argv[0]);
424 oparser_p_static = new Getopt_long (argc, argv, options_static);
425 while (Long_option_init const * opt = (*oparser_p_static) ())
427 switch (opt->shortname_ch_)
431 exit (0); // we print a version anyway.
435 String s = oparser_p_static->optional_argument_ch_C_;
436 Path p = split_path (s);
437 if (s != "-" && p.ext.empty_b ())
438 p.ext = format_to_ext (output_format_global);
439 output_name_global = p.str ();
443 init_scheme_code_string +=
444 oparser_p_static->optional_argument_ch_C_;
451 output_format_global = oparser_p_static->optional_argument_ch_C_;
454 dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
457 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
460 global_path.push (oparser_p_static->optional_argument_ch_C_);
463 init_name_global = oparser_p_static->optional_argument_ch_C_;
470 verbose_global_b = true;
473 safe_global_b = true;
476 dependency_global_b = true;
479 no_paper_global_b = true;
489 scm_boot_guile (argc, argv, main_prog, 0);
491 scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
494 return 0; // unreachable