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? -- FIXME: should be re-analised */
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")},
125 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
127 {0, "version", 'v', _i ("print version number")},
128 {0, "verbose", 'V', _i ("be verbose")},
129 {0, "warranty", 'w', _i ("show warranty and copyright")},
136 fputs (gnu_lilypond_version_string ().to_str0 (), out);
143 fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
144 fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
145 fprintf (out, "localedir: `%s'\n", LOCALEDIR);
147 char *lilypond_prefix = getenv ("LILYPONDPREFIX");
148 fprintf (out, "LILYPONDPREFIX: `%s'\n",
149 (lilypond_prefix ? lilypond_prefix : ""));
156 /* No version number or newline here. It confuses help2man. */
157 printf (_f ("Usage: %s [OPTIONS]... FILE...", "lilypond").to_str0 ());
159 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
161 printf(_ ("LilyPond produces beautiful music notation.\n"
162 "For more information, see http://lilypond.org/"
166 printf (_ ("Options:").to_str0 ());
168 printf (Long_option_init::table_string (options_static).to_str0 ());
170 printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
181 "This is free software. It is covered by the GNU General Public License,\n"
182 "and you are welcome to change it and/or distribute copies of it under\n"
183 "certain conditions. Invoke as `%s --warranty' for more information.\n",
184 "lilypond").to_str0 ());
187 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
189 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
190 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
197 printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
199 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
201 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
202 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
205 " This program is free software; you can redistribute it and/or\n"
206 "modify it under the terms of the GNU General Public License version 2\n"
207 "as published by the Free Software Foundation.\n"
209 " This program is distributed in the hope that it will be useful,\n"
210 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
211 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
212 "General Public License for more details.\n"
214 " You should have received a copy (refer to the file COPYING) of the\n"
215 "GNU General Public License along with this program; if not, write to\n"
216 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,\n"
217 "USA.\n").to_str0 ());
221 /* Where LilyPond's init files live. Typically:
222 LILYPOND_DATADIR = /usr/local/share/lilypond
223 LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
224 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
229 if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
230 prefix_directory[1] = lilypond_prefix;
234 setlocale (LC_ALL, "");
235 /* Mustn't have commas in TeX output... */
236 setlocale (LC_NUMERIC, "C");
237 String lily_locale_dir;
238 String name (PACKAGE);
240 bindtextdomain (name.to_str0 (), LOCALEDIR);
241 textdomain (name.to_str0 ());
244 global_path.add ("");
247 /* Adding mf/out make lilypond unchanged source directory, when setting
248 LILYPONDPREFIX to lilypond-x.y.z */
249 char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
251 for (unsigned i = 0; prefix_directory[i]; i++)
252 for (char **s = suffixes; *s; s++)
254 String p = prefix_directory[i] + to_string ('/') + String (*s);
255 global_path.prepend (p);
258 /* Urg: GNU make's $ (word) index starts at 1 */
260 while (global_path.try_add (p + to_string (".") + to_string (i)))
267 Make input file name from command argument.
269 Path describes file name with added default extension,
270 ".ly" if none. "-" is stdin.
273 distill_inname (String str)
275 Path p = split_path (str);
276 if (str.is_empty () || str == "-")
280 String orig_ext = p.ext;
281 char const *extensions[] = {"ly", "", 0};
282 for (int i = 0; extensions[i]; i++)
285 if (*extensions[i] && !p.ext.is_empty ())
287 p.ext += extensions[i];
288 if (!global_path.find (p.to_string ()).is_empty ())
291 /* Reshuffle extension */
292 p = split_path (p.to_string ());
298 prepend_load_path (String dir)
300 String s = "(set! %load-path (cons \""
303 scm_c_eval_string (s.to_str0 ());
307 main_prog (void *, int, char **)
309 /* Engravers use lily.scm contents, need to make Guile find it.
310 Prepend onto GUILE %load-path, very ugh. */
312 for (unsigned i = 0; prefix_directory[i]; i++)
314 prepend_load_path (prefix_directory[i]);
316 /* Junk this. We should make real modules iso. just loading files. */
317 prepend_load_path (String (prefix_directory[i]) + "/scm");
320 if (verbose_global_b)
324 call_constructors ();
326 progress_indication ("\n");
328 all_fonts_global = new All_font_metrics (global_path.to_string ());
330 init_scheme_code_string += ")";
331 gh_eval_str ((char *)init_scheme_code_string.to_str0 ());
334 const char *arg = oparser_p_static->get_next_arg ();
336 /* Only exit until after running init_scheme_code, for
338 -e "(ly-set-option 'help #t)" */
342 /* No FILE arguments is now a usage error */
349 Path inpath = distill_inname (infile);
351 /* By default, use base name of input file for output file name */
352 Path outpath = inpath;
353 if (inpath.to_string () != "-")
354 outpath.ext = output_format_global;
356 /* By default, write output to cwd; do not copy directory part
357 of input file name */
361 if (!output_name_global.is_empty ())
362 outpath = split_path (output_name_global);
365 if (!init_name_global.is_empty ())
366 init = init_name_global;
370 do_one_file (init, inpath.to_string (), outpath.to_string ());
373 } while ((arg = oparser_p_static->get_next_arg ()));
374 delete oparser_p_static;
375 oparser_p_static = 0;
377 if (exit_status_global)
379 printf ("Failed files: ");
380 for (int i = 0; i < failed_files.size (); i++)
381 printf ("%s", failed_files[i].to_str0 ());
383 exit (exit_status_global);
388 sane_putenv (char const* key, char const* value, bool overwrite)
390 if (overwrite || !getenv (key))
392 String combine = String (key) + "=" + String (value);
393 char * s = strdup(combine.to_str0 ());
400 main (int argc, char **argv)
406 These settings hopefully prepare lily for a lot of memory usage.
408 In practice the effect on GC times is barely measurable -- larger
409 segments cost slighly less time for the conservative marking. (but
410 not impressively much)
413 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
414 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
416 init_kpath (argv[0]);
419 oparser_p_static = new Getopt_long (argc, argv, options_static);
420 while (Long_option_init const * opt = (*oparser_p_static) ())
422 switch (opt->shortname_char_)
426 exit (0); // we print a version anyway.
430 String s = oparser_p_static->optional_argument_str0_;
431 Path p = split_path (s);
432 if (s != "-" && p.ext.is_empty ())
433 p.ext = output_format_global;
435 output_name_global = p.to_string ();
439 init_scheme_code_string +=
440 oparser_p_static->optional_argument_str0_;
447 if (oparser_p_static->optional_argument_str0_ == "help")
449 printf("This option is for developers only; Read the source code for more information\n");
451 // actually it's not here,
452 // but we award you special points for looking here.
456 output_format_global = oparser_p_static->optional_argument_str0_;
459 dependency_prefix_global = oparser_p_static->optional_argument_str0_;
462 dump_header_fieldnames_global.push (oparser_p_static->optional_argument_str0_);
465 global_path.push (oparser_p_static->optional_argument_str0_);
468 init_name_global = oparser_p_static->optional_argument_str0_;
474 verbose_global_b = true;
477 safe_global_b = true;
480 dependency_global_b = true;
483 no_paper_global_b = true;
494 if (verbose_global_b)
499 scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
501 return 0; // unreachable