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 static int sane_putenv (char const* key
, char const* value
, bool overwrite
= false);
40 Global options that can be overridden through command line.
43 /* Write dependencies file? */
44 bool dependency_global_b
= false;
46 /* Prepend to dependencies */
47 String dependency_prefix_global
;
49 /* Names of header fields to be dumped to a separate file. */
50 Array
<String
> dump_header_fieldnames_global
;
52 /* Name of initialisation file. */
53 String init_name_global
;
55 /* Do not calculate and write paper output? */
56 bool no_paper_global_b
= false;
58 /* Selected output format.
59 One of tex, ps, scm, as. */
60 String output_format_global
= "tex";
62 /* Current output name. */
63 String output_name_global
;
65 /* Run in safe mode? -- FIXME: should be re-analised */
66 bool safe_global_b
= false;
68 /* Verbose progress indication? */
69 bool verbose_global_b
= false;
71 /* Scheme code to execute before parsing, after .scm init */
72 String init_scheme_code_string
= "(begin #t ";
80 All_font_metrics
*all_fonts_global
;
81 int exit_status_global
;
82 File_path global_path
;
84 /* Number of current score output block. If there's more than one
85 score block, this counter will be added to the output filename. */
86 int score_count_global
;
94 /* The option parser */
95 static Getopt_long
*oparser_p_static
= 0;
98 Internationalisation kludge in two steps:
99 * use _i () to get entry in POT file
100 * call gettext () explicitely for actual "translation"
102 Note: these messages all start with lower case (ie, don't
103 follow regular localisation guidelines).
105 static Long_option_init options_static
[] = {
106 {_i ("EXPR"), "evaluate", 'e',
107 _i ("set options, use -e '(ly-option-usage)' for help")},
108 /* another bug in option parser: --output=foe is taken as an abbreviation
109 for --output-format */
110 {_i ("EXT"), "format", 'f', _i ("use output format EXT")},
111 {0, "help", 'h', _i ("this help")},
112 {_i ("FIELD"), "header", 'H', _i ("write header field to BASENAME.FIELD")},
113 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
114 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
115 {0, "dependencies", 'M', _i ("write Makefile dependencies for every input file")},
116 {0, "no-paper", 'm', _i ("produce MIDI output only")},
117 {_i ("FILE"), "output", 'o', _i ("write output to FILE")},
118 {_i ("DIR"), "dep-prefix", 'P', _i ("prepend DIR to dependencies")},
123 {0, "safe", 's', _i ("inhibit file output naming and exporting")},
125 {0, "version", 'v', _i ("print version number")},
126 {0, "verbose", 'V', _i ("verbose")},
127 {0, "warranty", 'w', _i ("show warranty and copyright")},
134 fputs (gnu_lilypond_version_string ().to_str0 (), out
);
141 fprintf (out
, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR
);
142 fprintf (out
, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR
);
143 fprintf (out
, "localedir: `%s'\n", LOCALEDIR
);
145 char *lilypond_prefix
= getenv ("LILYPONDPREFIX");
146 fprintf (out
, "LILYPONDPREFIX: `%s'\n",
147 (lilypond_prefix
? lilypond_prefix
: ""));
154 /* No version number or newline here. It confuses help2man. */
155 printf (_f ("Usage: %s [OPTION]... FILE...", "lilypond").to_str0 ());
157 printf (_ ("Typeset music and or play MIDI from FILE").to_str0 ());
160 "LilyPond is a music typesetter. It produces beautiful sheet music\n"
161 "using a high level description file as input. LilyPond is part of \n"
166 printf (_ ("Options:").to_str0 ());
168 printf (Long_option_init::table_string (options_static
).to_str0 ());
172 printf (_f ("Report bugs to %s", "bug-lilypond@gnu.org").to_str0 ());
182 "This is free software. It is covered by the GNU General Public License,\n"
183 "and you are welcome to change it and/or distribute copies of it under\n"
184 "certain conditions. Invoke as `%s --warranty' for more information.\n",
185 "lilypond").to_str0 ());
188 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
190 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
191 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
198 printf (_ ("GNU LilyPond -- The music typesetter").to_str0 ());
200 printf (_f ("Copyright (c) %s by", "1996--2003").to_str0 ());
202 printf (" Han-Wen Nienhuys <hanwen@cs.uu.nl>\n");
203 printf (" Jan Nieuwenhuizen <janneke@gnu.org>\n");
206 " This program is free software; you can redistribute it and/or\n"
207 "modify it under the terms of the GNU General Public License version 2\n"
208 "as published by the Free Software Foundation.\n"
210 " This program is distributed in the hope that it will be useful,\n"
211 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
212 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
213 "General Public License for more details.\n"
215 " You should have received a copy (refer to the file COPYING) of the\n"
216 "GNU General Public License along with this program; if not, write to\n"
217 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
218 "USA.\n").to_str0 ());
222 /* Where LilyPond's init files live. Typically:
223 LILYPOND_DATADIR = /usr/local/share/lilypond
224 LOCAL_LILYPOND_DATADIR = /usr/local/share/lilypond/1.5.68 */
225 char const *prefix_directory
[] = {LILYPOND_DATADIR
, LOCAL_LILYPOND_DATADIR
, 0};
230 if (char const *lilypond_prefix
= getenv ("LILYPONDPREFIX"))
231 prefix_directory
[1] = lilypond_prefix
;
235 setlocale (LC_ALL
, "");
236 /* Mustn't have commas in TeX output... */
237 setlocale (LC_NUMERIC
, "C");
238 String lily_locale_dir
;
239 String
name (PACKAGE
);
241 bindtextdomain (name
.to_str0 (), LOCALEDIR
);
242 textdomain (name
.to_str0 ());
245 global_path
.add ("");
248 /* Adding mf/out make lilypond unchanged source directory, when setting
249 LILYPONDPREFIX to lilypond-x.y.z */
250 char *suffixes
[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
252 for (unsigned i
= 0; prefix_directory
[i
]; i
++)
253 for (char **s
= suffixes
; *s
; s
++)
255 String p
= prefix_directory
[i
] + to_string ('/') + String (*s
);
256 global_path
.prepend (p
);
259 /* Urg: GNU make's $ (word) index starts at 1 */
261 while (global_path
.try_add (p
+ to_string (".") + to_string (i
)))
268 Make input file name from command argument.
270 Path describes file name with added default extension,
271 ".ly" if none. "-" is stdin.
274 distill_inname (String str
)
276 Path p
= split_path (str
);
277 if (str
.empty_b () || str
== "-")
281 String orig_ext
= p
.ext
;
282 char const *extensions
[] = {"ly", "", 0};
283 for (int i
= 0; extensions
[i
]; i
++)
286 if (*extensions
[i
] && !p
.ext
.empty_b ())
288 p
.ext
+= extensions
[i
];
289 if (!global_path
.find (p
.to_string ()).empty_b ())
292 /* Reshuffle extension */
293 p
= split_path (p
.to_string ());
299 format_to_ext (String format
)
302 /* .lytex change put off */
303 return "tex"; // "lytex";
308 prepend_load_path (String dir
)
310 String s
= "(set! %load-path (cons \""
313 scm_c_eval_string (s
.to_str0 ());
317 main_prog (void *, int, char **)
319 /* Engravers use lily.scm contents, need to make Guile find it.
320 Prepend onto GUILE %load-path, very ugh. */
322 for (unsigned i
= 0; prefix_directory
[i
]; i
++)
324 prepend_load_path (prefix_directory
[i
]);
326 /* Junk this. We should make real modules iso. just loading files. */
327 prepend_load_path (String (prefix_directory
[i
]) + "/scm");
330 if (verbose_global_b
)
334 call_constructors ();
336 progress_indication ("\n");
338 all_fonts_global
= new All_font_metrics (global_path
.to_string ());
340 init_scheme_code_string
+= ")";
341 gh_eval_str ((char *)init_scheme_code_string
.to_str0 ());
344 const char *arg
= oparser_p_static
->get_next_arg ();
346 /* Only exit until after running init_scheme_code, for
348 -e "(ly-set-option 'help #t)" */
352 /* No FILE arguments is now a usage error */
359 Midi_def::reset_score_count ();
360 Paper_def::reset_score_count ();
362 Path inpath
= distill_inname (infile
);
364 /* By default, use base name of input file for output file name */
365 Path outpath
= inpath
;
366 if (inpath
.to_string () != "-")
367 outpath
.ext
= format_to_ext (output_format_global
);
369 /* By default, write output to cwd; do not copy directory part
370 of input file name */
374 if (!output_name_global
.empty_b ())
375 outpath
= split_path (output_name_global
);
378 if (!init_name_global
.empty_b ())
379 init
= init_name_global
;
383 /* Burp: output name communication goes through _global */
384 String save_output_name_global
= output_name_global
;
385 output_name_global
= outpath
.to_string ();
386 do_one_file (init
, inpath
.to_string ());
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
))
402 String combine
= String (key
) + "=" + String (value
);
403 char * s
= strdup(combine
.to_str0 ());
410 main (int argc
, char **argv
)
416 These settings hopefully prepare lily for a lot of memory usage.
418 In practice the effect on GC times is barely measurable -- larger
419 segments cost slighly less time for the conservative marking. (but
420 not impressively much)
423 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
424 sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
426 ly_init_kpath (argv
[0]);
429 oparser_p_static
= new Getopt_long (argc
, argv
, options_static
);
430 while (Long_option_init
const * opt
= (*oparser_p_static
) ())
432 switch (opt
->shortname_char_
)
436 exit (0); // we print a version anyway.
440 String s
= oparser_p_static
->optional_argument_str0_
;
441 Path p
= split_path (s
);
442 if (s
!= "-" && p
.ext
.empty_b ())
443 p
.ext
= format_to_ext (output_format_global
);
444 output_name_global
= p
.to_string ();
448 init_scheme_code_string
+=
449 oparser_p_static
->optional_argument_str0_
;
456 if (oparser_p_static
->optional_argument_str0_
== "help")
458 printf("See http://lilypond.org/wiki?OutputFormats for more information.\n"\
459 "This option is for developers only.\n");
462 output_format_global
= oparser_p_static
->optional_argument_str0_
;
465 dependency_prefix_global
= oparser_p_static
->optional_argument_str0_
;
468 dump_header_fieldnames_global
.push (oparser_p_static
->optional_argument_str0_
);
471 global_path
.push (oparser_p_static
->optional_argument_str0_
);
474 init_name_global
= oparser_p_static
->optional_argument_str0_
;
480 verbose_global_b
= true;
483 safe_global_b
= true;
486 dependency_global_b
= true;
489 no_paper_global_b
= true;
501 if (verbose_global_b
)
506 scm_boot_guile (argc
, argv
, (void (*) (void*, int, char**))main_prog
, 0);
508 return 0; // unreachable