2 main.cc -- implement main: entrypoints
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 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 "input-file-results.hh"
31 #include "lily-guile.hh"
32 #include "paper-def.hh"
33 #include "midi-def.hh"
34 #include "global-ctor.hh"
37 Array<String> failed_files;
39 static int sane_putenv (char const* key, char const* value, bool overwrite = false);
42 Global options that can be overridden through command line.
45 /* Write dependencies file? */
46 bool dependency_global_b = false;
48 /* Prepend to dependencies */
49 String dependency_prefix_global;
51 /* Names of header fields to be dumped to a separate file. */
52 Array<String> dump_header_fieldnames_global;
54 /* Name of initialisation file. */
55 String init_name_global;
57 /* Do not calculate and write paper output? */
58 bool no_paper_global_b = false;
60 /* Selected output format.
61 One of tex, ps, scm, as. */
62 String output_format_global = "tex";
64 /* Current output name. */
65 String output_name_global;
67 /* Run in safe mode? */
68 bool safe_global_b = false;
70 /* Verbose progress indication? */
71 bool verbose_global_b = false;
73 /* Scheme code to execute before parsing, after .scm init */
74 String init_scheme_code_string = "(begin #t ";
82 All_font_metrics *all_fonts_global;
83 int exit_status_global;
84 File_path global_path;
86 /* Number of current score output block. If there's more than one
87 score block, this counter will be added to the output filename. */
88 int score_count_global;
96 /* The option parser */
97 static Getopt_long *oparser_p_static = 0;
100 Internationalisation kludge in two steps:
101 * use _i () to get entry in POT file
102 * call gettext () explicitely for actual "translation"
104 Note: these messages all start with lower case (ie, don't
105 follow regular localisation guidelines).
107 static Long_option_init options_static[] = {
108 {_i ("EXPR"), "evaluate", 'e',
109 _i ("set options, use -e '(ly-option-usage)' for help")},
110 /* another bug in option parser: --output=foe is taken as an abbreviation
111 for --output-format */
112 {_i ("EXT"), "format", 'f', _i ("use output format EXT")},
113 {0, "help", 'h', _i ("print this help")},
114 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
115 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
116 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
117 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
118 {0, "no-paper", 'm', _i ("produce MIDI output only")},
119 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
120 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
121 {0, "safe-mode", 's', _i ("run in safe mode")},
122 {0, "version", 'v', _i ("print version number")},
123 {0, "verbose", 'V', _i ("be verbose")},
124 {0, "warranty", 'w', _i ("show warranty and copyright")},
131 fputs (gnu_lilypond_version_string ().to_str0 (), out);
138 fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
139 fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
140 fprintf (out, "localedir: `%s'\n", LOCALEDIR);
142 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
143 fprintf (out, "LILYPONDPREFIX: `%s'\n",
144 (lilypond_prefix ? lilypond_prefix : ""));
151 /* No version number or newline here. It confuses help2man. */
152 printf (_f ("Usage: %s [OPTIONS]... FILE...", "lilypond").to_str0 ());
154 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
156 printf(_ ("LilyPond produces beautiful music notation.\n"
157 "For more information, see http://lilypond.org/"
161 printf (_ ("Options:").to_str0 ());
163 printf (Long_option_init::table_string (options_static).to_str0 ());
165 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
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",
179 "lilypond").to_str0 ());
182 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
184 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
185 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
192 printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
194 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
196 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
197 printf (" 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\n"
212 "USA.\n").to_str0 ());
216 /* Where LilyPond's init files live. Typically:
217 LILYPOND_DATADIR = /usr/local/share/lilypond
218 LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
219 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
224 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
225 prefix_directory[1] = lilypond_prefix;
229 setlocale (LC_ALL, "");
230 /* Mustn't have commas in TeX output... */
231 setlocale (LC_NUMERIC, "C");
232 String lily_locale_dir;
233 String name (PACKAGE);
235 bindtextdomain (name.to_str0 (), LOCALEDIR);
236 textdomain (name.to_str0 ());
239 global_path.add ("");
242 /* Adding mf/out make lilypond unchanged source directory, when setting
243 LILYPONDPREFIX to lilypond-x.y.z */
244 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
246 for (unsigned i = 0; prefix_directory[i]; i++)
247 for (char **s = suffixes; *s; s++)
249 String p = prefix_directory[i] + to_string ('/') + String (*s);
250 global_path.prepend (p);
253 /* Urg: GNU make's $ (word) index starts at 1 */
255 while (global_path.try_add (p + to_string (".") + to_string (i)))
262 Make input file name from command argument.
264 Path describes file name with added default extension,
265 ".ly" if none. "-" is stdin.
268 distill_inname (String str)
270 Path p = split_path (str);
271 if (str.is_empty () || str == "-")
275 String orig_ext = p.ext;
276 char const *extensions[] = {"ly", "", 0};
277 for (int i = 0; extensions[i]; i++)
280 if (*extensions[i] && !p.ext.is_empty ())
282 p.ext += extensions[i];
283 if (!global_path.find (p.to_string ()).is_empty ())
286 /* Reshuffle extension */
287 p = split_path (p.to_string ());
293 prepend_load_path (String dir)
295 String s = "(set! %load-path (cons \""
298 scm_c_eval_string (s.to_str0 ());
302 main_prog (void *, int, char **)
304 /* Engravers use lily.scm contents, need to make Guile find it.
305 Prepend onto GUILE %load-path, very ugh. */
307 for (unsigned i = 0; prefix_directory[i]; i++)
309 prepend_load_path (prefix_directory[i]);
311 /* Junk this. We should make real modules iso. just loading files. */
312 prepend_load_path (String (prefix_directory[i]) + "/scm");
315 if (verbose_global_b)
319 call_constructors ();
321 progress_indication ("\n");
323 all_fonts_global = new All_font_metrics (global_path.to_string ());
325 init_scheme_code_string += ")";
326 gh_eval_str ((char *)init_scheme_code_string.to_str0 ());
329 const char *arg = oparser_p_static->get_next_arg ();
331 /* Only exit until after running init_scheme_code, for
333 -e "(ly-set-option 'help #t)" */
337 /* No FILE arguments is now a usage error */
344 Path inpath = distill_inname (infile);
346 /* By default, use base name of input file for output file name */
347 Path outpath = inpath;
348 if (inpath.to_string () != "-")
349 outpath.ext = output_format_global;
351 /* By default, write output to cwd; do not copy directory part
352 of input file name */
356 if (!output_name_global.is_empty ())
357 outpath = split_path (output_name_global);
360 if (!init_name_global.is_empty ())
361 init = init_name_global;
365 do_one_file (init, inpath.to_string (), outpath.to_string ());
368 } while ((arg = oparser_p_static->get_next_arg ()));
369 delete oparser_p_static;
370 oparser_p_static = 0;
372 if (exit_status_global)
374 printf ("Failed files: ");
375 for (int i = 0; i < failed_files.size (); i++)
376 printf ("%s", failed_files[i].to_str0 ());
378 exit (exit_status_global);
383 sane_putenv (char const* key, char const* value, bool overwrite)
385 if (overwrite || !getenv (key))
387 String combine = String (key) + "=" + String (value);
388 char * s = strdup(combine.to_str0 ());
395 main (int argc, char **argv)
401 These settings hopefully prepare lily for a lot of memory usage.
403 In practice the effect on GC times is barely measurable -- larger
404 segments cost slighly less time for the conservative marking. (but
405 not impressively much)
408 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
409 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
411 init_kpath (argv[0]);
414 oparser_p_static = new Getopt_long (argc, argv, options_static);
415 while (Long_option_init const * opt = (*oparser_p_static) ())
417 switch (opt->shortname_char_)
421 exit (0); // we print a version anyway.
425 String s = oparser_p_static->optional_argument_str0_;
426 Path p = split_path (s);
427 if (s != "-" && p.ext.is_empty ())
428 p.ext = output_format_global;
430 output_name_global = p.to_string ();
434 init_scheme_code_string +=
435 oparser_p_static->optional_argument_str0_;
442 if (oparser_p_static->optional_argument_str0_ == "help")
444 printf("This option is for developers only; Read the source code for more information\n");
446 // actually it's not here,
447 // but we award you special points for looking here.
451 output_format_global = oparser_p_static->optional_argument_str0_;
454 dependency_prefix_global = oparser_p_static->optional_argument_str0_;
457 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_str0_);
460 global_path.push (oparser_p_static->optional_argument_str0_);
463 init_name_global = oparser_p_static->optional_argument_str0_;
469 verbose_global_b = true;
472 safe_global_b = true;
475 dependency_global_b = true;
478 no_paper_global_b = true;
489 if (verbose_global_b)
494 scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
496 return 0; // unreachable