2 main.cc -- implement main () entrypoint.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
18 #include <sys/types.h>
33 #include "all-font-metrics.hh"
34 #include "file-name.hh"
35 #include "freetype.hh"
36 #include "getopt-long.hh"
37 #include "global-ctor.hh"
38 #include "lily-guile.hh"
39 #include "lily-version.hh"
41 #include "output-def.hh"
42 #include "string-convert.hh"
45 #include "program-option.hh"
48 * Global options that can be overridden through command line.
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 /* Selected output backend
58 One of (gnome, ps [default], scm, svg, tex, texstr)") */
59 String output_backend_global = "ps";
60 /* Output formats to generate. */
61 String output_format_global = "";
63 bool is_pango_format_global;
64 bool is_TeX_format_global;
66 /* Current output name. */
67 String output_name_global;
69 /* Run in safe mode? */
70 bool be_safe_global = false;
72 /* Provide URI links to the original file */
73 bool point_and_click_global = true;
75 /* Verbose progress indication? */
76 bool be_verbose_global = false;
78 /* Scheme code to execute before parsing, after .scm init.
79 This is where -e arguments are appended to. */
80 String init_scheme_code_string;
81 String init_scheme_variables;
83 /* Generate preview of first system. */
84 bool make_preview = false;
86 /* Generate printed output. */
87 bool make_print = true;
90 * Miscellaneous global stuff.
92 File_path global_path;
98 static char const *AUTHORS
99 = " Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
100 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
102 static char const *PROGRAM_NAME = "lilypond";
103 static char const *PROGRAM_URL = "http://lilypond.org";
105 static char const *NOTICE
106 = _i ("This program is free software. It is covered by the GNU General Public\n"
107 "License and you are welcome to change it and/or distribute copies of it\n"
108 "under certain conditions. Invoke as `%s --warranty' for more\n"
111 static char const *WARRANTY
112 = _i (" This program is free software; you can redistribute it and/or\n"
113 "modify it under the terms of the GNU General Public License version 2\n"
114 "as published by the Free Software Foundation.\n"
116 " This program is distributed in the hope that it will be useful,\n"
117 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
118 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
119 "General Public License for more details.\n"
121 " You should have received a copy (refer to the file COPYING) of the\n"
122 "GNU General Public License along with this program; if not, write to\n"
123 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
124 "Boston, MA 02111-1307, USA.\n");
126 /* Where the init files live. Typically:
127 LILYPOND_DATADIR = /usr/share/lilypond
129 String prefix_directory;
131 /* The jail specification: USER,GROUP,JAIL,DIR. */
134 /* The option parser */
135 static Getopt_long *option_parser = 0;
137 /* Internationalisation kludge in two steps:
138 * use _i () to get entry in POT file
139 * call gettext () explicitely for actual "translation" */
141 static Long_option_init options_static[]
143 {_i ("BACK"), "backend", 'b', _i ("use backend BACK (gnome, ps [default],\nscm, svg, tex, texstr)")},
145 {_i ("SYM=VAL"), "define-default", 'd',
146 _i ("set a Scheme program option. Uses #t if VAL is not specified\n"
147 "Try -dhelp for help.")},
149 {_i ("EXPR"), "evaluate", 'e', _i ("evaluate scheme code")},
150 /* Bug in option parser: --output =foe is taken as an abbreviation
151 for --output-format. */
152 {_i ("FORMATs"), "formats", 'f', _i ("dump FORMAT,... Also as separate options:")},
153 {0, "dvi", 0, _i ("generate DVI (tex backend only)")},
154 {0, "pdf", 0, _i ("generate PDF (default)")},
155 {0, "png", 0, _i ("generate PNG")},
156 {0, "ps", 0, _i ("generate PostScript")},
157 {0, "tex", 0, _i ("generate TeX (tex backend only)")},
158 {0, "help", 'h', _i ("print this help")},
159 {_i ("FIELD"), "header", 'H', _i ("dump a header field to file BASENAME.FIELD")},
160 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
161 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
162 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
164 {_i ("USER,GROUP,JAIL,DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP\n"
167 {0, "no-print", 0, _i ("do not generate printed output")},
168 {0, "preview", 'p', _i ("generate a preview of the first system")},
169 {0, "safe-mode", 's', _i ("disallow unsafe Scheme and PostScript operations")},
170 {0, "version", 'v', _i ("print version number")},
171 {0, "verbose", 'V', _i ("be verbose")},
172 {0, "warranty", 'w', _i ("show warranty and copyright")},
176 #define LILYPOND_DATADIR PACKAGE_DATADIR "/" TOPLEVEL_VERSION
179 env_var_info (FILE *out, char const *key)
181 if (char const *value = getenv (key))
182 fprintf (out, "%s=\"%s\"\n", key, value);
189 fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
190 env_var_info (out, "LILYPONDPREFIX");
191 fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
193 fprintf (out, "\nEffective prefix: \"%s\"\n", prefix_directory.to_str0 ());
196 env_var_info (out, "FONTCONFIG_FILE");
197 env_var_info (out, "FONTCONFIG_PATH");
198 env_var_info (out, "GS_FONTPATH");
199 env_var_info (out, "GS_LIB");
200 env_var_info (out, "GUILE_LOAD_PATH");
201 env_var_info (out, "PANGO_RC_FILE");
202 env_var_info (out, "PATH");
209 printf (_f ("Copyright (c) %s by\n%s and others.",
211 AUTHORS).to_str0 ());
218 fputs (gnu_lilypond_version_string ().to_str0 (), out);
226 puts (_f (NOTICE, PROGRAM_NAME).to_str0 ());
231 LY_DEFINE (ly_usage, "ly:usage",
233 "Print usage message.")
235 /* No version number or newline here. It confuses help2man. */
236 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
238 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
240 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
242 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
244 printf (_ ("Options:").to_str0 ());
246 printf (Long_option_init::table_string (options_static).to_str0 ());
248 printf (_f ("Report bugs to %s or via %s",
249 "bug-lilypond@gnu.org"
250 "http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs"
254 return SCM_UNSPECIFIED;
264 printf (_ (WARRANTY).to_str0 ());
268 sane_putenv (char const *key, String value, bool overwrite = true)
270 if (overwrite || !getenv (key))
272 String combine = String (key) + "=" + value;
273 char *s = strdup (combine.to_str0 ());
281 set_env_file (char const *key, String value)
284 return sane_putenv (key, value, false);
285 else if (be_verbose_global)
286 warning (_f ("no such file: %s", value));
291 prepend_env_path (char const *key, String value)
295 if (char const *cur = getenv (key))
296 value += to_string (PATHSEP) + cur;
297 return sane_putenv (key, value.to_str0 ());
299 else if (be_verbose_global)
300 warning (_f ("no such directory: %s", value));
305 dir_name (String const file_name)
307 String s = file_name;
308 s.substitute ('\\', '/');
309 s = s.left_string (s.index_last ('/'));
319 setup_paths (char const *argv0)
321 prefix_directory = LILYPOND_DATADIR;
325 if (getenv ("LILYPOND_VERBOSE"))
326 be_verbose_global = true;
328 #if 1 /* Huh, argv0 is not absolute on windows? ndef __MINGW32__ */
330 p.parse_path (getenv ("PATH"));
331 String bindir = dir_name (p.find (argv0));
332 #else /* __MINGW32__ */
333 String bindir = dir_name (argv0);
335 String argv0_prefix = dir_name (bindir);
336 if (argv0_prefix != dir_name (dir_name (dir_name (prefix_directory))))
338 if (be_verbose_global)
339 warning (_f ("argv0 relocation: prefix=%s, argv0=%s, argv0_prefix=%s",
342 argv0_prefix.to_str0 ()));
343 String datadir = argv0_prefix + "/share";
344 String libdir = argv0_prefix + "/lib";
345 String localedir = datadir + "/locale";
346 String sysconfdir = argv0_prefix + "/etc";
347 String argv0_lilypond_datadir = datadir + "/lilypond/" TOPLEVEL_VERSION;
349 if (is_dir (argv0_lilypond_datadir))
350 prefix_directory = argv0_lilypond_datadir;
353 if (is_dir (localedir))
354 bindtextdomain ("lilypond", localedir.to_str0 ());
357 set_env_file ("FONTCONFIG_FILE", sysconfdir + "/fonts/fonts.conf");
359 char font_dir[PATH_MAX];
360 ExpandEnvironmentStrings ("%windir%/fonts", font_dir, sizeof (font_dir));
361 prepend_env_path ("GS_FONTPATH", font_dir);
364 /* FIXME: *cough* 8.15 *cough* */
365 prepend_env_path ("GS_FONTPATH", datadir + "/ghostscript/8.15/fonts");
366 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/Resource");
367 prepend_env_path ("GS_LIB", datadir + "/ghostscript/8.15/lib");
369 prepend_env_path ("GS_FONTPATH", datadir + "/gs/fonts");
370 prepend_env_path ("GS_LIB", datadir + "/gs/Resource");
371 prepend_env_path ("GS_LIB", datadir + "/gs/lib");
373 prepend_env_path ("GUILE_LOAD_PATH", datadir
374 + to_string ("/guile/%d.%d",
375 SCM_MAJOR_VERSION, SCM_MINOR_VERSION));
376 set_env_file ("PANGO_RC_FILE", sysconfdir + "/pango/pangorc");
377 prepend_env_path ("PATH", bindir);
381 #endif /* ARGV0_RELOCATION */
383 /* FIXME: use LILYPOND_DATADIR. */
384 if (char const *env = getenv ("LILYPONDPREFIX"))
387 /* Normalize file name. */
388 env = File_name (env).to_string ().get_copy_str0 ();
390 prefix_directory = env;
393 global_path.append ("");
397 When running from build dir, a full LILYPOND_PREFIX is set-up at
399 $(OUTBASE)/share/lilypond/TOPLEVEL_VERSION
401 This historical hack will allow the shorthand
403 LILYPONDPREFIX=out lily/out/lilypond ...
408 String build_prefix = prefix_directory + "/share/lilypond/" TOPLEVEL_VERSION;
409 if (stat (build_prefix.to_str0 (), &statbuf) == 0)
410 prefix_directory = build_prefix;
413 /* Adding mf/out make lilypond unchanged source directory, when setting
414 LILYPONDPREFIX to lilypond-x.y.z */
415 char *suffixes[] = {"ly", "ps", "scm", 0 };
419 for (char **s = suffixes; *s; s++)
421 String path = prefix_directory + to_string ('/') + String (*s);
426 dirs.push (prefix_directory + "/fonts/otf/");
427 dirs.push (prefix_directory + "/fonts/type1/");
428 dirs.push (prefix_directory + "/fonts/svg/");
430 for (int i = 0; i < dirs.size (); i++)
431 global_path.prepend (dirs[i]);
435 prepend_load_path (String dir)
437 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
438 scm_c_eval_string (s.to_str0 ());
441 void init_global_tweak_registry ();
442 void init_fontconfig ();
448 /* Now we chroot, setuid/setgrp and chdir. If something goes wrong,
449 we exit (this is a security-sensitive area). First we split
450 jail_spec into its components, then we retrieve the user/group id
451 (necessarily *before* chroot'ing) and finally we perform the
456 USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
459 Array<String> components = String_convert::split (jail_spec, ',');
460 if (components.size () != JAIL_MAX)
462 error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
463 components.size ()));
471 if (passwd * passwd = getpwnam (components[USER_NAME].to_str0 ()))
472 uid = passwd->pw_uid;
476 error (_f ("no such user: %s", components[USER_NAME]));
478 error (_f ("can't get user id from user name: %s: %s",
479 components[USER_NAME],
488 if (group * group = getgrnam (components[GROUP_NAME].to_str0 ()))
493 error (_f ("no such group: %s", components[GROUP_NAME]));
495 error (_f ("can't get group id from group name: %s: %s",
496 components[GROUP_NAME],
501 if (chroot (components[JAIL].to_str0 ()))
503 error (_f ("can't chroot to: %s: %s", components[JAIL],
510 error (_f ("can't change group id to: %d: %s", gid, strerror (errno)));
516 error (_f ("can't change user id to: %d: %s", uid, strerror (errno)));
520 if (chdir (components[DIR].to_str0 ()))
522 error (_f ("can't change working directory to: %s: %s", components[DIR],
530 main_with_guile (void *, int, char **)
532 /* Engravers use lily.scm contents, need to make Guile find it.
533 Prepend onto GUILE %load-path, very ugh. */
535 prepend_load_path (prefix_directory);
536 prepend_load_path (prefix_directory + "/scm");
538 if (be_verbose_global)
540 is_TeX_format_global = (output_backend_global == "tex"
541 || output_backend_global == "texstr");
543 is_pango_format_global = !is_TeX_format_global;
546 call_constructors ();
547 ly_set_option (ly_symbol2scm ("verbose"), scm_from_bool (be_verbose_global));
549 init_global_tweak_registry ();
554 all_fonts_global = new All_font_metrics (global_path.to_string ());
556 if (!init_scheme_variables.is_empty ()
557 || !init_scheme_code_string.is_empty ())
559 init_scheme_variables = "(map (lambda (x) (ly:set-option (car x) (cdr x))) (list "
560 + init_scheme_variables + "))";
562 init_scheme_code_string
564 + init_scheme_variables
565 + init_scheme_code_string
568 char const *str0 = init_scheme_code_string.to_str0 ();
570 if (be_verbose_global)
571 progress_indication (_f ("Evaluating %s", str0));
572 scm_c_eval_string ((char *) str0);
575 /* We accept multiple independent music files on the command line to
576 reduce compile time when processing lots of small files.
577 Starting the GUILE engine is very time consuming. */
581 while (char const *arg = option_parser->get_next_arg ())
583 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
584 tail = SCM_CDRLOC (*tail);
586 delete option_parser;
590 if (!jail_spec.is_empty ())
594 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
602 setup_localisation ()
606 setlocale (LC_ALL, "");
608 /* FIXME: check if this is still true.
609 Disable localisation of float values. This breaks TeX output. */
610 setlocale (LC_NUMERIC, "C");
612 String localedir = LOCALEDIR;
613 if (char const *env = getenv ("LILYPOND_LOCALEDIR"))
616 bindtextdomain ("lilypond", localedir.to_str0 ());
617 textdomain ("lilypond");
622 add_output_format (String format)
624 if (output_format_global != "")
625 output_format_global += ",";
626 output_format_global += format;
630 parse_argv (int argc, char **argv)
632 bool show_help = false;
633 option_parser = new Getopt_long (argc, argv, options_static);
634 while (Long_option_init const *opt = (*option_parser) ())
636 switch (opt->shortname_char_)
639 if (String (opt->longname_str0_) == "dvi"
640 || String (opt->longname_str0_) == "pdf"
641 || String (opt->longname_str0_) == "png"
642 || String (opt->longname_str0_) == "ps"
643 || String (opt->longname_str0_) == "tex")
644 add_output_format (opt->longname_str0_);
645 else if (String (opt->longname_str0_) == "preview")
647 else if (String (opt->longname_str0_) == "no-pages")
653 String arg (option_parser->optional_argument_str0_);
654 int eq = arg.index ('=');
661 key = arg.left_string (eq);
662 val = arg.right_string (arg.length () - eq - 1);
665 init_scheme_variables
666 += "(cons \'" + key + " " + val + ")\n";
676 String s = option_parser->optional_argument_str0_;
677 File_name file_name (s);
678 output_name_global = file_name.to_string ();
682 jail_spec = option_parser->optional_argument_str0_;
685 init_scheme_code_string += option_parser->optional_argument_str0_;
693 output_backend_global = option_parser->optional_argument_str0_;
697 output_format_global = option_parser->optional_argument_str0_;
701 dump_header_fieldnames_global
702 .push (option_parser->optional_argument_str0_);
705 global_path.append (option_parser->optional_argument_str0_);
708 init_name_global = option_parser->optional_argument_str0_;
714 be_verbose_global = true;
717 be_safe_global = true;
723 programming_error (to_string ("unhandled short option: %c",
724 opt->shortname_char_));
730 if (output_format_global == "")
731 output_format_global = "pdf";
737 if (be_verbose_global)
746 char *yield = getenv ("LILYPOND_GC_YIELD");
747 bool overwrite = true;
754 sane_putenv ("GUILE_MIN_YIELD_1", yield, overwrite);
755 sane_putenv ("GUILE_MIN_YIELD_2", yield, overwrite);
756 sane_putenv ("GUILE_MIN_YIELD_MALLOC", yield, overwrite);
760 main (int argc, char **argv)
762 setup_localisation ();
763 setup_paths (argv[0]);
765 parse_argv (argc, argv);
766 if (isatty (STDIN_FILENO))
769 scm_boot_guile (argc, argv, main_with_guile, 0);
771 /* Only reachable if GUILE exits. That is an error. */