2 main.cc -- implement main () entrypoint.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 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 "lily-guile.hh"
37 #include "lily-version.hh"
39 #include "output-def.hh"
40 #include "string-convert.hh"
43 #include "program-option.hh"
44 #include "relocate.hh"
47 * Global options that can be overridden through command line.
50 /* Names of header fields to be dumped to a separate file. */
51 Array<String> dump_header_fieldnames_global;
53 /* Name of initialisation file. */
54 String init_name_global;
56 /* Selected output backend
57 One of (gnome, ps [default], eps, scm, svg, tex, texstr)") */
58 String output_backend_global = "ps";
59 /* Output formats to generate. */
60 String output_format_global = "";
62 bool is_pango_format_global;
63 bool is_TeX_format_global;
65 /* Current output name. */
66 String output_name_global;
68 /* Run in safe mode? */
69 bool be_safe_global = false;
71 /* Provide URI links to the original file */
72 bool point_and_click_global = true;
74 /* Verbose progress indication? */
75 bool be_verbose_global = false;
77 /* Scheme code to execute before parsing, after .scm init.
78 This is where -e arguments are appended to. */
79 String init_scheme_code_string;
80 String init_scheme_variables;
82 /* Generate preview of first system. */
83 bool make_preview = false;
85 /* Generate printed output. */
86 bool make_print = true;
89 bool relocate_binary =
99 * Miscellaneous global stuff.
101 File_path global_path;
107 static char const *AUTHORS
108 = " Han-Wen Nienhuys <hanwen@xs4all.nl>\n"
109 " Jan Nieuwenhuizen <janneke@gnu.org>\n";
111 static char const *PROGRAM_NAME = "lilypond";
112 static char const *PROGRAM_URL = "http://lilypond.org";
114 static char const *NOTICE
115 = _i ("This program is free software. It is covered by the GNU General Public\n"
116 "License and you are welcome to change it and/or distribute copies of it\n"
117 "under certain conditions. Invoke as `%s --warranty' for more\n"
120 static char const *WARRANTY
121 = _i (" This program is free software; you can redistribute it and/or\n"
122 "modify it under the terms of the GNU General Public License version 2\n"
123 "as published by the Free Software Foundation.\n"
125 " This program is distributed in the hope that it will be useful,\n"
126 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
127 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
128 "General Public License for more details.\n"
130 " You should have received a copy (refer to the file COPYING) of the\n"
131 "GNU General Public License along with this program; if not, write to\n"
132 "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
133 "Boston, MA 02111-1307, USA.\n");
135 /* Where the init files live. Typically:
136 LILYPOND_DATADIR = /usr/share/lilypond
138 String prefix_directory;
140 /* The jail specification: USER,GROUP,JAIL,DIR. */
143 /* The option parser */
144 static Getopt_long *option_parser = 0;
146 /* Internationalisation kludge in two steps:
147 * use _i () to get entry in POT file
148 * call gettext () explicitely for actual "translation" */
150 static Long_option_init options_static[]
152 {_i ("BACK"), "backend", 'b', _i ("use backend BACK (gnome, ps,eps,\nscm, svg, tex, texstr)\ndefault: PS")},
154 {_i ("SYM=VAL"), "define-default", 'd',
155 _i ("set a Scheme program option. Uses #t if VAL is not specified\n"
156 "Try -dhelp for help.")},
158 {_i ("EXPR"), "evaluate", 'e', _i ("evaluate scheme code")},
159 /* Bug in option parser: --output =foe is taken as an abbreviation
160 for --output-format. */
161 {_i ("FORMATs"), "formats", 'f', _i ("dump FORMAT,... Also as separate options:")},
162 {0, "dvi", 0, _i ("generate DVI (tex backend only)")},
163 {0, "relocate", 0, _i ("relocate using directory of lilypond program")},
164 {0, "pdf", 0, _i ("generate PDF (default)")},
165 {0, "png", 0, _i ("generate PNG")},
166 {0, "ps", 0, _i ("generate PostScript")},
167 {0, "tex", 0, _i ("generate TeX (tex backend only)")},
168 {0, "help", 'h', _i ("print this help")},
169 {_i ("FIELD"), "header", 'H', _i ("dump a header field to file BASENAME.FIELD")},
170 {_i ("DIR"), "include", 'I', _i ("add DIR to search path")},
171 {_i ("FILE"), "init", 'i', _i ("use FILE as init file")},
173 {_i ("USER,GROUP,JAIL,DIR"), "jail", 'j', _i ("chroot to JAIL, become USER:GROUP\n"
176 {0, "no-print", 0, _i ("do not generate printed output")},
177 {_i ("FILE"), "output", 'o', _i ("write output to FILE (suffix will be added)")},
178 {0, "preview", 'p', _i ("generate a preview of the first system")},
179 {0, "safe-mode", 's', _i ("disallow unsafe Scheme and PostScript operations")},
180 {0, "version", 'v', _i ("print version number")},
181 {0, "verbose", 'V', _i ("be verbose")},
182 {0, "warranty", 'w', _i ("show warranty and copyright")},
186 const char *LILYPOND_DATADIR = PACKAGE_DATADIR "/" TOPLEVEL_VERSION;
189 env_var_info (FILE *out, char const *key)
191 if (char const *value = getenv (key))
192 fprintf (out, "%s=\"%s\"\n", key, value);
199 fprintf (out, "LILYPOND_DATADIR=\"%s\"\n", LILYPOND_DATADIR);
200 env_var_info (out, "LILYPONDPREFIX");
201 fprintf (out, "LOCALEDIR=\"%s\"\n", LOCALEDIR);
203 fprintf (out, "\nEffective prefix: \"%s\"\n", prefix_directory.to_str0 ());
207 env_var_info (out, "FONTCONFIG_FILE");
208 env_var_info (out, "FONTCONFIG_PATH");
209 env_var_info (out, "GS_FONTPATH");
210 env_var_info (out, "GS_LIB");
211 env_var_info (out, "GUILE_LOAD_PATH");
212 env_var_info (out, "PANGO_RC_FILE");
213 env_var_info (out, "PATH");
220 printf (_f ("Copyright (c) %s by\n%s and others.",
222 AUTHORS).to_str0 ());
229 fputs (gnu_lilypond_version_string ().to_str0 (), out);
237 puts (_f (NOTICE, PROGRAM_NAME).to_str0 ());
242 LY_DEFINE (ly_usage, "ly:usage",
244 "Print usage message.")
246 /* No version number or newline here. It confuses help2man. */
247 printf (_f ("Usage: %s [OPTION]... FILE...", PROGRAM_NAME).to_str0 ());
249 printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
251 printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
253 printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
255 printf (_ ("Options:").to_str0 ());
257 printf (Long_option_init::table_string (options_static).to_str0 ());
259 printf (_f ("Report bugs via %s",
260 "http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs"
264 return SCM_UNSPECIFIED;
274 printf (_ (WARRANTY).to_str0 ());
278 prepend_load_path (String dir)
280 String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
281 scm_c_eval_string (s.to_str0 ());
284 void init_global_tweak_registry ();
285 void init_fontconfig ();
291 /* Now we chroot, setuid/setgrp and chdir. If something goes wrong,
292 we exit (this is a security-sensitive area). First we split
293 jail_spec into its components, then we retrieve the user/group id
294 (necessarily *before* chroot'ing) and finally we perform the
299 USER_NAME, GROUP_NAME, JAIL, DIR, JAIL_MAX
302 Array<String> components = String_convert::split (jail_spec, ',');
303 if (components.size () != JAIL_MAX)
305 error (_f ("expected %d arguments with jail, found: %d", JAIL_MAX,
306 components.size ()));
314 if (passwd * passwd = getpwnam (components[USER_NAME].to_str0 ()))
315 uid = passwd->pw_uid;
319 error (_f ("no such user: %s", components[USER_NAME]));
321 error (_f ("can't get user id from user name: %s: %s",
322 components[USER_NAME],
331 if (group * group = getgrnam (components[GROUP_NAME].to_str0 ()))
336 error (_f ("no such group: %s", components[GROUP_NAME]));
338 error (_f ("can't get group id from group name: %s: %s",
339 components[GROUP_NAME],
344 if (chroot (components[JAIL].to_str0 ()))
346 error (_f ("can't chroot to: %s: %s", components[JAIL],
353 error (_f ("can't change group id to: %d: %s", gid, strerror (errno)));
359 error (_f ("can't change user id to: %d: %s", uid, strerror (errno)));
363 if (chdir (components[DIR].to_str0 ()))
365 error (_f ("can't change working directory to: %s: %s", components[DIR],
373 main_with_guile (void *, int, char **)
375 /* Engravers use lily.scm contents, need to make Guile find it.
376 Prepend onto GUILE %load-path, very ugh. */
378 prepend_load_path (prefix_directory);
379 prepend_load_path (prefix_directory + "/scm");
381 if (be_verbose_global)
383 is_TeX_format_global = (output_backend_global == "tex"
384 || output_backend_global == "texstr");
386 is_pango_format_global = !is_TeX_format_global;
389 call_constructors ();
390 init_global_tweak_registry ();
395 all_fonts_global = new All_font_metrics (global_path.to_string ());
397 if (!init_scheme_variables.is_empty ()
398 || !init_scheme_code_string.is_empty ())
400 init_scheme_variables = "(map (lambda (x) (ly:set-option (car x) (cdr x))) (list "
401 + init_scheme_variables + "))";
403 init_scheme_code_string
405 + init_scheme_variables
406 + init_scheme_code_string
409 char const *str0 = init_scheme_code_string.to_str0 ();
411 if (be_verbose_global)
412 progress_indication (_f ("Evaluating %s", str0));
413 scm_c_eval_string ((char *) str0);
416 /* We accept multiple independent music files on the command line to
417 reduce compile time when processing lots of small files.
418 Starting the GUILE engine is very time consuming. */
422 while (char const *arg = option_parser->get_next_arg ())
424 *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
425 tail = SCM_CDRLOC (*tail);
427 delete option_parser;
431 if (!jail_spec.is_empty ())
435 SCM result = scm_call_1 (ly_lily_module_constant ("lilypond-main"), files);
443 setup_localisation ()
447 setlocale (LC_ALL, "");
449 /* FIXME: check if this is still true.
450 Disable localisation of float values. This breaks TeX output. */
451 setlocale (LC_NUMERIC, "C");
453 String localedir = LOCALEDIR;
454 if (char const *env = getenv ("LILYPOND_LOCALEDIR"))
457 bindtextdomain ("lilypond", localedir.to_str0 ());
458 textdomain ("lilypond");
463 add_output_format (String format)
465 if (output_format_global != "")
466 output_format_global += ",";
467 output_format_global += format;
471 parse_argv (int argc, char **argv)
473 bool show_help = false;
474 option_parser = new Getopt_long (argc, argv, options_static);
475 while (Long_option_init const *opt = (*option_parser) ())
477 switch (opt->shortname_char_)
480 if (String (opt->longname_str0_) == "dvi"
481 || String (opt->longname_str0_) == "pdf"
482 || String (opt->longname_str0_) == "png"
483 || String (opt->longname_str0_) == "ps"
484 || String (opt->longname_str0_) == "tex")
485 add_output_format (opt->longname_str0_);
486 else if (String (opt->longname_str0_) == "preview")
488 else if (String (opt->longname_str0_) == "no-pages")
490 else if (String (opt->longname_str0_) == "relocate")
491 relocate_binary = true;
496 String arg (option_parser->optional_argument_str0_);
497 int eq = arg.index ('=');
504 key = arg.left_string (eq);
505 val = arg.right_string (arg.length () - eq - 1);
508 init_scheme_variables
509 += "(cons \'" + key + " " + val + ")\n";
519 String s = option_parser->optional_argument_str0_;
520 File_name file_name (s);
521 output_name_global = file_name.to_string ();
525 jail_spec = option_parser->optional_argument_str0_;
528 init_scheme_code_string += option_parser->optional_argument_str0_;
536 output_backend_global = option_parser->optional_argument_str0_;
540 output_format_global = option_parser->optional_argument_str0_;
544 dump_header_fieldnames_global
545 .push (option_parser->optional_argument_str0_);
548 global_path.append (option_parser->optional_argument_str0_);
551 init_name_global = option_parser->optional_argument_str0_;
557 be_verbose_global = true;
560 be_safe_global = true;
566 programming_error (to_string ("unhandled short option: %c",
567 opt->shortname_char_));
573 if (output_format_global == "")
574 output_format_global = "pdf";
580 if (be_verbose_global)
589 char *yield = getenv ("LILYPOND_GC_YIELD");
590 bool overwrite = true;
597 sane_putenv ("GUILE_MIN_YIELD_1", yield, overwrite);
598 sane_putenv ("GUILE_MIN_YIELD_2", yield, overwrite);
599 sane_putenv ("GUILE_MIN_YIELD_MALLOC", yield, overwrite);
603 main (int argc, char **argv)
605 if (getenv ("LILYPOND_VERBOSE"))
606 be_verbose_global = true;
608 setup_localisation ();
609 parse_argv (argc, argv);
610 if (isatty (STDIN_FILENO))
613 setup_paths (argv[0]);
615 scm_boot_guile (argc, argv, main_with_guile, 0);
617 /* Only reachable if GUILE exits. That is an error. */