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>
21 #include "lily-guile.hh"
22 #include "lily-version.hh"
23 #include "all-font-metrics.hh"
24 #include "getopt-long.hh"
28 #include "file-path.hh"
29 #include "file-results.hh"
31 #include "lily-guile.hh"
32 #include "paper-def.hh"
33 #include "midi-def.hh"
34 #include "global-ctor.hh"
39 Global options that can be overridden through command line.
42 /* Write dependencies file? */
43 bool dependency_global_b = false;
45 /* Prepend to dependencies */
46 String dependency_prefix_global;
48 /* Names of header fields to be dumped to a separate file. */
49 Array<String> dump_header_fieldnames_global;
51 /* Name of initialisation file. */
52 String init_name_global;
54 /* Do not calculate and write paper output? */
55 bool no_paper_global_b = false;
57 /* Selected output format.
58 One of tex, ps, scm, as. */
59 String output_format_global = "tex";
61 /* Current output name. */
62 String output_name_global;
64 /* Run in safe mode? -- FIXME: should be re-analised */
65 bool safe_global_b = false;
67 /* Verbose progress indication? */
68 bool verbose_global_b = false;
70 /* Scheme code to execute before parsing, after .scm init */
71 String init_scheme_code_string = "(begin #t ";
79 All_font_metrics *all_fonts_global_p;
80 int exit_status_global;
81 File_path global_path;
83 /* Number of current score output block. If there's more than one
84 score block, this counter will be added to the output filename. */
85 int score_count_global;
93 /* The option parser */
94 static Getopt_long *oparser_p_static = 0;
97 Internationalisation kludge in two steps:
98 * use _i () to get entry in POT file
99 * call gettext () explicitely for actual "translation"
101 Note: these messages all start with lower case (ie, don't
102 follow regular localisation guidelines).
104 static Long_option_init options_static[] = {
105 /* print example usage: lilypond -e "(set-lily-option 'help 0)" ? */
106 {_i ("EXPR"), "evaluate", 'e',_i ("evalute EXPR as Scheme after .scm init is read")},
107 /* another bug in option parser: --output=foe is taken as an abbreviation
108 for --output-format */
109 {_i ("EXT"), "format", 'f', _i ("use output format EXT (tex [default], pdftex, ps, scm or as)")},
110 {0, "help", 'h', _i ("this help")},
111 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
112 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
113 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
114 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
115 {0, "no-paper", 'm', _i ("produce MIDI output only")},
116 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
117 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
118 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
119 {0, "version", 'v', _i ("print version number")},
120 {0, "verbose", 'V', _i ("verbose")},
121 {0, "warranty", 'w', _i ("show warranty and copyright")},
126 identify (ostream* os)
128 *os << gnu_lilypond_version_str ();
136 No version number or newline here. It confuses help2man
138 cout << _f ("Usage: %s [OPTION]... FILE...", "lilypond");
140 cout << _ ("Typeset music and or play MIDI from FILE");
144 "LilyPond is a music typesetter. It produces beautiful sheet music\n"
145 "using a high level description file as input. LilyPond is part of \n"
150 cout << _ ("Options:");
152 cout << Long_option_init::table_str (options_static);
154 cout << _ ("This binary was compiled with the following options:")
160 "datadir: `" DIR_DATADIR "'\n"
161 "localedir: `" DIR_LOCALEDIR "'\n"
167 cout << _f ("Report bugs to %s", "bug-lilypond@gnu.org") << endl;
176 "This is free software. It is covered by the GNU General Public License,\n"
177 "and you are welcome to change it and/or distribute copies of it under\n"
178 "certain conditions. Invoke as `%s --warranty' for more information.\n",
182 cout << _f ("Copyright (c) %s by", "1996--2001");
184 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
185 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
192 cout << _ ("GNU LilyPond -- The music typesetter");
194 cout << _f ("Copyright (c) %s by", "1996--2001");
196 cout << " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
197 cout << " Jan Nieuwenhuizen <janneke@gnu.org>\n";
200 " This program is free software; you can redistribute it and/or\n"
201 "modify it under the terms of the GNU General Public License version 2\n"
202 "as published by the Free Software Foundation.\n"
204 " This program is distributed in the hope that it will be useful,\n"
205 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
206 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
207 "General Public License for more details.\n"
209 " You should have received a copy (refer to the file COPYING) of the\n"
210 "GNU General Public License along with this program; if not, write to\n"
211 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
215 String prefix_directory;
220 // facilitate binary distributions
221 char const *env_lily = getenv ("LILYPONDPREFIX");
224 prefix_directory = env_lily;
227 setlocale (LC_ALL, ""); /* enable locales */
228 setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
229 String lily_locale_dir;
230 String name (PACKAGE);
234 urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
235 handy for multiple source-dir runs, though...
237 if (!prefix_directory.empty_b ())
239 lily_locale_dir = prefix_directory + "/share/locale";
240 bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
243 bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
244 textdomain (name.ch_C ());
247 global_path.add ("");
248 // must override (come before) "/usr/local/share/lilypond"!
249 char const *env_sz = getenv ("LILYINCLUDE");
251 global_path.parse_path (env_sz);
254 /* Adding mf/out make lilypond unchanged source directory, when setting
255 LILYPONDPREFIX to lilypond-x.y.z */
256 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
257 String prefix = prefix_directory;
258 if (prefix.empty_b ())
259 prefix = DIR_DATADIR;
260 for (char **s = suffixes; *s; s++)
262 String p = prefix + to_str ('/') + String (*s);
266 /* Urg: GNU make's $ (word) index starts at 1 */
268 while (global_path.try_add (p + to_str (".") + to_str (i)))
273 char const * glp = getenv ("GUILE_LOAD_PATH");
275 String new_glp (glp? glp : "") ;
277 new_glp = ":" + new_glp;
278 new_glp = prefix_directory + new_glp;
281 Yes , so setenv is not posix.
286 setenv ("GUILE_LOAD_PATH", new_glp.ch_C(), 1);
290 Make input file name from command argument.
292 Path describes file name with added default extension,
293 ".ly" if none. "-" is stdin.
296 distill_inname (String str)
298 Path p = split_path (str);
299 if (str.empty_b () || str == "-")
303 String orig_ext = p.ext;
304 char const *extensions[] = {"ly", "fly", "sly", "", 0};
305 for (int i = 0; extensions[i]; i++)
308 if (*extensions[i] && !p.ext.empty_b ())
310 p.ext += extensions[i];
311 if (!global_path.find (p.str ()).empty_b ())
314 /* Reshuffle extension */
315 p = split_path (p.str ());
321 format_to_ext (String format)
324 /* .lytex change put off */
325 return "tex"; // "lytex";
330 main_prog (void * , int, char**)
333 need to do this first. Engravers use lily.scm contents.
338 call_constructors ();
339 all_fonts_global_p = new All_font_metrics (global_path.str ());
341 init_scheme_code_string += ")";
342 gh_eval_str ((char *)init_scheme_code_string.ch_C());
345 const char *arg = oparser_p_static->get_next_arg ();
350 /* No FILE arguments is now a usage error */
358 /* What/when was this supposed to do?
359 It looks like it reset the outname_str_global for every new
360 file, but only if user didn't specify a outname? Huh?
362 // if (outname_str_global == "")
366 Midi_def::reset_score_count ();
367 Paper_def::reset_score_count ();
370 Path inpath = distill_inname (infile);
372 /* By default, use base name of input file for output file name */
373 Path outpath = inpath;
374 if (inpath.str () != "-")
375 outpath.ext = format_to_ext (output_format_global);
377 /* By default, write output to cwd; do not copy directory part
378 of input file name */
382 if (!output_name_global.empty_b ())
383 outpath = split_path (output_name_global);
386 if (!init_name_global.empty_b ())
387 init = init_name_global;
388 else if (!inpath.ext.empty_b ())
389 init = "init." + inpath.ext;
393 /* Burp: output name communication goes through _global */
394 String save_output_name_global = output_name_global;
395 output_name_global = outpath.str ();
396 do_one_file (init, inpath.str ());
397 output_name_global = save_output_name_global;
400 } while ((arg = oparser_p_static->get_next_arg ()));
401 delete oparser_p_static;
402 oparser_p_static = 0;
403 exit (exit_status_global);
407 sane_putenv (char const* key, char const* value)
410 putenv is POSIX, setenv is BSD 4.3
411 Urg, but putenv blindly overwrites environment settings.
414 return putenv ((char*)((String (key) + "=" + value).ch_C ()));
419 main (int argc, char **argv)
423 /* Prepare GUILE for heavy memory usage. If you have plenty memory,
424 this may speed up GUILE a bit. If you're short on memory, these
427 export GUILE_INIT_SEGMENT_SIZE_1=36000
428 export GUILE_MAX_SEGMENT_SIZE=576000
430 may considerably decrease memory footprint (~*0.85), with a small
431 execution time penalty (~*1.10). However, if this 15% gain in memory
432 usage prevents swapping, the execution time falls drastically. */
434 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304");
435 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608");
437 ly_init_kpath (argv[0]);
439 oparser_p_static = new Getopt_long (argc, argv, options_static);
440 while (Long_option_init const * opt = (*oparser_p_static) ())
442 switch (opt->shortname_ch_)
446 exit (0); // we print a version anyway.
450 String s = oparser_p_static->optional_argument_ch_C_;
451 Path p = split_path (s);
452 if (s != "-" && p.ext.empty_b ())
453 p.ext = format_to_ext (output_format_global);
454 output_name_global = p.str ();
458 init_scheme_code_string +=
459 oparser_p_static->optional_argument_ch_C_;
466 output_format_global = oparser_p_static->optional_argument_ch_C_;
469 dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
472 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
475 global_path.push (oparser_p_static->optional_argument_ch_C_);
478 init_name_global = oparser_p_static->optional_argument_ch_C_;
485 verbose_global_b = true;
488 safe_global_b = true;
491 dependency_global_b = true;
494 no_paper_global_b = true;
504 scm_boot_guile (argc, argv, main_prog, 0);
506 scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
509 return 0; // unreachable