2 main.cc -- implement main () entrypoint.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
18 #include <sys/types.h>
31 #include "all-font-metrics.hh"
32 #include "file-name.hh"
33 #include "freetype.hh"
34 #include "getopt-long.hh"
35 #include "global-ctor.hh"
36 #include "international.hh"
37 #include "lily-version.hh"
39 #include "output-def.hh"
40 #include "program-option.hh"
41 #include "relocate.hh"
42 #include "string-convert.hh"
47 * Global options that can be overridden through command line.
50 /* Names of header fields to be dumped to a separate file. */
51 vector<string> dump_header_fieldnames_global;
53 /* Name of initialisation file. */
54 string init_name_global;
57 /* Output formats to generate. */
58 string output_format_global = "";
60 bool is_pango_format_global;
61 bool is_TeX_format_global;
63 /* Current output name. */
64 string output_name_global;
66 /* Run in safe mode? */
67 bool be_safe_global = false;
69 /* Provide URI links to the original file */
70 bool point_and_click_global = true;
72 /* Verbose progress indication? */
73 bool be_verbose_global = false;
75 /* Scheme code to execute before parsing, after .scm init.
76 This is where -e arguments are appended to. */
77 string init_scheme_code_global;
78 string init_scheme_variables_global;
80 /* Generate preview of first system. */
81 bool make_preview = false;
83 /* Generate printed output. */
84 bool make_print = true;
87 bool relocate_binary =
97 * Miscellaneous global stuff.
99 File_path global_path;
105 static char const *AUTHORS
106 = " Han-Wen Nienhuys <hanwen@xs4all.nl>\n"
107 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
109 static char const *PROGRAM_NAME = "lilypond";
110 static char const *PROGRAM_URL = "http://lilypond.org";
112 static char const *NOTICE
113 = _i ("This program is free software. It is covered by the GNU General Public\n"
114 "License and you are welcome to change it and/or distribute copies of it\n"
115 "under certain conditions. Invoke as `%s --warranty' for more\n"
118 static char const *WARRANTY
119 = _i (" This program is free software; you can redistribute it and/or\n"
120 "modify it under the terms of the GNU General Public License version 2\n"
121 "as published by the Free Software Foundation.\n"
123 " This program is distributed in the hope that it will be useful,\n"
124 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
125 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
126 "General Public License for more details.\n"
128 " You should have received a copy of the\n"
129 "GNU General Public License along with this program; if not, write to\n"
130 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
131 "Boston, MA 02111-1307, USA.\n");
133 /* Where the init files live. Typically:
134 LILYPOND_DATADIR = /usr/share/lilypond
136 string lilypond_datadir;
138 /* The jail specification: USER, GROUP, JAIL, DIR. */
141 /* The option parser */
142 static Getopt_long *option_parser = 0;
144 /* Internationalisation kludge in two steps:
145 * use _i () to get entry in POT file
146 * call gettext () explicitely for actual "translation" */
148 static Long_option_init options_static[]
150 {_i ("SYM[=VAL]"), "define-default", 'd',
151 _i ("set Scheme option SYM to VAL (default: #t).\n"
152 "Use -dhelp for help.")},
154 {_i ("EXPR"), "evaluate", 'e', _i ("evaluate scheme code")},
155 /* Bug in option parser: --output =foe is taken as an abbreviation
156 for --output-format. */
157 {_i ("FORMATs"), "formats", 'f', _i ("dump FORMAT,... Also as separate options:")},
158 {0, "dvi", 0, _i ("generate DVI (tex backend only)")},
159 {0, "pdf", 0, _i ("generate PDF (default)")},
160 {0, "png", 0, _i ("generate PNG")},
161 {0, "ps", 0, _i ("generate PostScript")},
162 {0, "tex", 0, _i ("generate TeX (tex backend only)")},
163 {0, "help", 'h', _i ("show this help and exit")},
164 {_i ("FIELD"), "header", 'H', _i ("dump header field FIELD to file\n"
165 "named BASENAME.FIELD")},
166 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
167 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
169 {_i ("USER, GROUP, JAIL, DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP\n"
172 {0, "no-print", 0, _i ("do not generate printed output")},
173 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
174 {0, "preview", 'p', _i ("generate a preview of the first system")},
175 {0, "relocate", 0, _i ("relocate using directory of lilypond program")},
176 {0, "version", 'v', _i ("show version number and exit")},
177 {0, "verbose", 'V', _i ("be verbose")},
178 {0, "warranty", 'w', _i ("show warranty and copyright")},
182 char const *LILYPOND_DATADIR = PACKAGE_DATADIR "/" TOPLEVEL_VERSION;
185 env_var_info (FILE *out, char const *key)
187 if (char const *value = getenv (key))
188 fprintf (out, "%s=\"%s\"\n", key, value);
195 fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
196 env_var_info (out, "LILYPONDPREFIX");
197 env_var_info (out, "LILYPOND_DATADIR");
198 fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
200 fprintf (out, "\nEffective prefix: \"%s\"\n", lilypond_datadir.c_str ());
204 env_var_info (out, "FONTCONFIG_FILE");
205 env_var_info (out, "FONTCONFIG_PATH");
206 env_var_info (out, "GS_FONTPATH");
207 env_var_info (out, "GS_LIB");
208 env_var_info (out, "GUILE_LOAD_PATH");
209 env_var_info (out, "PANGO_RC_FILE");
210 env_var_info (out, "PANGO_PREFIX");
211 env_var_info (out, "PATH");
218 printf (_f ("Copyright (c) %s by\n%s and others.",
227 fputs (gnu_lilypond_version_string ().c_str (), out);
235 puts (_f (NOTICE, PROGRAM_NAME).c_str ());
240 LY_DEFINE (ly_usage, "ly:usage",
242 "Print usage message.")
244 /* No version number or newline here. It confuses help2man. */
245 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).c_str ());
247 printf (_ ("Typeset music and/or produce MIDI from FILE.").c_str ());
249 printf (_ ("LilyPond produces beautiful music notation.").c_str ());
251 printf (_f ("For more information, see %s", PROGRAM_URL).c_str ());
253 printf (_ ("Options:").c_str ());
255 printf (Long_option_init::table_string (options_static).c_str ());
257 printf (_f ("Report bugs via %s",
258 "http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs"
262 return SCM_UNSPECIFIED;
272 printf (_ (WARRANTY).c_str ());
276 prepend_load_path (string dir)
278 string s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
279 scm_c_eval_string (s.c_str ());
282 void init_global_tweak_registry ();
283 void init_fontconfig ();
289 /* Now we chroot, setuid/setgrp and chdir. If something goes wrong,
290 we exit (this is a security-sensitive area). First we split
291 jail_spec into its components, then we retrieve the user/group id
292 (necessarily *before* chroot'ing) and finally we perform the
297 USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
300 vector<string> components = string_split (jail_spec, ',');
301 if (components.size () != JAIL_MAX)
303 error (_f ("expected %d arguments with jail, found: %u", JAIL_MAX,
304 (unsigned) components.size ()));
312 if (passwd * passwd = getpwnam (components[USER_NAME].c_str ()))
313 uid = passwd->pw_uid;
317 error (_f ("no such user: %s", components[USER_NAME]));
319 error (_f ("cannot get user id from user name: %s: %s",
320 components[USER_NAME],
329 if (group * group = getgrnam (components[GROUP_NAME].c_str ()))
334 error (_f ("no such group: %s", components[GROUP_NAME]));
336 error (_f ("cannot get group id from group name: %s: %s",
337 components[GROUP_NAME],
342 if (chroot (components[JAIL].c_str ()))
344 error (_f ("cannot chroot to: %s: %s", components[JAIL],
351 error (_f ("cannot change group id to: %d: %s", gid, strerror (errno)));
357 error (_f ("cannot change user id to: %d: %s", uid, strerror (errno)));
361 if (chdir (components[DIR].c_str ()))
363 error (_f ("cannot change working directory to: %s: %s", components[DIR],
371 main_with_guile (void *, int, char **)
373 /* Engravers use lily.scm contents, need to make Guile find it.
374 Prepend onto GUILE %load-path, very ugh. */
376 prepend_load_path (lilypond_datadir);
377 prepend_load_path (lilypond_datadir + "/scm");
379 if (be_verbose_global)
382 is_pango_format_global = !is_TeX_format_global;
383 init_scheme_variables_global = "(list " + init_scheme_variables_global + ")";
384 init_scheme_code_global = "(begin " + init_scheme_code_global + ")";
387 call_constructors ();
391 ly_reset_all_fonts ();
393 is_TeX_format_global = (get_output_backend_name () == "tex"
394 || get_output_backend_name () == "texstr");
397 /* We accept multiple independent music files on the command line to
398 reduce compile time when processing lots of small files.
399 Starting the GUILE engine is very time consuming. */
403 while (char const *arg = option_parser->get_next_arg ())
405 *tail = scm_cons (scm_from_locale_string (arg), SCM_EOL);
406 tail = SCM_CDRLOC (*tail);
409 delete option_parser;
413 if (!jail_spec.empty ())
417 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
425 setup_localisation ()
429 setlocale (LC_ALL, "");
431 /* FIXME: check if this is still true.
432 Disable localisation of float values. This breaks TeX output. */
433 setlocale (LC_NUMERIC, "C");
435 string localedir = LOCALEDIR;
436 if (char const *env = getenv ("LILYPOND_LOCALEDIR"))
439 bindtextdomain ("lilypond", localedir.c_str ());
440 textdomain ("lilypond");
445 add_output_format (string format)
447 if (output_format_global != "")
448 output_format_global += ",";
449 output_format_global += format;
453 parse_argv (int argc, char **argv)
455 bool show_help = false;
456 option_parser = new Getopt_long (argc, argv, options_static);
457 while (Long_option_init const *opt = (*option_parser) ())
459 switch (opt->shortname_char_)
462 if (string (opt->longname_str0_) == "dvi"
463 || string (opt->longname_str0_) == "pdf"
464 || string (opt->longname_str0_) == "png"
465 || string (opt->longname_str0_) == "ps"
466 || string (opt->longname_str0_) == "tex")
467 add_output_format (opt->longname_str0_);
468 else if (string (opt->longname_str0_) == "preview")
470 else if (string (opt->longname_str0_) == "no-pages")
472 else if (string (opt->longname_str0_) == "relocate")
473 relocate_binary = true;
478 string arg (option_parser->optional_argument_str0_);
479 ssize eq = arg.find ('=');
486 key = arg.substr (0, eq);
487 val = arg.substr (eq + 1, arg.length () - 1);
490 init_scheme_variables_global
491 += "(cons \'" + key + " '" + val + ")\n";
501 string s = option_parser->optional_argument_str0_;
502 File_name file_name (s);
503 output_name_global = file_name.to_string ();
507 jail_spec = option_parser->optional_argument_str0_;
511 init_scheme_code_global += option_parser->optional_argument_str0_ + string (" ");
520 vector<string> components
521 = string_split (option_parser->optional_argument_str0_, ',');
522 for (vsize i = 0; i < components.size (); i++)
523 add_output_format (components[i]);
528 dump_header_fieldnames_global
529 .push_back (option_parser->optional_argument_str0_);
532 global_path.append (option_parser->optional_argument_str0_);
535 init_name_global = option_parser->optional_argument_str0_;
541 be_verbose_global = true;
547 programming_error (to_string ("unhandled short option: %c",
548 opt->shortname_char_));
554 if (output_format_global == "")
555 output_format_global = "pdf";
561 if (be_verbose_global)
570 const char *yield = getenv ("LILYPOND_GC_YIELD");
571 bool overwrite = true;
578 sane_putenv ("GUILE_MIN_YIELD_1", yield, overwrite);
579 sane_putenv ("GUILE_MIN_YIELD_2", yield, overwrite);
580 sane_putenv ("GUILE_MIN_YIELD_MALLOC", yield, overwrite);
583 sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1",
584 "10485760", overwrite);
585 sane_putenv ("GUILE_MAX_SEGMENT_SIZE",
586 "104857600", overwrite);
590 main (int argc, char **argv)
592 if (getenv ("LILYPOND_VERBOSE"))
593 be_verbose_global = true;
595 setup_localisation ();
596 parse_argv (argc, argv);
597 if (isatty (STDIN_FILENO))
600 setup_paths (argv[0]);
607 scm_boot_guile (argc, argv, main_with_guile, 0);
611 error (_f ("exception caught: %s", e.what ()));
614 scm_boot_guile (argc, argv, main_with_guile, 0);
617 /* Only reachable if GUILE exits. That is an error. */
621 SCM atexit_list = SCM_EOL;
623 LY_DEFINE (ly_atexit, "ly:atexit",
624 2, 0, 0, (SCM proc, SCM args),
625 "Just before exiting, call the procedure given. "
626 "If this is called multiple times, the procedures are called "
629 atexit_list = scm_cons (scm_cons (proc, args), atexit_list);
630 scm_gc_protect_object (atexit_list);
631 return SCM_UNSPECIFIED;
634 LY_DEFINE (ly_do_atexit, "ly:do-atexit",
636 "Call the atexit procedures.")
638 for (SCM s = atexit_list; scm_is_pair (s); s = scm_cdr (s))
639 scm_apply_0 (scm_caar (s), scm_cdar (s));
640 return SCM_UNSPECIFIED;